diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2006-08-19 03:12:28 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2006-08-19 03:12:28 +0000 |
commit | 1fd361a1ea06e44286c213ca1f814f49306fdc43 (patch) | |
tree | 64c8c96cf54d8718847339a403e5e67b922e8c3f /converter/ppm/ppmtopuzz.c | |
download | netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.gz netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.xz netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.zip |
Create Subversion repository
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/ppm/ppmtopuzz.c')
-rw-r--r-- | converter/ppm/ppmtopuzz.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/converter/ppm/ppmtopuzz.c b/converter/ppm/ppmtopuzz.c new file mode 100644 index 00000000..1277cc20 --- /dev/null +++ b/converter/ppm/ppmtopuzz.c @@ -0,0 +1,96 @@ +/* ppmtopuzz.c - read a portable pixmap and write an X11 "puzzle" file +** +** Copyright (C) 1991 by Jef Poskanzer. +** +** Permission to use, copy, modify, and distribute this software and its +** documentation for any purpose and without fee is hereby granted, provided +** that the above copyright notice appear in all copies and that both that +** copyright notice and this permission notice appear in supporting +** documentation. This software is provided "as is" without express or +** implied warranty. +*/ + +#include "ppm.h" + +#define MAXVAL 255 +#define MAXCOLORS 256 + +int +main( argc, argv ) + int argc; + char* argv[]; + { + FILE* ifp; + pixel** pixels; + register pixel* pP; + colorhist_vector chv; + colorhash_table cht; + int rows, cols, row, colors, i; + register int col; + pixval maxval; + + + ppm_init( &argc, argv ); + + if ( argc > 2 ) + pm_usage( "[ppmfile]" ); + + if ( argc == 2 ) + ifp = pm_openr( argv[1] ); + else + ifp = stdin; + + pixels = ppm_readppm( ifp, &cols, &rows, &maxval ); + pm_close( ifp ); + + pm_message( "computing colormap..." ); + chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colors ); + if ( chv == (colorhist_vector) 0 ) + { + pm_message( + "too many colors - try doing a 'pnmquant %d'", MAXCOLORS ); + exit( 1 ); + } + pm_message( "%d colors found", colors ); + + /* Write puzzle header. */ + (void) pm_writebiglong( stdout, cols ); + (void) pm_writebiglong( stdout, rows ); + (void) putchar( (unsigned char) colors ); + if ( maxval > MAXVAL ) + pm_message( + "maxval is not %d - automatically rescaling colors", MAXVAL ); + for ( i = 0; i < colors; ++i ) + { + pixel p; + + p = chv[i].color; + if ( maxval != MAXVAL ) + PPM_DEPTH( p, p, maxval, MAXVAL ); + (void) putchar( (unsigned char) PPM_GETR( p ) ); + (void) putchar( (unsigned char) PPM_GETG( p ) ); + (void) putchar( (unsigned char) PPM_GETB( p ) ); + } + + /* Convert color vector to color hash table, for fast lookup. */ + cht = ppm_colorhisttocolorhash( chv, colors ); + ppm_freecolorhist( chv ); + + /* And write out the data. */ + for ( row = 0; row < rows; ++row ) + { + for ( col = 0, pP = pixels[row]; col < cols; ++col, ++pP ) + { + register int color; + + color = ppm_lookupcolor( cht, pP ); + if ( color == -1 ) + pm_error( + "color not found?!? row=%d col=%d r=%d g=%d b=%d", + row, col, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP) ); + (void) putchar( (unsigned char) color ); + } + } + + exit( 0 ); + } |