/*! * pixWriteMemGif() * * Input: &data (<return> data of tiff compressed image) * &size (<return> size of returned data) * pix * Return: 0 if OK, 1 on error * * Notes: * (1) Of course, we are cheating here -- writing the pix out * as a gif file and then reading it back into memory. */ l_int32 pixWriteMemGif(l_uint8 **pdata, size_t *psize, PIX *pix) { char *tname; l_uint8 *data; l_int32 nbytes; PROCNAME("pixWriteMemGif"); if (!pdata) return ERROR_INT("&data not defined", procName, 1 ); if (!psize) return ERROR_INT("&size not defined", procName, 1 ); if (!pix) return ERROR_INT("&pix not defined", procName, 1 ); tname = genTempFilename("/tmp/", "junk_mem_gif.blah", 1); pixWrite(tname, pix, IFF_GIF); data = arrayRead(tname, &nbytes); FREE(tname); if (!data) return ERROR_INT("data not returned", procName, 1 ); *pdata = data; *psize = nbytes; return 0; }
/*! * pixWriteTempfile() * * Input: dir (directory name; use '.' for local dir; no trailing '/') * tail (<optional> tailname, including extension if any) * pix * format * &filename (<optional> return actual filename used; use * null to skip) * Return: 0 if OK; 1 on error * * Notes: * (1) This generates a temp filename, writes the pix to it, * and optionally returns the temp filename. * (2) If the filename is returned to a windows program from a DLL, * use lept_free() to free it. * (3) See genTempFilename() for details. We omit the time and pid * here. */ l_int32 pixWriteTempfile(const char *dir, const char *tail, PIX *pix, l_int32 format, char **pfilename) { char *filename; l_int32 ret; PROCNAME("pixWriteTempfile"); if (!dir) return ERROR_INT("filename not defined", procName, 1); if (!pix) return ERROR_INT("pix not defined", procName, 1); if ((filename = genTempFilename(dir, tail, 0, 0)) == NULL) return ERROR_INT("temp filename not made", procName, 1); ret = pixWrite(filename, pix, format); if (pfilename) *pfilename = filename; else FREE(filename); return ret; }
/*! * regTestSetup() * * Input: argc (to regtest: either 1 or 2) * argv (to regtest: if @argc == 2, @argv[1] is either * "generate" or a log file name) * &fp (<return> stream for writing to a temporary file, * or null for the "generate" case) * &display (<return> TRUE or FALSE for image display) * &success (<return> initialize to TRUE) * &rp (<optional return> all regression params for either * passthrough to subroutines or for automated * "write and check") * Return: 0 if OK, 1 on error * * Notes: * (1) Call this function with the args to the reg test. * There are exactly three cases: * Case 1: * The second arg is "generate". This will cause * generation of new golden files for the reg test. * The results of the reg test are not recorded. Also, * the returned @display == FALSE (prevents image display). * Case 2: * The second arg is the name of the output log file * into which all reg test failures are put, along with * a SUCCESS or FAILURE summary for each test. This is the * mode in which you run a set of reg tests, looking for * failures, and logging the results to a file. For this * case, as in (b), the returned @display == FALSE. * Case 3: * There is no second arg to the reg test. The results * (failures, summary) will go to stderr. The returned * @display == TRUE; this is used with pixDisplayWithTitle(). * (2) If you wish to use regTestWritePixAndCheck(), you must * let regTestSetup() make and return an @rp struct. It is also * convenient to pass @rp into subroutines of the reg test. * (3) See regutils.h for examples of usage. */ l_int32 regTestSetup(l_int32 argc, char **argv, FILE **pfp, l_int32 *pdisplay, l_int32 *psuccess, L_REGPARAMS **prp) { char *tempname; char errormsg[64]; L_REGPARAMS *rp; PROCNAME("regTestSetup"); if (argc != 1 && argc != 2) { snprintf(errormsg, sizeof(errormsg), "Syntax: %s [generate | <logfile>]", argv[0]); return ERROR_INT(errormsg, procName, 1); } if (prp) *prp = NULL; if (!psuccess) return ERROR_INT("&success not defined", procName, 1); *psuccess = TRUE; if (!pdisplay) return ERROR_INT("&display not defined", procName, 1); if (!pfp) return ERROR_INT("&fp defined", procName, 1); *pfp = NULL; /* default for "generate case */ *pdisplay = (argc == 1) ? TRUE : FALSE; if (argc == 1 || strcmp(argv[1], "generate")) { tempname = genTempFilename("/tmp", "regtest_output.txt", 1); *pfp = fopen(tempname, "wb"); FREE(tempname); if (*pfp == NULL) return ERROR_INT("stream not opened", procName, 1); } if (prp) { if ((rp = (L_REGPARAMS *)CALLOC(1, sizeof(L_REGPARAMS))) == NULL) return ERROR_INT("rp not made", procName, 1); *prp = rp; rp->fp = *pfp; rp->argv = argv; rp->display = *pdisplay; rp->success = *psuccess; } return 0; }
/*! * regTestCleanup() * * Input: argc (to regtest: either 1 or 2) * argv (to regtest: if @argc == 2, @argv[1] is either * "generate" or a log file name) * fp (stream that was used writing to a temporary file; * null for the "generate" case) * success (overall for this reg test) * rp (regression test params; can be null) * Return: 0 if OK, 1 on error * * Notes: * (1) This outputs anything written to the temporary file and * closes the stream to that file. * (2) If a rp struct is made in regTestSetup(), it must be * passed in here for destruction. */ l_int32 regTestCleanup(l_int32 argc, char **argv, FILE *fp, l_int32 success, L_REGPARAMS *rp) { char result[128]; char *tempname, *text, *message; l_int32 nbytes; PROCNAME("regTestCleanup"); if (!fp) { /* for generating golden files; release rp if it exists */ if (rp) FREE(rp); return 0; } fclose(fp); /* Read back data from temp file */ tempname = genTempFilename("/tmp", "regtest_output.txt", 1); text = (char *)arrayRead(tempname, &nbytes); FREE(tempname); if (!text) { if (rp) FREE(rp); return ERROR_INT("text not returned", procName, 1); } /* Prepare result message */ if (rp) /* if either is 0, success == FALSE */ success = rp->success && success; if (success) snprintf(result, sizeof(result), "SUCCESS: %s\n", argv[0]); else snprintf(result, sizeof(result), "FAILURE: %s\n", argv[0]); message = stringJoin(text, result); FREE(text); if (argc == 1) fprintf(stderr, "%s", message); else fileAppendString(argv[1], message); FREE(message); if (rp) FREE(rp); return 0; }
/*! * pixReadMemGif() * * Input: data (const; gif-encoded) * size (of data) * Return: pix, or null on error * * Notes: * (1) Of course, we are cheating here -- writing the data out * to file and then reading it back in as a gif format. */ PIX * pixReadMemGif(const l_uint8 *cdata, size_t size) { char *tname; l_uint8 *data; PIX *pix; PROCNAME("pixReadMemGif"); if (!cdata) return (PIX *)ERROR_PTR("cdata not defined", procName, NULL); tname = genTempFilename("/tmp/", "junk_mem_gif.blah", 1); data = (l_uint8 *)cdata; /* we're really not going to change this */ arrayWrite(tname, "w", data, size); pix = pixRead(tname); FREE(tname); if (!pix) return (PIX *)ERROR_PTR("pix not read", procName, NULL); return pix; }
/*! * regTestSetup() * * Input: argc (from invocation; can be either 1 or 2) * argv (to regtest: @argv[1] is one of these: * "generate", "compare", "display") * &rp (<return> all regression params) * Return: 0 if OK, 1 on error * * Notes: * (1) Call this function with the args to the reg test. * There are three cases: * Case 1: * The second arg is "generate". This will cause * generation of new golden files for the reg test. * The results of the reg test are not recorded, and * the display field is set to FALSE, preventing image display. * Case 2: * The second arg is "compare". This is the mode in which * you run a regression test (or a set of them), looking * for failures and logging the results to a file. * The output, which includes logging of all reg test * failures plus a SUCCESS or FAILURE summary for each test, * is appended to the file "/tmp/reg_results.txt. For this * case, as in Case 1, the display field in rp is set to FALSE. * Case 3: * There is either only arg, or the second arg is "display". * The test will run and files will be written. Comparisons * with golden files will not be carried out, so the only * notion of success or failure is with tests that do not * involve golden files. The display field in rp is TRUE, * and this is used by pixDisplayWithTitle(). * (2) See regutils.h for examples of usage. */ l_int32 regTestSetup(l_int32 argc, char **argv, L_REGPARAMS **prp) { char *testname, *vers; char errormsg[64]; L_REGPARAMS *rp; PROCNAME("regTestSetup"); if (argc != 1 && argc != 2) { snprintf(errormsg, sizeof(errormsg), "Syntax: %s [generate | compare | [display]]", argv[0]); return ERROR_INT(errormsg, procName, 1); } if ((testname = getRootNameFromArgv0(argv[0])) == NULL) return ERROR_INT("invalid root", procName, 1); if ((rp = (L_REGPARAMS *)CALLOC(1, sizeof(L_REGPARAMS))) == NULL) return ERROR_INT("rp not made", procName, 1); *prp = rp; rp->testname = testname; rp->index = -1; /* increment before each test */ /* Initialize to true. A failure in any test is registered * as a failure of the regression test. */ rp->success = TRUE; /* Only open a stream to a temp file for the 'compare' case */ if (argc == 1 || !strcmp(argv[1], "display")) { rp->mode = L_REG_DISPLAY; rp->display = TRUE; } else if (!strcmp(argv[1], "compare")) { rp->mode = L_REG_COMPARE; rp->tempfile = genTempFilename("/tmp", "regtest_output.txt", 0, 1); rp->fp = fopenWriteStream(rp->tempfile, "wb"); if (rp->fp == NULL) { rp->success = FALSE; return ERROR_INT("stream not opened for tempfile", procName, 1); } } else if (!strcmp(argv[1], "generate")) { rp->mode = L_REG_GENERATE; lept_mkdir("golden"); } else { FREE(rp); snprintf(errormsg, sizeof(errormsg), "Syntax: %s [generate | compare | [display]]", argv[0]); return ERROR_INT(errormsg, procName, 1); } /* Print out test name and both the leptonica and * image libarary versions */ fprintf(stderr, "\n################ %s_reg ###############\n", rp->testname); vers = getLeptonicaVersion(); fprintf(stderr, "%s\n", vers); FREE(vers); vers = getImagelibVersions(); fprintf(stderr, "%s\n", vers); FREE(vers); rp->tstart = startTimerNested(); return 0; }
main(int argc, char **argv) { #if HAVE_FMEMOPEN char psname[256]; #endif /* HAVE_FMEMOPEN */ char *tempname; l_uint8 *data; l_int32 i, d, n, success, failure, same; l_int32 w, h, bps, spp; size_t size, nbytes; PIX *pix1, *pix2, *pix4, *pix8, *pix16, *pix32; PIX *pix, *pixt, *pixd; PIXA *pixa; L_REGPARAMS *rp; #if !HAVE_LIBJPEG fprintf(stderr, "Omitting libjpeg tests in ioformats_reg\n"); #endif /* !HAVE_LIBJPEG */ #if !HAVE_LIBTIFF fprintf(stderr, "Omitting libtiff tests in ioformats_reg\n"); #endif /* !HAVE_LIBTIFF */ #if !HAVE_LIBPNG || !HAVE_LIBZ fprintf(stderr, "Omitting libpng tests in ioformats_reg\n"); #endif /* !HAVE_LIBPNG || !HAVE_LIBZ */ if (regTestSetup(argc, argv, &rp)) return 1; /* --------- Part 1: Test all lossless formats for r/w to file ---------*/ failure = FALSE; success = TRUE; fprintf(stderr, "Test bmp 1 bpp file:\n"); if (ioFormatTest(BMP_FILE)) success = FALSE; #if HAVE_LIBTIFF fprintf(stderr, "\nTest other 1 bpp file:\n"); if (ioFormatTest(FILE_1BPP)) success = FALSE; #endif /* HAVE_LIBTIFF */ #if HAVE_LIBPNG fprintf(stderr, "\nTest 2 bpp file:\n"); if (ioFormatTest(FILE_2BPP)) success = FALSE; fprintf(stderr, "\nTest 2 bpp file with cmap:\n"); if (ioFormatTest(FILE_2BPP_C)) success = FALSE; fprintf(stderr, "\nTest 4 bpp file:\n"); if (ioFormatTest(FILE_4BPP)) success = FALSE; fprintf(stderr, "\nTest 4 bpp file with cmap:\n"); if (ioFormatTest(FILE_4BPP_C)) success = FALSE; fprintf(stderr, "\nTest 8 bpp grayscale file with cmap:\n"); if (ioFormatTest(FILE_8BPP_1)) success = FALSE; fprintf(stderr, "\nTest 8 bpp color file with cmap:\n"); if (ioFormatTest(FILE_8BPP_2)) success = FALSE; #endif /* HAVE_LIBPNG */ #if HAVE_LIBJPEG fprintf(stderr, "\nTest 8 bpp file without cmap:\n"); if (ioFormatTest(FILE_8BPP_3)) success = FALSE; #endif /* HAVE_LIBJPEG */ #if HAVE_LIBTIFF fprintf(stderr, "\nTest 16 bpp file:\n"); if (ioFormatTest(FILE_16BPP)) success = FALSE; #endif /* HAVE_LIBTIFF */ #if HAVE_LIBJPEG fprintf(stderr, "\nTest 32 bpp file:\n"); if (ioFormatTest(FILE_32BPP)) success = FALSE; #endif /* HAVE_LIBJPEG */ if (success) fprintf(stderr, "\n ********** Success on all i/o format tests *********\n"); else fprintf(stderr, "\n ******* Failure on at least one i/o format test ******\n"); if (!success) failure = TRUE; /* ------------------ Part 2: Test tiff r/w to file ------------------- */ #if !HAVE_LIBTIFF goto part6; #endif /* !HAVE_LIBTIFF */ fprintf(stderr, "\nTest tiff r/w and format extraction\n"); pixa = pixaCreate(6); pix1 = pixRead(BMP_FILE); pix2 = pixConvert1To2(NULL, pix1, 3, 0); pix4 = pixConvert1To4(NULL, pix1, 15, 0); pix16 = pixRead(FILE_16BPP); fprintf(stderr, "Input format: %d\n", pixGetInputFormat(pix16)); pix8 = pixConvert16To8(pix16, 1); pix32 = pixRead(FILE_32BPP); pixaAddPix(pixa, pix1, L_INSERT); pixaAddPix(pixa, pix2, L_INSERT); pixaAddPix(pixa, pix4, L_INSERT); pixaAddPix(pixa, pix8, L_INSERT); pixaAddPix(pixa, pix16, L_INSERT); pixaAddPix(pixa, pix32, L_INSERT); n = pixaGetCount(pixa); success = TRUE; for (i = 0; i < n; i++) { pix = pixaGetPix(pixa, i, L_CLONE); d = pixGetDepth(pix); fprintf(stderr, "%d bpp\n", d); if (i == 0) { /* 1 bpp */ pixWrite("/tmp/junkg3.tif", pix, IFF_TIFF_G3); pixWrite("/tmp/junkg4.tif", pix, IFF_TIFF_G4); pixWrite("/tmp/junkrle.tif", pix, IFF_TIFF_RLE); pixWrite("/tmp/junkpb.tif", pix, IFF_TIFF_PACKBITS); if (testcomp("/tmp/junkg3.tif", pix, IFF_TIFF_G3)) success = FALSE; if (testcomp("/tmp/junkg4.tif", pix, IFF_TIFF_G4)) success = FALSE; if (testcomp("/tmp/junkrle.tif", pix, IFF_TIFF_RLE)) success = FALSE; if (testcomp("/tmp/junkpb.tif", pix, IFF_TIFF_PACKBITS)) success = FALSE; } pixWrite("/tmp/junklzw.tif", pix, IFF_TIFF_LZW); pixWrite("/tmp/junkzip.tif", pix, IFF_TIFF_ZIP); pixWrite("/tmp/junknon.tif", pix, IFF_TIFF); if (testcomp("/tmp/junklzw.tif", pix, IFF_TIFF_LZW)) success = FALSE; if (testcomp("/tmp/junkzip.tif", pix, IFF_TIFF_ZIP)) success = FALSE; if (testcomp("/tmp/junknon.tif", pix, IFF_TIFF)) success = FALSE; pixDestroy(&pix); } if (success) fprintf(stderr, "\n ********** Success on tiff r/w to file *********\n\n"); else fprintf(stderr, "\n ******* Failure on at least one tiff r/w to file ******\n\n"); if (!success) failure = TRUE; /* ------------------ Part 3: Test tiff r/w to memory ----------------- */ success = TRUE; for (i = 0; i < n; i++) { pix = pixaGetPix(pixa, i, L_CLONE); d = pixGetDepth(pix); fprintf(stderr, "%d bpp\n", d); if (i == 0) { /* 1 bpp */ pixWriteMemTiff(&data, &size, pix, IFF_TIFF_G3); nbytes = nbytesInFile("/tmp/junkg3.tif"); fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_G3)) success = FALSE; lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF_G4); nbytes = nbytesInFile("/tmp/junkg4.tif"); fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_G4)) success = FALSE; readHeaderMemTiff(data, size, 0, &w, &h, &bps, &spp, NULL, NULL, NULL); fprintf(stderr, "(w,h,bps,spp) = (%d,%d,%d,%d)\n", w, h, bps, spp); lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF_RLE); nbytes = nbytesInFile("/tmp/junkrle.tif"); fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_RLE)) success = FALSE; lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF_PACKBITS); nbytes = nbytesInFile("/tmp/junkpb.tif"); fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_PACKBITS)) success = FALSE; lept_free(data); } pixWriteMemTiff(&data, &size, pix, IFF_TIFF_LZW); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_LZW)) success = FALSE; lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF_ZIP); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_ZIP)) success = FALSE; readHeaderMemTiff(data, size, 0, &w, &h, &bps, &spp, NULL, NULL, NULL); fprintf(stderr, "(w,h,bps,spp) = (%d,%d,%d,%d)\n", w, h, bps, spp); lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF)) success = FALSE; lept_free(data); pixDestroy(&pix); } if (success) fprintf(stderr, "\n ********** Success on tiff r/w to memory *********\n\n"); else fprintf(stderr, "\n ******* Failure on at least one tiff r/w to memory ******\n\n"); if (!success) failure = TRUE; /* ---------------- Part 4: Test non-tiff r/w to memory ---------------- */ #if HAVE_FMEMOPEN pixDisplayWrite(NULL, -1); success = TRUE; for (i = 0; i < n; i++) { pix = pixaGetPix(pixa, i, L_CLONE); d = pixGetDepth(pix); sprintf(psname, "/tmp/junkps.%d", d); fprintf(stderr, "%d bpp\n", d); if (d != 16) { if (test_writemem(pix, IFF_PNG, NULL)) success = FALSE; if (test_writemem(pix, IFF_BMP, NULL)) success = FALSE; } if (test_writemem(pix, IFF_PNM, NULL)) success = FALSE; if (test_writemem(pix, IFF_PS, psname)) success = FALSE; if (d == 8 || d == 32) if (test_writemem(pix, IFF_JFIF_JPEG, NULL)) success = FALSE; pixDestroy(&pix); } if (success) fprintf(stderr, "\n ********** Success on non-tiff r/w to memory *********\n\n"); else fprintf(stderr, "\n **** Failure on at least one non-tiff r/w to memory *****\n\n"); if (!success) failure = TRUE; #else fprintf(stderr, "\n ***** Non-tiff r/w to memory not enabled *****\n\n"); #endif /* HAVE_FMEMOPEN */ pixaDestroy(&pixa); /* ------------ Part 5: Test multipage tiff r/w to memory ------------ */ /* Make a multipage tiff file, and read it back into memory */ success = TRUE; pix = pixRead("feyn.tif"); pixa = pixaSplitPix(pix, 3, 3, 0, 0); for (i = 0; i < 9; i++) { pixt = pixaGetPix(pixa, i, L_CLONE); if (i == 0) pixWriteTiff("/tmp/junktiffmpage.tif", pixt, IFF_TIFF_G4, "w"); else pixWriteTiff("/tmp/junktiffmpage.tif", pixt, IFF_TIFF_G4, "a"); pixDestroy(&pixt); } data = l_binaryRead("/tmp/junktiffmpage.tif", &nbytes); pixaDestroy(&pixa); /* Read the individual pages from memory to a pix */ pixa = pixaCreate(9); for (i = 0; i < 9; i++) { pixt = pixReadMemTiff(data, nbytes, i); pixaAddPix(pixa, pixt, L_INSERT); } lept_free(data); /* Un-tile the pix in the pixa back to the original image */ pixt = pixaDisplayUnsplit(pixa, 3, 3, 0, 0); pixaDestroy(&pixa); /* Clip to foreground to remove any extra rows or columns */ pixClipToForeground(pix, &pix1, NULL); pixClipToForeground(pixt, &pix2, NULL); pixEqual(pix1, pix2, &same); if (same) fprintf(stderr, "\n ******* Success on tiff multipage read from memory ******\n\n"); else fprintf(stderr, "\n ******* Failure on tiff multipage read from memory ******\n\n"); if (!same) failure = TRUE; pixDestroy(&pix); pixDestroy(&pixt); pixDestroy(&pix1); pixDestroy(&pix2); /* ------------ Part 6: Test 24 bpp writing ------------ */ #if !HAVE_LIBTIFF part6: #endif /* !HAVE_LIBTIFF */ #if !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF goto finish; #endif /* !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF */ /* Generate a 24 bpp (not 32 bpp !!) rgb pix and write it out */ success = TRUE; pix = pixRead("marge.jpg"); pixt = make_24_bpp_pix(pix); pixWrite("/tmp/junk24.png", pixt, IFF_PNG); pixWrite("/tmp/junk24.jpg", pixt, IFF_JFIF_JPEG); pixWrite("/tmp/junk24.tif", pixt, IFF_TIFF); pixd = pixRead("/tmp/junk24.png"); pixEqual(pix, pixd, &same); if (!same) success = FALSE; pixDestroy(&pixd); pixd = pixRead("/tmp/junk24.jpg"); regTestCompareSimilarPix(rp, pix, pixd, 10, 0.0002, 0); pixDestroy(&pixd); pixd = pixRead("/tmp/junk24.tif"); pixEqual(pix, pixd, &same); if (!same) success = FALSE; pixDestroy(&pixd); if (success) fprintf(stderr, "\n ******* Success on 24 bpp rgb writing *******\n\n"); else fprintf(stderr, "\n ******* Failure on 24 bpp rgb writing *******\n\n"); if (!success) failure = TRUE; pixDestroy(&pix); pixDestroy(&pixt); /* -------------- Part 7: Read header information -------------- */ success = TRUE; if (get_header_data(FILE_1BPP, IFF_TIFF_G4)) success = FALSE; if (get_header_data(FILE_2BPP, IFF_PNG)) success = FALSE; if (get_header_data(FILE_2BPP_C, IFF_PNG)) success = FALSE; if (get_header_data(FILE_4BPP, IFF_PNG)) success = FALSE; if (get_header_data(FILE_4BPP_C, IFF_PNG)) success = FALSE; if (get_header_data(FILE_8BPP_1, IFF_PNG)) success = FALSE; if (get_header_data(FILE_8BPP_2, IFF_PNG)) success = FALSE; if (get_header_data(FILE_8BPP_3, IFF_JFIF_JPEG)) success = FALSE; if (get_header_data(FILE_16BPP, IFF_TIFF_ZIP)) success = FALSE; if (get_header_data(FILE_32BPP, IFF_JFIF_JPEG)) success = FALSE; #if HAVE_FMEMOPEN pix = pixRead(FILE_8BPP_1); tempname = genTempFilename((const char *)"/tmp", (const char *)".pnm", 1, 1); pixWrite(tempname, pix, IFF_PNM); if (get_header_data(tempname, IFF_PNM)) success = FALSE; pixDestroy(&pix); lept_free(tempname); #endif /* HAVE_FMEMOPEN */ pix = pixRead(FILE_1BPP); tempname = genTempFilename((const char *)"/tmp", (const char *)".tif", 1, 1); pixWrite(tempname, pix, IFF_TIFF_G3); if (get_header_data(tempname, IFF_TIFF_G3)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_G4); if (get_header_data(tempname, IFF_TIFF_G4)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_PACKBITS); if (get_header_data(tempname, IFF_TIFF_PACKBITS)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_RLE); if (get_header_data(tempname, IFF_TIFF_RLE)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_LZW); if (get_header_data(tempname, IFF_TIFF_LZW)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_ZIP); if (get_header_data(tempname, IFF_TIFF_ZIP)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF); if (get_header_data(tempname, IFF_TIFF)) success = FALSE; pixDestroy(&pix); lept_free(tempname); if (success) fprintf(stderr, "\n ******* Success on reading headers *******\n\n"); else fprintf(stderr, "\n ******* Failure on reading headers *******\n\n"); if (!success) failure = TRUE; #if !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF finish: #endif /* !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF */ if (!failure) fprintf(stderr, " ******* Success on all tests *******\n\n"); else fprintf(stderr, " ******* Failure on at least one test *******\n\n"); return regTestCleanup(rp); }
/* * pixaWriteCompressedToPS() * * Input: pixa (any set of images) * fileout (output ps file) * res (of input image) * level (compression: 2 or 3) * Return: 0 if OK, 1 on error * * Notes: * (1) This generates a PS file of multiple page images, all * with bounding boxes. * (2) It compresses to: * cmap + level2: jpeg * cmap + level3: flate * 1 bpp: tiffg4 * 2 or 4 bpp + level2: jpeg * 2 or 4 bpp + level3: flate * 8 bpp: jpeg * 16 bpp: flate * 32 bpp: jpeg * (3) To generate a pdf, use: ps2pdf <infile.ps> <outfile.pdf> */ l_int32 pixaWriteCompressedToPS(PIXA *pixa, const char *fileout, l_int32 res, l_int32 level) { char *tname, *g4_name, *jpeg_name, *png_name; l_int32 i, n, firstfile, index, writeout, d; PIX *pix, *pixt; PIXCMAP *cmap; PROCNAME("pixaWriteCompressedToPS"); if (!pixa) return ERROR_INT("pixa not defined", procName, 1); if (!fileout) return ERROR_INT("fileout not defined", procName, 1); if (level != 2 && level != 3) { L_ERROR("only levels 2 and 3 permitted; using level 2\n", procName); level = 2; } n = pixaGetCount(pixa); firstfile = TRUE; index = 0; lept_mkdir("lept/comp"); g4_name = genTempFilename("/tmp/lept/comp", "temp.tif", 0, 0); jpeg_name = genTempFilename("/tmp/lept/comp", "temp.jpg", 0, 0); png_name = genTempFilename("/tmp/lept/comp", "temp.png", 0, 0); for (i = 0; i < n; i++) { writeout = TRUE; pix = pixaGetPix(pixa, i, L_CLONE); d = pixGetDepth(pix); cmap = pixGetColormap(pix); if (d == 1) { tname = g4_name; pixWrite(tname, pix, IFF_TIFF_G4); } else if (cmap) { if (level == 2) { pixt = pixConvertForPSWrap(pix); tname = jpeg_name; pixWrite(tname, pixt, IFF_JFIF_JPEG); pixDestroy(&pixt); } else { /* level == 3 */ tname = png_name; pixWrite(tname, pix, IFF_PNG); } } else if (d == 16) { if (level == 2) L_WARNING("d = 16; must write out flate\n", procName); tname = png_name; pixWrite(tname, pix, IFF_PNG); } else if (d == 2 || d == 4) { if (level == 2) { pixt = pixConvertTo8(pix, 0); tname = jpeg_name; pixWrite(tname, pixt, IFF_JFIF_JPEG); pixDestroy(&pixt); } else { /* level == 3 */ tname = png_name; pixWrite(tname, pix, IFF_PNG); } } else if (d == 8 || d == 32) { tname = jpeg_name; pixWrite(tname, pix, IFF_JFIF_JPEG); } else { /* shouldn't happen */ L_ERROR("invalid depth: %d\n", procName, d); writeout = FALSE; } pixDestroy(&pix); if (writeout) writeImageCompressedToPSFile(tname, fileout, res, &firstfile, &index); } LEPT_FREE(g4_name); LEPT_FREE(jpeg_name); LEPT_FREE(png_name); return 0; }
/*! * dewarpBuildModel() * * Input: dew * debugflag (1 for debugging output) * Return: 0 if OK, 1 on error * * Notes: * (1) This is the basic function that builds the vertical * disparity array, which allows determination of the * src pixel in the input image corresponding to each * dest pixel in the dewarped image. * (2) The method is as follows: * * Estimate the centers of all the long textlines and * fit a LS quadratic to each one. This smooths the curves. * * Sample each curve at a regular interval, find the y-value * of the flat point on each curve, and subtract the sampled * curve value from this value. This is the vertical * disparity. * * Fit a LS quadratic to each set of vertically aligned * disparity samples. This smooths the disparity values * in the vertical direction. Then resample at the same * regular interval, We now have a regular grid of smoothed * vertical disparity valuels. * * Interpolate this grid to get a full resolution disparity * map. This can be applied directly to the src image * pixels to dewarp the image in the vertical direction, * making all textlines horizontal. */ l_int32 dewarpBuildModel(L_DEWARP *dew, l_int32 debugflag) { char *tempname; l_int32 i, j, nlines, nx, ny, sampling; l_float32 c0, c1, c2, x, y, flaty, val; l_float32 *faflats; NUMA *nax, *nafit, *nacurve, *nacurves, *naflat, *naflats, *naflatsi; PIX *pixs, *pixt1, *pixt2; PTA *pta, *ptad; PTAA *ptaa1, *ptaa2, *ptaa3, *ptaa4, *ptaa5, *ptaa6, *ptaa7; FPIX *fpix1, *fpix2, *fpix3; PROCNAME("dewarpBuildModel"); if (!dew) return ERROR_INT("dew not defined", procName, 1); pixs = dew->pixs; if (debugflag) { pixDisplayWithTitle(pixs, 0, 0, "pixs", 1); pixWriteTempfile("/tmp", "pixs.png", pixs, IFF_PNG, NULL); } /* Make initial estimate of centers of textlines */ ptaa1 = pixGetTextlineCenters(pixs, DEBUG_TEXTLINE_CENTERS); if (debugflag) { pixt1 = pixConvertTo32(pixs); pixt2 = pixDisplayPtaa(pixt1, ptaa1); pixWriteTempfile("/tmp", "lines1.png", pixt2, IFF_PNG, NULL); pixDestroy(&pixt1); pixDestroy(&pixt2); } /* Remove all lines that are not near the length * of the longest line. */ ptaa2 = ptaaRemoveShortLines(pixs, ptaa1, 0.8, DEBUG_SHORT_LINES); if (debugflag) { pixt1 = pixConvertTo32(pixs); pixt2 = pixDisplayPtaa(pixt1, ptaa2); pixWriteTempfile("/tmp", "lines2.png", pixt2, IFF_PNG, NULL); pixDestroy(&pixt1); pixDestroy(&pixt2); } nlines = ptaaGetCount(ptaa2); if (nlines < dew->minlines) return ERROR_INT("insufficient lines to build model", procName, 1); /* Do quadratic fit to smooth each line. A single quadratic * over the entire width of the line appears to be sufficient. * Quartics tend to overfit to noise. Each line is thus * represented by three coefficients: c2 * x^2 + c1 * x + c0. * Using the coefficients, sample each fitted curve uniformly * across the full width of the image. */ sampling = dew->sampling; nx = dew->nx; ny = dew->ny; ptaa3 = ptaaCreate(nlines); nacurve = numaCreate(nlines); /* stores curvature coeff c2 */ for (i = 0; i < nlines; i++) { /* for each line */ pta = ptaaGetPta(ptaa2, i, L_CLONE); ptaGetQuadraticLSF(pta, &c2, &c1, &c0, NULL); numaAddNumber(nacurve, c2); ptad = ptaCreate(nx); for (j = 0; j < nx; j++) { /* uniformly sampled in x */ x = j * sampling; applyQuadraticFit(c2, c1, c0, x, &y); ptaAddPt(ptad, x, y); } ptaaAddPta(ptaa3, ptad, L_INSERT); ptaDestroy(&pta); } if (debugflag) { ptaa4 = ptaaCreate(nlines); for (i = 0; i < nlines; i++) { pta = ptaaGetPta(ptaa2, i, L_CLONE); ptaGetArrays(pta, &nax, NULL); ptaGetQuadraticLSF(pta, NULL, NULL, NULL, &nafit); ptad = ptaCreateFromNuma(nax, nafit); ptaaAddPta(ptaa4, ptad, L_INSERT); ptaDestroy(&pta); numaDestroy(&nax); numaDestroy(&nafit); } pixt1 = pixConvertTo32(pixs); pixt2 = pixDisplayPtaa(pixt1, ptaa4); pixWriteTempfile("/tmp", "lines3.png", pixt2, IFF_PNG, NULL); pixDestroy(&pixt1); pixDestroy(&pixt2); ptaaDestroy(&ptaa4); } /* Find and save the flat points in each curve. */ naflat = numaCreate(nlines); for (i = 0; i < nlines; i++) { pta = ptaaGetPta(ptaa3, i, L_CLONE); numaGetFValue(nacurve, i, &c2); if (c2 <= 0) /* flat point at bottom; max value of y in curve */ ptaGetRange(pta, NULL, NULL, NULL, &flaty); else /* flat point at top; min value of y in curve */ ptaGetRange(pta, NULL, NULL, &flaty, NULL); numaAddNumber(naflat, flaty); ptaDestroy(&pta); } /* Sort the lines in ptaa3 by their position */ naflatsi = numaGetSortIndex(naflat, L_SORT_INCREASING); naflats = numaSortByIndex(naflat, naflatsi); nacurves = numaSortByIndex(nacurve, naflatsi); dew->naflats = naflats; dew->nacurves = nacurves; ptaa4 = ptaaSortByIndex(ptaa3, naflatsi); numaDestroy(&naflat); numaDestroy(&nacurve); numaDestroy(&naflatsi); if (debugflag) { tempname = genTempFilename("/tmp", "naflats.na", 0); numaWrite(tempname, naflats); FREE(tempname); } /* Convert the sampled points in ptaa3 to a sampled disparity with * with respect to the flat point in the curve. */ ptaa5 = ptaaCreate(nlines); for (i = 0; i < nlines; i++) { pta = ptaaGetPta(ptaa4, i, L_CLONE); numaGetFValue(naflats, i, &flaty); ptad = ptaCreate(nx); for (j = 0; j < nx; j++) { ptaGetPt(pta, j, &x, &y); ptaAddPt(ptad, x, flaty - y); } ptaaAddPta(ptaa5, ptad, L_INSERT); ptaDestroy(&pta); } if (debugflag) { tempname = genTempFilename("/tmp", "ptaa5.ptaa", 0); ptaaWrite(tempname, ptaa5, 0); FREE(tempname); } /* Generate a ptaa taking vertical 'columns' from ptaa5. * We want to fit the vertical disparity on the column to the * vertical position of the line, which we call 'y' here and * obtain from naflats. */ ptaa6 = ptaaCreate(nx); faflats = numaGetFArray(naflats, L_NOCOPY); for (j = 0; j < nx; j++) { pta = ptaCreate(nlines); for (i = 0; i < nlines; i++) { y = faflats[i]; ptaaGetPt(ptaa5, i, j, NULL, &val); /* disparity value */ ptaAddPt(pta, y, val); } ptaaAddPta(ptaa6, pta, L_INSERT); } if (debugflag) { tempname = genTempFilename("/tmp", "ptaa6.ptaa", 0); ptaaWrite(tempname, ptaa6, 0); FREE(tempname); } /* Do quadratic fit vertically on a subset of pixel columns * for the vertical displacement, which identifies the * src pixel(s) for each dest pixel. Sample the displacement * on a regular grid in the vertical direction. */ ptaa7 = ptaaCreate(nx); /* uniformly sampled across full height of image */ for (j = 0; j < nx; j++) { /* for each column */ pta = ptaaGetPta(ptaa6, j, L_CLONE); ptaGetQuadraticLSF(pta, &c2, &c1, &c0, NULL); ptad = ptaCreate(ny); for (i = 0; i < ny; i++) { /* uniformly sampled in y */ y = i * sampling; applyQuadraticFit(c2, c1, c0, y, &val); ptaAddPt(ptad, y, val); } ptaaAddPta(ptaa7, ptad, L_INSERT); ptaDestroy(&pta); } if (debugflag) { tempname = genTempFilename("/tmp", "ptaa7.ptaa", 0); ptaaWrite(tempname, ptaa7, 0); FREE(tempname); } /* Save the result in a fpix at the specified subsampling */ fpix1 = fpixCreate(nx, ny); for (i = 0; i < ny; i++) { for (j = 0; j < nx; j++) { ptaaGetPt(ptaa7, j, i, NULL, &val); fpixSetPixel(fpix1, j, i, val); } } dew->sampvdispar = fpix1; /* Generate a full res fpix for vertical dewarping. We require that * the size of this fpix is at least as big as the input image. */ fpix2 = fpixScaleByInteger(fpix1, sampling); dew->fullvdispar = fpix2; if (debugflag) { pixt1 = fpixRenderContours(fpix2, -2., 2.0, 0.2); pixWriteTempfile("/tmp", "vert-contours.png", pixt1, IFF_PNG, NULL); pixDisplay(pixt1, 1000, 0); pixDestroy(&pixt1); } /* Generate full res and sampled fpix for horizontal dewarping. This * works to the extent that the line curvature is due to bending * out of the plane normal to the camera, and not wide-angle * "fishbowl" distortion. Also generate the sampled horizontal * disparity array. */ if (dew->applyhoriz) { fpix3 = fpixBuildHorizontalDisparity(fpix2, 0, &dew->extraw); dew->fullhdispar = fpix3; dew->samphdispar = fpixSampledDisparity(fpix3, dew->sampling); if (debugflag) { pixt1 = fpixRenderContours(fpix3, -2., 2.0, 0.2); pixWriteTempfile("/tmp", "horiz-contours.png", pixt1, IFF_PNG, NULL); pixDisplay(pixt1, 1000, 0); pixDestroy(&pixt1); } } dew->success = 1; ptaaDestroy(&ptaa1); ptaaDestroy(&ptaa2); ptaaDestroy(&ptaa3); ptaaDestroy(&ptaa4); ptaaDestroy(&ptaa5); ptaaDestroy(&ptaa6); ptaaDestroy(&ptaa7); return 0; }
/*! * pixGetRegionsBinary() * * Input: pixs (1 bpp, assumed to be 300 to 400 ppi) * &pixhm (<optional return> halftone mask) * &pixtm (<optional return> textline mask) * &pixtb (<optional return> textblock mask) * debug (flag: set to 1 for debug output) * Return: 0 if OK, 1 on error * * Notes: * (1) It is best to deskew the image before segmenting. * (2) The debug flag enables a number of outputs. These * are included to show how to generate and save/display * these results. */ l_int32 pixGetRegionsBinary(PIX *pixs, PIX **ppixhm, PIX **ppixtm, PIX **ppixtb, l_int32 debug) { char *tempname; l_int32 htfound, tlfound; PIX *pixr, *pixt1, *pixt2; PIX *pixtext; /* text pixels only */ PIX *pixhm2; /* halftone mask; 2x reduction */ PIX *pixhm; /* halftone mask; */ PIX *pixtm2; /* textline mask; 2x reduction */ PIX *pixtm; /* textline mask */ PIX *pixvws; /* vertical white space mask */ PIX *pixtb2; /* textblock mask; 2x reduction */ PIX *pixtbf2; /* textblock mask; 2x reduction; small comps filtered */ PIX *pixtb; /* textblock mask */ PROCNAME("pixGetRegionsBinary"); if (ppixhm) *ppixhm = NULL; if (ppixtm) *ppixtm = NULL; if (ppixtb) *ppixtb = NULL; if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (pixGetDepth(pixs) != 1) return ERROR_INT("pixs not 1 bpp", procName, 1); /* 2x reduce, to 150 -200 ppi */ pixr = pixReduceRankBinaryCascade(pixs, 1, 0, 0, 0); pixDisplayWrite(pixr, debug); /* Get the halftone mask */ pixhm2 = pixGenHalftoneMask(pixr, &pixtext, &htfound, debug); /* Get the textline mask from the text pixels */ pixtm2 = pixGenTextlineMask(pixtext, &pixvws, &tlfound, debug); /* Get the textblock mask from the textline mask */ pixtb2 = pixGenTextblockMask(pixtm2, pixvws, debug); pixDestroy(&pixr); pixDestroy(&pixtext); pixDestroy(&pixvws); /* Remove small components from the mask, where a small * component is defined as one with both width and height < 60 */ pixtbf2 = pixSelectBySize(pixtb2, 60, 60, 4, L_SELECT_IF_EITHER, L_SELECT_IF_GTE, NULL); pixDestroy(&pixtb2); pixDisplayWriteFormat(pixtbf2, debug, IFF_PNG); /* Expand all masks to full resolution, and do filling or * small dilations for better coverage. */ pixhm = pixExpandReplicate(pixhm2, 2); pixt1 = pixSeedfillBinary(NULL, pixhm, pixs, 8); pixOr(pixhm, pixhm, pixt1); pixDestroy(&pixt1); pixDisplayWriteFormat(pixhm, debug, IFF_PNG); pixt1 = pixExpandReplicate(pixtm2, 2); pixtm = pixDilateBrick(NULL, pixt1, 3, 3); pixDestroy(&pixt1); pixDisplayWriteFormat(pixtm, debug, IFF_PNG); pixt1 = pixExpandReplicate(pixtbf2, 2); pixtb = pixDilateBrick(NULL, pixt1, 3, 3); pixDestroy(&pixt1); pixDisplayWriteFormat(pixtb, debug, IFF_PNG); pixDestroy(&pixhm2); pixDestroy(&pixtm2); pixDestroy(&pixtbf2); /* Debug: identify objects that are neither text nor halftone image */ if (debug) { pixt1 = pixSubtract(NULL, pixs, pixtm); /* remove text pixels */ pixt2 = pixSubtract(NULL, pixt1, pixhm); /* remove halftone pixels */ pixDisplayWriteFormat(pixt2, 1, IFF_PNG); pixDestroy(&pixt1); pixDestroy(&pixt2); } /* Debug: display textline components with random colors */ if (debug) { l_int32 w, h; BOXA *boxa; PIXA *pixa; boxa = pixConnComp(pixtm, &pixa, 8); pixGetDimensions(pixtm, &w, &h, NULL); pixt1 = pixaDisplayRandomCmap(pixa, w, h); pixcmapResetColor(pixGetColormap(pixt1), 0, 255, 255, 255); pixDisplay(pixt1, 100, 100); pixDisplayWriteFormat(pixt1, 1, IFF_PNG); pixaDestroy(&pixa); boxaDestroy(&boxa); pixDestroy(&pixt1); } /* Debug: identify the outlines of each textblock */ if (debug) { PIXCMAP *cmap; PTAA *ptaa; ptaa = pixGetOuterBordersPtaa(pixtb); tempname = genTempFilename("/tmp", "tb_outlines.ptaa", 0, 0); ptaaWrite(tempname, ptaa, 1); FREE(tempname); pixt1 = pixRenderRandomCmapPtaa(pixtb, ptaa, 1, 16, 1); cmap = pixGetColormap(pixt1); pixcmapResetColor(cmap, 0, 130, 130, 130); pixDisplay(pixt1, 500, 100); pixDisplayWriteFormat(pixt1, 1, IFF_PNG); pixDestroy(&pixt1); ptaaDestroy(&ptaa); } /* Debug: get b.b. for all mask components */ if (debug) { BOXA *bahm, *batm, *batb; bahm = pixConnComp(pixhm, NULL, 4); batm = pixConnComp(pixtm, NULL, 4); batb = pixConnComp(pixtb, NULL, 4); tempname = genTempFilename("/tmp", "htmask.boxa", 0, 0); boxaWrite(tempname, bahm); FREE(tempname); tempname = genTempFilename("/tmp", "textmask.boxa", 0, 0); boxaWrite(tempname, batm); FREE(tempname); tempname = genTempFilename("/tmp", "textblock.boxa", 0, 0); boxaWrite(tempname, batb); FREE(tempname); boxaDestroy(&bahm); boxaDestroy(&batm); boxaDestroy(&batb); } if (ppixhm) *ppixhm = pixhm; else pixDestroy(&pixhm); if (ppixtm) *ppixtm = pixtm; else pixDestroy(&pixtm); if (ppixtb) *ppixtb = pixtb; else pixDestroy(&pixtb); return 0; }