aboutsummaryrefslogtreecommitdiffstats
path: root/graphics/netpbm/files/patch-au
blob: 2b4a3c967dfb5cf5764149cf6d045010b54ca3d1 (plain) (blame)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
--- ppm/ppmtobmp.c.orig Mon Oct  4 18:12:36 1993
+++ ppm/ppmtobmp.c  Sat Oct 16 02:53:18 1999
@@ -75,7 +75,7 @@
     unsigned short cBitCount, pixel **pixels, colorhash_table cht));
 static int colorstobpp ARGS((int colors));
 static void BMPEncode ARGS((FILE *fp, int class, int x, int y, pixel **pixels,
-    int colors, colorhash_table cht, pixval *R, pixval *G, pixval *B));
+    int bpp, int colors, colorhash_table cht, pixval *R, pixval *G, pixval *B));
 static void
 PutByte(fp, v)
         FILE           *fp;
@@ -253,19 +253,23 @@
         int             i;
         long            ncolors;
 
-        for (i = 0; i < colors; i++)
-        {
-                nbyte += BMPwritergb(fp,class,R[i],G[i],B[i]);
-        }
-
-        ncolors = (1 << bpp);
+   if (bpp != 24)
+   {
 
-        for (; i < ncolors; i++)
-        {
-                nbyte += BMPwritergb(fp,class,0,0,0);
-        }
+       for (i = 0; i < colors; i++)
+       {
+           nbyte += BMPwritergb(fp,class,R[i],G[i],B[i]);
+       }
+
+       ncolors = (1 << bpp);
+
+       for (; i < ncolors; i++)
+       {
+           nbyte += BMPwritergb(fp,class,0,0,0);
+       }
+   }
 
-        return nbyte;
+   return nbyte;
 }
 
 /*
@@ -284,26 +288,38 @@
         int             rc;
         unsigned        x;
 
-        if ((b = pm_bitinit(fp, "w")) == (BITSTREAM) 0)
-        {
-                return -1;
-        }
-
-        for (x = 0; x < cx; x++, row++)
-        {
-                if ((rc = pm_bitwrite(b, bpp, ppm_lookupcolor(cht, row))) == -1)
-                {
-                        return -1;
-                }
-                nbyte += rc;
-        }
-
-        if ((rc = pm_bitfini(b)) == -1)
-        {
-                return -1;
-        }
-        nbyte += rc;
-
+   if (bpp != 24)
+   {
+       if ((b = pm_bitinit(fp, "w")) == (BITSTREAM) 0)
+       {
+           return -1;
+       }
+
+       for (x = 0; x < cx; x++, row++)
+       {
+           if ((rc = pm_bitwrite(b, bpp, ppm_lookupcolor(cht, row))) == -1)
+           {
+               return -1;
+           }
+           nbyte += rc;
+       }
+
+       if ((rc = pm_bitfini(b)) == -1)
+       {
+           return -1;
+       }
+       nbyte += rc;
+   }
+   else
+   {
+       for (x = 0; x < cx; x++, row++)
+       {
+           PutByte(fp, PPM_GETB(*row));
+           PutByte(fp, PPM_GETG(*row));
+           PutByte(fp, PPM_GETR(*row));
+           nbyte += 3;
+       }
+   }
         /*
          * Make sure we write a multiple of 4 bytes.
          */
@@ -394,48 +410,21 @@
  * arrays are undefined.
  */
 static void
-BMPEncode(fp, class, x, y, pixels, colors, cht, R, G, B)
+BMPEncode(fp, class, x, y, pixels, bpp, colors, cht, R, G, B)
         FILE           *fp;
         int             class;
         int             x;
         int             y;
         pixel         **pixels;
+   int     bpp;    /* bits per pixel */
         int             colors; /* number of valid entries in R,G,B */
         colorhash_table cht;
         pixval         *R;
         pixval         *G;
         pixval         *B;
 {
-        int             bpp;    /* bits per pixel */
         unsigned long   nbyte = 0;
 
-        bpp = colorstobpp(colors);
-
-        /*
-         * I have found empirically at least one BMP-displaying program
-         * that can't deal with (for instance) using 3 bits per pixel.
-         * I have seen no programs that can deal with using 3 bits per
-         * pixel.  I have seen programs which can deal with 1, 4, and
-         * 8 bits per pixel.
-         *
-         * Based on this, I adjust actual the number of bits per pixel
-         * as follows.  If anyone knows better, PLEASE tell me!
-         */
-        switch(bpp)
-        {
-        case 2:
-        case 3:
-                bpp = 4;
-                break;
-        case 5:
-        case 6:
-        case 7:
-                bpp = 8;
-                break;
-        }
-
-        pm_message("Using %d bits per pixel", bpp);
-
         nbyte += BMPwritefileheader(fp, class, bpp, x, y);
         nbyte += BMPwriteinfoheader(fp, class, bpp, x, y);
         nbyte += BMPwritergbtable(fp, class, bpp, colors, R, G, B);
@@ -517,13 +506,15 @@
         char          **argv;
 {
         FILE           *ifp = stdin;
-        char           *usage = "[-windows] [-os2] [ppmfile]";
+        char           *usage = "[-windows] [-os2] [-1/4/8/24bits] [ppmfile]";
         int             class = C_OS2;
 
         int             argn;
         int             rows;
         int             cols;
         int             colors;
+   int     maxcolors = MAXCOLORS;
+        int        bpp = 0;
         int             i;
         pixval          maxval;
         colorhist_vector chv;
@@ -545,6 +536,14 @@
                         class = C_WIN;
                 else if (pm_keymatch(argv[argn], "-os2", 2))
                         class = C_OS2;
+                else if (pm_keymatch(argv[argn], "-24bits", 3))
+                        bpp = 24, maxcolors = 256;
+                else if (pm_keymatch(argv[argn], "-8bits", 2))
+                        bpp = 8, maxcolors = 256;
+                else if (pm_keymatch(argv[argn], "-4bits", 2))
+                        bpp = 4, maxcolors = 16;
+                else if (pm_keymatch(argv[argn], "-1bit", 2))
+                        bpp = 1, maxcolors = 2;
                 else
                         pm_usage(usage);
                 ++argn;
@@ -585,44 +584,90 @@
 #endif
 
         /* Figure out the colormap. */
-        pm_message("computing colormap...");
-        chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors);
+   chv = (colorhist_vector) 0;
+        if(bpp != 24)
+   {
+       pm_message("computing colormap...");
+       chv = ppm_computecolorhist(pixels, cols, rows, maxcolors, &colors);
+   }
         if (chv == (colorhist_vector) 0)
-                pm_error("too many colors - try doing a 'ppmquant %d'"
-                        , MAXCOLORS);
-        pm_message("%d colors found", colors);
-
-        /*
-         * Now turn the ppm colormap into the appropriate GIF
-         * colormap.
-         */
-        if (maxval > 255)
-        {
-                pm_message("maxval is not 255 - automatically rescaling colors");
-        }
-        for (i = 0; i < colors; ++i)
-        {
-                if (maxval == 255)
-                {
-                        Red[i] = PPM_GETR(chv[i].color);
-                        Green[i] = PPM_GETG(chv[i].color);
-                        Blue[i] = PPM_GETB(chv[i].color);
-                }
-                else
-                {
-                        Red[i] = (pixval) PPM_GETR(chv[i].color) * 255 / maxval;
-                        Green[i] = (pixval) PPM_GETG(chv[i].color) * 255 / maxval;
-                        Blue[i] = (pixval) PPM_GETB(chv[i].color) * 255 / maxval;
-                }
-        }
+   {
+       if (bpp == 0)
+       {
+           pm_message("over 256 colors found");
+           bpp = 24;
+       }
+       else if (bpp != 24)
+           pm_error("too many colors - try doing a 'ppmquant %d'"
+               , maxcolors);
+   }
+   else
+   {
+       pm_message("%d colors found", colors);
+
+       /*
+        * I have found empirically at least one BMP-displaying program
+        * that can't deal with (for instance) using 3 bits per pixel.
+        * I have seen no programs that can deal with using 3 bits per
+        * pixel.  I have seen programs which can deal with 1, 4, and
+        * 8 bits per pixel.
+        *
+        * Based on this, I adjust actual the number of bits per pixel
+        * as follows.  If anyone knows better, PLEASE tell me!
+        */
+
+       if (!bpp)
+       {
+           bpp = colorstobpp(colors);
+
+           switch(bpp)
+           {
+           case 2:
+           case 3:
+               bpp = 4;
+               break;
+           case 5:
+           case 6:
+           case 7:
+               bpp = 8;
+               break;
+           }
+       }
+
+       /*
+        * Now turn the ppm colormap into the appropriate GIF
+        * colormap.
+        */
+       if (maxval > 255)
+       {
+           pm_message("maxval is not 255 - automatically rescaling colors");
+       }
+       for (i = 0; i < colors; ++i)
+       {
+           if (maxval == 255)
+           {
+               Red[i] = PPM_GETR(chv[i].color);
+               Green[i] = PPM_GETG(chv[i].color);
+               Blue[i] = PPM_GETB(chv[i].color);
+           }
+           else
+           {
+               Red[i] = (pixval) PPM_GETR(chv[i].color) * 255 / maxval;
+               Green[i] = (pixval) PPM_GETG(chv[i].color) * 255 / maxval;
+               Blue[i] = (pixval) PPM_GETB(chv[i].color) * 255 / maxval;
+           }
+       }
+
+       /* And make a hash table for fast lookup. */
+       cht = ppm_colorhisttocolorhash(chv, colors);
+       ppm_freecolorhist(chv);
+   }
 
-        /* And make a hash table for fast lookup. */
-        cht = ppm_colorhisttocolorhash(chv, colors);
-        ppm_freecolorhist(chv);
+   pm_message("Using %d bits per pixel", bpp);
 
         /* All set, let's do it. */
         BMPEncode(stdout, class
-                , cols, rows, pixels, colors, cht
+                , cols, rows, pixels, bpp, colors, cht
                 ,Red, Green, Blue);
 
         pm_close(stdout);