diff options
Diffstat (limited to 'converter/other/pnmtopalm/palmtopnm.c')
-rw-r--r-- | converter/other/pnmtopalm/palmtopnm.c | 402 |
1 files changed, 206 insertions, 196 deletions
diff --git a/converter/other/pnmtopalm/palmtopnm.c b/converter/other/pnmtopalm/palmtopnm.c index 0f76207d..b3c0321a 100644 --- a/converter/other/pnmtopalm/palmtopnm.c +++ b/converter/other/pnmtopalm/palmtopnm.c @@ -21,17 +21,18 @@ #include "mallocvar.h" #include "palm.h" +#include "palmcolormap.h" -enum palmCompressionType { - COMPRESSION_NONE, - COMPRESSION_RLE, +enum PalmCompressionType { + COMPRESSION_NONE, + COMPRESSION_RLE, COMPRESSION_SCANLINE, COMPRESSION_PACKBITS }; -struct palmHeader { +struct PalmHeader { unsigned short cols; unsigned short rows; unsigned short bytesPerRow; @@ -46,7 +47,7 @@ struct palmHeader { unsigned int pixelSize; unsigned char version; unsigned int transparentIndex; - enum palmCompressionType compressionType; + enum PalmCompressionType compressionType; /* version 3 encoding specific */ unsigned char size; unsigned char pixelFormat; @@ -56,7 +57,7 @@ struct palmHeader { -struct directPixelFormat { +struct DirectPixelFormat { unsigned int redbits; unsigned int greenbits; unsigned int bluebits; @@ -64,15 +65,15 @@ struct directPixelFormat { -struct directColorInfo { - struct directPixelFormat pixelFormat; - Color_s transparentColor; +struct DirectColorInfo { + struct DirectPixelFormat pixelFormat; + ColormapEntry transparentColor; }; -struct cmdlineInfo { +struct CmdlineInfo { /* All the information the user supplied in the command line, in a form easy for the program to use. */ @@ -85,13 +86,13 @@ struct cmdlineInfo { static void -parseCommandLine(int argc, char ** argv, - struct cmdlineInfo *cmdlineP) { +parseCommandLine(int argc, const char ** argv, + struct CmdlineInfo *cmdlineP) { /*---------------------------------------------------------------------------- Note that the file spec array we return is stored in the storage that was passed to us as the argv array. -----------------------------------------------------------------------------*/ - optEntry *option_def = malloc( 100*sizeof( optEntry ) ); + optEntry * option_def; /* Instructions to pm_optParseOptions3 on how to parse our options. */ optStruct3 opt; @@ -100,6 +101,8 @@ parseCommandLine(int argc, char ** argv, unsigned int option_def_index; + MALLOCARRAY_NOFAIL(option_def, 100); + option_def_index = 0; /* incremented by OPTENTRY */ OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); @@ -107,23 +110,23 @@ parseCommandLine(int argc, char ** argv, &cmdlineP->showhist, 0); OPTENT3(0, "transparent", OPT_FLAG, NULL, &cmdlineP->transparent, 0); - OPTENT3(0, "rendition", OPT_UINT, &cmdlineP->rendition, + OPTENT3(0, "rendition", OPT_UINT, &cmdlineP->rendition, &renditionSpec, 0); opt.opt_table = option_def; opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ opt.allowNegNum = FALSE; /* We may have parms that are negative numbers */ - pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); + pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); /* Uses and sets argc, argv, and some of *cmdlineP and others. */ if (renditionSpec) { if (cmdlineP->rendition < 1) pm_error("The -rendition value must be at least 1"); - } else + } else cmdlineP->rendition = 1; - + if (cmdlineP->transparent && cmdlineP->showhist) pm_error("You can't specify -showhist with -transparent"); @@ -135,12 +138,13 @@ parseCommandLine(int argc, char ** argv, pm_error("Too many arguments (%d). The only non-option " "argument is the file name", argc-1); } + free(option_def); } static xelval * -createGraymap(unsigned int const ncolors, +createGraymap(unsigned int const ncolors, xelval const maxval) { int i; xelval *map; @@ -154,8 +158,8 @@ createGraymap(unsigned int const ncolors, -static void -skipbytes(FILE * const ifP, +static void +skipbytes(FILE * const ifP, unsigned int const nbytes) { unsigned char buf[256]; @@ -176,15 +180,15 @@ skipbytes(FILE * const ifP, pm_error("Error reading Palm file. Short read."); n = 0; } - } + } } static void interpretCompression(unsigned char const compressionValue, - enum palmCompressionType * const compressionTypeP) { - + enum PalmCompressionType * const compressionTypeP) { + switch (compressionValue) { case PALM_COMPRESSION_RLE: *compressionTypeP = COMPRESSION_RLE; @@ -220,17 +224,18 @@ readRestOfHeaderVersion3(FILE * const ifP, short * const nextDepthOffsetP) { unsigned char unused; - + pm_readcharu(ifP, sizeP); /* should be 0x18, but I can't see why we should really care */ if (*sizeP != 0x18) - pm_message("Strange value for Palm bitmap header size: %hu", *sizeP); + pm_message("Strange value for Palm bitmap header size: %u", + (unsigned)*sizeP); pm_readcharu(ifP, pixelFormatP); if (*pixelFormatP != PALM_FORMAT_INDEXED && *pixelFormatP != PALM_FORMAT_565) pm_error("Unrecognized pixelformat type: %u", *pixelFormatP); - + pm_readcharu(ifP, &unused); @@ -244,15 +249,15 @@ readRestOfHeaderVersion3(FILE * const ifP, *densityP != PALM_DENSITY_TRIPLE && *densityP != PALM_DENSITY_QUADRUPLE) pm_error("Invalid value for -density: %d.", *densityP); - + pm_readbiglong(ifP, transparentValueP); if (pixelSize < 16) *transparentIndexP = *transparentValueP; else *transparentIndexP = 0; - + pm_readbiglong(ifP, nextBitmapOffsetP); - + /* version < 3 specific */ *nextDepthOffsetP = 0; } @@ -269,18 +274,18 @@ readRestOfHeaderOld(FILE * const ifP, long * const transparentValueP, long * const nextBitmapOffsetP, short * const nextDepthOffsetP) { - + short pad; unsigned char transparentIndex; - + pm_readbigshort(ifP, nextDepthOffsetP); pm_readcharu(ifP, &transparentIndex); *transparentIndexP = transparentIndex; - + pm_readcharu(ifP,compressionTypeP); - + pm_readbigshort(ifP, &pad); /* reserved by Palm as of 8/9/00 */ - + /* version 3 specific */ *sizeP = 0; *pixelFormatP = 0; @@ -292,7 +297,7 @@ readRestOfHeaderOld(FILE * const ifP, static void -interpretHeader(struct palmHeader * const palmHeaderP, +interpretHeader(struct PalmHeader * const palmHeaderP, short const cols, short const rows, short const bytesPerRow, @@ -306,7 +311,7 @@ interpretHeader(struct palmHeader * const palmHeaderP, long const transparentValue, unsigned int const transparentIndex, unsigned char const compressionType) { - + palmHeaderP->cols = cols; palmHeaderP->rows = rows; palmHeaderP->bytesPerRow = bytesPerRow; @@ -329,11 +334,11 @@ interpretHeader(struct palmHeader * const palmHeaderP, */ pm_error("PALM_DIRECT_COLOR_FLAG is set but pixelFormat is not" "PALM_FORMAT_565."); - - palmHeaderP->directColor = ((flags & PALM_DIRECT_COLOR_FLAG) || + + palmHeaderP->directColor = ((flags & PALM_DIRECT_COLOR_FLAG) || palmHeaderP->pixelFormat == PALM_FORMAT_565); - - if (flags & PALM_IS_COMPRESSED_FLAG) + + if (flags & PALM_IS_COMPRESSED_FLAG) interpretCompression(compressionType, &palmHeaderP->compressionType); else @@ -345,7 +350,7 @@ interpretHeader(struct palmHeader * const palmHeaderP, static void readHeader(FILE * const ifP, unsigned int const requestedRendition, - struct palmHeader * const palmHeaderP) { + struct PalmHeader * const palmHeaderP) { /*---------------------------------------------------------------------------- Read the Palm Bitmap header from the file 'ifP'. Read past all renditions up to 'requestedRendition' and read the header of that @@ -358,7 +363,7 @@ readHeader(FILE * const ifP, currentRendition = 1; while (!gotHeader) { short cols, rows, bytesPerRow, flags, nextDepthOffset, density; - unsigned char pixelSizeCode, version, compressionType, + unsigned char pixelSizeCode, version, compressionType, size, pixelFormat; long transparentValue, nextBitmapOffset; unsigned int pixelSize, transparentIndex; @@ -367,7 +372,7 @@ readHeader(FILE * const ifP, pm_readbigshort(ifP, &rows); pm_readbigshort(ifP, &bytesPerRow); pm_readbigshort(ifP, &flags); - + pm_readcharu(ifP, &pixelSizeCode); pixelSize = pixelSizeCode == 0 ? 1 : pixelSizeCode; if (pixelSizeCode != 0x00 && @@ -384,12 +389,12 @@ readHeader(FILE * const ifP, "bits per pixel.", bytesPerRow, cols, pixelSize); pm_readcharu(ifP, &version); - if (version > 3) + if (version > 3) pm_error("Unknown encoding version type: %d", version); else if (version == 3) readRestOfHeaderVersion3(ifP, pixelSize, &size, &pixelFormat, &compressionType, - &density, &transparentIndex, + &density, &transparentIndex, &transparentValue, &nextBitmapOffset, &nextDepthOffset); else @@ -400,29 +405,29 @@ readHeader(FILE * const ifP, &nextDepthOffset); if (currentRendition < requestedRendition) { - if (version < 3 && nextDepthOffset == 0 && pixelSizeCode != 0xFF) + if (version < 3 && nextDepthOffset == 0 && pixelSizeCode != 0xFF) pm_error("Not enough renditions in the input Palm Bitmap " "to extract the %dth", requestedRendition); - if (version == 3 && nextBitmapOffset == 0) + if (version == 3 && nextBitmapOffset == 0) pm_error("Not enough renditions in the input Palm Bitmap " "to extract the %dth", requestedRendition); /* nextDepthOffset is calculated in 4 byte words - from the beginning of this bitmap (so it equals its size) + from the beginning of this bitmap (so it equals its size) */ if (version < 3 && pixelSizeCode != 0xFF ) skipbytes(ifP, (nextDepthOffset*4)-16); else if (version == 3) /* FIXME rewrite skipbytes to accept longs? */ - skipbytes(ifP, (short) nextBitmapOffset-24); + skipbytes(ifP, (short) nextBitmapOffset-24); if (pixelSizeCode != 0xFF) ++currentRendition; } else if (pixelSizeCode != 0xFF) { gotHeader = TRUE; - + interpretHeader(palmHeaderP, cols, rows, bytesPerRow, flags, pixelSizeCode, pixelSize, version, size, pixelFormat, density, - transparentValue, transparentIndex, + transparentValue, transparentIndex, compressionType); } } @@ -441,8 +446,8 @@ yesno(bool const arg) { static void -reportPalmHeader(struct palmHeader const palmHeader, - struct directColorInfo const directColorInfo) { +reportPalmHeader(struct PalmHeader const palmHeader, + struct DirectColorInfo const directColorInfo) { const char *ctype; @@ -462,9 +467,9 @@ reportPalmHeader(struct palmHeader const palmHeader, } pm_message("Dimensions: %hu columns x %hu rows", palmHeader.cols, palmHeader.rows); - pm_message("Row layout: %hu bytes per row, %hu bits per pixel", + pm_message("Row layout: %hu bytes per row, %u bits per pixel", palmHeader.bytesPerRow, palmHeader.pixelSize); - pm_message("Pixel Size code: %hu", palmHeader.pixelSizeCode); + pm_message("Pixel Size code: %u", (unsigned)palmHeader.pixelSizeCode); pm_message("Flags: 0x%04hx", palmHeader.flags); pm_message(" Direct Color: %s", yesno(palmHeader.directColor)); pm_message(" Colormap: %s", yesno(palmHeader.hasColormap)); @@ -473,10 +478,10 @@ reportPalmHeader(struct palmHeader const palmHeader, if (palmHeader.hasTransparency) { if (palmHeader.directColor) { /* Copied from doTransparent(...) */ - Color_s const color = directColorInfo.transparentColor; - pm_message("Transparent value: #%02x%02x%02x", - (unsigned int)((color >> 16) & 0xFF), - (unsigned int)((color >> 8) & 0xFF), + ColormapEntry const color = directColorInfo.transparentColor; + pm_message("Transparent value: #%02x%02x%02x", + (unsigned int)((color >> 16) & 0xFF), + (unsigned int)((color >> 8) & 0xFF), (unsigned int)((color >> 0) & 0xFF)); } else pm_message("Transparent index: %u", palmHeader.transparentIndex); @@ -489,7 +494,7 @@ reportPalmHeader(struct palmHeader const palmHeader, static void -determineOutputFormat(struct palmHeader const palmHeader, +determineOutputFormat(struct PalmHeader const palmHeader, int * const formatP, xelval * const maxvalP) { @@ -515,14 +520,14 @@ determineOutputFormat(struct palmHeader const palmHeader, static void readRgbFormat(FILE * const ifP, - struct directPixelFormat * const pixelFormatP) { + struct DirectPixelFormat * const pixelFormatP) { unsigned char r, g, b; pm_readcharu(ifP, &r); pm_readcharu(ifP, &g); pm_readcharu(ifP, &b); - + if (r != 5 || g != 6 || b != 5) pm_error("This image has a direct color pixel format of " "%u red, %u green, %u blue bits. This program " @@ -537,8 +542,8 @@ readRgbFormat(FILE * const ifP, static void -readDirectTransparentColor(FILE * const ifP, - Color_s * const colorP) { +readDirectTransparentColor(FILE * const ifP, + ColormapEntry * const colorP) { unsigned char r, g, b; @@ -553,8 +558,8 @@ readDirectTransparentColor(FILE * const ifP, static void readDirectInfoType(FILE * const ifP, - struct palmHeader const palmHeader, - struct directColorInfo * const directInfoTypeP) { + struct PalmHeader const palmHeader, + struct DirectColorInfo * const directInfoTypeP) { /*---------------------------------------------------------------------------- Read the Palm Bitmap Direct Info Type section, if any. @@ -568,12 +573,12 @@ readDirectInfoType(FILE * const ifP, if ((palmHeader.directColor) && palmHeader.pixelSize != 16) pm_error("The image is of the direct color type, but has %u " "bits per pixel. The only kind of direct color images " - "this program understands are 16 bit ones.", + "this program understands are 16 bit ones.", palmHeader.pixelSize); if (palmHeader.version == 3) { /* All direct color info is in the header, because it'sversion - 3 encoding. No Direct Info Type section. + 3 encoding. No Direct Info Type section. */ } else { if (palmHeader.directColor) { @@ -584,7 +589,7 @@ readDirectInfoType(FILE * const ifP, pm_readcharu(ifP, &padding); pm_readcharu(ifP, &padding); - readDirectTransparentColor(ifP, + readDirectTransparentColor(ifP, &directInfoTypeP->transparentColor); } else { /* Not a direct color image; no Direct Info Type section. */ @@ -596,8 +601,8 @@ readDirectInfoType(FILE * const ifP, static void readColormap(FILE * const ifP, - struct palmHeader const palmHeader, - Colormap * const colormapP) { + struct PalmHeader const palmHeader, + Colormap ** const colormapPP) { /*---------------------------------------------------------------------------- Read the colormap, if any from the Palm Bitmap. @@ -605,18 +610,18 @@ readColormap(FILE * const ifP, return an undefined value as *colormapP. -----------------------------------------------------------------------------*/ if (palmHeader.hasColormap) - *colormapP = palmcolor_read_colormap(ifP); + *colormapPP = palmcolor_read_colormap(ifP); } static void -getColorInfo(struct palmHeader const palmHeader, - struct directColorInfo const directInfoType, - Colormap const colormapFromImage, - Colormap * const colormapP, +getColorInfo(struct PalmHeader const palmHeader, + struct DirectColorInfo const directInfoType, + Colormap * const colormapFromImageP, + Colormap ** const colormapPP, unsigned int * const ncolorsP, - struct directColorInfo * const directColorInfoP) { + struct DirectColorInfo * const directColorInfoP) { /*---------------------------------------------------------------------------- Gather color encoding information from the various sources. @@ -626,14 +631,14 @@ getColorInfo(struct palmHeader const palmHeader, If it's a version 3 direct color, the pixel format must be "565". -----------------------------------------------------------------------------*/ if (palmHeader.version == 3 && palmHeader.directColor) { - *colormapP = NULL; + *colormapPP = NULL; assert(palmHeader.pixelFormat == PALM_FORMAT_565); directColorInfoP->pixelFormat.redbits = 5; directColorInfoP->pixelFormat.greenbits = 6; directColorInfoP->pixelFormat.bluebits = 5; - directColorInfoP->transparentColor = + directColorInfoP->transparentColor = /* See convertRowToPnmDirect for this trick This will break once maxval isn't always set 255 for @@ -646,18 +651,18 @@ getColorInfo(struct palmHeader const palmHeader, ((((palmHeader.transparentValue >> 0) & 0x1F) * 255 / 0x1F) << 0); } else if (palmHeader.directColor) { - *colormapP = NULL; + *colormapPP = NULL; *directColorInfoP = directInfoType; } else if (palmHeader.hasColormap) - *colormapP = colormapFromImage; + *colormapPP = colormapFromImageP; else if (palmHeader.pixelSize >= 8) { - Colormap colormap; - colormap = palmcolor_build_default_8bit_colormap(); - qsort(colormap->color_entries, colormap->ncolors, - sizeof(Color_s), palmcolor_compare_indices); - *colormapP = colormap; + Colormap * const colormapP = + palmcolor_build_default_8bit_colormap(); + qsort(colormapP->color_entries, colormapP->ncolors, + sizeof(ColormapEntry), palmcolor_compare_indices); + *colormapPP = colormapP; } else - *colormapP = NULL; + *colormapPP = NULL; *ncolorsP = 1 << palmHeader.pixelSize; } @@ -670,8 +675,8 @@ doTransparent(FILE * const ofP, bool const directColor, unsigned char const transparentIndex, unsigned char const pixelSize, - Colormap const colormap, - struct directColorInfo const directColorInfo) { + Colormap * const colormapP, + struct DirectColorInfo const directColorInfo) { /*---------------------------------------------------------------------------- Generate a PNM comment on *ofP telling what color in the raster is supposed to be transparent. @@ -681,31 +686,32 @@ doTransparent(FILE * const ofP, RGB_ALPHA and GRAYSCALE_ALPHA tuple types). -----------------------------------------------------------------------------*/ if (hasTransparency) { - if (colormap) { - Color_s const color = transparentIndex << 24; - Color const actualColor = (bsearch(&color, - colormap->color_entries, - colormap->ncolors, - sizeof(color), - palmcolor_compare_indices)); - if (!actualColor) + if (colormapP) { + ColormapEntry const searchTarget = transparentIndex << 24; + ColormapEntry * const foundEntryP = + bsearch(&searchTarget, + colormapP->color_entries, + colormapP->ncolors, + sizeof(searchTarget), + palmcolor_compare_indices); + if (!foundEntryP) pm_error("Invalid input; transparent index %u " "is not among the %u colors in the image's colormap", - transparentIndex, colormap->ncolors); + transparentIndex, colormapP->ncolors); - fprintf(ofP, "#%02x%02x%02x\n", - (unsigned int) ((*actualColor >> 16) & 0xFF), - (unsigned int) ((*actualColor >> 8) & 0xFF), - (unsigned int) ((*actualColor >> 0) & 0xFF)); + fprintf(ofP, "#%02x%02x%02x\n", + (unsigned int) ((*foundEntryP >> 16) & 0xFF), + (unsigned int) ((*foundEntryP >> 8) & 0xFF), + (unsigned int) ((*foundEntryP >> 0) & 0xFF)); } else if (directColor) { - Color_s const color = directColorInfo.transparentColor; - fprintf(ofP, "#%02x%02x%02x\n", - (unsigned int)((color >> 16) & 0xFF), - (unsigned int)((color >> 8) & 0xFF), + ColormapEntry const color = directColorInfo.transparentColor; + fprintf(ofP, "#%02x%02x%02x\n", + (unsigned int)((color >> 16) & 0xFF), + (unsigned int)((color >> 8) & 0xFF), (unsigned int)((color >> 0) & 0xFF)); } else { unsigned int const maxval = pm_bitstomaxval(pixelSize); - unsigned int const grayval = + unsigned int const grayval = ((maxval - transparentIndex) * 256) / maxval; fprintf(ofP, "#%02x%02x%02x\n", grayval, grayval, grayval); } @@ -723,10 +729,10 @@ createHistogram(unsigned int const ncolors, MALLOCARRAY(seen, ncolors); if (!seen) pm_error("Can't allocate array for keeping track of " - "how many pixels of each of %u colors are in the image.", + "how many pixels of each of %u colors are in the image.", ncolors); - { + { /* Initialize the counter for each color to zero */ unsigned int i; for (i = 0; i < ncolors; ++i) @@ -761,7 +767,7 @@ readScanlineRow(FILE * const ifP, pm_readcharu(ifP, &diffmask); byteCount = MIN(bytesPerRow - j, 8); - + for (k = 0; k < byteCount; ++k) { /* the first row cannot be compressed */ if (firstRow || ((diffmask & (1 << (7 - k))) != 0)) { @@ -800,7 +806,7 @@ readRleRow(FILE * const ifP, memset(palmrow + j, inval, incount); j += incount; } -} +} @@ -810,7 +816,7 @@ readPackBitsRow16(FILE * const ifP, unsigned int const bytesPerRow) { /* From the Palm OS Programmer's API Reference: - + Although the [...] spec is byte-oriented, the 16-bit algorithm is identical [to the 8-bit algorithm]: just substitute "word" for "byte". */ @@ -819,7 +825,7 @@ readPackBitsRow16(FILE * const ifP, for (j = 0; j < bytesPerRow; ) { char incount; pm_readchar(ifP, &incount); - if (incount < 0) { + if (incount < 0) { /* How do we handle incount == -128 ? */ unsigned int const runlength = (-incount + 1) * 2; unsigned int k; @@ -841,13 +847,13 @@ readPackBitsRow16(FILE * const ifP, } j += nonrunlength; } - if (j > bytesPerRow) + if (j > bytesPerRow) pm_error("Bytes in PackBits compressed row exceed bytes per row. " "Bytes per row is %u. " "The bytes in this row were pushed up to %u bytes " "(and then we gave up).", bytesPerRow, j); } -} +} @@ -861,7 +867,7 @@ readPackBitsRow(FILE * const ifP, for (j = 0; j < bytesPerRow; ) { char incount; pm_readchar(ifP, &incount); - if (incount < 0) { + if (incount < 0) { /* How do we handle incount == -128 ? */ unsigned int const runlength = -incount + 1; unsigned char inval; @@ -879,13 +885,13 @@ readPackBitsRow(FILE * const ifP, } j += nonrunlength; } - if (j > bytesPerRow) + if (j > bytesPerRow) pm_error("Bytes in PackBits compressed row exceed bytes per row. " "Bytes per row is %u. " "The bytes in this row were pushed up to %u bytes " "(and then we gave up).", bytesPerRow, j); } -} +} @@ -895,7 +901,7 @@ readUncompressedRow(FILE * const ifP, unsigned int const bytesPerRow) { int bytesRead; - + bytesRead = fread(palmrow, 1, bytesPerRow, ifP); if (bytesRead != bytesPerRow) pm_error("Error reading Palm file. Short read."); @@ -907,7 +913,7 @@ static void readDecompressedRow(FILE * const ifP, unsigned char * const palmrow, unsigned char * const lastrow, - enum palmCompressionType const compressionType, + enum PalmCompressionType const compressionType, unsigned int const bytesPerRow, unsigned int const pixelSize, bool const firstRow) { @@ -968,27 +974,27 @@ convertRowToPnmDirect(const unsigned char * const palmrow, blue=0x1F. How do we promote those colors? Simple shift would give us R=248,G=252,B=248; which is slightly green. Hardly seems right. - + So I've perverted the math a bit. Each color value is multiplied by 255, then divided by either 31 (red or blue) or 63 (green). That's the right way to do it - anyway. + anyway. */ const unsigned char *inbyte; unsigned int j; - + for (inbyte = palmrow, j = 0; j < cols; ++j) { unsigned int inval; inval = *inbyte++ << 8; inval |= *inbyte++; - + if (seen) ++seen[inval]; - - PPM_ASSIGN(xelrow[j], - (((inval >> 11) & 0x1F) * maxval) / 0x1F, - (((inval >> 5) & 0x3F) * maxval) / 0x3F, + + PPM_ASSIGN(xelrow[j], + (((inval >> 11) & 0x1F) * maxval) / 0x1F, + (((inval >> 5) & 0x3F) * maxval) / 0x3F, (((inval >> 0) & 0x1F) * maxval) / 0x1F ); } @@ -1000,46 +1006,50 @@ static void convertRowToPnmNotDirect(const unsigned char * const palmrow, xel * const xelrow, unsigned int const cols, - Colormap const colormap, + Colormap * const colormapP, xelval * const graymap, unsigned int * const seen, unsigned int const pixelSize) { unsigned int const mask = (1 << pixelSize) - 1; - const unsigned char *inbyte; + const unsigned char * inbyteP; unsigned int inbit; unsigned int j; - + + assert(pixelSize <= 8); + inbit = 8 - pixelSize; - inbyte = palmrow; + inbyteP = &palmrow[0]; for (j = 0; j < cols; ++j) { - short const color = ((*inbyte) & (mask << inbit)) >> inbit; + short const color = (*inbyteP & (mask << inbit)) >> inbit; if (seen) ++seen[color]; - - if (colormap) { - Color_s const color2 = color << 24; - Color const actualColor = (bsearch (&color2, - colormap->color_entries, - colormap->ncolors, - sizeof(color2), - palmcolor_compare_indices)); - if (!actualColor) + + if (colormapP) { + ColormapEntry const searchTarget = color << 24; + ColormapEntry * const foundEntryP = + bsearch(&searchTarget, + colormapP->color_entries, + colormapP->ncolors, + sizeof(searchTarget), + palmcolor_compare_indices); + + if (!foundEntryP) pm_error("Invalid input. A color index in column %u " "is %u, which is not among the %u colors " "in the colormap", - j, color, colormap->ncolors); + j, color, colormapP->ncolors); - PPM_ASSIGN(xelrow[j], - (*actualColor >> 16) & 0xFF, - (*actualColor >> 8) & 0xFF, - (*actualColor >> 0) & 0xFF); + PPM_ASSIGN(xelrow[j], + (*foundEntryP >> 16) & 0xFF, + (*foundEntryP >> 8) & 0xFF, + (*foundEntryP >> 0) & 0xFF); } else PNM_ASSIGN1(xelrow[j], graymap[color]); - + if (!inbit) { - ++inbyte; + ++inbyteP; inbit = 8 - pixelSize; } else inbit -= pixelSize; @@ -1050,9 +1060,9 @@ convertRowToPnmNotDirect(const unsigned char * const palmrow, static void writePnm(FILE * const ofP, - struct palmHeader const palmHeader, + struct PalmHeader const palmHeader, FILE * const ifP, - Colormap const colormap, + Colormap * const colormapP, xelval * const graymap, unsigned int const nColors, int const format, @@ -1067,22 +1077,22 @@ writePnm(FILE * const ofP, xel * xelrow; unsigned int * seen; unsigned int row; - + pnm_writepnminit(ofP, cols, rows, maxval, format, 0); xelrow = pnm_allocrow(cols); /* Read the picture data, one row at a time */ MALLOCARRAY_NOFAIL(palmrow, palmHeader.bytesPerRow); - MALLOCARRAY_NOFAIL(lastrow, palmHeader.bytesPerRow); - + MALLOCARRAY_NOFAIL(lastrow, palmHeader.bytesPerRow); + if (seenP) { createHistogram(nColors, &seen); *seenP = seen; } else seen = NULL; - /* We should actually use compressedDataSizeNN for checking the sanity - of the data we're reading ... + /* We should actually use compressedDataSizeNN for checking the sanity + of the data we're reading ... */ if (palmHeader.compressionType != COMPRESSION_NONE) { if (palmHeader.version < 3) { @@ -1095,8 +1105,8 @@ writePnm(FILE * const ofP, } for (row = 0; row < rows; ++row) { - readDecompressedRow(ifP, palmrow, lastrow, - palmHeader.compressionType, + readDecompressedRow(ifP, palmrow, lastrow, + palmHeader.compressionType, palmHeader.bytesPerRow, palmHeader.pixelSize, row == 0); @@ -1105,7 +1115,7 @@ writePnm(FILE * const ofP, assert(palmHeader.pixelSize == 16); convertRowToPnmDirect(palmrow, xelrow, cols, maxval, seen); } else - convertRowToPnmNotDirect(palmrow, xelrow, cols, colormap, graymap, + convertRowToPnmNotDirect(palmrow, xelrow, cols, colormapP, graymap, seen, palmHeader.pixelSize); pnm_writepnmrow(ofP, xelrow, cols, maxval, format, 0); @@ -1119,28 +1129,29 @@ writePnm(FILE * const ofP, static void showHistogram(unsigned int * const seen, - Colormap const colormap, + Colormap * const colormapP, const xelval * const graymap, unsigned int const ncolors) { unsigned int colorIndex; - + for (colorIndex = 0; colorIndex < ncolors; ++colorIndex) { - if (!colormap) - pm_message("%.3d -> %.3d: %d", + if (!colormapP) + pm_message("%.3d -> %.3d: %d", colorIndex, graymap[colorIndex], seen[colorIndex]); else { - Color_s const color = colorIndex << 24; - Color const actualColor = (bsearch(&color, - colormap->color_entries, - colormap->ncolors, - sizeof(color), - palmcolor_compare_indices)); - if (actualColor) + ColormapEntry const searchTarget = colorIndex << 24; + ColormapEntry * const foundEntryP = + bsearch(&searchTarget, + colormapP->color_entries, + colormapP->ncolors, + sizeof(searchTarget), + palmcolor_compare_indices); + if (foundEntryP) pm_message("%.3d -> %ld,%ld,%ld: %d", colorIndex, - (*actualColor >> 16) & 0xFF, - (*actualColor >> 8) & 0xFF, - (*actualColor & 0xFF), seen[colorIndex]); + (*foundEntryP >> 16) & 0xFF, + (*foundEntryP >> 8) & 0xFF, + (*foundEntryP & 0xFF), seen[colorIndex]); } } } @@ -1148,22 +1159,21 @@ showHistogram(unsigned int * const seen, int -main(int argc, char **argv) { +main(int argc, const char **argv) { - struct cmdlineInfo cmdline; + struct CmdlineInfo cmdline; - FILE* ifP; - struct palmHeader palmHeader; - struct directColorInfo directInfoType; - Colormap colormapFromImage; - Colormap colormap; - struct directColorInfo directColorInfo; + FILE * ifP; + struct PalmHeader palmHeader; + struct DirectColorInfo directInfoType; + Colormap * colormapFromImageP; + Colormap * colormapP; + struct DirectColorInfo directColorInfo; int format; xelval maxval; unsigned int nColors; - /* Parse default params */ - pnm_init(&argc, argv); + pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); @@ -1172,22 +1182,22 @@ main(int argc, char **argv) { readHeader(ifP, cmdline.rendition, &palmHeader); readDirectInfoType(ifP, palmHeader, &directInfoType); - - readColormap(ifP, palmHeader, &colormapFromImage); - + + readColormap(ifP, palmHeader, &colormapFromImageP); + determineOutputFormat(palmHeader, &format, &maxval); - - getColorInfo(palmHeader, directInfoType, colormapFromImage, - &colormap, &nColors, &directColorInfo); + + getColorInfo(palmHeader, directInfoType, colormapFromImageP, + &colormapP, &nColors, &directColorInfo); if (cmdline.verbose) reportPalmHeader(palmHeader, directColorInfo); if (cmdline.transparent) - doTransparent(stdout, + doTransparent(stdout, palmHeader.hasTransparency, palmHeader.directColor, - palmHeader.transparentIndex, - palmHeader.pixelSize, colormap, directColorInfo); + palmHeader.transparentIndex, + palmHeader.pixelSize, colormapP, directColorInfo); else { unsigned int * seen; xelval * graymap; @@ -1195,12 +1205,12 @@ main(int argc, char **argv) { graymap = createGraymap(nColors, maxval); writePnm(stdout, - palmHeader, ifP, colormap, graymap, nColors, format, maxval, + palmHeader, ifP, colormapP, graymap, nColors, format, maxval, cmdline.showhist ? &seen : NULL); - + if (cmdline.showhist) - showHistogram(seen, colormap, graymap, nColors); - + showHistogram(seen, colormapP, graymap, nColors); + free(graymap); } pm_close(ifP); |