diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2008-12-28 19:13:20 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2008-12-28 19:13:20 +0000 |
commit | 86f29c0eb54d03d4a5c8703cf8386ee5cb7a95c5 (patch) | |
tree | 3b30fd23aa36e1c8d7b55bf6663ceeaf5c6b5c8f /converter/other/pnmtoddif.c | |
parent | a2f8752d2bd1379f7bbe422027c041925802f029 (diff) | |
download | netpbm-mirror-86f29c0eb54d03d4a5c8703cf8386ee5cb7a95c5.tar.gz netpbm-mirror-86f29c0eb54d03d4a5c8703cf8386ee5cb7a95c5.tar.xz netpbm-mirror-86f29c0eb54d03d4a5c8703cf8386ee5cb7a95c5.zip |
Release 10.45.0 - rebase to current trunk
git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@801 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/other/pnmtoddif.c')
-rw-r--r-- | converter/other/pnmtoddif.c | 264 |
1 files changed, 168 insertions, 96 deletions
diff --git a/converter/other/pnmtoddif.c b/converter/other/pnmtoddif.c index b80fba4f..ae8c8524 100644 --- a/converter/other/pnmtoddif.c +++ b/converter/other/pnmtoddif.c @@ -1,14 +1,4 @@ /* - * $Log: pnmtoddif.c,v $ - * Revision 1.6 1993/01/25 08:14:06 neideck - * Placed into public use form. - * - * Revision 1.5 1992/12/02 08:15:18 neideck - * Added RCS id. - * - */ - -/* * Author: Burkhard Neidecker-Lutz * Digital CEC Karlsruhe * neideck@nestvx.enet.dec.com @@ -31,6 +21,8 @@ */ #include <string.h> + +#include "mallocvar.h" #include "pnm.h" /* The structure we use to convey all sorts of "magic" data to the DDIF */ @@ -414,21 +406,181 @@ write_trailer(FILE * file) -int main(int argc, char *argv[]) -{ + +static void +convertPbmRaster(FILE * const ifP, + int const format, + unsigned int const cols, + unsigned int const rows, + FILE * const ofP, + unsigned int const bytesPerLine, + unsigned char * const data) { + + bit * const pixels = pbm_allocrow(cols); + + unsigned int row; + + for (row = 0; row < rows; ++row) { + unsigned int col; + unsigned int k; + unsigned int mask; + unsigned char * p; + size_t bytesWritten; + + pbm_readpbmrow(ifP, pixels, cols, format); + + mask = 0x00; + p = &data[0]; + for (col = 0, k = 0; col < cols; ++col) { + if (pixels[col] == PBM_BLACK) + mask |= 1 << k; + if (k == 7) { + *p++ = mask; + mask = 0x00; + k = 0; + } else + ++k; + } + if (k != 7) + /* Flush the rest of the column */ + *p = mask; + + bytesWritten = fwrite(data, 1, bytesPerLine, ofP); + if (bytesWritten != bytesPerLine) + pm_error("File write error on Row %u", row); + } + + pbm_freerow(pixels); +} + + + +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 +convertPpmRaster(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) { + + pixel * const pixels = ppm_allocrow(cols); + + unsigned int row; + + for (row = 0; row < rows; ++row) { + unsigned char * p; + unsigned int col; + size_t bytesWritten; + + p = &data[0]; + + ppm_readppmrow(ifP, pixels, cols, maxval, format); + + for (col = 0; col < cols; ++col) { + *p++ = PPM_GETR(pixels[col]); + *p++ = PPM_GETG(pixels[col]); + *p++ = PPM_GETB(pixels[col]); + } + bytesWritten = fwrite(data, 1, bytesPerLine, ofP); + if (bytesWritten != bytesPerLine) + pm_error("File write error on Row %u", row); + } + ppm_freerow(pixels); +} + + + +static void +convertRaster(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 * data; + unsigned char * p; + + MALLOCARRAY(data, bytesPerLine); + + if (data == NULL) + pm_error("Couldn't allocate %u-byte line buffer", bytesPerLine); + + p = data; /* initial value */ + + switch (PNM_FORMAT_TYPE(format)) { + case PBM_TYPE: + convertPbmRaster(ifP, format, cols, rows, ofP, bytesPerLine, data); + break; + case PGM_TYPE: + convertPgmRaster(ifP, format, maxval, cols, rows, ofP, bytesPerLine, + data); + break; + case PPM_TYPE: + convertPpmRaster(ifP, format, maxval, cols, rows, ofP, bytesPerLine, + data); + break; + default: + pm_error("INTERNAL ERROR: impossible format value"); + } + + free(data); +} + + + +int +main(int argc, char *argv[]) { FILE *ifd; - FILE *ofd; + 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); @@ -512,90 +664,10 @@ int main(int argc, char *argv[]) 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; - } + convertRaster(ifd, format, maxval, cols, rows, ofd, ip.bytes_per_line); pm_close(ifd); - free(data); - if (!write_trailer(ofd)) { perror("Writing trailer"); exit(1); |