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

  1. /* ppmtopi1.c - read a portable pixmap and write a Degas PI1 file
  2. **
  3. ** Copyright (C) 1991 by Steve Belczyk and 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 COLS 320
  17. #define ROWS 200
  18. #define MAXVAL 7
  19. #define MAXCOLORS 16
  20.  
  21. static short screen[ROWS*COLS/4];  /* simulate the ST's video RAM */
  22.  
  23. int
  24. main( argc, argv )
  25.     int argc;
  26.     char* argv[];
  27.     {
  28.     FILE* ifp;
  29.     pixel** pixels;
  30.     register pixel *pP;
  31.     colorhist_vector chv;
  32.     colorhash_table cht;
  33.     int rows, cols, row, colors, i;
  34.     register int col;
  35.     pixval maxval;
  36.  
  37.  
  38.     ppm_init( &argc, argv );
  39.  
  40.     if ( argc > 2 )
  41.         pm_usage( "[ppmfile]" );
  42.  
  43.     if ( argc == 2 )
  44.         ifp = pm_openr( argv[1] );
  45.     else
  46.         ifp = stdin;
  47.  
  48.     pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
  49.     pm_close( ifp );
  50.     if ( (cols > COLS) || (rows > ROWS) )
  51.         pm_error( "image is larger than %dx%d - sorry", COLS, ROWS );
  52.  
  53.     pm_message( "computing colormap..." );
  54.     chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colors );
  55.     if ( chv == (colorhist_vector) 0 )
  56.         {
  57.         pm_message(
  58.             "too many colors - try doing a 'ppmquant %d'", MAXCOLORS );
  59.         exit( 1 );
  60.         }
  61.     pm_message( "%d colors found", colors );
  62.  
  63.     /* Write PI1 header - resolution and palette. */
  64.     (void) pm_writebigshort( stdout, (short) 0 );       /* low resolution */
  65.     for ( i = 0; i < 16; ++i )
  66.         {
  67.         short w;
  68.  
  69.         if ( i < colors )
  70.             {
  71.             pixel p;
  72.  
  73.             p = chv[i].color;
  74.             if ( maxval != MAXVAL )
  75.                 PPM_DEPTH( p, p, maxval, MAXVAL );
  76.             w  = ( (int) PPM_GETR( p ) ) << 8;
  77.             w |= ( (int) PPM_GETG( p ) ) << 4;
  78.             w |= ( (int) PPM_GETB( p ) );
  79.             }
  80.         else
  81.             w = 0;
  82.         (void) pm_writebigshort( stdout, w );
  83.         }
  84.     if ( maxval > MAXVAL )
  85.         pm_message(
  86.             "maxval is not %d - automatically rescaling colors", MAXVAL );
  87.  
  88.     /* Convert color vector to color hash table, for fast lookup. */
  89.     cht = ppm_colorhisttocolorhash( chv, colors );
  90.     ppm_freecolorhist( chv );
  91.  
  92.     /* Clear the screen buffer. */
  93.     for ( i = 0; i < ROWS*COLS/4; ++i )
  94.         screen[i] = 0;
  95.  
  96.     /* Convert. */
  97.     for ( row = 0; row < rows; ++row )
  98.         {
  99.         for ( col = 0, pP = pixels[row]; col < cols; ++col, ++pP )
  100.             {
  101.             register int color, ind, b, plane;
  102.  
  103.             color = ppm_lookupcolor( cht, pP );
  104.             if ( color == -1 )
  105.                 pm_error(
  106.                     "color not found?!?  row=%d col=%d  r=%d g=%d b=%d",
  107.                     row, col, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP) );
  108.             ind = 80 * row + ( ( col >> 4 ) << 2 );
  109.             b = 0x8000 >> (col & 0xf);
  110.             for ( plane = 0; plane < 4; ++plane )
  111.                 if ( color & (1 << plane) )
  112.                     screen[ind+plane] |= b;
  113.             }
  114.         }
  115.  
  116.     /* And write out the screen buffer. */
  117.     for ( i = 0; i < ROWS*COLS/4; ++i )
  118.         (void) pm_writebigshort( stdout, screen[i] );
  119.  
  120.     exit( 0 );
  121.     }
  122.