static void colormapToImage(int const format, const struct pam * const colormapPamP, tupletable2 const colormap, bool const sort, bool const square, struct pam * const outpamP, tuple *** const outputRasterP) { /*---------------------------------------------------------------------------- Create a tuple array and pam structure for an image which includes one pixel of each of the colors in the colormap 'colormap'. May rearrange the contents of 'colormap'. -----------------------------------------------------------------------------*/ outpamP->size = sizeof(*outpamP); outpamP->len = PAM_STRUCT_SIZE(tuple_type); outpamP->format = format, outpamP->plainformat = FALSE; outpamP->depth = colormapPamP->depth; outpamP->maxval = colormapPamP->maxval; outpamP->bytes_per_sample = pnm_bytespersample(outpamP->maxval); STRSCPY(outpamP->tuple_type, colormapPamP->tuple_type); if (sort) sortColormap(colormap, outpamP->depth); if (square) colormapToSquare(outpamP, colormap, outputRasterP); else colormapToSingleRow(outpamP, colormap, outputRasterP); }
static void createoutputGenerator(struct cmdlineInfo const cmdline, const struct pam * const inPamP, outGenerator ** const outputGeneratorPP) { outGenerator * outGenP; MALLOCVAR_NOFAIL(outGenP); outGenP->pam.size = sizeof(struct pam); outGenP->pam.len = PAM_STRUCT_SIZE(tuple_type); outGenP->pam.file = stdout; outGenP->pam.height = inPamP->height + 3 * abs(cmdline.guidesize); /* Allow room for guides. */ outGenP->pam.width = inPamP->width; if (cmdline.patFilespec) { /* Background pixels should come from the pattern file. */ initPatternPixel(outGenP, cmdline); } else { /* Background pixels should be generated randomly */ initRandomColor(outGenP, inPamP, cmdline); } outGenP->pam.bytes_per_sample = pnm_bytespersample(outGenP->pam.maxval); *outputGeneratorPP = outGenP; }
int main(int argc, char *argv[]) { struct cmdlineInfo cmdline; struct pam pam; tuple * tupleRow; tuple * leftEdge; tuple * rightEdge; unsigned int row; pnm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); pam.size = sizeof pam; pam.len = PAM_STRUCT_SIZE(tuple_type); pam.file = stdout; pam.plainformat = 0; pam.width = cmdline.cols; pam.height = cmdline.rows; pam.maxval = cmdline.maxval; pam.bytes_per_sample = pnm_bytespersample(pam.maxval); pam.format = PAM_FORMAT; if (isgray(&pam, cmdline.colorTopLeft) && isgray(&pam, cmdline.colorTopRight) && isgray(&pam, cmdline.colorBottomLeft) && isgray(&pam, cmdline.colorBottomRight)) { pam.depth = 1; strcpy(pam.tuple_type, PAM_PGM_TUPLETYPE); } else { pam.depth = 3; strcpy(pam.tuple_type, PAM_PPM_TUPLETYPE); } pnm_writepaminit(&pam); tupleRow = pnm_allocpamrow(&pam); leftEdge = createEdge(&pam, cmdline.colorTopLeft, cmdline.colorBottomLeft); rightEdge = createEdge(&pam, cmdline.colorTopRight, cmdline.colorBottomRight); /* interpolate each row between the left edge and the right edge */ for (row = 0; row < pam.height; ++row) { interpolate(&pam, tupleRow, leftEdge[row], rightEdge[row]); pnm_writepamrow(&pam, tupleRow); } pm_close(stdout); pnm_freepamrow(rightEdge); pnm_freepamrow(leftEdge); pnm_freepamrow(tupleRow); freeCmdline(cmdline); return 0; }
static void writeImgPam(IPDB * const pdbP, FILE * const ofP) { struct pam pam; tuple * tupleRow; unsigned int row; uint8_t * imgRow; MALLOCARRAY(imgRow, ipdb_width(pdbP)); pam.size = sizeof(pam); pam.len = PAM_STRUCT_SIZE(tuple_type); pam.file = ofP; pam.plainformat = 0; pam.width = ipdb_width(pdbP); pam.height = ipdb_height(pdbP); pam.depth = 1; pam.maxval = ipdb_type(pdbP) == IMG_MONO ? 1 : 255; pam.bytes_per_sample = pnm_bytespersample(pam.maxval); pam.format = PAM_FORMAT; strcpy(pam.tuple_type, ipdb_type(pdbP) == IMG_MONO ? PAM_PBM_TUPLETYPE : PAM_PGM_TUPLETYPE); pnm_writepaminit(&pam); tupleRow = pnm_allocpamrow(&pam); for (row = 0; row < pam.height; ++row) { unsigned int col; if (ipdb_type(pdbP) == IMG_MONO) mrow(pdbP, row, imgRow); else if (ipdb_type(pdbP) == IMG_GRAY) grow(pdbP, row, imgRow); else g16row(pdbP, row, imgRow); for (col = 0; col < pam.width; ++col) tupleRow[col][0] = imgRow[col]; pnm_writepamrow(&pam, tupleRow); } pnm_freepamrow(tupleRow); free(imgRow); }
static void initOutpam(const struct pam * const inpamP, struct pam * const outpamP) { outpamP->file = stdout; outpamP->format = PAM_FORMAT; outpamP->plainformat = 0; outpamP->width = inpamP->width; outpamP->height = inpamP->height; outpamP->depth = 1; outpamP->maxval = 1; outpamP->bytes_per_sample = pnm_bytespersample(outpamP->maxval); outpamP->len = PAM_STRUCT_SIZE(bytes_per_sample); outpamP->size = sizeof(*outpamP); }
static void computeHistogram(FILE * const ifP, int * const formatP, struct pam * const freqPamP, tupletable2 * const colorfreqtableP) { /*---------------------------------------------------------------------------- Make a histogram of the colors in the image stream in the file '*ifP'. Return as *freqPamP a description of the tuple values in the histogram. Only the fields of *freqPamP that describe individual tuples are meaningful (depth, maxval, tuple type); As a fringe benefit, also return the format of the input file as *formatP. ----------------------------------------------------------------------------*/ unsigned int imageSeq; struct pam firstPam; tuplehash tuplehash; unsigned int colorCount; int eof; pm_message("making histogram..."); tuplehash = pnm_createtuplehash(); colorCount = 0; eof = FALSE; for (imageSeq = 0; !eof; ++imageSeq) { struct pam inpam; pm_message("Scanning image %u", imageSeq); pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); if (imageSeq == 0) firstPam = inpam; else validateCompatibleImage(&inpam, &firstPam, imageSeq); addImageColorsToHash(&inpam, tuplehash, &colorCount); pm_message("%u colors so far", colorCount); pnm_nextimage(ifP, &eof); } colorfreqtableP->table = pnm_tuplehashtotable(&firstPam, tuplehash, colorCount); colorfreqtableP->size = colorCount; pnm_destroytuplehash(tuplehash); pm_message("%u colors found", colorfreqtableP->size); freqPamP->size = sizeof(*freqPamP); freqPamP->len = PAM_STRUCT_SIZE(tuple_type); freqPamP->maxval = firstPam.maxval; freqPamP->bytes_per_sample = pnm_bytespersample(freqPamP->maxval); freqPamP->depth = firstPam.depth; STRSCPY(freqPamP->tuple_type, firstPam.tuple_type); *formatP = firstPam.format; }