Debian Codegraph @

src/bmp.c

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17 // 16-bit color masks and shifts, default is 5-5-5
18 static uint32_t masks[3] = { 0x7c00, 0x3e0, 0x1f };
19 static uint32_t shifts[3] = { 10, 5, 0 };
20 static uint32_t ncolors[3] = { (1 << 5) - 1, (1 << 5) - 1, (1 << 5) - 1 };
21
22 int
23 image_bmp_read_header(image *im)
24 {
25 int offset, palette_colors;
26
27 buffer_consume(im->buf, 10);
28
29 offset = buffer_get_int_le(im->buf);
30 buffer_consume(im->buf, 4);
31 im->width = buffer_get_int_le(im->buf);
32 im->height = buffer_get_int_le(im->buf);
33 buffer_consume(im->buf, 2);
34 im->bpp = buffer_get_short_le(im->buf);
35 im->compression = buffer_get_int_le(im->buf);
36
37 DEBUG_TRACE("BMP offset %d, width %d, height %d, bpp %d, compression %d\n",
38 offset, im->width, im->height, im->bpp, im->compression);
39
40 if (im->compression > 3) { // JPEG/PNG
41 warn("Image::Scale unsupported BMP compression type: %d (%s)\n", im->compression, SvPVX(im->path));
42 return 0;
43 }
44
45 // Negative height indicates a flipped image
46 if (im->height < 0) {
47 croak("flipped\n");
48 im->flipped = 1;
49 im->height = abs(im->height);
50 }
51
52 // Not used during reading, but lets output PNG be correct
53 im->channels = 4;
54
55 // Skip BMP size, resolution
56 buffer_consume(im->buf, 12);
57
58 palette_colors = buffer_get_int_le(im->buf);
59
60 // Skip number of important colors
61 buffer_consume(im->buf, 4);
62
63 // < 16-bit always has a palette
64 if (!palette_colors && im->bpp < 16) {
65 switch (im->bpp) {
66 case 8:
67 palette_colors = 256;
68 break;
69 case 4:
70 palette_colors = 16;
71 break;
72 case 1:
73 palette_colors = 2;
74 break;
75 }
76 }
77
78 DEBUG_TRACE("palette_colors %d\n", palette_colors);
79 if (palette_colors) {
80 // Read palette
81 int i;
82
83 if (palette_colors > 256) {
84 warn("Image::Scale cannot read BMP with palette > 256 colors (%s)\n", SvPVX(im->path));
85 return 0;
86 }
87
88 New(0, im->palette, 1, palette);
89
90 for (i = 0; i < palette_colors; i++) {
91 int b = buffer_get_char(im->buf);
92 int g = buffer_get_char(im->buf);
93 int r = buffer_get_char(im->buf);
94 buffer_consume(im->buf, 1);
95
96 im->palette->colors[i] = COL(r, g, b);
97 DEBUG_TRACE("palette %d = %08x\n", i, im->palette->colors[i]);
98 }
99 }
100 else if (im->compression == BMP_BI_BITFIELDS) {
101 int pos, bit, i;
102
103 if (im->bpp == 16) {
104 // Read 16-bit bitfield masks
105 for (i = 0; i < 3; i++) {
106 masks[i] = buffer_get_int_le(im->buf);
107
108 // Determine shift value
109 pos = 0;
110 bit = masks[i] & -masks[i];
111 while (bit) {
112 pos++;
113 bit >>= 1;
114 }
115 shifts[i] = pos - 1;
116
117 // green can be 6 bits
118 if (i == 1) {
119 if (masks[1] == 0x7e0)
120 ncolors[1] = (1 << 6) - 1;
121 else
122 ncolors[1] = (1 << 5) - 1;
123 }
124
125 DEBUG_TRACE("16bpp mask %d: %08x >> %d, ncolors %d\n", i, masks[i], shifts[i], ncolors[i]);
126 }
127 }
128 else { // 32-bit bitfields
129 // Read 32-bit bitfield masks
130 for (i = 0; i < 3; i++) {
131 masks[i] = buffer_get_int_le(im->buf);
132
133 // Determine shift value
134 pos = 0;
135 bit = masks[i] & -masks[i];
136 while (bit) {
137 pos++;
138 bit >>= 1;
139 }
140 shifts[i] = pos - 1;
141
142 DEBUG_TRACE("32bpp mask %d: %08x >> %d\n", i, masks[i], shifts[i]);
143 }
144 }
145 }
146
147 // XXX make sure to skip to offset
148
149 return 1;
150 }
151
152 int
153 image_bmp_load(image *im)
154 {
155 int offset = 0;
156 int paddingbits = 0;
157 int mask = 0;
158 int i, x, y, blen;
159 int starty, lasty, incy, linebytes;
160 unsigned char *bptr;
161
162 // If reusing the object a second time, reset buffer
163 if (im->used) {
164 DEBUG_TRACE("Resetting BMP state\n");
165 image_bmp_finish(im);
166
167 buffer_clear(im->buf);
168
169 if (im->fh != NULL) {
170 // reset file to begining of image
171 PerlIO_seek(im->fh, im->image_offset, SEEK_SET);
172
173 if ( !_check_buf(im->fh, im->buf, 8, BUFFER_SIZE) ) {
174 warn("Image::Scale unable to read BMP header (%s)\n", SvPVX(im->path));
175 image_bmp_finish(im);
176 return 0;
177 }
178 }
179 else {
180 // reset SV read
181 im->sv_offset = MIN(sv_len(im->sv_data) - im->image_offset, BUFFER_SIZE);
182 buffer_append(im->buf, SvPVX(im->sv_data) + im->image_offset, im->sv_offset);
183 }
184
185 image_bmp_read_header(im);
186 }
187
188 // Calculate bits of padding per line
189 paddingbits = 32 - (im->width * im->bpp) % 32;
190 if (paddingbits == 32)
191 paddingbits = 0;
192
193 // Bytes per line
194 linebytes = ((im->width * im->bpp) + paddingbits) / 8;
195
196 // No padding if RLE compressed
197 if (paddingbits && (im->compression == BMP_BI_RLE4 || im->compression == BMP_BI_RLE8))
198 paddingbits = 0;
199
200 // XXX Don't worry about RLE support yet
201 if (im->compression == BMP_BI_RLE4 || im->compression == BMP_BI_RLE8) {
202 warn("Image::Scale does not support BMP RLE compression yet\n");
203 image_bmp_finish(im);
204 return 0;
205 }
206
207 DEBUG_TRACE("linebits %d, paddingbits %d, linebytes %d\n", im->width * im->bpp, paddingbits, linebytes);
208
209 bptr = buffer_ptr(im->buf);
210 blen = buffer_len(im->buf);
211
212 // Allocate storage for decompressed image
213 image_alloc(im, im->width, im->height);
214
215 if (im->flipped) {
216 starty = 0;
217 lasty = im->height;
218 incy = 1;
219 }
220 else {
221 starty = im->height - 1;
222 lasty = -1;
223 incy = -1;
224 }
225
226 y = starty;
227
228 if (im->bpp == 1)
229 mask = 0x80;
230 else if (im->bpp == 4)
231 mask = 0xF0;
232
233 while (y != lasty) {
234 for (x = 0; x < im->width; x++) {
235 if (blen <= 0 || blen < im->bpp / 8) {
236 // Load more from the buffer
237 if (blen < 0)
238 blen = 0;
239
240 buffer_consume(im->buf, buffer_len(im->buf) - blen);
241
242 if (im->fh != NULL) {
243 // Read from file
244 if ( !_check_buf(im->fh, im->buf, im->channels, 8192) ) {
245 image_bmp_finish(im);
246 warn("Image::Scale unable to read entire BMP file (%s)\n", SvPVX(im->path));
247 return 0;
248 }
249 }
250 else {
251 // Read from scalar
252 int svbuflen = MIN(sv_len(im->sv_data) - im->sv_offset, 8192);
253
254 if (!svbuflen) {
255 image_bmp_finish(im);
256 warn("Image::Scale unable to read entire BMP file (%s)\n", SvPVX(im->path));
257 return 0;
258 }
259 buffer_append(im->buf, SvPVX(im->sv_data) + im->sv_offset, svbuflen);
260 im->sv_offset += svbuflen;
261 }
262
263 bptr = buffer_ptr(im->buf);
264 blen = buffer_len(im->buf);
265 offset = 0;
266 }
267
268 i = x + (y * im->width);
269
270 switch (im->bpp) {
271 case 32: // XXX how to detect alpha channel?
272 //im->pixbuf[i] = COL_FULL(bptr[offset+2], bptr[offset+1], bptr[offset], bptr[offset+3]);
273 im->pixbuf[i] = COL(bptr[offset+2], bptr[offset+1], bptr[offset]);
274 offset += 4;
275 blen -= 4;
276 linebytes -= 4;
277 break;
278
279 case 24: // 24-bit BGR
280 im->pixbuf[i] = COL(bptr[offset+2], bptr[offset+1], bptr[offset]);
281 offset += 3;
282 blen -= 3;
283 linebytes -= 3;
284 break;
285
286 case 16:
287 {
288 int p = (bptr[offset+1] << 8) | bptr[offset];
289 DEBUG_TRACE("p %x (r %02x g %02x b %02x)\n", p,
290 ((p & masks[0]) >> shifts[0]) * 255 / ncolors[0],
291 ((p & masks[1]) >> shifts[1]) * 255 / ncolors[1],
292 ((p & masks[2]) >> shifts[2]) * 255 / ncolors[2]);
293
294 im->pixbuf[i] = COL(
295 ((p & masks[0]) >> shifts[0]) * 255 / ncolors[0],
296 ((p & masks[1]) >> shifts[1]) * 255 / ncolors[1],
297 ((p & masks[2]) >> shifts[2]) * 255 / ncolors[2]
298 );
299
300 offset += 2;
301 blen -= 2;
302 linebytes -= 2;
303 break;
304 }
305
306 case 8:
307 im->pixbuf[i] = im->palette->colors[ bptr[offset] ];
308 offset++;
309 blen--;
310 linebytes--;
311 break;
312
313 case 4:
314 // uncompressed
315 if (mask == 0xF0) {
316 im->pixbuf[i] = im->palette->colors[ (bptr[offset] & mask) >> 4 ];
317 mask = 0xF;
318 }
319 else {
320 im->pixbuf[i] = im->palette->colors[ (bptr[offset] & mask) ];
321 offset++;
322 blen--;
323 linebytes--;
324 mask = 0xF0;
325 }
326 break;
327
328 case 1:
329 im->pixbuf[i] = im->palette->colors[ (bptr[offset] & mask) ? 1 : 0 ];
330 mask >>= 1;
331 if (!mask) {
332 offset++;
333 blen--;
334 linebytes--;
335 mask = 0x80;
336 }
337 break;
338 }
339
340 DEBUG_TRACE("x %d / y %d, linebytes left %d, pix %08x\n", x, y, linebytes, im->pixbuf[i]);
341 }
342
343 if (linebytes) {
344 DEBUG_TRACE("Consuming %d bytes of padding\n", linebytes);
345
346 if (blen < linebytes) {
347 // Load more from the buffer
348 buffer_consume(im->buf, buffer_len(im->buf) - blen);
349 if ( !_check_buf(im->fh, im->buf, im->channels, 8192) ) {
350 image_bmp_finish(im);
351 warn("Image::Scale unable to read entire BMP file (%s)\n", SvPVX(im->path));
352 return 0;
353 }
354 bptr = buffer_ptr(im->buf);
355 blen = buffer_len(im->buf);
356 offset = 0;
357 }
358
359 offset += linebytes;
360 blen -= linebytes;
361
362 // Reset mask for next line
363 if (im->bpp == 4)
364 mask = 0xF0;
365 else if (im->bpp == 1)
366 mask = 0x80;
367 }
368
369 linebytes = ((im->width * im->bpp) + paddingbits) / 8;
370
371 y += incy;
372 }
373
374 // Set channels to 4 so we write a color PNG, unless bpp is 1
375 if (im->bpp > 1)
376 im->channels = 4;
377
378 return 1;
379 }
380
381 void
382 image_bmp_finish(image *im)
383 {
384 if (im->palette != NULL) {
385 Safefree(im->palette);
386 im->palette = NULL;
387 }
388 }