Debian Codegraph @

converter/ppm/xvminitoppm.c

raw symbols SCIP
1 /*=============================================================================
2 xvminitoppm
3 ===============================================================================
4 Convert XV mini thumbnail image to PPM.
5
6 This replaces the program of the same name by Ingo Wilken
7 ([email protected]), 1993.
8
9 Written by Bryan Henderson in April 2006 and contributed to the public
10 domain.
11 =============================================================================*/
12
13 #include <assert.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <stdbool.h>
17
18 #include "pm_c_util.h"
19 #include "nstring.h"
20 #include "pm.h"
21 #include "ppm.h"
22
23
24
25 typedef struct xvPalette {
26 unsigned int red[256];
27 unsigned int grn[256];
28 unsigned int blu[256];
29 } xvPalette;
30
31
32 struct cmdlineInfo {
33 const char * inputFileName;
34 };
35
36
37
38 static void
39 parseCommandLine(int const argc,
40 char * argv[],
41 struct cmdlineInfo * const cmdlineP) {
42
43 if (argc-1 < 1)
44 cmdlineP->inputFileName = "-";
45 else {
46 cmdlineP->inputFileName = argv[1];
47
48 if (argc-1 > 1)
49 pm_error("Too many arguments: %u. Only argument is optional "
50 "input file name.", argc-1);
51 }
52 }
53
54
55
56 static void
57 makeXvPalette(xvPalette * const xvPaletteP) {
58
59 unsigned int paletteIndex;
60 unsigned int r;
61
62 paletteIndex = 0;
63
64 for (r = 0; r < 8; ++r) {
65 unsigned int g;
66 for (g = 0; g < 8; ++g) {
67 unsigned int b;
68 for (b = 0; b < 4; ++b) {
69 xvPaletteP->red[paletteIndex] = (r*255)/7;
70 xvPaletteP->grn[paletteIndex] = (g*255)/7;
71 xvPaletteP->blu[paletteIndex] = (b*255)/3;
72 ++paletteIndex;
73 }
74 }
75 }
76
77 }
78
79
80
81 static void
82 readXvHeader(FILE * const ifP,
83 unsigned int * const colsP,
84 unsigned int * const rowsP,
85 unsigned int * const maxvalP) {
86
87 char * buf;
88 size_t bufferSz;
89 int eof;
90 size_t lineLen;
91 unsigned int cols, rows, maxval;
92 int rc;
93 bool endOfComments;
94
95 buf = NULL; /* initial value */
96 bufferSz = 0; /* initial value */
97
98 pm_getline(ifP, &buf, &bufferSz, &eof, &lineLen);
99
100 if (eof || !strneq(buf, "P7 332", 6))
101 pm_error("Input is not a XV thumbnail picture. It does not "
102 "begin with the characters 'P7 332'.");
103
104 for (endOfComments = false; !endOfComments; ) {
105 int eof;
106 size_t lineLen;
107 pm_getline(ifP, &buf, &bufferSz, &eof, &lineLen);
108 if (eof)
109 pm_error("EOF before #END_OF_COMMENTS line");
110 if (strneq(buf, "#END_OF_COMMENTS", 16))
111 endOfComments = true;
112 else if (strneq(buf, "#BUILTIN", 8))
113 pm_error("This program does not know how to "
114 "convert builtin XV thumbnail pictures");
115 }
116 pm_getline(ifP, &buf, &bufferSz, &eof, &lineLen);
117 if (eof)
118 pm_error("EOF where cols/rows/maxval line expected");
119
120 rc = sscanf(buf, "%u %u %u", &cols, &rows, &maxval);
121 if (rc != 3)
122 pm_error("error parsing dimension info '%s'. "
123 "It does not consist of 3 decimal numbers.", buf);
124 if (maxval != 255)
125 pm_error("bogus XV thumbnail maxval %u. Should be 255", maxval);
126
127 *colsP = cols;
128 *rowsP = rows;
129 *maxvalP = maxval;
130
131 if (buf)
132 free(buf);
133 }
134
135
136
137 static void
138 writePpm(FILE * const ifP,
139 const xvPalette * const xvPaletteP,
140 unsigned int const cols,
141 unsigned int const rows,
142 pixval const maxval,
143 FILE * const ofP) {
144 /*----------------------------------------------------------------------------
145 Write out the PPM image, from the XV-mini input file ifP, which is
146 positioned to the raster.
147
148 The raster contains indices into the palette *xvPaletteP.
149 -----------------------------------------------------------------------------*/
150 pixel * pixrow;
151 unsigned int row;
152
153 pixrow = ppm_allocrow(cols);
154
155 ppm_writeppminit(ofP, cols, rows, maxval, 0);
156
157 for (row = 0; row < rows; ++row) {
158 unsigned int col;
159 for (col = 0; col < cols; ++col) {
160 int byte;
161 byte = fgetc(ifP);
162 if (byte == EOF)
163 pm_error("unexpected EOF");
164 else {
165 unsigned int const paletteIndex = byte;
166 assert(byte >= 0);
167
168 PPM_ASSIGN(pixrow[col],
169 xvPaletteP->red[paletteIndex],
170 xvPaletteP->grn[paletteIndex],
171 xvPaletteP->blu[paletteIndex]);
172 }
173 }
174 ppm_writeppmrow(ofP, pixrow, cols, maxval, 0);
175 }
176
177 ppm_freerow(pixrow);
178 }
179
180
181
182 int
183 main(int argc,
184 char * argv[]) {
185
186 struct cmdlineInfo cmdline;
187 FILE * ifP;
188 unsigned int cols, rows;
189 pixval maxval;
190 xvPalette xvPalette;
191
192 ppm_init(&argc, argv);
193
194 parseCommandLine(argc, argv, &cmdline);
195
196 ifP = pm_openr(cmdline.inputFileName);
197
198 makeXvPalette(&xvPalette);
199
200 readXvHeader(ifP, &cols, &rows, &maxval);
201
202 writePpm(ifP, &xvPalette, cols, rows, maxval, stdout);
203
204 pm_close(ifP);
205
206 return 0;
207 }
208
209
210