/*! * pixWritePng() * * Input: filename * pix * gamma * Return: 0 if OK; 1 on error * * Notes: * (1) Special version for writing png with a specified gamma. * When using pixWrite(), no field is given for gamma. */ l_int32 pixWritePng(const char *filename, PIX *pix, l_float32 gamma) { FILE *fp; PROCNAME("pixWritePng"); if (!pix) return ERROR_INT("pix not defined", procName, 1); if (!filename) return ERROR_INT("filename not defined", procName, 1); if ((fp = fopenWriteStream(filename, "wb+")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (pixWriteStreamPng(fp, pix, gamma)) { fclose(fp); return ERROR_INT("pix not written to stream", procName, 1); } fclose(fp); return 0; }
/*! * \brief pixWriteJpeg() * * \param[in] filename * \param[in] pix any depth; cmap is OK * \param[in] quality 1 - 100; 75 is default * \param[in] progressive 0 for baseline sequential; 1 for progressive * \return 0 if OK; 1 on error */ l_int32 pixWriteJpeg(const char *filename, PIX *pix, l_int32 quality, l_int32 progressive) { FILE *fp; PROCNAME("pixWriteJpeg"); if (!pix) return ERROR_INT("pix not defined", procName, 1); if (!filename) return ERROR_INT("filename not defined", procName, 1); if ((fp = fopenWriteStream(filename, "wb+")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (pixWriteStreamJpeg(fp, pix, quality, progressive)) { fclose(fp); return ERROR_INT("pix not written to stream", procName, 1); } fclose(fp); return 0; }
/*! * pixWriteJp2k() * * Input: filename * pix (any depth, cmap is OK) * quality (SNR > 0; default ~34; 0 for lossless encoding) * nlevels (resolution levels; <= 10; default = 5) * hint (a bitwise OR of L_JP2K_* values; 0 for default) * debug (output callback messages, etc) * Return: 0 if OK; 1 on error * * Notes: * (1) The @quality parameter is the SNR. The useful range is narrow: * SNR < 27 (terrible quality) * SNR = 34 (default; approximately equivalent to jpeg quality 75) * SNR = 40 (very high quality) * SNR = 45 (nearly lossless) * Use 0 for default. * (2) The @nlevels parameter is the number of resolution levels * to be written. For example, with nlevels == 5, images with * reduction factors of 1, 2, 4, 8 and 16 are encoded, and retrieval * is done at the level requested when reading. For default, * use either 5 or 0. * (3) The @hint parameter is not yet in use. * (4) For now, we only support 1 "layer" for quality. */ l_int32 pixWriteJp2k(const char *filename, PIX *pix, l_int32 quality, l_int32 nlevels, l_int32 hint, l_int32 debug) { FILE *fp; PROCNAME("pixWriteJp2k"); if (!pix) return ERROR_INT("pix not defined", procName, 1); if (!filename) return ERROR_INT("filename not defined", procName, 1); if ((fp = fopenWriteStream(filename, "wb+")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (pixWriteStreamJp2k(fp, pix, quality, nlevels, hint, debug)) { fclose(fp); return ERROR_INT("pix not written to stream", procName, 1); } fclose(fp); return 0; }
/*! * gplotWrite() * * Input: filename * gplot * Return: 0 if OK; 1 on error */ l_int32 gplotWrite(const char *filename, GPLOT *gplot) { FILE *fp; PROCNAME("gplotWrite"); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (!gplot) return ERROR_INT("gplot not defined", procName, 1); if ((fp = fopenWriteStream(filename, "wb")) == NULL) return ERROR_INT("stream not opened", procName, 1); fprintf(fp, "Gplot Version %d\n", GPLOT_VERSION_NUMBER); fprintf(fp, "Rootname: %s\n", gplot->rootname); fprintf(fp, "Output format: %d\n", gplot->outformat); fprintf(fp, "Title: %s\n", gplot->title); fprintf(fp, "X axis label: %s\n", gplot->xlabel); fprintf(fp, "Y axis label: %s\n", gplot->ylabel); fprintf(fp, "Commandfile name: %s\n", gplot->cmdname); fprintf(fp, "\nCommandfile data:"); sarrayWriteStream(fp, gplot->cmddata); fprintf(fp, "\nDatafile names:"); sarrayWriteStream(fp, gplot->datanames); fprintf(fp, "\nPlot data:"); sarrayWriteStream(fp, gplot->plotdata); fprintf(fp, "\nPlot titles:"); sarrayWriteStream(fp, gplot->plottitles); fprintf(fp, "\nPlot styles:"); numaWriteStream(fp, gplot->plotstyles); fprintf(fp, "Number of plots: %d\n", gplot->nplots); fprintf(fp, "Output file name: %s\n", gplot->outname); fprintf(fp, "Axis scaling: %d\n", gplot->scaling); fclose(fp); return 0; }
/*! * kernelWrite() * * Input: fname (output file) * kernel * Return: 0 if OK, 1 on error */ l_int32 kernelWrite(const char *fname, L_KERNEL *kel) { FILE *fp; PROCNAME("kernelWrite"); if (!fname) return ERROR_INT("fname not defined", procName, 1); if (!kel) return ERROR_INT("kel not defined", procName, 1); if ((fp = fopenWriteStream(fname, "wb")) == NULL) return ERROR_INT("stream not opened", procName, 1); kernelWriteStream(fp, kel); fclose(fp); return 0; }
/*! * recogWrite() * * Input: filename * recog * Return: 0 if OK, 1 on error */ l_int32 recogWrite(const char *filename, L_RECOG *recog) { FILE *fp; PROCNAME("recogWrite"); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (!recog) return ERROR_INT("recog not defined", procName, 1); if ((fp = fopenWriteStream(filename, "wb")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (recogWriteStream(fp, recog, filename)) return ERROR_INT("recog not written to stream", procName, 1); fclose(fp); return 0; }
/*! * boxaWrite() * * Input: filename * boxa * Return: 0 if OK, 1 on error */ l_int32 boxaWrite(const char *filename, BOXA *boxa) { FILE *fp; PROCNAME("boxaWrite"); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (!boxa) return ERROR_INT("boxa not defined", procName, 1); if ((fp = fopenWriteStream(filename, "w")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (boxaWriteStream(fp, boxa)) return ERROR_INT("boxa not written to stream", procName, 1); fclose(fp); return 0; }
/*! * l_dnaaWrite() * * Input: filename, daa * Return: 0 if OK, 1 on error */ l_int32 l_dnaaWrite(const char *filename, L_DNAA *daa) { FILE *fp; PROCNAME("l_dnaaWrite"); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (!daa) return ERROR_INT("daa not defined", procName, 1); if ((fp = fopenWriteStream(filename, "w")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (l_dnaaWriteStream(fp, daa)) return ERROR_INT("daa not written to stream", procName, 1); fclose(fp); return 0; }
/*! * dewarpWrite() * * Input: filename * dew * Return: 0 if OK, 1 on error */ l_int32 dewarpWrite(const char *filename, L_DEWARP *dew) { FILE *fp; PROCNAME("dewarpWrite"); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (!dew) return ERROR_INT("dew not defined", procName, 1); if ((fp = fopenWriteStream(filename, "wb")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (dewarpWriteStream(fp, dew)) return ERROR_INT("dew not written to stream", procName, 1); fclose(fp); return 0; }
/*! * numaaWrite() * * Input: filename, naa * Return: 0 if OK, 1 on error */ l_int32 numaaWrite(const char *filename, NUMAA *naa) { FILE *fp; PROCNAME("numaaWrite"); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (!naa) return ERROR_INT("naa not defined", procName, 1); if ((fp = fopenWriteStream(filename, "w")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (numaaWriteStream(fp, naa)) return ERROR_INT("naa not written to stream", procName, 1); fclose(fp); return 0; }
/*! * l_byteaWrite() * * Input: fname (output file) * ba * startloc (first byte to output) * endloc (last byte to output; use 0 to write to the * end of the data array) * Return: 0 if OK, 1 on error */ l_int32 l_byteaWrite(const char *fname, L_BYTEA *ba, size_t startloc, size_t endloc) { l_int32 ret; FILE *fp; PROCNAME("l_byteaWrite"); if (!fname) return ERROR_INT("fname not defined", procName, 1); if (!ba) return ERROR_INT("ba not defined", procName, 1); if ((fp = fopenWriteStream(fname, "wb")) == NULL) return ERROR_INT("stream not opened", procName, 1); ret = l_byteaWriteStream(fp, ba, startloc, endloc); fclose(fp); return ret; }
/*! * \brief pmsGetAlloc() * * \param[in] nbytes * \return data * * <pre> * Notes: * (1) This is called when a request for pix data cannot be * obtained from the preallocated memory store. After use it * is freed like normal memory. * (2) If logging is on, only write out allocs that are as large as * the minimum size handled by the memory store. * (3) size_t is %lu on 64 bit platforms and %u on 32 bit platforms. * The C99 platform-independent format specifier for size_t is %zu, * but windows hasn't conformed, so we are forced to go back to * C89, use %lu, and cast to get platform-independence. Ugh. * </pre> */ void * pmsGetAlloc(size_t nbytes) { void *data; FILE *fp; L_PIX_MEM_STORE *pms; PROCNAME("pmsGetAlloc"); if ((pms = CustomPMS) == NULL) return (void *)ERROR_PTR("pms not defined", procName, NULL); if ((data = (void *)LEPT_CALLOC(nbytes, sizeof(char))) == NULL) return (void *)ERROR_PTR("data not made", procName, NULL); if (pms->logfile && nbytes >= pms->smallest) { fp = fopenWriteStream(pms->logfile, "a"); fprintf(fp, "Alloc %lu bytes at %p\n", (unsigned long)nbytes, data); fclose(fp); } return data; }
/*! * gplotGenDataFiles() * * Input: gplot * Return: 0 if OK, 1 on error */ l_int32 gplotGenDataFiles(GPLOT *gplot) { char *plotdata, *dataname; l_int32 i, nplots; FILE *fp; PROCNAME("gplotGenDataFiles"); if (!gplot) return ERROR_INT("gplot not defined", procName, 1); nplots = sarrayGetCount(gplot->datanames); for (i = 0; i < nplots; i++) { plotdata = sarrayGetString(gplot->plotdata, i, L_NOCOPY); dataname = sarrayGetString(gplot->datanames, i, L_NOCOPY); if ((fp = fopenWriteStream(dataname, "w")) == NULL) return ERROR_INT("datafile stream not opened", procName, 1); fwrite(plotdata, 1, strlen(plotdata), fp); fclose(fp); } return 0; }
/*! * pixWriteWebP() * * Input: filename * pixs * quality (0 - 100; default ~80) * lossless (use 1 for lossless; 0 for lossy) * Return: 0 if OK, 1 on error * * Notes: * (1) Special top-level function allowing specification of quality. */ l_int32 pixWriteWebP(const char *filename, PIX *pixs, l_int32 quality, l_int32 lossless) { FILE *fp; PROCNAME("pixWriteWebP"); if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (!filename) return ERROR_INT("filename not defined", procName, 1); if ((fp = fopenWriteStream(filename, "wb+")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (pixWriteStreamWebP(fp, pixs, quality, lossless) != 0) { fclose(fp); return ERROR_INT("pixs not compressed to stream", procName, 1); } fclose(fp); return 0; }
int main(int argc, char **argv) { l_uint8 *data; l_int32 w, h, n1, n2, n, i, minval, maxval; l_int32 ncolors, rval, gval, bval, equal; l_int32 *rmap, *gmap, *bmap; l_uint32 color; l_float32 gamma; BOX *box; FILE *fp; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6; PIX *pixs, *pixb, *pixg, *pixc, *pixd; PIX *pixg2, *pixcs1, *pixcs2, *pixd1, *pixd2; PIXA *pixa, *pixa2, *pixa3; PIXCMAP *cmap, *cmap2; RGBA_QUAD *cta; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* ------------------------ (1) ----------------------------*/ /* Blend with a white background */ pix1 = pixRead("books_logo.png"); pixDisplayWithTitle(pix1, 100, 0, NULL, rp->display); pix2 = pixAlphaBlendUniform(pix1, 0xffffff00); pixDisplayWithTitle(pix2, 100, 150, NULL, rp->display); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 0 */ regTestWritePixAndCheck(rp, pix2, IFF_PNG); /* 1 */ /* Generate an alpha layer based on the white background */ pix3 = pixSetAlphaOverWhite(pix2); pixSetSpp(pix3, 3); pixWrite("/tmp/alphaops.2.png", pix3, IFF_PNG); /* without alpha */ regTestCheckFile(rp, "/tmp/alphaops.2.png"); /* 2 */ pixSetSpp(pix3, 4); regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 3, with alpha */ pixDisplayWithTitle(pix3, 100, 300, NULL, rp->display); /* Render on a light yellow background */ pix4 = pixAlphaBlendUniform(pix3, 0xffffe000); regTestWritePixAndCheck(rp, pix4, IFF_PNG); /* 4 */ pixDisplayWithTitle(pix4, 100, 450, NULL, rp->display); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); /* ------------------------ (2) ----------------------------*/ lept_rmdir("alpha"); lept_mkdir("alpha"); /* Make the transparency (alpha) layer. * pixs is the mask. We turn it into a transparency (alpha) * layer by converting to 8 bpp. A small convolution fuzzes * the mask edges so that you don't see the pixels. */ pixs = pixRead("feyn-fract.tif"); pixGetDimensions(pixs, &w, &h, NULL); pixg = pixConvert1To8(NULL, pixs, 0, 255); pixg2 = pixBlockconvGray(pixg, NULL, 1, 1); regTestWritePixAndCheck(rp, pixg2, IFF_JFIF_JPEG); /* 5 */ pixDisplayWithTitle(pixg2, 0, 0, "alpha", rp->display); /* Make the viewable image. * pixc is the image that we see where the alpha layer is * opaque -- i.e., greater than 0. Scale it to the same * size as the mask. To visualize what this will look like * when displayed over a black background, create the black * background image, pixb, and do the blending with pixcs1 * explicitly using the alpha layer pixg2. */ pixc = pixRead("tetons.jpg"); pixcs1 = pixScaleToSize(pixc, w, h); regTestWritePixAndCheck(rp, pixcs1, IFF_JFIF_JPEG); /* 6 */ pixDisplayWithTitle(pixcs1, 300, 0, "viewable", rp->display); pixb = pixCreateTemplate(pixcs1); /* black */ pixd1 = pixBlendWithGrayMask(pixb, pixcs1, pixg2, 0, 0); regTestWritePixAndCheck(rp, pixd1, IFF_JFIF_JPEG); /* 7 */ pixDisplayWithTitle(pixd1, 600, 0, "alpha-blended 1", rp->display); /* Embed the alpha layer pixg2 into the color image pixc. * Write it out as is. Then clean pixcs1 (to 0) under the fully * transparent part of the alpha layer, and write that result * out as well. */ pixSetRGBComponent(pixcs1, pixg2, L_ALPHA_CHANNEL); pixWrite("/tmp/alpha/pixcs1.png", pixcs1, IFF_PNG); pixcs2 = pixSetUnderTransparency(pixcs1, 0, 0); pixWrite("/tmp/alpha/pixcs2.png", pixcs2, IFF_PNG); /* What will this look like over a black background? * Do the blending explicitly and display. It should * look identical to the blended result pixd1 before cleaning. */ pixd2 = pixBlendWithGrayMask(pixb, pixcs2, pixg2, 0, 0); regTestWritePixAndCheck(rp, pixd2, IFF_JFIF_JPEG); /* 8 */ pixDisplayWithTitle(pixd2, 0, 400, "alpha blended 2", rp->display); /* Read the two images back, ignoring the transparency layer. * The uncleaned image will come back identical to pixcs1. * However, the cleaned image will be black wherever * the alpha layer was fully transparent. It will * look the same when viewed through the alpha layer, * but have much better compression. */ pix1 = pixRead("/tmp/alpha/pixcs1.png"); /* just pixcs1 */ pix2 = pixRead("/tmp/alpha/pixcs2.png"); /* cleaned under transparent */ n1 = nbytesInFile("/tmp/alpha/pixcs1.png"); n2 = nbytesInFile("/tmp/alpha/pixcs2.png"); fprintf(stderr, " Original: %d bytes\n Cleaned: %d bytes\n", n1, n2); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 9 */ regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG); /* 10 */ pixDisplayWithTitle(pix1, 300, 400, "without alpha", rp->display); pixDisplayWithTitle(pix2, 600, 400, "cleaned under transparent", rp->display); pixa = pixaCreate(0); pixSaveTiled(pixg2, pixa, 1.0, 1, 20, 32); pixSaveTiled(pixcs1, pixa, 1.0, 1, 20, 0); pixSaveTiled(pix1, pixa, 1.0, 0, 20, 0); pixSaveTiled(pixd1, pixa, 1.0, 1, 20, 0); pixSaveTiled(pixd2, pixa, 1.0, 0, 20, 0); pixSaveTiled(pix2, pixa, 1.0, 1, 20, 0); pixd = pixaDisplay(pixa, 0, 0); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 11 */ pixDisplayWithTitle(pixd, 200, 200, "composite", rp->display); pixWrite("/tmp/alpha/alpha.png", pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixaDestroy(&pixa); pixDestroy(&pixs); pixDestroy(&pixb); pixDestroy(&pixg); pixDestroy(&pixg2); pixDestroy(&pixc); pixDestroy(&pixcs1); pixDestroy(&pixcs2); pixDestroy(&pixd); pixDestroy(&pixd1); pixDestroy(&pixd2); pixDestroy(&pix1); pixDestroy(&pix2); /* ------------------------ (3) ----------------------------*/ color = 0xffffa000; gamma = 1.0; minval = 0; maxval = 200; box = boxCreate(0, 85, 600, 100); pixa = pixaCreate(6); pix1 = pixRead("blend-green1.jpg"); pixaAddPix(pixa, pix1, L_INSERT); pix1 = pixRead("blend-green2.png"); pixaAddPix(pixa, pix1, L_INSERT); pix1 = pixRead("blend-green3.png"); pixaAddPix(pixa, pix1, L_INSERT); pix1 = pixRead("blend-orange.jpg"); pixaAddPix(pixa, pix1, L_INSERT); pix1 = pixRead("blend-yellow.jpg"); pixaAddPix(pixa, pix1, L_INSERT); pix1 = pixRead("blend-red.png"); pixaAddPix(pixa, pix1, L_INSERT); n = pixaGetCount(pixa); pixa2 = pixaCreate(n); pixa3 = pixaCreate(n); for (i = 0; i < n; i++) { pix1 = pixaGetPix(pixa, i, L_CLONE); pix2 = DoBlendTest(pix1, box, color, gamma, minval, maxval, 1); regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG); /* 12, 14, ... 22 */ pixDisplayWithTitle(pix2, 150 * i, 0, NULL, rp->display); pixaAddPix(pixa2, pix2, L_INSERT); pix2 = DoBlendTest(pix1, box, color, gamma, minval, maxval, 2); regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG); /* 13, 15, ... 23 */ pixDisplayWithTitle(pix2, 150 * i, 200, NULL, rp->display); pixaAddPix(pixa3, pix2, L_INSERT); pixDestroy(&pix1); } if (rp->display) { pixaConvertToPdf(pixa2, 0, 0.75, L_FLATE_ENCODE, 0, "blend 1 test", "/tmp/alpha/blending1.pdf"); pixaConvertToPdf(pixa3, 0, 0.75, L_FLATE_ENCODE, 0, "blend 2 test", "/tmp/alpha/blending2.pdf"); } pixaDestroy(&pixa); pixaDestroy(&pixa2); pixaDestroy(&pixa3); boxDestroy(&box); /* ------------------------ (4) ----------------------------*/ /* Use one image as the alpha component for a second image */ pix1 = pixRead("test24.jpg"); pix2 = pixRead("marge.jpg"); pix3 = pixScale(pix2, 1.9, 2.2); pix4 = pixConvertTo8(pix3, 0); pixSetRGBComponent(pix1, pix4, L_ALPHA_CHANNEL); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 24 */ pixDisplayWithTitle(pix1, 600, 0, NULL, rp->display); /* Set the alpha value in a colormap to bval */ pix5 = pixOctreeColorQuant(pix1, 128, 0); cmap = pixGetColormap(pix5); pixcmapToArrays(cmap, &rmap, &gmap, &bmap, NULL); n = pixcmapGetCount(cmap); for (i = 0; i < n; i++) { pixcmapGetColor(cmap, i, &rval, &gval, &bval); cta = (RGBA_QUAD *)cmap->array; cta[i].alpha = bval; } /* Test binary serialization/deserialization of colormap with alpha */ pixcmapSerializeToMemory(cmap, 4, &ncolors, &data); cmap2 = pixcmapDeserializeFromMemory(data, 4, ncolors); CmapEqual(cmap, cmap2, &equal); regTestCompareValues(rp, TRUE, equal, 0.0); /* 25 */ pixcmapDestroy(&cmap2); lept_free(data); /* Test ascii serialization/deserialization of colormap with alpha */ fp = fopenWriteStream("/tmp/alpha/cmap.4", "w"); pixcmapWriteStream(fp, cmap); fclose(fp); fp = fopenReadStream("/tmp/alpha/cmap.4"); cmap2 = pixcmapReadStream(fp); fclose(fp); CmapEqual(cmap, cmap2, &equal); regTestCompareValues(rp, TRUE, equal, 0.0); /* 26 */ pixcmapDestroy(&cmap2); /* Test r/w for cmapped pix with non-opaque alpha */ pixDisplayWithTitle(pix5, 900, 0, NULL, rp->display); regTestWritePixAndCheck(rp, pix5, IFF_PNG); /* 27 */ pixWrite("/tmp/alpha/fourcomp.png", pix5, IFF_PNG); pix6 = pixRead("/tmp/alpha/fourcomp.png"); regTestComparePix(rp, pix5, pix6); /* 28 */ pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); lept_free(rmap); lept_free(gmap); lept_free(bmap); return regTestCleanup(rp); }
/*! * 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: * There is either only one arg, or 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 2, * the display field in rp is set to FALSE, preventing * image display. * Case 2: * 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 in rp is set to FALSE. * Case 3: * 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; /* Make sure the regout subdirectory exists */ lept_mkdir("regout"); /* Only open a stream to a temp file for the 'compare' case */ if (argc == 1 || !strcmp(argv[1], "compare")) { rp->mode = L_REG_COMPARE; rp->tempfile = genPathname("/tmp/regout", "regtest_output.txt"); 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 if (!strcmp(argv[1], "display")) { rp->mode = L_REG_DISPLAY; rp->display = TRUE; } 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; }
/*! * gplotGenCommandFile() * * Input: gplot * Return: 0 if OK, 1 on error */ l_int32 gplotGenCommandFile(GPLOT *gplot) { char buf[L_BUF_SIZE]; char *cmdstr, *plottitle, *dataname; l_int32 i, plotstyle, nplots; FILE *fp; PROCNAME("gplotGenCommandFile"); if (!gplot) return ERROR_INT("gplot not defined", procName, 1); /* Remove any previous command data */ sarrayClear(gplot->cmddata); /* Generate command data instructions */ if (gplot->title) { /* set title */ snprintf(buf, L_BUF_SIZE, "set title '%s'", gplot->title); sarrayAddString(gplot->cmddata, buf, L_COPY); } if (gplot->xlabel) { /* set xlabel */ snprintf(buf, L_BUF_SIZE, "set xlabel '%s'", gplot->xlabel); sarrayAddString(gplot->cmddata, buf, L_COPY); } if (gplot->ylabel) { /* set ylabel */ snprintf(buf, L_BUF_SIZE, "set ylabel '%s'", gplot->ylabel); sarrayAddString(gplot->cmddata, buf, L_COPY); } if (gplot->outformat == GPLOT_PNG) /* set terminal type and output */ snprintf(buf, L_BUF_SIZE, "set terminal png; set output '%s'", gplot->outname); else if (gplot->outformat == GPLOT_PS) snprintf(buf, L_BUF_SIZE, "set terminal postscript; set output '%s'", gplot->outname); else if (gplot->outformat == GPLOT_EPS) snprintf(buf, L_BUF_SIZE, "set terminal postscript eps; set output '%s'", gplot->outname); else if (gplot->outformat == GPLOT_LATEX) snprintf(buf, L_BUF_SIZE, "set terminal latex; set output '%s'", gplot->outname); else /* gplot->outformat == GPLOT_X11 */ #ifndef _WIN32 snprintf(buf, L_BUF_SIZE, "set terminal x11"); #else snprintf(buf, L_BUF_SIZE, "set terminal windows"); #endif /* _WIN32 */ sarrayAddString(gplot->cmddata, buf, L_COPY); if (gplot->scaling == GPLOT_LOG_SCALE_X || gplot->scaling == GPLOT_LOG_SCALE_X_Y) { snprintf(buf, L_BUF_SIZE, "set logscale x"); sarrayAddString(gplot->cmddata, buf, L_COPY); } if (gplot->scaling == GPLOT_LOG_SCALE_Y || gplot->scaling == GPLOT_LOG_SCALE_X_Y) { snprintf(buf, L_BUF_SIZE, "set logscale y"); sarrayAddString(gplot->cmddata, buf, L_COPY); } nplots = sarrayGetCount(gplot->datanames); for (i = 0; i < nplots; i++) { plottitle = sarrayGetString(gplot->plottitles, i, L_NOCOPY); dataname = sarrayGetString(gplot->datanames, i, L_NOCOPY); numaGetIValue(gplot->plotstyles, i, &plotstyle); if (nplots == 1) { snprintf(buf, L_BUF_SIZE, "plot '%s' title '%s' %s", dataname, plottitle, gplotstylenames[plotstyle]); } else { if (i == 0) snprintf(buf, L_BUF_SIZE, "plot '%s' title '%s' %s, \\", dataname, plottitle, gplotstylenames[plotstyle]); else if (i < nplots - 1) snprintf(buf, L_BUF_SIZE, " '%s' title '%s' %s, \\", dataname, plottitle, gplotstylenames[plotstyle]); else snprintf(buf, L_BUF_SIZE, " '%s' title '%s' %s", dataname, plottitle, gplotstylenames[plotstyle]); } sarrayAddString(gplot->cmddata, buf, L_COPY); } /* Write command data to file */ cmdstr = sarrayToString(gplot->cmddata, 1); if ((fp = fopenWriteStream(gplot->cmdname, "w")) == NULL) return ERROR_INT("cmd stream not opened", procName, 1); fwrite(cmdstr, 1, strlen(cmdstr), fp); fclose(fp); FREE(cmdstr); return 0; }
/*! * pixWrite() * * Input: filename * pix * format (defined in imageio.h) * Return: 0 if OK; 1 on error * * Notes: * (1) Open for write using binary mode (with the "b" flag) * to avoid having Windows automatically translate the NL * into CRLF, which corrupts image files. On non-windows * systems this flag should be ignored, per ISO C90. * Thanks to Dave Bryan for pointing this out. * (2) If the default image format is requested, we use the input format; * if the input format is unknown, a lossless format is assigned. * (3) There are two modes with respect to file naming. * (a) The default code writes to @filename. * (b) If WRITE_AS_NAMED is defined to 0, it's a bit fancier. * Then, if @filename does not have a file extension, one is * automatically appended, depending on the requested format. * The original intent for providing option (b) was to insure * that filenames on Windows have an extension that matches * the image compression. However, this is not the default. */ l_int32 pixWrite(const char *filename, PIX *pix, l_int32 format) { char *fname; FILE *fp; PROCNAME("pixWrite"); if (!pix) return ERROR_INT("pix not defined", procName, 1); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (format == IFF_JP2) return ERROR_INT("jp2 not supported", procName, 1); fname = genPathname(filename, NULL); #if WRITE_AS_NAMED /* Default */ if ((fp = fopenWriteStream(fname, "wb+")) == NULL) { FREE(fname); return ERROR_INT("stream not opened", procName, 1); } #else /* Add an extension to the output name if none exists */ {l_int32 extlen; char *extension, *filebuf; splitPathAtExtension(fname, NULL, &extension); extlen = strlen(extension); FREE(extension); if (extlen == 0) { if (format == IFF_DEFAULT || format == IFF_UNKNOWN) format = pixChooseOutputFormat(pix); filebuf = (char *)CALLOC(strlen(fname) + 10, sizeof(char)); if (!filebuf) { return ERROR_INT("filebuf not made", procName, 1); FREE(fname); } strncpy(filebuf, fname, strlen(fname)); strcat(filebuf, "."); strcat(filebuf, ImageFileFormatExtensions[format]); } else { filebuf = (char *)fname; } fp = fopenWriteStream(filebuf, "wb+"); if (filebuf != fname) FREE(filebuf); if (fp == NULL) { FREE(fname); return ERROR_INT("stream not opened", procName, 1); } } #endif /* WRITE_AS_NAMED */ FREE(fname); if (pixWriteStream(fp, pix, format)) { fclose(fp); return ERROR_INT("pix not written to stream", procName, 1); } /* Close the stream except if GIF under windows, because * EGifCloseFile() closes the windows file stream! */ if (format != IFF_GIF) fclose(fp); #ifndef _WIN32 else /* gif file */ fclose(fp); #endif /* ! _WIN32 */ return 0; }
/*! * \brief pixWrite() * * \param[in] filename * \param[in] pix * \param[in] format defined in imageio.h * \return 0 if OK; 1 on error * * <pre> * Notes: * (1) Open for write using binary mode (with the "b" flag) * to avoid having Windows automatically translate the NL * into CRLF, which corrupts image files. On non-windows * systems this flag should be ignored, per ISO C90. * Thanks to Dave Bryan for pointing this out. * (2) If the default image format IFF_DEFAULT is requested: * use the input format if known; otherwise, use a lossless format. * (3) There are two modes with respect to file naming. * (a) The default code writes to %filename. * (b) If WRITE_AS_NAMED is defined to 0, it's a bit fancier. * Then, if %filename does not have a file extension, one is * automatically appended, depending on the requested format. * The original intent for providing option (b) was to insure * that filenames on Windows have an extension that matches * the image compression. However, this is not the default. * </pre> */ l_int32 pixWrite(const char *filename, PIX *pix, l_int32 format) { char *fname; FILE *fp; PROCNAME("pixWrite"); if (!pix) return ERROR_INT("pix not defined", procName, 1); if (!filename) return ERROR_INT("filename not defined", procName, 1); fname = genPathname(filename, NULL); #if WRITE_AS_NAMED /* Default */ if ((fp = fopenWriteStream(fname, "wb+")) == NULL) { LEPT_FREE(fname); return ERROR_INT("stream not opened", procName, 1); } #else /* Add an extension to the output name if none exists */ {l_int32 extlen; char *extension, *filebuf; splitPathAtExtension(fname, NULL, &extension); extlen = strlen(extension); LEPT_FREE(extension); if (extlen == 0) { if (format == IFF_DEFAULT || format == IFF_UNKNOWN) format = pixChooseOutputFormat(pix); filebuf = (char *)LEPT_CALLOC(strlen(fname) + 10, sizeof(char)); if (!filebuf) { return ERROR_INT("filebuf not made", procName, 1); LEPT_FREE(fname); } strncpy(filebuf, fname, strlen(fname)); strcat(filebuf, "."); strcat(filebuf, ImageFileFormatExtensions[format]); } else { filebuf = (char *)fname; } fp = fopenWriteStream(filebuf, "wb+"); if (filebuf != fname) LEPT_FREE(filebuf); if (fp == NULL) { LEPT_FREE(fname); return ERROR_INT("stream not opened", procName, 1); } } #endif /* WRITE_AS_NAMED */ LEPT_FREE(fname); if (pixWriteStream(fp, pix, format)) { fclose(fp); return ERROR_INT("pix not written to stream", procName, 1); } fclose(fp); return 0; }