diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libpam.c | 23 | ||||
-rw-r--r-- | lib/libpammap.c | 2 | ||||
-rw-r--r-- | lib/libpamn.c | 6 | ||||
-rw-r--r-- | lib/libpamwrite.c | 14 | ||||
-rw-r--r-- | lib/libpbm1.c | 27 | ||||
-rw-r--r-- | lib/libpgm1.c | 20 | ||||
-rw-r--r-- | lib/libppm1.c | 22 | ||||
-rw-r--r-- | lib/libppmcolor.c | 108 | ||||
-rw-r--r-- | lib/libsystem.c | 16 | ||||
-rw-r--r-- | lib/pam.h | 13 | ||||
-rw-r--r-- | lib/pmfileio.c | 4 | ||||
-rw-r--r-- | lib/util/mallocvar.h | 2 | ||||
-rw-r--r-- | lib/util/pm_c_util.h | 62 |
13 files changed, 200 insertions, 119 deletions
diff --git a/lib/libpam.c b/lib/libpam.c index 549f4c55..f188f7d6 100644 --- a/lib/libpam.c +++ b/lib/libpam.c @@ -1046,9 +1046,7 @@ pnm_writepaminit(struct pam * const pamP) { switch (PAM_FORMAT_TYPE(pamP->format)) { case PAM_TYPE: - if (pm_plain_output) - pm_error("There is no plain version of PAM. -plain option " - "is not allowed"); + /* See explanation below of why we ignore 'pm_plain_output' here. */ fprintf(pamP->file, "P7\n"); writeComments(pamP); fprintf(pamP->file, "WIDTH %u\n", (unsigned)pamP->width); @@ -1108,6 +1106,23 @@ pnm_writepaminit(struct pam * const pamP) { +/* EFFECT OF -plain WHEN WRITING PAM FORMAT: + + Before Netpbm 10.63 (June 2013), pnm_writepaminit() did a pm_error() here + if 'pm_plain_output' was set (i.e. the user said -plain). But this isn't + really logical, because -plain is a global option for the program and here + we are just writing one image. As a global option, -plain must be defined + to have effect where it makes sense and have no effect where it doesn't. + Note that a program that generates GIF just ignores -plain. Note also that + a program could conceivably generate both a PPM image and a PAM image. + + Note also how we handle the other a user can request plain format: the + 'plainformat' member of the PAM struct. In the case of PAM, we ignore that + member. +*/ + + + void pnm_checkpam(const struct pam * const pamP, enum pm_check_type const checkType, @@ -1291,7 +1306,7 @@ pnm_addopacityrow(const struct pam * const pamP, void pnm_getopacity(const struct pam * const pamP, - bool * const haveOpacityP, + int * const haveOpacityP, unsigned int * const opacityPlaneP) { /* Usage note: this is obsolete since we added 'have_opacity', etc. diff --git a/lib/libpammap.c b/lib/libpammap.c index 66aa6fc0..ef373e81 100644 --- a/lib/libpammap.c +++ b/lib/libpammap.c @@ -574,7 +574,7 @@ pnm_computetupletablehash(struct pam * const pamP, -----------------------------------------------------------------------------*/ tuplehash tupletablehash; unsigned int i; - bool fits; + int fits; tupletablehash = pnm_createtuplehash(); diff --git a/lib/libpamn.c b/lib/libpamn.c index bd3fab21..b30bde53 100644 --- a/lib/libpamn.c +++ b/lib/libpamn.c @@ -498,7 +498,7 @@ gammaCommon(struct pam * const pamP, unsigned int plane; unsigned int opacityPlane; - bool haveOpacity; + int haveOpacity; pnm_getopacity(pamP, &haveOpacity, &opacityPlane); @@ -548,7 +548,7 @@ applyopacityCommon(enum applyUnapply const applyUnapply, if the foreground values had already been so multiplied. -----------------------------------------------------------------------------*/ unsigned int opacityPlane; - bool haveOpacity; + int haveOpacity; pnm_getopacity(pamP, &haveOpacity, &opacityPlane); @@ -645,7 +645,7 @@ createUngammaMapOffset(const struct pam * const pamP, MALLOCARRAY(ungammaTransformMap, pamP->maxval+1); if (ungammaTransformMap != NULL) { - bool haveOpacity; + int haveOpacity; unsigned int opacityPlane; unsigned int plane; diff --git a/lib/libpamwrite.c b/lib/libpamwrite.c index 73e5e78a..9f86e359 100644 --- a/lib/libpamwrite.c +++ b/lib/libpamwrite.c @@ -22,6 +22,7 @@ #include <math.h> #include "pm_config.h" +#include "pm_c_util.h" #include "pam.h" @@ -354,7 +355,9 @@ pnm_writepamrow(const struct pam * const pamP, pnm_writepaminit(). */ - if (pm_plain_output || pamP->plainformat) { + if (pamP->format == PAM_FORMAT || !(pm_plain_output || pamP->plainformat)) + writePamRawRow(pamP, tuplerow, 1); + else { switch (PAM_FORMAT_TYPE(pamP->format)) { case PBM_TYPE: writePamPlainPbmRow(pamP, tuplerow); @@ -364,18 +367,13 @@ pnm_writepamrow(const struct pam * const pamP, writePamPlainRow(pamP, tuplerow); break; case PAM_TYPE: - /* pm_plain_output is impossible here due to assumption stated - above about pnm_writepaminit() having checked it. The - pamP->plainformat is meaningless for PAM. - */ - writePamRawRow(pamP, tuplerow, 1); + assert(false); break; default: pm_error("Invalid 'format' value %u in pam structure", pamP->format); } - } else - writePamRawRow(pamP, tuplerow, 1); + } } diff --git a/lib/libpbm1.c b/lib/libpbm1.c index 25498bc7..c96779ed 100644 --- a/lib/libpbm1.c +++ b/lib/libpbm1.c @@ -57,27 +57,28 @@ pbm_nextimage(FILE *file, int * const eofP) { void -pbm_check(FILE * file, const enum pm_check_type check_type, - const int format, const int cols, const int rows, - enum pm_check_code * const retval_p) { +pbm_check(FILE * const fileP, + enum pm_check_type const checkType, + int const format, + int const cols, + int const rows, + enum pm_check_code * const retvalP) { if (rows < 0) pm_error("Invalid number of rows passed to pbm_check(): %d", rows); if (cols < 0) pm_error("Invalid number of columns passed to pbm_check(): %d", cols); - if (check_type != PM_CHECK_BASIC) { - if (retval_p) *retval_p = PM_CHECK_UNKNOWN_TYPE; + if (checkType != PM_CHECK_BASIC) { + if (retvalP) + *retvalP = PM_CHECK_UNKNOWN_TYPE; } else if (format != RPBM_FORMAT) { - if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE; + if (retvalP) + *retvalP = PM_CHECK_UNCHECKABLE; } else { - pm_filepos const bytes_per_row = (cols+7)/8; - pm_filepos const need_raster_size = rows * bytes_per_row; -#ifdef LARGEFILEDEBUG - pm_message("pm_filepos passed to pm_check() is %u bytes", - sizeof(pm_filepos)); -#endif - pm_check(file, check_type, need_raster_size, retval_p); + pm_filepos const bytesPerRow = (cols+7)/8; + pm_filepos const needRasterSize = rows * bytesPerRow; + pm_check(fileP, checkType, needRasterSize, retvalP); } } diff --git a/lib/libpgm1.c b/lib/libpgm1.c index 5a533f49..987ee04e 100644 --- a/lib/libpgm1.c +++ b/lib/libpgm1.c @@ -338,28 +338,30 @@ pgm_readpgm(FILE * const fileP, void pgm_check(FILE * const file, - enum pm_check_type const check_type, + enum pm_check_type const checkType, int const format, int const cols, int const rows, gray const maxval, - enum pm_check_code * const retval_p) { + enum pm_check_code * const retvalP) { if (rows < 0) pm_error("Invalid number of rows passed to pgm_check(): %d", rows); if (cols < 0) pm_error("Invalid number of columns passed to pgm_check(): %d", cols); - if (check_type != PM_CHECK_BASIC) { - if (retval_p) *retval_p = PM_CHECK_UNKNOWN_TYPE; + if (checkType != PM_CHECK_BASIC) { + if (retvalP) + *retvalP = PM_CHECK_UNKNOWN_TYPE; } else if (PGM_FORMAT_TYPE(format) == PBM_TYPE) { - pbm_check(file, check_type, format, cols, rows, retval_p); + pbm_check(file, checkType, format, cols, rows, retvalP); } else if (format != RPGM_FORMAT) { - if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE; + if (retvalP) + *retvalP = PM_CHECK_UNCHECKABLE; } else { - pm_filepos const bytes_per_row = cols * (maxval > 255 ? 2 : 1); - pm_filepos const need_raster_size = rows * bytes_per_row; + pm_filepos const bytesPerRow = cols * (maxval > 255 ? 2 : 1); + pm_filepos const needRasterSize = rows * bytesPerRow; - pm_check(file, check_type, need_raster_size, retval_p); + pm_check(file, checkType, needRasterSize, retvalP); } } diff --git a/lib/libppm1.c b/lib/libppm1.c index 1b417613..195aec70 100644 --- a/lib/libppm1.c +++ b/lib/libppm1.c @@ -422,30 +422,32 @@ ppm_readppm(FILE * const fileP, void ppm_check(FILE * const fileP, - enum pm_check_type const check_type, + enum pm_check_type const checkType, int const format, int const cols, int const rows, pixval const maxval, - enum pm_check_code * const retval_p) { + enum pm_check_code * const retvalP) { if (rows < 0) pm_error("Invalid number of rows passed to ppm_check(): %d", rows); if (cols < 0) pm_error("Invalid number of columns passed to ppm_check(): %d", cols); - if (check_type != PM_CHECK_BASIC) { - if (retval_p) *retval_p = PM_CHECK_UNKNOWN_TYPE; + if (checkType != PM_CHECK_BASIC) { + if (retvalP) + *retvalP = PM_CHECK_UNKNOWN_TYPE; } else if (PPM_FORMAT_TYPE(format) == PBM_TYPE) { - pbm_check(fileP, check_type, format, cols, rows, retval_p); + pbm_check(fileP, checkType, format, cols, rows, retvalP); } else if (PPM_FORMAT_TYPE(format) == PGM_TYPE) { - pgm_check(fileP, check_type, format, cols, rows, maxval, retval_p); + pgm_check(fileP, checkType, format, cols, rows, maxval, retvalP); } else if (format != RPPM_FORMAT) { - if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE; + if (retvalP) + *retvalP = PM_CHECK_UNCHECKABLE; } else { - pm_filepos const bytes_per_row = cols * 3 * (maxval > 255 ? 2 : 1); - pm_filepos const need_raster_size = rows * bytes_per_row; + pm_filepos const bytesPerRow = cols * 3 * (maxval > 255 ? 2 : 1); + pm_filepos const needRasterSize = rows * bytesPerRow; - pm_check(fileP, check_type, need_raster_size, retval_p); + pm_check(fileP, checkType, needRasterSize, retvalP); } } diff --git a/lib/libppmcolor.c b/lib/libppmcolor.c index 70e8499d..347bab29 100644 --- a/lib/libppmcolor.c +++ b/lib/libppmcolor.c @@ -451,6 +451,43 @@ ppm_colorname(const pixel * const colorP, #define MAXCOLORNAMES 1000u +static const char ** +allocColorNames() { + + const char ** colornames; + + MALLOCARRAY(colornames, MAXCOLORNAMES); + + if (colornames) { + unsigned int i; + for (i = 0; i < MAXCOLORNAMES; ++i) + colornames[i] = NULL; + } + return colornames; +} + + + +static colorhash_table +allocColorHash(void) { + + colorhash_table cht; + jmp_buf jmpbuf; + jmp_buf * origJmpbufP; + + if (setjmp(jmpbuf) != 0) + cht = NULL; + else { + pm_setjmpbufsave(&jmpbuf, &origJmpbufP); + cht = ppm_alloccolorhash(); + } + pm_setjmpbuf(origJmpbufP); + + return cht; +} + + + static void processColorfileEntry(struct colorfile_entry const ce, colorhash_table const cht, @@ -525,6 +562,9 @@ readOpenColorFile(FILE * const colorFileP, Read the color dictionary file *colorFileP and add the colors in it to colornames[], colors[], and 'cht'. + colornames[] and colors[] must be allocated with MAXCOLORNAMES entries + at entry. + We may add colors to 'cht' even if we fail. -----------------------------------------------------------------------------*/ unsigned int nColorsDone; @@ -543,12 +583,7 @@ readOpenColorFile(FILE * const colorFileP, processColorfileEntry(ce, cht, colornames, colors, &nColorsDone, errorP); } - if (!*errorP) { - *nColorsP = nColorsDone; - - while (nColorsDone < MAXCOLORNAMES) - colornames[nColorsDone++] = NULL; - } + *nColorsP = nColorsDone; if (*errorP) { unsigned int colorIndex; @@ -560,26 +595,6 @@ readOpenColorFile(FILE * const colorFileP, -static colorhash_table -allocColorHash(void) { - - colorhash_table cht; - jmp_buf jmpbuf; - jmp_buf * origJmpbufP; - - if (setjmp(jmpbuf) != 0) - cht = NULL; - else { - pm_setjmpbufsave(&jmpbuf, &origJmpbufP); - cht = ppm_alloccolorhash(); - } - pm_setjmpbuf(origJmpbufP); - - return cht; -} - - - static void readColorFile(const char * const fileName, bool const mustOpen, @@ -588,7 +603,20 @@ readColorFile(const char * const fileName, pixel * const colors, colorhash_table const cht, const char ** const errorP) { +/*---------------------------------------------------------------------------- + Read the color dictionary file named 'fileName' and add the colors in it + to colornames[], colors[], and 'cht'. Return as *nColorsP the number + of colors in it. + If the file is not openable (e.g. not file by that name exists), abort the + program if 'mustOpen' is true; otherwise, return values indicating a + dictionary with no colors. + + colornames[] and colors[] must be allocated with MAXCOLORNAMES entries + at entry. + + We may add colors to 'cht' even if we fail. +-----------------------------------------------------------------------------*/ FILE * colorFileP; openColornameFile(fileName, mustOpen, &colorFileP, errorP); @@ -598,11 +626,6 @@ readColorFile(const char * const fileName, empty file */ *nColorsP = 0; - { - unsigned int i; - for (i = 0; i < MAXCOLORNAMES; ++i) - colornames[i] = NULL; - } *errorP = NULL; } else { readOpenColorFile(colorFileP, nColorsP, colornames, colors, cht, @@ -626,7 +649,7 @@ readcolordict(const char * const fileName, const char ** colornames; - MALLOCARRAY(colornames, MAXCOLORNAMES); + colornames = allocColorNames(); if (colornames == NULL) pm_asprintf(errorP, "Unable to allocate space for colorname table."); @@ -675,7 +698,28 @@ ppm_readcolordict(const char * const fileName, const char *** const colornamesP, pixel ** const colorsP, colorhash_table * const chtP) { +/*---------------------------------------------------------------------------- + Read the color dictionary from the file named 'fileName'. If we can't open + the file (e.g. because it does not exist), and 'mustOpen' is false, return + an empty dictionary (it contains no colors). But if 'mustOpen' is true, + abort the program instead of returning an empty dictionary. + + Return as *nColorsP the number of colors in the dictionary. + Return as *colornamesP the names of those colors. *colornamesP is a + malloced array that Caller must free with ppm_freecolornames(). + The first *nColorsP entries are valid; *chtP contains indices into this + array. + + Return as *colorsP the colors. *colorsP is a malloced array of size + MAXCOLORS with the first elements filled in and the rest undefined. + + Return as *chtP a color hash table mapping each color in the dictionary + to the index into *colornamesP for the name of the color. + + Each of 'nColorsP, 'colornamesP', and 'colorsP' may be null, in which case + we do not return the corresponding information (or allocate memory for it). +-----------------------------------------------------------------------------*/ colorhash_table cht; const char ** colornames; pixel * colors; diff --git a/lib/libsystem.c b/lib/libsystem.c index 259f18e4..b8c3ea50 100644 --- a/lib/libsystem.c +++ b/lib/libsystem.c @@ -501,20 +501,20 @@ pm_feed_from_memory(int const pipeToFeedFd, struct bufferDesc * const inputBufferP = feederParm; - FILE * const outfile = fdopen(pipeToFeedFd, "w"); + FILE * const outFileP = fdopen(pipeToFeedFd, "w"); - int bytesTransferred; + size_t bytesTransferred; /* The following signals (and normally kills) the process with SIGPIPE if the pipe does not take all 'size' bytes. */ bytesTransferred = - fwrite(inputBufferP->buffer, 1, inputBufferP->size, outfile); + fwrite(inputBufferP->buffer, 1, inputBufferP->size, outFileP); if (inputBufferP->bytesTransferredP) *(inputBufferP->bytesTransferredP) = bytesTransferred; - fclose(outfile); + fclose(outFileP); } @@ -525,14 +525,14 @@ pm_accept_to_memory(int const pipetosuckFd, struct bufferDesc * const outputBufferP = accepterParm; - FILE * const infile = fdopen(pipetosuckFd, "r"); + FILE * const inFileP = fdopen(pipetosuckFd, "r"); - int bytesTransferred; + size_t bytesTransferred; bytesTransferred = - fread(outputBufferP->buffer, 1, outputBufferP->size, infile); + fread(outputBufferP->buffer, 1, outputBufferP->size, inFileP); - fclose(infile); + fclose(inFileP); if (outputBufferP->bytesTransferredP) *(outputBufferP->bytesTransferredP) = bytesTransferred; diff --git a/lib/pam.h b/lib/pam.h index 6213dab3..2726092b 100644 --- a/lib/pam.h +++ b/lib/pam.h @@ -52,9 +52,13 @@ struct pam { */ FILE * file; int format; - /* The format code of the raw image. This is PAM_FORMAT + /* The format code of the image. This is PAM_FORMAT unless the PAM image is really a view of a PBM, PGM, or PPM - image. Then it's PBM_FORMAT, RPBM_FORMAT, etc. + image. Then it's PBM_FORMAT, RPBM_FORMAT, etc. For output, + only the format _type_ is significant, e.g. PBM_FORMAT + and RPBM_FORMAT have identical effect. This is because on + output, 'plainformat' determines whether the output is the + raw or plain format of the type given by 'format'. */ unsigned int plainformat; /* Logical: On output, use plain version of the format type @@ -67,6 +71,9 @@ struct pam { Before Netpbm 10.32, this was rather different. It simply described for convenience the plainness of the format indicated by 'format'. + + This is meaningless when 'format' is PAM_FORMAT, as PAM does not + have plain and raw variations. */ int height; /* Height of image in rows */ int width; @@ -514,6 +521,8 @@ pnm_backgroundtuple(struct pam * const pamP, /*---------------------------------------------------------------------------- These are meant for passing to pm_system() as Standard Input feeder and Standard Output accepter. + + The 'feederParm' or 'accepterParm' is a pointer to a struct pamtuples. -----------------------------------------------------------------------------*/ void diff --git a/lib/pmfileio.c b/lib/pmfileio.c index 82b44cdb..1263261a 100644 --- a/lib/pmfileio.c +++ b/lib/pmfileio.c @@ -919,10 +919,6 @@ pm_check(FILE * const file, pm_filepos curpos; /* Current position of file; -1 if none */ int rc; -#ifdef LARGEFILEDEBUG - pm_message("pm_filepos received by pm_check() is %u bytes.", - sizeof(pm_filepos)); -#endif /* Note: FTELLO() is either ftello() or ftell(), depending on the capabilities of the underlying C library. It is defined in pm_config.h. ftello(), in turn, may be either ftell() or diff --git a/lib/util/mallocvar.h b/lib/util/mallocvar.h index 31ad38bc..e92e3fe4 100644 --- a/lib/util/mallocvar.h +++ b/lib/util/mallocvar.h @@ -87,7 +87,7 @@ reallocProduct(void ** const blockP, void * array; \ array = arrayName; \ reallocProduct(&array, nElements, sizeof(arrayName[0])); \ - if (!array) \ + if (!array && arrayName) \ free(arrayName); \ arrayName = array; \ } while (0) diff --git a/lib/util/pm_c_util.h b/lib/util/pm_c_util.h index f17d1e03..79897cf0 100644 --- a/lib/util/pm_c_util.h +++ b/lib/util/pm_c_util.h @@ -41,35 +41,49 @@ use "int". */ -/* We used to assume that if TRUE was defined, then bool was too. - However, we had a report on 2001.09.21 of a Tru64 system that had - TRUE but not bool and on 2002.03.21 of an AIX 4.3 system that was - likewise. So now we define bool all the time, unless the macro - HAVE_BOOL is defined. If someone is using the Netpbm libraries and - also another library that defines bool, he can either make the - other library define/respect HAVE_BOOL or just define HAVE_BOOL in - the file that includes pm_config.h or with a compiler option. Note - that C++ always has bool. +/* We will probably never again see a system that does not have + <stdbool.h>, but just in case, we have our own alternative here. - A preferred way of getting booleans is <stdbool.h>. But it's not - available on all platforms, and it's easy to reproduce what it does - here. + Evidence shows that the compiler actually produces better code with + <stdbool.h> than with bool simply typedefed to int. */ + +#ifdef __cplusplus + /* C++ has a bool type and false and true constants built in. */ +#else + /* The test for __STDC__ is paranoid. It is there just in case some + nonstandard compiler defines __STDC_VERSION__ in an arbitrary manner. + */ + #if ( defined(__GNUC__) && (__GNUC__ >= 3) ) || \ + ( defined(__STDC__) && (__STDC__ ==1) && \ + defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) ) + #include <stdbool.h> + #else + /* We used to assume that if TRUE was defined, then bool was too. + However, we had a report on 2001.09.21 of a Tru64 system that had + TRUE but not bool and on 2002.03.21 of an AIX 4.3 system that was + likewise. So now we define bool all the time, unless the macro + HAVE_BOOL is defined. If someone is using the Netpbm libraries and + also another library that defines bool, he can either make the + other library define/respect HAVE_BOOL or just define HAVE_BOOL in + the file that includes pm_config.h or with a compiler option. Note + that C++ always has bool. + */ + #ifndef HAVE_BOOL + #define HAVE_BOOL 1 + typedef int bool; + #endif + #ifndef true + enum boolvalue {false=0, true=1}; + #endif + #endif +#endif + #ifndef TRUE - #define TRUE 1 + #define TRUE true #endif #ifndef FALSE - #define FALSE 0 - #endif -/* C++ has a bool type and false and true constants built in. */ -#ifndef __cplusplus - #ifndef HAVE_BOOL - #define HAVE_BOOL 1 - typedef int bool; - #endif - #ifndef true - enum boolvalue {false=0, true=1}; - #endif +#define FALSE false #endif #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |