1 /* pi1toppm.c - read a Degas PI1 file and produce a portable pixmap
2 **
3 ** Copyright (C) 1991 by Steve Belczyk (seb3@gte.com) 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 short screen[ROWS*COLS/4];          /* simulates the Atari's video RAM */
20 
21 int
main(argc,argv)22 main( argc, argv )
23     int argc;
24     char* argv[];
25     {
26     FILE* ifp;
27     pixel pal[16];                      /* Degas palette */
28     short i;
29     short j;
30     pixel* pixelrow;
31     register pixel* pP;
32     int row, col;
33 
34 
35     ppm_init( &argc, argv );
36 
37     /* Check args. */
38     if ( argc > 2 )
39         pm_usage( "[pi1file]" );
40 
41     if ( argc == 2 )
42         ifp = pm_openr( argv[1] );
43     else
44         ifp = stdin;
45 
46     /* Check resolution word */
47     (void) pm_readbigshort (ifp, &j);
48     if ( j != 0 )
49         pm_error( "not a PI1 file" );
50 
51     /* Read the palette. */
52     for ( i = 0; i < 16; ++i )
53         {
54         (void) pm_readbigshort (ifp, &j);
55         PPM_ASSIGN( pal[i],
56             ( j & 0x700 ) >> 8,
57             ( j & 0x070 ) >> 4,
58             ( j & 0x007 ) );
59         }
60 
61     /* Read the screen data */
62     for ( i = 0; i < ROWS*COLS/4; ++i )
63         (void) pm_readbigshort( ifp, &screen[i] );
64 
65     pm_close( ifp );
66 
67     /* Ok, get set for writing PPM. */
68     ppm_writeppminit( stdout, COLS, ROWS, (pixval) MAXVAL, 0 );
69     pixelrow = ppm_allocrow( COLS );
70 
71     /* Now do the conversion. */
72     for ( row = 0; row < ROWS; ++row )
73         {
74         for ( col = 0, pP = pixelrow; col < COLS; ++col, ++pP )
75             {
76             register int c, ind, b, plane;
77 
78             ind = 80 * row + ( ( col >> 4 ) << 2 );
79             b = 0x8000 >> ( col & 0xf );
80             c = 0;
81             for ( plane = 0; plane < 4; ++plane )
82                 if ( b & screen[ind+plane] )
83                     c |= (1 << plane);
84             *pP = pal[c];
85             }
86         ppm_writeppmrow( stdout, pixelrow, COLS, (pixval) MAXVAL, 0 );
87         }
88 
89     pm_close( stdout );
90 
91     exit( 0 );
92     }
93