static void computeColorMap(struct pam * const pamP, tuple ** const tupleArray, unsigned int * const numColorsP, tupletable * const colormapP, tuplehash * const colorhashP, bool const show) { unsigned int numColors; tupletable colormap; colormap = pnm_computetuplefreqtable(pamP, tupleArray, 0, &numColors); if (numColors > 0xFF0) pm_error("too many colors; " "use pnmquant to reduce to no more than %u colors", 0xFF0); if (show) { unsigned int colorIndex; fprintf(stderr, "Color map:\n"); fprintf(stderr, " Index Color\n"); for (colorIndex = 0; colorIndex < numColors; ++colorIndex) { unsigned int plane; fprintf(stderr, " %5u ", colorIndex); for (plane = 0; plane < pamP->depth; ++plane) fprintf(stderr, "%3lu ", colormap[colorIndex]->tuple[plane]); fprintf(stderr, "\n"); } } *colorhashP = pnm_computetupletablehash(pamP, colormap, numColors); *numColorsP = numColors; *colormapP = colormap; }
static void computeImageType_cht(struct pam * const pamP, struct cmdlineInfo const cmdline, tuple ** const tuples, enum TGAbaseImageType * const baseImgTypeP, bool * const withAlphaP, tupletable * const chvP, tuplehash * const chtP, int * const ncolorsP) { unsigned int ncolors; enum TGAbaseImageType baseImgType; bool withAlpha; validateTupleType(pamP); withAlpha = (streq(pamP->tuple_type, "RGB_ALPHA")); if (cmdline.defaultFormat) { /* default the image type */ if (withAlpha) { baseImgType = TGA_RGB_TYPE; *chvP = NULL; } else if (pamP->depth > 1) { pm_message("computing colormap..."); *chvP = pnm_computetuplefreqtable(pamP, tuples, MAXCOLORS, &ncolors); if (*chvP == NULL) { pm_message("Too many colors for colormapped TGA. Doing RGB."); baseImgType = TGA_RGB_TYPE; } else baseImgType = TGA_MAP_TYPE; } else { baseImgType = TGA_MONO_TYPE; *chvP = NULL; } } else { baseImgType = cmdline.imgType; if (baseImgType == TGA_MAP_TYPE) { if (withAlpha) pm_error("Can't do a colormap because image has transparency " "information"); pm_message("computing colormap..."); *chvP = pnm_computetuplefreqtable(pamP, tuples, MAXCOLORS, &ncolors); if (*chvP == NULL) pm_error("Too many colors for colormapped TGA. " "Use 'pnmquant %d' to reduce the number of colors.", MAXCOLORS); } else *chvP = NULL; if (baseImgType == TGA_MONO_TYPE && pamP->depth > 1) pm_error("For Mono TGA output, input must be " "GRAYSCALE or BLACKANDWHITE PAM or PBM or PGM"); } if (baseImgType == TGA_MAP_TYPE) { pm_message("%d colors found.", ncolors); /* Make a hash table for fast color lookup. */ *chtP = pnm_computetupletablehash(pamP, *chvP, ncolors); } else *chtP = NULL; *baseImgTypeP = baseImgType; *withAlphaP = withAlpha; *ncolorsP = ncolors; }
static void convertImage(FILE * const ifP, TIFF * const tifP, const char * const inputFileDescription, struct cmdlineInfo const cmdline) { tupletable chv; tuplehash cht; unsigned short ** tiffColorMap; /* malloc'ed */ struct pam pam; unsigned int colors; bool grayscale; unsigned short photometric; unsigned short samplesperpixel; unsigned short bitspersample; unsigned short tiff_maxval; /* This is the maxval of the samples in the tiff file. It is determined solely by the bits per sample ('bitspersample'). */ int bytesperrow; pm_filepos rasterPos; pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type)); pm_tell2(ifP, &rasterPos, sizeof(rasterPos)); analyzeColors(&pam, cmdline, MAXCOLORS, &chv, &colors, &grayscale); /* Go back to beginning of raster */ pm_seek2(ifP, &rasterPos, sizeof(rasterPos)); /* Figure out TIFF parameters. */ computeRasterParm(&pam, chv, colors, grayscale, cmdline.compression, cmdline.minisblack, cmdline.miniswhite, cmdline.indexsizeAllowed, &samplesperpixel, &bitspersample, &photometric, &bytesperrow); tiff_maxval = pm_bitstomaxval(bitspersample); if (!chv) { cht = NULL; tiffColorMap = NULL; } else { createTiffColorMap(&pam, bitspersample, chv, colors, &tiffColorMap); /* Convert color vector to color hash table, for fast lookup. */ cht = pnm_computetupletablehash(&pam, chv, colors); pnm_freetupletable(&pam, chv); } setTiffFields(tifP, cmdline, &pam, bitspersample, photometric, samplesperpixel, tiffColorMap, inputFileDescription, cmdline.taglist); writeScanLines(&pam, tifP, cht, tiff_maxval, bitspersample, photometric, bytesperrow, cmdline.fillorder); if (tiffColorMap) destroyTiffColorMap(&pam, tiffColorMap); }