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