[project @ 2006-02-28 00:45:15 by rjw]
Implement bitfield encoding for BMPs. We now correctly decode every image in the BMP test suite at http://pobox.com/~jason1/bmpsuite/ svn path=/import/netsurf/; revision=2099
This commit is contained in:
parent
6929d90d41
commit
6f654c6f1e
125
image/bmpread.c
125
image/bmpread.c
|
@ -5,7 +5,6 @@
|
|||
* Copyright 2006 Richard Wilson <info@tinct.net>
|
||||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
@ -20,6 +19,7 @@
|
|||
|
||||
bmp_result bmp_analyse_header(struct bmp_image *bmp, char *data);
|
||||
bmp_result bmp_decode_rgb24(struct bmp_image *bmp, char **start, int bytes);
|
||||
bmp_result bmp_decode_rgb16(struct bmp_image *bmp, char **start, int bytes);
|
||||
bmp_result bmp_decode_rgb(struct bmp_image *bmp, char **start, int bytes);
|
||||
bmp_result bmp_decode_mask(struct bmp_image *bmp, char *data, int bytes);
|
||||
bmp_result bmp_decode_rle(struct bmp_image *bmp, char *data, int bytes, int size);
|
||||
|
@ -132,7 +132,7 @@ bmp_result ico_analyse(struct ico_collection *ico) {
|
|||
bmp_result bmp_analyse_header(struct bmp_image *bmp, char *data) {
|
||||
unsigned int header_size;
|
||||
unsigned int i;
|
||||
int width, height;
|
||||
int width, height, j;
|
||||
int palette_size;
|
||||
unsigned int flags;
|
||||
|
||||
|
@ -220,13 +220,39 @@ bmp_result bmp_analyse_header(struct bmp_image *bmp, char *data) {
|
|||
if (bmp->bpp == 0)
|
||||
bmp->bpp = 8;
|
||||
bmp->encoding = READ_INT(data, 16);
|
||||
if (bmp->encoding >= BMP_ENCODING_BITFIELDS) /* unsupported so far */
|
||||
if (bmp->encoding > BMP_ENCODING_BITFIELDS)
|
||||
return BMP_DATA_ERROR;
|
||||
if (bmp->encoding == BMP_ENCODING_BITFIELDS) {
|
||||
if ((bmp->bpp != 16) && (bmp->bpp != 32))
|
||||
return BMP_DATA_ERROR;
|
||||
if (header_size == 40) {
|
||||
header_size += 12;
|
||||
if (bmp->buffer_size < (14 + header_size))
|
||||
return BMP_INSUFFICIENT_DATA;
|
||||
for (i = 0; i < 3; i++)
|
||||
bmp->mask[i] = READ_INT(data, 40 + (i << 2));
|
||||
} else {
|
||||
for (i = 0; i < 4; i++)
|
||||
bmp->mask[i] = READ_INT(data, 40 + (i << 2));
|
||||
}
|
||||
for (i = 0; i < 4; i++) {
|
||||
if (bmp->mask[i] == 0)
|
||||
break;
|
||||
for (j = 31; j > 0; j--)
|
||||
if (bmp->mask[i] & (1 << j)) {
|
||||
if ((j - 7) > 0)
|
||||
bmp->mask[i] &= 0xff << (j - 7);
|
||||
else
|
||||
bmp->mask[i] &= 0xff >> (-(j - 7));
|
||||
bmp->shift[i] = (i << 3) - (j - 7);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
bmp->colours = READ_INT(data, 32);
|
||||
if (bmp->colours == 0)
|
||||
bmp->colours = (1 << bmp->bpp);
|
||||
palette_size = 4;
|
||||
/* we don't understand the rest of the data yet */
|
||||
}
|
||||
data += header_size;
|
||||
|
||||
|
@ -256,7 +282,7 @@ bmp_result bmp_analyse_header(struct bmp_image *bmp, char *data) {
|
|||
|
||||
/* create our bitmap */
|
||||
flags = BITMAP_NEW | BITMAP_CLEAR_MEMORY;
|
||||
if (!bmp->ico)
|
||||
if ((!bmp->ico) || (bmp->mask[3] == 0))
|
||||
flags |= BITMAP_OPAQUE;
|
||||
bmp->bitmap = bitmap_create(bmp->width, bmp->height, flags);
|
||||
if (!bmp->bitmap) {
|
||||
|
@ -341,7 +367,7 @@ bmp_result bmp_decode(struct bmp_image *bmp) {
|
|||
if (bmp->bpp >= 24)
|
||||
result = bmp_decode_rgb24(bmp, &data, bytes);
|
||||
else if (bmp->bpp > 8)
|
||||
return BMP_DATA_ERROR;
|
||||
result = bmp_decode_rgb16(bmp, &data, bytes);
|
||||
else
|
||||
result = bmp_decode_rgb(bmp, &data, bytes);
|
||||
break;
|
||||
|
@ -352,6 +378,11 @@ bmp_result bmp_decode(struct bmp_image *bmp) {
|
|||
result = bmp_decode_rle(bmp, data, bytes, 4);
|
||||
break;
|
||||
case BMP_ENCODING_BITFIELDS:
|
||||
if (bmp->bpp == 32)
|
||||
result = bmp_decode_rgb24(bmp, &data, bytes);
|
||||
else if (bmp->bpp == 16)
|
||||
result = bmp_decode_rgb16(bmp, &data, bytes);
|
||||
else
|
||||
return BMP_DATA_ERROR;
|
||||
}
|
||||
|
||||
|
@ -376,6 +407,7 @@ bmp_result bmp_decode_rgb24(struct bmp_image *bmp, char **start, int bytes) {
|
|||
unsigned int *scanline;
|
||||
unsigned int x, y, swidth, skip;
|
||||
unsigned int addr;
|
||||
unsigned int i, word;
|
||||
|
||||
data = *start;
|
||||
swidth = bitmap_get_rowstride(bmp->bitmap);
|
||||
|
@ -395,11 +427,88 @@ bmp_result bmp_decode_rgb24(struct bmp_image *bmp, char **start, int bytes) {
|
|||
scanline = (unsigned int *)(top + (y * swidth));
|
||||
else
|
||||
scanline = (unsigned int *)(bottom - (y * swidth));
|
||||
if (bmp->encoding == BMP_ENCODING_BITFIELDS) {
|
||||
for (x = 0; x < bmp->width; x++) {
|
||||
scanline[x] = data[2] | (data[1] << 8) | (data[0] << 16);
|
||||
word = data[0] | (data[1] << 8) | (data[2] << 16) |
|
||||
(data[3] << 24);
|
||||
scanline[x] = 0;
|
||||
for (i = 0; i < 4; i++)
|
||||
if (bmp->shift[i] > 0)
|
||||
scanline[x] |= ((word & bmp->mask[i]) <<
|
||||
bmp->shift[i]);
|
||||
else
|
||||
scanline[x] |= ((word & bmp->mask[i]) >>
|
||||
(-bmp->shift[i]));
|
||||
data += 4;
|
||||
}
|
||||
} else {
|
||||
for (x = 0; x < bmp->width; x++) {
|
||||
scanline[x] = data[2] | (data[1] << 8) | (data[0] << 16) |
|
||||
(data[3] << 24);
|
||||
data += skip;
|
||||
}
|
||||
}
|
||||
}
|
||||
*start = data;
|
||||
return BMP_OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decode BMP data stored in 16bpp colour.
|
||||
*
|
||||
* \param bmp the BMP image to decode
|
||||
* \param start the data to decode, updated to last byte read on success
|
||||
* \param bytes the number of bytes of data available
|
||||
* \return BMP_OK on success
|
||||
*/
|
||||
bmp_result bmp_decode_rgb16(struct bmp_image *bmp, char **start, int bytes) {
|
||||
char *top, *bottom, *end, *data;
|
||||
unsigned int *scanline;
|
||||
unsigned int x, y, swidth;
|
||||
unsigned int addr;
|
||||
unsigned int word, i;
|
||||
|
||||
data = *start;
|
||||
swidth = bitmap_get_rowstride(bmp->bitmap);
|
||||
top = bitmap_get_buffer(bmp->bitmap);
|
||||
bottom = top + swidth * (bmp->height - 1);
|
||||
end = data + bytes;
|
||||
addr = ((unsigned int)data) & 3;
|
||||
bmp->decoded = true;
|
||||
|
||||
for (y = 0; y < bmp->height; y++) {
|
||||
if (addr != (((unsigned int)data) & 3))
|
||||
data += 2;
|
||||
if ((data + (2 * bmp->width)) > end)
|
||||
return BMP_INSUFFICIENT_DATA;
|
||||
if (bmp->reversed)
|
||||
scanline = (unsigned int *)(top + (y * swidth));
|
||||
else
|
||||
scanline = (unsigned int *)(bottom - (y * swidth));
|
||||
if (bmp->encoding == BMP_ENCODING_BITFIELDS) {
|
||||
for (x = 0; x < bmp->width; x++) {
|
||||
word = data[0] | (data[1] << 8);
|
||||
scanline[x] = 0;
|
||||
for (i = 0; i < 4; i++)
|
||||
if (bmp->shift[i] > 0)
|
||||
scanline[x] |= ((word & bmp->mask[i]) <<
|
||||
bmp->shift[i]);
|
||||
else
|
||||
scanline[x] |= ((word & bmp->mask[i]) >>
|
||||
(-bmp->shift[i]));
|
||||
data += 2;
|
||||
}
|
||||
} else {
|
||||
for (x = 0; x < bmp->width; x++) {
|
||||
word = data[0] | (data[1] << 8);
|
||||
scanline[x] = ((word & (31 << 0)) << 19) |
|
||||
((word & (31 << 5)) << 6) |
|
||||
((word & (31 << 10)) >> 7);
|
||||
data += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
*start = data;
|
||||
return BMP_OK;
|
||||
}
|
||||
|
@ -552,7 +661,7 @@ bmp_result bmp_decode_rle(struct bmp_image *bmp, char *data, int bytes, int size
|
|||
if (y >= bmp->height)
|
||||
return BMP_DATA_ERROR;
|
||||
} else {
|
||||
/* 00 - NN means escape pixels */
|
||||
/* 00 - NN means escape NN pixels */
|
||||
if (bmp->reversed) {
|
||||
pixels_left = (y + 1) * bmp->width - x;
|
||||
scanline = (unsigned int *)(top + (y * swidth));
|
||||
|
|
|
@ -31,7 +31,6 @@ typedef enum {
|
|||
BMP_ENCODING_BITFIELDS = 3
|
||||
} bmp_encoding;
|
||||
|
||||
|
||||
struct bmp_image {
|
||||
unsigned char *bmp_data; /** pointer to BMP data */
|
||||
unsigned int buffer_size; /** total number of bytes of BMP data available */
|
||||
|
@ -45,6 +44,8 @@ struct bmp_image {
|
|||
bool reversed; /** scanlines are top to bottom */
|
||||
bool decoded; /** whether the image has been decoded */
|
||||
bool ico; /** image is part of an ICO, mask follows */
|
||||
unsigned int mask[4]; /** four bitwise mask */
|
||||
int shift[4]; /** four bitwise shifts */
|
||||
struct bitmap *bitmap; /** decoded image */
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue