[AD] Additional functionality for load_bmp |
[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
I've had a few complaints from some people using my software that it won't
import some BMP files. I did a little investigation and it turns out these
files use the BI_BITFIELDS compression type, which Allegro doesn't support.
The attached patch adds support for 15-bit, 16-bit and 32-bit BMP files of
this type (according to MSDN, I think these are the only colour depths that
use this type). It works with the two files I have to test it with, so if
you approve please do add it to allegro.
Patch is against BMP.C from Allegro 4.1.12
Cheers,
Chris
276a277,330
> static void read_bitfields_image(PACKFILE *f, BITMAP *bmp, AL_CONST BITMAPINFOHEADER *infoheader)
> {
> int k, i;
> int bpp;
> int bytesPerPixel;
> int red, grn, blu;
> unsigned long buffer;
>
> bpp = bitmap_color_depth(bmp);
>
> if (bpp == 15)
> bytesPerPixel = 2;
> else
> bytesPerPixel = bpp / 8;
>
> for (i=0; i<(int)infoheader->biHeight; i++) {
> for (k=0; k<(int)infoheader->biWidth; k++) {
> pack_fread(&buffer, bytesPerPixel, f);
>
> if (bpp == 15)
> {
> red = (buffer >> 10) & 0x1f;
> grn = (buffer >> 5) & 0x1f;
> blu = (buffer) & 0x1f;
> buffer = (red << _rgb_r_shift_15) |
> (grn << _rgb_g_shift_15) |
> (blu << _rgb_b_shift_15);
> }
> else if (bpp == 16)
> {
> red = (buffer >> 11) & 0x1f;
> grn = (buffer >> 5) & 0x3f;
> blu = (buffer) & 0x1f;
> buffer = (red << _rgb_r_shift_16) |
> (grn << _rgb_g_shift_16) |
> (blu << _rgb_b_shift_16);
> }
> else
> {
> red = (buffer >> 16) & 0xff;
> grn = (buffer >> 8) & 0xff;
> blu = (buffer) & 0xff;
> buffer = (red << _rgb_r_shift_32) |
> (grn << _rgb_g_shift_32) |
> (blu << _rgb_b_shift_32);
>
> }
>
> memcpy(&bmp->line[(infoheader->biHeight - i) - 1][k * bytesPerPixel], &buffer, bytesPerPixel);
> }
> }
> }
>
>
502c556,558
< read_bmicolors(ncol, pal, f, 1);
---
>
> if (infoheader.biCompression != BI_BITFIELDS)
> read_bmicolors(ncol, pal, f, 1);
511c567,569
< read_bmicolors(ncol, pal, f, 0);
---
>
> if (infoheader.biCompression != BI_BITFIELDS)
> read_bmicolors(ncol, pal, f, 0);
519a578,579
> else if (infoheader.biBitCount == 16)
> bpp = 16;
522a583,603
> if (infoheader.biCompression == BI_BITFIELDS)
> {
> unsigned long redMask = pack_igetl(f);
> unsigned long grnMask = pack_igetl(f);
> unsigned long bluMask = pack_igetl(f);
>
> if ((bluMask == 0x001f) && (redMask == 0x7C00))
> bpp = 15;
> else if ((bluMask == 0x001f) && (redMask == 0xF800))
> bpp = 16;
> else if ((bluMask == 0x0000FF) && (redMask == 0xFF0000))
> bpp = 32;
> else
> {
> // Unrecognised bit masks/depth
> pack_fclose(f);
> return NULL;
> }
> }
>
>
545a627,630
>
> case BI_BITFIELDS:
> read_bitfields_image(f, bmp, &infoheader);
> break;