static xel background2Corners(FILE * const ifP, int const cols, pixval const maxval, int const format) { /*---------------------------------------------------------------------------- Look at just the top row of pixels and determine the background color from the top corners; often this is enough to accurately determine the background color. Expect the file to be positioned to the start of the raster, and leave it positioned arbitrarily. ----------------------------------------------------------------------------*/ xel *xelrow; xel background; /* our return value */ xelrow = pnm_allocrow(cols); pnm_readpnmrow(ifP, xelrow, cols, maxval, format); background = pnm_backgroundxelrow(xelrow, cols, maxval, format); pnm_freerow(xelrow); return background; }
static void extract_one_image(FILE *infile, const char outputfilename[]) { FILE *outfile; xelval maxval; int rows, cols, format; enum pm_check_code check_retval; int row; xel *xelrow; pnm_readpnminit(infile, &cols, &rows, &maxval, &format); pnm_check(infile, PM_CHECK_BASIC, format, cols, rows, maxval, &check_retval); outfile = pm_openw(outputfilename); pnm_writepnminit(outfile, cols, rows, maxval, format, 0); xelrow = pnm_allocrow(cols); for (row = 0; row < rows; row++) { pnm_readpnmrow(infile, xelrow, cols, maxval, format); pnm_writepnmrow(outfile, xelrow, cols, maxval, format, 0); } pnm_freerow(xelrow); pm_close(outfile); }
static void writeConvolutionImage(FILE * const cofp, unsigned int const cols, unsigned int const rows, int const format) { xelval const convmaxval = rows * cols * 2; /* normalizing factor for our convolution matrix */ xelval const g = rows * cols + 1; /* weight of all pixels in our convolution matrix */ int row; xel *outputrow; if (convmaxval > PNM_OVERALLMAXVAL) pm_error("The convolution matrix is too large. " "Width x Height x 2\n" "must not exceed %d and it is %d.", PNM_OVERALLMAXVAL, convmaxval); pnm_writepnminit(cofp, cols, rows, convmaxval, format, 0); outputrow = pnm_allocrow(cols); for (row = 0; row < rows; ++row) { unsigned int col; for (col = 0; col < cols; ++col) PNM_ASSIGN1(outputrow[col], g); pnm_writepnmrow(cofp, outputrow, cols, convmaxval, format, 0); } pnm_freerow(outputrow); }
static void concatenateLeftRightGen(FILE * const ofP, unsigned int const nfiles, unsigned int const newcols, unsigned int const newrows, xelval const newmaxval, int const newformat, enum justification const justification, struct imgInfo const img[], enum backcolor const backcolor) { xel * const outrow = pnm_allocrow(newcols); struct imgGen2 * img2; unsigned int row; getGenImgInfo(img, nfiles, outrow, newrows, newmaxval, newformat, justification, backcolor, &img2); for (row = 0; row < newrows; ++row) { unsigned int i; for (i = 0; i < nfiles; ++i) { if ((row == 0 && img2[i].padtop > 0) || row == img2[i].padtop + img[i].rows) { /* This row begins a run of padding, either above or below file 'i', so set 'outrow' to padding. */ unsigned int col; for (col = 0; col < img[i].cols; ++col) img2[i].inrow[col] = img2[i].background; } if (row == img2[i].padtop && img2[i].xelrow) { /* We're at the top row of file 'i', and that row has already been read to xelrow[] to determine background. Copy it to 'outrow'. */ unsigned int col; for (col = 0; col < img[i].cols; ++col) img2[i].inrow[col] = img2[i].xelrow[col]; free(img2[i].xelrow); } else if (row >= img2[i].padtop && row < img2[i].padtop + img[i].rows) { pnm_readpnmrow( img[i].ifP, img2[i].inrow, img[i].cols, img[i].maxval, img[i].format); pnm_promoteformatrow( img2[i].inrow, img[i].cols, img[i].maxval, img[i].format, newmaxval, newformat); } else { /* It's a row of padding, so outrow[] is already set appropriately. */ } } pnm_writepnmrow(ofP, outrow, newcols, newmaxval, newformat, 0); } pnm_freerow(outrow); }
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); }
/* Draw a pair of guide boxes. */ static void drawguides(int const guidesize, const struct pam * const outPamP, double const eyesep, unsigned int const dpi, double const depthOfField) { int const far = separation(0, eyesep, dpi, depthOfField); /* Space between the two guide boxes. */ int const width = outPamP->width; /* Width of the output image */ tuple *outrow; /* One row of output data */ tuple blackTuple; int col; pnm_createBlackTuple(outPamP, &blackTuple); outrow = pnm_allocpamrow(outPamP); /* Leave some blank rows before the guides. */ makeWhiteRow(outPamP, outrow); writeRowCopies(outPamP, outrow, guidesize); /* Draw the guides. */ if ((width - far + guidesize)/2 < 0 || (width + far - guidesize)/2 >= width) pm_message("warning: the guide boxes are completely out of bounds " "at %d DPI", dpi); else if ((width - far - guidesize)/2 < 0 || (width + far + guidesize)/2 >= width) pm_message("warning: the guide boxes are partially out of bounds " "at %d DPI", dpi); for (col = (width - far - guidesize)/2; col < (width - far + guidesize)/2; ++col) if (col >= 0 && col < width) pnm_assigntuple(outPamP, outrow[col], blackTuple); for (col = (width + far - guidesize)/2; col < (width + far + guidesize)/2; ++col) if (col >= 0 && col < width) pnm_assigntuple(outPamP, outrow[col], blackTuple); writeRowCopies(outPamP,outrow, guidesize); /* Leave some blank rows after the guides. */ makeWhiteRow(outPamP, outrow); writeRowCopies(outPamP, outrow, guidesize); pnm_freerow(outrow); }
static void readOffBorderNonPbm(unsigned int const height, FILE * const ifP, unsigned int const cols, xelval const maxval, int const format) { xel * xelrow; unsigned int i; xelrow = pnm_allocrow(cols); for (i = 0; i < height; ++i) pnm_readpnmrow(ifP, xelrow, cols, maxval, format); pnm_freerow(xelrow); }
static void outputNewBorderNonPbm(unsigned int const height, unsigned int const width, xel const color, FILE * const ofP, xelval const maxval, int const format) { /*---------------------------------------------------------------------------- Output to 'ofP' a horizontal border (i.e. top or bottom) of color 'backgroundColor', height 'height', width 'width'. -----------------------------------------------------------------------------*/ xel * xelrow; unsigned int i; xelrow = pnm_allocrow(width); fillRow(xelrow, width, color); for (i = 0; i < height; ++i) pnm_writepnmrow(ofP, xelrow, width, maxval, format, 0); pnm_freerow(xelrow); }
int main(int argc, char *argv[]) { FILE* ifp; xel* xelrow; /* Row from input image */ xel* output_row; /* Row of output image */ xelval maxval; int rows, cols, format, row; int leftcol, rightcol, toprow, bottomrow; int output_cols; /* Width of output image */ struct cmdline_info cmdline; pnm_init( &argc, argv ); parse_command_line(argc, argv, &cmdline); ifp = pm_openr(cmdline.input_filespec); pnm_readpnminit(ifp, &cols, &rows, &maxval, &format); xelrow = pnm_allocrow(cols); black_xel = pnm_blackxel(maxval, format); compute_cut_bounds(cols, rows, cmdline.left, cmdline.right, cmdline.top, cmdline.bottom, cmdline.width, cmdline.height, &leftcol, &rightcol, &toprow, &bottomrow); if (!cmdline.pad) reject_out_of_bounds(cols, rows, leftcol, rightcol, toprow, bottomrow); if (cmdline.verbose) { pm_message("Image goes from Row 0, Column 0 through Row %d, Column %d", rows-1, cols-1); pm_message("Cutting from Row %d, Column %d through Row %d Column %d", toprow, leftcol, bottomrow, rightcol); } output_cols = rightcol-leftcol+1; output_row = pnm_allocrow(output_cols); pnm_writepnminit(stdout, output_cols, bottomrow-toprow+1, maxval, format, 0 ); /* Implementation note: If speed is ever an issue, we can probably speed up significantly the non-padding case by writing a special case loop here for the case cmdline.pad == FALSE. */ /* Write out top padding */ write_black_rows(stdout, 0 - toprow, output_cols, output_row, maxval, format); /* Read input and write out rows extracted from it */ for (row = 0; row < rows; row++) { pnm_readpnmrow(ifp, xelrow, cols, maxval, format); if (row >= toprow && row <= bottomrow) { int col; /* Put in left padding */ for (col = leftcol; col < 0; col++) { output_row[col-leftcol] = black_xel; } /* Put in extracted columns */ for (col = MAX(leftcol, 0); col <= MIN(rightcol, cols-1); col++) { output_row[col-leftcol] = xelrow[col]; } /* Put in right padding */ for (col = MAX(cols, leftcol); col <= rightcol; col++) { output_row[col-leftcol] = black_xel; } pnm_writepnmrow(stdout, output_row, output_cols, maxval, format, 0); } } /* Note that we may be tempted just to quit after reaching the bottom of the extracted image, but that would cause a broken pipe problem for the process that's feeding us the image. */ /* Write out bottom padding */ write_black_rows(stdout, bottomrow - (rows-1), output_cols, output_row, maxval, format); pnm_freerow(output_row); pnm_freerow(xelrow); pm_close(ifp); pm_close(stdout); exit( 0 ); }
static void convertImage(FILE * const ofP, struct cmdlineInfo const cmdline, struct jpeg_decompress_struct * const cinfoP) { int output_type; /* 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. */ JSAMPROW jpegbuffer; /* Input buffer. Filled by jpeg_scanlines() */ unsigned int 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, &output_type, &cinfoP->out_color_space); maxval = (1 << cinfoP->data_precision) - 1; if (cmdline.verbose) tellDetails(*cinfoP, maxval, output_type); /* Calculate output image dimensions so we can allocate space */ jpeg_calc_output_dimensions(cinfoP); overflow2(cinfoP->output_width, cinfoP->output_components); jpegbuffer = ((*cinfoP->mem->alloc_sarray) ((j_common_ptr) cinfoP, JPOOL_IMAGE, cinfoP->output_width * cinfoP->output_components, (JDIMENSION) 1) )[0]; /* Start decompressor */ jpeg_start_decompress(cinfoP); if (ofP) /* Write pnm output header */ pnm_writepnminit(ofP, cinfoP->output_width, cinfoP->output_height, maxval, output_type, FALSE); pnmbuffer = pnm_allocrow(cinfoP->output_width); color_space = computeColorSpace(cinfoP, cmdline.inklevel); /* Process data */ while (cinfoP->output_scanline < cinfoP->output_height) { jpeg_read_scanlines(cinfoP, &jpegbuffer, 1); if (ofP) copy_pixel_row(jpegbuffer, cinfoP->output_width, cinfoP->out_color_components, color_space, maxval, ofP, output_type); } 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 writeCroppedNonPbm(FILE * const ifP, unsigned int const cols, unsigned int const rows, xelval const maxval, int const format, cropSet const crop, xel const backgroundColor, FILE * const ofP) { /* In order to do cropping, padding or both at the same time, we have a rather complicated row buffer: xelrow[] is both the input and the output buffer. So it contains the foreground pixels, the original border pixels, and the new border pixels. We're calling foreground everything that isn't being cropped out or padded in. So the "foreground" may include some of what is really a background border in the original image -- because the user can choose to retain part of that border as a margin. The foreground pixels are in the center of the buffer, starting at Column 'foregroundLeft' and going to 'foregroundRight'. There is space to the left of that for the larger of the input left border and the output left border. Similarly, there is space to the right of the foreground pixels for the larger of the input right border and the output right border. We have to read an entire row, including the pixels we'll be leaving out of the output, so we pick a starting location in the buffer that lines up the first foreground pixel at 'foregroundLeft'. When we output the row, we pick a starting location in the buffer that includes the proper number of left border pixels before 'foregroundLeft'. That's for the middle rows. For the top and bottom, we just use the left portion of xelrow[], starting at 0. This is the general case. Enhancement for PBM appears below. (Logic works for PBM). */ unsigned int const foregroundCols = cols - crop.op[LEFT].removeSize - crop.op[RIGHT].removeSize; unsigned int const outputCols = foregroundCols + crop.op[LEFT].padSize + crop.op[RIGHT].padSize; unsigned int const foregroundRows = rows - crop.op[TOP].removeSize - crop.op[BOTTOM].removeSize; unsigned int const outputRows = foregroundRows + crop.op[TOP].padSize + crop.op[BOTTOM].padSize; unsigned int const foregroundLeft = MAX(crop.op[LEFT].removeSize, crop.op[LEFT].padSize); /* Index into xelrow[] of leftmost pixel of foreground */ unsigned int const foregroundRight = foregroundLeft + foregroundCols; /* Index into xelrow[] just past rightmost pixel of foreground */ unsigned int const allocCols = foregroundRight + MAX(crop.op[RIGHT].removeSize, crop.op[RIGHT].padSize); xel * xelrow; unsigned int i; pnm_writepnminit(ofP, outputCols, outputRows, maxval, format, 0); xelrow = pnm_allocrow(allocCols); readOffBorderNonPbm(crop.op[TOP].removeSize, ifP, cols, maxval, format); outputNewBorderNonPbm(crop.op[TOP].padSize, outputCols, backgroundColor, ofP, maxval, format); /* Set left border pixels */ fillRow(&xelrow[foregroundLeft - crop.op[LEFT].padSize], crop.op[LEFT].padSize, backgroundColor); /* Set right border pixels */ fillRow(&xelrow[foregroundRight], crop.op[RIGHT].padSize, backgroundColor); /* Read and output foreground rows */ for (i = 0; i < foregroundRows; ++i) { /* Read foreground pixels */ pnm_readpnmrow(ifP, &(xelrow[foregroundLeft - crop.op[LEFT].removeSize]), cols, maxval, format); pnm_writepnmrow(ofP, &(xelrow[foregroundLeft - crop.op[LEFT].padSize]), outputCols, maxval, format, 0); } readOffBorderNonPbm(crop.op[BOTTOM].removeSize, ifP, cols, maxval, format); outputNewBorderNonPbm(crop.op[BOTTOM].padSize, outputCols, backgroundColor, ofP, maxval, format); pnm_freerow(xelrow); }
static void concatenateTopBottomGen(FILE * const ofP, unsigned int const nfiles, int const newcols, int const newrows, xelval const newmaxval, int const newformat, enum justification const justification, struct imgInfo const img[], enum backcolor const backcolor) { xel * const newxelrow = pnm_allocrow(newcols); xel * inrow; unsigned int padleft; unsigned int i; unsigned int row, startRow; xel background, backgroundPrev; bool backChange; /* The background color is different from that of the previous input image. */ switch (backcolor) { case BACK_AUTO: /* do nothing now, determine at start of each image */ break; case BACK_BLACK: background = pnm_blackxel(newmaxval, newformat); break; case BACK_WHITE: background = pnm_whitexel(newmaxval, newformat); break; } for ( i = 0; i < nfiles; ++i, backgroundPrev = background) { if (img[i].cols == newcols) { /* no padding */ startRow = 0; backChange = FALSE; inrow = newxelrow; } else { /* Calculate left padding amount */ switch (justification) { case JUST_MIN: padleft = 0; break; case JUST_MAX: padleft = newcols - img[i].cols; break; case JUST_CENTER: padleft = (newcols - img[i].cols) / 2; break; } if (backcolor == BACK_AUTO) { /* Determine background color */ startRow = 1; inrow = &newxelrow[padleft]; pnm_readpnmrow(img[i].ifP, inrow, img[i].cols, img[i].maxval, img[i].format); pnm_promoteformatrow(inrow, img[i].cols, img[i].maxval, img[i].format, newmaxval, newformat); background = pnm_backgroundxelrow( inrow, img[i].cols, newmaxval, newformat); backChange = i==0 || !PNM_EQUAL(background, backgroundPrev); } else { /* background color is constant: black or white */ startRow = 0; inrow = &newxelrow[padleft]; backChange = (i==0); } if (backChange || (i > 0 && img[i-1].cols > img[i].cols)) { unsigned int col; for (col = 0; col < padleft; ++col) newxelrow[col] = background; for (col = padleft + img[i].cols; col < newcols; ++col) newxelrow[col] = background; } } if (startRow == 1) /* Top row already read for auto background color determination. Write it out. */ pnm_writepnmrow(ofP, newxelrow, newcols, newmaxval, newformat, 0); for (row = startRow; row < img[i].rows; ++row) { pnm_readpnmrow(img[i].ifP, inrow, img[i].cols, img[i].maxval, img[i].format); pnm_promoteformatrow( inrow, img[i].cols, img[i].maxval, img[i].format, newmaxval, newformat); pnm_writepnmrow(ofP, newxelrow, newcols, newmaxval, newformat, 0); } } pnm_freerow(newxelrow); }