/*! * \brief pixWriteMemGif() * * \param[out] pdata data of gif compressed image * \param[out] psize size of returned data * \param[in] pix * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) See comments in pixReadMemGif() * (2) For Giflib version >= 5.1, this uses the EGifOpen() buffer * interface. No temp files are required. * </pre> */ l_int32 pixWriteMemGif(l_uint8 **pdata, size_t *psize, PIX *pix) { #if (GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1) || GIFLIB_MAJOR > 5 int giferr; l_int32 result; GifFileType *gif; L_BBUFFER *buffer; #else char *fname; #endif /* 5.1 and beyond */ PROCNAME("pixWriteMemGif"); if (!pdata) return ERROR_INT("&data not defined", procName, 1 ); *pdata = NULL; if (!psize) return ERROR_INT("&size not defined", procName, 1 ); *psize = 0; if (!pix) return ERROR_INT("&pix not defined", procName, 1 ); #if (GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1) || GIFLIB_MAJOR > 5 if((buffer = bbufferCreate(NULL, 0)) == NULL) { return ERROR_INT("failed to create buffer", procName, 1); } if ((gif = EGifOpen((void*)buffer, gifWriteFunc, NULL)) == NULL) { bbufferDestroy(&buffer); return ERROR_INT("failed to create GIF image handle", procName, 1); } result = pixToGif(pix, gif); EGifCloseFile(gif, &giferr); if(result == 0) { *pdata = bbufferDestroyAndSaveData(&buffer, psize); } else { bbufferDestroy(&buffer); } return result; #else L_INFO("writing to a temp file, not directly to memory\n", procName); /* Write to a temp file */ fname = l_makeTempFilename(NULL); pixWrite(fname, pix, IFF_GIF); /* Read back into memory */ *pdata = l_binaryRead(fname, psize); lept_rmfile(fname); LEPT_FREE(fname); return 0; #endif }
/* Retrieve header data from file and from array in memory */ static l_int32 get_header_data(const char *filename) { l_uint8 *data; l_int32 ret1, ret2, format1, format2; l_int32 w1, w2, h1, h2, d1, d2, bps1, bps2, spp1, spp2, iscmap1, iscmap2; size_t nbytes1, nbytes2; /* Read header from file */ nbytes1 = nbytesInFile(filename); ret1 = pixReadHeader(filename, &format1, &w1, &h1, &bps1, &spp1, &iscmap1); d1 = bps1 * spp1; if (d1 == 24) d1 = 32; if (ret1) fprintf(stderr, "Error: couldn't read header data from file: %s\n", filename); else { fprintf(stderr, "Format data for image %s with format %s:\n" " nbytes = %ld, size (w, h, d) = (%d, %d, %d)\n" " bps = %d, spp = %d, iscmap = %d\n", filename, ImageFileFormatExtensions[format1], nbytes1, w1, h1, d1, bps1, spp1, iscmap1); if (format1 != IFF_PNG) { fprintf(stderr, "Error: format is %d; should be %d\n", format1, IFF_PNG); ret1 = 1; } } /* Read header from array in memory */ ret2 = 0; #if HAVE_FMEMOPEN data = l_binaryRead(filename, &nbytes2); ret2 = pixReadHeaderMem(data, nbytes2, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); lept_free(data); d2 = bps2 * spp2; if (d2 == 24) d2 = 32; if (ret2) fprintf(stderr, "Error: couldn't mem-read header data: %s\n", filename); else { if (nbytes1 != nbytes2 || format1 != format2 || w1 != w2 || h1 != h2 || d1 != d2 || bps1 != bps2 || spp1 != spp2 || iscmap1 != iscmap2) { fprintf(stderr, "Incomsistency reading image %s with format %s\n", filename, ImageFileFormatExtensions[IFF_PNG]); ret2 = 1; } } #endif /* HAVE_FMEMOPEN */ return ret1 || ret2; }
/*! * \brief regTestCleanup() * * \param[in] rp regression test parameters * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) This copies anything written to the temporary file to the * output file /tmp/lept/reg_results.txt. * </pre> */ l_int32 regTestCleanup(L_REGPARAMS *rp) { char result[512]; char *results_file; /* success/failure output in 'compare' mode */ char *text, *message; l_int32 retval; size_t nbytes; PROCNAME("regTestCleanup"); if (!rp) return ERROR_INT("rp not defined", procName, 1); fprintf(stderr, "Time: %7.3f sec\n", stopTimerNested(rp->tstart)); fprintf(stderr, "################################################\n"); /* If generating golden files or running in display mode, release rp */ if (!rp->fp) { LEPT_FREE(rp->testname); LEPT_FREE(rp->tempfile); LEPT_FREE(rp); return 0; } /* Compare mode: read back data from temp file */ fclose(rp->fp); text = (char *)l_binaryRead(rp->tempfile, &nbytes); LEPT_FREE(rp->tempfile); if (!text) { rp->success = FALSE; LEPT_FREE(rp->testname); LEPT_FREE(rp); return ERROR_INT("text not returned", procName, 1); } /* Prepare result message */ if (rp->success) snprintf(result, sizeof(result), "SUCCESS: %s_reg\n", rp->testname); else snprintf(result, sizeof(result), "FAILURE: %s_reg\n", rp->testname); message = stringJoin(text, result); LEPT_FREE(text); results_file = genPathname("/tmp/lept", "reg_results.txt"); fileAppendString(results_file, message); retval = (rp->success) ? 0 : 1; LEPT_FREE(results_file); LEPT_FREE(message); LEPT_FREE(rp->testname); LEPT_FREE(rp); return retval; }
/*! * \brief strcodeCreateFromFile() * * \param[in] filein containing filenames of serialized data * \param[in] fileno integer that labels the two output files * \param[in] outdir [optional] if null, files are made in /tmp/lept/auto * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) The %filein has one filename on each line. * Comment lines begin with "#". * (2) The output is 2 files: * autogen.\<fileno\>.c * autogen.\<fileno\>.h * </pre> */ l_int32 strcodeCreateFromFile(const char *filein, l_int32 fileno, const char *outdir) { char *fname; const char *type; l_uint8 *data; size_t nbytes; l_int32 i, n, index; SARRAY *sa; L_STRCODE *strcode; PROCNAME("strcodeCreateFromFile"); if (!filein) return ERROR_INT("filein not defined", procName, 1); if ((data = l_binaryRead(filein, &nbytes)) == NULL) return ERROR_INT("data not read from file", procName, 1); sa = sarrayCreateLinesFromString((char *)data, 0); LEPT_FREE(data); if (!sa) return ERROR_INT("sa not made", procName, 1); if ((n = sarrayGetCount(sa)) == 0) { sarrayDestroy(&sa); return ERROR_INT("no filenames in the file", procName, 1); } strcode = strcodeCreate(fileno); for (i = 0; i < n; i++) { fname = sarrayGetString(sa, i, L_NOCOPY); if (fname[0] == '#') continue; if (l_getIndexFromFile(fname, &index)) { L_ERROR("File %s has no recognizable type\n", procName, fname); } else { type = l_assoc[index].type; L_INFO("File %s is type %s\n", procName, fname, type); strcodeGenerate(strcode, fname, type); } } strcodeFinalize(&strcode, outdir); return 0; }
/*! * \brief l_genDataString() * * \param[in] filein input file of serialized data * \param[in] ifunc index into set of functions in output file * \return encoded ascii data string, or NULL on error reading from file */ static char * l_genDataString(const char *filein, l_int32 ifunc) { char buf[80]; char *cdata1, *cdata2, *cdata3; l_uint8 *data1, *data2; l_int32 csize1, csize2; size_t size1, size2; SARRAY *sa; PROCNAME("l_genDataString"); if (!filein) return (char *)ERROR_PTR("filein not defined", procName, NULL); /* Read it in, gzip it, encode, and reformat. We gzip because some * serialized data has a significant amount of ascii content. */ if ((data1 = l_binaryRead(filein, &size1)) == NULL) return (char *)ERROR_PTR("bindata not returned", procName, NULL); data2 = zlibCompress(data1, size1, &size2); cdata1 = encodeBase64(data2, size2, &csize1); cdata2 = reformatPacked64(cdata1, csize1, 4, 72, 1, &csize2); LEPT_FREE(data1); LEPT_FREE(data2); LEPT_FREE(cdata1); /* Prepend the string declaration signature and put it together */ sa = sarrayCreate(3); snprintf(buf, sizeof(buf), "static const char *l_strdata_%d =\n", ifunc); sarrayAddString(sa, buf, L_COPY); sarrayAddString(sa, cdata2, L_INSERT); sarrayAddString(sa, (char *)";\n", L_COPY); cdata3 = sarrayToString(sa, 0); sarrayDestroy(&sa); return cdata3; }
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); }
/* Retrieve header data from file */ static l_int32 get_header_data(const char *filename, l_int32 true_format) { char buf[64]; l_uint8 *data; l_int32 ret1, ret2, format1, format2; l_int32 w1, w2, h1, h2, d1, d2, bps1, bps2, spp1, spp2, iscmap1, iscmap2; size_t size1, size2; /* Fail silently if library is not available */ #if !HAVE_LIBJPEG if (true_format == IFF_JFIF_JPEG) return 0; #endif /* !HAVE_LIBJPEG */ #if !HAVE_LIBPNG if (true_format == IFF_PNG) return 0; #endif /* !HAVE_LIBPNG */ #if !HAVE_LIBTIFF if (true_format == IFF_TIFF_G3 || true_format == IFF_TIFF_G4 || true_format == IFF_TIFF_ZIP || true_format == IFF_TIFF_LZW || true_format == IFF_TIFF_PACKBITS || true_format == IFF_TIFF_RLE || true_format == IFF_TIFF) return 0; #endif /* !HAVE_LIBTIFF */ /* Read header from file */ size1 = nbytesInFile(filename); ret1 = pixReadHeader(filename, &format1, &w1, &h1, &bps1, &spp1, &iscmap1); d1 = bps1 * spp1; if (d1 == 24) d1 = 32; if (ret1) fprintf(stderr, "Error: couldn't read header data: %s\n", filename); else { if (format1 > IFF_PNG && format1 < IFF_PNM) { get_tiff_compression_name(buf, format1); fprintf(stderr, "Format data for image %s with format %s:\n" " nbytes = %ld, size (w, h, d) = (%d, %d, %d)\n" " bps = %d, spp = %d, iscmap = %d\n", filename, buf, size1, w1, h1, d1, bps1, spp1, iscmap1); } else { fprintf(stderr, "Format data for image %s with format %s:\n" " nbytes = %ld, size (w, h, d) = (%d, %d, %d)\n" " bps = %d, spp = %d, iscmap = %d\n", filename, ImageFileFormatExtensions[format1], size1, w1, h1, d1, bps1, spp1, iscmap1); } if (format1 != true_format) { fprintf(stderr, "Error: format is %d; should be %d\n", format1, true_format); ret1 = 1; } } /* Read header from array in memory */ data = l_binaryRead(filename, &size2); ret2 = pixReadHeaderMem(data, size2, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); lept_free(data); d2 = bps2 * spp2; if (d2 == 24) d2 = 32; if (ret2) fprintf(stderr, "Error: couldn't mem-read header data: %s\n", filename); else { if (size1 != size2 || format1 != format2 || w1 != w2 || h1 != h2 || d1 != d2 || bps1 != bps2 || spp1 != spp2 || iscmap1 != iscmap2) { fprintf(stderr, "Incomsistency reading image %s with format %s\n", filename, buf); ret2 = 1; } } return ret1 || ret2; }
/*! * \brief sudokuReadFile() * * \param[in] filename of formatted sudoku file * \return array of 81 numbers, or NULL on error * * <pre> * Notes: * (1) The file format has: * * any number of comment lines beginning with '#' * * a set of 9 lines, each having 9 digits (0-9) separated * by a space * </pre> */ l_int32 * sudokuReadFile(const char *filename) { char *str, *strj; l_uint8 *data; l_int32 i, j, nlines, val, index, error; l_int32 *array; size_t size; SARRAY *saline, *sa1, *sa2; PROCNAME("sudokuReadFile"); if (!filename) return (l_int32 *)ERROR_PTR("filename not defined", procName, NULL); data = l_binaryRead(filename, &size); sa1 = sarrayCreateLinesFromString((char *)data, 0); sa2 = sarrayCreate(9); /* Filter out the comment lines; verify that there are 9 data lines */ nlines = sarrayGetCount(sa1); for (i = 0; i < nlines; i++) { str = sarrayGetString(sa1, i, L_NOCOPY); if (str[0] != '#') sarrayAddString(sa2, str, L_COPY); } LEPT_FREE(data); sarrayDestroy(&sa1); nlines = sarrayGetCount(sa2); if (nlines != 9) { sarrayDestroy(&sa2); L_ERROR("file has %d lines\n", procName, nlines); return (l_int32 *)ERROR_PTR("invalid file", procName, NULL); } /* Read the data into the array, verifying that each data * line has 9 numbers. */ error = FALSE; array = (l_int32 *)LEPT_CALLOC(81, sizeof(l_int32)); for (i = 0, index = 0; i < 9; i++) { str = sarrayGetString(sa2, i, L_NOCOPY); saline = sarrayCreateWordsFromString(str); if (sarrayGetCount(saline) != 9) { error = TRUE; sarrayDestroy(&saline); break; } for (j = 0; j < 9; j++) { strj = sarrayGetString(saline, j, L_NOCOPY); if (sscanf(strj, "%d", &val) != 1) error = TRUE; else array[index++] = val; } sarrayDestroy(&saline); if (error) break; } sarrayDestroy(&sa2); if (error) { LEPT_FREE(array); return (l_int32 *)ERROR_PTR("invalid data", procName, NULL); } return array; }
int main(int argc, char **argv) { l_uint8 *array1, *array2; l_int32 n1, n2, n3; size_t size1, size2; FILE *fp; BOXA *boxa1, *boxa2; PIX *pixs, *pix1; PIXA *pixa1; PIXCMAP *cmap; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixs = pixRead("feyn.tif"); /* --------------------------------------------------------------- * * Test pixConnComp() and pixCountConnComp(), * * with output to both boxa and pixa * * --------------------------------------------------------------- */ /* First, test with 4-cc */ boxa1= pixConnComp(pixs, &pixa1, 4); n1 = boxaGetCount(boxa1); boxa2= pixConnComp(pixs, NULL, 4); n2 = boxaGetCount(boxa2); pixCountConnComp(pixs, 4, &n3); fprintf(stderr, "Number of 4 c.c.: n1 = %d; n2 = %d, n3 = %d\n", n1, n2, n3); regTestCompareValues(rp, n1, n2, 0); /* 0 */ regTestCompareValues(rp, n1, n3, 0); /* 1 */ regTestCompareValues(rp, n1, 4452, 0); /* 2 */ pix1 = pixaDisplay(pixa1, pixGetWidth(pixs), pixGetHeight(pixs)); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 3 */ regTestComparePix(rp, pixs, pix1); /* 4 */ pixaDestroy(&pixa1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); pixDestroy(&pix1); /* Test with 8-cc */ boxa1= pixConnComp(pixs, &pixa1, 8); n1 = boxaGetCount(boxa1); boxa2= pixConnComp(pixs, NULL, 8); n2 = boxaGetCount(boxa2); pixCountConnComp(pixs, 8, &n3); fprintf(stderr, "Number of 8 c.c.: n1 = %d; n2 = %d, n3 = %d\n", n1, n2, n3); regTestCompareValues(rp, n1, n2, 0); /* 5 */ regTestCompareValues(rp, n1, n3, 0); /* 6 */ regTestCompareValues(rp, n1, 4305, 0); /* 7 */ pix1 = pixaDisplay(pixa1, pixGetWidth(pixs), pixGetHeight(pixs)); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 8 */ regTestComparePix(rp, pixs, pix1); /* 9 */ pixaDestroy(&pixa1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); pixDestroy(&pix1); /* --------------------------------------------------------------- * * Test boxa I/O * * --------------------------------------------------------------- */ lept_mkdir("lept/conn"); boxa1 = pixConnComp(pixs, NULL, 4); fp = lept_fopen("/tmp/lept/conn/boxa1.ba", "wb+"); boxaWriteStream(fp, boxa1); lept_fclose(fp); fp = lept_fopen("/tmp/lept/conn/boxa1.ba", "rb"); boxa2 = boxaReadStream(fp); lept_fclose(fp); fp = lept_fopen("/tmp/lept/conn/boxa2.ba", "wb+"); boxaWriteStream(fp, boxa2); lept_fclose(fp); array1 = l_binaryRead("/tmp/lept/conn/boxa1.ba", &size1); array2 = l_binaryRead("/tmp/lept/conn/boxa2.ba", &size2); regTestCompareStrings(rp, array1, size1, array2, size2); /* 10 */ lept_free(array1); lept_free(array2); boxaDestroy(&boxa1); boxaDestroy(&boxa2); /* --------------------------------------------------------------- * * Just for fun, display each component as a random color in * * cmapped 8 bpp. Background is color 0; it is set to white. * * --------------------------------------------------------------- */ boxa1 = pixConnComp(pixs, &pixa1, 4); pix1 = pixaDisplayRandomCmap(pixa1, pixGetWidth(pixs), pixGetHeight(pixs)); cmap = pixGetColormap(pix1); pixcmapResetColor(cmap, 0, 255, 255, 255); /* reset background to white */ regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 11 */ if (rp->display) pixDisplay(pix1, 100, 100); boxaDestroy(&boxa1); pixDestroy(&pix1); pixaDestroy(&pixa1); pixDestroy(&pixs); return regTestCleanup(rp); }
/*! * \brief fmorphautogen1() * * \param[in] sela * \param[in] fileindex * \param[in] filename [optional]; can be null * \return 0 if OK; 1 on error * * <pre> * Notes: * (1) This function uses morphtemplate1.txt to create a * top-level file that contains two functions. These * functions will carry out dilation, erosion, * opening or closing for any of the sels in the input sela. * (2) The fileindex parameter is inserted into the output * filename, as described below. * (3) If filename == NULL, the output file is fmorphgen.<n>.c, * where <n> is equal to the 'fileindex' parameter. * (4) If filename != NULL, the output file is <filename>.<n>.c. * </pre> */ l_int32 fmorphautogen1(SELA *sela, l_int32 fileindex, const char *filename) { char *filestr; char *str_proto1, *str_proto2, *str_proto3; char *str_doc1, *str_doc2, *str_doc3, *str_doc4; char *str_def1, *str_def2, *str_proc1, *str_proc2; char *str_dwa1, *str_low_dt, *str_low_ds, *str_low_ts; char *str_low_tsp1, *str_low_dtp1; char bigbuf[L_BUF_SIZE]; l_int32 i, nsels, nbytes, actstart, end, newstart; size_t size; SARRAY *sa1, *sa2, *sa3; PROCNAME("fmorphautogen1"); if (!sela) return ERROR_INT("sela not defined", procName, 1); if (fileindex < 0) fileindex = 0; if ((nsels = selaGetCount(sela)) == 0) return ERROR_INT("no sels in sela", procName, 1); /* Make array of textlines from morphtemplate1.txt */ if ((filestr = (char *)l_binaryRead(TEMPLATE1, &size)) == NULL) return ERROR_INT("filestr not made", procName, 1); sa2 = sarrayCreateLinesFromString(filestr, 1); LEPT_FREE(filestr); if (!sa2) return ERROR_INT("sa2 not made", procName, 1); /* Make array of sel names */ sa1 = selaGetSelnames(sela); /* Make strings containing function call names */ sprintf(bigbuf, "PIX *pixMorphDwa_%d(PIX *pixd, PIX *pixs, " "l_int32 operation, char *selname);", fileindex); str_proto1 = stringNew(bigbuf); sprintf(bigbuf, "PIX *pixFMorphopGen_%d(PIX *pixd, PIX *pixs, " "l_int32 operation, char *selname);", fileindex); str_proto2 = stringNew(bigbuf); sprintf(bigbuf, "l_int32 fmorphopgen_low_%d(l_uint32 *datad, l_int32 w,\n" " l_int32 h, l_int32 wpld,\n" " l_uint32 *datas, l_int32 wpls,\n" " l_int32 index);", fileindex); str_proto3 = stringNew(bigbuf); sprintf(bigbuf, " * PIX *pixMorphDwa_%d()", fileindex); str_doc1 = stringNew(bigbuf); sprintf(bigbuf, " * PIX *pixFMorphopGen_%d()", fileindex); str_doc2 = stringNew(bigbuf); sprintf(bigbuf, " * pixMorphDwa_%d()", fileindex); str_doc3 = stringNew(bigbuf); sprintf(bigbuf, " * pixFMorphopGen_%d()", fileindex); str_doc4 = stringNew(bigbuf); sprintf(bigbuf, "pixMorphDwa_%d(PIX *pixd,", fileindex); str_def1 = stringNew(bigbuf); sprintf(bigbuf, "pixFMorphopGen_%d(PIX *pixd,", fileindex); str_def2 = stringNew(bigbuf); sprintf(bigbuf, " PROCNAME(\"pixMorphDwa_%d\");", fileindex); str_proc1 = stringNew(bigbuf); sprintf(bigbuf, " PROCNAME(\"pixFMorphopGen_%d\");", fileindex); str_proc2 = stringNew(bigbuf); sprintf(bigbuf, " pixt2 = pixFMorphopGen_%d(NULL, pixt1, operation, selname);", fileindex); str_dwa1 = stringNew(bigbuf); sprintf(bigbuf, " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index);", fileindex); str_low_dt = stringNew(bigbuf); sprintf(bigbuf, " fmorphopgen_low_%d(datad, w, h, wpld, datas, wpls, index);", fileindex); str_low_ds = stringNew(bigbuf); sprintf(bigbuf, " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index+1);", fileindex); str_low_tsp1 = stringNew(bigbuf); sprintf(bigbuf, " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index);", fileindex); str_low_ts = stringNew(bigbuf); sprintf(bigbuf, " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index+1);", fileindex); str_low_dtp1 = stringNew(bigbuf); /* Make the output sa */ sa3 = sarrayCreate(0); /* Copyright notice and info header */ sarrayParseRange(sa2, 0, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* Insert function names as documentation */ sarrayAddString(sa3, str_doc1, L_INSERT); sarrayAddString(sa3, str_doc2, L_INSERT); /* Add '#include's */ sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* Insert function prototypes */ sarrayAddString(sa3, str_proto1, L_INSERT); sarrayAddString(sa3, str_proto2, L_INSERT); sarrayAddString(sa3, str_proto3, L_INSERT); /* Add static globals */ sprintf(bigbuf, "\nstatic l_int32 NUM_SELS_GENERATED = %d;", nsels); sarrayAddString(sa3, bigbuf, L_COPY); sprintf(bigbuf, "static char SEL_NAMES[][80] = {"); sarrayAddString(sa3, bigbuf, L_COPY); for (i = 0; i < nsels - 1; i++) { sprintf(bigbuf, " \"%s\",", sarrayGetString(sa1, i, L_NOCOPY)); sarrayAddString(sa3, bigbuf, L_COPY); } sprintf(bigbuf, " \"%s\"};", sarrayGetString(sa1, i, L_NOCOPY)); sarrayAddString(sa3, bigbuf, L_COPY); /* Start pixMorphDwa_*() function description */ sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_doc3, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* Finish pixMorphDwa_*() function definition */ sarrayAddString(sa3, str_def1, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_proc1, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_dwa1, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* Start pixFMorphopGen_*() function description */ sarrayAddString(sa3, str_doc4, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* Finish pixFMorphopGen_*() function definition */ sarrayAddString(sa3, str_def2, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_proc2, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_low_dt, L_COPY); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_low_ds, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_low_tsp1, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_low_dt, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_low_ts, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); sarrayAddString(sa3, str_low_dtp1, L_INSERT); sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* Output to file */ filestr = sarrayToString(sa3, 1); nbytes = strlen(filestr); if (filename) snprintf(bigbuf, L_BUF_SIZE, "%s.%d.c", filename, fileindex); else sprintf(bigbuf, "%s.%d.c", OUTROOT, fileindex); l_binaryWrite(bigbuf, "w", filestr, nbytes); sarrayDestroy(&sa1); sarrayDestroy(&sa2); sarrayDestroy(&sa3); LEPT_FREE(filestr); return 0; }
int main(int argc, char **argv) { char *str; l_uint8 *data1, *data2; l_int32 i, n, same1, same2; size_t size1, size2, slice, total, start, end; FILE *fp; L_DNA *da; SARRAY *sa; L_BYTEA *lba1, *lba2, *lba3, *lba4, *lba5; static char mainName[] = "byteatest"; if (argc != 1) return ERROR_INT("syntax: byteatest", mainName, 1); lept_mkdir("bytea"); /* Test basic init, join and split */ lba1 = l_byteaInitFromFile("feyn.tif"); lba2 = l_byteaInitFromFile("test24.jpg"); size1 = l_byteaGetSize(lba1); size2 = l_byteaGetSize(lba2); l_byteaJoin(lba1, &lba2); lba3 = l_byteaInitFromMem(lba1->data, size1); lba4 = l_byteaInitFromMem(lba1->data + size1, size2); /* Split by hand */ l_binaryWrite("/tmp/bytea/junk1.dat", "w", lba3->data, lba3->size); l_binaryWrite("/tmp/bytea/junk2.dat", "w", lba4->data, lba4->size); filesAreIdentical("feyn.tif", "/tmp/bytea/junk1.dat", &same1); filesAreIdentical("test24.jpg", "/tmp/bytea/junk2.dat", &same2); if (same1 && same2) fprintf(stderr, "OK for join file\n"); else fprintf(stderr, "Error: files are different!\n"); /* Split by function */ l_byteaSplit(lba1, size1, &lba5); l_binaryWrite("/tmp/bytea/junk3.dat", "w", lba1->data, lba1->size); l_binaryWrite("/tmp/bytea/junk4.dat", "w", lba5->data, lba5->size); filesAreIdentical("feyn.tif", "/tmp/bytea/junk3.dat", &same1); filesAreIdentical("test24.jpg", "/tmp/bytea/junk4.dat", &same2); if (same1 && same2) fprintf(stderr, "OK for split file\n"); else fprintf(stderr, "Error: files are different!\n"); l_byteaDestroy(&lba1); l_byteaDestroy(&lba2); l_byteaDestroy(&lba3); l_byteaDestroy(&lba4); l_byteaDestroy(&lba5); /* Test appending with strings */ data1 = l_binaryRead("kernel_reg.c", &size1); sa = sarrayCreateLinesFromString((char *)data1, 1); lba1 = l_byteaCreate(0); n = sarrayGetCount(sa); for (i = 0; i < n; i++) { str = sarrayGetString(sa, i, L_NOCOPY); l_byteaAppendString(lba1, str); l_byteaAppendString(lba1, (char *)"\n"); } data2 = l_byteaGetData(lba1, &size2); l_binaryWrite("/tmp/bytea/junk5.dat", "w", data2, size2); filesAreIdentical("kernel_reg.c", "/tmp/bytea/junk5.dat", &same1); if (same1) fprintf(stderr, "OK for appended string data\n"); else fprintf(stderr, "Error: appended string data is different!\n"); lept_free(data1); sarrayDestroy(&sa); l_byteaDestroy(&lba1); /* Test appending with binary data */ slice = 1000; total = nbytesInFile("breviar-a38.jp2"); lba1 = l_byteaCreate(100); n = 1 + total / slice; fprintf(stderr, "******************************************************\n"); fprintf(stderr, "* Testing error checking: ignore two reported errors *\n"); for (i = 0, start = 0; i <= n; i++, start += slice) { data1 = l_binaryReadSelect("breviar-a38.jp2", start, slice, &size1); l_byteaAppendData(lba1, data1, size1); lept_free(data1); } fprintf(stderr, "******************************************************\n"); data2 = l_byteaGetData(lba1, &size2); l_binaryWrite("/tmp/bytea/junk6.dat", "w", data2, size2); filesAreIdentical("breviar-a38.jp2", "/tmp/bytea/junk6.dat", &same1); if (same1) fprintf(stderr, "OK for appended binary data\n"); else fprintf(stderr, "Error: appended binary data is different!\n"); l_byteaDestroy(&lba1); /* Test search */ convertToPdf("test24.jpg", L_JPEG_ENCODE, 0, "/tmp/bytea/junk7.pdf", 0, 0, 100, NULL, NULL, 0); lba1 = l_byteaInitFromFile("/tmp/bytea/junk7.pdf"); l_byteaFindEachSequence(lba1, (l_uint8 *)" 0 obj\n", 7, &da); /* l_dnaWriteStream(stderr, da); */ n = l_dnaGetCount(da); if (n == 6) fprintf(stderr, "OK for search: found 6 instances\n"); else fprintf(stderr, "Error in search: found %d instances, not 6\n", n); l_byteaDestroy(&lba1); l_dnaDestroy(&da); /* Test write to file */ lba1 = l_byteaInitFromFile("feyn.tif"); fp = lept_fopen("/tmp/bytea/junk8.dat", "wb"); size1 = l_byteaGetSize(lba1); for (start = 0; start < size1; start += 1000) { end = L_MIN(start + 1000 - 1, size1 - 1); l_byteaWriteStream(fp, lba1, start, end); } lept_fclose(fp); filesAreIdentical("feyn.tif", "/tmp/bytea/junk8.dat", &same1); if (same1) fprintf(stderr, "OK for written binary data\n"); else fprintf(stderr, "Error: written binary data is different!\n"); l_byteaDestroy(&lba1); return 0; }
int main(int argc, char **argv) { char buf[256]; size_t size; l_int32 i, w, h; l_int32 format, bps, spp, iscmap, format2, w2, h2, bps2, spp2, iscmap2; l_uint8 *data; l_uint32 *data32, *data32r; BOX *box; PIX *pixs, *pixt, *pixt2, *pixd; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* Test basic serialization/deserialization */ data32 = NULL; for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); /* Serialize to memory */ pixSerializeToMemory(pixs, &data32, &size); /* Just for fun, write and read back from file */ l_binaryWrite("/tmp/regout/array", "w", data32, size); data32r = (l_uint32 *)l_binaryRead("/tmp/regout/array", &size); /* Deserialize */ pixd = pixDeserializeFromMemory(data32r, size); regTestComparePix(rp, pixs, pixd); /* i */ pixDestroy(&pixd); pixDestroy(&pixs); lept_free(data32); lept_free(data32r); } /* Test read/write fileio interface */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixGetDimensions(pixs, &w, &h, NULL); box = boxCreate(0, 0, L_MIN(150, w), L_MIN(150, h)); pixt = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); snprintf(buf, sizeof(buf), "/tmp/regout/pixs.%d.spix", rp->index + 1); pixWrite(buf, pixt, IFF_SPIX); regTestCheckFile(rp, buf); /* nfiles + 2 * i */ pixt2 = pixRead(buf); regTestComparePix(rp, pixt, pixt2); /* nfiles + 2 * i + 1 */ pixDestroy(&pixs); pixDestroy(&pixt); pixDestroy(&pixt2); } /* Test read header. Note that for rgb input, spp = 3, * but for 32 bpp spix, we set spp = 4. */ data = NULL; for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixWriteMem(&data, &size, pixs, IFF_SPIX); pixReadHeader(filename[i], &format, &w, &h, &bps, &spp, &iscmap); pixReadHeaderMem(data, size, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); if (format2 != IFF_SPIX || w != w2 || h != h2 || bps != bps2 || iscmap != iscmap2) { if (rp->fp) fprintf(rp->fp, "Failure comparing data"); else fprintf(stderr, "Failure comparing data"); } pixDestroy(&pixs); lept_free(data); } #if 0 /* Do timing */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); startTimer(); pixSerializeToMemory(pixs, &data32, &size); pixd = pixDeserializeFromMemory(data32, size); fprintf(stderr, "Time for %s: %7.3f sec\n", filename[i], stopTimer()); lept_free(data32); pixDestroy(&pixs); pixDestroy(&pixd); } #endif return regTestCleanup(rp); }
int main(int argc, char **argv) { char *str1, *str2; l_int32 i; size_t size1, size2; l_float32 x, y1, y2, pi; GPLOT *gplot1, *gplot2, *gplot3, *gplot4, *gplot5; NUMA *nax, *nay1, *nay2; static char mainName[] = "plottest"; if (argc != 1) return ERROR_INT(" Syntax: plottest", mainName, 1); /* Generate plot data */ nax = numaCreate(0); nay1 = numaCreate(0); nay2 = numaCreate(0); pi = 3.1415926535; for (i = 0; i < 180; i++) { x = (pi / 180.) * i; y1 = (l_float32)sin(2.4 * x); y2 = (l_float32)cos(2.4 * x); numaAddNumber(nax, x); numaAddNumber(nay1, y1); numaAddNumber(nay2, y2); } /* Show the plot */ gplot1 = gplotCreate("/tmp/plotroot1", GPLOT_OUTPUT, "Example plots", "theta", "f(theta)"); gplotAddPlot(gplot1, nax, nay1, GPLOT_STYLE, "sin (2.4 * theta)"); gplotAddPlot(gplot1, nax, nay2, GPLOT_STYLE, "cos (2.4 * theta)"); gplotMakeOutput(gplot1); /* Also save the plot to png */ gplot1->outformat = GPLOT_PNG; stringReplace(&gplot1->outname, "/tmp/plotroot1.png"); gplotMakeOutput(gplot1); /* Test gplot serialization */ gplotWrite("/tmp/gplot1", gplot1); if ((gplot2 = gplotRead("/tmp/gplot1")) == NULL) return ERROR_INT("gplotRead failure!", mainName, 1); gplotWrite("/tmp/gplot2", gplot2); /* Are the two written gplot files the same? */ str1 = (char *)l_binaryRead("/tmp/gplot1", &size1); str2 = (char *)l_binaryRead("/tmp/gplot2", &size2); if (size1 != size2) fprintf(stderr, "Error: size1 = %lu, size2 = %lu\n", (unsigned long)size1, (unsigned long)size2); else fprintf(stderr, "Correct: size1 = size2 = %lu\n", (unsigned long)size1); if (strcmp(str1, str2)) fprintf(stderr, "Error: str1 != str2\n"); else fprintf(stderr, "Correct: str1 == str2\n"); lept_free(str1); lept_free(str2); /* Read from file and regenerate the plot */ gplot3 = gplotRead("/tmp/gplot2"); stringReplace(&gplot3->title , "Example plots regen"); gplot3->outformat = GPLOT_X11; gplotMakeOutput(gplot3); /* Build gplot but do not make the output formatted stuff */ gplot4 = gplotCreate("/tmp/plotroot2", GPLOT_OUTPUT, "Example plots 2", "theta", "f(theta)"); gplotAddPlot(gplot4, nax, nay1, GPLOT_STYLE, "sin (2.4 * theta)"); gplotAddPlot(gplot4, nax, nay2, GPLOT_STYLE, "cos (2.4 * theta)"); /* Write, read back, and generate the plot */ gplotWrite("/tmp/gplot4", gplot4); if ((gplot5 = gplotRead("/tmp/gplot4")) == NULL) return ERROR_INT("gplotRead failure!", mainName, 1); gplotMakeOutput(gplot5); gplotDestroy(&gplot1); gplotDestroy(&gplot2); gplotDestroy(&gplot3); gplotDestroy(&gplot4); gplotDestroy(&gplot5); numaDestroy(&nax); numaDestroy(&nay1); numaDestroy(&nay2); return 0; }
int main(int argc, char **argv) { l_uint8 *data1, *data2; l_int32 i, same, w, h, width, success, nba; size_t size1, size2; l_float32 diffarea, diffxor, scalefact; BOX *box; BOXA *boxa1, *boxa2, *boxa3; BOXAA *baa1, *baa2, *baa3; PIX *pix1, *pixdb; PIXA *pixa1, *pixa2; static char mainName[] = "boxa1_reg"; if (argc != 1) return ERROR_INT(" Syntax: boxa1_reg", mainName, 1); lept_mkdir("lept/boxa"); /* Make a boxa and display its contents */ boxa1 = boxaCreate(6); box = boxCreate(60, 60, 40, 20); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(120, 50, 20, 50); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(50, 140, 46, 60); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(166, 130, 64, 28); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(64, 224, 44, 34); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(117, 206, 26, 74); boxaAddBox(boxa1, box, L_INSERT); pix1 = DisplayBoxa(boxa1); pixDisplay(pix1, 100, 100); pixDestroy(&pix1); boxaCompareRegions(boxa1, boxa1, 100, &same, &diffarea, &diffxor, NULL); fprintf(stderr, "same = %d, diffarea = %5.3f, diffxor = %5.3f\n", same, diffarea, diffxor); boxa2 = boxaTransform(boxa1, -13, -13, 1.0, 1.0); boxaCompareRegions(boxa1, boxa2, 10, &same, &diffarea, &diffxor, NULL); fprintf(stderr, "same = %d, diffarea = %5.3f, diffxor = %5.3f\n", same, diffarea, diffxor); boxaDestroy(&boxa2); boxa2 = boxaReconcileEvenOddHeight(boxa1, L_ADJUST_TOP_AND_BOT, 6, L_ADJUST_CHOOSE_MIN, 1.0, 0); pix1 = DisplayBoxa(boxa2); pixDisplay(pix1, 100, 500); pixDestroy(&pix1); boxaCompareRegions(boxa1, boxa2, 10, &same, &diffarea, &diffxor, &pixdb); fprintf(stderr, "same = %d, diffarea = %5.3f, diffxor = %5.3f\n", same, diffarea, diffxor); pixDisplay(pixdb, 700, 100); pixDestroy(&pixdb); boxaDestroy(&boxa1); boxaDestroy(&boxa2); /* Input is a fairly clean boxa */ boxa1 = boxaRead("boxa1.ba"); boxa2 = boxaReconcileEvenOddHeight(boxa1, L_ADJUST_TOP, 80, L_ADJUST_CHOOSE_MIN, 1.05, 1); width = 100; boxaGetExtent(boxa2, &w, &h, NULL); scalefact = (l_float32)width / (l_float32)w; boxa3 = boxaTransform(boxa2, 0, 0, scalefact, scalefact); pix1 = boxaDisplayTiled(boxa3, NULL, 1500, 2, 1.0, 0, 3, 2); pixDisplay(pix1, 0, 100); pixWrite("/tmp/lept/boxa/pix1.png", pix1, IFF_PNG); pixDestroy(&pix1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); boxaDestroy(&boxa3); /* Input is an unsmoothed and noisy boxa */ boxa1 = boxaRead("boxa2.ba"); boxa2 = boxaReconcileEvenOddHeight(boxa1, L_ADJUST_TOP, 80, L_ADJUST_CHOOSE_MIN, 1.05, 1); width = 100; boxaGetExtent(boxa2, &w, &h, NULL); scalefact = (l_float32)width / (l_float32)w; boxa3 = boxaTransform(boxa2, 0, 0, scalefact, scalefact); pix1 = boxaDisplayTiled(boxa3, NULL, 1500, 2, 1.0, 0, 3, 2); pixDisplay(pix1, 500, 100); pixWrite("/tmp/lept/boxa/pix2.png", pix1, IFF_PNG); pixDestroy(&pix1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); boxaDestroy(&boxa3); /* Input is a boxa smoothed with a median window filter */ boxa1 = boxaRead("boxa3.ba"); boxa2 = boxaReconcileEvenOddHeight(boxa1, L_ADJUST_TOP, 80, L_ADJUST_CHOOSE_MIN, 1.05, 1); width = 100; boxaGetExtent(boxa2, &w, &h, NULL); scalefact = (l_float32)width / (l_float32)w; boxa3 = boxaTransform(boxa2, 0, 0, scalefact, scalefact); pix1 = boxaDisplayTiled(boxa3, NULL, 1500, 2, 1.0, 0, 3, 2); pixDisplay(pix1, 1000, 100); pixWrite("/tmp/lept/boxa/pix3.png", pix1, IFF_PNG); pixDestroy(&pix1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); boxaDestroy(&boxa3); /* Test serialized boxa I/O to and from memory */ data1 = l_binaryRead("boxa2.ba", &size1); boxa1 = boxaReadMem(data1, size1); boxaWriteMem(&data2, &size2, boxa1); boxa2 = boxaReadMem(data2, size2); boxaWrite("/tmp/lept/boxa/boxa1.ba", boxa1); boxaWrite("/tmp/lept/boxa/boxa2.ba", boxa2); filesAreIdentical("/tmp/lept/boxa/boxa1.ba", "/tmp/lept/boxa/boxa2.ba", &same); if (same) fprintf(stderr, "Good: boxes files are identical\n"); else fprintf(stderr, "Bad: boxes files differ\n"); boxaDestroy(&boxa1); boxaDestroy(&boxa2); lept_free(data1); lept_free(data2); /* Test pixaDisplayBoxaa() */ pixa1 = pixaReadBoth("showboxes.pac"); baa1 = boxaaRead("showboxes1.baa"); baa2 = boxaaTranspose(baa1); baa3 = boxaaTranspose(baa2); nba = boxaaGetCount(baa1); success = TRUE; for (i = 0; i < nba; i++) { boxa1 = boxaaGetBoxa(baa1, i, L_CLONE); boxa2 = boxaaGetBoxa(baa3, i, L_CLONE); boxaEqual(boxa1, boxa2, 0, NULL, &same); boxaDestroy(&boxa1); boxaDestroy(&boxa2); if (!same) success = FALSE; } if (success) fprintf(stderr, "Good: transpose is reversible\n"); else fprintf(stderr, "Bad: transpose failed\n"); pixa2 = pixaDisplayBoxaa(pixa1, baa2, L_DRAW_RGB, 2); pix1 = pixaDisplayTiledInRows(pixa2, 32, 1400, 1.0, 0, 10, 0); pixDisplay(pix1, 0, 600); fprintf(stderr, "Writing to: /tmp/lept/boxa/show.pdf\n"); pixaConvertToPdf(pixa2, 75, 1.0, 0, 0, NULL, "/tmp/lept/boxa/show.pdf"); pixDestroy(&pix1); pixaDestroy(&pixa1); pixaDestroy(&pixa2); boxaaDestroy(&baa1); boxaaDestroy(&baa2); boxaaDestroy(&baa3); return 0; }
main(int argc, char **argv) { char *filein, *fileout; l_uint8 *array1, *array2, *dataout, *dataout2; l_int32 i, blocksize; size_t nbytes, nout, nout2; BBUFFER *bb, *bb2; FILE *fp; static char mainName[] = "buffertest"; if (argc != 3) exit(ERROR_INT(" Syntax: buffertest filein fileout", mainName, 1)); filein = argv[1]; fileout = argv[2]; if ((array1 = l_binaryRead(filein, &nbytes)) == NULL) exit(ERROR_INT("array not made", mainName, 1)); fprintf(stderr, " Bytes read from file: %ld\n", nbytes); /* Application of byte buffer ops: compress/decompress in memory */ #if 1 dataout = zlibCompress(array1, nbytes, &nout); l_binaryWrite(fileout, "w", dataout, nout); dataout2 = zlibUncompress(dataout, nout, &nout2); l_binaryWrite("/tmp/junktest", "w", dataout2, nout2); fprintf(stderr, "nbytes in = %ld, nbytes comp = %ld, nbytes uncomp = %ld\n", nbytes, nout, nout2); lept_free(dataout); lept_free(dataout2); #endif /* Low-level byte buffer read/write test */ #if 0 bb = bbufferCreate(array1, nbytes); bbufferRead(bb, array1, nbytes); array2 = (l_uint8 *)lept_calloc(2 * nbytes, sizeof(l_uint8)); fprintf(stderr, " Bytes initially in buffer: %d\n", bb->n); blocksize = (2 * nbytes) / NBLOCKS; for (i = 0; i <= NBLOCKS; i++) { bbufferWrite(bb, array2, blocksize, &nout); fprintf(stderr, " block %d: wrote %d bytes\n", i + 1, nout); } fprintf(stderr, " Bytes left in buffer: %d\n", bb->n); bb2 = bbufferCreate(NULL, 0); bbufferRead(bb2, array1, nbytes); fp = lept_fopen(fileout, "wb"); bbufferWriteStream(bb2, fp, nbytes, &nout); fprintf(stderr, " bytes written out to fileout: %d\n", nout); bbufferDestroy(&bb); bbufferDestroy(&bb2); lept_free(array2); #endif lept_free(array1); return 0; }
/*! * \brief strcodeFinalize() * * \param[in,out] pstrcode destroys after .c and .h files have been generated * \param[in] outdir [optional] if NULL, files are made in /tmp/lept/auto * \return void */ l_int32 strcodeFinalize(L_STRCODE **pstrcode, const char *outdir) { char buf[256]; char *filestr, *casestr, *descr, *datastr, *realoutdir; l_int32 actstart, end, newstart, fileno, nbytes; size_t size; L_STRCODE *strcode; SARRAY *sa1, *sa2, *sa3; PROCNAME("strcodeFinalize"); lept_mkdir("lept/auto"); if (!pstrcode || *pstrcode == NULL) return ERROR_INT("No input data", procName, 1); strcode = *pstrcode; if (!outdir) { L_INFO("no outdir specified; writing to /tmp/lept/auto\n", procName); realoutdir = stringNew("/tmp/lept/auto"); } else { realoutdir = stringNew(outdir); } /* ------------------------------------------------------- */ /* Make the output autogen.*.c file */ /* ------------------------------------------------------- */ /* Make array of textlines from TEMPLATE1 */ if ((filestr = (char *)l_binaryRead(TEMPLATE1, &size)) == NULL) return ERROR_INT("filestr not made", procName, 1); if ((sa1 = sarrayCreateLinesFromString(filestr, 1)) == NULL) return ERROR_INT("sa1 not made", procName, 1); LEPT_FREE(filestr); if ((sa3 = sarrayCreate(0)) == NULL) return ERROR_INT("sa3 not made", procName, 1); /* Copyright notice */ sarrayParseRange(sa1, 0, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa1, actstart, end); /* File name comment */ fileno = strcode->fileno; snprintf(buf, sizeof(buf), " * autogen.%d.c", fileno); sarrayAddString(sa3, buf, L_COPY); /* More text */ sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa1, actstart, end); /* Description of function types by index */ descr = sarrayToString(strcode->descr, 1); descr[strlen(descr) - 1] = '\0'; sarrayAddString(sa3, descr, L_INSERT); /* Includes */ sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa1, actstart, end); snprintf(buf, sizeof(buf), "#include \"autogen.%d.h\"", fileno); sarrayAddString(sa3, buf, L_COPY); /* Header for auto-generated deserializers */ sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa1, actstart, end); /* Function name (as comment) */ snprintf(buf, sizeof(buf), " * l_autodecode_%d()", fileno); sarrayAddString(sa3, buf, L_COPY); /* Input and return values */ sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa1, actstart, end); /* Function name */ snprintf(buf, sizeof(buf), "l_autodecode_%d(l_int32 index)", fileno); sarrayAddString(sa3, buf, L_COPY); /* Stack vars */ sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa1, actstart, end); /* Declaration of nfunc on stack */ snprintf(buf, sizeof(buf), "l_int32 nfunc = %d;\n", strcode->n); sarrayAddString(sa3, buf, L_COPY); /* Declaration of PROCNAME */ snprintf(buf, sizeof(buf), " PROCNAME(\"l_autodecode_%d\");", fileno); sarrayAddString(sa3, buf, L_COPY); /* Test input variables */ sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa1, actstart, end); /* Insert case string */ casestr = sarrayToString(strcode->function, 0); casestr[strlen(casestr) - 1] = '\0'; sarrayAddString(sa3, casestr, L_INSERT); /* End of function */ sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa1, actstart, end); /* Flatten to string and output to autogen*.c file */ if ((filestr = sarrayToString(sa3, 1)) == NULL) return ERROR_INT("filestr from sa3 not made", procName, 1); nbytes = strlen(filestr); snprintf(buf, sizeof(buf), "%s/autogen.%d.c", realoutdir, fileno); l_binaryWrite(buf, "w", filestr, nbytes); LEPT_FREE(filestr); sarrayDestroy(&sa1); sarrayDestroy(&sa3); /* ------------------------------------------------------- */ /* Make the output autogen.*.h file */ /* ------------------------------------------------------- */ /* Make array of textlines from TEMPLATE2 */ if ((filestr = (char *)l_binaryRead(TEMPLATE2, &size)) == NULL) return ERROR_INT("filestr not made", procName, 1); if ((sa2 = sarrayCreateLinesFromString(filestr, 1)) == NULL) return ERROR_INT("sa2 not made", procName, 1); LEPT_FREE(filestr); if ((sa3 = sarrayCreate(0)) == NULL) return ERROR_INT("sa3 not made", procName, 1); /* Copyright notice */ sarrayParseRange(sa2, 0, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* File name comment */ snprintf(buf, sizeof(buf), " * autogen.%d.h", fileno); sarrayAddString(sa3, buf, L_COPY); /* More text */ sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* Beginning header protection */ snprintf(buf, sizeof(buf), "#ifndef LEPTONICA_AUTOGEN_%d_H\n" "#define LEPTONICA_AUTOGEN_%d_H", fileno, fileno); sarrayAddString(sa3, buf, L_COPY); /* Prototype header text */ sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* Prototype declaration */ snprintf(buf, sizeof(buf), "void *l_autodecode_%d(l_int32 index);", fileno); sarrayAddString(sa3, buf, L_COPY); /* Prototype trailer text */ sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa3, sa2, actstart, end); /* Insert serialized data strings */ datastr = sarrayToString(strcode->data, 1); datastr[strlen(datastr) - 1] = '\0'; sarrayAddString(sa3, datastr, L_INSERT); /* End header protection */ snprintf(buf, sizeof(buf), "#endif /* LEPTONICA_AUTOGEN_%d_H */", fileno); sarrayAddString(sa3, buf, L_COPY); /* Flatten to string and output to autogen*.h file */ if ((filestr = sarrayToString(sa3, 1)) == NULL) return ERROR_INT("filestr from sa3 not made", procName, 1); nbytes = strlen(filestr); snprintf(buf, sizeof(buf), "%s/autogen.%d.h", realoutdir, fileno); l_binaryWrite(buf, "w", filestr, nbytes); LEPT_FREE(filestr); LEPT_FREE(realoutdir); sarrayDestroy(&sa2); sarrayDestroy(&sa3); /* Cleanup */ strcodeDestroy(pstrcode); return 0; }
/* * fmorphautogen2() * * Input: sela * fileindex * filename (<optional>; can be null) * Return: 0 if OK; 1 on error * * Notes: * (1) This function uses morphtemplate2.txt to create a * low-level file that contains the low-level functions for * implementing dilation and erosion for every sel * in the input sela. * (2) The fileindex parameter is inserted into the output * filename, as described below. * (3) If filename == NULL, the output file is fmorphgenlow.<n>.c, * where <n> is equal to the 'fileindex' parameter. * (4) If filename != NULL, the output file is <filename>low.<n>.c. */ l_int32 fmorphautogen2(SELA *sela, l_int32 fileindex, const char *filename) { char *filestr, *linestr, *fname; char *str_doc1, *str_doc2, *str_doc3, *str_doc4, *str_def1; char bigbuf[L_BUF_SIZE]; char breakstring[] = " break;"; char staticstring[] = "static void"; l_int32 i, nsels, nbytes, actstart, end, newstart; l_int32 argstart, argend, loopstart, loopend, finalstart, finalend; size_t size; SARRAY *sa1, *sa2, *sa3, *sa4, *sa5, *sa6; SEL *sel; PROCNAME("fmorphautogen2"); if (!sela) return ERROR_INT("sela not defined", procName, 1); if (fileindex < 0) fileindex = 0; if ((nsels = selaGetCount(sela)) == 0) return ERROR_INT("no sels in sela", procName, 1); /* Make the array of textlines from morphtemplate2.txt */ if ((filestr = (char *)l_binaryRead(TEMPLATE2, &size)) == NULL) return ERROR_INT("filestr not made", procName, 1); sa1 = sarrayCreateLinesFromString(filestr, 1); LEPT_FREE(filestr); if (!sa1) return ERROR_INT("sa1 not made", procName, 1); /* Make the array of static function names */ if ((sa2 = sarrayCreate(2 * nsels)) == NULL) { sarrayDestroy(&sa1); return ERROR_INT("sa2 not made", procName, 1); } for (i = 0; i < nsels; i++) { sprintf(bigbuf, "fdilate_%d_%d", fileindex, i); sarrayAddString(sa2, bigbuf, L_COPY); sprintf(bigbuf, "ferode_%d_%d", fileindex, i); sarrayAddString(sa2, bigbuf, L_COPY); } /* Make the static prototype strings */ sa3 = sarrayCreate(2 * nsels); /* should be ok */ for (i = 0; i < 2 * nsels; i++) { fname = sarrayGetString(sa2, i, L_NOCOPY); sprintf(bigbuf, "static void %s%s", fname, PROTOARGS); sarrayAddString(sa3, bigbuf, L_COPY); } /* Make strings containing function names */ sprintf(bigbuf, " * l_int32 fmorphopgen_low_%d()", fileindex); str_doc1 = stringNew(bigbuf); sprintf(bigbuf, " * void fdilate_%d_*()", fileindex); str_doc2 = stringNew(bigbuf); sprintf(bigbuf, " * void ferode_%d_*()", fileindex); str_doc3 = stringNew(bigbuf); sprintf(bigbuf, " * fmorphopgen_low_%d()", fileindex); str_doc4 = stringNew(bigbuf); sprintf(bigbuf, "fmorphopgen_low_%d(l_uint32 *datad,", fileindex); str_def1 = stringNew(bigbuf); /* Output to this sa */ sa4 = sarrayCreate(0); /* Copyright notice and info header */ sarrayParseRange(sa1, 0, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa4, sa1, actstart, end); /* Insert function names as documentation */ sarrayAddString(sa4, str_doc1, L_INSERT); sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa4, sa1, actstart, end); sarrayAddString(sa4, str_doc2, L_INSERT); sarrayAddString(sa4, str_doc3, L_INSERT); sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa4, sa1, actstart, end); /* Insert static protos */ for (i = 0; i < 2 * nsels; i++) { if ((linestr = sarrayGetString(sa3, i, L_COPY)) == NULL) { sarrayDestroy(&sa1); sarrayDestroy(&sa2); sarrayDestroy(&sa3); sarrayDestroy(&sa4); return ERROR_INT("linestr not retrieved", procName, 1); } sarrayAddString(sa4, linestr, L_INSERT); } /* Insert function header */ sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa4, sa1, actstart, end); sarrayAddString(sa4, str_doc4, L_INSERT); sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa4, sa1, actstart, end); sarrayAddString(sa4, str_def1, L_INSERT); sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa4, sa1, actstart, end); /* Generate and insert the dispatcher code */ for (i = 0; i < 2 * nsels; i++) { sprintf(bigbuf, " case %d:", i); sarrayAddString(sa4, bigbuf, L_COPY); sprintf(bigbuf, " %s(datad, w, h, wpld, datas, wpls);", sarrayGetString(sa2, i, L_NOCOPY)); sarrayAddString(sa4, bigbuf, L_COPY); sarrayAddString(sa4, breakstring, L_COPY); } /* Finish the dispatcher and introduce the low-level code */ sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0); sarrayAppendRange(sa4, sa1, actstart, end); /* Get the range for the args common to all functions */ sarrayParseRange(sa1, newstart, &argstart, &argend, &newstart, "--", 0); /* Get the range for the loop code common to all functions */ sarrayParseRange(sa1, newstart, &loopstart, &loopend, &newstart, "--", 0); /* Get the range for the ending code common to all functions */ sarrayParseRange(sa1, newstart, &finalstart, &finalend, &newstart, "--", 0); /* Do all the static functions */ for (i = 0; i < 2 * nsels; i++) { /* Generate the function header and add the common args */ sarrayAddString(sa4, staticstring, L_COPY); fname = sarrayGetString(sa2, i, L_NOCOPY); sprintf(bigbuf, "%s(l_uint32 *datad,", fname); sarrayAddString(sa4, bigbuf, L_COPY); sarrayAppendRange(sa4, sa1, argstart, argend); /* Declare and define wplsN args, as necessary */ if ((sel = selaGetSel(sela, i/2)) == NULL) { sarrayDestroy(&sa1); sarrayDestroy(&sa2); sarrayDestroy(&sa3); sarrayDestroy(&sa4); return ERROR_INT("sel not returned", procName, 1); } sa5 = sarrayMakeWplsCode(sel); sarrayJoin(sa4, sa5); sarrayDestroy(&sa5); /* Add the function loop code */ sarrayAppendRange(sa4, sa1, loopstart, loopend); /* Insert barrel-op code for *dptr */ sa6 = sarrayMakeInnerLoopDWACode(sel, i); sarrayJoin(sa4, sa6); sarrayDestroy(&sa6); /* Finish the function code */ sarrayAppendRange(sa4, sa1, finalstart, finalend); } /* Output to file */ filestr = sarrayToString(sa4, 1); nbytes = strlen(filestr); if (filename) snprintf(bigbuf, L_BUF_SIZE, "%slow.%d.c", filename, fileindex); else sprintf(bigbuf, "%slow.%d.c", OUTROOT, fileindex); l_binaryWrite(bigbuf, "w", filestr, nbytes); sarrayDestroy(&sa1); sarrayDestroy(&sa2); sarrayDestroy(&sa3); sarrayDestroy(&sa4); LEPT_FREE(filestr); return 0; }
/* * parseForProtos() * * Input: filein (output of cpp) * prestring (<optional> string that prefaces each decl; * use NULL to omit) * Return: parsestr (string of function prototypes), or NULL on error * * Notes: * (1) We parse the output of cpp: * cpp -ansi <filein> * Three plans were attempted, with success on the third. * (2) Plan 1. A cursory examination of the cpp output indicated that * every function was preceded by a cpp comment statement. * So we just need to look at statements beginning after comments. * Unfortunately, this is NOT the case. Some functions start * without cpp comment lines, typically when there are no * comments in the source that immediately precede the function. * (3) Plan 2. Consider the keywords in the language that start * parts of the cpp file. Some, like 'typedef', 'enum', * 'union' and 'struct', are followed after a while by '{', * and eventually end with '}, plus an optional token and a * final ';' Others, like 'extern' and 'static', are never * the beginnings of global function definitions. Function * prototypes have one or more sets of '(' followed eventually * by a ')', and end with ';'. But function definitions have * tokens, followed by '(', more tokens, ')' and then * immediately a '{'. We would generate a prototype from this * by adding a ';' to all tokens up to the ')'. So we use * these special tokens to decide what we are parsing. And * whenever a function definition is found and the prototype * extracted, we skip through the rest of the function * past the corresponding '}'. This token ends a line, and * is often on a line of its own. But as it turns out, * the only keyword we need to consider is 'static'. * (4) Plan 3. Consider the parentheses and braces for various * declarations. A struct, enum, or union has a pair of * braces followed by a semicolon. They cannot have parentheses * before the left brace, but a struct can have lots of parentheses * within the brace set. A function prototype has no braces. * A function declaration can have sets of left and right * parentheses, but these are followed by a left brace. * So plan 3 looks at the way parentheses and braces are * organized. Once the beginning of a function definition * is found, the prototype is extracted and we search for * the ending right brace. * (5) To find the ending right brace, it is necessary to do some * careful parsing. For example, in this file, we have * left and right braces as characters, and these must not * be counted. Somewhat more tricky, the file fhmtauto.c * generates code, and includes a right brace in a string. * So we must not include braces that are in strings. But how * do we know if something is inside a string? Keep state, * starting with not-inside, and every time you hit a double quote * that is not escaped, toggle the condition. Any brace * found in the state of being within a string is ignored. * (6) When a prototype is extracted, it is put in a canonical * form (i.e., cleaned up). Finally, we check that it is * not static and save it. (If static, it is ignored). * (7) The @prestring for unix is NULL; it is included here so that * you can use Microsoft's declaration for importing or * exporting to a dll. See environ.h for examples of use. * Here, we set: @prestring = "LEPT_DLL ". Note in particular * the space character that will separate 'LEPT_DLL' from * the standard unix prototype that follows. */ char * parseForProtos(const char *filein, const char *prestring) { char *strdata, *str, *newstr, *parsestr, *secondword; l_int32 start, next, stop, charindex, found; size_t nbytes; SARRAY *sa, *saout, *satest; PROCNAME("parseForProtos"); if (!filein) return (char *)ERROR_PTR("filein not defined", procName, NULL); /* Read in the cpp output into memory, one string for each * line in the file, omitting blank lines. */ strdata = (char *)l_binaryRead(filein, &nbytes); sa = sarrayCreateLinesFromString(strdata, 0); saout = sarrayCreate(0); next = 0; while (1) { /* repeat after each non-static prototype is extracted */ searchForProtoSignature(sa, next, &start, &stop, &charindex, &found); if (!found) break; /* fprintf(stderr, " start = %d, stop = %d, charindex = %d\n", start, stop, charindex); */ str = captureProtoSignature(sa, start, stop, charindex); /* Make sure that the signature found by cpp is neither * static nor extern. We get 'extern' declarations from * header files, and with some versions of cpp running on * #include <sys/stat.h> we get something of the form: * extern ... (( ... )) ... ( ... ) { ... * For this, the 1st '(' is the lp, the 2nd ')' is the rp, * and there is a lot of garbage between the rp and the lb. * It is easiest to simply reject any signature that starts * with 'extern'. Note also that an 'extern' token has been * prepended to each prototype, so the 'static' or * 'extern' keywords we are looking for, if they exist, * would be the second word. */ satest = sarrayCreateWordsFromString(str); secondword = sarrayGetString(satest, 1, L_NOCOPY); if (strcmp(secondword, "static") && /* not static */ strcmp(secondword, "extern")) { /* not extern */ if (prestring) { /* prepend it to the prototype */ newstr = stringJoin(prestring, str); sarrayAddString(saout, newstr, L_INSERT); LEPT_FREE(str); } else { sarrayAddString(saout, str, L_INSERT); } } else { LEPT_FREE(str); } sarrayDestroy(&satest); skipToEndOfFunction(sa, stop, charindex, &next); if (next == -1) break; } /* Flatten into a string with newlines between prototypes */ parsestr = sarrayToString(saout, 1); LEPT_FREE(strdata); sarrayDestroy(&sa); sarrayDestroy(&saout); return parsestr; }
int main(int argc, char **argv) { l_uint8 *data1, *data2; l_int32 i, same, w, h, width, success, nba; size_t size1, size2; l_float32 diffarea, diffxor, scalefact; BOX *box; BOXA *boxa1, *boxa2, *boxa3; BOXAA *baa1, *baa2, *baa3; PIX *pix1, *pixdb; PIXA *pixa1, *pixa2; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; lept_mkdir("lept/boxa"); /* Make a boxa and display its contents */ boxa1 = boxaCreate(6); box = boxCreate(60, 60, 40, 20); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(120, 50, 20, 50); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(50, 140, 46, 60); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(166, 130, 64, 28); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(64, 224, 44, 34); boxaAddBox(boxa1, box, L_INSERT); box = boxCreate(117, 206, 26, 74); boxaAddBox(boxa1, box, L_INSERT); pix1 = DisplayBoxa(boxa1); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 0 */ pixDisplayWithTitle(pix1, 0, 0, NULL, rp->display); pixDestroy(&pix1); boxaCompareRegions(boxa1, boxa1, 100, &same, &diffarea, &diffxor, NULL); regTestCompareValues(rp, 1, same, 0.0); /* 1 */ regTestCompareValues(rp, 0.0, diffarea, 0.0); /* 2 */ regTestCompareValues(rp, 0.0, diffxor, 0.0); /* 3 */ boxa2 = boxaTransform(boxa1, -13, -13, 1.0, 1.0); boxaCompareRegions(boxa1, boxa2, 10, &same, &diffarea, &diffxor, NULL); regTestCompareValues(rp, 1, same, 0.0); /* 4 */ regTestCompareValues(rp, 0.0, diffarea, 0.0); /* 5 */ regTestCompareValues(rp, 0.0, diffxor, 0.0); /* 6 */ boxaDestroy(&boxa2); boxa2 = boxaReconcileEvenOddHeight(boxa1, L_ADJUST_TOP_AND_BOT, 6, L_ADJUST_CHOOSE_MIN, 1.0, 0); pix1 = DisplayBoxa(boxa2); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 7 */ pixDisplayWithTitle(pix1, 200, 0, NULL, rp->display); pixDestroy(&pix1); boxaCompareRegions(boxa1, boxa2, 10, &same, &diffarea, &diffxor, &pixdb); regTestCompareValues(rp, 1, same, 0.0); /* 8 */ regTestCompareValues(rp, 0.053, diffarea, 0.002); /* 9 */ regTestCompareValues(rp, 0.240, diffxor, 0.002); /* 10 */ regTestWritePixAndCheck(rp, pixdb, IFF_PNG); /* 11 */ pixDisplayWithTitle(pixdb, 400, 0, NULL, rp->display); pixDestroy(&pixdb); boxaDestroy(&boxa1); boxaDestroy(&boxa2); /* Input is a fairly clean boxa */ boxa1 = boxaRead("boxa1.ba"); boxa2 = boxaReconcileEvenOddHeight(boxa1, L_ADJUST_TOP, 80, L_ADJUST_CHOOSE_MIN, 1.05, 1); width = 100; boxaGetExtent(boxa2, &w, &h, NULL); scalefact = (l_float32)width / (l_float32)w; boxa3 = boxaTransform(boxa2, 0, 0, scalefact, scalefact); pix1 = boxaDisplayTiled(boxa3, NULL, 1500, 2, 1.0, 0, 3, 2); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 12 */ pixDisplayWithTitle(pix1, 600, 0, NULL, rp->display); pixDestroy(&pix1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); boxaDestroy(&boxa3); /* Input is an unsmoothed and noisy boxa */ boxa1 = boxaRead("boxa2.ba"); boxa2 = boxaReconcileEvenOddHeight(boxa1, L_ADJUST_TOP, 80, L_ADJUST_CHOOSE_MIN, 1.05, 1); width = 100; boxaGetExtent(boxa2, &w, &h, NULL); scalefact = (l_float32)width / (l_float32)w; boxa3 = boxaTransform(boxa2, 0, 0, scalefact, scalefact); pix1 = boxaDisplayTiled(boxa3, NULL, 1500, 2, 1.0, 0, 3, 2); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 13 */ pixDisplayWithTitle(pix1, 800, 0, NULL, rp->display); pixDestroy(&pix1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); boxaDestroy(&boxa3); /* Input is a boxa smoothed with a median window filter */ boxa1 = boxaRead("boxa3.ba"); boxa2 = boxaReconcileEvenOddHeight(boxa1, L_ADJUST_TOP, 80, L_ADJUST_CHOOSE_MIN, 1.05, 1); width = 100; boxaGetExtent(boxa2, &w, &h, NULL); scalefact = (l_float32)width / (l_float32)w; boxa3 = boxaTransform(boxa2, 0, 0, scalefact, scalefact); pix1 = boxaDisplayTiled(boxa3, NULL, 1500, 2, 1.0, 0, 3, 2); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 14 */ pixDisplayWithTitle(pix1, 1000, 0, NULL, rp->display); pixDestroy(&pix1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); boxaDestroy(&boxa3); /* Test serialized boxa I/O to and from memory */ data1 = l_binaryRead("boxa2.ba", &size1); boxa1 = boxaReadMem(data1, size1); boxaWriteMem(&data2, &size2, boxa1); boxa2 = boxaReadMem(data2, size2); boxaWrite("/tmp/lept/boxa/boxa1.ba", boxa1); boxaWrite("/tmp/lept/boxa/boxa2.ba", boxa2); filesAreIdentical("/tmp/lept/boxa/boxa1.ba", "/tmp/lept/boxa/boxa2.ba", &same); regTestCompareValues(rp, 1, same, 0.0); /* 15 */ boxaDestroy(&boxa1); boxaDestroy(&boxa2); lept_free(data1); lept_free(data2); /* ----------- Test pixaDisplayBoxaa() ------------ */ pixa1 = pixaReadBoth("showboxes.pac"); baa1 = boxaaRead("showboxes1.baa"); baa2 = boxaaTranspose(baa1); baa3 = boxaaTranspose(baa2); nba = boxaaGetCount(baa1); success = TRUE; for (i = 0; i < nba; i++) { boxa1 = boxaaGetBoxa(baa1, i, L_CLONE); boxa2 = boxaaGetBoxa(baa3, i, L_CLONE); boxaEqual(boxa1, boxa2, 0, NULL, &same); boxaDestroy(&boxa1); boxaDestroy(&boxa2); if (!same) success = FALSE; } /* Check that the transpose is reversible */ regTestCompareValues(rp, 1, success, 0.0); /* 16 */ pixa2 = pixaDisplayBoxaa(pixa1, baa2, L_DRAW_RGB, 2); pix1 = pixaDisplayTiledInRows(pixa2, 32, 1400, 1.0, 0, 10, 0); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 17 */ pixDisplayWithTitle(pix1, 0, 600, NULL, rp->display); fprintf(stderr, "Writing to: /tmp/lept/boxa/show.pdf\n"); l_pdfSetDateAndVersion(FALSE); pixaConvertToPdf(pixa2, 75, 1.0, 0, 0, NULL, "/tmp/lept/boxa/show.pdf"); regTestCheckFile(rp, "/tmp/lept/boxa/show.pdf"); /* 18 */ pixDestroy(&pix1); pixaDestroy(&pixa1); pixaDestroy(&pixa2); boxaaDestroy(&baa1); boxaaDestroy(&baa2); boxaaDestroy(&baa3); return regTestCleanup(rp); }
int main(int argc, char **argv) { l_int32 ignore; size_t nbytesin, nbytesout; char *infile, *instring, *outstring; SARRAY *sa1, *sa2, *sa3, *sa4, *sa5; char buf[256]; static char mainName[] = "string_reg"; if (argc != 2) return ERROR_INT(" Syntax: string_reg infile", mainName, 1); infile = argv[1]; instring = (char *)l_binaryRead(infile, &nbytesin); if (!instring) return ERROR_INT("file not read", mainName, 1); sa1 = sarrayCreateWordsFromString(instring); sa2 = sarrayCreateLinesFromString(instring, 0); sa3 = sarrayCreateLinesFromString(instring, 1); outstring = sarrayToString(sa1, 0); nbytesout = strlen(outstring); l_binaryWrite("/tmp/junk1.txt", "w", outstring, nbytesout); lept_free(outstring); outstring = sarrayToString(sa1, 1); nbytesout = strlen(outstring); l_binaryWrite("/tmp/junk2.txt", "w", outstring, nbytesout); lept_free(outstring); outstring = sarrayToString(sa2, 0); nbytesout = strlen(outstring); l_binaryWrite("/tmp/junk3.txt", "w", outstring, nbytesout); lept_free(outstring); outstring = sarrayToString(sa2, 1); nbytesout = strlen(outstring); l_binaryWrite("/tmp/junk4.txt", "w", outstring, nbytesout); lept_free(outstring); outstring = sarrayToString(sa3, 0); nbytesout = strlen(outstring); l_binaryWrite("/tmp/junk5.txt", "w", outstring, nbytesout); lept_free(outstring); outstring = sarrayToString(sa3, 1); nbytesout = strlen(outstring); l_binaryWrite("/tmp/junk6.txt", "w", outstring, nbytesout); lept_free(outstring); sprintf(buf, "diff -s /tmp/junk6.txt %s", infile); ignore = system(buf); /* write/read/write; compare /tmp/junkout5 with /tmp/junkout6 */ sarrayWrite("/tmp/junk7.txt", sa2); sarrayWrite("/tmp/junk8.txt", sa3); sa4 = sarrayRead("/tmp/junk8.txt"); sarrayWrite("/tmp/junk9.txt", sa4); sa5 = sarrayRead("/tmp/junk9.txt"); ignore = system("diff -s /tmp/junk8.txt /tmp/junk9.txt"); sarrayDestroy(&sa1); sarrayDestroy(&sa2); sarrayDestroy(&sa3); sarrayDestroy(&sa4); sarrayDestroy(&sa5); lept_free(instring); return 0; }
int main(int argc, char **argv) { char buf[512]; char *pathname, *datastr, *formstr; l_uint8 *data1, *data2; l_int32 i, bl1, bl2, bl3, sbytes, formbytes, fontsize, rbytes; size_t nbytes; PIX *pix1, *pix2, *pixd; PIXA *pixa; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* ------------ Generate pixa char bitmap files from file ----------- */ lept_rmdir("filefonts"); lept_mkdir("filefonts"); for (i = 0; i < 9; i++) { pixaSaveFont("fonts", "/tmp/filefonts", sizes[i]); pathname = genPathname("/tmp/filefonts", outputfonts[i]); pixa = pixaRead(pathname); if (rp->display) { fprintf(stderr, "Found %d chars in font size %d\n", pixaGetCount(pixa), sizes[i]); } pixd = pixaDisplayTiled(pixa, 1500, 0, 15); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 0 - 8 */ if (i == 2) pixDisplayWithTitle(pixd, 100, 0, NULL, rp->display); pixDestroy(&pixd); pixaDestroy(&pixa); lept_free(pathname); } lept_rmdir("filefonts"); /* ---------- Generate pixa char bitmap files from string --------- */ lept_rmdir("strfonts"); lept_mkdir("strfonts"); for (i = 0; i < 9; i++) { pixaSaveFont(NULL, "/tmp/strfonts", sizes[i]); pathname = genPathname("/tmp/strfonts", outputfonts[i]); pixa = pixaRead(pathname); if (rp->display) { fprintf(stderr, "Found %d chars in font size %d\n", pixaGetCount(pixa), sizes[i]); } pixd = pixaDisplayTiled(pixa, 1500, 0, 15); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 9 - 17 */ if (i == 2) pixDisplayWithTitle(pixd, 100, 150, NULL, rp->display); pixDestroy(&pixd); pixaDestroy(&pixa); lept_free(pathname); } /* ----- Use pixaGetFont() and write the result out -----*/ lept_rmdir("pafonts"); lept_mkdir("pafonts"); for (i = 0; i < 9; i++) { pixa = pixaGetFont("/tmp/strfonts", sizes[i], &bl1, &bl2, &bl3); fprintf(stderr, "Baselines are at: %d, %d, %d\n", bl1, bl2, bl3); snprintf(buf, sizeof(buf), "/tmp/pafonts/chars-%d.pa", sizes[i]); pixaWrite(buf, pixa); if (i == 2) { pixd = pixaDisplayTiled(pixa, 1500, 0, 15); pixDisplayWithTitle(pixd, 100, 300, NULL, rp->display); pixDestroy(&pixd); } pixaDestroy(&pixa); } lept_rmdir("pafonts"); /* ------- Generate 4/3 encoded ascii strings from tiff files ------ */ lept_rmdir("fontencode"); lept_mkdir("fontencode"); for (i = 0; i < 9; i++) { fontsize = 2 * i + 4; pathname = genPathname("fonts", inputfonts[i]); data1 = l_binaryRead(pathname, &nbytes); datastr = encodeBase64(data1, nbytes, &sbytes); if (rp->display) fprintf(stderr, "nbytes = %lu, sbytes = %d\n", (unsigned long)nbytes, sbytes); formstr = reformatPacked64(datastr, sbytes, 4, 72, 1, &formbytes); snprintf(buf, sizeof(buf), "/tmp/fontencode/formstr_%d.txt", fontsize); l_binaryWrite(buf, "w", formstr, formbytes); regTestCheckFile(rp, buf); /* 18-26 */ if (i == 8) pix1 = pixReadMem(data1, nbytes); /* original */ FREE(data1); data2 = decodeBase64(datastr, sbytes, &rbytes); snprintf(buf, sizeof(buf), "/tmp/fontencode/image_%d.tif", fontsize); l_binaryWrite(buf, "w", data2, rbytes); if (i == 8) { pix2 = pixReadMem(data2, rbytes); /* encode/decode */ regTestComparePix(rp, pix1, pix2); /* 27 */ pixDestroy(&pix1); pixDestroy(&pix2); } FREE(data2); FREE(pathname); FREE(datastr); FREE(formstr); } /* ------------ Get timing for font generation ----------- */ startTimer(); for (i = 0; i < 100; i++) { pixa = pixaGenerateFontFromString(sizes[5], &bl1, &bl2, &bl3); pixaDestroy(&pixa); } fprintf(stderr, "Time for font gen = %7.4f sec\n", stopTimer() / 100.0); return regTestCleanup(rp); }
/*! * kernelCreateFromFile() * * Input: filename * Return: kernel, or null on error * * Notes: * (1) The file contains, in the following order: * - Any number of comment lines starting with '#' are ignored * - The height and width of the kernel * - The y and x values of the kernel origin * - The kernel data, formatted as lines of numbers (integers * or floats) for the kernel values in row-major order, * and with no other punctuation. * (Note: this differs from kernelCreateFromString(), * where each line must begin and end with a double-quote * to tell the compiler it's part of a string.) * - The kernel specification ends when a blank line, * a comment line, or the end of file is reached. * (2) All lines must be left-justified. * (3) See kernelCreateFromString() for a description of the string * format for the kernel data. As an example, here are the lines * of a valid kernel description file In the file, all lines * are left-justified: * # small 3x3 kernel * 3 3 * 1 1 * 25.5 51 24.3 * 70.2 146.3 73.4 * 20 50.9 18.4 */ L_KERNEL * kernelCreateFromFile(const char *filename) { char *filestr, *line; l_int32 nlines, i, j, first, index, w, h, cx, cy, n; l_float32 val; size_t size; NUMA *na, *nat; SARRAY *sa; L_KERNEL *kel; PROCNAME("kernelCreateFromFile"); if (!filename) return (L_KERNEL *)ERROR_PTR("filename not defined", procName, NULL); filestr = (char *)l_binaryRead(filename, &size); sa = sarrayCreateLinesFromString(filestr, 1); FREE(filestr); nlines = sarrayGetCount(sa); /* Find the first data line. */ for (i = 0; i < nlines; i++) { line = sarrayGetString(sa, i, L_NOCOPY); if (line[0] != '#') { first = i; break; } } /* Find the kernel dimensions and origin location. */ line = sarrayGetString(sa, first, L_NOCOPY); if (sscanf(line, "%d %d", &h, &w) != 2) return (L_KERNEL *)ERROR_PTR("error reading h,w", procName, NULL); line = sarrayGetString(sa, first + 1, L_NOCOPY); if (sscanf(line, "%d %d", &cy, &cx) != 2) return (L_KERNEL *)ERROR_PTR("error reading cy,cx", procName, NULL); /* Extract the data. This ends when we reach eof, or when we * encounter a line of data that is either a null string or * contains just a newline. */ na = numaCreate(0); for (i = first + 2; i < nlines; i++) { line = sarrayGetString(sa, i, L_NOCOPY); if (line[0] == '\0' || line[0] == '\n' || line[0] == '#') break; nat = parseStringForNumbers(line, " \t\n"); numaJoin(na, nat, 0, -1); numaDestroy(&nat); } sarrayDestroy(&sa); n = numaGetCount(na); if (n != w * h) { numaDestroy(&na); fprintf(stderr, "w = %d, h = %d, num ints = %d\n", w, h, n); return (L_KERNEL *)ERROR_PTR("invalid integer data", procName, NULL); } kel = kernelCreate(h, w); kernelSetOrigin(kel, cy, cx); index = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { numaGetFValue(na, index, &val); kernelSetElement(kel, i, j, val); index++; } } numaDestroy(&na); return kel; }
main(int argc, char **argv) { char *str; l_uint8 *data1, *data2; l_int32 i, n, start, end, same1, same2; size_t size1, size2; FILE *fp; L_DNA *da; SARRAY *sa; L_BYTEA *lba1, *lba2, *lba3, *lba4, *lba5; static char mainName[] = "byteatest"; if (argc != 1) exit(ERROR_INT("syntax: whatever11", mainName, 1)); /* Test basic init, join and split */ lba1 = l_byteaInitFromFile("feyn.tif"); lba2 = l_byteaInitFromFile("test24.jpg"); size1 = l_byteaGetSize(lba1); size2 = l_byteaGetSize(lba2); l_byteaJoin(lba1, &lba2); lba3 = l_byteaInitFromMem(lba1->data, size1); lba4 = l_byteaInitFromMem(lba1->data + size1, size2); /* Split by hand */ l_binaryWrite("junk1", "w", lba3->data, lba3->size); l_binaryWrite("junk2", "w", lba4->data, lba4->size); filesAreIdentical("feyn.tif", "junk1", &same1); filesAreIdentical("test24.jpg", "junk2", &same2); if (same1 && same2) fprintf(stderr, "OK for join file\n"); else fprintf(stderr, "Error: files are different!\n"); /* Split by function */ l_byteaSplit(lba1, size1, &lba5); l_binaryWrite("junk3", "w", lba1->data, lba1->size); l_binaryWrite("junk4", "w", lba5->data, lba5->size); filesAreIdentical("feyn.tif", "junk3", &same1); filesAreIdentical("test24.jpg", "junk4", &same2); if (same1 && same2) fprintf(stderr, "OK for split file\n"); else fprintf(stderr, "Error: files are different!\n"); l_byteaDestroy(&lba1); l_byteaDestroy(&lba2); l_byteaDestroy(&lba3); l_byteaDestroy(&lba4); l_byteaDestroy(&lba5); /* Test appending */ data1 = l_binaryRead("whatever10.c", &size1); sa = sarrayCreateLinesFromString((char *)data1, 1); lba1 = l_byteaCreate(0); n = sarrayGetCount(sa); for (i = 0; i < n; i++) { str = sarrayGetString(sa, i, L_NOCOPY); l_byteaAppendString(lba1, str); l_byteaAppendString(lba1, (char *)"\n"); } data2 = l_byteaGetData(lba1, &size2); l_binaryWrite("junk1.txt", "w", data2, size2); filesAreIdentical("whatever10.c", "junk1.txt", &same1); if (same1) fprintf(stderr, "OK for appended file\n"); else fprintf(stderr, "Error: appended file is different!\n"); lept_free(data1); sarrayDestroy(&sa); l_byteaDestroy(&lba1); /* Test search */ convertToPdf("test24.jpg", L_JPEG_ENCODE, 0, "junk3.pdf", 0, 0, 100, NULL, 0, NULL); lba1 = l_byteaInitFromFile("junk3.pdf"); l_byteaFindEachSequence(lba1, (l_uint8 *)" 0 obj\n", 7, &da); l_dnaWriteStream(stderr, da); l_byteaDestroy(&lba1); l_dnaDestroy(&da); /* Test write to file */ lba1 = l_byteaInitFromFile("feyn.tif"); fp = lept_fopen("junk5", "wb"); size1 = l_byteaGetSize(lba1); for (start = 0; start < size1; start += 1000) { end = L_MIN(start + 1000 - 1, size1 - 1); l_byteaWriteStream(fp, lba1, start, end); } lept_fclose(fp); l_byteaDestroy(&lba1); return 0; }