static void getGenImgInfo(struct imgInfo const img[], unsigned int const nfiles, xel * const newxelrow, unsigned int const newrows, xelval const newmaxval, int const newformat, enum justification const justification, enum backcolor const backcolor, struct imgGen2 ** const img2P) { struct imgGen2 * img2; unsigned int i; MALLOCARRAY_NOFAIL(img2, nfiles); for (i = 0; i < nfiles; ++i) { switch (justification) { /* Determine top padding */ case JUST_MIN: img2[i].padtop = 0; break; case JUST_MAX: img2[i].padtop = newrows - img[i].rows; break; case JUST_CENTER: img2[i].padtop = (newrows - img[i].rows) / 2; break; } img2[i].inrow = (i == 0 ? &newxelrow[0] : img2[i-1].inrow + img[i-1].cols); if (img[i].rows == newrows) /* no padding */ img2[i].xelrow = NULL; else { /* Determine pad color */ switch (backcolor){ case BACK_AUTO: img2[i].xelrow = pnm_allocrow(img[i].cols); pnm_readpnmrow(img[i].ifP, img2[i].xelrow, img[i].cols, img[i].maxval, img[i].format); pnm_promoteformatrow(img2[i].xelrow, img[i].cols, img[i].maxval, img[i].format, newmaxval, newformat); img2[i].background = pnm_backgroundxelrow( img2[i].xelrow, img[i].cols, newmaxval, newformat); break; case BACK_BLACK: img2[i].xelrow = NULL; img2[i].background = pnm_blackxel(newmaxval, newformat); break; case BACK_WHITE: img2[i].xelrow = NULL; img2[i].background = pnm_whitexel(newmaxval, newformat); break; } } } *img2P = img2; }
static xel computeBackground(FILE * const ifP, int const cols, int const rows, xelval const maxval, int const format, enum bg_choice const backgroundChoice) { /*---------------------------------------------------------------------------- Determine what color is the background color of the image in file *ifP, which is described by 'cols', 'rows', 'maxval', and 'format'. 'backgroundChoice' is the method we are to use in determining the background color. Expect the file to be positioned to the start of the raster, and leave it positioned arbitrarily. -----------------------------------------------------------------------------*/ xel background; /* Our return value */ switch (backgroundChoice) { case BG_WHITE: background = pnm_whitexel(maxval, format); break; case BG_BLACK: background = pnm_blackxel(maxval, format); break; case BG_SIDES: background = background3Corners(ifP, rows, cols, maxval, format); break; case BG_DEFAULT: background = background2Corners(ifP, cols, maxval, format); break; } return(background); }
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 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); }