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

  1. /* sputoppm.c - read an uncompressed Spectrum file and produce a portable pixmap
  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.  
  15. #define ROWS 200
  16. #define COLS 320
  17. #define MAXVAL 7
  18.  
  19. static pixel pal[ROWS][48];                /* Spectrum palettes, three per row */
  20. static short screen[ROWS*COLS/4];          /* simulates the Atari's video RAM */
  21.  
  22. int
  23. main( argc, argv )
  24.     int argc;
  25.     char* argv[];
  26.     {
  27.     FILE* ifp;
  28.     int i, j;
  29.     pixel* pixelrow;
  30.     register pixel* pP;
  31.     int row, col;
  32.  
  33.  
  34.     ppm_init( &argc, argv );
  35.  
  36.     /* Check args. */
  37.     if ( argc > 2 )
  38.         pm_usage( "[spufile]" );
  39.  
  40.     if ( argc == 2 )
  41.         ifp = pm_openr( argv[1] );
  42.     else
  43.         ifp = stdin;
  44.  
  45.     /* Read the SPU file */
  46.  
  47.     /* Read the screen data. */
  48.     for ( i = 0; i < ROWS*COLS/4; ++i )
  49.         (void) pm_readbigshort( ifp, &screen[i] );
  50.  
  51.     /* Clear the first palette line. */
  52.     for ( j = 0; j < 48; ++j )
  53.         PPM_ASSIGN( pal[0][j], 0, 0, 0 );
  54.  
  55.     /* Read the palettes. */
  56.     for ( i = 1; i < ROWS; ++i )
  57.         for ( j = 0; j < 48; ++j )
  58.             {
  59.             short k;
  60.             (void) pm_readbigshort( ifp, &k );
  61.             PPM_ASSIGN( pal[i][j],
  62.                 ( k & 0x700 ) >> 8,
  63.                 ( k & 0x070 ) >> 4,
  64.                 ( k & 0x007 ) );
  65.             }
  66.  
  67.     pm_close( ifp );
  68.  
  69.     /* Ok, get set for writing PPM. */
  70.     ppm_writeppminit( stdout, COLS, ROWS, (pixval) MAXVAL, 0 );
  71.     pixelrow = ppm_allocrow( COLS );
  72.  
  73.     /* Now do the conversion. */
  74.     for ( row = 0; row < ROWS; ++row )
  75.         {
  76.         for ( col = 0, pP = pixelrow; col < COLS; ++col, ++pP )
  77.             {
  78.             int c, ind, b, plane, x1;
  79.  
  80.             /* Compute pixel value. */
  81.             ind = 80 * row + ( ( col >> 4 ) << 2 );
  82.             b = 0x8000 >> (col & 0xf);
  83.             c = 0;
  84.             for ( plane = 0; plane < 4; ++plane )
  85.                 if ( b & screen[ind+plane] )
  86.                     c |= (1 << plane);
  87.  
  88.             /* Compute palette index. */
  89.             x1 = 10 * c;
  90.             if ( c & 1 )
  91.                 x1 -= 5;
  92.             else
  93.                 ++x1;
  94.             if ( ( col >= x1 ) && ( col < ( x1 + 160 ) ) )
  95.                 c += 16;
  96.             if ( col >= ( x1 + 160 ) )
  97.                 c += 32;
  98.  
  99.             /* Store the proper color. */
  100.             *pP = pal[row][c];
  101.             }
  102.         ppm_writeppmrow( stdout, pixelrow, COLS, (pixval) MAXVAL, 0 );
  103.         }
  104.  
  105.     pm_close( stdout );
  106.  
  107.     exit( 0 );
  108.     }
  109.