Debian Codegraph @

converter/other/pnmtopalm/palmcolormap.c

raw symbols SCIP
1 /* See LICENSE file for licensing information.
2 */
3
4 #include "pm_c_util.h"
5 #include "mallocvar.h"
6 #include "pnm.h"
7 #include "palm.h"
8
9 #include "palmcolormap.h"
10
11
12
13 static pixval
14 scaleSample(pixval const arg,
15 pixval const oldMaxval,
16 pixval const newMaxval) {
17
18 return (arg * newMaxval + oldMaxval/2) / oldMaxval;
19 }
20
21
22
23 ColormapEntry
24 palmcolor_mapEntryColorFmPixel(pixel const color,
25 pixval const maxval,
26 pixval const newMaxval) {
27
28
29 return
30 0
31 | (scaleSample(PPM_GETR(color), maxval, newMaxval) << 16)
32 | (scaleSample(PPM_GETG(color), maxval, newMaxval) << 8)
33 | (scaleSample(PPM_GETB(color), maxval, newMaxval) << 0);
34 }
35
36
37
38 int
39 palmcolor_compare_indices(const void * const p1,
40 const void * const p2) {
41 /*----------------------------------------------------------------------------
42 This is a 'qsort' collation function.
43 -----------------------------------------------------------------------------*/
44 if ((*((ColormapEntry *) p1) & 0xFF000000) < (*((ColormapEntry *) p2) & 0xFF000000))
45 return -1;
46 else if ((*((ColormapEntry *) p1) & 0xFF000000) > (*((ColormapEntry *) p2) & 0xFF000000))
47 return 1;
48 else
49 return 0;
50 }
51
52
53
54 int
55 palmcolor_compare_colors(const void * const p1,
56 const void * const p2) {
57 /*----------------------------------------------------------------------------
58 This is a 'qsort' collation function.
59 -----------------------------------------------------------------------------*/
60 unsigned long const val1 = *((const unsigned long *) p1) & 0xFFFFFF;
61 unsigned long const val2 = *((const unsigned long *) p2) & 0xFFFFFF;
62
63 if (val1 < val2)
64 return -1;
65 else if (val1 > val2)
66 return 1;
67 else
68 return 0;
69 }
70
71
72
73 /***********************************************************************
74 ***********************************************************************
75 ***********************************************************************
76 ******* colortables from pilrc-2.6/bitmap.c ***************************
77 ***********************************************************************
78 ***********************************************************************
79 ***********************************************************************/
80
81 #if 0
82
83 /*
84 * The 1bit-2 color system palette for Palm Computing Devices.
85 */
86 static int PalmPalette1bpp[2][3] =
87 {
88 { 255, 255, 255}, { 0, 0, 0 }
89 };
90
91 /*
92 * The 2bit-4 color system palette for Palm Computing Devices.
93 */
94 static int PalmPalette2bpp[4][3] =
95 {
96 { 255, 255, 255}, { 192, 192, 192}, { 128, 128, 128 }, { 0, 0, 0 }
97 };
98
99 /*
100 * The 4bit-16 color system palette for Palm Computing Devices.
101 */
102 static int PalmPalette4bpp[16][3] =
103 {
104 { 255, 255, 255}, { 238, 238, 238 }, { 221, 221, 221 }, { 204, 204, 204 },
105 { 187, 187, 187}, { 170, 170, 170 }, { 153, 153, 153 }, { 136, 136, 136 },
106 { 119, 119, 119}, { 102, 102, 102 }, { 85, 85, 85 }, { 68, 68, 68 },
107 { 51, 51, 51}, { 34, 34, 34 }, { 17, 17, 17 }, { 0, 0, 0 }
108 };
109
110 /*
111 * The 4bit-16 color system palette for Palm Computing Devices.
112 */
113 static int PalmPalette4bppColor[16][3] =
114 {
115 { 255, 255, 255}, { 128, 128, 128 }, { 128, 0, 0 }, { 128, 128, 0 },
116 { 0, 128, 0}, { 0, 128, 128 }, { 0, 0, 128 }, { 128, 0, 128 },
117 { 255, 0, 255}, { 192, 192, 192 }, { 255, 0, 0 }, { 255, 255, 0 },
118 { 0, 255, 0}, { 0, 255, 255 }, { 0, 0, 255 }, { 0, 0, 0 }
119 };
120
121 #endif /* 0 */
122
123 /*
124 * The 8bit-256 color system palette for Palm Computing Devices.
125 *
126 * NOTE: only the first 231, plus the last one, are valid.
127 */
128 static int PalmPalette8bpp[256][3] =
129 {
130 { 255, 255, 255 }, { 255, 204, 255 }, { 255, 153, 255 }, { 255, 102, 255 },
131 { 255, 51, 255 }, { 255, 0, 255 }, { 255, 255, 204 }, { 255, 204, 204 },
132 { 255, 153, 204 }, { 255, 102, 204 }, { 255, 51, 204 }, { 255, 0, 204 },
133 { 255, 255, 153 }, { 255, 204, 153 }, { 255, 153, 153 }, { 255, 102, 153 },
134 { 255, 51, 153 }, { 255, 0, 153 }, { 204, 255, 255 }, { 204, 204, 255 },
135 { 204, 153, 255 }, { 204, 102, 255 }, { 204, 51, 255 }, { 204, 0, 255 },
136 { 204, 255, 204 }, { 204, 204, 204 }, { 204, 153, 204 }, { 204, 102, 204 },
137 { 204, 51, 204 }, { 204, 0, 204 }, { 204, 255, 153 }, { 204, 204, 153 },
138 { 204, 153, 153 }, { 204, 102, 153 }, { 204, 51, 153 }, { 204, 0, 153 },
139 { 153, 255, 255 }, { 153, 204, 255 }, { 153, 153, 255 }, { 153, 102, 255 },
140 { 153, 51, 255 }, { 153, 0, 255 }, { 153, 255, 204 }, { 153, 204, 204 },
141 { 153, 153, 204 }, { 153, 102, 204 }, { 153, 51, 204 }, { 153, 0, 204 },
142 { 153, 255, 153 }, { 153, 204, 153 }, { 153, 153, 153 }, { 153, 102, 153 },
143 { 153, 51, 153 }, { 153, 0, 153 }, { 102, 255, 255 }, { 102, 204, 255 },
144 { 102, 153, 255 }, { 102, 102, 255 }, { 102, 51, 255 }, { 102, 0, 255 },
145 { 102, 255, 204 }, { 102, 204, 204 }, { 102, 153, 204 }, { 102, 102, 204 },
146 { 102, 51, 204 }, { 102, 0, 204 }, { 102, 255, 153 }, { 102, 204, 153 },
147 { 102, 153, 153 }, { 102, 102, 153 }, { 102, 51, 153 }, { 102, 0, 153 },
148 { 51, 255, 255 }, { 51, 204, 255 }, { 51, 153, 255 }, { 51, 102, 255 },
149 { 51, 51, 255 }, { 51, 0, 255 }, { 51, 255, 204 }, { 51, 204, 204 },
150 { 51, 153, 204 }, { 51, 102, 204 }, { 51, 51, 204 }, { 51, 0, 204 },
151 { 51, 255, 153 }, { 51, 204, 153 }, { 51, 153, 153 }, { 51, 102, 153 },
152 { 51, 51, 153 }, { 51, 0, 153 }, { 0, 255, 255 }, { 0, 204, 255 },
153 { 0, 153, 255 }, { 0, 102, 255 }, { 0, 51, 255 }, { 0, 0, 255 },
154 { 0, 255, 204 }, { 0, 204, 204 }, { 0, 153, 204 }, { 0, 102, 204 },
155 { 0, 51, 204 }, { 0, 0, 204 }, { 0, 255, 153 }, { 0, 204, 153 },
156 { 0, 153, 153 }, { 0, 102, 153 }, { 0, 51, 153 }, { 0, 0, 153 },
157 { 255, 255, 102 }, { 255, 204, 102 }, { 255, 153, 102 }, { 255, 102, 102 },
158 { 255, 51, 102 }, { 255, 0, 102 }, { 255, 255, 51 }, { 255, 204, 51 },
159 { 255, 153, 51 }, { 255, 102, 51 }, { 255, 51, 51 }, { 255, 0, 51 },
160 { 255, 255, 0 }, { 255, 204, 0 }, { 255, 153, 0 }, { 255, 102, 0 },
161 { 255, 51, 0 }, { 255, 0, 0 }, { 204, 255, 102 }, { 204, 204, 102 },
162 { 204, 153, 102 }, { 204, 102, 102 }, { 204, 51, 102 }, { 204, 0, 102 },
163 { 204, 255, 51 }, { 204, 204, 51 }, { 204, 153, 51 }, { 204, 102, 51 },
164 { 204, 51, 51 }, { 204, 0, 51 }, { 204, 255, 0 }, { 204, 204, 0 },
165 { 204, 153, 0 }, { 204, 102, 0 }, { 204, 51, 0 }, { 204, 0, 0 },
166 { 153, 255, 102 }, { 153, 204, 102 }, { 153, 153, 102 }, { 153, 102, 102 },
167 { 153, 51, 102 }, { 153, 0, 102 }, { 153, 255, 51 }, { 153, 204, 51 },
168 { 153, 153, 51 }, { 153, 102, 51 }, { 153, 51, 51 }, { 153, 0, 51 },
169 { 153, 255, 0 }, { 153, 204, 0 }, { 153, 153, 0 }, { 153, 102, 0 },
170 { 153, 51, 0 }, { 153, 0, 0 }, { 102, 255, 102 }, { 102, 204, 102 },
171 { 102, 153, 102 }, { 102, 102, 102 }, { 102, 51, 102 }, { 102, 0, 102 },
172 { 102, 255, 51 }, { 102, 204, 51 }, { 102, 153, 51 }, { 102, 102, 51 },
173 { 102, 51, 51 }, { 102, 0, 51 }, { 102, 255, 0 }, { 102, 204, 0 },
174 { 102, 153, 0 }, { 102, 102, 0 }, { 102, 51, 0 }, { 102, 0, 0 },
175 { 51, 255, 102 }, { 51, 204, 102 }, { 51, 153, 102 }, { 51, 102, 102 },
176 { 51, 51, 102 }, { 51, 0, 102 }, { 51, 255, 51 }, { 51, 204, 51 },
177 { 51, 153, 51 }, { 51, 102, 51 }, { 51, 51, 51 }, { 51, 0, 51 },
178 { 51, 255, 0 }, { 51, 204, 0 }, { 51, 153, 0 }, { 51, 102, 0 },
179 { 51, 51, 0 }, { 51, 0, 0 }, { 0, 255, 102 }, { 0, 204, 102 },
180 { 0, 153, 102 }, { 0, 102, 102 }, { 0, 51, 102 }, { 0, 0, 102 },
181 { 0, 255, 51 }, { 0, 204, 51 }, { 0, 153, 51 }, { 0, 102, 51 },
182 { 0, 51, 51 }, { 0, 0, 51 }, { 0, 255, 0 }, { 0, 204, 0 },
183 { 0, 153, 0 }, { 0, 102, 0 }, { 0, 51, 0 }, { 17, 17, 17 },
184 { 34, 34, 34 }, { 68, 68, 68 }, { 85, 85, 85 }, { 119, 119, 119 },
185 { 136, 136, 136 }, { 170, 170, 170 }, { 187, 187, 187 }, { 221, 221, 221 },
186 { 238, 238, 238 }, { 192, 192, 192 }, { 128, 0, 0 }, { 128, 0, 128 },
187 { 0, 128, 0 }, { 0, 128, 128 }, { 0, 0, 0 }, { 0, 0, 0 },
188 { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
189 { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
190 { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
191 { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
192 { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
193 { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }
194 };
195
196
197
198 Colormap *
199 palmcolor_build_default_8bit_colormap(void) {
200
201 unsigned int i;
202
203 Colormap * cmP;
204
205 MALLOCVAR_NOFAIL(cmP);
206 cmP->nentries = 232;
207 MALLOCARRAY_NOFAIL(cmP->color_entries, cmP->nentries);
208
209 /* Fill in the colors */
210 for (i = 0; i < 231; ++i) {
211 cmP->color_entries[i] = ((i << 24) |
212 (PalmPalette8bpp[i][0] << 16) |
213 (PalmPalette8bpp[i][1] << 8) |
214 (PalmPalette8bpp[i][2]));
215 }
216 cmP->color_entries[231] = 0xFF000000;
217 cmP->ncolors = 232;
218
219 /* now sort the table */
220 qsort(cmP->color_entries, cmP->ncolors, sizeof(ColormapEntry),
221 palmcolor_compare_colors);
222 return cmP;
223 }
224
225
226
227 Colormap *
228 palmcolor_build_custom_8bit_colormap(pixel ** const pixels,
229 unsigned int const rows,
230 unsigned int const cols,
231 pixval const maxval) {
232
233 unsigned int row;
234 Colormap * colormapP;
235
236 MALLOCVAR_NOFAIL(colormapP);
237 colormapP->nentries = 256;
238 MALLOCARRAY_NOFAIL(colormapP->color_entries, colormapP->nentries);
239 colormapP->ncolors = 0; /* initial value */
240
241 for (row = 0; row < rows; ++row) {
242 unsigned int col;
243 for (col = 0; col < cols; ++col) {
244 ColormapEntry * foundEntryP;
245 ColormapEntry const searchTarget =
246 palmcolor_mapEntryColorFmPixel(pixels[row][col], maxval, 255);
247
248 foundEntryP =
249 bsearch(&searchTarget,
250 colormapP->color_entries, colormapP->ncolors,
251 sizeof(ColormapEntry), palmcolor_compare_colors);
252 if (!foundEntryP) {
253 if (colormapP->ncolors >= colormapP->nentries)
254 pm_error("Too many colors for custom colormap "
255 "(max %u). "
256 "Try using pnmquant to reduce the number "
257 "of colors.", colormapP->nentries);
258 else {
259 /* add the new color, and re-sort */
260 unsigned int const colorIndex = colormapP->ncolors++;
261 ColormapEntry const newEntry =
262 searchTarget | (colorIndex << 24);
263 colormapP->color_entries[colorIndex] = newEntry;
264 qsort(colormapP->color_entries, colormapP->ncolors,
265 sizeof(ColormapEntry), palmcolor_compare_colors);
266 }
267 }
268 }
269 }
270 return colormapP;
271 }
272
273
274
275 Colormap *
276 palmcolor_read_colormap (FILE * const ifP) {
277
278 unsigned short ncolors;
279 Colormap * retval;
280 int rc;
281
282 rc = pm_readbigshort(ifP, (short *) &ncolors);
283 if (rc != 0)
284 retval = NULL;
285 else {
286 long colorentry;
287 Colormap * colormapP;
288 unsigned int i;
289 bool error;
290
291 MALLOCVAR_NOFAIL(colormapP);
292 colormapP->nentries = ncolors;
293 MALLOCARRAY_NOFAIL(colormapP->color_entries, colormapP->nentries);
294
295 for (i = 0, error = FALSE; i < ncolors && !error; ++i) {
296 int rc;
297 rc = pm_readbiglong(ifP, &colorentry);
298 if (rc != 0)
299 error = TRUE;
300 else
301 colormapP->color_entries[i] = (colorentry & 0xFFFFFFFF);
302 }
303 if (error) {
304 free (colormapP->color_entries);
305 free (colormapP);
306 retval = NULL;
307 } else {
308 colormapP->ncolors = ncolors;
309 retval = colormapP;
310 }
311 }
312 return retval;
313 }
314
315
316