From 4c233fcfb54b386fcd96f71deb1e88e10d635825 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 27 Mar 2010 19:22:22 +0000 Subject: Release 10.50.00 - copied from trunk git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@1162 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- converter/other/Makefile | 4 +- converter/other/avstopam.c | 103 ++++++++++++++ converter/other/pamtoavs.c | 150 ++++++++++++++++++++ converter/other/pamtosvg/thin-image.c | 4 +- converter/other/pnmtosgi.c | 47 +++---- converter/other/ppmtopgm.c | 4 +- converter/pbm/pbmtogem.c | 252 +++++++++++++++++----------------- converter/pbm/pbmtoybm.c | 173 +++++++++++++---------- converter/pbm/ybmtopbm.c | 174 ++++++++++++----------- converter/pgm/pgmtolispm.c | 224 +++++++++++++++++------------- converter/ppm/Makefile | 2 +- converter/ppm/ppmtompeg/jpeg.c | 2 +- 12 files changed, 740 insertions(+), 399 deletions(-) create mode 100644 converter/other/avstopam.c create mode 100644 converter/other/pamtoavs.c (limited to 'converter') diff --git a/converter/other/Makefile b/converter/other/Makefile index 83676aaa..1417cd3a 100644 --- a/converter/other/Makefile +++ b/converter/other/Makefile @@ -77,9 +77,9 @@ ifeq ($(TIFFLIB_NEEDS_Z),Y) endif endif -PORTBINARIES = bmptopnm fitstopnm \ +PORTBINARIES = avstopam bmptopnm fitstopnm \ gemtopnm giftopnm hdifftopam infotopam \ - pamtodjvurle pamtofits pamtogif \ + pamtoavs pamtodjvurle pamtofits pamtogif \ pamtohdiff pamtohtmltbl pamtompfont pamtooctaveimg \ pamtopam pamtopfm pamtopnm pamtouil \ pamtoxvmini \ diff --git a/converter/other/avstopam.c b/converter/other/avstopam.c new file mode 100644 index 00000000..8d32008c --- /dev/null +++ b/converter/other/avstopam.c @@ -0,0 +1,103 @@ +/* ---------------------------------------------------------------------- + * + * Convert an AVS X image to a PAM image + * + * By Scott Pakin + * + * ---------------------------------------------------------------------- + * + * Copyright (C) 2010 Scott Pakin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + * ---------------------------------------------------------------------- + */ + +#include + +#include "pm.h" +#include "pam.h" + + + +static void +producePam(FILE * const avsFileP, + struct pam * const pamP) { + + tuple * tuplerow; + unsigned int row; + + tuplerow = pnm_allocpamrow(pamP); + for (row = 0; row < pamP->height; ++row) { + unsigned int col; + for (col = 0; col < pamP->width; ++col) { + tuple const thisTuple = tuplerow[col]; + char c; + pm_readchar(avsFileP, &c); thisTuple[3] = c; + pm_readchar(avsFileP, &c); thisTuple[0] = c; + pm_readchar(avsFileP, &c); thisTuple[1] = c; + pm_readchar(avsFileP, &c); thisTuple[2] = c; + } + pnm_writepamrow(pamP, tuplerow); + } + pnm_freepamrow(tuplerow); +} + + + +int +main(int argc, const char *argv[]) { + + const char * comment = "Produced by avstopam"; /* constant */ + + struct pam outPam; + const char * inputFilename; + FILE * inFileP; + long width; + long height; + + pm_proginit(&argc, argv); + + inputFilename = (argc > 1) ? argv[1] : "-"; + + inFileP = pm_openr(inputFilename); + + pm_readbiglong(inFileP, &width); + pm_readbiglong(inFileP, &height); + + outPam.size = sizeof(struct pam); + outPam.len = PAM_STRUCT_SIZE(comment_p); + outPam.file = stdout; + outPam.format = PAM_FORMAT; + outPam.plainformat = 0; + outPam.width = width; + outPam.height = height; + outPam.depth = 4; + outPam.maxval = 255; + outPam.bytes_per_sample = 1; + sprintf(outPam.tuple_type, "RGB_ALPHA"); + outPam.allocation_depth = 4; + outPam.comment_p = &comment; + + /* Produce a PAM output header. Note that AVS files *always* + contain four channels with one byte per channel. + */ + pnm_writepaminit(&outPam); + + producePam(inFileP, &outPam); + + pm_closer(inFileP); + + return 0; +} diff --git a/converter/other/pamtoavs.c b/converter/other/pamtoavs.c new file mode 100644 index 00000000..4764c9e8 --- /dev/null +++ b/converter/other/pamtoavs.c @@ -0,0 +1,150 @@ +/* ---------------------------------------------------------------------- + * + * Convert a PAM image to an AVS X image + * + * By Scott Pakin + * + * ---------------------------------------------------------------------- + * + * Copyright (C) 2010 Scott Pakin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + * ---------------------------------------------------------------------- + */ + +#include +#include "pm.h" +#include "pam.h" + + + +static char +sample2char(sample const s, + sample const maxval) { +/* Scale down a sample to a single byte. */ + + return maxval==255 ? s : s * 255 / maxval; +} + + +#define THIS_SAMPLE_CHAR(PLANE) \ + sample2char(tuplerow[col][PLANE], pamP->maxval) + +static void +produceAvs(struct pam * const pamP, + FILE * const avsFileP) { + + tuple * tuplerow; + + /* Write the AVS header (image width and height as 4-byte + big-endian integers). + */ + pm_writebiglong(avsFileP, pamP->width); + pm_writebiglong(avsFileP, pamP->height); + + /* Write the AVS data (alpha, red, green, blue -- one byte apiece. */ + tuplerow = pnm_allocpamrow(pamP); + switch (pamP->depth) { + case 1: { + /* Black-and-white or grayscale, no alpha */ + unsigned int row; + for (row = 0; row < pamP->height; ++row) { + unsigned int col; + pnm_readpamrow(pamP, tuplerow); + for (col = 0; col < pamP->width; ++col) { + pm_writechar(avsFileP, (char)255); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); + } + } + } break; + + case 2: { + /* Black-and-white or grayscale plus alpha */ + unsigned int row; + for (row = 0; row < pamP->height; ++row) { + unsigned int col; + pnm_readpamrow(pamP, tuplerow); + for (col = 0; col < pamP->width; ++col) { + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); + } + } + } break; + + case 3: { + /* RGB, no alpha */ + unsigned int row; + for (row = 0; row < pamP->height; ++row) { + unsigned int col; + pnm_readpamrow(pamP, tuplerow); + for (col = 0; col < pamP->width; ++col) { + pm_writechar(avsFileP, (char)255); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(2)); + } + } + } break; + + case 4: { + /* RGB plus alpha */ + unsigned int row; + for (row = 0; row < pamP->height; ++row) { + unsigned int col; + pnm_readpamrow( pamP, tuplerow ); + for (col = 0; col < pamP->width; ++col) { + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(3)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1)); + pm_writechar(avsFileP, THIS_SAMPLE_CHAR(2)); + } + } + } break; + + default: + pm_error("Unrecognized PAM depth %u. We understand only " + "1, 2, 3, and 4", pamP->depth); + break; + } + pnm_freepamrow(tuplerow); +} + + + +int +main(int argc, const char *argv[]) { + struct pam inPam; + const char * inputFilename; + FILE * inFileP; + + pm_proginit(&argc, argv); + + inputFilename = (argc > 1) ? argv[1] : "-"; + + inFileP = pm_openr(inputFilename); + + pnm_readpaminit(inFileP, &inPam, PAM_STRUCT_SIZE(tuple_type)); + + produceAvs(&inPam, stdout); + + pm_closer(inFileP); + + return 0; +} + diff --git a/converter/other/pamtosvg/thin-image.c b/converter/other/pamtosvg/thin-image.c index 40ced794..86d1037c 100644 --- a/converter/other/pamtosvg/thin-image.c +++ b/converter/other/pamtosvg/thin-image.c @@ -171,7 +171,7 @@ thin_image(bitmap_type *image, bool bgSpec, pixel bg, if (PPM_ISGRAY(background)) bg_color = PPM_GETR(background); else - bg_color = PPM_LUMIN(background); + bg_color = ppm_luminosity(background); for (n = num_pixels - 1; n >= 0L; --n) { @@ -306,7 +306,7 @@ void thin1(bitmap_type *image, unsigned char colour) if (PPM_ISGRAY(background)) bg_color = PPM_GETR(background); else - bg_color = PPM_LUMIN(background); + bg_color = ppm_luminosity(background); LOG (" Thinning image.....\n "); xsize = image->width; diff --git a/converter/other/pnmtosgi.c b/converter/other/pnmtosgi.c index 472b5197..169125b3 100644 --- a/converter/other/pnmtosgi.c +++ b/converter/other/pnmtosgi.c @@ -13,11 +13,16 @@ ** implied warranty. ** ** 29Jan94: first version + +** Feb 2010 afu +** Added dimension check to prevent short int from overflowing */ #include "pnm.h" #include "sgi.h" #include "mallocvar.h" + + /*#define DEBUG*/ typedef short ScanElem; @@ -42,6 +47,7 @@ static int rle_compress ARGS((ScanElem *inbuf, int cols)); #define MAXVAL_BYTE 255 #define MAXVAL_WORD 65535 +#define INT16MAX 32767 static char storage = STORAGE_RLE; static ScanLine * channel[3]; @@ -87,9 +93,7 @@ write_header(int const cols, int -main(argc, argv) - int argc; - char *argv[]; +main(int argc,char * argv[]) { FILE *ifp; int argn; @@ -131,6 +135,9 @@ main(argc, argv) pm_usage(usage); pnm_readpnminit(ifp, &cols, &rows, &maxval, &format); + if( rows>INT16MAX || cols>INT16MAX ) + pm_error ("Input image is too large."); + pnmrow = pnm_allocrow(cols); switch( PNM_FORMAT_TYPE(format) ) { @@ -172,9 +179,7 @@ main(argc, argv) static void -write_table(table, tabsize) - long *table; - int tabsize; +write_table(long * table, int const tabsize) { int i; long offset; @@ -194,9 +199,8 @@ write_table(table, tabsize) static void -write_channels(cols, rows, channels, put) - int cols, rows, channels; - void (*put) ARGS((short)); +write_channels(int const cols,int const rows, int const channels, + void (*put) (short)) { int i, row, col; @@ -214,7 +218,7 @@ write_channels(cols, rows, channels, put) } static void -put_big_short(short s) +put_big_short(short const s) { if ( pm_writebigshort( stdout, s ) == -1 ) pm_error( "write error" ); @@ -222,8 +226,7 @@ put_big_short(short s) static void -put_big_long(l) - long l; +put_big_long(long const l) { if ( pm_writebiglong( stdout, l ) == -1 ) pm_error( "write error" ); @@ -231,15 +234,16 @@ put_big_long(l) static void -put_short_as_byte(short s) +put_short_as_byte(short const s) { put_byte((unsigned char)s); } static long * -build_channels(FILE *ifp, int cols, int rows, xelval maxval, - int format, int bpc, int channels) +build_channels(FILE * const ifp, int const cols, int const rows, + xelval const maxval, int const format, + int const bpc, int const channels) { int i, row, col, sgirow; long *table = NULL; @@ -286,11 +290,10 @@ build_channels(FILE *ifp, int cols, int rows, xelval maxval, static ScanElem * -compress(temp, row, rows, cols, chan_no, table, bpc) - ScanElem *temp; - int row, rows, cols, chan_no; - long *table; - int bpc; +compress(ScanElem * temp, + int const row, int const rows, + int const cols, int const chan_no, + long * table, int const bpc) { int len, i, tabrow; ScanElem *p; @@ -323,9 +326,7 @@ slightly modified RLE algorithm from ppmtoilbm.c written by Robert A. Knop (rknop@mop.caltech.edu) */ static int -rle_compress(inbuf, size) - ScanElem *inbuf; - int size; +rle_compress(ScanElem * const inbuf, int const size) { int in, out, hold, count; ScanElem *outbuf = rletemp; diff --git a/converter/other/ppmtopgm.c b/converter/other/ppmtopgm.c index 86e7ae6a..e20c5660 100644 --- a/converter/other/ppmtopgm.c +++ b/converter/other/ppmtopgm.c @@ -36,9 +36,9 @@ convertRaster(FILE * const ifP, outputRow[col] = (gray) ppm_fastlumin(inputRow[col]); } else { /* Can't use fast approximation, so fall back on floats. */ - int col; + unsigned int col; for (col = 0; col < cols; ++col) - outputRow[col] = (gray) (PPM_LUMIN(inputRow[col]) + 0.5); + outputRow[col] = ppm_luminosity(inputRow[col]); } pgm_writepgmrow(ofP, outputRow, cols, maxval, 0); } diff --git a/converter/pbm/pbmtogem.c b/converter/pbm/pbmtogem.c index cefbdc95..9eab0416 100644 --- a/converter/pbm/pbmtogem.c +++ b/converter/pbm/pbmtogem.c @@ -27,17 +27,21 @@ * removed rounding of the imagewidth to the next word boundary * removed arbitrary limit to imagewidth * changed pattern length to 1 to simplify locating of compressable parts -* in real world images +* in real world images * add solid run and pattern run compression * * Deficiencies: * Compression of repeated scanlines not added * -* Johann Haider (jh@fortec.tuwien.ac.at) +* Johann Haider (jh@fortec.tuwien.ac.at) * * 94/01/31 Andreas Schwab (schwab@ls5.informatik.uni-dortmund.de) * Changed to remove architecture dependencies * Added compression of repeated scanlines +* +* Feb 2010 afu +* Added dimension check to prevent short int from overflowing +* Changed code style (ANSI-style function definitions, etc.) */ #include @@ -47,59 +51,11 @@ #define SOLID_0 0 #define SOLID_1 0xff #define MINRUN 4 +#define INT16MAX 32767 + #define putsolid(v,c) putc((v&0x80)|c, stdout) #define putpattern(v,c) putc(0, stdout);putc(c, stdout);putc(v, stdout) -static void putinit ARGS ((int rows, int cols)); -static void putbit ARGS(( bit b )); -static void putitem ARGS(( void )); -static void putrow ARGS(( void )); -static void flushrow ARGS ((void)); -static void putstring ARGS((register unsigned char *p, register int n)); - -int -main( argc, argv ) - int argc; - char* argv[]; - { - FILE* ifp; - bit* bitrow; - register bit* bP; - int rows, cols, format, row, col; - - pbm_init( &argc, argv ); - - if ( argc > 2 ) - pm_usage( "[pbmfile]" ); - - if ( argc == 2 ) - ifp = pm_openr( argv[1] ); - else - ifp = stdin; - - pbm_readpbminit( ifp, &cols, &rows, &format ); - - bitrow = pbm_allocrow( cols ); - - putinit (rows, cols); - for ( row = 0; row < rows; ++row ) - { -#ifdef DEBUG - fprintf (stderr, "row %d\n", row); -#endif - pbm_readpbmrow( ifp, bitrow, cols, format ); - for ( col = 0, bP = bitrow; col < cols; ++col, ++bP ) - putbit( *bP ); - putrow( ); - } - flushrow (); - - pm_close( ifp ); - - - exit( 0 ); - } - static short item; static int outcol, outmax; static short bitsperitem, bitshift; @@ -107,9 +63,9 @@ static short linerepeat; static unsigned char *outrow, *lastrow; static void -putinit (rows, cols) - int rows, cols; +putinit (int const rows, int const cols) { + if (pm_writebigshort (stdout, (short) 1) == -1 /* Image file version */ || pm_writebigshort (stdout, (short) 8) == -1 /* Header length */ || pm_writebigshort (stdout, (short) 1) == -1 /* Number of planes */ @@ -129,17 +85,6 @@ putinit (rows, cols) linerepeat = -1; } -static void -putbit( bit b ) - { - if ( bitsperitem == 8 ) - putitem( ); - ++bitsperitem; - if ( b == PBM_BLACK ) - item += 1 << bitshift; - --bitshift; - } - static void putitem( ) { @@ -149,19 +94,93 @@ putitem( ) bitshift = 7; } + +static void +putbit( bit const b ) + { + if ( bitsperitem == 8 ) + putitem( ); + ++bitsperitem; + if ( b == PBM_BLACK ) + item += 1 << bitshift; + --bitshift; + } + + static void -putstring (p, n) -register unsigned char *p; -register int n; +putstring ( unsigned char *p, int n) { #ifdef DEBUG fprintf (stderr, "Bitstring, length: %d, pos %d\n", n, outcol); #endif (void) putc((char) 0x80, stdout); /* a Bit string */ - (void) putc(n, stdout); /* count */ + (void) putc(n, stdout); /* count */ fwrite( p, n, 1, stdout ); } + +static void +flushrow( ) + { + unsigned char *outp, *p, *q; + int count; + int col = outmax; + + if (linerepeat > 1) + { + /* Put out line repeat count */ + fwrite ("\0\0\377", 3, 1, stdout); + putchar (linerepeat); + } + for (outp = p = lastrow; col > 0;) + { + for (q = p, count=0; (count < col) && (*q == *p); q++,count++); + if (count > MINRUN) + { + if (p > outp) + { + putstring (outp, p-outp); + outp = p; + } + col -= count; + switch (*p) + { + case SOLID_0: +#ifdef DEBUG +/* if (outcol > 0) */ + fprintf (stderr, "Solid run 0, length: %d\n", count); +#endif + putsolid (SOLID_0, count); + break; + + case SOLID_1: +#ifdef DEBUG + fprintf (stderr, "Solid run 1, length: %d, pos %d\n", count, outcol); +#endif + putsolid (SOLID_1, count); + break; + default: +#ifdef DEBUG + fprintf (stderr, "Pattern run, length: %d\n", count); +#endif + putpattern (*p, count); + break; + } + outp = p = q; + } + else + { + p++; + col--; + } + } + if (p > outp) + putstring (outp, p-outp); + if (ferror (stdout)) + pm_error ("write error"); +} + + static void putrow( ) { @@ -173,7 +192,7 @@ putrow( ) { unsigned char *temp; if (linerepeat != -1) /* Unless first line */ - flushrow (); + flushrow (); /* Swap the pointers */ temp = outrow; outrow = lastrow; lastrow = temp; linerepeat = 1; @@ -183,64 +202,47 @@ putrow( ) linerepeat++; } -static void -flushrow( ) - { - register unsigned char *outp, *p, *q; - register int count; - int col = outmax; - if (linerepeat > 1) - { - /* Put out line repeat count */ - fwrite ("\0\0\377", 3, 1, stdout); - putchar (linerepeat); - } - for (outp = p = lastrow; col > 0;) +int +main( int argc, char* argv[]) { - for (q = p, count=0; (count < col) && (*q == *p); q++,count++); - if (count > MINRUN) - { - if (p > outp) - { - putstring (outp, p-outp); - outp = p; - } - col -= count; - switch (*p) - { - case SOLID_0: -#ifdef DEBUG -/* if (outcol > 0) */ - fprintf (stderr, "Solid run 0, length: %d\n", count); -#endif - putsolid (SOLID_0, count); - break; + FILE* ifp; + bit* bitrow; + int rows, cols, format, row, col; - case SOLID_1: -#ifdef DEBUG - fprintf (stderr, "Solid run 1, length: %d, pos %d\n", count, outcol); -#endif - putsolid (SOLID_1, count); - break; - default: + pbm_init( &argc, argv ); + + if ( argc > 2 ) + pm_usage( "[pbmfile]" ); + + if ( argc == 2 ) + ifp = pm_openr( argv[1] ); + else + ifp = stdin; + + pbm_readpbminit( ifp, &cols, &rows, &format ); + + if( rows>INT16MAX || cols>INT16MAX ) + pm_error ("Input image is too large."); + + + bitrow = pbm_allocrow( cols ); + + putinit (rows, cols); + for ( row = 0; row < rows; ++row ) + { #ifdef DEBUG - fprintf (stderr, "Pattern run, length: %d\n", count); + fprintf (stderr, "row %d\n", row); #endif - putpattern (*p, count); - break; - } - outp = p = q; - } - else - { - p++; - col--; - } - } - if (p > outp) - putstring (outp, p-outp); - if (ferror (stdout)) - pm_error ("write error"); -} + pbm_readpbmrow( ifp, bitrow, cols, format ); + for ( col = 0; col < cols; ++col ) + putbit( bitrow[col] ); + putrow( ); + } + flushrow (); + + pm_close( ifp ); + + exit( 0 ); + } diff --git a/converter/pbm/pbmtoybm.c b/converter/pbm/pbmtoybm.c index 508e8e92..3fdd805d 100644 --- a/converter/pbm/pbmtoybm.c +++ b/converter/pbm/pbmtoybm.c @@ -9,100 +9,129 @@ ** copyright notice and this permission notice appear in supporting ** documentation. This software is provided "as is" without express or ** implied warranty. +** +** Feb 2010 afu +** Added dimension check to prevent short int from overflowing +** Changed code style (ANSI-style function definitions, etc.) */ #include + +#include "pm.h" #include "pbm.h" #define YBM_MAGIC ( ( '!' << 8 ) | '!' ) +#define INT16MAX 32767 -static void putinit ARGS(( int cols, int rows )); -static void putbit ARGS(( bit b )); -static void putrest ARGS(( void )); -static void putitem ARGS(( void )); +static long item; +static int bitsperitem, bitshift; -int -main( argc, argv ) - int argc; - char* argv[]; - { - FILE* ifp; - bit* bitrow; - register bit* bP; - int rows, cols, format, padright, row, col; - - - pbm_init( &argc, argv ); - - if ( argc > 2 ) - pm_usage( "[pbmfile]" ); - if ( argc == 2 ) - ifp = pm_openr( argv[1] ); - else - ifp = stdin; - - pbm_readpbminit( ifp, &cols, &rows, &format ); - bitrow = pbm_allocrow( cols ); - - /* Compute padding to round cols up to the nearest multiple of 16. */ - padright = ( ( cols + 15 ) / 16 ) * 16 - cols; - putinit( cols, rows ); - for ( row = 0; row < rows; ++row ) - { - pbm_readpbmrow( ifp, bitrow, cols, format ); - for ( col = 0, bP = bitrow; col < cols; ++col, ++bP ) - putbit( *bP ); - for ( col = 0; col < padright; ++col ) - putbit( 0 ); - } +static void +putitem(void) { - if ( ifp != stdin ) - fclose( ifp ); + pm_writebigshort(stdout, item); - putrest( ); + item = 0; + bitsperitem = 0; + bitshift = 0; +} - exit( 0 ); - } -static long item; -static int bitsperitem, bitshift; static void -putinit( cols, rows ) - int cols, rows; - { - pm_writebigshort( stdout, YBM_MAGIC ); - pm_writebigshort( stdout, cols ); - pm_writebigshort( stdout, rows ); - item = 0; +putinit(int const cols, + int const rows) { + + pm_writebigshort(stdout, YBM_MAGIC); + pm_writebigshort(stdout, cols); + pm_writebigshort(stdout, rows); + + item = 0; bitsperitem = 0; - bitshift = 0; - } + bitshift = 0; +} + + static void -putbit( bit b ) - { - if ( bitsperitem == 16 ) - putitem( ); +putbit(bit const b) { + + if (bitsperitem == 16) + putitem(); + ++bitsperitem; - if ( b == PBM_BLACK ) - item += 1 << bitshift; + + if (b == PBM_BLACK) + item += 1 << bitshift; + ++bitshift; - } +} + + static void -putrest( ) - { - if ( bitsperitem > 0 ) - putitem( ); +putrest(void) { + + if (bitsperitem > 0) + putitem(); +} + + + +int +main(int argc, const char *argv[]) { + + FILE * ifP; + bit * bitrow; + int rows; + int cols; + int format; + unsigned int padright; + unsigned int row; + 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"); } -static void -putitem( ) - { - pm_writebigshort( stdout, item ); - item = 0; - bitsperitem = 0; - bitshift = 0; + ifP = pm_openr(inputFile); + + pbm_readpbminit(ifP, &cols, &rows, &format); + + if (rows > INT16MAX || cols > INT16MAX) + pm_error("Input image is too large."); + + bitrow = pbm_allocrow(cols); + + /* Compute padding to round cols up to the nearest multiple of 16. */ + padright = ((cols + 15) / 16) * 16 - cols; + + putinit(cols, rows); + for (row = 0; row < rows; ++row) { + unsigned int col; + + pbm_readpbmrow(ifP, bitrow, cols, format); + + for (col = 0; col < cols; ++col) + putbit(bitrow[col]); + + for (col = 0; col < padright; ++col) + putbit(0); } + + if (ifP != stdin) + fclose(ifP); + + putrest(); + + return 0; +} diff --git a/converter/pbm/ybmtopbm.c b/converter/pbm/ybmtopbm.c index 739e168a..3d012483 100644 --- a/converter/pbm/ybmtopbm.c +++ b/converter/pbm/ybmtopbm.c @@ -10,104 +10,122 @@ ** implied warranty. */ -#include +#include "pm.h" #include "pbm.h" -static void getinit ARGS(( FILE* file, short* colsP, short* rowsP, short* depthP, short* padrightP )); -static bit getbit ARGS(( FILE* file )); +static short const ybmMagic = ( ( '!' << 8 ) | '!' ); -#define YBM_MAGIC ( ( '!' << 8 ) | '!' ) -int -main( argc, argv ) - int argc; - char* argv[]; - { - FILE* ifp; - bit* bitrow; - register bit* bP; - short rows, cols, padright, row, col; - short depth; - - pbm_init( &argc, argv ); - if ( argc > 2 ) - pm_usage( "[ybmfile]" ); - if ( argc == 2 ) - ifp = pm_openr( argv[1] ); - else - ifp = stdin; +static int item; +static int bitsInBuffer, bitshift; - getinit( ifp, &cols, &rows, &depth, &padright ); - if ( depth != 1 ) - pm_error( - "YBM file has depth of %d, must be 1", - (int) depth ); - pbm_writepbminit( stdout, cols, rows, 0 ); - bitrow = pbm_allocrow( cols ); - for ( row = 0; row < rows; ++row ) - { - /* Get data. */ - for ( col = 0, bP = bitrow; col < cols; ++col, ++bP ) - *bP = getbit( ifp ); - /* Discard line padding */ - for ( col = 0; col < padright; ++col ) - (void) getbit( ifp ); - pbm_writepbmrow( stdout, bitrow, cols, 0 ); - } +static void +getinit(FILE * const ifP, + short * const colsP, + short * const rowsP, + short * const depthP, + short * const padrightP) { - pm_close( ifp ); - pm_close( stdout ); + short magic; + int rc; - exit( 0 ); - } + rc = pm_readbigshort(ifP, &magic); + if (rc == -1) + pm_error("EOF / read error"); -static int item; -static int bitsperitem, bitshift; + if (magic != ybmMagic) + pm_error("bad magic number in YBM file"); -static void -getinit( file, colsP, rowsP, depthP, padrightP ) - FILE* file; - short* colsP; - short* rowsP; - short* depthP; - short* padrightP; - { - short magic; + rc = pm_readbigshort(ifP, colsP); + if (rc == -1 ) + pm_error("EOF / read error"); - if ( pm_readbigshort( file, &magic ) == -1 ) - pm_error( "EOF / read error" ); - if ( magic != YBM_MAGIC ) - pm_error( "bad magic number in YBM file" ); - if ( pm_readbigshort( file, colsP ) == -1 ) - pm_error( "EOF / read error" ); - if ( pm_readbigshort( file, rowsP ) == -1 ) - pm_error( "EOF / read error" ); + rc = pm_readbigshort(ifP, rowsP); + if (rc == -1) + pm_error("EOF / read error"); *depthP = 1; - *padrightP = ( ( *colsP + 15 ) / 16 ) * 16 - *colsP; - bitsperitem = 0; - } + *padrightP = ((*colsP + 15) / 16) * 16 - *colsP; +} + + static bit -getbit( file ) - FILE* file; - { +getbit(FILE * const ifP) { + bit b; - if ( bitsperitem == 0 ) - { - item = getc(file) | getc(file)<<8; - if ( item == EOF ) - pm_error( "EOF / read error" ); - bitsperitem = 16; - bitshift = 0; - } - b = ( ( item >> bitshift) & 1 ) ? PBM_BLACK : PBM_WHITE; - --bitsperitem; + if (bitsInBuffer == 0) { + item = (getc(ifP) << 8) | (getc(ifP) << 0); + + if (item == EOF) + pm_error("EOF / read error"); + + bitsInBuffer = 16; + bitshift = 0; + } + + b = ((item >> bitshift) & 1 ) ? PBM_BLACK : PBM_WHITE; + --bitsInBuffer; ++bitshift; return b; +} + + + +int +main(int argc, const char * argv[]) { + + FILE * ifP; + bit * bitrow; + short rows, cols, padright; + unsigned int row; + short depth; + 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); + + bitsInBuffer = 0; + + getinit(ifP, &cols, &rows, &depth, &padright); + if (depth != 1) + pm_error("YBM file has depth of %u, must be 1", (unsigned)depth); + + pbm_writepbminit(stdout, cols, rows, 0); + + bitrow = pbm_allocrow(cols); + + for (row = 0; row < rows; ++row) { + /* Get raster. */ + unsigned int col; + + for (col = 0; col < cols; ++col) + bitrow[col] = getbit(ifP); + + /* Discard line padding */ + for (col = 0; col < padright; ++col) + getbit(ifP); + pbm_writepbmrow(stdout, bitrow, cols, 0); } + + pm_close(ifP); + pm_close(stdout); + + return 0; +} diff --git a/converter/pgm/pgmtolispm.c b/converter/pgm/pgmtolispm.c index abb85494..7d931fb3 100644 --- a/converter/pgm/pgmtolispm.c +++ b/converter/pgm/pgmtolispm.c @@ -14,129 +14,167 @@ ** usually a color image; but a color map is not written in the file, so we ** treat this as a graymap instead. To convert a color image to Lispm ** format, you must convert it to a pgm, and hand-edit a color map... Ick. +** +** Feb 2010 afu +** Added dimension check to prevent short int from overflowing +** Changed code style (ANSI-style function definitions, etc.) */ -#include +#include "pm.h" #include "pgm.h" #define LISPM_MAGIC "This is a BitMap file" +#define INT16MAX 32767 -static void putinit ARGS(( int cols, int rows, int depth )); -static int depth_to_word_size ARGS(( int depth )); -static void putval ARGS(( gray b )); -static void putrest ARGS(( void )); -static void putitem ARGS(( void )); -int -main( argc, argv ) - int argc; - char* argv[]; - { - FILE* ifp; - gray *grayrow; - register gray* gP; - int rows, cols, depth, format, padright, row, col; - gray maxval; +static unsigned int item; +static unsigned int bitsperitem, maxbitsperitem, bitshift; +static unsigned int +depth_to_word_size(unsigned int const depth) { - pgm_init( &argc, argv ); + /* Lispm architecture specific - if a bitmap is written */ + /* out with a depth of 5, it really has a depth of 8, and */ + /* is stored that way in the file. */ - if ( argc > 2 ) - pm_usage( "[pgmfile]" ); - if ( argc == 2 ) - ifp = pm_openr( argv[1] ); - else - ifp = stdin; + unsigned int const wordSize = + depth == 1 ? 1 : + depth == 2 ? 2 : + depth <= 4 ? 4 : + depth <= 8 ? 8 : + depth <= 16 ? 16 : + depth <= 32 ? 32 : + 0; - pgm_readpgminit( ifp, &cols, &rows, &maxval, &format ); - grayrow = pgm_allocrow( cols ); - depth = pm_maxvaltobits( maxval ); + if (wordSize == 0) + pm_error("depth was %u, which is not in the range 1-32", depth); - /* Compute padding to round cols up to the nearest multiple of 32. */ - padright = ( ( cols + 31 ) / 32 ) * 32 - cols; + return wordSize; +} - putinit( cols, rows, depth ); - for ( row = 0; row < rows; ++row ) - { - pgm_readpgmrow( ifp, grayrow, cols, maxval, format ); - for ( col = 0, gP = grayrow; col < cols; ++col, ++gP ) - putval( *gP ); - for ( col = 0; col < padright; ++col ) - putval( 0 ); - } - pm_close( ifp ); - putrest( ); +static void +putinit(unsigned int const cols, + unsigned int const rows, + unsigned int const depth) { - exit( 0 ); - } + unsigned int const cols32 = ((cols + 31 ) / 32) * 32; -static unsigned int item; -static unsigned int bitsperitem, maxbitsperitem, bitshift; + unsigned int i; -static void -putinit( cols, rows, depth ) - int cols, rows, depth; - { - int i; - int cols32 = ( ( cols + 31 ) / 32 ) * 32; /* Lispms are able to write bit files that are not mod32 wide, but we */ - /* don't. This should be ok, since bit arrays which are not mod32 wide */ - printf(LISPM_MAGIC); /* are pretty useless on a lispm (can't hand them to bitblt). */ - pm_writelittleshort( stdout, cols ); - pm_writelittleshort( stdout, rows ); - pm_writelittleshort( stdout, cols32 ); + /* Lispms are able to write bit files that are not mod32 wide, but we */ + /* don't. This should be ok, since bit arrays which are not mod32 wide */ + /* are pretty useless on a lispm (can't hand them to bitblt). */ + + if (rows > INT16MAX || cols > INT16MAX || cols32 > INT16MAX) + pm_error("Input image is too large."); + + printf(LISPM_MAGIC); + + pm_writelittleshort(stdout, cols); + pm_writelittleshort(stdout, rows); + pm_writelittleshort(stdout, cols32); putchar(depth & 0xFF); - for ( i = 0; i < 9; ++i ) - putchar( 0 ); /* pad bytes */ + for (i = 0; i < 9; ++i) + putchar(0); /* pad bytes */ - item = 0; - bitsperitem = 0; - maxbitsperitem = depth_to_word_size( depth ); - bitshift = 0; - } + item = 0; + bitsperitem = 0; + maxbitsperitem = depth_to_word_size(depth); + bitshift = 0; +} -static int -depth_to_word_size (depth) /* Lispm architecture specific - if a bitmap is written */ - int depth; /* out with a depth of 5, it really has a depth of 8, and */ -{ /* is stored that way in the file. */ - if (depth==0 || depth==1) return ( 1); - else if (depth == 2) return ( 2); - else if (depth <= 4) return ( 4); - else if (depth <= 8) return ( 8); - else if (depth <= 16) return (16); - else if (depth <= 32) return (32); - else { - pm_error( "depth was %d, which is not in the range 1-32", depth ); - return(-1); /* Should never reach here */ - } + + +static void +putitem(void) { + + pm_writelittlelong(stdout, ~item); + + item = 0; + bitsperitem = 0; + bitshift = 0; } static void -putval( gray b ) - { - if ( bitsperitem == 32 ) - putitem( ); - item = item | ( b << bitshift ); +putval(gray const b) { + + if (bitsperitem == 32) + putitem(); + + item = item | (b << bitshift); bitsperitem = bitsperitem + maxbitsperitem; - bitshift = bitshift + maxbitsperitem; - } + bitshift = bitshift + maxbitsperitem; +} + + static void -putrest( ) - { - if ( bitsperitem > 0 ) - putitem( ); +putrest(void) { + + if (bitsperitem > 0) + putitem(); +} + + + +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"); } -static void -putitem( ) - { - pm_writelittlelong( stdout, ~item ); - item = 0; - bitsperitem = 0; - bitshift = 0; + 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; +} diff --git a/converter/ppm/Makefile b/converter/ppm/Makefile index adc3a400..1bade11a 100644 --- a/converter/ppm/Makefile +++ b/converter/ppm/Makefile @@ -20,7 +20,7 @@ PORTBINARIES = 411toppm eyuvtoppm gouldtoppm ilbmtoppm imgtoppm \ ppmtowinicon ppmtoxpm ppmtoyuv ppmtoyuvsplit \ qrttoppm rawtoppm rgb3toppm spctoppm \ sputoppm tgatoppm winicontoppm ximtoppm xpmtoppm xvminitoppm \ - yuvtoppm yuvsplittoppm + yuvsplittoppm yuvtoppm MATHBINARIES = sldtoppm diff --git a/converter/ppm/ppmtompeg/jpeg.c b/converter/ppm/ppmtompeg/jpeg.c index a703cf39..2567666d 100644 --- a/converter/ppm/ppmtompeg/jpeg.c +++ b/converter/ppm/ppmtompeg/jpeg.c @@ -360,7 +360,7 @@ JMovie2JPEG(const char * const infilename, * * allocate and initialize JPEG decompression object * specify data source (eg, a file) - * jpeg_read_header(); // obtain image dimensions and other parameters + * jpeg_read_header(); obtain image dimensions and other parameters * set parameters for decompression * jpeg_start_decompress(); * while (scan lines remain to be read) -- cgit 1.4.1