main(int argc, char **argv) { char *dirin, *substr, *title, *fileout, *boxaafile, *boxaapath; l_int32 ret, res, type, thresh; l_float32 scalefactor; BOXAA *baa; static char mainName[] = "convertsegfilestopdf"; if (argc != 10) { fprintf(stderr, " Syntax: convertsegfilestopdf dirin substr res type thresh \\ \n" " boxaafile scalefactor title fileout\n" " where\n" " dirin: input directory for image files\n" " substr: Use 'allfiles' to convert all files\n" " in the directory\n" " res: Input resolution of each image;\n" " assumed to all be the same\n" " type: compression used for non-image regions:\n" " 0: default (G4 encoding)\n" " 1: JPEG encoding\n" " 2: G4 encoding\n" " 3: PNG encoding\n" " thresh: threshold for binarization; use 0 for default\n" " boxaafile: File of 'image' regions within each page\n" " This contains a boxa for each page,\n" " consisting of a set of regions\n" " scalefactor: Use to scale down the image regions\n" " title: Use 'none' to omit\n" " fileout: Output pdf file\n"); return 1; } dirin = argv[1]; substr = argv[2]; res = atoi(argv[3]); type = atoi(argv[4]); thresh = atoi(argv[5]); boxaafile = argv[6]; scalefactor = atof(argv[7]); title = argv[8]; fileout = argv[9]; if (!strcmp(substr, "allfiles")) substr = NULL; if (scalefactor <= 0.0 || scalefactor > 1.0) { L_WARNING("invalid scalefactor: setting to 1.0", mainName); scalefactor = 1.0; } if (type != 1 && type != 2 && type != 3) type = L_G4_ENCODE; if (thresh <= 0) thresh = 150; if (!strcmp(title, "none")) title = NULL; boxaapath = genPathname(boxaafile, NULL); if ((baa = boxaaRead(boxaapath)) == NULL) { L_WARNING( "boxaa file not found; converting unsegmented and unscaled", mainName); ret = convertFilesToPdf(dirin, substr, res, 1.0, 75, title, fileout); FREE(boxaapath); return ret; } ret = convertSegmentedFilesToPdf(dirin, substr, res, type, thresh, baa, 75, scalefactor, title, fileout); FREE(boxaapath); boxaaDestroy(&baa); return ret; }
main(int argc, char **argv) { l_int32 h; l_float32 scalefactor; BOX *box; BOXA *boxa1, *boxa2; BOXAA *baa; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8, *pix9; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; lept_rmdir("segtest"); lept_mkdir("segtest"); baa = boxaaCreate(5); /* Image region input. */ pix1 = pixRead("wet-day.jpg"); pix2 = pixScaleToSize(pix1, WIDTH, 0); pixWrite("/tmp/segtest/0.jpg", pix2, IFF_JFIF_JPEG); regTestCheckFile(rp, "/tmp/segtest/0.jpg"); /* 0 */ box = boxCreate(105, 161, 620, 872); /* image region */ boxa1 = boxaCreate(1); boxaAddBox(boxa1, box, L_INSERT); boxaaAddBoxa(baa, boxa1, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); /* Compute image region at w = 2 * WIDTH */ pix1 = pixRead("candelabrum-11.jpg"); pix2 = pixScaleToSize(pix1, WIDTH, 0); pix3 = pixConvertTo1(pix2, 100); pix4 = pixExpandBinaryPower2(pix3, 2); /* w = 2 * WIDTH */ pix5 = pixGenHalftoneMask(pix4, NULL, NULL, 1); pix6 = pixMorphSequence(pix5, "c20.1 + c1.20", 0); pix7 = pixMaskConnComp(pix6, 8, &boxa1); pix8 = pixReduceBinary2(pix7, NULL); /* back to w = WIDTH */ pix9 = pixBackgroundNormSimple(pix2, pix8, NULL); pixWrite("/tmp/segtest/1.jpg", pix9, IFF_JFIF_JPEG); regTestCheckFile(rp, "/tmp/segtest/1.jpg"); /* 1 */ boxa2 = boxaTransform(boxa1, 0, 0, 0.5, 0.5); /* back to w = WIDTH */ boxaaAddBoxa(baa, boxa2, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); pixDestroy(&pix9); boxaDestroy(&boxa1); /* Use mask to find image region */ pix1 = pixRead("lion-page.00016.jpg"); pix2 = pixScaleToSize(pix1, WIDTH, 0); pixWrite("/tmp/segtest/2.jpg", pix2, IFF_JFIF_JPEG); regTestCheckFile(rp, "/tmp/segtest/2.jpg"); /* 2 */ pix3 = pixRead("lion-mask.00016.tif"); pix4 = pixScaleToSize(pix3, WIDTH, 0); boxa1 = pixConnComp(pix4, NULL, 8); boxaaAddBoxa(baa, boxa1, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); /* Compute image region at full res */ pix1 = pixRead("rabi.png"); scalefactor = (l_float32)WIDTH / (l_float32)pixGetWidth(pix1); pix2 = pixScaleToGray(pix1, scalefactor); pixWrite("/tmp/segtest/3.jpg", pix2, IFF_JFIF_JPEG); regTestCheckFile(rp, "/tmp/segtest/3.jpg"); /* 3 */ pix3 = pixGenHalftoneMask(pix1, NULL, NULL, 0); pix4 = pixMorphSequence(pix3, "c20.1 + c1.20", 0); boxa1 = pixConnComp(pix4, NULL, 8); boxa2 = boxaTransform(boxa1, 0, 0, scalefactor, scalefactor); boxaaAddBoxa(baa, boxa2, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); boxaDestroy(&boxa1); /* Page with no image regions */ pix1 = pixRead("lucasta-47.jpg"); pix2 = pixScaleToSize(pix1, WIDTH, 0); boxa1 = boxaCreate(1); pixWrite("/tmp/segtest/4.jpg", pix2, IFF_JFIF_JPEG); regTestCheckFile(rp, "/tmp/segtest/4.jpg"); /* 4 */ boxaaAddBoxa(baa, boxa1, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); /* Page that is all image */ pix1 = pixRead("map1.jpg"); pix2 = pixScaleToSize(pix1, WIDTH, 0); pixWrite("/tmp/segtest/5.jpg", pix2, IFF_JFIF_JPEG); regTestCheckFile(rp, "/tmp/segtest/5.jpg"); /* 5 */ h = pixGetHeight(pix2); box = boxCreate(0, 0, WIDTH, h); boxa1 = boxaCreate(1); boxaAddBox(boxa1, box, L_INSERT); boxaaAddBoxa(baa, boxa1, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); /* Save the boxaa file */ boxaaWrite("/tmp/segtest/seg.baa", baa); regTestCheckFile(rp, "/tmp/segtest/seg.baa"); /* 6 */ /* Do the conversion */ l_pdfSetDateAndVersion(FALSE); convertSegmentedFilesToPdf("/tmp/segtest", ".jpg", 100, L_G4_ENCODE, 140, baa, 75, 0.6, "Segmentation Test", "/tmp/pdfseg.7.pdf"); regTestCheckFile(rp, "/tmp/pdfseg.7.pdf"); /* 7 */ boxaaDestroy(&baa); return regTestCleanup(rp); }
main(int argc, char **argv) { char *pagedir, *pagesubstr, *maskdir, *masksubstr; char *title, *fileout, *boxaafile, *boxaapath; l_int32 ret, res, type, thresh; l_float32 scalefactor; BOXAA *baa; static char mainName[] = "convertsegfilestopdf"; if (argc != 12) { fprintf(stderr, " Syntax: convertsegfilestopdf dirin substr res type thresh \\ \n" " boxaafile scalefactor title fileout\n" " where\n" " pagedir: input directory for image files\n" " pagesubstr: Use 'allfiles' to convert all files\n" " in the directory\n" " maskdir: input directory for mask files;\n" " use 'skip' to skip \n" " masksubstr: Use 'allfiles' to convert all files\n" " in the directory; 'skip' to skip\n" " res: Input resolution of each image;\n" " assumed to all be the same\n" " type: compression used for non-image regions:\n" " 0: default (G4 encoding)\n" " 1: JPEG encoding\n" " 2: G4 encoding\n" " 3: PNG encoding\n" " thresh: threshold for binarization; use 0 for default\n" " boxaafile: Optional file of 'image' regions within\n" " each page. This contains a boxa for each\n" " page, consisting of a set of regions.\n" " Use 'skip' to skip.\n" " scalefactor: Use to scale down the image regions\n" " title: Use 'none' to omit\n" " fileout: Output pdf file\n"); return 1; } pagedir = argv[1]; pagesubstr = argv[2]; maskdir = argv[3]; masksubstr = argv[4]; res = atoi(argv[5]); type = atoi(argv[6]); thresh = atoi(argv[7]); boxaafile = argv[8]; scalefactor = atof(argv[9]); title = argv[10]; fileout = argv[11]; if (!strcmp(pagesubstr, "allfiles")) pagesubstr = NULL; if (!strcmp(maskdir, "skip")) maskdir = NULL; if (!strcmp(masksubstr, "allfiles")) masksubstr = NULL; if (scalefactor <= 0.0 || scalefactor > 1.0) { L_WARNING("invalid scalefactor: setting to 1.0", mainName); scalefactor = 1.0; } if (type != 1 && type != 2 && type != 3) type = L_G4_ENCODE; if (thresh <= 0) thresh = 150; if (!strcmp(title, "none")) title = NULL; if (maskdir) /* use this; ignore any input boxaafile */ baa = convertNumberedMasksToBoxaa(maskdir, masksubstr, 0, 0); else if (strcmp(boxaafile, "skip")) { /* use the boxaafile */ boxaapath = genPathname(boxaafile, NULL); baa = boxaaRead(boxaapath); FREE(boxaapath); } else /* no maskdir and no input boxaafile */ baa = NULL; ret = convertSegmentedFilesToPdf(pagedir, pagesubstr, res, type, thresh, baa, 75, scalefactor, title, fileout); boxaaDestroy(&baa); return ret; }