1 /* imgtoppm.c - read an Img-whatnot file and produce a portable pixmap
2 **
3 ** Based on a simple conversion program posted to comp.graphics by Ed Falk.
4 **
5 ** Copyright (C) 1989 by Jef Poskanzer.
6 **
7 ** Permission to use, copy, modify, and distribute this software and its
8 ** documentation for any purpose and without fee is hereby granted, provided
9 ** that the above copyright notice appear in all copies and that both that
10 ** copyright notice and this permission notice appear in supporting
11 ** documentation.  This software is provided "as is" without express or
12 ** implied warranty.
13 */
14 
15 #include <string.h>
16 #include "ppm.h"
17 
18 
19 
20 int
main(int argc,char ** argv)21 main(int argc, char ** argv) {
22 
23     FILE* ifp;
24     pixel* pixelrow;
25     pixel colormap[256];
26     register pixel* pP;
27     unsigned int cols;
28     unsigned int rows;
29     int argn, row, i;
30     int col;
31     pixval maxval;
32     unsigned int cmaplen;
33     int len, gotAT, gotCM, gotPD;
34     unsigned char buf[4096];
35     unsigned char* bP;
36 
37 
38     ppm_init( &argc, argv );
39 
40     argn = 1;
41 
42     if ( argn < argc )
43     {
44         ifp = pm_openr( argv[argn] );
45         argn++;
46     }
47     else
48         ifp = stdin;
49 
50     if ( argn != argc )
51         pm_usage( "[imgfile]" );
52 
53     /* Get signature. */
54     fread( buf, 8, 1, ifp );
55     buf[8] = '\0';
56 
57     /* Get entries. */
58     gotAT = 0;
59     gotCM = 0;
60     gotPD = 0;
61     while ( fread( buf, 2, 1, ifp ) == 1 )
62     {
63         if ( strncmp( (char*) buf, "AT", 2 ) == 0 )
64         {
65             if ( fread( buf, 8, 1, ifp ) != 1 )
66                 pm_error( "bad attributes header" );
67             buf[8] = '\0';
68             len = atoi( (char*) buf );
69             if ( fread( buf, len, 1, ifp ) != 1 )
70                 pm_error( "bad attributes buf" );
71             buf[len] = '\0';
72             sscanf( (char*) buf, "%4u%4u%4u", &cols, &rows, &cmaplen );
73             maxval = 255;
74             gotAT = 1;
75         }
76 
77         else if ( strncmp( (char*) buf, "CM", 2 ) == 0 )
78         {
79             if ( ! gotAT )
80                 pm_error( "missing attributes header" );
81             if ( fread( buf, 8, 1, ifp ) != 1 )
82                 pm_error( "bad colormap header" );
83             buf[8] = '\0';
84             len = atoi((char*) buf );
85             if ( fread( buf, len, 1, ifp ) != 1 )
86                 pm_error( "bad colormap buf" );
87             if ( cmaplen * 3 != len )
88             {
89                 pm_message(
90                     "cmaplen (%d) and colormap buf length (%d) do not match",
91                     cmaplen, len );
92                 if ( cmaplen * 3 < len )
93                     len = cmaplen * 3;
94                 else if ( cmaplen * 3 > len )
95                     cmaplen = len / 3;
96             }
97             for ( i = 0; i < len; i += 3 )
98                 PPM_ASSIGN( colormap[i / 3], buf[i], buf[i + 1], buf[i + 2] );
99             gotCM = 1;
100         }
101 
102         else if ( strncmp( (char*) buf, "PD", 2 ) == 0 )
103         {
104             if ( fread( buf, 8, 1, ifp ) != 1 )
105                 pm_error( "bad pixel data header" );
106             buf[8] = '\0';
107             len = atoi((char*) buf );
108             if ( len != cols * rows )
109                 pm_message(
110                     "pixel data length (%d) does not match image size (%d)",
111                     len, cols * rows );
112 
113             ppm_writeppminit( stdout, cols, rows, maxval, 0 );
114             pixelrow = ppm_allocrow( cols );
115 
116             for ( row = 0; row < rows; row++ )
117             {
118                 if ( fread( buf, 1, cols, ifp ) != cols )
119                     pm_error( "EOF / read error" );
120                 for ( col = 0, pP = pixelrow, bP = buf;
121                       col < cols; col++, pP++, bP++ )
122                 {
123                     if ( gotCM )
124                         *pP = colormap[*bP];
125                     else
126                         PPM_ASSIGN( *pP, *bP, *bP, *bP );
127                 }
128                 ppm_writeppmrow( stdout, pixelrow, cols, maxval, 0 );
129             }
130             gotPD = 1;
131 
132         }
133     }
134     if ( ! gotPD )
135         pm_error( "missing pixel data header" );
136 
137     pm_close( ifp );
138     /* If the program failed, it previously aborted with nonzero completion
139        code, via various function calls.
140     */
141     return 0;
142 }
143