Daniel Stenberg | 1c0c861 | 2002-05-17 12:22:24 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 by Linus Nielsen Feltzing |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Daniel Stenberg | 1c0c861 | 2002-05-17 12:22:24 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | 2005-04-16 Tomas Salfischberger: |
| 24 | - New BMP loader function, based on the old one (borrowed a lot of |
| 25 | calculations and checks there.) |
| 26 | - Conversion part needs some optimization, doing unneeded calulations now. |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 27 | 2006-11-18 Jens Arnold: complete rework |
| 28 | - All canonical formats supported now (1, 4, 8, 15/16, 24 and 32 bit) |
| 29 | - better protection against malformed / non-standard BMPs |
| 30 | - code heavily optimised for both size and speed |
| 31 | - dithering for 2 bit targets |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 32 | 2008-11-02 Akio Idehara: refactor for scaler frontend |
| 33 | 2008-12-08 Andrew Mahone: partial-line reading, scaler frontend |
| 34 | - read_part_line does the actual source BMP reading, return columns read |
| 35 | and updates fields in a struct bmp_args with the new data and current |
| 36 | reader state |
| 37 | - skip_lines_bmp and store_part_bmp implement the scaler callbacks to skip |
| 38 | ahead by whole lines, or read the next chunk of the current line |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 39 | */ |
Daniel Stenberg | 1c0c861 | 2002-05-17 12:22:24 +0000 | [diff] [blame] | 40 | |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
Linus Nielsen Feltzing | 745adad | 2006-01-28 12:12:42 +0000 | [diff] [blame] | 43 | #include <string.h> |
Jens Arnold | 83e18d9 | 2006-11-11 11:26:42 +0000 | [diff] [blame] | 44 | #include "inttypes.h" |
Nils Wallménius | d1a9e0c | 2009-10-06 22:02:06 +0000 | [diff] [blame] | 45 | #include "system.h" |
Andrew Mahone | 4eedc93 | 2009-01-04 21:22:05 +0000 | [diff] [blame] | 46 | #ifndef PLUGIN |
Felix Arends | 1f50b8b | 2002-05-29 14:36:09 +0000 | [diff] [blame] | 47 | #include "debug.h" |
Andrew Mahone | 4eedc93 | 2009-01-04 21:22:05 +0000 | [diff] [blame] | 48 | #endif |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 49 | #include "lcd.h" |
Dave Chapman | 78d29f5 | 2008-03-26 23:35:34 +0000 | [diff] [blame] | 50 | #include "file.h" |
Maurus Cuelenaere | 36c71a6 | 2009-01-29 20:49:43 +0000 | [diff] [blame] | 51 | #include "bmp.h" |
Jens Arnold | 848e0b5 | 2006-11-20 00:20:09 +0000 | [diff] [blame] | 52 | #ifdef HAVE_REMOTE_LCD |
| 53 | #include "lcd-remote.h" |
| 54 | #endif |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 55 | #ifdef ROCKBOX_DEBUG_BMP_LOADER |
Thomas Martitz | 2f4a941 | 2014-01-05 19:37:48 +0100 | [diff] [blame] | 56 | #define BDEBUGF DEBUGF |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 57 | #else |
| 58 | #define BDEBUGF(...) |
| 59 | #endif |
Dave Chapman | b9bb723 | 2008-03-26 18:18:22 +0000 | [diff] [blame] | 60 | #ifndef __PCTOOL__ |
Linus Nielsen Feltzing | 745adad | 2006-01-28 12:12:42 +0000 | [diff] [blame] | 61 | #include "config.h" |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 62 | #include "resize.h" |
Dave Chapman | b9bb723 | 2008-03-26 18:18:22 +0000 | [diff] [blame] | 63 | #else |
| 64 | #undef DEBUGF |
| 65 | #define DEBUGF(...) |
| 66 | #endif |
Daniel Stenberg | 1c0c861 | 2002-05-17 12:22:24 +0000 | [diff] [blame] | 67 | |
| 68 | #ifdef __GNUC__ |
| 69 | #define STRUCT_PACKED __attribute__((packed)) |
Felix Arends | 1f50b8b | 2002-05-29 14:36:09 +0000 | [diff] [blame] | 70 | #else |
| 71 | #define STRUCT_PACKED |
| 72 | #pragma pack (push, 2) |
Daniel Stenberg | 1c0c861 | 2002-05-17 12:22:24 +0000 | [diff] [blame] | 73 | #endif |
| 74 | |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 75 | /* BMP header structure */ |
| 76 | struct bmp_header { |
| 77 | uint16_t type; /* signature - 'BM' */ |
| 78 | uint32_t size; /* file size in bytes */ |
| 79 | uint16_t reserved1; /* 0 */ |
| 80 | uint16_t reserved2; /* 0 */ |
| 81 | uint32_t off_bits; /* offset to bitmap */ |
| 82 | uint32_t struct_size; /* size of this struct (40) */ |
Jens Arnold | 89bfb66 | 2007-11-30 08:51:18 +0000 | [diff] [blame] | 83 | int32_t width; /* bmap width in pixels */ |
| 84 | int32_t height; /* bmap height in pixels */ |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 85 | uint16_t planes; /* num planes - always 1 */ |
| 86 | uint16_t bit_count; /* bits per pixel */ |
| 87 | uint32_t compression; /* compression flag */ |
| 88 | uint32_t size_image; /* image size in bytes */ |
| 89 | int32_t x_pels_per_meter; /* horz resolution */ |
| 90 | int32_t y_pels_per_meter; /* vert resolution */ |
| 91 | uint32_t clr_used; /* 0 -> color table size */ |
| 92 | uint32_t clr_important; /* important color count */ |
Daniel Stenberg | 1c0c861 | 2002-05-17 12:22:24 +0000 | [diff] [blame] | 93 | } STRUCT_PACKED; |
| 94 | |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 95 | /* masks for supported BI_BITFIELDS encodings (16/32 bit) */ |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 96 | static const struct uint8_rgb bitfields[][4] = { |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 97 | /* 15bit */ |
| 98 | { |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 99 | { .blue = 0x00, .green = 0x7c, .red = 0x00, .alpha = 0x00 }, |
| 100 | { .blue = 0xe0, .green = 0x03, .red = 0x00, .alpha = 0x00 }, |
| 101 | { .blue = 0x1f, .green = 0x00, .red = 0x00, .alpha = 0x00 }, |
| 102 | { .blue = 0x00, .green = 0x00, .red = 0x00, .alpha = 0x00 }, |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 103 | }, |
| 104 | /* 16bit */ |
| 105 | { |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 106 | { .blue = 0x00, .green = 0xf8, .red = 0x00, .alpha = 0x00 }, |
| 107 | { .blue = 0xe0, .green = 0x07, .red = 0x00, .alpha = 0x00 }, |
| 108 | { .blue = 0x1f, .green = 0x00, .red = 0x00, .alpha = 0x00 }, |
| 109 | { .blue = 0x00, .green = 0x00, .red = 0x00, .alpha = 0x00 }, |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 110 | }, |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 111 | /* 32bit BGRA */ |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 112 | { |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 113 | { .blue = 0x00, .green = 0x00, .red = 0xff, .alpha = 0x00 }, |
| 114 | { .blue = 0x00, .green = 0xff, .red = 0x00, .alpha = 0x00 }, |
| 115 | { .blue = 0xff, .green = 0x00, .red = 0x00, .alpha = 0x00 }, |
| 116 | { .blue = 0x00, .green = 0x00, .red = 0x00, .alpha = 0xff }, |
| 117 | }, |
| 118 | /* 32bit ABGR */ |
| 119 | { |
| 120 | { .blue = 0x00, .green = 0x00, .red = 0x00, .alpha = 0xff }, |
| 121 | { .blue = 0x00, .green = 0x00, .red = 0xff, .alpha = 0x00 }, |
| 122 | { .blue = 0x00, .green = 0xff, .red = 0x00, .alpha = 0x00 }, |
| 123 | { .blue = 0xff, .green = 0x00, .red = 0x00, .alpha = 0x00 }, |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 124 | }, |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 125 | }; |
Michael Sevakis | 76a6aaa | 2006-10-11 01:26:09 +0000 | [diff] [blame] | 126 | |
Andrew Mahone | 07e982d | 2009-01-08 02:49:23 +0000 | [diff] [blame] | 127 | #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 128 | /* the full 16x16 Bayer dither matrix may be calculated quickly with this table |
| 129 | */ |
| 130 | const unsigned char dither_table[16] = |
| 131 | { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 }; |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 132 | #endif |
Michael Sevakis | bed0db2 | 2006-10-11 18:12:36 +0000 | [diff] [blame] | 133 | |
Jens Arnold | 8113717 | 2008-03-20 17:40:58 +0000 | [diff] [blame] | 134 | #if ((LCD_DEPTH == 2) && (LCD_PIXELFORMAT == VERTICAL_INTERLEAVED)) \ |
| 135 | || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH == 2) \ |
| 136 | && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED)) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 137 | const unsigned short vi_pattern[4] = { |
Jens Arnold | 848e0b5 | 2006-11-20 00:20:09 +0000 | [diff] [blame] | 138 | 0x0101, 0x0100, 0x0001, 0x0000 |
| 139 | }; |
| 140 | #endif |
| 141 | |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 142 | /****************************************************************************** |
| 143 | * read_bmp_file() |
| 144 | * |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 145 | * Reads a BMP file and puts the data in rockbox format in *bitmap. |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 146 | * |
| 147 | *****************************************************************************/ |
Nicolas Pennequin | ec6569e | 2007-11-10 13:26:11 +0000 | [diff] [blame] | 148 | int read_bmp_file(const char* filename, |
Linus Nielsen Feltzing | 745adad | 2006-01-28 12:12:42 +0000 | [diff] [blame] | 149 | struct bitmap *bm, |
| 150 | int maxsize, |
Andrew Mahone | 9058620 | 2008-12-26 07:05:13 +0000 | [diff] [blame] | 151 | int format, |
| 152 | const struct custom_format *cformat) |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 153 | { |
Nicolas Pennequin | ec6569e | 2007-11-10 13:26:11 +0000 | [diff] [blame] | 154 | int fd, ret; |
Andrew Mahone | 07e982d | 2009-01-08 02:49:23 +0000 | [diff] [blame] | 155 | fd = open(filename, O_RDONLY); |
Nicolas Pennequin | ec6569e | 2007-11-10 13:26:11 +0000 | [diff] [blame] | 156 | |
| 157 | /* Exit if file opening failed */ |
| 158 | if (fd < 0) { |
| 159 | DEBUGF("read_bmp_file: can't open '%s', rc: %d\n", filename, fd); |
| 160 | return fd * 10 - 1; |
| 161 | } |
| 162 | |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 163 | BDEBUGF("read_bmp_file: '%s' remote: %d resize: %d keep_aspect: %d\n", |
| 164 | filename, !!(format & FORMAT_REMOTE), !!(format & FORMAT_RESIZE), |
| 165 | !!(format & FORMAT_KEEP_ASPECT)); |
Andrew Mahone | 9058620 | 2008-12-26 07:05:13 +0000 | [diff] [blame] | 166 | ret = read_bmp_fd(fd, bm, maxsize, format, cformat); |
Andrew Mahone | 07e982d | 2009-01-08 02:49:23 +0000 | [diff] [blame] | 167 | close(fd); |
Nicolas Pennequin | ec6569e | 2007-11-10 13:26:11 +0000 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 171 | enum color_order { |
| 172 | /* only used for different types of 32bpp images */ |
| 173 | BGRA, /* should be most common */ |
| 174 | ABGR /* generated by some GIMP versions */ |
| 175 | }; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 176 | |
| 177 | struct bmp_args { |
Thomas Martitz | 042d8bf | 2014-01-05 01:14:14 +0100 | [diff] [blame] | 178 | /* needs to be at least 2byte aligned for faster 16bit reads. |
| 179 | * but aligning to cache should be even faster */ |
| 180 | unsigned char buf[BM_MAX_WIDTH * 4] CACHEALIGN_AT_LEAST_ATTR(2); |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 181 | int fd; |
| 182 | short padded_width; |
| 183 | short read_width; |
| 184 | short width; |
| 185 | short depth; |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 186 | enum color_order order; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 187 | struct uint8_rgb *palette; |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 188 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 189 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 190 | int cur_row; |
| 191 | int cur_col; |
| 192 | struct img_part part; |
| 193 | #endif |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 194 | /* as read_part_line() goes through the rows it'll set this to true |
Thomas Martitz | caec07b | 2011-11-11 22:03:29 +0000 | [diff] [blame] | 195 | * if it finds transparency. Initialize to 0 before calling */ |
| 196 | int alpha_detected; |
| 197 | /* for checking transparency it checks the against the very first byte |
| 198 | * of the bitmap. Initalize to 0x80 before calling */ |
| 199 | unsigned char first_alpha_byte; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | static unsigned int read_part_line(struct bmp_args *ba) |
| 203 | { |
| 204 | const int padded_width = ba->padded_width; |
| 205 | const int read_width = ba->read_width; |
| 206 | const int width = ba->width; |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 207 | int depth = ba->depth; |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 208 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 209 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 210 | int cur_row = ba->cur_row; |
| 211 | int cur_col = ba->cur_col; |
| 212 | #endif |
| 213 | const int fd = ba->fd; |
| 214 | uint8_t *ibuf; |
| 215 | struct uint8_rgb *buf = (struct uint8_rgb *)(ba->buf); |
| 216 | const struct uint8_rgb *palette = ba->palette; |
Nils Wallménius | 3b04a85 | 2011-06-12 22:17:45 +0000 | [diff] [blame] | 217 | uint32_t component, data; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 218 | int ret; |
| 219 | int i, cols, len; |
| 220 | |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 221 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 222 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 223 | cols = MIN(width - cur_col,(int)BM_MAX_WIDTH); |
| 224 | BDEBUGF("reading %d cols (width: %d, max: %d)\n",cols,width,BM_MAX_WIDTH); |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 225 | len = (cols * (depth == 15 ? 16 : depth) + 7) >> 3; |
| 226 | #else |
| 227 | cols = width; |
| 228 | len = read_width; |
| 229 | #endif |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 230 | ibuf = ((unsigned char *)buf) + (BM_MAX_WIDTH << 2) - len; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 231 | BDEBUGF("read_part_line: cols=%d len=%d\n",cols,len); |
Andrew Mahone | 07e982d | 2009-01-08 02:49:23 +0000 | [diff] [blame] | 232 | ret = read(fd, ibuf, len); |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 233 | if (ret != len) |
| 234 | { |
| 235 | DEBUGF("read_part_line: error reading image, read returned %d " |
| 236 | "expected %d\n", ret, len); |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 237 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 238 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 239 | BDEBUGF("cur_row: %d cur_col: %d cols: %d len: %d\n", cur_row, cur_col, |
| 240 | cols, len); |
Andrew Mahone | 4eedc93 | 2009-01-04 21:22:05 +0000 | [diff] [blame] | 241 | #endif |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 244 | |
Thomas Martitz | caec07b | 2011-11-11 22:03:29 +0000 | [diff] [blame] | 245 | /* detect if the image has useful alpha information. |
| 246 | * if all alpha bits are 0xff or 0x00 discard the information. |
| 247 | * if it has other bits, or is mixed with 0x00 and 0xff then interpret |
| 248 | * as alpha. assume no alpha until the opposite is proven. as mixed |
| 249 | * is alpha, compare to the first byte instead of 0xff and 0x00 separately |
| 250 | */ |
| 251 | if (depth == 32 && ba->first_alpha_byte == 0x80) |
| 252 | ba->first_alpha_byte = ibuf[3] ? 0xff : 0x0; |
| 253 | |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 254 | /* select different color orders within the switch-case to avoid |
| 255 | * nested if/switch */ |
| 256 | if (depth == 32) |
| 257 | depth += ba->order; |
| 258 | |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 259 | while (ibuf < ba->buf + (BM_MAX_WIDTH << 2)) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 260 | { |
| 261 | switch (depth) |
| 262 | { |
| 263 | case 1: |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 264 | data = *ibuf++; |
| 265 | for (i = 0; i < 8; i++) |
| 266 | { |
| 267 | *buf++ = palette[data & 0x80 ? 1 : 0]; |
| 268 | data <<= 1; |
| 269 | } |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 270 | break; |
| 271 | case 4: |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 272 | data = *ibuf++; |
| 273 | *buf++ = palette[data >> 4]; |
| 274 | *buf++ = palette[data & 0xf]; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 275 | break; |
| 276 | case 8: |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 277 | *buf++ = palette[*ibuf++]; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 278 | break; |
| 279 | case 15: |
| 280 | case 16: |
Thomas Martitz | 042d8bf | 2014-01-05 01:14:14 +0100 | [diff] [blame] | 281 | data = letoh16(*(uint16_t*)ibuf); |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 282 | component = (data << 3) & 0xf8; |
| 283 | component |= component >> 5; |
| 284 | buf->blue = component; |
| 285 | if (depth == 15) |
| 286 | { |
| 287 | data >>= 2; |
| 288 | component = data & 0xf8; |
| 289 | component |= component >> 5; |
| 290 | } else { |
| 291 | data >>= 3; |
| 292 | component = data & 0xfc; |
| 293 | component |= component >> 6; |
| 294 | } |
| 295 | buf->green = component; |
| 296 | data >>= 5; |
| 297 | component = data & 0xf8; |
| 298 | component |= component >> 5; |
| 299 | buf->red = component; |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 300 | buf->alpha = 0xff; |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 301 | buf++; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 302 | ibuf += 2; |
| 303 | break; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 304 | case 24: |
| 305 | buf->blue = *ibuf++; |
| 306 | buf->green = *ibuf++; |
| 307 | buf->red = *ibuf++; |
Thomas Martitz | caec07b | 2011-11-11 22:03:29 +0000 | [diff] [blame] | 308 | buf->alpha = 0xff; |
| 309 | buf++; |
| 310 | break; |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 311 | case 32 + BGRA: |
Thomas Martitz | caec07b | 2011-11-11 22:03:29 +0000 | [diff] [blame] | 312 | buf->blue = *ibuf++; |
| 313 | buf->green = *ibuf++; |
| 314 | buf->red = *ibuf++; |
| 315 | buf->alpha = *ibuf++; |
| 316 | ba->alpha_detected |= (buf->alpha != ba->first_alpha_byte); |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 317 | buf++; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 318 | break; |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 319 | case 32 + ABGR: |
| 320 | buf->alpha = *ibuf++; |
| 321 | buf->blue = *ibuf++; |
| 322 | buf->green = *ibuf++; |
| 323 | buf->red = *ibuf++; |
| 324 | ba->alpha_detected |= (buf->alpha != ba->first_alpha_byte); |
| 325 | buf++; |
| 326 | break; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 327 | } |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 328 | } |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 329 | #if !defined(HAVE_LCD_COLOR) && \ |
| 330 | ((LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) || \ |
| 331 | defined(PLUGIN)) |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 332 | ibuf = ba->buf; |
| 333 | buf = (struct uint8_rgb*)ba->buf; |
| 334 | while (ibuf < ba->buf + cols) |
| 335 | *ibuf++ = brightness(*buf++); |
| 336 | #endif |
| 337 | |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 338 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 339 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 340 | cur_col += cols; |
| 341 | if (cur_col == width) |
| 342 | { |
| 343 | #endif |
| 344 | int pad = padded_width - read_width; |
| 345 | if (pad > 0) |
| 346 | { |
| 347 | BDEBUGF("seeking %d bytes to next line\n",pad); |
Andrew Mahone | 07e982d | 2009-01-08 02:49:23 +0000 | [diff] [blame] | 348 | lseek(fd, pad, SEEK_CUR); |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 349 | } |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 350 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 351 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 352 | cur_col = 0; |
| 353 | BDEBUGF("read_part_line: completed row %d\n", cur_row); |
| 354 | cur_row += 1; |
| 355 | } |
| 356 | |
| 357 | ba->cur_row = cur_row; |
| 358 | ba->cur_col = cur_col; |
| 359 | #endif |
| 360 | return cols; |
| 361 | } |
| 362 | |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 363 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 364 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 365 | static struct img_part *store_part_bmp(void *args) |
| 366 | { |
| 367 | struct bmp_args *ba = (struct bmp_args *)args; |
| 368 | |
| 369 | ba->part.len = read_part_line(ba); |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 370 | #ifdef HAVE_LCD_COLOR |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 371 | ba->part.buf = (struct uint8_rgb *)ba->buf; |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 372 | #else |
| 373 | ba->part.buf = (uint8_t *)ba->buf; |
| 374 | #endif |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 375 | if (ba->part.len) |
| 376 | return &(ba->part); |
| 377 | else |
| 378 | return NULL; |
| 379 | } |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 380 | #endif |
| 381 | |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 382 | static inline int rgbcmp(const struct uint8_rgb *rgb1, const struct uint8_rgb *rgb2) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 383 | { |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 384 | return memcmp(rgb1, rgb2, sizeof(struct uint8_rgb)); |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 385 | } |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 386 | |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 387 | #if LCD_DEPTH > 1 |
Andrew Mahone | 59e70b5 | 2009-05-09 10:32:07 +0000 | [diff] [blame] | 388 | #if !defined(PLUGIN) && !defined(HAVE_JPEG) && !defined(HAVE_BMP_SCALING) |
| 389 | static inline |
| 390 | #endif |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 391 | void output_row_8_native(uint32_t row, void * row_in, |
| 392 | struct scaler_context *ctx) |
| 393 | { |
| 394 | int col; |
| 395 | int fb_width = BM_WIDTH(ctx->bm->width,FORMAT_NATIVE,0); |
| 396 | uint8_t dy = DITHERY(row); |
| 397 | #ifdef HAVE_LCD_COLOR |
| 398 | struct uint8_rgb *qp = (struct uint8_rgb*)row_in; |
| 399 | #else |
| 400 | uint8_t *qp = (uint8_t*)row_in; |
| 401 | #endif |
| 402 | BDEBUGF("output_row: y: %lu in: %p\n",row, row_in); |
| 403 | #if LCD_DEPTH == 2 |
| 404 | #if LCD_PIXELFORMAT == HORIZONTAL_PACKING |
| 405 | /* greyscale iPods */ |
| 406 | fb_data *dest = (fb_data *)ctx->bm->data + fb_width * row; |
| 407 | int shift = 6; |
| 408 | int delta = 127; |
| 409 | unsigned bright; |
| 410 | unsigned data = 0; |
| 411 | |
| 412 | for (col = 0; col < ctx->bm->width; col++) { |
| 413 | if (ctx->dither) |
| 414 | delta = DITHERXDY(col,dy); |
| 415 | bright = *qp++; |
| 416 | bright = (3 * bright + (bright >> 6) + delta) >> 8; |
| 417 | data |= (~bright & 3) << shift; |
| 418 | shift -= 2; |
| 419 | if (shift < 0) { |
| 420 | *dest++ = data; |
| 421 | data = 0; |
| 422 | shift = 6; |
| 423 | } |
| 424 | } |
| 425 | if (shift < 6) |
| 426 | *dest++ = data; |
| 427 | #elif LCD_PIXELFORMAT == VERTICAL_PACKING |
| 428 | /* iriver H1x0 */ |
| 429 | fb_data *dest = (fb_data *)ctx->bm->data + fb_width * |
| 430 | (row >> 2); |
| 431 | int shift = 2 * (row & 3); |
| 432 | int delta = 127; |
| 433 | unsigned bright; |
| 434 | |
| 435 | for (col = 0; col < ctx->bm->width; col++) { |
| 436 | if (ctx->dither) |
| 437 | delta = DITHERXDY(col,dy); |
| 438 | bright = *qp++; |
| 439 | bright = (3 * bright + (bright >> 6) + delta) >> 8; |
| 440 | *dest++ |= (~bright & 3) << shift; |
| 441 | } |
| 442 | #elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED |
| 443 | /* iAudio M3 */ |
| 444 | fb_data *dest = (fb_data *)ctx->bm->data + fb_width * |
| 445 | (row >> 3); |
| 446 | int shift = row & 7; |
| 447 | int delta = 127; |
| 448 | unsigned bright; |
| 449 | |
| 450 | for (col = 0; col < ctx->bm->width; col++) { |
| 451 | if (ctx->dither) |
| 452 | delta = DITHERXDY(col,dy); |
| 453 | bright = *qp++; |
| 454 | bright = (3 * bright + (bright >> 6) + delta) >> 8; |
| 455 | *dest++ |= vi_pattern[bright] << shift; |
| 456 | } |
| 457 | #endif /* LCD_PIXELFORMAT */ |
Thomas Martitz | a1842c0 | 2014-06-18 07:15:00 +0200 | [diff] [blame] | 458 | #elif LCD_DEPTH >= 16 |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 459 | /* iriver h300, colour iPods, X5 */ |
| 460 | (void)fb_width; |
| 461 | fb_data *dest = STRIDE_MAIN((fb_data *)ctx->bm->data + fb_width * row, |
| 462 | (fb_data *)ctx->bm->data + row); |
Karl Kurbjun | e434536 | 2009-09-01 00:57:47 +0000 | [diff] [blame] | 463 | int delta = 127; |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 464 | unsigned r, g, b; |
| 465 | /* setup alpha channel buffer */ |
| 466 | unsigned char *bm_alpha = NULL; |
| 467 | if (ctx->bm->alpha_offset > 0) |
| 468 | bm_alpha = ctx->bm->data + ctx->bm->alpha_offset; |
| 469 | if (bm_alpha) |
Thomas Martitz | 312b2a2 | 2011-11-11 19:05:11 +0000 | [diff] [blame] | 470 | bm_alpha += ALIGN_UP(ctx->bm->width, 2) * row/2; |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 471 | |
Karl Kurbjun | e434536 | 2009-09-01 00:57:47 +0000 | [diff] [blame] | 472 | for (col = 0; col < ctx->bm->width; col++) { |
Thomas Martitz | a1842c0 | 2014-06-18 07:15:00 +0200 | [diff] [blame] | 473 | (void) delta; |
Karl Kurbjun | e434536 | 2009-09-01 00:57:47 +0000 | [diff] [blame] | 474 | if (ctx->dither) |
| 475 | delta = DITHERXDY(col,dy); |
| 476 | r = qp->red; |
| 477 | g = qp->green; |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 478 | b = qp->blue; |
Thomas Martitz | a1842c0 | 2014-06-18 07:15:00 +0200 | [diff] [blame] | 479 | #if LCD_DEPTH < 24 |
Karl Kurbjun | e434536 | 2009-09-01 00:57:47 +0000 | [diff] [blame] | 480 | r = (31 * r + (r >> 3) + delta) >> 8; |
| 481 | g = (63 * g + (g >> 2) + delta) >> 8; |
| 482 | b = (31 * b + (b >> 3) + delta) >> 8; |
Thomas Martitz | a1842c0 | 2014-06-18 07:15:00 +0200 | [diff] [blame] | 483 | #endif |
| 484 | *dest = FB_RGBPACK_LCD(r, g, b); |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 485 | dest += STRIDE_MAIN(1, ctx->bm->height); |
| 486 | if (bm_alpha) { |
Thomas Martitz | ca634a0 | 2013-02-12 10:26:44 +0100 | [diff] [blame] | 487 | /* pack alpha channel for 2 pixels into 1 byte and negate |
| 488 | * according to the interal alpha channel format */ |
| 489 | uint8_t alpha = ~qp->alpha; |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 490 | if (col%2) |
| 491 | *bm_alpha++ |= alpha&0xf0; |
| 492 | else |
| 493 | *bm_alpha = alpha>>4; |
| 494 | } |
| 495 | qp++; |
Karl Kurbjun | e434536 | 2009-09-01 00:57:47 +0000 | [diff] [blame] | 496 | } |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 497 | #endif /* LCD_DEPTH */ |
| 498 | } |
| 499 | #endif |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 500 | |
Nicolas Pennequin | ec6569e | 2007-11-10 13:26:11 +0000 | [diff] [blame] | 501 | /****************************************************************************** |
| 502 | * read_bmp_fd() |
| 503 | * |
| 504 | * Reads a BMP file in an open file descriptor and puts the data in rockbox |
| 505 | * format in *bitmap. |
| 506 | * |
| 507 | *****************************************************************************/ |
| 508 | int read_bmp_fd(int fd, |
| 509 | struct bitmap *bm, |
| 510 | int maxsize, |
Andrew Mahone | 9058620 | 2008-12-26 07:05:13 +0000 | [diff] [blame] | 511 | int format, |
| 512 | const struct custom_format *cformat) |
Nicolas Pennequin | ec6569e | 2007-11-10 13:26:11 +0000 | [diff] [blame] | 513 | { |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 514 | struct bmp_header bmph; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 515 | int padded_width; |
| 516 | int read_width; |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 517 | int depth, numcolors, compression, totalsize; |
Thomas Martitz | ce8aef7 | 2014-01-11 14:14:46 +0100 | [diff] [blame] | 518 | int ret, hdr_size; |
Maurus Cuelenaere | aec37aa | 2009-05-25 11:12:27 +0000 | [diff] [blame] | 519 | bool return_size = format & FORMAT_RETURN_SIZE; |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 520 | bool read_alpha = format & FORMAT_TRANSPARENT; |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 521 | enum color_order order = BGRA; |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 522 | |
| 523 | unsigned char *bitmap = bm->data; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 524 | struct uint8_rgb palette[256]; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 525 | struct rowset rset; |
| 526 | struct dim src_dim; |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 527 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) || \ |
| 528 | defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 529 | bool dither = false; |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 530 | #endif |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 531 | |
Andree Buschmann | 29f04cc | 2011-09-03 11:04:41 +0000 | [diff] [blame] | 532 | bool remote = false; |
Thomas Martitz | a1842c0 | 2014-06-18 07:15:00 +0200 | [diff] [blame] | 533 | #ifdef HAVE_REMOTE_LCD |
Jens Arnold | 848e0b5 | 2006-11-20 00:20:09 +0000 | [diff] [blame] | 534 | if (format & FORMAT_REMOTE) { |
Andree Buschmann | 29f04cc | 2011-09-03 11:04:41 +0000 | [diff] [blame] | 535 | remote = true; |
Jens Arnold | 848e0b5 | 2006-11-20 00:20:09 +0000 | [diff] [blame] | 536 | #if LCD_REMOTE_DEPTH == 1 |
| 537 | format = FORMAT_MONO; |
Jens Arnold | 848e0b5 | 2006-11-20 00:20:09 +0000 | [diff] [blame] | 538 | #endif |
| 539 | } |
| 540 | #endif /* HAVE_REMOTE_LCD */ |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 541 | |
Thomas Martitz | 354015e | 2011-11-08 22:34:35 +0000 | [diff] [blame] | 542 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 543 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
| 544 | unsigned int resize = IMG_NORESIZE; |
| 545 | |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 546 | if (format & FORMAT_RESIZE) { |
| 547 | resize = IMG_RESIZE; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Michael Sevakis | 76a6aaa | 2006-10-11 01:26:09 +0000 | [diff] [blame] | 550 | #else |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 551 | |
| 552 | (void)format; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 553 | #endif /*(LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)*/ |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 554 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) || \ |
| 555 | defined(PLUGIN) |
| 556 | if (format & FORMAT_DITHER) { |
| 557 | dither = true; |
| 558 | } |
| 559 | #endif |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 560 | /* read fileheader */ |
Andrew Mahone | 07e982d | 2009-01-08 02:49:23 +0000 | [diff] [blame] | 561 | ret = read(fd, &bmph, sizeof(struct bmp_header)); |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 562 | if (ret < 0) { |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 563 | return ret * 10 - 2; |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 564 | } |
Michael Sevakis | e0710b2 | 2006-10-15 17:51:00 +0000 | [diff] [blame] | 565 | |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 566 | if (ret != sizeof(struct bmp_header)) { |
Nicolas Pennequin | ec6569e | 2007-11-10 13:26:11 +0000 | [diff] [blame] | 567 | DEBUGF("read_bmp_fd: can't read BMP header."); |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 568 | return -3; |
| 569 | } |
| 570 | |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 571 | src_dim.width = letoh32(bmph.width); |
| 572 | src_dim.height = letoh32(bmph.height); |
| 573 | if (src_dim.height < 0) { /* Top-down BMP file */ |
| 574 | src_dim.height = -src_dim.height; |
| 575 | rset.rowstep = 1; |
Jens Arnold | 89bfb66 | 2007-11-30 08:51:18 +0000 | [diff] [blame] | 576 | } else { /* normal BMP */ |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 577 | rset.rowstep = -1; |
Jens Arnold | 89bfb66 | 2007-11-30 08:51:18 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 580 | depth = letoh16(bmph.bit_count); |
| 581 | /* 4-byte boundary aligned */ |
| 582 | read_width = ((src_dim.width * (depth == 15 ? 16 : depth) + 7) >> 3); |
| 583 | padded_width = (read_width + 3) & ~3; |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 584 | |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 585 | BDEBUGF("width: %d height: %d depth: %d padded_width: %d\n", src_dim.width, |
| 586 | src_dim.height, depth, padded_width); |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 587 | |
| 588 | #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 589 | if ((format & 3) == FORMAT_ANY) { |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 590 | if (depth == 1) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 591 | format = (format & ~3); |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 592 | else |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 593 | format = (format & ~3) | FORMAT_NATIVE; |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 594 | } |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 595 | bm->format = format & 1; |
| 596 | if ((format & 1) == FORMAT_MONO) |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 597 | { |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 598 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 599 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 600 | resize &= ~IMG_RESIZE; |
| 601 | resize |= IMG_NORESIZE; |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 602 | #endif |
Frank Gevaerts | bd3056c | 2011-08-01 20:07:05 +0000 | [diff] [blame] | 603 | remote = false; |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 604 | } |
Andrew Mahone | 4eedc93 | 2009-01-04 21:22:05 +0000 | [diff] [blame] | 605 | #elif !defined(PLUGIN) |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 606 | if (src_dim.width > BM_MAX_WIDTH) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 607 | return -6; |
| 608 | #endif /*(LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)*/ |
| 609 | |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 610 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 611 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 612 | if (resize & IMG_RESIZE) { |
| 613 | if(format & FORMAT_KEEP_ASPECT) { |
| 614 | /* keep aspect ratio.. */ |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 615 | struct dim resize_dim = { |
| 616 | .width = bm->width, |
| 617 | .height = bm->height, |
| 618 | }; |
| 619 | if (recalc_dimension(&resize_dim, &src_dim)) |
| 620 | resize = IMG_NORESIZE; |
| 621 | bm->width = resize_dim.width; |
| 622 | bm->height = resize_dim.height; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if (!(resize & IMG_RESIZE)) { |
| 627 | #endif |
| 628 | /* returning image size */ |
| 629 | bm->width = src_dim.width; |
| 630 | bm->height = src_dim.height; |
| 631 | |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 632 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 633 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 634 | } |
| 635 | #endif |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 636 | #if LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1) |
| 637 | format &= 1; |
| 638 | #endif |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 639 | if (rset.rowstep > 0) { /* Top-down BMP file */ |
| 640 | rset.rowstart = 0; |
| 641 | rset.rowstop = bm->height; |
| 642 | } else { /* normal BMP */ |
| 643 | rset.rowstart = bm->height - 1; |
| 644 | rset.rowstop = -1; |
| 645 | } |
| 646 | |
Thomas Martitz | 312b2a2 | 2011-11-11 19:05:11 +0000 | [diff] [blame] | 647 | /* need even rows (see lcd-16bit-common.c for details) */ |
| 648 | int alphasize = ALIGN_UP(bm->width, 2) * bm->height / 2; |
Andrew Mahone | 9058620 | 2008-12-26 07:05:13 +0000 | [diff] [blame] | 649 | if (cformat) |
| 650 | totalsize = cformat->get_size(bm); |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 651 | else { |
Andrew Mahone | 9058620 | 2008-12-26 07:05:13 +0000 | [diff] [blame] | 652 | totalsize = BM_SIZE(bm->width,bm->height,format,remote); |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 653 | if (!remote) |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 654 | if (depth == 32 && read_alpha) /* account for possible 4bit alpha per pixel */ |
Thomas Martitz | 312b2a2 | 2011-11-11 19:05:11 +0000 | [diff] [blame] | 655 | totalsize += alphasize; |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 656 | } |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 657 | |
Maurus Cuelenaere | aec37aa | 2009-05-25 11:12:27 +0000 | [diff] [blame] | 658 | if(return_size) |
Maurus Cuelenaere | 0bddb7e | 2009-05-25 15:19:34 +0000 | [diff] [blame] | 659 | { |
Maurus Cuelenaere | d2ea7db | 2009-05-25 20:15:02 +0000 | [diff] [blame] | 660 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 661 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Maurus Cuelenaere | 0bddb7e | 2009-05-25 15:19:34 +0000 | [diff] [blame] | 662 | if(resize) |
| 663 | totalsize += BM_SCALED_SIZE(bm->width, 0, 0, 0); |
Teruaki Kawashima | c4eea8f | 2010-02-18 15:10:31 +0000 | [diff] [blame] | 664 | else if (bm->width > BM_MAX_WIDTH) |
| 665 | totalsize += bm->width*4; |
Maurus Cuelenaere | d2ea7db | 2009-05-25 20:15:02 +0000 | [diff] [blame] | 666 | #endif |
Maurus Cuelenaere | aec37aa | 2009-05-25 11:12:27 +0000 | [diff] [blame] | 667 | return totalsize; |
Maurus Cuelenaere | 0bddb7e | 2009-05-25 15:19:34 +0000 | [diff] [blame] | 668 | } |
Maurus Cuelenaere | aec37aa | 2009-05-25 11:12:27 +0000 | [diff] [blame] | 669 | |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 670 | /* Check if this fits the buffer */ |
| 671 | if (totalsize > maxsize) { |
Nicolas Pennequin | ec6569e | 2007-11-10 13:26:11 +0000 | [diff] [blame] | 672 | DEBUGF("read_bmp_fd: Bitmap too large for buffer: " |
Marcin Bukat | d556809 | 2017-04-27 11:36:40 +0200 | [diff] [blame] | 673 | "%d bytes (%d max).\n", totalsize, maxsize); |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 674 | return -6; |
| 675 | } |
| 676 | |
Thomas Martitz | ce8aef7 | 2014-01-11 14:14:46 +0100 | [diff] [blame] | 677 | hdr_size = letoh32(bmph.struct_size); |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 678 | compression = letoh32(bmph.compression); |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 679 | if (depth <= 8) { |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 680 | numcolors = letoh32(bmph.clr_used); |
Linus Nielsen Feltzing | 745adad | 2006-01-28 12:12:42 +0000 | [diff] [blame] | 681 | if (numcolors == 0) |
Jens Arnold | 1d6df54 | 2009-06-07 21:27:05 +0000 | [diff] [blame] | 682 | numcolors = BIT_N(depth); |
Thomas Martitz | ce8aef7 | 2014-01-11 14:14:46 +0100 | [diff] [blame] | 683 | /* forward to the color table */ |
| 684 | lseek(fd, 14+hdr_size, SEEK_SET); |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 685 | } else { |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 686 | numcolors = 0; |
| 687 | if (compression == 3) { |
| 688 | if (hdr_size >= 56) |
| 689 | numcolors = 4; |
| 690 | else /* hdr_size == 52 */ |
| 691 | numcolors = 3; |
| 692 | } |
| 693 | } |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 694 | |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 695 | /* read color tables. for BI_BITFIELDS this actually |
| 696 | * reads the color masks */ |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 697 | if (numcolors > 0 && numcolors <= 256) { |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 698 | int i; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 699 | for (i = 0; i < numcolors; i++) { |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 700 | if (read(fd, &palette[i], sizeof(struct uint8_rgb)) |
| 701 | != (int)sizeof(struct uint8_rgb)) { |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 702 | DEBUGF("read_bmp_fd: Can't read color palette\n"); |
| 703 | return -7; |
| 704 | } |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | switch (depth) { |
| 709 | case 16: |
| 710 | #if LCD_DEPTH >= 16 |
| 711 | /* don't dither 16 bit BMP to LCD with same or larger depth */ |
Jens Arnold | 848e0b5 | 2006-11-20 00:20:09 +0000 | [diff] [blame] | 712 | if (!remote) |
Jens Arnold | 848e0b5 | 2006-11-20 00:20:09 +0000 | [diff] [blame] | 713 | dither = false; |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 714 | #endif |
| 715 | if (compression == 0) { /* BI_RGB, i.e. 15 bit */ |
| 716 | depth = 15; |
| 717 | break; |
| 718 | } /* else fall through */ |
| 719 | |
| 720 | case 32: |
| 721 | if (compression == 3) { /* BI_BITFIELDS */ |
Thomas Martitz | a62bd92 | 2013-12-22 20:15:03 +0100 | [diff] [blame] | 722 | bool found = false; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 723 | int i, j; |
| 724 | |
Thomas Martitz | 9f878b1 | 2013-12-22 21:04:11 +0100 | [diff] [blame] | 725 | /* (i == 0) is 15bit, (i == 1) is 16bit, (i == {2,3}) is 32bit */ |
Thomas Martitz | a62bd92 | 2013-12-22 20:15:03 +0100 | [diff] [blame] | 726 | for (i = 0; i < ARRAY_SIZE(bitfields) && !found; i++) { |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 727 | /* for 15bpp and higher numcolors has the number of color masks */ |
| 728 | for (j = 0; j < numcolors; j++) { |
| 729 | if (!rgbcmp(&palette[j], &bitfields[i][j])) { |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 730 | found = true; |
| 731 | } else { |
| 732 | found = false; |
| 733 | break; |
| 734 | } |
| 735 | } |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 736 | } |
Thomas Martitz | a62bd92 | 2013-12-22 20:15:03 +0100 | [diff] [blame] | 737 | if (found) { |
Thomas Martitz | 9f878b1 | 2013-12-22 21:04:11 +0100 | [diff] [blame] | 738 | if (i == 1) /* 15bit */ |
Thomas Martitz | a62bd92 | 2013-12-22 20:15:03 +0100 | [diff] [blame] | 739 | depth = 15; |
Thomas Martitz | 9f878b1 | 2013-12-22 21:04:11 +0100 | [diff] [blame] | 740 | else if (i == 4) /* 32bit, ABGR bitmap */ |
Thomas Martitz | a62bd92 | 2013-12-22 20:15:03 +0100 | [diff] [blame] | 741 | order = ABGR; |
Thomas Martitz | 9f878b1 | 2013-12-22 21:04:11 +0100 | [diff] [blame] | 742 | break; |
Thomas Martitz | a62bd92 | 2013-12-22 20:15:03 +0100 | [diff] [blame] | 743 | } |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 744 | } /* else fall through */ |
| 745 | |
| 746 | default: |
| 747 | if (compression != 0) { /* not BI_RGB */ |
Nicolas Pennequin | ec6569e | 2007-11-10 13:26:11 +0000 | [diff] [blame] | 748 | DEBUGF("read_bmp_fd: Unsupported compression (type %d)\n", |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 749 | compression); |
Linus Nielsen Feltzing | 745adad | 2006-01-28 12:12:42 +0000 | [diff] [blame] | 750 | return -8; |
| 751 | } |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 752 | break; |
Linus Nielsen Feltzing | 745adad | 2006-01-28 12:12:42 +0000 | [diff] [blame] | 753 | } |
Michael Sevakis | e0710b2 | 2006-10-15 17:51:00 +0000 | [diff] [blame] | 754 | |
Thomas Martitz | a1842c0 | 2014-06-18 07:15:00 +0200 | [diff] [blame] | 755 | #if LCD_DEPTH >= 24 |
| 756 | /* Never dither 24/32 bit BMP to 24 bit LCDs */ |
| 757 | if (depth >= 24 && !remote) |
| 758 | dither = false; |
| 759 | #endif |
| 760 | |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 761 | /* Search to the beginning of the image data */ |
Andrew Mahone | 07e982d | 2009-01-08 02:49:23 +0000 | [diff] [blame] | 762 | lseek(fd, (off_t)letoh32(bmph.off_bits), SEEK_SET); |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 763 | |
Andrew Mahone | 07e982d | 2009-01-08 02:49:23 +0000 | [diff] [blame] | 764 | memset(bitmap, 0, totalsize); |
Michael Sevakis | e0710b2 | 2006-10-15 17:51:00 +0000 | [diff] [blame] | 765 | |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 766 | #ifdef HAVE_LCD_COLOR |
| 767 | if (read_alpha && depth == 32) |
Thomas Martitz | 312b2a2 | 2011-11-11 19:05:11 +0000 | [diff] [blame] | 768 | bm->alpha_offset = totalsize - alphasize; |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 769 | else |
| 770 | bm->alpha_offset = 0; |
| 771 | #endif |
| 772 | |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 773 | struct bmp_args ba = { |
| 774 | .fd = fd, .padded_width = padded_width, .read_width = read_width, |
| 775 | .width = src_dim.width, .depth = depth, .palette = palette, |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 776 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 777 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 778 | .cur_row = 0, .cur_col = 0, .part = {0,0}, |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 779 | #endif |
Thomas Martitz | caec07b | 2011-11-11 22:03:29 +0000 | [diff] [blame] | 780 | .alpha_detected = false, .first_alpha_byte = 0x80, |
Thomas Martitz | ef92ed4 | 2013-12-21 17:12:21 +0100 | [diff] [blame] | 781 | .order = order, |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 782 | }; |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 783 | |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 784 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 785 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 786 | if (resize) |
Andrew Mahone | 9058620 | 2008-12-26 07:05:13 +0000 | [diff] [blame] | 787 | { |
| 788 | if (resize_on_load(bm, dither, &src_dim, &rset, |
| 789 | bitmap + totalsize, maxsize - totalsize, |
Andrew Mahone | eef7945 | 2009-05-06 04:53:56 +0000 | [diff] [blame] | 790 | cformat, IF_PIX_FMT(0,) store_part_bmp, &ba)) |
Andrew Mahone | 9058620 | 2008-12-26 07:05:13 +0000 | [diff] [blame] | 791 | return totalsize; |
| 792 | else |
| 793 | return 0; |
| 794 | } |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 795 | #endif /* LCD_DEPTH */ |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 796 | |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 797 | #if LCD_DEPTH > 1 || defined(PLUGIN) |
| 798 | struct scaler_context ctx = { |
| 799 | .bm = bm, |
| 800 | .dither = dither, |
| 801 | }; |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 802 | #endif |
Andrew Mahone | 59e70b5 | 2009-05-09 10:32:07 +0000 | [diff] [blame] | 803 | #if defined(PLUGIN) || defined(HAVE_JPEG) || defined(HAVE_BMP_SCALING) |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 804 | #if LCD_DEPTH > 1 |
| 805 | void (*output_row_8)(uint32_t, void*, struct scaler_context*) = |
| 806 | output_row_8_native; |
| 807 | #elif defined(PLUGIN) |
| 808 | void (*output_row_8)(uint32_t, void*, struct scaler_context*) = NULL; |
| 809 | #endif |
| 810 | #if LCD_DEPTH > 1 || defined(PLUGIN) |
| 811 | if (cformat) |
| 812 | output_row_8 = cformat->output_row_8; |
| 813 | #endif |
Andrew Mahone | 59e70b5 | 2009-05-09 10:32:07 +0000 | [diff] [blame] | 814 | #endif |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 815 | |
Teruaki Kawashima | c4eea8f | 2010-02-18 15:10:31 +0000 | [diff] [blame] | 816 | unsigned char *buf = ba.buf; |
| 817 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) || \ |
| 818 | defined(PLUGIN) |
| 819 | if (bm->width > BM_MAX_WIDTH) |
| 820 | { |
| 821 | #if defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
| 822 | unsigned int len = maxsize - totalsize; |
| 823 | buf = bitmap + totalsize; |
| 824 | ALIGN_BUFFER(buf, len, sizeof(uint32_t)); |
| 825 | if (bm->width*4 > (int)len) |
| 826 | #endif |
| 827 | return -6; |
| 828 | } |
| 829 | #endif |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 830 | int row; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 831 | /* loop to read rows and put them to buffer */ |
| 832 | for (row = rset.rowstart; row != rset.rowstop; row += rset.rowstep) { |
Teruaki Kawashima | c4eea8f | 2010-02-18 15:10:31 +0000 | [diff] [blame] | 833 | #if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \ |
| 834 | defined(HAVE_BMP_SCALING) || defined(PLUGIN) |
| 835 | if (bm->width > BM_MAX_WIDTH) |
| 836 | { |
| 837 | #if defined(HAVE_LCD_COLOR) |
| 838 | struct uint8_rgb *p = (struct uint8_rgb *)buf; |
| 839 | #else |
| 840 | uint8_t* p = buf; |
| 841 | #endif |
| 842 | do { |
| 843 | int len = read_part_line(&ba); |
| 844 | if (!len) |
| 845 | return -9; |
| 846 | memcpy(p, ba.buf, len*sizeof(*p)); |
| 847 | p += len; |
| 848 | } while (ba.cur_col); |
| 849 | } |
| 850 | else |
| 851 | #endif |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 852 | if (!read_part_line(&ba)) |
| 853 | return -9; |
| 854 | #ifndef PLUGIN |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 855 | #if !defined(HAVE_LCD_COLOR) && \ |
| 856 | (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) |
Teruaki Kawashima | c4eea8f | 2010-02-18 15:10:31 +0000 | [diff] [blame] | 857 | uint8_t* qp = buf; |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 858 | #else |
Teruaki Kawashima | c4eea8f | 2010-02-18 15:10:31 +0000 | [diff] [blame] | 859 | struct uint8_rgb *qp = (struct uint8_rgb *)buf; |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 860 | #endif |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 861 | #endif |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 862 | /* Convert to destination format */ |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 863 | #if ((LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)) && \ |
| 864 | !defined(PLUGIN) |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 865 | if (format == FORMAT_NATIVE) { |
| 866 | #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1 |
| 867 | if (remote) { |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 868 | unsigned char dy = DITHERY(row); |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 869 | #if (LCD_REMOTE_DEPTH == 2) && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED) |
| 870 | /* iAudio X5/M5 remote */ |
| 871 | fb_remote_data *dest = (fb_remote_data *)bitmap |
| 872 | + bm->width * (row >> 3); |
| 873 | int shift = row & 7; |
| 874 | int delta = 127; |
| 875 | unsigned bright; |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 876 | |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 877 | int col; |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 878 | for (col = 0; col < bm->width; col++) { |
| 879 | if (dither) |
| 880 | delta = DITHERXDY(col,dy); |
| 881 | #if !defined(HAVE_LCD_COLOR) && \ |
| 882 | (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) |
| 883 | bright = *qp++; |
| 884 | #else |
| 885 | bright = brightness(*qp++); |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 886 | #endif |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 887 | bright = (3 * bright + (bright >> 6) + delta) >> 8; |
| 888 | *dest++ |= vi_pattern[bright] << shift; |
| 889 | } |
| 890 | #endif /* LCD_REMOTE_DEPTH / LCD_REMOTE_PIXELFORMAT */ |
| 891 | } else |
| 892 | #endif /* defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1 */ |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 893 | #endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && |
| 894 | (LCD_REMOTE_DEPTH > 1) */ |
| 895 | #if LCD_DEPTH > 1 || defined(PLUGIN) |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 896 | { |
Andrew Mahone | 59e70b5 | 2009-05-09 10:32:07 +0000 | [diff] [blame] | 897 | #if !defined(PLUGIN) && !defined(HAVE_JPEG) && !defined(HAVE_BMP_SCALING) |
Teruaki Kawashima | c4eea8f | 2010-02-18 15:10:31 +0000 | [diff] [blame] | 898 | output_row_8_native(row, buf, &ctx); |
Andrew Mahone | 59e70b5 | 2009-05-09 10:32:07 +0000 | [diff] [blame] | 899 | #else |
Teruaki Kawashima | c4eea8f | 2010-02-18 15:10:31 +0000 | [diff] [blame] | 900 | output_row_8(row, buf, &ctx); |
Andrew Mahone | 59e70b5 | 2009-05-09 10:32:07 +0000 | [diff] [blame] | 901 | #endif |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 902 | } |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 903 | #endif |
| 904 | #if ((LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)) && \ |
| 905 | !defined(PLUGIN) |
| 906 | } |
| 907 | #ifndef PLUGIN |
| 908 | else |
| 909 | #endif |
| 910 | #endif |
| 911 | #ifndef PLUGIN |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 912 | { |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 913 | unsigned char *p = bitmap + bm->width * (row >> 3); |
Jens Arnold | 1d6df54 | 2009-06-07 21:27:05 +0000 | [diff] [blame] | 914 | unsigned char mask = BIT_N(row & 7); |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 915 | int col; |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 916 | for (col = 0; col < bm->width; col++, p++) |
| 917 | #if !defined(HAVE_LCD_COLOR) && \ |
| 918 | (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) |
| 919 | if (*qp++ < 128) |
| 920 | *p |= mask; |
| 921 | #else |
Jens Arnold | adc4848 | 2006-11-18 14:29:51 +0000 | [diff] [blame] | 922 | if (brightness(*qp++) < 128) |
| 923 | *p |= mask; |
Andrew Mahone | f7fa7e5 | 2008-12-26 07:03:22 +0000 | [diff] [blame] | 924 | #endif |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 925 | } |
Andrew Mahone | 91efc16 | 2009-05-09 07:31:27 +0000 | [diff] [blame] | 926 | #endif |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 927 | } |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 928 | #ifdef HAVE_LCD_COLOR |
| 929 | if (!ba.alpha_detected) |
| 930 | { /* if this has an alpha channel, totalsize accounts for it as well |
| 931 | * subtract if no actual alpha information was found */ |
| 932 | if (bm->alpha_offset > 0) |
Thomas Martitz | 312b2a2 | 2011-11-11 19:05:11 +0000 | [diff] [blame] | 933 | totalsize -= alphasize; |
Thomas Martitz | f443e7b | 2011-11-08 21:36:49 +0000 | [diff] [blame] | 934 | bm->alpha_offset = 0; |
| 935 | } |
| 936 | #endif |
Linus Nielsen Feltzing | 745adad | 2006-01-28 12:12:42 +0000 | [diff] [blame] | 937 | return totalsize; /* return the used buffer size. */ |
Linus Nielsen Feltzing | 250678b | 2005-04-25 07:42:10 +0000 | [diff] [blame] | 938 | } |