Lines 1-5
Link Here
|
1 |
--- ppm/ppmtobmp.c.orig Mon Oct 4 18:12:36 1993 |
1 |
--- ppm/ppmtobmp.c.orig Thu Mar 23 16:34:34 2000 |
2 |
+++ ppm/ppmtobmp.c Sat Oct 16 02:53:18 1999 |
2 |
+++ ppm/ppmtobmp.c Sun Apr 1 12:00:00 2000 |
3 |
@@ -75,7 +75,7 @@ |
3 |
@@ -75,7 +75,7 @@ |
4 |
unsigned short cBitCount, pixel **pixels, colorhash_table cht)); |
4 |
unsigned short cBitCount, pixel **pixels, colorhash_table cht)); |
5 |
static int colorstobpp ARGS((int colors)); |
5 |
static int colorstobpp ARGS((int colors)); |
Lines 7-109
Link Here
|
7 |
- int colors, colorhash_table cht, pixval *R, pixval *G, pixval *B)); |
7 |
- int colors, colorhash_table cht, pixval *R, pixval *G, pixval *B)); |
8 |
+ int bpp, int colors, colorhash_table cht, pixval *R, pixval *G, pixval *B)); |
8 |
+ int bpp, int colors, colorhash_table cht, pixval *R, pixval *G, pixval *B)); |
9 |
static void |
9 |
static void |
10 |
PutByte(fp, v) |
10 |
PutByte(FILE *fp, char v) |
11 |
FILE *fp; |
11 |
{ |
12 |
@@ -253,19 +253,23 @@ |
12 |
@@ -244,16 +244,20 @@ |
13 |
int i; |
13 |
int i; |
14 |
long ncolors; |
14 |
long ncolors; |
15 |
|
15 |
|
16 |
- for (i = 0; i < colors; i++) |
16 |
- for (i = 0; i < colors; i++) |
17 |
- { |
17 |
+ if (bpp != 24) |
|
|
18 |
{ |
18 |
- nbyte += BMPwritergb(fp,class,R[i],G[i],B[i]); |
19 |
- nbyte += BMPwritergb(fp,class,R[i],G[i],B[i]); |
19 |
- } |
20 |
- } |
20 |
- |
21 |
|
21 |
- ncolors = (1 << bpp); |
22 |
- ncolors = (1 << bpp); |
22 |
+ if (bpp != 24) |
23 |
+ for (i = 0; i < colors; i++) |
23 |
+ { |
24 |
+ { |
|
|
25 |
+ nbyte += BMPwritergb(fp,class,R[i],G[i],B[i]); |
26 |
+ } |
27 |
+ |
28 |
+ ncolors = (1 << bpp); |
24 |
|
29 |
|
25 |
- for (; i < ncolors; i++) |
30 |
- for (; i < ncolors; i++) |
26 |
- { |
31 |
- { |
27 |
- nbyte += BMPwritergb(fp,class,0,0,0); |
32 |
- nbyte += BMPwritergb(fp,class,0,0,0); |
28 |
- } |
33 |
+ for (; i < ncolors; i++) |
29 |
+ for (i = 0; i < colors; i++) |
34 |
+ { |
30 |
+ { |
35 |
+ nbyte += BMPwritergb(fp,class,0,0,0); |
31 |
+ nbyte += BMPwritergb(fp,class,R[i],G[i],B[i]); |
36 |
+ } |
32 |
+ } |
37 |
} |
33 |
+ |
|
|
34 |
+ ncolors = (1 << bpp); |
35 |
+ |
36 |
+ for (; i < ncolors; i++) |
37 |
+ { |
38 |
+ nbyte += BMPwritergb(fp,class,0,0,0); |
39 |
+ } |
40 |
+ } |
41 |
|
42 |
- return nbyte; |
43 |
+ return nbyte; |
44 |
} |
45 |
|
38 |
|
46 |
/* |
39 |
return nbyte; |
47 |
@@ -284,26 +288,38 @@ |
40 |
@@ -271,26 +275,38 @@ |
48 |
int rc; |
41 |
int rc; |
49 |
unsigned x; |
42 |
unsigned x; |
50 |
|
43 |
|
51 |
- if ((b = pm_bitinit(fp, "w")) == (BITSTREAM) 0) |
44 |
- if ((b = pm_bitinit(fp, "w")) == (BITSTREAM) 0) |
52 |
- { |
45 |
+ if (bpp != 24) |
|
|
46 |
{ |
53 |
- return -1; |
47 |
- return -1; |
54 |
- } |
48 |
- } |
55 |
- |
49 |
+ if ((b = pm_bitinit(fp, "w")) == (BITSTREAM) 0) |
|
|
50 |
+ { |
51 |
+ return -1; |
52 |
+ } |
53 |
|
56 |
- for (x = 0; x < cx; x++, row++) |
54 |
- for (x = 0; x < cx; x++, row++) |
57 |
- { |
55 |
- { |
58 |
- if ((rc = pm_bitwrite(b, bpp, ppm_lookupcolor(cht, row))) == -1) |
56 |
- if ((rc = pm_bitwrite(b, bpp, ppm_lookupcolor(cht, row))) == -1) |
59 |
- { |
57 |
+ for (x = 0; x < cx; x++, row++) |
60 |
- return -1; |
58 |
+ { |
61 |
- } |
59 |
+ if ((rc = pm_bitwrite(b, bpp, ppm_lookupcolor(cht, row))) == -1) |
62 |
- nbyte += rc; |
60 |
+ { |
63 |
- } |
61 |
+ return -1; |
|
|
62 |
+ } |
63 |
+ nbyte += rc; |
64 |
+ } |
65 |
+ |
66 |
+ if ((rc = pm_bitfini(b)) == -1) |
67 |
{ |
68 |
return -1; |
69 |
} |
70 |
nbyte += rc; |
71 |
} |
64 |
- |
72 |
- |
65 |
- if ((rc = pm_bitfini(b)) == -1) |
73 |
- if ((rc = pm_bitfini(b)) == -1) |
66 |
- { |
74 |
+ else |
|
|
75 |
{ |
67 |
- return -1; |
76 |
- return -1; |
68 |
- } |
77 |
+ for (x = 0; x < cx; x++, row++) |
|
|
78 |
+ { |
79 |
+ PutByte(fp, PPM_GETB(*row)); |
80 |
+ PutByte(fp, PPM_GETG(*row)); |
81 |
+ PutByte(fp, PPM_GETR(*row)); |
82 |
+ nbyte += 3; |
83 |
+ } |
84 |
} |
69 |
- nbyte += rc; |
85 |
- nbyte += rc; |
70 |
- |
86 |
- |
71 |
+ if (bpp != 24) |
|
|
72 |
+ { |
73 |
+ if ((b = pm_bitinit(fp, "w")) == (BITSTREAM) 0) |
74 |
+ { |
75 |
+ return -1; |
76 |
+ } |
77 |
+ |
78 |
+ for (x = 0; x < cx; x++, row++) |
79 |
+ { |
80 |
+ if ((rc = pm_bitwrite(b, bpp, ppm_lookupcolor(cht, row))) == -1) |
81 |
+ { |
82 |
+ return -1; |
83 |
+ } |
84 |
+ nbyte += rc; |
85 |
+ } |
86 |
+ |
87 |
+ if ((rc = pm_bitfini(b)) == -1) |
88 |
+ { |
89 |
+ return -1; |
90 |
+ } |
91 |
+ nbyte += rc; |
92 |
+ } |
93 |
+ else |
94 |
+ { |
95 |
+ for (x = 0; x < cx; x++, row++) |
96 |
+ { |
97 |
+ PutByte(fp, PPM_GETB(*row)); |
98 |
+ PutByte(fp, PPM_GETG(*row)); |
99 |
+ PutByte(fp, PPM_GETR(*row)); |
100 |
+ nbyte += 3; |
101 |
+ } |
102 |
+ } |
103 |
/* |
87 |
/* |
104 |
* Make sure we write a multiple of 4 bytes. |
88 |
* Make sure we write a multiple of 4 bytes. |
105 |
*/ |
89 |
*/ |
106 |
@@ -394,48 +410,21 @@ |
90 |
@@ -376,48 +392,21 @@ |
107 |
* arrays are undefined. |
91 |
* arrays are undefined. |
108 |
*/ |
92 |
*/ |
109 |
static void |
93 |
static void |
Lines 114-120
Link Here
|
114 |
int x; |
98 |
int x; |
115 |
int y; |
99 |
int y; |
116 |
pixel **pixels; |
100 |
pixel **pixels; |
117 |
+ int bpp; /* bits per pixel */ |
101 |
+ int bpp; /* bits per pixel */ |
118 |
int colors; /* number of valid entries in R,G,B */ |
102 |
int colors; /* number of valid entries in R,G,B */ |
119 |
colorhash_table cht; |
103 |
colorhash_table cht; |
120 |
pixval *R; |
104 |
pixval *R; |
Lines 154-160
Link Here
|
154 |
nbyte += BMPwritefileheader(fp, class, bpp, x, y); |
138 |
nbyte += BMPwritefileheader(fp, class, bpp, x, y); |
155 |
nbyte += BMPwriteinfoheader(fp, class, bpp, x, y); |
139 |
nbyte += BMPwriteinfoheader(fp, class, bpp, x, y); |
156 |
nbyte += BMPwritergbtable(fp, class, bpp, colors, R, G, B); |
140 |
nbyte += BMPwritergbtable(fp, class, bpp, colors, R, G, B); |
157 |
@@ -517,13 +506,15 @@ |
141 |
@@ -499,13 +488,15 @@ |
158 |
char **argv; |
142 |
char **argv; |
159 |
{ |
143 |
{ |
160 |
FILE *ifp = stdin; |
144 |
FILE *ifp = stdin; |
Lines 166-177
Link Here
|
166 |
int rows; |
150 |
int rows; |
167 |
int cols; |
151 |
int cols; |
168 |
int colors; |
152 |
int colors; |
169 |
+ int maxcolors = MAXCOLORS; |
153 |
+ int maxcolors = MAXCOLORS; |
170 |
+ int bpp = 0; |
154 |
+ int bpp = 0; |
171 |
int i; |
155 |
int i; |
172 |
pixval maxval; |
156 |
pixval maxval; |
173 |
colorhist_vector chv; |
157 |
colorhist_vector chv; |
174 |
@@ -545,6 +536,14 @@ |
158 |
@@ -527,6 +518,14 @@ |
175 |
class = C_WIN; |
159 |
class = C_WIN; |
176 |
else if (pm_keymatch(argv[argn], "-os2", 2)) |
160 |
else if (pm_keymatch(argv[argn], "-os2", 2)) |
177 |
class = C_OS2; |
161 |
class = C_OS2; |
Lines 186-204
Link Here
|
186 |
else |
170 |
else |
187 |
pm_usage(usage); |
171 |
pm_usage(usage); |
188 |
++argn; |
172 |
++argn; |
189 |
@@ -585,44 +584,90 @@ |
173 |
@@ -567,44 +566,90 @@ |
190 |
#endif |
174 |
#endif |
191 |
|
175 |
|
192 |
/* Figure out the colormap. */ |
176 |
/* Figure out the colormap. */ |
193 |
- pm_message("computing colormap..."); |
177 |
- pm_message("computing colormap..."); |
194 |
- chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors); |
178 |
- chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors); |
195 |
+ chv = (colorhist_vector) 0; |
179 |
- if (chv == (colorhist_vector) 0) |
196 |
+ if(bpp != 24) |
|
|
197 |
+ { |
198 |
+ pm_message("computing colormap..."); |
199 |
+ chv = ppm_computecolorhist(pixels, cols, rows, maxcolors, &colors); |
200 |
+ } |
201 |
if (chv == (colorhist_vector) 0) |
202 |
- pm_error("too many colors - try doing a 'ppmquant %d'" |
180 |
- pm_error("too many colors - try doing a 'ppmquant %d'" |
203 |
- , MAXCOLORS); |
181 |
- , MAXCOLORS); |
204 |
- pm_message("%d colors found", colors); |
182 |
- pm_message("%d colors found", colors); |
Lines 208-307
Link Here
|
208 |
- * colormap. |
186 |
- * colormap. |
209 |
- */ |
187 |
- */ |
210 |
- if (maxval > 255) |
188 |
- if (maxval > 255) |
211 |
- { |
189 |
+ chv = (colorhist_vector) 0; |
|
|
190 |
+ if (bpp != 24) |
191 |
{ |
212 |
- pm_message("maxval is not 255 - automatically rescaling colors"); |
192 |
- pm_message("maxval is not 255 - automatically rescaling colors"); |
213 |
- } |
193 |
+ pm_message("computing colormap..."); |
|
|
194 |
+ chv = ppm_computecolorhist(pixels, cols, rows, maxcolors, &colors); |
195 |
} |
214 |
- for (i = 0; i < colors; ++i) |
196 |
- for (i = 0; i < colors; ++i) |
215 |
- { |
197 |
+ if (chv == (colorhist_vector) 0) |
|
|
198 |
{ |
216 |
- if (maxval == 255) |
199 |
- if (maxval == 255) |
217 |
- { |
200 |
+ if (bpp == 0) |
|
|
201 |
{ |
218 |
- Red[i] = PPM_GETR(chv[i].color); |
202 |
- Red[i] = PPM_GETR(chv[i].color); |
219 |
- Green[i] = PPM_GETG(chv[i].color); |
203 |
- Green[i] = PPM_GETG(chv[i].color); |
220 |
- Blue[i] = PPM_GETB(chv[i].color); |
204 |
- Blue[i] = PPM_GETB(chv[i].color); |
221 |
- } |
205 |
+ pm_message("over 256 colors found"); |
|
|
206 |
+ bpp = 24; |
207 |
} |
222 |
- else |
208 |
- else |
223 |
- { |
209 |
+ else if (bpp != 24) |
224 |
- Red[i] = (pixval) PPM_GETR(chv[i].color) * 255 / maxval; |
210 |
+ pm_error("too many colors - try doing a 'ppmquant %d'" |
225 |
- Green[i] = (pixval) PPM_GETG(chv[i].color) * 255 / maxval; |
211 |
+ , maxcolors); |
226 |
- Blue[i] = (pixval) PPM_GETB(chv[i].color) * 255 / maxval; |
212 |
+ } |
227 |
- } |
213 |
+ else |
228 |
- } |
214 |
+ { |
229 |
+ { |
215 |
+ pm_message("%d colors found", colors); |
230 |
+ if (bpp == 0) |
|
|
231 |
+ { |
232 |
+ pm_message("over 256 colors found"); |
233 |
+ bpp = 24; |
234 |
+ } |
235 |
+ else if (bpp != 24) |
236 |
+ pm_error("too many colors - try doing a 'ppmquant %d'" |
237 |
+ , maxcolors); |
238 |
+ } |
239 |
+ else |
240 |
+ { |
241 |
+ pm_message("%d colors found", colors); |
242 |
+ |
216 |
+ |
243 |
+ /* |
217 |
+ /* |
244 |
+ * I have found empirically at least one BMP-displaying program |
218 |
+ * I have found empirically at least one BMP-displaying program |
245 |
+ * that can't deal with (for instance) using 3 bits per pixel. |
219 |
+ * that can't deal with (for instance) using 3 bits per pixel. |
246 |
+ * I have seen no programs that can deal with using 3 bits per |
220 |
+ * I have seen no programs that can deal with using 3 bits per |
247 |
+ * pixel. I have seen programs which can deal with 1, 4, and |
221 |
+ * pixel. I have seen programs which can deal with 1, 4, and |
248 |
+ * 8 bits per pixel. |
222 |
+ * 8 bits per pixel. |
249 |
+ * |
223 |
+ * |
250 |
+ * Based on this, I adjust actual the number of bits per pixel |
224 |
+ * Based on this, I adjust actual the number of bits per pixel |
251 |
+ * as follows. If anyone knows better, PLEASE tell me! |
225 |
+ * as follows. If anyone knows better, PLEASE tell me! |
252 |
+ */ |
226 |
+ */ |
253 |
+ |
227 |
+ |
254 |
+ if (!bpp) |
228 |
+ if (!bpp) |
255 |
+ { |
229 |
+ { |
256 |
+ bpp = colorstobpp(colors); |
230 |
+ bpp = colorstobpp(colors); |
257 |
+ |
231 |
+ |
258 |
+ switch(bpp) |
232 |
+ switch(bpp) |
259 |
+ { |
233 |
+ { |
260 |
+ case 2: |
234 |
+ case 2: |
261 |
+ case 3: |
235 |
+ case 3: |
262 |
+ bpp = 4; |
236 |
+ bpp = 4; |
263 |
+ break; |
237 |
+ break; |
264 |
+ case 5: |
238 |
+ case 5: |
265 |
+ case 6: |
239 |
+ case 6: |
266 |
+ case 7: |
240 |
+ case 7: |
267 |
+ bpp = 8; |
241 |
+ bpp = 8; |
268 |
+ break; |
242 |
+ break; |
269 |
+ } |
243 |
+ } |
270 |
+ } |
244 |
+ } |
271 |
+ |
245 |
+ |
272 |
+ /* |
246 |
+ /* |
273 |
+ * Now turn the ppm colormap into the appropriate GIF |
247 |
+ * Now turn the ppm colormap into the appropriate GIF |
274 |
+ * colormap. |
248 |
+ * colormap. |
275 |
+ */ |
249 |
+ */ |
276 |
+ if (maxval > 255) |
250 |
+ if (maxval > 255) |
277 |
+ { |
251 |
+ { |
278 |
+ pm_message("maxval is not 255 - automatically rescaling colors"); |
252 |
+ pm_message("maxval is not 255 - automatically rescaling colors"); |
279 |
+ } |
253 |
+ } |
280 |
+ for (i = 0; i < colors; ++i) |
254 |
+ for (i = 0; i < colors; ++i) |
281 |
+ { |
255 |
{ |
282 |
+ if (maxval == 255) |
256 |
- Red[i] = (pixval) PPM_GETR(chv[i].color) * 255 / maxval; |
283 |
+ { |
257 |
- Green[i] = (pixval) PPM_GETG(chv[i].color) * 255 / maxval; |
284 |
+ Red[i] = PPM_GETR(chv[i].color); |
258 |
- Blue[i] = (pixval) PPM_GETB(chv[i].color) * 255 / maxval; |
285 |
+ Green[i] = PPM_GETG(chv[i].color); |
259 |
+ if (maxval == 255) |
286 |
+ Blue[i] = PPM_GETB(chv[i].color); |
260 |
+ { |
287 |
+ } |
261 |
+ Red[i] = PPM_GETR(chv[i].color); |
288 |
+ else |
262 |
+ Green[i] = PPM_GETG(chv[i].color); |
289 |
+ { |
263 |
+ Blue[i] = PPM_GETB(chv[i].color); |
290 |
+ Red[i] = (pixval) PPM_GETR(chv[i].color) * 255 / maxval; |
264 |
+ } |
291 |
+ Green[i] = (pixval) PPM_GETG(chv[i].color) * 255 / maxval; |
265 |
+ else |
292 |
+ Blue[i] = (pixval) PPM_GETB(chv[i].color) * 255 / maxval; |
266 |
+ { |
293 |
+ } |
267 |
+ Red[i] = (pixval) PPM_GETR(chv[i].color) * 255 / maxval; |
294 |
+ } |
268 |
+ Green[i] = (pixval) PPM_GETG(chv[i].color) * 255 / maxval; |
|
|
269 |
+ Blue[i] = (pixval) PPM_GETB(chv[i].color) * 255 / maxval; |
270 |
+ } |
271 |
} |
295 |
+ |
272 |
+ |
296 |
+ /* And make a hash table for fast lookup. */ |
273 |
+ /* And make a hash table for fast lookup. */ |
297 |
+ cht = ppm_colorhisttocolorhash(chv, colors); |
274 |
+ cht = ppm_colorhisttocolorhash(chv, colors); |
298 |
+ ppm_freecolorhist(chv); |
275 |
+ ppm_freecolorhist(chv); |
299 |
+ } |
276 |
} |
300 |
|
277 |
|
301 |
- /* And make a hash table for fast lookup. */ |
278 |
- /* And make a hash table for fast lookup. */ |
302 |
- cht = ppm_colorhisttocolorhash(chv, colors); |
279 |
- cht = ppm_colorhisttocolorhash(chv, colors); |
303 |
- ppm_freecolorhist(chv); |
280 |
- ppm_freecolorhist(chv); |
304 |
+ pm_message("Using %d bits per pixel", bpp); |
281 |
+ pm_message("Using %d bits per pixel", bpp); |
305 |
|
282 |
|
306 |
/* All set, let's do it. */ |
283 |
/* All set, let's do it. */ |
307 |
BMPEncode(stdout, class |
284 |
BMPEncode(stdout, class |