Debian Codegraph @

converter/other/pamtoxvmini.c

raw
1 /*=============================================================================
2 pamtoxvmini
3 ===============================================================================
4 Convert Netpbm image to XV mini thumbnail.
5
6 Written by Bryan Henderson in April 2006 and contributed to the public
7 domain.
8 =============================================================================*/
9
10 #include <assert.h>
11 #include <limits.h>
12 #include <string.h>
13
14 #include "pm_c_util.h"
15 #include "nstring.h"
16 #include "pam.h"
17 #include "pammap.h"
18
19 typedef struct xvPalette {
20 unsigned int red[256];
21 unsigned int grn[256];
22 unsigned int blu[256];
23 } xvPalette;
24
25
26 struct CmdlineInfo {
27 const char * inputFileName;
28 };
29
30
31
32 static void
33 parseCommandLine(int const argc,
34 const char * argv[],
35 struct CmdlineInfo * const cmdlineP) {
36
37 if (argc-1 < 1)
38 cmdlineP->inputFileName = "-";
39 else {
40 cmdlineP->inputFileName = argv[1];
41
42 if (argc-1 > 1)
43 pm_error("Too many arguments: %u. Only argument is optional "
44 "input file name.", argc-1);
45 }
46 }
47
48
49
50 static void
51 makeXvPalette(xvPalette * const xvPaletteP) {
52
53 unsigned int paletteIndex;
54 unsigned int r;
55
56 paletteIndex = 0;
57
58 for (r = 0; r < 8; ++r) {
59 unsigned int g;
60 for (g = 0; g < 8; ++g) {
61 unsigned int b;
62 for (b = 0; b < 4; ++b) {
63 xvPaletteP->red[paletteIndex] = (r*255)/7;
64 xvPaletteP->grn[paletteIndex] = (g*255)/7;
65 xvPaletteP->blu[paletteIndex] = (b*255)/3;
66 ++paletteIndex;
67 }
68 }
69 }
70 }
71
72
73
74 static void
75 writeXvHeader(FILE * const ofP,
76 unsigned int const cols,
77 unsigned int const rows) {
78
79 fprintf(ofP, "P7 332\n");
80
81 fprintf(ofP, "# Created by Pamtoxvmini\n");
82 fprintf(ofP, "#END_OF_COMMENTS\n");
83
84 /* I don't know what the maxval number (3rd field) means here, since
85 the maxvals are fixed at red=7, grn=7, blu=3. We used to have
86 it put the maxval of the input image there. That generated an
87 output that Xv choked on when the input maxval was 65535.
88 */
89
90 fprintf(ofP, "%u %u 255\n", cols, rows);
91 }
92
93
94
95 static void
96 findClosestColor(struct pam * const pamP,
97 tuple const tuple,
98 const xvPalette * const xvPaletteP,
99 unsigned int * const paletteIndexP) {
100 /*----------------------------------------------------------------------------
101 Find the color in the palette *xvPaletteP that is closest to the color
102 'tuple' and return its index in the palette.
103
104 *pamP gives the format of 'tuple', which must be RGB with maxval 255.
105 -----------------------------------------------------------------------------*/
106 unsigned int paletteIndex;
107 unsigned int bestPaletteIndex;
108 unsigned int bestDistanceSoFar;
109
110 /* An entry condition is that the tuple have the same form as the
111 colors in the XV palette:
112 */
113 assert(pamP->depth >= 3);
114 assert(pamP->maxval == 255);
115
116 bestPaletteIndex = 0;
117 bestDistanceSoFar = UINT_MAX;
118
119 for (paletteIndex = 0; paletteIndex < 256; ++paletteIndex) {
120 unsigned int const tupleRed = tuple[PAM_RED_PLANE];
121 unsigned int const tupleGrn = tuple[PAM_GRN_PLANE];
122 unsigned int const tupleBlu = tuple[PAM_BLU_PLANE];
123
124 unsigned int const paletteRed = xvPaletteP->red[paletteIndex];
125 unsigned int const paletteGrn = xvPaletteP->grn[paletteIndex];
126 unsigned int const paletteBlu = xvPaletteP->blu[paletteIndex];
127
128 unsigned int const distance =
129 SQR((int)tupleRed - (int)paletteRed) +
130 SQR((int)tupleGrn - (int)paletteGrn) +
131 SQR((int)tupleBlu - (int)paletteBlu);
132
133 if (distance < bestDistanceSoFar) {
134 bestDistanceSoFar = distance;
135 bestPaletteIndex = paletteIndex;
136 }
137 }
138 *paletteIndexP = bestPaletteIndex;
139 }
140
141
142
143 static void
144 getPaletteIndexThroughCache(struct pam * const pamP,
145 tuple const tuple,
146 const xvPalette * const xvPaletteP,
147 tuplehash const paletteHash,
148 unsigned int * const paletteIndexP) {
149 /*----------------------------------------------------------------------------
150 Return as *paletteIndexP the index into the palette *xvPaletteP of
151 the color that most closely resembles the color 'tuple'.
152
153 Use the hash table *paletteIndexP as a cache to speed up the search.
154 If the tuple-index association is in *paletteIndexP, use it. If not,
155 find it the hard way and add it to *palettedIndexP for the next guy.
156 -----------------------------------------------------------------------------*/
157 int found;
158 int paletteIndex;
159
160 /* As required by findClosestColor(): */
161 assert(pamP->depth >= 3);
162 assert(pamP->maxval == 255);
163
164 pnm_lookuptuple(pamP, paletteHash, tuple, &found, &paletteIndex);
165 if (found)
166 *paletteIndexP = paletteIndex;
167 else {
168 int fits;
169
170 findClosestColor(pamP, tuple, xvPaletteP, paletteIndexP);
171
172 pnm_addtotuplehash(pamP, paletteHash, tuple, *paletteIndexP, &fits);
173
174 if (!fits)
175 pm_error("Can't get memory for palette hash.");
176 }
177 }
178
179
180
181 static void
182 writeXvRaster(struct pam * const inpamP,
183 xvPalette * const xvPaletteP,
184 FILE * const ofP) {
185 /*----------------------------------------------------------------------------
186 Write out the XV image, from the Netpbm input file ifP, which is
187 positioned to the raster.
188
189 The XV raster contains palette indices into the palette *xvPaletteP.
190
191 If there is any color in the image which is not in the palette, we
192 fail the program. We really should use the closest color in the palette
193 instead.
194 -----------------------------------------------------------------------------*/
195 tuplehash paletteHash;
196 tuple * tuplerow;
197 unsigned int row;
198 unsigned char * xvrow;
199 struct pam scaledPam;
200 struct pam scaledRgbPam;
201
202 pnm_setminallocationdepth(inpamP, 3);
203
204 paletteHash = pnm_createtuplehash();
205
206 tuplerow = pnm_allocpamrow(inpamP);
207 xvrow = (unsigned char*)pm_allocrow(inpamP->width, 1);
208
209 scaledPam = *inpamP; /* initial value */
210 scaledPam.maxval = 255;
211
212 scaledRgbPam = scaledPam; /* initial value */
213 scaledRgbPam.depth = MAX(3, scaledPam.depth);
214
215 for (row = 0; row < inpamP->height; ++row) {
216 unsigned int col;
217
218 pnm_readpamrow(inpamP, tuplerow);
219 pnm_scaletuplerow(inpamP, tuplerow, tuplerow, scaledPam.maxval);
220 pnm_makerowrgb(&scaledPam, tuplerow);
221
222 for (col = 0; col < scaledRgbPam.width; ++col) {
223 unsigned int paletteIndex;
224
225 getPaletteIndexThroughCache(&scaledRgbPam, tuplerow[col],
226 xvPaletteP, paletteHash,
227 &paletteIndex);
228
229 assert(paletteIndex < 256);
230
231 xvrow[col] = paletteIndex;
232 }
233 fwrite(xvrow, 1, scaledRgbPam.width, ofP);
234 }
235
236 pm_freerow((char*)xvrow);
237 pnm_freepamrow(tuplerow);
238
239 pnm_destroytuplehash(paletteHash);
240 }
241
242
243
244 int
245 main(int argc,
246 const char * argv[]) {
247
248 struct CmdlineInfo cmdline;
249 FILE * ifP;
250 struct pam pam;
251 xvPalette xvPalette;
252
253 pm_proginit(&argc, argv);
254
255 parseCommandLine(argc, argv, &cmdline);
256
257 ifP = pm_openr(cmdline.inputFileName);
258
259 makeXvPalette(&xvPalette);
260
261 pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(allocation_depth));
262
263 writeXvHeader(stdout, pam.width, pam.height);
264
265 writeXvRaster(&pam, &xvPalette, stdout);
266
267 pm_close(ifP);
268
269 return 0;
270 }
271
272
273