void PGMImageWriter::beginOfImage(int cols, int rows) { _cols = cols; _outputRow = pgm_allocrow(cols); BOOST_VERIFY(_outputRow != NULL); pgm_writepgminit(_output, cols, rows, 255, 1); }
static void convertPgmRaster(FILE * const ifP, int const format, xelval const maxval, unsigned int const cols, unsigned int const rows, FILE * const ofP, unsigned int const bytesPerLine, unsigned char * const data) { gray * const pixels = pgm_allocrow(cols); unsigned int row; for (row = 0; row < rows; ++row) { unsigned char * p; unsigned int col; size_t bytesWritten; p = &data[0]; pgm_readpgmrow(ifP, pixels, cols, maxval, format); for (col = 0; col < cols; ++col) *p++ = (unsigned char) pixels[col]; bytesWritten = fwrite(data, 1, bytesPerLine, ofP); if (bytesWritten != bytesPerLine) pm_error("File write error on Row %u", row); } pgm_freerow(pixels); }
static void writepgmrow(FILE * const fileP, const xel * const xelrow, unsigned int const cols, xelval const maxval, int const format, bool const plainFormat) { jmp_buf jmpbuf; jmp_buf * origJmpbufP; gray * grayrow; grayrow = pgm_allocrow(cols); if (setjmp(jmpbuf) != 0) { pgm_freerow(grayrow); pm_setjmpbuf(origJmpbufP); pm_longjmp(); } else { unsigned int col; pm_setjmpbufsave(&jmpbuf, &origJmpbufP); for (col = 0; col < cols; ++col) grayrow[col] = PNM_GET1(xelrow[col]); pgm_writepgmrow(fileP, grayrow, cols, (gray) maxval, plainFormat); pm_setjmpbuf(origJmpbufP); } pgm_freerow(grayrow); }
int main(int argc, const char * argv[]) { FILE * ifP; gray * grayrow; int rows; int cols; unsigned int depth; int format; unsigned int padright; unsigned int row; gray maxval; const char * inputFile; pm_proginit(&argc, argv); if (argc-1 < 1) inputFile = "-"; else { inputFile = argv[1]; if (argc-1 > 2) pm_error("Too many arguments. The only argument is the optional " "input file name"); } ifP = pm_openr(inputFile); pgm_readpgminit(ifP, &cols, &rows, &maxval, &format); grayrow = pgm_allocrow(cols); depth = pm_maxvaltobits(maxval); /* Compute padding to round cols up to the nearest multiple of 32. */ padright = ((cols + 31) / 32) * 32 - cols; putinit(cols, rows, depth); for (row = 0; row < rows; ++row) { unsigned int col; pgm_readpgmrow(ifP, grayrow, cols, maxval, format); for (col = 0; col < cols; ++col) putval(grayrow[col]); for (col = 0; col < padright; ++col) putval(0); } pm_close(ifP); putrest(); return 0; }
static void writeRaster(FILE * const ifP, struct SbigHeader const hdr, FILE * const ofP) { gray * grayrow; unsigned int row; grayrow = pgm_allocrow(hdr.cols); for (row = 0; row < hdr.rows; ++row) { bool compthis; unsigned int col; if (hdr.isCompressed) { unsigned short rowlen; /* Compressed row length */ pm_readlittleshortu(ifP, &rowlen); /* If compression results in a row length >= the uncompressed row length, that row is output uncompressed. We detect this by observing that the compressed row length is equal to that of an uncompressed row. */ if (rowlen == hdr.cols * 2) compthis = false; else compthis = hdr.isCompressed; } else compthis = hdr.isCompressed; for (col = 0; col < hdr.cols; ++col) { unsigned short g; if (compthis) { if (col == 0) { pm_readlittleshortu(ifP, &g); } else { int const delta = getc(ifP); if (delta == 0x80) pm_readlittleshortu(ifP, &g); else g += ((signed char) delta); } } else pm_readlittleshortu(ifP, &g); grayrow[col] = g; } pgm_writepgmrow(ofP, grayrow, hdr.cols, hdr.maxval, 0); } pgm_freerow(grayrow); }
void pnm_writepnmrow(FILE * const fileP, xel * const xelrow, int const cols, xelval const maxval, int const format, int const forceplain) { bool const plainFormat = forceplain || pm_plain_output; switch (PNM_FORMAT_TYPE(format)) { case PPM_TYPE: ppm_writeppmrow(fileP, (pixel*) xelrow, cols, (pixval) maxval, plainFormat); break; case PGM_TYPE: { gray* grayrow; unsigned int col; grayrow = pgm_allocrow(cols); for (col = 0; col < cols; ++col) grayrow[col] = PNM_GET1(xelrow[col]); pgm_writepgmrow(fileP, grayrow, cols, (gray) maxval, plainFormat); pgm_freerow( grayrow ); } break; case PBM_TYPE: { bit* bitrow; unsigned int col; bitrow = pbm_allocrow(cols); for (col = 0; col < cols; ++col) bitrow[col] = PNM_GET1(xelrow[col]) == 0 ? PBM_BLACK : PBM_WHITE; pbm_writepbmrow(fileP, bitrow, cols, plainFormat); pbm_freerow(bitrow); } break; default: pm_error("invalid format argument received by pnm_writepnmrow(): %d" "PNM_FORMAT_TYPE(format) must be %d, %d, or %d", format, PBM_TYPE, PGM_TYPE, PPM_TYPE); } }
int main(int argc, char *argv[]) { FILE* ifP; const char * inputFilespec; int eof; ppm_init( &argc, argv ); if (argc-1 > 1) pm_error("The only argument is the (optional) input filename"); if (argc == 2) inputFilespec = argv[1]; else inputFilespec = "-"; ifP = pm_openr(inputFilespec); eof = FALSE; /* initial assumption */ while (!eof) { ppm_nextimage(ifP, &eof); if (!eof) { int rows, cols, format; pixval maxval; pixel* inputRow; gray* outputRow; ppm_readppminit(ifP, &cols, &rows, &maxval, &format); pgm_writepgminit(stdout, cols, rows, maxval, 0); inputRow = ppm_allocrow(cols); outputRow = pgm_allocrow(cols); convertRaster(ifP, cols, rows, maxval, format, inputRow, outputRow, stdout); ppm_freerow(inputRow); pgm_freerow(outputRow); } } pm_close(ifP); pm_close(stdout); return 0; }
static void getMapping(const char * const rmapFileName, const unsigned int * const lumahist, xelval const maxval, unsigned int const pixelCount, gray ** const lumamapP) { /*---------------------------------------------------------------------------- Calculate the luminosity mapping table which gives the histogram-equalized luminosity for each original luminosity. -----------------------------------------------------------------------------*/ gray * lumamap; lumamap = pgm_allocrow(maxval+1); if (rmapFileName) readMapFile(rmapFileName, maxval, lumamap); else computeMap(lumahist, maxval, pixelCount, lumamap); *lumamapP = lumamap; }
int main(int argc, char * argv[]) { FILE * ifP; struct cmdlineInfo cmdline; gray * grayrow; pixel * pixelrow; int rows, cols, format; gray maxval; ppm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFilename); pgm_readpgminit(ifP, &cols, &rows, &maxval, &format); grayrow = pgm_allocrow(cols); pixelrow = ppm_allocrow(cols); if (cmdline.map) convertWithMap(ifP, cols, rows, maxval, format, cmdline.map, stdout, grayrow, pixelrow); else convertLinear(ifP, cols, rows, maxval, format, cmdline.colorBlack, cmdline.colorWhite, stdout, grayrow, pixelrow); ppm_freerow(pixelrow); pgm_freerow(grayrow); pm_close(ifP); /* If the program failed, it previously aborted with nonzero completion code, via various function calls. */ return 0; }
int pnm_image_pgmWrite(FILE *fp, pnm_image_t *image) { int i,j; gray *row; if (!image || !fp) return (-1); row = pgm_allocrow(image->cols); for (i=0; i < image->channels; i++) { pgm_writepgminit(fp, image->cols, image->rows, image->maxval, 1 /* plain PGM format file, no raw */); for (j=0; j < image->rows; j++) { int k; for (k=0; k < image->cols; k++) row[k] = (gray) image->pixels[i][j][k]; pgm_writepgmrow(fp, row, image->cols, image->maxval, 1 /* plain PGM format file, no raw */); } } pgm_freerow(row); return (0); }
int main (int argc, char *argv[]) { int offset; int cols, rows, row; pixel* pixelrow; pixval maxval; FILE* Lifp; pixel* Lpixelrow; gray* Lgrayrow; int Lrows, Lcols, Lformat; pixval Lmaxval; FILE* Rifp; pixel* Rpixelrow; gray* Rgrayrow; int Rrows, Rcols, Rformat; pixval Rmaxval; ppm_init (&argc, argv); if (argc-1 > 3 || argc-1 < 2) pm_error("Wrong number of arguments (%d). Arguments are " "leftppmfile rightppmfile [horizontal_offset]", argc-1); Lifp = pm_openr (argv[1]); Rifp = pm_openr (argv[2]); if (argc-1 >= 3) offset = atoi (argv[3]); else offset = 30; ppm_readppminit (Lifp, &Lcols, &Lrows, &Lmaxval, &Lformat); ppm_readppminit (Rifp, &Rcols, &Rrows, &Rmaxval, &Rformat); if ((Lcols != Rcols) || (Lrows != Rrows) || (Lmaxval != Rmaxval) || (PPM_FORMAT_TYPE(Lformat) != PPM_FORMAT_TYPE(Rformat))) pm_error ("Pictures are not of same size and format"); cols = Lcols; rows = Lrows; maxval = Lmaxval; ppm_writeppminit (stdout, cols, rows, maxval, 0); Lpixelrow = ppm_allocrow (cols); Lgrayrow = pgm_allocrow (cols); Rpixelrow = ppm_allocrow (cols); Rgrayrow = pgm_allocrow (cols); pixelrow = ppm_allocrow (cols); for (row = 0; row < rows; ++row) { ppm_readppmrow(Lifp, Lpixelrow, cols, maxval, Lformat); ppm_readppmrow(Rifp, Rpixelrow, cols, maxval, Rformat); computeGrayscaleRow(Lpixelrow, Lgrayrow, maxval, cols); computeGrayscaleRow(Rpixelrow, Rgrayrow, maxval, cols); { int col; gray* LgP; gray* RgP; pixel* pP; for (col = 0, pP = pixelrow, LgP = Lgrayrow, RgP = Rgrayrow; col < cols + offset; ++col) { if (col < offset/2) ++LgP; else if (col >= offset/2 && col < offset) { const pixval Blue = (pixval) (float) *LgP; const pixval Red = (pixval) 0; PPM_ASSIGN (*pP, Red, Blue, Blue); ++LgP; ++pP; } else if (col >= offset && col < cols) { const pixval Red = (pixval) (float) *RgP; const pixval Blue = (pixval) (float) *LgP; PPM_ASSIGN (*pP, Red, Blue, Blue); ++LgP; ++RgP; ++pP; } else if (col >= cols && col < cols + offset/2) { const pixval Blue = (pixval) 0; const pixval Red = (pixval) (float) *RgP; PPM_ASSIGN (*pP, Red, Blue, Blue); ++RgP; ++pP; } else ++RgP; } } ppm_writeppmrow(stdout, pixelrow, cols, maxval, 0); } pm_close(Lifp); pm_close(Rifp); pm_close(stdout); return 0; }
int main(int argc, char * argv[]) { struct cmdlineInfo cmdline; FILE * ifP; int cols, rows; int median; enum medianMethod medianMethod; pgm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFileName); ccolso2 = cmdline.width / 2; crowso2 = cmdline.height / 2; pgm_readpgminit(ifP, &cols, &rows, &maxval, &format); pgm_writepgminit(stdout, cols, rows, maxval, forceplain); /* Allocate space for number of rows in mask size. */ grays = pgm_allocarray(cols, cmdline.height); grayrow = pgm_allocrow(cols); /* Read in and write out initial rows that won't get changed. */ for (row = 0; row < cmdline.height - 1; ++row) { pgm_readpgmrow(ifP, grays[row], cols, maxval, format); /* Write out the unchanged row. */ if (row < crowso2) pgm_writepgmrow(stdout, grays[row], cols, maxval, forceplain); } median = (cmdline.height * cmdline.width) / 2; /* Choose which sort to run. */ if (cmdline.type == MEDIAN_UNSPECIFIED) { if ((maxval / ((cmdline.width * cmdline.height) - 1)) < cmdline.cutoff) medianMethod = HISTOGRAM_SORT_MEDIAN; else medianMethod = SELECT_MEDIAN; } else medianMethod = cmdline.type; switch (medianMethod) { case SELECT_MEDIAN: selectMedian(ifP, cmdline.width, cmdline.height, cols, rows, median); break; case HISTOGRAM_SORT_MEDIAN: histogramSortMedian(ifP, cmdline.width, cmdline.height, cols, rows, median); break; case MEDIAN_UNSPECIFIED: pm_error("INTERNAL ERROR: median unspecified"); } pm_close(ifP); pm_close(stdout); pgm_freearray(grays, cmdline.height); pgm_freerow(grayrow); return 0; }
static void histogramSortMedian(FILE * const ifP, unsigned int const ccols, unsigned int const crows, unsigned int const cols, unsigned int const rows, unsigned int const median) { unsigned int const histmax = maxval + 1; unsigned int * hist; unsigned int mdn, ltmdn; gray * leftCol; gray * rghtCol; gray ** rowptr; MALLOCARRAY(rowptr, crows); MALLOCARRAY(hist, histmax); if (rowptr == NULL || hist == NULL) pm_error("Unable to allocate memory"); leftCol = pgm_allocrow(crows); rghtCol = pgm_allocrow(crows); /* Apply median to main part of image. */ for ( ; row < rows; ++row) { unsigned int col; unsigned int temprow; unsigned int rownum; unsigned int irow; unsigned int i; /* initialize hist[] */ for (i = 0; i < histmax; ++i) hist[i] = 0; pgm_readpgmrow(ifP, grays[row % crows], cols, maxval, format); /* Rotate pointers to rows, so rows can be accessed in order. */ temprow = (row + 1) % crows; rownum = 0; for (irow = temprow; irow < crows; ++rownum, ++irow) rowptr[rownum] = grays[irow]; for (irow = 0; irow < temprow; ++rownum, ++irow) rowptr[rownum] = grays[irow]; for (col = 0; col < cols; ++col) { if (col < ccolso2 || col >= cols - ccolso2) grayrow[col] = rowptr[crowso2][col]; else if (col == ccolso2) { unsigned int crow; unsigned int const leftcol = col - ccolso2; i = 0; for (crow = 0; crow < crows; ++crow) { unsigned int ccol; gray * const temprptr = rowptr[crow] + leftcol; for (ccol = 0; ccol < ccols; ++ccol) { gray const g = *(temprptr + ccol); ++hist[g]; ++i; } } ltmdn = 0; for (mdn = 0; ltmdn <= median; ++mdn) ltmdn += hist[mdn]; --mdn; if (ltmdn > median) ltmdn -= hist[mdn]; grayrow[col] = mdn; } else { unsigned int crow; unsigned int const subcol = col - (ccolso2 + 1); unsigned int const addcol = col + ccolso2; for (crow = 0; crow < crows; ++crow) { leftCol[crow] = *(rowptr[crow] + subcol); rghtCol[crow] = *(rowptr[crow] + addcol); } for (crow = 0; crow < crows; ++crow) { { gray const g = leftCol[crow]; --hist[(unsigned int) g]; if ((unsigned int) g < mdn) --ltmdn; } { gray const g = rghtCol[crow]; ++hist[(unsigned int) g]; if ((unsigned int) g < mdn) ++ltmdn; } } if (ltmdn > median) do { --mdn; ltmdn -= hist[mdn]; } while (ltmdn > median); else { /* This one change from Pitas algorithm can reduce run ** time by up to 10%. */ while (ltmdn <= median) { ltmdn += hist[mdn]; ++mdn; } --mdn; if (ltmdn > median) ltmdn -= hist[mdn]; } grayrow[col] = mdn; } } pgm_writepgmrow(stdout, grayrow, cols, maxval, forceplain); } { /* Write out remaining unchanged rows. */ unsigned int irow; for (irow = crowso2 + 1; irow < crows; ++irow) pgm_writepgmrow(stdout, rowptr[irow], cols, maxval, forceplain); } pgm_freerow(leftCol); pgm_freerow(rghtCol); free(hist); free(rowptr); }
static void selectMedian(FILE * const ifP, unsigned int const ccols, unsigned int const crows, unsigned int const cols, unsigned int const rows, unsigned int const median) { unsigned int const numValues = crows * ccols; unsigned int col; gray * garray; /* Array of the currenty gray values */ int * parray; int * subcol; gray ** rowptr; garray = pgm_allocrow(numValues); MALLOCARRAY(rowptr, crows); MALLOCARRAY(parray, numValues); MALLOCARRAY(subcol, cols); if (rowptr == NULL || parray == NULL || subcol == NULL) pm_error("Unable to allocate memory"); for (col = 0; col < cols; ++col) subcol[col] = (col - (ccolso2 + 1)) % ccols; /* Apply median to main part of image. */ for ( ; row < rows; ++row) { int crow; int rownum, irow, temprow; unsigned int col; pgm_readpgmrow(ifP, grays[row % crows], cols, maxval, format); /* Rotate pointers to rows, so rows can be accessed in order. */ temprow = (row + 1) % crows; rownum = 0; for (irow = temprow; irow < crows; ++rownum, ++irow) rowptr[rownum] = grays[irow]; for (irow = 0; irow < temprow; ++rownum, ++irow) rowptr[rownum] = grays[irow]; for (col = 0; col < cols; ++col) { if (col < ccolso2 || col >= cols - ccolso2) { grayrow[col] = rowptr[crowso2][col]; } else if (col == ccolso2) { unsigned int const leftcol = col - ccolso2; unsigned int i; i = 0; for (crow = 0; crow < crows; ++crow) { gray * const temprptr = rowptr[crow] + leftcol; unsigned int ccol; for (ccol = 0; ccol < ccols; ++ccol) { garray[i] = *(temprptr + ccol); parray[i] = i; ++i; } } select489(garray, parray, numValues, median); grayrow[col] = garray[parray[median]]; } else { unsigned int const addcol = col + ccolso2; unsigned int crow; unsigned int tsum; for (crow = 0, tsum = 0; crow < crows; ++crow, tsum += ccols) garray[tsum + subcol[col]] = *(rowptr[crow] + addcol ); select489( garray, parray, numValues, median ); grayrow[col] = garray[parray[median]]; } } pgm_writepgmrow( stdout, grayrow, cols, maxval, forceplain ); } { unsigned int irow; /* Write out remaining unchanged rows. */ for (irow = crowso2 + 1; irow < crows; ++irow) pgm_writepgmrow(stdout, rowptr[irow], cols, maxval, forceplain); } free(subcol); free(parray); free(rowptr); pgm_freerow(garray); }
static void histogram_sort_median(FILE * const ifp, int const ccols, int const crows, int const cols, int const rows, int const median) { int const histmax = maxval + 1; int *hist; int mdn, ltmdn; gray *left_col, *right_col; hist = (int *) pm_allocrow( histmax, sizeof( int ) ); left_col = pgm_allocrow( crows ); right_col = pgm_allocrow( crows ); /* Apply median to main part of image. */ for ( ; row < rows; ++row ) { int col; int temprow; int rownum; int irow; int i; /* initialize hist[] */ for ( i = 0; i < histmax; ++i ) hist[i] = 0; temprow = row % crows; pgm_readpgmrow( ifp, grays[temprow], cols, maxval, format ); /* Rotate pointers to rows, so rows can be accessed in order. */ temprow = ( row + 1 ) % crows; rownum = 0; for ( irow = temprow; irow < crows; ++rownum, ++irow ) rowptr[rownum] = grays[irow]; for ( irow = 0; irow < temprow; ++rownum, ++irow ) rowptr[rownum] = grays[irow]; for ( col = 0; col < cols; ++col ) { if ( col < ccolso2 || col >= cols - ccolso2 ) grayrow[col] = rowptr[crowso2][col]; else if ( col == ccolso2 ) { int crow; int const leftcol = col - ccolso2; i = 0; for ( crow = 0; crow < crows; ++crow ) { int ccol; gray * const temprptr = rowptr[crow] + leftcol; for ( ccol = 0; ccol < ccols; ++ccol ) { gray const g = *( temprptr + ccol ); ++hist[g]; ++i; } } ltmdn = 0; for ( mdn = 0; ltmdn <= median; ++mdn ) ltmdn += hist[mdn]; mdn--; if ( ltmdn > median ) ltmdn -= hist[mdn]; grayrow[col] = mdn; } else { int crow; int const subcol = col - ( ccolso2 + 1 ); int const addcol = col + ccolso2; for ( crow = 0; crow < crows; ++crow ) { left_col[crow] = *( rowptr[crow] + subcol ); right_col[crow] = *( rowptr[crow] + addcol ); } for ( crow = 0; crow < crows; ++crow ) { { gray const g = left_col[crow]; hist[(int) g]--; if ( (int) g < mdn ) ltmdn--; } { gray const g = right_col[crow]; hist[(int) g]++; if ( (int) g < mdn ) ltmdn++; } } if ( ltmdn > median ) do { mdn--; ltmdn -= hist[mdn]; } while ( ltmdn > median ); else { /* This one change from Pitas algorithm can reduce run ** time by up to 10%. */ while ( ltmdn <= median ) { ltmdn += hist[mdn]; mdn++; } mdn--; if ( ltmdn > median ) ltmdn -= hist[mdn]; } grayrow[col] = mdn; } } pgm_writepgmrow( stdout, grayrow, cols, maxval, forceplain ); } { /* Write out remaining unchanged rows. */ int irow; for ( irow = crowso2 + 1; irow < crows; ++irow ) pgm_writepgmrow( stdout, rowptr[irow], cols, maxval, forceplain ); } pm_freerow( (char *) hist ); pgm_freerow( left_col ); pgm_freerow( right_col ); }
static void select_median(FILE * const ifp, int const ccols, int const crows, int const cols, int const rows, int const median) { int ccol, col; int crow; int rownum, irow, temprow; gray *temprptr; int i, leftcol; int num_values; gray *garray; int *parray; int addcol; int *subcol; int tsum; /* Allocate storage for array of the current gray values. */ garray = pgm_allocrow( crows * ccols ); num_values = crows * ccols; parray = (int *) pm_allocrow( crows * ccols, sizeof(int) ); subcol = (int *) pm_allocrow( cols, sizeof(int) ); for ( i = 0; i < cols; ++i ) subcol[i] = ( i - (ccolso2 + 1) ) % ccols; /* Apply median to main part of image. */ for ( ; row < rows; ++row ) { temprow = row % crows; pgm_readpgmrow( ifp, grays[temprow], cols, maxval, format ); /* Rotate pointers to rows, so rows can be accessed in order. */ temprow = ( row + 1 ) % crows; rownum = 0; for ( irow = temprow; irow < crows; ++rownum, ++irow ) rowptr[rownum] = grays[irow]; for ( irow = 0; irow < temprow; ++rownum, ++irow ) rowptr[rownum] = grays[irow]; for ( col = 0; col < cols; ++col ) { if ( col < ccolso2 || col >= cols - ccolso2 ) { grayrow[col] = rowptr[crowso2][col]; } else if ( col == ccolso2 ) { leftcol = col - ccolso2; i = 0; for ( crow = 0; crow < crows; ++crow ) { temprptr = rowptr[crow] + leftcol; for ( ccol = 0; ccol < ccols; ++ccol ) { garray[i] = *( temprptr + ccol ); parray[i] = i; ++i; } } select_489( garray, parray, num_values, median ); grayrow[col] = garray[parray[median]]; } else { addcol = col + ccolso2; for (crow = 0, tsum = 0; crow < crows; ++crow, tsum += ccols) garray[tsum + subcol[col]] = *(rowptr[crow] + addcol ); select_489( garray, parray, num_values, median ); grayrow[col] = garray[parray[median]]; } } pgm_writepgmrow( stdout, grayrow, cols, maxval, forceplain ); } /* Write out remaining unchanged rows. */ for ( irow = crowso2 + 1; irow < crows; ++irow ) pgm_writepgmrow( stdout, rowptr[irow], cols, maxval, forceplain ); pgm_freerow( garray ); pm_freerow( (char *) parray ); pm_freerow( (char *) subcol ); }
int main(int argc, char *argv[]) { gray *outrow, maxval; int right, left, down, up; bit **inbits; int rows, cols; FILE *ifd; int row; int width, height; const char * const usage = "<w> <h> [pbmfile]"; pgm_init( &argc, argv ); if (argc > 4 || argc < 3) pm_usage(usage); width = atoi(argv[1]); height = atoi(argv[2]); if (width < 1 || height < 1) pm_error("width and height must be > 0"); left=width/2; right=width-left; up=width/2; down=height-up; if (argc == 4) ifd = pm_openr(argv[3]); else ifd = stdin ; inbits = pbm_readpbm(ifd, &cols, &rows) ; if (width > cols) pm_error("You specified a sample width (%u columns) which is greater " "than the image width (%u columns)", height, rows); if (height > rows) pm_error("You specified a sample height (%u rows) which is greater " "than the image height (%u rows)", height, rows); outrow = pgm_allocrow(cols) ; maxval = MIN(PGM_OVERALLMAXVAL, width*height); pgm_writepgminit(stdout, cols, rows, maxval, 0) ; for (row = 0; row < rows; row++) { int const t = (row > up) ? (row-up) : 0; int const b = (row+down < rows) ? (row+down) : rows; int const onv = height - (t-row+up) - (row+down-b); unsigned int col; for (col = 0; col < cols; col++) { int const l = (col > left) ? (col-left) : 0; int const r = (col+right < cols) ? (col+right) : cols; int const onh = width - (l-col+left) - (col+right-r); int value; int x; value = 0; /* initial value */ for (x = l; x < r; ++x) { int y; for (y = t; y < b; ++y) if (inbits[y][x] == PBM_WHITE) ++value; } outrow[col] = maxval*value/(onh*onv); } pgm_writepgmrow(stdout, outrow, cols, maxval, 0) ; } pm_close(ifd); return 0; }
int main(int argc, char *argv[]) { FILE *ifd; FILE *ofd; int rows, cols; xelval maxval; int format; const char * const usage = "[-resolution x y] [pnmfile [ddiffile]]"; int i, j; char *outfile; int argn; int hor_resolution = 75; int ver_resolution = 75; imageparams ip; unsigned char *data, *p; pnm_init(&argc, argv); for (argn = 1;argn < argc && argv[argn][0] == '-';argn++) { int arglen = strlen(argv[argn]); if (!strncmp (argv[argn],"-resolution", arglen)) { if (argn + 2 < argc) { hor_resolution = atoi(argv[argn+1]); ver_resolution = atoi(argv[argn+2]); argn += 2; continue; } else { pm_usage(usage); } } else { pm_usage(usage); } } if (hor_resolution <= 0 || ver_resolution <= 0) { fprintf(stderr,"Unreasonable resolution values: %d x %d\n", hor_resolution,ver_resolution); exit(1); } if (argn == argc - 2) { ifd = pm_openr(argv[argn]); outfile = argv[argn+1]; if (!(ofd = fopen(outfile,"wb"))) { perror(outfile); exit(1); } } else if (argn == argc - 1) { ifd = pm_openr(argv[argn]); ofd = stdout; } else { ifd = stdin; ofd = stdout; } pnm_readpnminit(ifd, &cols, &rows, &maxval, &format); ip.width = cols; ip.height = rows; ip.h_res = hor_resolution; ip.v_res = ver_resolution; switch (PNM_FORMAT_TYPE(format)) { case PBM_TYPE: ip.bits_per_pixel = 1; ip.bytes_per_line = (cols + 7) / 8; ip.spectral = 2; ip.components = 1; ip.bits_per_component = 1; ip.polarity = 1; break; case PGM_TYPE: ip.bytes_per_line = cols; ip.bits_per_pixel = 8; ip.spectral = 2; ip.components = 1; ip.bits_per_component = 8; ip.polarity = 2; break; case PPM_TYPE: ip.bytes_per_line = 3 * cols; ip.bits_per_pixel = 24; ip.spectral = 5; ip.components = 3; ip.bits_per_component = 8; ip.polarity = 2; break; default: fprintf(stderr, "Unrecognized PBMPLUS format %d\n", format); exit(1); } if (!write_header(ofd,&ip)) { perror("Writing header"); exit(1); } if (!(p = data = (unsigned char*) malloc(ip.bytes_per_line))) { perror("allocating line buffer"); exit(1); } switch (PNM_FORMAT_TYPE(format)) { case PBM_TYPE: { bit *pixels; int mask; int k; pixels = pbm_allocrow(cols); for (i = 0; i < rows; i++) { pbm_readpbmrow(ifd, pixels, cols, format); mask = 0; p = data; for (j = 0, k = 0; j < cols; j++) { if (pixels[j] == PBM_BLACK) { mask |= 1 << k; } if (k == 7) { *p++ = mask; mask = 0; k = 0; } else { k++; } } if (k != 7) { /* Flush the rest of the column */ *p = mask; } if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) { perror("Writing image data\n"); exit(1); } } } break; case PGM_TYPE: { gray *pixels = pgm_allocrow(cols); for (i = 0; i < rows; i++) { p = data; pgm_readpgmrow(ifd, pixels, cols, maxval, format); for (j = 0; j < cols; j++) { *p++ = (unsigned char) pixels[j]; } if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) { perror("Writing image data\n"); exit(1); } } pgm_freerow(pixels); } break; case PPM_TYPE: { pixel *pixels = ppm_allocrow(cols); for (i = 0; i < rows; i++) { p = data; ppm_readppmrow(ifd, pixels, cols, maxval, format); for (j = 0; j < cols; j++) { *p++ = PPM_GETR(pixels[j]); *p++ = PPM_GETG(pixels[j]); *p++ = PPM_GETB(pixels[j]); } if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) { perror("Writing image data\n"); exit(1); } } ppm_freerow(pixels); } break; } pm_close(ifd); free(data); if (!write_trailer(ofd)) { perror("Writing trailer"); exit(1); } if (fclose(ofd) == EOF) { perror("Closing output file"); exit(1); }; return(0); }
COSTTYPE * readPGM(const char *fname, int *width, int *height, bool raw) { pm_init("navfn_tests",0); FILE *pgmfile; pgmfile = fopen(fname,"r"); if (!pgmfile) { printf("readPGM() Can't find file %s\n", fname); return NULL; } printf("readPGM() Reading costmap file %s\n", fname); int ncols, nrows; gray maxval; int format; pgm_readpgminit(pgmfile, &ncols, &nrows, &maxval, &format); printf("readPGM() Size: %d x %d\n", ncols, nrows); // set up cost map COSTTYPE *cmap = (COSTTYPE *)malloc(ncols*nrows*sizeof(COSTTYPE)); if (!raw) for (int i=0; i<ncols*nrows; i++) cmap[i] = COST_NEUTRAL; gray * row(pgm_allocrow(ncols)); int otot = 0; int utot = 0; int ftot = 0; for (int ii = 0; ii < nrows; ii++) { pgm_readpgmrow(pgmfile, row, ncols, maxval, format); if (raw) // raw costmap from ROS { for (int jj(ncols - 1); jj >= 0; --jj) { int v = row[jj]; cmap[ii*ncols+jj] = v; if (v >= COST_OBS_ROS) otot++; if (v == 0) ftot++; } } else { ftot = ncols*nrows; for (int jj(ncols - 1); jj >= 0; --jj) { if (row[jj] < unknown_gray && ii < nrows-7 && ii > 7) { setcostobs(cmap,ii*ncols+jj,ncols); otot++; ftot--; } else if (row[jj] <= unknown_gray) { setcostunk(cmap,ii*ncols+jj,ncols); utot++; ftot--; } } } } printf("readPGM() Found %d obstacle cells, %d free cells, %d unknown cells\n", otot, ftot, utot); pgm_freerow(row); *width = ncols; *height = nrows; return cmap; }
static void pgmHist(FILE * const ifP, int const cols, int const rows, xelval const maxval, int const format, bool const dots, bool const no_white, bool const no_black, bool const verbose, xelval const startval, xelval const endval, unsigned int const histWidth, unsigned int const histHeight, bool const clipSpec, unsigned int const clipCount) { gray * grayrow; bit ** bits; int i, j; unsigned int * ghist; double vscale; unsigned int hmax; MALLOCARRAY(ghist, histWidth); if (ghist == NULL) pm_error("Not enough memory for histogram array (%u bytes)", histWidth * (unsigned)sizeof(int)); bits = pbm_allocarray(histWidth, histHeight); if (bits == NULL) pm_error("no space for output array (%u bits)", histWidth * histHeight); memset(ghist, 0, histWidth * sizeof(ghist[0])); /* read the pixel values into the histogram arrays */ grayrow = pgm_allocrow(cols); if (verbose) pm_message("making histogram..."); for (i = rows; i > 0; --i) { pgm_readpgmrow (ifP, grayrow, cols, maxval, format); for (j = cols-1; j >= 0; --j) countComp(grayrow[j], startval, endval, histWidth, ghist); } pgm_freerow(grayrow); /* find the highest-valued slot and set the vertical scale value */ if (verbose) pm_message("finding max. slot height..."); if (clipSpec) hmax = clipCount; else hmax = maxSlotCount(ghist, histWidth, no_white, no_black); assert(hmax > 0); if (verbose) pm_message("Done: height = %u", hmax); clipHistogram(ghist, histWidth, hmax); vscale = (double) histHeight / hmax; for (i = 0; i < histWidth; ++i) { int mark = histHeight - (int)(vscale * ghist[i]); for (j = 0; j < mark; ++j) bits[j][i] = PBM_BLACK; if (j < histHeight) bits[j++][i] = PBM_WHITE; for ( ; j < histHeight; ++j) bits[j][i] = dots ? PBM_BLACK : PBM_WHITE; } pbm_writepbm(stdout, bits, histWidth, histHeight, 0); }
int main( int argc, char** argv ){ FILE *ifp; gray maxval; int cols, rows, format; gray* prevrow; gray* thisrow; gray* tmprow; int* countTile; int* countEdgeX; int* countEdgeY; int* countVertex; int i, col, row; int maxtiles, maxedgex, maxedgey, maxvertex; int area, perimeter, eulerchi; double l2inv, linv; /* * parse arg and initialize */ pgm_init( &argc, argv ); if ( argc > 2 ) pm_usage( "[pgmfile]" ); if ( argc == 2 ) ifp = pm_openr( argv[1] ); else ifp = stdin; /* * initialize */ pgm_readpgminit( ifp, &cols, &rows, &maxval, &format ); prevrow = pgm_allocrow( cols ); thisrow = pgm_allocrow( cols ); MALLOCARRAY(countTile , maxval + 1 ); MALLOCARRAY(countEdgeX , maxval + 1 ); MALLOCARRAY(countEdgeY , maxval + 1 ); MALLOCARRAY(countVertex , maxval + 1 ); if (countTile == NULL || countEdgeX == NULL || countEdgeY == NULL || countVertex == NULL) pm_error( "out of memory" ); for ( i = 0; i <= maxval; i++ ) countTile[i] = 0; for ( i = 0; i <= maxval; i++ ) countEdgeX[i] = 0; for ( i = 0; i <= maxval; i++ ) countEdgeY[i] = 0; for ( i = 0; i <= maxval; i++ ) countVertex[i] = 0; /* first row */ pgm_readpgmrow( ifp, thisrow, cols, maxval, format ); /* tiles */ for ( col = 0; col < cols; ++col ) ++countTile[thisrow[col]]; /* y-edges */ for ( col = 0; col < cols; ++col ) ++countEdgeY[thisrow[col]]; /* x-edges */ ++countEdgeX[thisrow[0]]; for ( col = 0; col < cols-1; ++col ) ++countEdgeX[ MAX2(thisrow[col], thisrow[col+1]) ]; ++countEdgeX[thisrow[cols-1]]; /* shortcut: for the first row, countVertex == countEdgeX */ ++countVertex[thisrow[0]]; for ( col = 0; col < cols-1; ++col ) ++countVertex[ MAX2(thisrow[col], thisrow[col+1]) ]; ++countVertex[thisrow[cols-1]]; for ( row = 1; row < rows; ++row ){ tmprow = prevrow; prevrow = thisrow; thisrow = tmprow; pgm_readpgmrow( ifp, thisrow, cols, maxval, format ); /* tiles */ for ( col = 0; col < cols; ++col ) ++countTile[thisrow[col]]; /* y-edges */ for ( col = 0; col < cols; ++col ) ++countEdgeY[ MAX2(thisrow[col], prevrow[col]) ]; /* x-edges */ ++countEdgeX[thisrow[0]]; for ( col = 0; col < cols-1; ++col ) ++countEdgeX[ MAX2(thisrow[col], thisrow[col+1]) ]; ++countEdgeX[thisrow[cols-1]]; /* vertices */ ++countVertex[ MAX2(thisrow[0],prevrow[0]) ]; for ( col = 0; col < cols-1; ++col ) ++countVertex[ MAX4(thisrow[col], thisrow[col+1], prevrow[col], prevrow[col+1]) ]; ++countVertex[ MAX2(thisrow[cols-1],prevrow[cols-1]) ]; } /* for row */ /* now thisrow contains the top row*/ /* tiles and x-edges have been counted, now upper y-edges and top vertices remain */ /* y-edges */ for ( col = 0; col < cols; ++col ) ++countEdgeY[ thisrow[col] ]; /* vertices */ ++countVertex[thisrow[0]]; for ( col = 0; col < cols-1; ++col ) ++countVertex[ MAX2(thisrow[col],thisrow[col+1]) ]; ++countVertex[ thisrow[cols-1] ]; /* cleanup */ maxtiles = rows * cols; maxedgex = rows * (cols+1); maxedgey = (rows+1) * cols; maxvertex= (rows+1) * (cols+1); l2inv = 1.0/maxtiles; linv = 0.5/(rows+cols); /* And print it. */ printf( "#threshold\t tiles\tx-edges\ty-edges\tvertices\n" ); printf( "#---------\t -----\t-------\t-------\t--------\n" ); for ( i = 0; i <= maxval; i++ ){ if( !(countTile[i] || countEdgeX[i] || countEdgeY[i] || countVertex[i] ) ) continue; /* skip empty slots */ area = maxtiles; perimeter = 2*maxedgex + 2*maxedgey - 4*maxtiles; eulerchi = maxtiles - maxedgex - maxedgey + maxvertex; printf( "%f\t%6d\t%7d\t%7d\t%8d\t%g\t%g\t%6d\n", (float) i/(1.0*maxval), maxtiles, maxedgex, maxedgey, maxvertex, area*l2inv, perimeter*linv, eulerchi ); maxtiles -= countTile[i]; maxedgex -= countEdgeX[i]; maxedgey -= countEdgeY[i]; maxvertex-= countVertex[i]; /* i, countTile[i], countEdgeX[i], countEdgeY[i], countVertex[i] */ } /* these should be zero: */ printf( "# check:\t%6d\t%7d\t%7d\t%8d\n", maxtiles, maxedgex, maxedgey, maxvertex ); pm_close( ifp ); exit( 0 ); } /*main*/