From e09c1f283aa5a896facc297b617775373519d450 Mon Sep 17 00:00:00 2001 From: danblooomberg Date: Thu, 8 Feb 2024 22:03:32 -0800 Subject: [PATCH] Remove lept_fopen() and lept_fclose() functions from use in library. * They should only be used in programs; use fopenReadStream(), fopenWriteStream() and fclose() instead. * Do the opposite in prog: replace calls to fopenReadStream() and fopenWriteStream() by lept_fopen(); replace calls to fclose() by lept_fclose(). * Also add documentation clarifying when to use the functions lept_open, lept_close, lept_calloc and lept_free. --- prog/alphaops_reg.c | 8 ++++---- prog/corrupttest.c | 8 ++++---- prog/jp2kio_reg.c | 4 ++-- prog/overlap_reg.c | 4 ++-- prog/pngio_reg.c | 12 ++++++------ src/jp2kio.c | 7 ++++--- src/readfile.c | 12 ++++++------ src/utils2.c | 10 +++++----- version-notes.html | 10 ++++++---- 9 files changed, 39 insertions(+), 36 deletions(-) diff --git a/prog/alphaops_reg.c b/prog/alphaops_reg.c index 4348d7a0b..07bcdd7d9 100644 --- a/prog/alphaops_reg.c +++ b/prog/alphaops_reg.c @@ -262,13 +262,13 @@ L_REGPARAMS *rp; lept_free(data); /* Test ascii serialization/deserialization of colormap with alpha */ - if ((fp = fopenWriteStream("/tmp/lept/alpha/cmap.4", "w")) != NULL) { + if ((fp = lept_fopen("/tmp/lept/alpha/cmap.4", "wb")) != NULL) { pixcmapWriteStream(fp, cmap); - fclose(fp); + lept_fclose(fp); } - if ((fp = fopenReadStream("/tmp/lept/alpha/cmap.4")) != NULL) { + if ((fp = lept_fopen("/tmp/lept/alpha/cmap.4", "rb")) != NULL) { cmap2 = pixcmapReadStream(fp); - fclose(fp); + lept_fclose(fp); } cmapEqual(cmap, cmap2, 4, &equal); regTestCompareValues(rp, TRUE, equal, 0.0); /* 26 */ diff --git a/prog/corrupttest.c b/prog/corrupttest.c index d0b04b33b..32e288f89 100644 --- a/prog/corrupttest.c +++ b/prog/corrupttest.c @@ -96,7 +96,7 @@ PIX *pix; } else { /* mutation */ fileCorruptByMutation(filein, loc, size, corruptfile); } - if ((fp = fopenReadStream(corruptfile)) == NULL) + if ((fp = lept_fopen(corruptfile, "rb")) == NULL) return ERROR_INT_1("stream not opened", corruptfile, __func__, 1); if (format == IFF_JFIF_JPEG) { if ((pix = pixReadJpeg(corruptfile, 0, 1, &nwarn, hint)) != NULL) { @@ -130,7 +130,7 @@ PIX *pix; } freadHeaderPnm(fp, &w, NULL, NULL, NULL, NULL, NULL); } - fclose(fp); + lept_fclose(fp); return 0; } @@ -239,7 +239,7 @@ PIX *pix; pixDestroy(&pix); /* Attempt to read the header and the resolution */ - fp = fopenReadStream(corruptfile); + fp = lept_fopen(corruptfile, "rb"); if (format == IFF_JFIF_JPEG) { freadHeaderJpeg(fp, &w, NULL, NULL, NULL, NULL); if (fgetJpegResolution(fp, &xres, &yres) == 0) @@ -262,7 +262,7 @@ PIX *pix; if (freadHeaderPnm(fp, &w, NULL, NULL, NULL, NULL, NULL) == 0) lept_stderr("w = %d\n", w); } - fclose(fp); + lept_fclose(fp); } } return 0; diff --git a/prog/jp2kio_reg.c b/prog/jp2kio_reg.c index 39c5a2527..ae323e0ab 100644 --- a/prog/jp2kio_reg.c +++ b/prog/jp2kio_reg.c @@ -194,9 +194,9 @@ PIX *pix0, *pix1; /* Test write and read using J2K codec */ lept_mkdir("lept/jp2k"); pix0 = pixRead(fname); - if ((fp = fopenWriteStream("/tmp/lept/jp2k/wyom.j2k", "wb+")) != NULL) { + if ((fp = lept_fopen("/tmp/lept/jp2k/wyom.j2k", "wb+")) != NULL) { pixWriteStreamJp2k(fp, pix0, 34, 6, L_J2K_CODEC, 0, 0); - fclose(fp); + lept_fclose(fp); } pix1 = pixRead("/tmp/lept/jp2k/wyom.j2k"); regTestCompareSimilarPix(rp, pix0, pix1, 20, 0.01, 0); /* 16 */ diff --git a/prog/overlap_reg.c b/prog/overlap_reg.c index 88594e465..bf6cd4252 100644 --- a/prog/overlap_reg.c +++ b/prog/overlap_reg.c @@ -177,7 +177,7 @@ L_REGPARAMS *rp; /* --------------------------------------------------------- */ box1 = boxCreate(0, 0, 1, 1); lept_mkdir("lept/overlap"); - fp = fopenWriteStream("/tmp/lept/overlap/result.dat", "w"); + fp = lept_fopen("/tmp/lept/overlap/result.dat", "wb"); for (i = 0; i < 3; i++) { /* 9 1x1 boxes on a 3x3 square */ for (j = 0; j < 3; j++) { box2 = boxCreate(i, j, 1, 1); @@ -188,7 +188,7 @@ L_REGPARAMS *rp; boxDestroy(&box2); } } - fclose(fp); + lept_fclose(fp); data = l_binaryRead("/tmp/lept/overlap/result.dat", &nbytes); regTestWriteDataAndCheck(rp, data, nbytes, "dat"); /* 12 */ lept_free(data); diff --git a/prog/pngio_reg.c b/prog/pngio_reg.c index 5005cd68b..ccc359666 100644 --- a/prog/pngio_reg.c +++ b/prog/pngio_reg.c @@ -338,9 +338,9 @@ PIXCMAP *cmap; pixDisplayWithTitle(pix2, 700, 0, NULL, rp->display); pixDestroy(&pix1); pixDestroy(&pix2); - fp = fopenReadStream("/tmp/lept/regout/1bpp-trans.png"); + fp = lept_fopen("/tmp/lept/regout/1bpp-trans.png", "rb"); fgetPngColormapInfo(fp, &cmap, &transp); - if (fp) fclose(fp); + if (fp) lept_fclose(fp); if (transp) lept_stderr("1bpp_trans: correct -- transparency found\n"); else @@ -373,9 +373,9 @@ PIXCMAP *cmap; pixDisplayWithTitle(pix2, 700, 100, NULL, rp->display); pixDestroy(&pix1); pixDestroy(&pix2); - fp = fopenReadStream("/tmp/lept/regout/1bpp-color.png"); + fp = lept_fopen("/tmp/lept/regout/1bpp-color.png", "rb"); fgetPngColormapInfo(fp, &cmap, &transp); - if (fp) fclose(fp); + if (fp) lept_fclose(fp); if (transp) lept_stderr("1bpp_color: error -- transparency found!\n"); else @@ -577,9 +577,9 @@ PIXCMAP *cmap; pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); - fp = fopenReadStream("/tmp/lept/regout/8bpp-trans.png"); + fp = lept_fopen("/tmp/lept/regout/8bpp-trans.png", "rb"); fgetPngColormapInfo(fp, &cmap, &transp); - if (fp) fclose(fp); + if (fp) lept_fclose(fp); if (transp) lept_stderr("8bpp_trans: correct -- transparency found\n"); else diff --git a/src/jp2kio.c b/src/jp2kio.c index fe0adcf46..afa26985d 100644 --- a/src/jp2kio.c +++ b/src/jp2kio.c @@ -59,9 +59,10 @@ * --------------------------------------------------- * In previous versions, for systems like Windows that do not have * fmemopen() and open_memstream(), we wrote data to a temp file. - * Now we use the opj_stream interface directly for operations to and - * from memory. The file stream interface for these operations - * is a wrapper around the memory interface. + * Now thanks to the contribution of Anton Tykhyy, we use the + * opj_stream interface directly for operations to and from memory. + * The file stream interface for these operations is a wrapper + * around the memory interface. * * Pdf can accept jp2k compressed strings directly * ----------------------------------------------- diff --git a/src/readfile.c b/src/readfile.c index f86521df2..91ef87035 100644 --- a/src/readfile.c +++ b/src/readfile.c @@ -1126,17 +1126,17 @@ PIXCMAP *cmap; findFileFormat(filename, &format); if (format == IFF_JP2) { - fpin = lept_fopen(filename, "rb"); + fpin = fopenReadStream(filename); fgetJp2kResolution(fpin, &xres, &yres); fclose(fpin); fprintf(fpout, " xres = %d, yres = %d\n", xres, yres); } else if (format == IFF_PNG) { - fpin = lept_fopen(filename, "rb"); + fpin = fopenReadStream(filename); fgetPngResolution(fpin, &xres, &yres); fclose(fpin); fprintf(fpout, " xres = %d, yres = %d\n", xres, yres); if (iscmap) { - fpin = lept_fopen(filename, "rb"); + fpin = fopenReadStream(filename); fgetPngColormapInfo(fpin, &cmap, &transparency); fclose(fpin); if (transparency) @@ -1147,7 +1147,7 @@ PIXCMAP *cmap; pixcmapDestroy(&cmap); } } else if (format == IFF_JFIF_JPEG) { - fpin = lept_fopen(filename, "rb"); + fpin = fopenReadStream(filename); fgetJpegResolution(fpin, &xres, &yres); fclose(fpin); fprintf(fpout, " xres = %d, yres = %d\n", xres, yres); @@ -1200,9 +1200,9 @@ PIXCMAP *cmap; if (format == IFF_TIFF || format == IFF_TIFF_G4 || format == IFF_TIFF_G3 || format == IFF_TIFF_PACKBITS) { fprintf(fpout, " Tiff header information:\n"); - fpin = lept_fopen(filename, "rb"); + fpin = fopenReadStream(filename); tiffGetCount(fpin, &npages); - lept_fclose(fpin); + fclose(fpin); if (npages == 1) fprintf(fpout, " One page in file\n"); else diff --git a/src/utils2.c b/src/utils2.c index 1d3064831..8d945f627 100644 --- a/src/utils2.c +++ b/src/utils2.c @@ -95,7 +95,7 @@ * FILE *fopenWriteWinTempfile() * * Multi-platform functions that avoid C-runtime boundary crossing - * with Windows DLLs + * with Windows DLLs (use in programs only) * FILE *lept_fopen() * l_int32 lept_fclose() * void *lept_calloc() @@ -145,10 +145,10 @@ * (5) For moving, copying and removing files and directories that are in * subdirectories of /tmp, use the lept_*() file system shell wrappers: * lept_mkdir(), lept_rmdir(), lept_mv(), lept_rm() and lept_cp(). - * (6) Use the lept_*() C library wrappers. These work properly on - * Windows, where the same DLL must perform complementary operations - * on file streams (open/close) and heap memory (malloc/free): - * lept_fopen(), lept_fclose(), lept_calloc() and lept_free(). + * (6) For programs use the lept_fopen(), lept_fclose(), lept_calloc() + * and lept_free() C library wrappers. These work properly on Windows, + * where the same DLL must perform complementary operations on + * file streams (open/close) and heap memory (malloc/free). * (7) Why read and write files to temp directories? * The library needs the ability to read and write ephemeral * files to default places, both for generating debugging output diff --git a/version-notes.html b/version-notes.html index 030ec5588..1eb6291af 100644 --- a/version-notes.html +++ b/version-notes.html @@ -93,12 +93,14 @@

* Use wrapper callSystemDebug() instead of system() in programs. * Fixed Issue #730: artifacts and bad compression with pixWriteJp2k. Results are now identical with ImageMagick convert (to jp2). - * With PR732, atykhyy made pix(Read,Write)MemJp2k work with + * With PR732, Anton Tykhyy made pix(Read,Write)MemJp2k work with direct memory access using openjpeg opj_stream functions. This removes need for temp file on Windows when writing to/from memory. - * Source files changed: gplot.c, jp2kheader.c, - jp2kheaderstub.c, jp2kio.c, partify.c, pdfio2.c, - psio2.c, utils2.c, writefile.c, allheaders.h, environ.h + * Achieve consistency with fclose() in library and lept_fclose() + in progs. Ditto for lept_free, lept_calloc, lept_fopen(), etc. + * Source files changed: adaptmap.c gplot.c, grayquant.c, jp2kheader.c, + jp2kheaderstub.c, jp2kio.c, partify.c, pdfio2.c, pixconv.c, + psio2.c, readfile.c, utils2.c, writefile.c, allheaders.h, environ.h * Prog files changed: libre_makefigs.c, cleanpdf.c croppdf.c, compresspdf.c, alltests_reg.c, htmlviewer.c, jp2kio_reg.c, pdfio2_reg.c, printimage.c, printsplitimage.c,