Skip to content

Commit

Permalink
Remove lept_fopen() and lept_fclose() functions from use in library.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
DanBloomberg committed Feb 9, 2024
1 parent ee7cb15 commit e09c1f2
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 36 deletions.
8 changes: 4 additions & 4 deletions prog/alphaops_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
8 changes: 4 additions & 4 deletions prog/corrupttest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -130,7 +130,7 @@ PIX *pix;
}
freadHeaderPnm(fp, &w, NULL, NULL, NULL, NULL, NULL);
}
fclose(fp);
lept_fclose(fp);
return 0;
}

Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions prog/jp2kio_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions prog/overlap_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions prog/pngio_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/jp2kio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
* -----------------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions src/readfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/utils2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions version-notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ <h2 align=center> <IMG SRC="moller52.jpg" border=1 ALIGN_MIDDLE> </h2>
* 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,
Expand Down

0 comments on commit e09c1f2

Please sign in to comment.