+ if(argn==2)
+ break;
+ }
+ closedir(_dir);
+}
+
+#include "../gfxdevice.h"
+#include "../pdf/pdf.h"
+
+static int max_segments = 0;
+static int max_any_segments = 0;
+void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
+{
+ //gfxpoly_t*c = gfxpoly_from_fill(line, 0.05);
+ //gfxpoly_free(c);
+
+ //gfxpoly_t*poly1 = gfxpoly_from_fill(line, 0.05);
+ gfxpoly_t*poly1 = gfxpoly_from_fill(line, 0.05);
+
+ //gfxline_dump(line, stderr, "");
+ //gfxpoly_dump(poly);
+
+ int size = gfxpoly_size(poly1);
+ if(size == 4) {
+ //rectangles are boring.
+ gfxpoly_destroy(poly1);
+ return;
+ }
+
+ max_any_segments = size > max_any_segments? size : max_any_segments;
+ if(size>100000) {
+ fprintf(stderr, "%d segments (skipping)\n", size);
+ return;
+ } else {
+ max_segments = size > max_segments? size : max_segments;
+ fprintf(stderr, "%d segments (max so far: %d/%d)\n", size, max_segments, max_any_segments);
+ }
+
+ if(!gfxpoly_check(poly1)) {
+ gfxpoly_destroy(poly1);
+ fprintf(stderr, "bad polygon\n");
+ return;
+ }
+
+ windrule_t*rule = &windrule_evenodd;
+
+ double zoom = 1.0;
+ intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
+ unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
+ if(!bitmap_ok(&bbox, bitmap1)) {
+ fprintf(stderr, "bad polygon or error in renderer\n");
+ return;
+ }
+ gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
+ unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
+ if(!bitmap_ok(&bbox, bitmap2)) {
+ save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+ assert(!"error in bitmap");
+ }
+ if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
+ save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+ assert(!"bitmaps don't match");
+ }
+ free(bitmap1);
+ free(bitmap2);
+
+ gfxpoly_destroy(poly1);
+ gfxpoly_destroy(poly2);
+}
+int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
+ return 0;
+}
+void extract_polygons_startclip(gfxdevice_t*dev, gfxline_t*line)
+{
+ extract_polygons_fill(dev, line, 0);
+}
+void extract_polygons_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*imgcoord2devcoord, gfxcxform_t*cxform)
+{
+ extract_polygons_fill(dev, line, 0);
+}
+void extract_polygons_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*gradcoord2devcoord)
+{
+ extract_polygons_fill(dev, line, 0);
+}
+void extract_polygons_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
+{
+ extract_polygons_fill(dev, line, 0);
+}
+void extract_polygons_addfont(gfxdevice_t*dev, gfxfont_t*font)
+{
+ int t;
+ for(t=0;t<font->num_glyphs;t++) {
+ //extract_polygons_fill(dev, font->glyphs[t].line, 0);
+ }
+}
+void extract_polygons_endclip(gfxdevice_t*dev)
+{
+}
+void extract_polygons_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
+{
+}
+void extract_polygons_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
+{
+}
+
+gfxdevice_t extract_polygons =
+{
+name: "extract polygons",
+setparameter:extract_polygons_setparameter,
+startclip: extract_polygons_startclip,
+endclip: extract_polygons_endclip,
+stroke: extract_polygons_stroke,
+fill: extract_polygons_fill,
+fillbitmap: extract_polygons_fillbitmap,
+fillgradient: extract_polygons_fillgradient,
+addfont: extract_polygons_addfont,
+drawchar: extract_polygons_drawchar,
+drawlink: extract_polygons_drawlink,
+startpage: 0,
+endpage: 0,
+geterror: 0,
+finish: 0,
+internal: 0
+};
+
+void test5(int argn, char*argv[])
+{
+ gfxsource_t*driver = gfxsource_pdf_create();
+ char*dir = "pdfs";
+ DIR*_dir = opendir(dir);
+ if(!_dir) return;
+ struct dirent*file;
+ while(1) {
+ file = readdir(_dir);
+ if (!file)
+ break;
+ if(!strstr(file->d_name, ".pdf"))
+ continue;
+ char* filename = allocprintf("%s/%s", dir, file->d_name);
+
+ if(argn>1)
+ filename = argv[1];
+
+ gfxdocument_t*doc = driver->open(driver, filename);
+ gfxdevice_t*out = &extract_polygons;
+ int t;
+ for(t=1;t<=doc->num_pages;t++) {
+ fprintf(stderr, "%s (page %d)\n", filename, t);
+ gfxpage_t* page = doc->getpage(doc, t);
+ page->render(page, out);
+ page->destroy(page);
+ }
+ doc->destroy(doc);
+ if(argn>1)
+ break;
+ free(filename);