static void determineOutputTupleType(BaseTupletype const baseTupletype, bool const underlayHaveOpacity, char * const tupleType, size_t const size) { char buffer[80]; switch (baseTupletype) { case TT_BLACKANDWHITE: STRSCPY(buffer, "RGB"); break; case TT_GRAYSCALE: STRSCPY(buffer, "GRAYSCALE"); break; case TT_RGB: STRSCPY(buffer, "RGB"); break; } if (underlayHaveOpacity) STRSCAT(buffer, "_ALPHA"); strncpy(tupleType, buffer, size); }
static void getline(char * const line, size_t const size, FILE * const stream) { /*---------------------------------------------------------------------------- Read the next line from the input file 'stream', through the one-line buffer lastInputLine[]. If 'backup' is true, the "next line" is the previously read line, i.e. the one in that one-line buffer. Otherwise, the "next line" is the next line from the real file. After reading the backed up line, we reset 'backup' to false. Return the line as a null terminated string in *line, which is an array of 'size' bytes. Exit program if the line doesn't fit in the buffer. -----------------------------------------------------------------------------*/ if (size > sizeof(lastInputLine)) pm_error("INTERNAL ERROR: getline() received 'size' parameter " "which is out of bounds"); if (backup) { strncpy(line, lastInputLine, size); backup = FALSE; } else { if (fgets(line, size, stream) == NULL) pm_error("EOF or read error on input file"); if (strlen(line) == size - 1) pm_error("Input file has line that is too long (longer than " "%u bytes).", (unsigned)size - 1); STRSCPY(lastInputLine, line); } }
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 formatPeerInfoInet6(const struct sockaddr_in6 * const sockaddrIn6P, socklen_t const sockaddrLen, const char ** const peerStringP) { if (sockaddrLen < sizeof(*sockaddrIn6P)) xmlrpc_asprintf(peerStringP, "??? getpeername() returned " "the wrong size"); else { /* Gcc on Debian 6 gives a bewildering error message about aliasing if we try to dereference sockaddrIn6P in various ways, regardless of casts to void * or anything else. So we copy the data structure as raw memory contents and then access the copy. 12.02.05. */ struct sockaddr_in6 sockaddr; MEMSCPY(&sockaddr, sockaddrIn6P); { char buffer[256]; const char * rc; rc = inet_ntop(AF_INET6, &sockaddr.sin6_addr, buffer, sizeof(buffer)); /* Punt the supposedly impossible error */ if (rc == NULL) STRSCPY(buffer, "???"); xmlrpc_asprintf(peerStringP, "[%s]:%hu", buffer, sockaddr.sin6_port); } } }
int main(int argc, const char ** argv) { struct cmdlineInfo cmdline; FILE * ifP; tuple ** outTuples; /* Output image */ scaler * scalerP; struct pam inpam; struct pam outpam; pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFileName); pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(allocation_depth)); pnm_setminallocationdepth(&inpam, 3); outpam.size = sizeof(outpam); outpam.len = PAM_STRUCT_SIZE(tuple_type); outpam.file = stdout; outpam.width = inpam.width; outpam.height = inpam.height; outpam.depth = 3; outpam.maxval = pm_lcm(cmdline.colorRes.c[RED]-1, cmdline.colorRes.c[GRN]-1, cmdline.colorRes.c[BLU]-1, PPM_MAXMAXVAL); outpam.bytes_per_sample = inpam.bytes_per_sample; STRSCPY(outpam.tuple_type, "RGB"); outpam.format = RPPM_FORMAT; outpam.plainformat = false; scaler_create(outpam.maxval, cmdline.colorRes, &scalerP); ditherImage(&inpam, scalerP, cmdline.dim, cmdline.colorRes, &outpam, &outTuples); pnm_writepam(&outpam, outTuples); scaler_destroy(scalerP); pnm_freepamarray(outTuples, &outpam); pm_close(ifP); return 0; }
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; }
static void parseCommandLine(int argc, char ** argv, struct cmdlineInfo * const 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; /* Instructions to OptParseOptions3 on how to parse our options. */ optStruct3 opt; unsigned int option_def_index; const char * nameOpt; unsigned int nameSpec; MALLOCARRAY(option_def, 100); option_def_index = 0; /* incremented by OPTENTRY */ OPTENT3(0, "alphamask", OPT_STRING, &cmdlineP->alpha_filename, NULL, 0); OPTENT3(0, "name", OPT_STRING, &nameOpt, &nameSpec, 0); OPTENT3(0, "rgb", OPT_STRING, &cmdlineP->rgb, NULL, 0); OPTENT3(0, "hexonly", OPT_FLAG, NULL, &cmdlineP->hexonly, 0); OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); /* Set the defaults */ cmdlineP->alpha_filename = NULL; /* no transparency */ cmdlineP->rgb = NULL; /* no rgb file specified */ 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); /* Uses and sets argc, argv, and some of *cmdlineP and others. */ if (argc-1 == 0) cmdlineP->inputFilename = "-"; else if (argc-1 != 1) pm_error("Program takes zero or one argument (filename). You " "specified %d", argc-1); else cmdlineP->inputFilename = argv[1]; /* If output filename not specified, use input filename as default. */ if (nameSpec) cmdlineP->name = nameOpt; else if (streq(cmdlineP->inputFilename, "-")) cmdlineP->name = "noname"; else { static char name[80+1]; char *cp; STRSCPY(name, cmdlineP->inputFilename); cp = strchr(name, '.'); if (cp) *cp = '\0'; /* remove extension */ cmdlineP->name = name; } }