static void computeDepth(xelval const input_maxval, unsigned int * const bitspersampleP, unsigned int * const ps_maxvalP) { /*---------------------------------------------------------------------------- Figure out how many bits will represent each sample in the Postscript program, and the maxval of the Postscript program samples. The maxval is just the maximum value allowable in the number of bits. -----------------------------------------------------------------------------*/ unsigned int const bitsRequiredByMaxval = pm_maxvaltobits(input_maxval); if (bitsRequiredByMaxval <= 1) *bitspersampleP = 1; else if (bitsRequiredByMaxval > 1 && bitsRequiredByMaxval <= 4) *bitspersampleP = 4; else *bitspersampleP = 8; if (*bitspersampleP < bitsRequiredByMaxval) pm_message("Maxval of input requires %u bit samples for full " "resolution, but we are using the Postscript maximum " "of %u", bitsRequiredByMaxval, *bitspersampleP); *ps_maxvalP = pm_bitstomaxval(*bitspersampleP); if (verbose) pm_message("Input maxval is %u. Postscript raster will have " "%u bits per sample, so maxval = %u", input_maxval, *bitspersampleP, *ps_maxvalP); }
static void convertImage(FILE * const ofP, struct cmdlineInfo const cmdline, struct jpeg_decompress_struct * const cinfoP) { int format; /* The type of output file, PGM or PPM. Value is either PPM_TYPE or PGM_TYPE, which conveniently also pass as format values PPM_FORMAT and PGM_FORMAT. */ xelval maxval; /* The maximum value of a sample (color component), both in the input and the output. */ enum colorspace color_space; /* The color space of the pixels coming out of the JPEG decompressor */ beginJpegInput(cinfoP, cmdline.verbose, cmdline.dct_method, cmdline.max_memory_to_use, cmdline.nosmooth); set_color_spaces(cinfoP->jpeg_color_space, &format, &cinfoP->out_color_space); maxval = pm_bitstomaxval(cinfoP->data_precision); if (cmdline.verbose) tellDetails(*cinfoP, maxval, format); /* Calculate output image dimensions so we can allocate space */ jpeg_calc_output_dimensions(cinfoP); /* Start decompressor */ jpeg_start_decompress(cinfoP); if (ofP) /* Write pnm output header */ pnm_writepnminit(ofP, cinfoP->output_width, cinfoP->output_height, maxval, format, FALSE); pnmbuffer = pnm_allocrow(cinfoP->output_width); color_space = computeColorSpace(cinfoP, cmdline.inklevel); convertRaster(cinfoP, color_space, ofP, format, maxval); if (cmdline.comments) print_comments(*cinfoP); if (cmdline.dumpexif) dump_exif(*cinfoP); if (cmdline.exif_filespec) save_exif(*cinfoP, cmdline.exif_filespec); pnm_freerow(pnmbuffer); /* Finish decompression and release decompressor memory. */ jpeg_finish_decompress(cinfoP); }
static void computeComponentMaxval(struct pam * const outpamP, jas_image_t * const jasperP, int const jasperCmpt[], sample ** const jasperMaxvalP, bool * const singleMaxvalP) { sample * jasperMaxval; unsigned int plane; MALLOCARRAY(jasperMaxval, outpamP->depth); *singleMaxvalP = TRUE; /* initial assumption */ for (plane = 0; plane < outpamP->depth; ++plane) { jasperMaxval[plane] = pm_bitstomaxval(jas_image_cmptprec(jasperP, jasperCmpt[plane])); if (jasperMaxval[plane] != jasperMaxval[0]) *singleMaxvalP = FALSE; } *jasperMaxvalP = jasperMaxval; }
static void computeOutputParm(jas_image_t * const jasperP, struct pam * const outpamP, int ** const jasperCmptNoP) { int * jasperCmptNo; /* Malloc'ed array. jaspercmptNo[P] is the component number for use with the Jasper library that corresponds to Plane P of the PAM. */ switch (jas_clrspc_fam(jas_image_clrspc(jasperP))) { case JAS_CLRSPC_FAM_GRAY: outpamP->depth = 1; MALLOCARRAY_NOFAIL(jasperCmptNo, 1); getGrayComponent(&jasperCmptNo[0], jasperP); if (jas_image_cmptprec(jasperP, jasperCmptNo[0]) == 1) { outpamP->format = RPBM_FORMAT; strcpy(outpamP->tuple_type, PAM_PBM_TUPLETYPE); } else { outpamP->format = RPGM_FORMAT; strcpy(outpamP->tuple_type, PAM_PGM_TUPLETYPE); } break; case JAS_CLRSPC_FAM_RGB: outpamP->depth = 3; MALLOCARRAY_NOFAIL(jasperCmptNo, 3); getRgbComponents(jasperCmptNo, jasperP); outpamP->format = RPPM_FORMAT; strcpy(outpamP->tuple_type, PAM_PPM_TUPLETYPE); break; default: outpamP->format = PAM_FORMAT; outpamP->depth = jas_image_numcmpts(jasperP); { unsigned int plane; MALLOCARRAY_NOFAIL(jasperCmptNo, outpamP->depth); for (plane = 0; plane < outpamP->depth; ++plane) jasperCmptNo[plane] = plane; } strcpy(outpamP->tuple_type, ""); if (jas_image_cmptsgnd(jasperP, 0)) pm_message("Warning: Input image has signed sample values. " "They will be represented in the PAM output in " "two's complement."); } outpamP->plainformat = FALSE; outpamP->width = jas_image_cmptwidth(jasperP, 0); outpamP->height = jas_image_cmptheight(jasperP, 0); validateComponentsAlike(jasperP); { unsigned int const maxPrecision = maxJasperComponentPrecision(jasperP); outpamP->maxval = pm_bitstomaxval(maxPrecision); outpamP->bytes_per_sample = (maxPrecision + 7)/8; } *jasperCmptNoP = jasperCmptNo; }
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); }
static void createJasperRaster(struct pam * const inpamP, jas_image_t * const jasperP) { jas_matrix_t ** matrix; /* malloc'ed */ /* matrix[X] is the data for Plane X of the current row */ unsigned int plane; unsigned int row; tuple * tuplerow; bool oddMaxval; sample jasperMaxval; matrix = malloc(inpamP->depth * sizeof(jas_matrix_t *)); if (matrix == NULL) pm_error("Out of memory"); for (plane = 0; plane < inpamP->depth; ++plane) { matrix[plane] = jas_matrix_create(1, inpamP->width); if (matrix[plane] == NULL) pm_error("Unable to create matrix for plane %u. " "jas_matrix_create() failed.", plane); } tuplerow = pnm_allocpamrow(inpamP); jasperMaxval = pm_bitstomaxval(pm_maxvaltobits(inpamP->maxval)); oddMaxval = jasperMaxval != inpamP->maxval; for (row = 0; row < inpamP->height; ++row) { unsigned int col; pnm_readpamrow(inpamP, tuplerow); for (col = 0; col < inpamP->width; ++col) { unsigned int plane; for (plane = 0; plane < inpamP->depth; ++plane) { unsigned int jasperSample; if (oddMaxval) jasperSample = tuplerow[col][plane] * jasperMaxval / inpamP->maxval; else jasperSample = tuplerow[col][plane]; jas_matrix_set(matrix[plane], 0, col, jasperSample); } } { unsigned int plane; for (plane = 0; plane < inpamP->depth; ++plane) { int rc; rc = jas_image_writecmpt(jasperP, plane, 0, row, inpamP->width, 1, matrix[plane]); if (rc != 0) pm_error("jas_image_writecmpt() of plane %u failed.", plane); } } } pnm_freepamrow(tuplerow); for (plane = 0; plane < inpamP->depth; ++plane) jas_matrix_destroy(matrix[plane]); free(matrix); }