more green seen as everyone has a green fetish atm
[Rockbox.git] / apps / recorder / bmp.c
blob7b6f6291c87a47e08515a635382acc1686b52708
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 2005-04-16 Tomas Salfischberger:
22 - New BMP loader function, based on the old one (borrowed a lot of
23 calculations and checks there.)
24 - Conversion part needs some optimization, doing unneeded calulations now.
25 2006-11-18 Jens Arnold: complete rework
26 - All canonical formats supported now (1, 4, 8, 15/16, 24 and 32 bit)
27 - better protection against malformed / non-standard BMPs
28 - code heavily optimised for both size and speed
29 - dithering for 2 bit targets
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "inttypes.h"
36 #include "debug.h"
37 #include "lcd.h"
38 #ifdef HAVE_REMOTE_LCD
39 #include "lcd-remote.h"
40 #endif
41 #include "file.h"
42 #include "config.h"
43 #include "system.h"
44 #include "bmp.h"
45 #include "lcd.h"
47 #ifdef __GNUC__
48 #define STRUCT_PACKED __attribute__((packed))
49 #else
50 #define STRUCT_PACKED
51 #pragma pack (push, 2)
52 #endif
54 /* BMP header structure */
55 struct bmp_header {
56 uint16_t type; /* signature - 'BM' */
57 uint32_t size; /* file size in bytes */
58 uint16_t reserved1; /* 0 */
59 uint16_t reserved2; /* 0 */
60 uint32_t off_bits; /* offset to bitmap */
61 uint32_t struct_size; /* size of this struct (40) */
62 int32_t width; /* bmap width in pixels */
63 int32_t height; /* bmap height in pixels */
64 uint16_t planes; /* num planes - always 1 */
65 uint16_t bit_count; /* bits per pixel */
66 uint32_t compression; /* compression flag */
67 uint32_t size_image; /* image size in bytes */
68 int32_t x_pels_per_meter; /* horz resolution */
69 int32_t y_pels_per_meter; /* vert resolution */
70 uint32_t clr_used; /* 0 -> color table size */
71 uint32_t clr_important; /* important color count */
72 } STRUCT_PACKED;
74 union rgb_union {
75 struct { /* Little endian */
76 unsigned char blue;
77 unsigned char green;
78 unsigned char red;
79 unsigned char reserved;
81 uint32_t raw;
84 /* masks for supported BI_BITFIELDS encodings (16/32 bit), little endian */
85 static const unsigned char bitfields[3][12] = {
86 { 0x00,0x7c,0x00,0, 0xe0,0x03,0x00,0, 0x1f,0x00,0x00,0 }, /* 15 bit */
87 { 0x00,0xf8,0x00,0, 0xe0,0x07,0x00,0, 0x1f,0x00,0x00,0 }, /* 16 bit */
88 { 0x00,0x00,0xff,0, 0x00,0xff,0x00,0, 0xff,0x00,0x00,0 }, /* 32 bit */
91 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
92 /* canonical ordered dither matrix */
93 static const unsigned char dither_matrix[16][16] = {
94 { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 },
95 { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
96 { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
97 { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
98 { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 },
99 { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
100 { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
101 { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
102 { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 },
103 { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
104 { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
105 { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
106 { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 },
107 { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
108 { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
109 { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
111 #endif
113 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH == 2) \
114 && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED)
115 static const fb_remote_data remote_pattern[4] = {
116 0x0101, 0x0100, 0x0001, 0x0000
118 #endif
120 /* little endian functions */
121 static inline unsigned readshort(uint16_t *value)
123 unsigned char* bytes = (unsigned char*) value;
124 return (unsigned)bytes[0] | ((unsigned)bytes[1] << 8);
127 static inline uint32_t readlong(uint32_t *value)
129 unsigned char* bytes = (unsigned char*) value;
130 return (uint32_t)bytes[0] | ((uint32_t)bytes[1] << 8) |
131 ((uint32_t)bytes[2] << 16) | ((uint32_t)bytes[3] << 24);
134 static inline unsigned brightness(union rgb_union color)
136 return (3 * (unsigned)color.red + 6 * (unsigned)color.green
137 + (unsigned)color.blue) / 10;
140 /******************************************************************************
141 * read_bmp_file()
143 * Reads a BMP file and puts the data in rockbox format in *bitmap.
145 *****************************************************************************/
146 int read_bmp_file(const char* filename,
147 struct bitmap *bm,
148 int maxsize,
149 int format)
151 int fd, ret;
152 fd = open(filename, O_RDONLY);
154 /* Exit if file opening failed */
155 if (fd < 0) {
156 DEBUGF("read_bmp_file: can't open '%s', rc: %d\n", filename, fd);
157 return fd * 10 - 1;
160 ret = read_bmp_fd(fd, bm, maxsize, format);
161 close(fd);
162 return ret;
165 /******************************************************************************
166 * read_bmp_fd()
168 * Reads a BMP file in an open file descriptor and puts the data in rockbox
169 * format in *bitmap.
171 *****************************************************************************/
172 int read_bmp_fd(int fd,
173 struct bitmap *bm,
174 int maxsize,
175 int format)
177 struct bmp_header bmph;
178 int width, height, padded_width;
179 int dst_height, dst_width;
180 int depth, numcolors, compression, totalsize;
181 int row, col, ret;
182 int rowstart, rowstop, rowstep;
184 unsigned char *bitmap = bm->data;
185 uint32_t bmpbuf[LCD_WIDTH]; /* Buffer for one line */
186 uint32_t palette[256];
187 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
188 bool transparent = false;
189 bool dither = false;
190 #ifdef HAVE_REMOTE_LCD
191 bool remote = false;
193 if (format & FORMAT_REMOTE) {
194 remote = true;
195 #if LCD_REMOTE_DEPTH == 1
196 format = FORMAT_MONO;
197 #else
198 format &= ~FORMAT_REMOTE;
199 #endif
201 #endif /* HAVE_REMOTE_LCD */
202 if (format & FORMAT_TRANSPARENT) {
203 transparent = true;
204 format &= ~FORMAT_TRANSPARENT;
206 if (format & FORMAT_DITHER) {
207 dither = true;
208 format &= ~FORMAT_DITHER;
210 #else
212 (void)format;
213 #endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */
215 /* read fileheader */
216 ret = read(fd, &bmph, sizeof(struct bmp_header));
217 if (ret < 0) {
218 return ret * 10 - 2;
221 if (ret != sizeof(struct bmp_header)) {
222 DEBUGF("read_bmp_fd: can't read BMP header.");
223 return -3;
226 width = readlong(&bmph.width);
227 if (width > LCD_WIDTH) {
228 DEBUGF("read_bmp_fd: Bitmap too wide (%d pixels, max is %d)\n",
229 width, LCD_WIDTH);
230 return -4;
233 height = readlong(&bmph.height);
234 if (height < 0) { /* Top-down BMP file */
235 height = -height;
236 rowstart = 0;
237 rowstop = height;
238 rowstep = 1;
239 } else { /* normal BMP */
240 rowstart = height - 1;
241 rowstop = -1;
242 rowstep = -1;
245 depth = readshort(&bmph.bit_count);
246 padded_width = ((width * depth + 31) >> 3) & ~3; /* 4-byte boundary aligned */
248 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
249 if (format == FORMAT_ANY) {
250 if (depth == 1)
251 format = FORMAT_MONO;
252 else
253 format = FORMAT_NATIVE;
255 bm->format = format;
256 #endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */
257 /* returning image size */
258 bm->width = width;
259 bm->height = height;
261 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
262 if (format == FORMAT_NATIVE) {
263 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
264 if (remote) {
265 #if (LCD_REMOTE_DEPTH == 2) && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED)
266 dst_width = width;
267 dst_height = (height + 7) >> 3;
268 #endif /* LCD_REMOTE_DEPTH / LCD_REMOTE_PIXELFORMAT */
269 totalsize = dst_width * dst_height * sizeof(fb_remote_data);
270 } else
271 #endif /* defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1 */
273 #if LCD_DEPTH == 2
274 #if LCD_PIXELFORMAT == VERTICAL_PACKING
275 dst_width = width;
276 dst_height = (height + 3) >> 2;
277 #else /* LCD_PIXELFORMAT == HORIZONTAL_PACKING */
278 dst_width = (width + 3) >> 2;
279 dst_height = height;
280 #endif /* LCD_PIXELFORMAT */
281 #elif LCD_DEPTH == 16
282 dst_width = width;
283 dst_height = height;
284 #endif /* LCD_DEPTH */
285 totalsize = dst_width * dst_height * sizeof(fb_data);
287 } else
288 #endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */
290 dst_width = width;
291 dst_height = (height + 7) >> 3;
292 totalsize = dst_width * dst_height;
295 /* Check if this fits the buffer */
296 if (totalsize > maxsize) {
297 DEBUGF("read_bmp_fd: Bitmap too large for buffer: "
298 "%d bytes.\n", totalsize);
299 return -6;
302 compression = readlong(&bmph.compression);
303 if (depth <= 8) {
304 numcolors = readlong(&bmph.clr_used);
305 if (numcolors == 0)
306 numcolors = 1 << depth;
307 } else
308 numcolors = (compression == 3) ? 3 : 0;
310 if (numcolors > 0 && numcolors <= 256) {
311 if (read(fd, palette, numcolors * sizeof(uint32_t))
312 != numcolors * (int)sizeof(uint32_t))
314 DEBUGF("read_bmp_fd: Can't read color palette\n");
315 return -7;
319 switch (depth) {
320 case 16:
321 #if LCD_DEPTH >= 16
322 /* don't dither 16 bit BMP to LCD with same or larger depth */
323 #ifdef HAVE_REMOTE_LCD
324 if (!remote)
325 #endif
326 dither = false;
327 #endif
328 if (compression == 0) { /* BI_RGB, i.e. 15 bit */
329 depth = 15;
330 break;
331 } /* else fall through */
333 case 32:
334 if (compression == 3) { /* BI_BITFIELDS */
335 if (!memcmp(palette, bitfields[0], 12)) { /* 15 bit */
336 depth = 15;
337 break;
339 if (!memcmp(palette, bitfields[1], 12) /* 16 bit */
340 || !memcmp(palette, bitfields[2], 12)) /* 32 bit */
342 break;
344 } /* else fall through */
346 default:
347 if (compression != 0) { /* not BI_RGB */
348 DEBUGF("read_bmp_fd: Unsupported compression (type %d)\n",
349 compression);
350 return -8;
352 break;
355 /* Search to the beginning of the image data */
356 lseek(fd, (off_t)readlong(&bmph.off_bits), SEEK_SET);
358 memset(bitmap, 0, totalsize);
360 /* loop to read rows and put them to buffer */
361 for (row = rowstart; row != rowstop; row += rowstep) {
362 unsigned data, mask;
363 unsigned char *p;
364 uint16_t *p2;
365 uint32_t *rp;
366 union rgb_union *qp;
367 union rgb_union q0, q1;
369 /* read one row */
370 ret = read(fd, bmpbuf, padded_width);
371 if (ret != padded_width) {
372 DEBUGF("read_bmp_fd: error reading image, read returned: %d "
373 "expected: %d\n", ret, padded_width);
374 return -9;
377 /* convert whole line in-place to XRGB8888 (little endian) */
378 rp = bmpbuf + width;
379 switch (depth) {
380 case 1:
381 q0.raw = palette[0];
382 q1.raw = palette[1];
383 p = (unsigned char*)bmpbuf + ((width + 7) >> 3);
384 mask = 0x80 >> ((width + 7) & 7);
385 while (p > (unsigned char*)bmpbuf) {
386 data = *(--p);
387 for (; mask <= 0x80; mask <<= 1)
388 *(--rp) = (data & mask) ? q1.raw : q0.raw;
389 mask = 0x01;
391 break;
393 case 4:
394 if (width & 1)
395 rp++;
396 p = (unsigned char*)bmpbuf + ((width + 1) >> 1);
397 while (p > (unsigned char*)bmpbuf) {
398 data = *(--p);
399 *(--rp) = palette[data & 0x0f];
400 *(--rp) = palette[data >> 4];
402 break;
404 case 8:
405 p = (unsigned char*)bmpbuf + width;
406 while (p > (unsigned char*)bmpbuf)
407 *(--rp) = palette[*(--p)];
408 break;
410 case 15:
411 case 16:
412 p2 = (uint16_t *)bmpbuf + width;
413 while (p2 > (uint16_t *)bmpbuf) {
414 unsigned component, rgb;
416 data = letoh16(*(--p2));
417 /* blue */
418 component = (data << 3) & 0xf8;
419 #ifdef ROCKBOX_BIG_ENDIAN
420 rgb = (component | (component >> 5)) << 8;
421 /* green */
422 data >>= 2;
423 if (depth == 15) {
424 component = data & 0xf8;
425 rgb |= component | (component >> 5);
426 } else {
427 data >>= 1;
428 component = data & 0xfc;
429 rgb |= component | (component >> 6);
431 /* red */
432 data >>= 5;
433 component = data & 0xf8;
434 rgb = (rgb << 8) | component | (component >> 5);
435 *(--rp) = rgb << 8;
436 #else /* little endian */
437 rgb = component | (component >> 5);
438 /* green */
439 data >>= 2;
440 if (depth == 15) {
441 component = data & 0xf8;
442 rgb |= (component | (component >> 5)) << 8;
443 } else {
444 data >>= 1;
445 component = data & 0xfc;
446 rgb |= (component | (component >> 6)) << 8;
448 /* red */
449 data >>= 5;
450 component = data & 0xf8;
451 rgb |= (component | (component >> 5)) << 16;
452 *(--rp) = rgb;
453 #endif
455 break;
457 case 24:
458 p = (unsigned char*)bmpbuf + 3 * width;
459 while (p > (unsigned char*)bmpbuf) {
460 data = *(--p);
461 data = (data << 8) | *(--p);
462 data = (data << 8) | *(--p);
463 *(--rp) = htole32(data);
465 break;
467 case 32: /* already in desired format */
468 break;
471 /* Convert to destination format */
472 qp = (union rgb_union *)bmpbuf;
473 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
474 if (format == FORMAT_NATIVE) {
475 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
476 if (remote) {
477 #if (LCD_REMOTE_DEPTH == 2) && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED)
478 fb_remote_data *dest = (fb_remote_data *)bitmap
479 + dst_width * (row >> 3);
480 int shift = row & 7;
481 int delta = 127;
482 unsigned bright;
484 for (col = 0; col < width; col++) {
485 if (dither)
486 delta = dither_matrix[row & 0xf][col & 0xf];
487 bright = brightness(*qp++);
488 bright = (3 * bright + (bright >> 6) + delta) >> 8;
489 *dest++ |= remote_pattern[bright] << shift;
491 #endif /* LCD_REMOTE_DEPTH / LCD_REMOTE_PIXELFORMAT */
492 } else
493 #endif /* defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1 */
495 #if LCD_DEPTH == 2
496 #if LCD_PIXELFORMAT == VERTICAL_PACKING
497 /* iriver H1x0 */
498 fb_data *dest = (fb_data *)bitmap + dst_width * (row >> 2);
499 int shift = 2 * (row & 3);
500 int delta = 127;
501 unsigned bright;
503 for (col = 0; col < width; col++) {
504 if (dither)
505 delta = dither_matrix[row & 0xf][col & 0xf];
506 bright = brightness(*qp++);
507 bright = (3 * bright + (bright >> 6) + delta) >> 8;
508 *dest++ |= (~bright & 3) << shift;
510 #else /* LCD_PIXELFORMAT == HORIZONTAL_PACKING */
511 /* greyscale iPods */
512 fb_data *dest = (fb_data *)bitmap + dst_width * row;
513 int shift = 6;
514 int delta = 127;
515 unsigned bright;
516 unsigned data = 0;
518 for (col = 0; col < width; col++) {
519 if (dither)
520 delta = dither_matrix[row & 0xf][col & 0xf];
521 bright = brightness(*qp++);
522 bright = (3 * bright + (bright >> 6) + delta) >> 8;
523 data |= (~bright & 3) << shift;
524 shift -= 2;
525 if (shift < 0) {
526 *dest++ = data;
527 data = 0;
528 shift = 6;
531 if (shift < 6)
532 *dest++ = data;
533 #endif /* LCD_PIXELFORMAT */
534 #elif LCD_DEPTH == 16
535 /* iriver h300, colour iPods, X5 */
536 fb_data *dest = (fb_data *)bitmap + dst_width * row;
537 int delta = 127;
538 unsigned r, g, b;
540 for (col = 0; col < width; col++) {
541 if (dither)
542 delta = dither_matrix[row & 0xf][col & 0xf];
543 q0 = *qp++;
544 r = (31 * q0.red + (q0.red >> 3) + delta) >> 8;
545 g = (63 * q0.green + (q0.green >> 2) + delta) >> 8;
546 b = (31 * q0.blue + (q0.blue >> 3) + delta) >> 8;
547 *dest++ = LCD_RGBPACK_LCD(r, g, b);
549 #endif /* LCD_DEPTH */
551 } else
552 #endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */
554 p = bitmap + dst_width * (row >> 3);
555 mask = 1 << (row & 7);
557 for (col = 0; col < width; col++, p++)
558 if (brightness(*qp++) < 128)
559 *p |= mask;
563 DEBUGF("totalsize: %d\n", totalsize);
564 return totalsize; /* return the used buffer size. */