1 /*
2 * BMP image format decoder
3 * Copyright (c) 2005 Mans Rullgard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <inttypes.h>
23
30
33 {
34 const uint8_t *buf = avpkt->
data;
35 int buf_size = avpkt->
size;
36 unsigned int fsize, hsize;
38 unsigned int depth;
40 unsigned int ihsize;
41 int i, j, n, linesize,
ret;
42 uint32_t
rgb[3] = {0};
44 uint8_t *ptr;
45 int dsize;
46 const uint8_t *buf0 = buf;
48
49 if (buf_size < 14) {
52 }
53
54 if (bytestream_get_byte(&buf) != 'B' ||
55 bytestream_get_byte(&buf) != 'M') {
58 }
59
60 fsize = bytestream_get_le32(&buf);
61 if (buf_size <
fsize) {
65 }
66
67 buf += 2; /* reserved1 */
68 buf += 2; /* reserved2 */
69
70 hsize = bytestream_get_le32(&buf); /* header size */
71 ihsize = bytestream_get_le32(&buf); /* more header size */
72 if (ihsize + 14LL > hsize) {
75 }
76
77 /* sometimes file size is set to some headers size, set a real size in that case */
80
83 "Declared file size is less than header size (%u < %u)\n",
86 }
87
88 switch (ihsize) {
89 case 40: // windib
90 case 56: // windib v3
91 case 64: // OS/2 v2
92 case 108: // windib v4
93 case 124: // windib v5
94 width = bytestream_get_le32(&buf);
95 height = bytestream_get_le32(&buf);
96 break;
97 case 12: // OS/2 v1
98 width = bytestream_get_le16(&buf);
99 height = bytestream_get_le16(&buf);
100 break;
101 default:
103 ihsize);
105 }
106
107 /* planes */
108 if (bytestream_get_le16(&buf) != 1) {
111 }
112
113 depth = bytestream_get_le16(&buf);
114
115 if (ihsize >= 40)
116 comp = bytestream_get_le32(&buf);
117 else
119
124 }
125
127 buf += 20;
128 rgb[0] = bytestream_get_le32(&buf);
129 rgb[1] = bytestream_get_le32(&buf);
130 rgb[2] = bytestream_get_le32(&buf);
131 if (ihsize > 40)
132 alpha = bytestream_get_le32(&buf);
133 }
134
139 }
140
142
143 switch (depth) {
144 case 32:
146 if (
rgb[0] == 0xFF000000 &&
rgb[1] == 0x00FF0000 &&
rgb[2] == 0x0000FF00)
148 else if (
rgb[0] == 0x00FF0000 &&
rgb[1] == 0x0000FF00 &&
rgb[2] == 0x000000FF)
150 else if (
rgb[0] == 0x0000FF00 &&
rgb[1] == 0x00FF0000 &&
rgb[2] == 0xFF000000)
152 else if (
rgb[0] == 0x000000FF &&
rgb[1] == 0x0000FF00 &&
rgb[2] == 0x00FF0000)
154 else {
156 "%0"PRIX32
" %0"PRIX32
" %0"PRIX32
"\n",
rgb[0],
rgb[1],
rgb[2]);
158 }
159 } else {
161 }
162 break;
163 case 24:
165 break;
166 case 16:
170 if (
rgb[0] == 0xF800 &&
rgb[1] == 0x07E0 &&
rgb[2] == 0x001F)
172 else if (
rgb[0] == 0x7C00 &&
rgb[1] == 0x03E0 &&
rgb[2] == 0x001F)
174 else if (
rgb[0] == 0x0F00 &&
rgb[1] == 0x00F0 &&
rgb[2] == 0x000F)
176 else {
178 "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
181 }
182 }
183 break;
184 case 8:
185 if (hsize - ihsize - 14 > 0)
187 else
189 break;
190 case 1:
191 case 4:
192 if (hsize - ihsize - 14 > 0) {
194 } else {
196 1 << depth);
198 }
199 break;
200 default:
203 }
204
208 }
209
214
215 buf = buf0 + hsize;
216 dsize = buf_size - hsize;
217
218 /* Line size in file multiple of 4 */
219 n = ((avctx->
width * depth + 31) / 8) & ~3;
220
222 n = (avctx->
width * depth + 7) / 8;
223 if (n * avctx->
height > dsize) {
225 dsize, n * avctx->
height);
227 }
228 av_log(avctx,
AV_LOG_ERROR,
"data size too small, assuming missing line alignment\n");
229 }
230
231 // RLE may skip decoding some picture areas, so blank picture before decoding
234
238 } else {
241 }
242
244 int colors = 1 << depth;
245
246 memset(p->
data[1], 0, 1024);
247
248 if (ihsize >= 36) {
249 int t;
250 buf = buf0 + 46;
251 t = bytestream_get_le32(&buf);
252 if (t < 0 || t > (1 << depth)) {
254 "Incorrect number of colors - %X for bitdepth %u\n",
255 t, depth);
256 } else if (t) {
257 colors = t;
258 }
259 } else {
260 colors =
FFMIN(256, (hsize-ihsize-14) / 3);
261 }
262 buf = buf0 + 14 + ihsize; //palette location
263 // OS/2 bitmap, 3 bytes per palette entry
264 if ((hsize-ihsize-14) < (colors << 2)) {
265 if ((hsize-ihsize-14) < colors * 3) {
268 }
269 for (
i = 0;
i < colors;
i++)
270 ((uint32_t*)p->
data[1])[
i] = (0xFF
U<<24) | bytestream_get_le24(&buf);
271 } else {
272 for (
i = 0;
i < colors;
i++)
273 ((uint32_t*)p->
data[1])[
i] = 0xFFU << 24 | bytestream_get_le32(&buf);
274 }
275 buf = buf0 + hsize;
276 }
281 }
287 }
288 } else {
289 switch (depth) {
290 case 1:
292 int j;
293 for (j = 0; j < avctx->
width >> 3; j++) {
294 ptr[j*8+0] = buf[j] >> 7;
295 ptr[j*8+1] = (buf[j] >> 6) & 1;
296 ptr[j*8+2] = (buf[j] >> 5) & 1;
297 ptr[j*8+3] = (buf[j] >> 4) & 1;
298 ptr[j*8+4] = (buf[j] >> 3) & 1;
299 ptr[j*8+5] = (buf[j] >> 2) & 1;
300 ptr[j*8+6] = (buf[j] >> 1) & 1;
301 ptr[j*8+7] = buf[j] & 1;
302 }
303 for (j = 0; j < (avctx->
width & 7); j++) {
304 ptr[avctx->
width - (avctx->
width & 7) + j] = buf[avctx->
width >> 3] >> (7 - j) & 1;
305 }
306 buf += n;
307 ptr += linesize;
308 }
309 break;
310 case 8:
311 case 24:
312 case 32:
314 memcpy(ptr, buf, n);
315 buf += n;
316 ptr += linesize;
317 }
318 break;
319 case 4:
321 int j;
322 for (j = 0; j < n; j++) {
323 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
324 ptr[j*2+1] = buf[j] & 0xF;
325 }
326 buf += n;
327 ptr += linesize;
328 }
329 break;
330 case 16:
332 const uint16_t *
src = (
const uint16_t *) buf;
333 uint16_t *dst = (uint16_t *) ptr;
334
335 for (j = 0; j < avctx->
width; j++)
337
338 buf += n;
339 ptr += linesize;
340 }
341 break;
342 default:
345 }
346 }
349 int j;
351 for (j = 0; j < avctx->
width; j++) {
352 if (ptr[4*j])
353 break;
354 }
355 if (j < avctx->
width)
356 break;
357 }
360 }
361
362 *got_frame = 1;
363
364 return buf_size;
365 }
366
374 };