home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / ppmtopuzz.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  3KB  |  98 lines

  1. /* ppmtopuzz.c - read a portable pixmap and write an X11 "puzzle" file
  2. **
  3. ** Copyright (C) 1991 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14. #include "ppmcmap.h"
  15.  
  16. #define MAXVAL 255
  17. #define MAXCOLORS 256
  18.  
  19. int
  20. main( argc, argv )
  21.     int argc;
  22.     char* argv[];
  23.     {
  24.     FILE* ifp;
  25.     pixel** pixels;
  26.     register pixel* pP;
  27.     colorhist_vector chv;
  28.     colorhash_table cht;
  29.     int rows, cols, row, colors, i;
  30.     register int col;
  31.     pixval maxval;
  32.  
  33.  
  34.     ppm_init( &argc, argv );
  35.  
  36.     if ( argc > 2 )
  37.     pm_usage( "[ppmfile]" );
  38.  
  39.     if ( argc == 2 )
  40.     ifp = pm_openr( argv[1] );
  41.     else
  42.     ifp = stdin;
  43.  
  44.     pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
  45.     pm_close( ifp );
  46.  
  47.     pm_message( "computing colormap..." );
  48.     chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colors );
  49.     if ( chv == (colorhist_vector) 0 )
  50.     {
  51.     pm_message(
  52.         "too many colors - try doing a 'ppmquant %d'", MAXCOLORS );
  53.     exit( 1 );
  54.     }
  55.     pm_message( "%d colors found", colors );
  56.  
  57.     /* Write puzzle header. */
  58.     (void) pm_writebiglong( stdout, cols );
  59.     (void) pm_writebiglong( stdout, rows );
  60.     (void) putchar( (unsigned char) colors );
  61.     if ( maxval > MAXVAL )
  62.     pm_message(
  63.         "maxval is not %d - automatically rescaling colors", MAXVAL );
  64.     for ( i = 0; i < colors; ++i )
  65.     {
  66.     pixel p;
  67.  
  68.     p = chv[i].color;
  69.     if ( maxval != MAXVAL )
  70.         PPM_DEPTH( p, p, maxval, MAXVAL );
  71.     (void) putchar( (unsigned char) PPM_GETR( p ) );
  72.     (void) putchar( (unsigned char) PPM_GETG( p ) );
  73.     (void) putchar( (unsigned char) PPM_GETB( p ) );
  74.     }
  75.  
  76.     /* Convert color vector to color hash table, for fast lookup. */
  77.     cht = ppm_colorhisttocolorhash( chv, colors );
  78.     ppm_freecolorhist( chv );
  79.  
  80.     /* And write out the data. */
  81.     for ( row = 0; row < rows; ++row )
  82.     {
  83.     for ( col = 0, pP = pixels[row]; col < cols; ++col, ++pP )
  84.         {
  85.         register int color;
  86.  
  87.         color = ppm_lookupcolor( cht, pP );
  88.         if ( color == -1 )
  89.         pm_error(
  90.             "color not found?!?  row=%d col=%d  r=%d g=%d b=%d",
  91.             row, col, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP) );
  92.         (void) putchar( (unsigned char) color );
  93.         }
  94.     }
  95.  
  96.     exit( 0 );
  97.     }
  98.