| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* BMP reader. |
| 12 |
* |
| 13 |
* By Seymour Shlien. |
| 14 |
* |
| 15 |
* OS/2 BMP support and BMP save function by Jonas Petersen. |
| 16 |
* |
| 17 |
* See readme.txt for copyright information. |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include "allegro5/allegro.h" |
| 24 |
#include "allegro5/allegro_image.h" |
| 25 |
#include "allegro5/internal/aintern_convert.h" |
| 26 |
#include "allegro5/internal/aintern_image.h" |
| 27 |
|
| 28 |
#include "iio.h" |
| 29 |
|
| 30 |
ALLEGRO_DEBUG_CHANNEL("image") |
| 31 |
|
| 32 |
/* Do NOT simplify this to just (x), it doesn't work in MSVC. */ |
| 33 |
#define INT_TO_BOOL(x) ((x) != 0) |
| 34 |
|
| 35 |
|
| 36 |
#define BIT_RGB 0 |
| 37 |
#define BIT_RLE8 1 |
| 38 |
#define BIT_RLE4 2 |
| 39 |
#define BIT_BITFIELDS 3 |
| 40 |
|
| 41 |
#define OS2INFOHEADERSIZE 12 |
| 42 |
#define WININFOHEADERSIZE 40 |
| 43 |
#define WININFOHEADERSIZEV2 52 |
| 44 |
#define WININFOHEADERSIZEV3 56 |
| 45 |
#define WININFOHEADERSIZEV4 108 |
| 46 |
#define WININFOHEADERSIZEV5 124 |
| 47 |
|
| 48 |
typedef struct BMPFILEHEADER |
| 49 |
{ |
| 50 |
unsigned short bfType; |
| 51 |
unsigned long bfSize; |
| 52 |
unsigned short bfReserved1; |
| 53 |
unsigned short bfReserved2; |
| 54 |
unsigned long bfOffBits; |
| 55 |
} BMPFILEHEADER; |
| 56 |
|
| 57 |
|
| 58 |
/* Used for both OS/2 and Windows BMP. |
| 59 |
* Contains only the parameters needed to load the image |
| 60 |
*/ |
| 61 |
typedef struct BMPINFOHEADER |
| 62 |
{ |
| 63 |
unsigned long biWidth; |
| 64 |
signed long biHeight; |
| 65 |
unsigned short biBitCount; |
| 66 |
unsigned long biCompression; |
| 67 |
unsigned long biClrUsed; |
| 68 |
uint32_t biRedMask; |
| 69 |
uint32_t biGreenMask; |
| 70 |
uint32_t biBlueMask; |
| 71 |
bool biHaveAlphaMask; |
| 72 |
uint32_t biAlphaMask; |
| 73 |
} BMPINFOHEADER; |
| 74 |
|
| 75 |
|
| 76 |
typedef struct WINBMPINFOHEADER |
| 77 |
{ /* size: 40 */ |
| 78 |
unsigned long biWidth; |
| 79 |
signed long biHeight; |
| 80 |
unsigned short biPlanes; |
| 81 |
unsigned short biBitCount; |
| 82 |
unsigned long biCompression; |
| 83 |
unsigned long biSizeImage; |
| 84 |
unsigned long biXPelsPerMeter; |
| 85 |
unsigned long biYPelsPerMeter; |
| 86 |
unsigned long biClrUsed; |
| 87 |
unsigned long biClrImportant; |
| 88 |
} WINBMPINFOHEADER; |
| 89 |
|
| 90 |
|
| 91 |
typedef struct OS2BMPINFOHEADER |
| 92 |
{ /* size: 12 */ |
| 93 |
unsigned short biWidth; |
| 94 |
unsigned short biHeight; |
| 95 |
unsigned short biPlanes; |
| 96 |
unsigned short biBitCount; |
| 97 |
} OS2BMPINFOHEADER; |
| 98 |
|
| 99 |
|
| 100 |
typedef void(*bmp_line_fn)(ALLEGRO_FILE *f, char *buf, char *data, |
| 101 |
int length, bool premul); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
/* read_bmfileheader: |
| 106 |
* Reads a BMP file header and check that it has the BMP magic number. |
| 107 |
*/ |
| 108 |
static int read_bmfileheader(ALLEGRO_FILE *f, BMPFILEHEADER *fileheader) |
| 109 |
{ |
| 110 |
fileheader->bfType = (uint16_t)al_fread16le(f); |
| 111 |
fileheader->bfSize = (uint32_t)al_fread32le(f); |
| 112 |
fileheader->bfReserved1 = (uint16_t)al_fread16le(f); |
| 113 |
fileheader->bfReserved2 = (uint16_t)al_fread16le(f); |
| 114 |
fileheader->bfOffBits = (uint32_t)al_fread32le(f); |
| 115 |
|
| 116 |
if (fileheader->bfType != 0x4D42) { |
| 117 |
ALLEGRO_WARN("Not BMP format\n"); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
|
| 121 |
if (al_feof(f) || al_ferror(f)) { |
| 122 |
ALLEGRO_ERROR("Failed to read file header\n"); |
| 123 |
return -1; |
| 124 |
} |
| 125 |
|
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
/* read_win_bminfoheader: |
| 132 |
* Reads information from a BMP file header. |
| 133 |
*/ |
| 134 |
static int read_win_bminfoheader(ALLEGRO_FILE *f, BMPINFOHEADER *infoheader) |
| 135 |
{ |
| 136 |
WINBMPINFOHEADER win_infoheader; |
| 137 |
|
| 138 |
win_infoheader.biWidth = (uint32_t)al_fread32le(f); |
| 139 |
win_infoheader.biHeight = al_fread32le(f); |
| 140 |
win_infoheader.biPlanes = (uint16_t)al_fread16le(f); |
| 141 |
win_infoheader.biBitCount = (uint16_t)al_fread16le(f); |
| 142 |
win_infoheader.biCompression = (uint32_t)al_fread32le(f); |
| 143 |
win_infoheader.biSizeImage = (uint32_t)al_fread32le(f); |
| 144 |
win_infoheader.biXPelsPerMeter = (uint32_t)al_fread32le(f); |
| 145 |
win_infoheader.biYPelsPerMeter = (uint32_t)al_fread32le(f); |
| 146 |
win_infoheader.biClrUsed = (uint32_t)al_fread32le(f); |
| 147 |
win_infoheader.biClrImportant = (uint32_t)al_fread32le(f); |
| 148 |
|
| 149 |
infoheader->biWidth = win_infoheader.biWidth; |
| 150 |
infoheader->biHeight = win_infoheader.biHeight; |
| 151 |
infoheader->biBitCount = win_infoheader.biBitCount; |
| 152 |
infoheader->biCompression = win_infoheader.biCompression; |
| 153 |
infoheader->biClrUsed = win_infoheader.biClrUsed; |
| 154 |
|
| 155 |
if (al_feof(f) || al_ferror(f)) { |
| 156 |
ALLEGRO_ERROR("Failed to read file header\n"); |
| 157 |
return -1; |
| 158 |
} |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
/* read_os2_bminfoheader: |
| 166 |
* Reads information from an OS/2 format BMP file header. |
| 167 |
*/ |
| 168 |
static int read_os2_bminfoheader(ALLEGRO_FILE *f, BMPINFOHEADER *infoheader) |
| 169 |
{ |
| 170 |
OS2BMPINFOHEADER os2_infoheader; |
| 171 |
|
| 172 |
os2_infoheader.biWidth = (uint16_t)al_fread16le(f); |
| 173 |
os2_infoheader.biHeight = (uint16_t)al_fread16le(f); |
| 174 |
os2_infoheader.biPlanes = (uint16_t)al_fread16le(f); |
| 175 |
os2_infoheader.biBitCount = (uint16_t)al_fread16le(f); |
| 176 |
|
| 177 |
infoheader->biWidth = os2_infoheader.biWidth; |
| 178 |
infoheader->biHeight = os2_infoheader.biHeight; |
| 179 |
infoheader->biBitCount = os2_infoheader.biBitCount; |
| 180 |
infoheader->biCompression = BIT_RGB; |
| 181 |
infoheader->biClrUsed = 0; /* default */ |
| 182 |
|
| 183 |
if (al_feof(f) || al_ferror(f)) { |
| 184 |
ALLEGRO_ERROR("Failed to read file header\n"); |
| 185 |
return -1; |
| 186 |
} |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
/* decode_bitfield: |
| 194 |
* Converts a bitfield in to a shift+mask pair |
| 195 |
*/ |
| 196 |
static void decode_bitfield(uint32_t m, int *shift_out, uint32_t *mask_out) |
| 197 |
{ |
| 198 |
int shift = 0; |
| 199 |
|
| 200 |
if (m == 0) { |
| 201 |
*shift_out = 0; |
| 202 |
*mask_out = 0; |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
#ifdef __GNUC__ |
| 207 |
shift = __builtin_ctz(m); |
| 208 |
m >>= shift; |
| 209 |
#else |
| 210 |
while ((m & 1) == 0) { |
| 211 |
m >>= 1; |
| 212 |
++shift; |
| 213 |
} |
| 214 |
#endif |
| 215 |
|
| 216 |
*shift_out = shift; |
| 217 |
*mask_out = m; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
/* read_palette: |
| 223 |
* Loads the color palette for 1,4,8 bit formats. |
| 224 |
* OS/2 bitmaps take 3 bytes per color. |
| 225 |
* Windows bitmaps take 4 bytes per color. |
| 226 |
*/ |
| 227 |
static void read_palette(int ncolors, PalEntry *pal, ALLEGRO_FILE *f, |
| 228 |
int flags, const BMPINFOHEADER *infoheader, int win_flag) |
| 229 |
{ |
| 230 |
int i; |
| 231 |
unsigned char c[3]; |
| 232 |
uint32_t r, g, b, a; |
| 233 |
bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA); |
| 234 |
|
| 235 |
int as; |
| 236 |
uint32_t am; |
| 237 |
|
| 238 |
decode_bitfield(infoheader->biAlphaMask, &as, &am); |
| 239 |
|
| 240 |
for (i = 0; i < ncolors; i++) { |
| 241 |
uint32_t pixel; |
| 242 |
al_fread(f, c, 3); |
| 243 |
r = c[2]; |
| 244 |
g = c[1]; |
| 245 |
b = c[0]; |
| 246 |
|
| 247 |
pixel = (r << 16) | (g << 8) | b; |
| 248 |
|
| 249 |
switch (am) { |
| 250 |
case 0x00: |
| 251 |
a = 255; |
| 252 |
break; |
| 253 |
|
| 254 |
case 0x01: |
| 255 |
a = ((pixel >> as) & am) * 255; |
| 256 |
break; |
| 257 |
|
| 258 |
case 0xFF: |
| 259 |
a = ((pixel >> as) & am); |
| 260 |
break; |
| 261 |
|
| 262 |
default: |
| 263 |
a = ((pixel >> as) & am) * 255 / am; |
| 264 |
} |
| 265 |
|
| 266 |
if (am && premul) { |
| 267 |
r = r * a / 255; |
| 268 |
g = g * a / 255; |
| 269 |
b = b * a / 255; |
| 270 |
} |
| 271 |
|
| 272 |
pal[i].r = r; |
| 273 |
pal[i].g = g; |
| 274 |
pal[i].b = b; |
| 275 |
pal[i].a = a; |
| 276 |
|
| 277 |
if (win_flag) { |
| 278 |
al_fgetc(f); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
/* read_16le: |
| 286 |
* Support function for reading 16-bit little endian values |
| 287 |
* from a memory buffer. |
| 288 |
*/ |
| 289 |
static uint16_t read_16le(void *buf) |
| 290 |
{ |
| 291 |
unsigned char *ucbuf = (unsigned char *)buf; |
| 292 |
|
| 293 |
return ucbuf[0] | (ucbuf[1] << 8); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
/* read_32le: |
| 299 |
* Support function for reading 32-bit little endian values |
| 300 |
* from a memory buffer. |
| 301 |
*/ |
| 302 |
static uint32_t read_32le(void *buf) |
| 303 |
{ |
| 304 |
unsigned char *ucbuf = (unsigned char *)buf; |
| 305 |
|
| 306 |
return ucbuf[0] | (ucbuf[1] << 8) | (ucbuf[2] << 16) | (ucbuf[3] << 24); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
/* read_1bit_line: |
| 312 |
* Support function for reading the 1 bit bitmap file format. |
| 313 |
*/ |
| 314 |
static void read_1bit_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 315 |
int length, bool premul) |
| 316 |
{ |
| 317 |
int i, j; |
| 318 |
unsigned char *ucbuf = (unsigned char *)buf; |
| 319 |
size_t bytes_wanted = ((length + 7) / 8 + 3) & ~3; |
| 320 |
|
| 321 |
size_t bytes_read = al_fread(f, ucbuf, bytes_wanted); |
| 322 |
memset(ucbuf + bytes_read, 0, bytes_wanted - bytes_read); |
| 323 |
|
| 324 |
(void)premul; |
| 325 |
(void)data; |
| 326 |
|
| 327 |
for (i = (length - 1) / 8; i >= 0; --i) { |
| 328 |
unsigned char x = ucbuf[i]; |
| 329 |
|
| 330 |
for (j = 0; j < 8; ++j) |
| 331 |
ucbuf[i*8 + 7 - j] = (x & (1 << j)) >> j; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
/* read_2bit_line: |
| 338 |
* Support function for reading the 2 bit bitmap file format. |
| 339 |
*/ |
| 340 |
static void read_2bit_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 341 |
int length, bool premul) |
| 342 |
{ |
| 343 |
int i; |
| 344 |
unsigned char *ucbuf = (unsigned char *)buf; |
| 345 |
size_t bytes_wanted = ((length + 3) / 4 + 3) & ~3; |
| 346 |
|
| 347 |
size_t bytes_read = al_fread(f, ucbuf, bytes_wanted); |
| 348 |
memset(ucbuf + bytes_read, 0, bytes_wanted - bytes_read); |
| 349 |
|
| 350 |
(void)premul; |
| 351 |
(void)data; |
| 352 |
|
| 353 |
for (i = (length - 1) / 4; i >= 0; --i) { |
| 354 |
unsigned char x = ucbuf[i]; |
| 355 |
ucbuf[i*4] = (x & 0xC0) >> 6; |
| 356 |
ucbuf[i*4+1] = (x & 0x30) >> 4; |
| 357 |
ucbuf[i*4+2] = (x & 0x0C) >> 2; |
| 358 |
ucbuf[i*4+3] = (x & 0x03); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
/* read_4bit_line: |
| 365 |
* Support function for reading the 4 bit bitmap file format. |
| 366 |
*/ |
| 367 |
static void read_4bit_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 368 |
int length, bool premul) |
| 369 |
{ |
| 370 |
int i; |
| 371 |
unsigned char *ucbuf = (unsigned char *)buf; |
| 372 |
size_t bytes_wanted = ((length + 1) / 2 + 3) & ~3; |
| 373 |
|
| 374 |
size_t bytes_read = al_fread(f, ucbuf, bytes_wanted); |
| 375 |
memset(ucbuf + bytes_read, 0, bytes_wanted - bytes_read); |
| 376 |
|
| 377 |
(void)premul; |
| 378 |
(void)data; |
| 379 |
|
| 380 |
for (i = (length - 1) / 2; i >= 0; --i) { |
| 381 |
unsigned char x = ucbuf[i]; |
| 382 |
ucbuf[i*2] = (x & 0xF0) >> 4; |
| 383 |
ucbuf[i*2+1] = (x & 0x0F); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
/* read_8bit_line: |
| 390 |
* Support function for reading the 8 bit bitmap file format. |
| 391 |
*/ |
| 392 |
static void read_8bit_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 393 |
int length, bool premul) |
| 394 |
{ |
| 395 |
size_t bytes_wanted = (length + 3) & ~3; |
| 396 |
|
| 397 |
size_t bytes_read = al_fread(f, buf, bytes_wanted); |
| 398 |
memset(buf + bytes_read, 0, bytes_wanted - bytes_read); |
| 399 |
|
| 400 |
(void)premul; |
| 401 |
(void)data; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
/* read_16_rgb_555_line: |
| 407 |
* Support function for reading the 16 bit / RGB555 bitmap file format. |
| 408 |
*/ |
| 409 |
static void read_16_rgb_555_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 410 |
int length, bool premul) |
| 411 |
{ |
| 412 |
int i; |
| 413 |
uint32_t *data32 = (uint32_t *)data; |
| 414 |
size_t bytes_wanted = (length + (length & 1)) * 2; |
| 415 |
|
| 416 |
size_t bytes_read = al_fread(f, buf, bytes_wanted); |
| 417 |
memset(buf + bytes_read, 0, bytes_wanted - bytes_read); |
| 418 |
|
| 419 |
(void)premul; |
| 420 |
|
| 421 |
for (i = 0; i < length; ++i) { |
| 422 |
uint16_t pixel = read_16le(buf + i*2); |
| 423 |
data32[i] = ALLEGRO_CONVERT_RGB_555_TO_ABGR_8888_LE(pixel); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
/* read_16_argb_1555_line: |
| 430 |
* Support function for reading the 16 bit / ARGB1555 bitmap file format. |
| 431 |
*/ |
| 432 |
static void read_16_argb_1555_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 433 |
int length, bool premul) |
| 434 |
{ |
| 435 |
int i; |
| 436 |
uint32_t *data32 = (uint32_t *)data; |
| 437 |
size_t bytes_wanted = (length + (length & 1)) * 2; |
| 438 |
|
| 439 |
size_t bytes_read = al_fread(f, buf, bytes_wanted); |
| 440 |
memset(buf + bytes_read, 0, bytes_wanted - bytes_read); |
| 441 |
|
| 442 |
for (i = 0; i < length; ++i) { |
| 443 |
uint16_t pixel = read_16le(buf + i*2); |
| 444 |
data32[i] = ALLEGRO_CONVERT_ARGB_1555_TO_ABGR_8888_LE(pixel); |
| 445 |
|
| 446 |
if (premul && (pixel & 0x8000)) |
| 447 |
data32[i] = 0; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
/* read_16_rgb_565_line: |
| 454 |
* Support function for reading the 16 bit / RGB565 bitmap file format. |
| 455 |
*/ |
| 456 |
static void read_16_rgb_565_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 457 |
int length, bool premul) |
| 458 |
{ |
| 459 |
int i; |
| 460 |
uint32_t *data32 = (uint32_t *)data; |
| 461 |
size_t bytes_wanted = (length + (length & 1)) * 2; |
| 462 |
|
| 463 |
size_t bytes_read = al_fread(f, buf, bytes_wanted); |
| 464 |
memset(buf + bytes_read, 0, bytes_wanted - bytes_read); |
| 465 |
|
| 466 |
(void)premul; |
| 467 |
|
| 468 |
for (i = 0; i < length; i++) { |
| 469 |
uint16_t pixel = read_16le(buf + i*2); |
| 470 |
data32[i] = ALLEGRO_CONVERT_RGB_565_TO_ABGR_8888_LE(pixel); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
/* read_24_rgb_888_line: |
| 477 |
* Support function for reading the 24 bit / RGB888 bitmap file format. |
| 478 |
*/ |
| 479 |
static void read_24_rgb_888_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 480 |
int length, bool premul) |
| 481 |
{ |
| 482 |
int bi, i; |
| 483 |
unsigned char *ucbuf = (unsigned char *)buf; |
| 484 |
uint32_t *data32 = (uint32_t *)data; |
| 485 |
size_t bytes_wanted = length * 3 + (length & 3); |
| 486 |
|
| 487 |
size_t bytes_read = al_fread(f, buf, bytes_wanted); |
| 488 |
memset(buf + bytes_read, 0, bytes_wanted - bytes_read); |
| 489 |
|
| 490 |
(void)premul; |
| 491 |
|
| 492 |
for (i = 0, bi = 0; i < (length & ~3); i += 4, bi += 3) { |
| 493 |
uint32_t a = read_32le(buf + bi*4); // BGRB [LE:BRGB] |
| 494 |
uint32_t b = read_32le(buf + bi*4 + 4); // GRBG [LE:GBRG] |
| 495 |
uint32_t c = read_32le(buf + bi*4 + 8); // RBGR [LE:RGBR] |
| 496 |
|
| 497 |
uint32_t w = a; |
| 498 |
uint32_t x = (a >> 24) | (b << 8); |
| 499 |
uint32_t y = (b >> 16) | (c << 16); |
| 500 |
uint32_t z = (c >> 8); |
| 501 |
|
| 502 |
data32[i] = ALLEGRO_CONVERT_RGB_888_TO_ABGR_8888_LE(w); |
| 503 |
data32[i+1] = ALLEGRO_CONVERT_RGB_888_TO_ABGR_8888_LE(x); |
| 504 |
data32[i+2] = ALLEGRO_CONVERT_RGB_888_TO_ABGR_8888_LE(y); |
| 505 |
data32[i+3] = ALLEGRO_CONVERT_RGB_888_TO_ABGR_8888_LE(z); |
| 506 |
} |
| 507 |
|
| 508 |
bi *= 4; |
| 509 |
|
| 510 |
for (; i < length; i++, bi += 3) { |
| 511 |
uint32_t pixel = ucbuf[bi] | (ucbuf[bi+1] << 8) | (ucbuf[bi+2] << 16); |
| 512 |
data32[i] = ALLEGRO_CONVERT_RGB_888_TO_ABGR_8888_LE(pixel); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
/* read_32_xrgb_8888_line: |
| 519 |
* Support function for reading the 32 bit / XRGB8888 bitmap file format. |
| 520 |
*/ |
| 521 |
static void read_32_xrgb_8888_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 522 |
int length, bool premul) |
| 523 |
{ |
| 524 |
int i; |
| 525 |
uint32_t *data32 = (uint32_t *)data; |
| 526 |
size_t bytes_wanted = length * 4; |
| 527 |
|
| 528 |
size_t bytes_read = al_fread(f, buf, bytes_wanted); |
| 529 |
memset(buf + bytes_read, 0, bytes_wanted - bytes_read); |
| 530 |
|
| 531 |
(void)premul; |
| 532 |
|
| 533 |
for (i = 0; i < length; i++) { |
| 534 |
uint32_t pixel = read_32le(buf + i*4); |
| 535 |
data32[i] = ALLEGRO_CONVERT_XRGB_8888_TO_ABGR_8888_LE(pixel); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
/* read_32_rgbx_8888_line: |
| 542 |
* Support function for reading the 32 bit / RGBX8888 bitmap file format. |
| 543 |
*/ |
| 544 |
static void read_32_rgbx_8888_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 545 |
int length, bool premul) |
| 546 |
{ |
| 547 |
int i; |
| 548 |
uint32_t *data32 = (uint32_t *)data; |
| 549 |
size_t bytes_wanted = length * 4; |
| 550 |
|
| 551 |
size_t bytes_read = al_fread(f, buf, bytes_wanted); |
| 552 |
memset(buf + bytes_read, 0, bytes_wanted - bytes_read); |
| 553 |
|
| 554 |
(void)premul; |
| 555 |
|
| 556 |
for (i = 0; i < length; i++) { |
| 557 |
uint32_t pixel = read_32le(buf + i*4); |
| 558 |
data32[i] = ALLEGRO_CONVERT_RGBX_8888_TO_ABGR_8888_LE(pixel); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
/* read_32_argb_8888_line: |
| 565 |
* Support function for reading the 32 bit / ARGB8888 bitmap file format. |
| 566 |
*/ |
| 567 |
static void read_32_argb_8888_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 568 |
int length, bool premul) |
| 569 |
{ |
| 570 |
int i; |
| 571 |
uint32_t *data32 = (uint32_t *)data; |
| 572 |
size_t bytes_wanted = length * 4; |
| 573 |
|
| 574 |
size_t bytes_read = al_fread(f, buf, bytes_wanted); |
| 575 |
memset(buf + bytes_read, 0, bytes_wanted - bytes_read); |
| 576 |
|
| 577 |
for (i = 0; i < length; i++) { |
| 578 |
uint32_t pixel = read_32le(buf + i*4); |
| 579 |
uint32_t a = (pixel & 0xFF000000U) >> 24; |
| 580 |
data32[i] = ALLEGRO_CONVERT_ARGB_8888_TO_ABGR_8888_LE(pixel); |
| 581 |
|
| 582 |
if (premul && a != 255) { |
| 583 |
data[i*4+1] = data[i*4+1] * a / 255; |
| 584 |
data[i*4+2] = data[i*4+2] * a / 255; |
| 585 |
data[i*4+3] = data[i*4+3] * a / 255; |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
/* read_32_rgba_8888_line: |
| 593 |
* Support function for reading the 32 bit / RGBA8888 bitmap file format. |
| 594 |
*/ |
| 595 |
static void read_32_rgba_8888_line(ALLEGRO_FILE *f, char *buf, char *data, |
| 596 |
int length, bool premul) |
| 597 |
{ |
| 598 |
int i; |
| 599 |
uint32_t *data32 = (uint32_t *)data; |
| 600 |
size_t bytes_wanted = length * 4; |
| 601 |
|
| 602 |
size_t bytes_read = al_fread(f, buf, bytes_wanted); |
| 603 |
memset(buf + bytes_read, 0, bytes_wanted - bytes_read); |
| 604 |
|
| 605 |
for (i = 0; i < length; i++) { |
| 606 |
uint32_t pixel = read_32le(buf + i*4); |
| 607 |
uint32_t a = (pixel & 0x000000FFU); |
| 608 |
data32[i] = ALLEGRO_CONVERT_RGBA_8888_TO_ABGR_8888_LE(pixel); |
| 609 |
|
| 610 |
if (premul && a != 255) { |
| 611 |
data[i*4] = data[i*4] * a / 255; |
| 612 |
data[i*4+1] = data[i*4+1] * a / 255; |
| 613 |
data[i*4+2] = data[i*4+2] * a / 255; |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
/* read_RGB_image: |
| 621 |
* For reading the standard BMP image format |
| 622 |
*/ |
| 623 |
static bool read_RGB_image(ALLEGRO_FILE *f, int flags, |
| 624 |
const BMPINFOHEADER *infoheader, ALLEGRO_LOCKED_REGION *lr, |
| 625 |
bmp_line_fn fn) |
| 626 |
{ |
| 627 |
int i, line, height, width, dir; |
| 628 |
size_t linesize; |
| 629 |
char *linebuf; |
| 630 |
bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA); |
| 631 |
|
| 632 |
height = infoheader->biHeight; |
| 633 |
width = infoheader->biWidth; |
| 634 |
|
| 635 |
// Includes enough space to read the padding for a line |
| 636 |
linesize = (infoheader->biWidth + 3) & ~3; |
| 637 |
|
| 638 |
if (infoheader->biBitCount < 8) |
| 639 |
linesize *= (8 / infoheader->biBitCount); |
| 640 |
else |
| 641 |
linesize *= (infoheader->biBitCount / 8); |
| 642 |
|
| 643 |
linebuf = al_malloc(linesize); |
| 644 |
|
| 645 |
if (!linebuf) { |
| 646 |
ALLEGRO_WARN("Failed to allocate pixel row buffer\n"); |
| 647 |
return false; |
| 648 |
} |
| 649 |
|
| 650 |
line = height < 0 ? 0 : height - 1; |
| 651 |
dir = height < 0 ? 1 : -1; |
| 652 |
height = abs(height); |
| 653 |
|
| 654 |
for (i = 0; i < height; i++, line += dir) { |
| 655 |
char *data = (char *)lr->data + lr->pitch * line; |
| 656 |
fn(f, linebuf, data, width, premul); |
| 657 |
} |
| 658 |
|
| 659 |
al_free(linebuf); |
| 660 |
|
| 661 |
return true; |
| 662 |
} |
| 663 |
|
| 664 |
/* read_RGB_image_indices: |
| 665 |
* For reading the palette indices from BMP image format |
| 666 |
*/ |
| 667 |
static bool read_RGB_image_indices(ALLEGRO_FILE *f, int flags, |
| 668 |
const BMPINFOHEADER *infoheader, ALLEGRO_LOCKED_REGION *lr, |
| 669 |
bmp_line_fn fn) |
| 670 |
{ |
| 671 |
int i, line, height, width, dir; |
| 672 |
size_t linesize; |
| 673 |
char *linebuf; |
| 674 |
|
| 675 |
(void)flags; |
| 676 |
|
| 677 |
height = infoheader->biHeight; |
| 678 |
width = infoheader->biWidth; |
| 679 |
|
| 680 |
// Includes enough space to read the padding for a line |
| 681 |
linesize = (width + 3) & ~3; |
| 682 |
|
| 683 |
// Indices are always 8-bit, so no need to adjust linesize |
| 684 |
|
| 685 |
linebuf = al_malloc(linesize); |
| 686 |
|
| 687 |
if (!linebuf) { |
| 688 |
ALLEGRO_WARN("Failed to allocate pixel row buffer\n"); |
| 689 |
return false; |
| 690 |
} |
| 691 |
|
| 692 |
if (height < 0) { |
| 693 |
dir = 1; |
| 694 |
line = 0; |
| 695 |
height = -height; |
| 696 |
} else { |
| 697 |
dir = -1; |
| 698 |
line = height - 1; |
| 699 |
} |
| 700 |
|
| 701 |
for (i = 0; i < height; i++, line += dir) { |
| 702 |
char *data = (char *)lr->data + lr->pitch * line; |
| 703 |
fn(f, linebuf, data, width, false); |
| 704 |
memcpy(data, linebuf, width); |
| 705 |
} |
| 706 |
|
| 707 |
al_free(linebuf); |
| 708 |
|
| 709 |
return true; |
| 710 |
} |
| 711 |
|
| 712 |
/* read_RGB_paletted_image: |
| 713 |
* For reading the standard palette mapped BMP image format |
| 714 |
*/ |
| 715 |
static bool read_RGB_paletted_image(ALLEGRO_FILE *f, int flags, |
| 716 |
const BMPINFOHEADER *infoheader, PalEntry* pal, |
| 717 |
ALLEGRO_LOCKED_REGION *lr, bmp_line_fn fn) |
| 718 |
{ |
| 719 |
int i, j, line, height, width, dir; |
| 720 |
size_t linesize; |
| 721 |
char *linebuf; |
| 722 |
|
| 723 |
(void)flags; |
| 724 |
|
| 725 |
height = infoheader->biHeight; |
| 726 |
width = infoheader->biWidth; |
| 727 |
|
| 728 |
// Includes enough space to read the padding for a line |
| 729 |
linesize = (width + 3) & ~3; |
| 730 |
|
| 731 |
if (infoheader->biBitCount < 8) |
| 732 |
linesize *= (8 / infoheader->biBitCount); |
| 733 |
else |
| 734 |
linesize *= (infoheader->biBitCount / 8); |
| 735 |
|
| 736 |
linebuf = al_malloc(linesize); |
| 737 |
|
| 738 |
if (!linebuf) { |
| 739 |
ALLEGRO_WARN("Failed to allocate pixel row buffer\n"); |
| 740 |
return false; |
| 741 |
} |
| 742 |
|
| 743 |
line = height < 0 ? 0 : height - 1; |
| 744 |
dir = height < 0 ? 1 : -1; |
| 745 |
height = abs(height); |
| 746 |
|
| 747 |
for (i = 0; i < height; i++, line += dir) { |
| 748 |
char *data = (char *)lr->data + lr->pitch * line; |
| 749 |
fn(f, linebuf, data, width, false); |
| 750 |
|
| 751 |
for (j = 0; j < width; ++j) { |
| 752 |
unsigned char idx = linebuf[j]; |
| 753 |
data[j*4] = pal[idx].r; |
| 754 |
data[j*4+1] = pal[idx].g; |
| 755 |
data[j*4+2] = pal[idx].b; |
| 756 |
data[j*4+3] = pal[idx].a; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
al_free(linebuf); |
| 761 |
|
| 762 |
return true; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
/* generate_scale_table: |
| 768 |
* Helper function to generate color tables for bitfield format bitmaps. |
| 769 |
*/ |
| 770 |
static void generate_scale_table(int* table, int entries) |
| 771 |
{ |
| 772 |
int i; |
| 773 |
|
| 774 |
for (i = 0; i < entries; ++i) |
| 775 |
table[i] = i * 255 / (entries - 1); |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
/* read_bitfields_image: |
| 781 |
* For reading the generic bitfield compressed BMP image format |
| 782 |
*/ |
| 783 |
static bool read_bitfields_image(ALLEGRO_FILE *f, int flags, |
| 784 |
const BMPINFOHEADER *infoheader, ALLEGRO_LOCKED_REGION *lr) |
| 785 |
{ |
| 786 |
int i, k, line, height, width, dir; |
| 787 |
size_t linesize, bytes_read; |
| 788 |
unsigned char *linebuf; |
| 789 |
int bytes_per_pixel = infoheader->biBitCount / 8; |
| 790 |
bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA); |
| 791 |
|
| 792 |
int rs, gs, bs, as; |
| 793 |
uint32_t rm, gm, bm, am; |
| 794 |
|
| 795 |
// Temporary colour conversion tables for 1..10 bit channels |
| 796 |
// Worst case: ~7KB is temporarily allocated |
| 797 |
int *tempconvert[10]; |
| 798 |
int *rtable = NULL; |
| 799 |
int *gtable = NULL; |
| 800 |
int *btable = NULL; |
| 801 |
int *atable = NULL; |
| 802 |
|
| 803 |
height = infoheader->biHeight; |
| 804 |
width = infoheader->biWidth; |
| 805 |
// Includes enough space to read the padding for a line |
| 806 |
linesize = width * bytes_per_pixel + ((width * bytes_per_pixel) & 3); |
| 807 |
|
| 808 |
// Overallocate by bytes_per_pixel so a 32-bit read can overlap the end |
| 809 |
linebuf = al_malloc((width + 1) * bytes_per_pixel); |
| 810 |
|
| 811 |
if (!linebuf) { |
| 812 |
ALLEGRO_WARN("Failed to allocate pixel row buffer\n"); |
| 813 |
return false; |
| 814 |
} |
| 815 |
|
| 816 |
decode_bitfield(infoheader->biRedMask, &rs, &rm); |
| 817 |
decode_bitfield(infoheader->biGreenMask, &gs, &gm); |
| 818 |
decode_bitfield(infoheader->biBlueMask, &bs, &bm); |
| 819 |
decode_bitfield(infoheader->biAlphaMask, &as, &am); |
| 820 |
|
| 821 |
for (i = 0; i < (int)(sizeof(tempconvert) / sizeof(int *)); ++i) { |
| 822 |
uint32_t mask = ~(0xFFFFFFFFU << (i+1)) & 0xFFFFFFFFU; |
| 823 |
|
| 824 |
switch (i) { |
| 825 |
case 0: tempconvert[i] = _al_rgb_scale_1; break; |
| 826 |
case 3: tempconvert[i] = _al_rgb_scale_4; break; |
| 827 |
case 4: tempconvert[i] = _al_rgb_scale_5; break; |
| 828 |
case 5: tempconvert[i] = _al_rgb_scale_6; break; |
| 829 |
default: |
| 830 |
if (rm == mask || gm == mask || bm == mask || am == mask) { |
| 831 |
int entries = (1 << (i+1)); |
| 832 |
|
| 833 |
// Skip generating tables for tiny images |
| 834 |
if (width * height > entries * 2) { |
| 835 |
tempconvert[i] = al_malloc(sizeof(int) * entries); |
| 836 |
generate_scale_table(tempconvert[i], entries); |
| 837 |
} |
| 838 |
else { |
| 839 |
tempconvert[i] = NULL; |
| 840 |
} |
| 841 |
} |
| 842 |
else { |
| 843 |
tempconvert[i] = NULL; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
if (rm == mask) rtable = tempconvert[i]; |
| 848 |
if (gm == mask) gtable = tempconvert[i]; |
| 849 |
if (bm == mask) btable = tempconvert[i]; |
| 850 |
if (am == mask) atable = tempconvert[i]; |
| 851 |
} |
| 852 |
|
| 853 |
line = height < 0 ? 0 : height - 1; |
| 854 |
dir = height < 0 ? 1 : -1; |
| 855 |
height = abs(height); |
| 856 |
|
| 857 |
for (i = 0; i < height; i++, line += dir) { |
| 858 |
unsigned char *data = (unsigned char *)lr->data + lr->pitch * line; |
| 859 |
|
| 860 |
bytes_read = al_fread(f, linebuf, linesize); |
| 861 |
memset(linebuf + bytes_read, 0, linesize - bytes_read); |
| 862 |
|
| 863 |
for (k = 0; k < width; k++) { |
| 864 |
uint32_t pixel = read_32le(linebuf + k*bytes_per_pixel); |
| 865 |
uint32_t r, g, b, a = 255; |
| 866 |
|
| 867 |
r = ((pixel >> rs) & rm); |
| 868 |
g = ((pixel >> gs) & gm); |
| 869 |
b = ((pixel >> bs) & bm); |
| 870 |
|
| 871 |
if (rtable) r = rtable[r]; |
| 872 |
else if (rm > 0) r = r * 255 / rm; |
| 873 |
|
| 874 |
if (gtable) g = gtable[g]; |
| 875 |
else if (gm > 0) g = g * 255 / gm; |
| 876 |
|
| 877 |
if (btable) b = btable[b]; |
| 878 |
else if (bm > 0) b = b * 255 / bm; |
| 879 |
|
| 880 |
if (am) { |
| 881 |
a = ((pixel >> as) & am); |
| 882 |
|
| 883 |
if (atable) a = atable[a]; |
| 884 |
else a = a * 255 / am; |
| 885 |
|
| 886 |
if (premul) { |
| 887 |
r = r * a / 255; |
| 888 |
g = g * a / 255; |
| 889 |
b = b * a / 255; |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
data[0] = r; |
| 894 |
data[1] = g; |
| 895 |
data[2] = b; |
| 896 |
data[3] = a; |
| 897 |
|
| 898 |
data += 4; |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
al_free(linebuf); |
| 903 |
|
| 904 |
for (i = 0; i < (int)(sizeof(tempconvert) / sizeof(int *)); ++i) { |
| 905 |
if (i != 0 && i != 3 && i != 4 && i != 5) |
| 906 |
al_free(tempconvert[i]); |
| 907 |
} |
| 908 |
|
| 909 |
return true; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
/* read_RGB_image_32bit_alpha_hack: |
| 915 |
* For reading the non-compressed BMP image format (32-bit). |
| 916 |
* These are treatly specially because some programs put alpha information in |
| 917 |
* the fourth byte of each pixel, which is normally just padding (and zero). |
| 918 |
* We use a heuristic: if every pixel has zero in that fourth byte then assume |
| 919 |
* the whole image is opaque (a=255). Otherwise treat the fourth byte as an |
| 920 |
* alpha channel. |
| 921 |
* |
| 922 |
* Note that V3 headers include an alpha bit mask, which can properly indicate |
| 923 |
* the presence or absence of an alpha channel. |
| 924 |
* This hack is not required then. |
| 925 |
*/ |
| 926 |
static bool read_RGB_image_32bit_alpha_hack(ALLEGRO_FILE *f, int flags, |
| 927 |
const BMPINFOHEADER *infoheader, ALLEGRO_LOCKED_REGION *lr) |
| 928 |
{ |
| 929 |
int i, j, line, startline, height, width, dir; |
| 930 |
int have_alpha = 0; |
| 931 |
size_t linesize; |
| 932 |
char *linebuf; |
| 933 |
const bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA); |
| 934 |
|
| 935 |
height = infoheader->biHeight; |
| 936 |
width = infoheader->biWidth; |
| 937 |
|
| 938 |
// Includes enough space to read the padding for a line |
| 939 |
linesize = (infoheader->biWidth + 3) & ~3; |
| 940 |
|
| 941 |
if (infoheader->biBitCount < 8) |
| 942 |
linesize *= (8 / infoheader->biBitCount); |
| 943 |
else |
| 944 |
linesize *= (infoheader->biBitCount / 8); |
| 945 |
|
| 946 |
linebuf = al_malloc(linesize); |
| 947 |
|
| 948 |
if (!linebuf) { |
| 949 |
ALLEGRO_WARN("Failed to allocate pixel row buffer\n"); |
| 950 |
return false; |
| 951 |
} |
| 952 |
|
| 953 |
line = startline = height < 0 ? 0 : height - 1; |
| 954 |
dir = height < 0 ? 1 : -1; |
| 955 |
height = abs(height); |
| 956 |
|
| 957 |
for (i = 0; i < height; i++, line += dir) { |
| 958 |
unsigned char *data = (unsigned char *)lr->data + lr->pitch * line; |
| 959 |
|
| 960 |
/* Don't premultiply alpha here or the image will come out all black */ |
| 961 |
read_32_argb_8888_line(f, linebuf, (char *)data, width, false); |
| 962 |
|
| 963 |
/* Check the alpha values of every pixel in the row */ |
| 964 |
for (j = 0; j < width; j++) { |
| 965 |
have_alpha |= ((data[j*4+3] & 0xFF) != 0); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
/* Fixup pass - make imague opaque or premultiply alpha */ |
| 970 |
if (!have_alpha) { |
| 971 |
line = startline; |
| 972 |
|
| 973 |
for (i = 0; i < height; i++, line += dir) { |
| 974 |
unsigned char *data = (unsigned char *)lr->data + lr->pitch * line; |
| 975 |
|
| 976 |
for (j = 0; j < width; j++) { |
| 977 |
data[j*4+3] = 255; |
| 978 |
} |
| 979 |
} |
| 980 |
} |
| 981 |
else if (premul) { |
| 982 |
line = startline; |
| 983 |
|
| 984 |
for (i = 0; i < height; i++, line += dir) { |
| 985 |
unsigned char *data = (unsigned char *)lr->data + lr->pitch * line; |
| 986 |
|
| 987 |
for (j = 0; j < width; j++) { |
| 988 |
data[j*4] = data[j*4] * data[j*4+3] / 255; |
| 989 |
data[j*4+1] = data[j*4+1] * data[j*4+3] / 255; |
| 990 |
data[j*4+2] = data[j*4+2] * data[j*4+3] / 255; |
| 991 |
} |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
al_free(linebuf); |
| 996 |
|
| 997 |
return true; |
| 998 |
} |
| 999 |
|
| 1000 |
/* read_RLE8_compressed_image: |
| 1001 |
* For reading the 8 bit RLE compressed BMP image format. |
| 1002 |
*/ |
| 1003 |
static bool read_RLE8_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, |
| 1004 |
const BMPINFOHEADER *infoheader) |
| 1005 |
{ |
| 1006 |
int count; |
| 1007 |
unsigned char val; |
| 1008 |
int j, pos, line, width, height, dir; |
| 1009 |
int eolflag, eopicflag; |
| 1010 |
|
| 1011 |
eopicflag = 0; |
| 1012 |
width = (int)infoheader->biWidth; |
| 1013 |
height = abs((int)infoheader->biHeight); |
| 1014 |
line = (infoheader->biHeight < 0) ? 0 : height - 1; |
| 1015 |
dir = (infoheader->biHeight < 0) ? 1 : -1; |
| 1016 |
|
| 1017 |
while (eopicflag == 0) { |
| 1018 |
pos = 0; /* x position in bitmap */ |
| 1019 |
eolflag = 0; /* end of line flag */ |
| 1020 |
|
| 1021 |
while ((eolflag == 0) && (eopicflag == 0)) { |
| 1022 |
count = al_fgetc(f); |
| 1023 |
if (count == EOF) { |
| 1024 |
return false; |
| 1025 |
} |
| 1026 |
val = al_fgetc(f); |
| 1027 |
|
| 1028 |
if (count > 0) { /* repeat pixel count times */ |
| 1029 |
if (count > width - pos) { |
| 1030 |
count = width - pos; |
| 1031 |
} |
| 1032 |
|
| 1033 |
for (j = 0; j < count; j++) { |
| 1034 |
buf[line * width + pos] = val; |
| 1035 |
pos++; |
| 1036 |
} |
| 1037 |
} |
| 1038 |
else { |
| 1039 |
switch (val) { |
| 1040 |
|
| 1041 |
case 0: /* end of line flag */ |
| 1042 |
eolflag = 1; |
| 1043 |
break; |
| 1044 |
|
| 1045 |
case 1: /* end of picture flag */ |
| 1046 |
eopicflag = 1; |
| 1047 |
break; |
| 1048 |
|
| 1049 |
case 2: /* displace picture */ |
| 1050 |
count = al_fgetc(f); |
| 1051 |
if (count == EOF) { |
| 1052 |
return false; |
| 1053 |
} |
| 1054 |
pos += count; |
| 1055 |
count = al_fgetc(f); |
| 1056 |
if (count == EOF) { |
| 1057 |
return false; |
| 1058 |
} |
| 1059 |
line += dir * count; |
| 1060 |
if (line < 0) |
| 1061 |
line = 0; |
| 1062 |
if (line >= height) |
| 1063 |
line = height - 1; |
| 1064 |
break; |
| 1065 |
|
| 1066 |
default: /* read in absolute mode */ |
| 1067 |
count = val; |
| 1068 |
if (count > width - pos) { |
| 1069 |
count = width - pos; |
| 1070 |
} |
| 1071 |
for (j = 0; j < count; j++) { |
| 1072 |
val = al_fgetc(f); |
| 1073 |
buf[line * width + pos] = val; |
| 1074 |
pos++; |
| 1075 |
} |
| 1076 |
|
| 1077 |
if (j % 2 == 1) |
| 1078 |
val = al_fgetc(f); /* align on word boundary */ |
| 1079 |
|
| 1080 |
break; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
if (pos > width + 1) |
| 1085 |
eolflag = 1; |
| 1086 |
} |
| 1087 |
|
| 1088 |
line += dir; |
| 1089 |
if (line < 0 || line >= height) |
| 1090 |
eopicflag = 1; |
| 1091 |
} |
| 1092 |
return true; |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
/* read_RLE4_compressed_image: |
| 1098 |
* For reading the 4 bit RLE compressed BMP image format. |
| 1099 |
*/ |
| 1100 |
static bool read_RLE4_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, |
| 1101 |
const BMPINFOHEADER *infoheader) |
| 1102 |
{ |
| 1103 |
unsigned char b[8]; |
| 1104 |
int count; |
| 1105 |
unsigned short val; |
| 1106 |
int j, k, pos, line, width, height, dir; |
| 1107 |
int eolflag, eopicflag; |
| 1108 |
|
| 1109 |
eopicflag = 0; /* end of picture flag */ |
| 1110 |
width = (int)infoheader->biWidth; |
| 1111 |
height = abs((int)infoheader->biHeight); |
| 1112 |
line = (infoheader->biHeight < 0) ? 0 : height - 1; |
| 1113 |
dir = (infoheader->biHeight < 0) ? 1 : -1; |
| 1114 |
|
| 1115 |
while (eopicflag == 0) { |
| 1116 |
pos = 0; |
| 1117 |
eolflag = 0; /* end of line flag */ |
| 1118 |
|
| 1119 |
while ((eolflag == 0) && (eopicflag == 0)) { |
| 1120 |
count = al_fgetc(f); |
| 1121 |
if (count == EOF) |
| 1122 |
return false; |
| 1123 |
|
| 1124 |
val = al_fgetc(f); |
| 1125 |
|
| 1126 |
if (count > 0) { /* repeat pixels count times */ |
| 1127 |
if (count > width - pos) { |
| 1128 |
count = width - pos; |
| 1129 |
} |
| 1130 |
b[1] = val & 15; |
| 1131 |
b[0] = (val >> 4) & 15; |
| 1132 |
for (j = 0; j < count; j++) { |
| 1133 |
buf[line * width + pos] = b[j % 2]; |
| 1134 |
pos++; |
| 1135 |
} |
| 1136 |
} |
| 1137 |
else { |
| 1138 |
switch (val) { |
| 1139 |
|
| 1140 |
case 0: /* end of line */ |
| 1141 |
eolflag = 1; |
| 1142 |
break; |
| 1143 |
|
| 1144 |
case 1: /* end of picture */ |
| 1145 |
eopicflag = 1; |
| 1146 |
break; |
| 1147 |
|
| 1148 |
case 2: /* displace image */ |
| 1149 |
count = al_fgetc(f); |
| 1150 |
if (count == EOF) |
| 1151 |
return false; |
| 1152 |
pos += count; |
| 1153 |
count = al_fgetc(f); |
| 1154 |
if (count == EOF) |
| 1155 |
return false; |
| 1156 |
line += dir * count; |
| 1157 |
if (line < 0) |
| 1158 |
line = 0; |
| 1159 |
if (line >= height) |
| 1160 |
line = height - 1; |
| 1161 |
break; |
| 1162 |
|
| 1163 |
default: /* read in absolute mode */ |
| 1164 |
count = val; |
| 1165 |
if (count > width - pos) { |
| 1166 |
count = width - pos; |
| 1167 |
} |
| 1168 |
for (j = 0; j < count; j++) { |
| 1169 |
if ((j % 4) == 0) { |
| 1170 |
val = (uint16_t)al_fread16le(f); |
| 1171 |
for (k = 0; k < 2; k++) { |
| 1172 |
b[2 * k + 1] = val & 15; |
| 1173 |
val = val >> 4; |
| 1174 |
b[2 * k] = val & 15; |
| 1175 |
val = val >> 4; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
buf[line * width + pos] = b[j % 4]; |
| 1179 |
pos++; |
| 1180 |
} |
| 1181 |
break; |
| 1182 |
} |
| 1183 |
} |
| 1184 |
|
| 1185 |
if (pos > width + 1) |
| 1186 |
eolflag = 1; |
| 1187 |
} |
| 1188 |
|
| 1189 |
line += dir; |
| 1190 |
if (line < 0 || line >= height) |
| 1191 |
eopicflag = 1; |
| 1192 |
} |
| 1193 |
return true; |
| 1194 |
} |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
/* Like load_bmp, but starts loading from the current place in the ALLEGRO_FILE |
| 1199 |
* specified. If successful the offset into the file will be left just after |
| 1200 |
* the image data. If unsuccessful the offset into the file is unspecified, |
| 1201 |
* i.e. you must either reset the offset to some known place or close the |
| 1202 |
* packfile. The packfile is not closed by this function. |
| 1203 |
*/ |
| 1204 |
ALLEGRO_BITMAP *_al_load_bmp_f(ALLEGRO_FILE *f, int flags) |
| 1205 |
{ |
| 1206 |
BMPFILEHEADER fileheader; |
| 1207 |
BMPINFOHEADER infoheader; |
| 1208 |
ALLEGRO_BITMAP *bmp; |
| 1209 |
PalEntry pal[256]; |
| 1210 |
int64_t file_start; |
| 1211 |
int64_t header_start; |
| 1212 |
unsigned long biSize; |
| 1213 |
unsigned char *buf = NULL; |
| 1214 |
ALLEGRO_LOCKED_REGION *lr; |
| 1215 |
bool keep_index = INT_TO_BOOL(flags & ALLEGRO_KEEP_INDEX); |
| 1216 |
bool loaded_ok; |
| 1217 |
|
| 1218 |
ASSERT(f); |
| 1219 |
|
| 1220 |
file_start = al_ftell(f); |
| 1221 |
|
| 1222 |
if (read_bmfileheader(f, &fileheader) != 0) { |
| 1223 |
return NULL; |
| 1224 |
} |
| 1225 |
|
| 1226 |
header_start = al_ftell(f); |
| 1227 |
|
| 1228 |
biSize = (uint32_t)al_fread32le(f); |
| 1229 |
if (al_feof(f) || al_ferror(f)) { |
| 1230 |
ALLEGRO_ERROR("EOF or file error while reading bitmap header.\n"); |
| 1231 |
return NULL; |
| 1232 |
} |
| 1233 |
|
| 1234 |
switch (biSize) { |
| 1235 |
case WININFOHEADERSIZE: |
| 1236 |
case WININFOHEADERSIZEV2: |
| 1237 |
case WININFOHEADERSIZEV3: |
| 1238 |
case WININFOHEADERSIZEV4: |
| 1239 |
case WININFOHEADERSIZEV5: |
| 1240 |
if (read_win_bminfoheader(f, &infoheader) != 0) { |
| 1241 |
return NULL; |
| 1242 |
} |
| 1243 |
break; |
| 1244 |
|
| 1245 |
case OS2INFOHEADERSIZE: |
| 1246 |
if (read_os2_bminfoheader(f, &infoheader) != 0) { |
| 1247 |
return NULL; |
| 1248 |
} |
| 1249 |
ASSERT(infoheader.biCompression == BIT_RGB); |
| 1250 |
break; |
| 1251 |
|
| 1252 |
default: |
| 1253 |
ALLEGRO_WARN("Unsupported header size: %ld\n", biSize); |
| 1254 |
return NULL; |
| 1255 |
} |
| 1256 |
|
| 1257 |
/* End of header for OS/2 and BITMAPV2INFOHEADER (V1). */ |
| 1258 |
if (biSize == OS2INFOHEADERSIZE || biSize == WININFOHEADERSIZE) { |
| 1259 |
ASSERT(al_ftell(f) == header_start + (int64_t) biSize); |
| 1260 |
} |
| 1261 |
|
| 1262 |
if ((int)infoheader.biWidth < 0) { |
| 1263 |
ALLEGRO_WARN("negative width: %ld\n", infoheader.biWidth); |
| 1264 |
return NULL; |
| 1265 |
} |
| 1266 |
if (infoheader.biCompression != BIT_RGB |
| 1267 |
&& infoheader.biCompression != BIT_RLE8 |
| 1268 |
&& infoheader.biCompression != BIT_RLE4 |
| 1269 |
&& infoheader.biCompression != BIT_BITFIELDS) { |
| 1270 |
ALLEGRO_ERROR("Unsupported compression: 0x%x\n", (int) infoheader.biCompression); |
| 1271 |
return NULL; |
| 1272 |
} |
| 1273 |
if (infoheader.biBitCount != 1 && infoheader.biBitCount != 2 && infoheader.biBitCount != 4 && |
| 1274 |
infoheader.biBitCount != 8 && infoheader.biBitCount != 16 && infoheader.biBitCount != 24 && |
| 1275 |
infoheader.biBitCount != 32) { |
| 1276 |
ALLEGRO_WARN("unsupported bit depth: %d\n", infoheader.biBitCount); |
| 1277 |
return NULL; |
| 1278 |
} |
| 1279 |
|
| 1280 |
if (infoheader.biCompression == BIT_RLE4 && infoheader.biBitCount != 4) { |
| 1281 |
ALLEGRO_WARN("unsupported bit depth for RLE4 compression: %d\n", infoheader.biBitCount); |
| 1282 |
return NULL; |
| 1283 |
} |
| 1284 |
|
| 1285 |
if (infoheader.biCompression == BIT_RLE8 && infoheader.biBitCount != 8) { |
| 1286 |
ALLEGRO_WARN("unsupported bit depth for RLE8 compression: %d\n", infoheader.biBitCount); |
| 1287 |
return NULL; |
| 1288 |
} |
| 1289 |
|
| 1290 |
if (infoheader.biCompression == BIT_BITFIELDS && |
| 1291 |
infoheader.biBitCount != 16 && infoheader.biBitCount != 24 && infoheader.biBitCount != 32) { |
| 1292 |
ALLEGRO_WARN("unsupported bit depth for bitfields compression: %d\n", infoheader.biBitCount); |
| 1293 |
return NULL; |
| 1294 |
} |
| 1295 |
|
| 1296 |
/* In BITMAPINFOHEADER (V1) the RGB bit masks are not part of the header. |
| 1297 |
* In BITMAPV2INFOHEADER they form part of the header, but only valid when |
| 1298 |
* for BITFIELDS images. |
| 1299 |
*/ |
| 1300 |
if (infoheader.biCompression == BIT_BITFIELDS |
| 1301 |
|| biSize >= WININFOHEADERSIZEV2) { |
| 1302 |
infoheader.biRedMask = (uint32_t)al_fread32le(f); |
| 1303 |
infoheader.biGreenMask = (uint32_t)al_fread32le(f); |
| 1304 |
infoheader.biBlueMask = (uint32_t)al_fread32le(f); |
| 1305 |
} |
| 1306 |
|
| 1307 |
/* BITMAPV3INFOHEADER and above include an Alpha bit mask. */ |
| 1308 |
if (biSize < WININFOHEADERSIZEV3) { |
| 1309 |
infoheader.biHaveAlphaMask = false; |
| 1310 |
infoheader.biAlphaMask = 0x0; |
| 1311 |
} |
| 1312 |
else { |
| 1313 |
uint32_t pixel_mask = 0xFFFFFFFFU; |
| 1314 |
infoheader.biHaveAlphaMask = true; |
| 1315 |
infoheader.biAlphaMask = (uint32_t)al_fread32le(f); |
| 1316 |
|
| 1317 |
if (infoheader.biBitCount < 32) |
| 1318 |
pixel_mask = ~(pixel_mask << infoheader.biBitCount) & 0xFFFFFFFFU; |
| 1319 |
|
| 1320 |
if ((infoheader.biAlphaMask & pixel_mask) == 0) { |
| 1321 |
infoheader.biAlphaMask = 0; |
| 1322 |
ALLEGRO_WARN("Ignoring invalid alpha mask\n"); |
| 1323 |
} |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* Seek past the end of the header to reach the palette / image data */ |
| 1327 |
if (biSize > WININFOHEADERSIZEV3) { |
| 1328 |
if (!al_fseek(f, file_start + 14 + biSize, ALLEGRO_SEEK_SET)) { |
| 1329 |
ALLEGRO_ERROR("Seek error\n"); |
| 1330 |
return NULL; |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
if (infoheader.biBitCount <= 8) { |
| 1335 |
int i; |
| 1336 |
for (i = 0; i < 256; ++i) { |
| 1337 |
pal[i].r = 0; |
| 1338 |
pal[i].g = 0; |
| 1339 |
pal[i].b = 0; |
| 1340 |
pal[i].a = 255; |
| 1341 |
} |
| 1342 |
} |
| 1343 |
|
| 1344 |
/* Read the palette, if any. Higher bit depth images _may_ have an optional |
| 1345 |
* palette but we don't use it. |
| 1346 |
*/ |
| 1347 |
if (infoheader.biCompression != BIT_BITFIELDS |
| 1348 |
&& infoheader.biBitCount <= 8) { |
| 1349 |
int win_flag = (biSize != OS2INFOHEADERSIZE); |
| 1350 |
int ncolors = infoheader.biClrUsed; |
| 1351 |
int extracolors = 0; |
| 1352 |
int bytes_per_color = win_flag ? 4 : 3; |
| 1353 |
|
| 1354 |
if (infoheader.biClrUsed >= INT_MAX) { |
| 1355 |
ALLEGRO_ERROR("Illegal palette size: %lu\n", infoheader.biClrUsed); |
| 1356 |
return NULL; |
| 1357 |
} |
| 1358 |
|
| 1359 |
if (win_flag) { |
| 1360 |
if (ncolors == 0) { |
| 1361 |
ncolors = (1 << infoheader.biBitCount); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
else { |
| 1365 |
/* detect palette size for OS2v1 format BMP files */ |
| 1366 |
if (ncolors == 0) { |
| 1367 |
ncolors = (fileheader.bfOffBits - 14 - OS2INFOHEADERSIZE) / 3; |
| 1368 |
} |
| 1369 |
|
| 1370 |
if (ncolors == 0) { |
| 1371 |
ALLEGRO_WARN("No palette in OS2v1 BMP file!\n"); |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (ncolors > 256) { |
| 1376 |
ALLEGRO_WARN("Too many colors: %d\n", ncolors); |
| 1377 |
ncolors = 256; |
| 1378 |
extracolors = ncolors - 256; |
| 1379 |
} |
| 1380 |
|
| 1381 |
read_palette(ncolors, pal, f, flags, &infoheader, win_flag); |
| 1382 |
if (al_feof(f) || al_ferror(f)) { |
| 1383 |
ALLEGRO_ERROR("EOF or I/O error\n"); |
| 1384 |
return NULL; |
| 1385 |
} |
| 1386 |
|
| 1387 |
if (!al_fseek(f, extracolors * bytes_per_color, ALLEGRO_SEEK_SET)) { |
| 1388 |
ALLEGRO_ERROR("Seek error\n"); |
| 1389 |
return NULL; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
else if (infoheader.biClrUsed && infoheader.biBitCount > 8) { |
| 1393 |
int win_flag = (biSize != OS2INFOHEADERSIZE); |
| 1394 |
int bytes_per_color = win_flag ? 4 : 3; |
| 1395 |
|
| 1396 |
if (!al_fseek(f, infoheader.biClrUsed * bytes_per_color, ALLEGRO_SEEK_CUR)) { |
| 1397 |
ALLEGRO_ERROR("Seek error\n"); |
| 1398 |
return NULL; |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
/* Skip to the pixel data only if it's outside of the image metadata */ |
| 1403 |
if (file_start + (int64_t)fileheader.bfOffBits > al_ftell(f)) { |
| 1404 |
if (!al_fseek(f, file_start + fileheader.bfOffBits, ALLEGRO_SEEK_SET)) { |
| 1405 |
ALLEGRO_ERROR("Seek error\n"); |
| 1406 |
return NULL; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
|
| 1410 |
bmp = al_create_bitmap(infoheader.biWidth, abs((int)infoheader.biHeight)); |
| 1411 |
if (!bmp) { |
| 1412 |
ALLEGRO_ERROR("Failed to create bitmap\n"); |
| 1413 |
return NULL; |
| 1414 |
} |
| 1415 |
|
| 1416 |
if (infoheader.biWidth == 0 || infoheader.biHeight == 0) { |
| 1417 |
ALLEGRO_WARN("Creating zero-sized bitmap\n"); |
| 1418 |
return bmp; |
| 1419 |
} |
| 1420 |
|
| 1421 |
if (infoheader.biBitCount <= 8 && keep_index) { |
| 1422 |
lr = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8, |
| 1423 |
ALLEGRO_LOCK_WRITEONLY); |
| 1424 |
} |
| 1425 |
else { |
| 1426 |
lr = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, |
| 1427 |
ALLEGRO_LOCK_WRITEONLY); |
| 1428 |
} |
| 1429 |
|
| 1430 |
if (!lr) { |
| 1431 |
ALLEGRO_ERROR("Failed to lock region\n"); |
| 1432 |
al_destroy_bitmap(bmp); |
| 1433 |
return NULL; |
| 1434 |
} |
| 1435 |
|
| 1436 |
if (infoheader.biCompression == BIT_RLE8 |
| 1437 |
|| infoheader.biCompression == BIT_RLE4) { |
| 1438 |
/* Questionable but most loaders handle this, so we should. */ |
| 1439 |
if (infoheader.biHeight < 0) { |
| 1440 |
ALLEGRO_WARN("compressed bitmap with negative height\n"); |
| 1441 |
} |
| 1442 |
|
| 1443 |
/* RLE decoding may skip pixels so clear the buffer first. */ |
| 1444 |
buf = al_calloc(infoheader.biWidth, abs((int)infoheader.biHeight)); |
| 1445 |
} |
| 1446 |
loaded_ok = true; |
| 1447 |
switch (infoheader.biCompression) { |
| 1448 |
case BIT_RGB: |
| 1449 |
if (infoheader.biBitCount == 32 && !infoheader.biHaveAlphaMask) { |
| 1450 |
if (!read_RGB_image_32bit_alpha_hack(f, flags, &infoheader, lr)) |
| 1451 |
return NULL; |
| 1452 |
} |
| 1453 |
else { |
| 1454 |
bmp_line_fn fn = NULL; |
| 1455 |
|
| 1456 |
switch (infoheader.biBitCount) { |
| 1457 |
case 1: fn = read_1bit_line; break; |
| 1458 |
case 2: fn = read_2bit_line; break; |
| 1459 |
case 4: fn = read_4bit_line; break; |
| 1460 |
case 8: fn = read_8bit_line; break; |
| 1461 |
case 16: fn = read_16_rgb_555_line; break; |
| 1462 |
case 24: fn = read_24_rgb_888_line; break; |
| 1463 |
case 32: fn = read_32_xrgb_8888_line; break; |
| 1464 |
default: |
| 1465 |
ALLEGRO_ERROR("No decoding function for bit depth %d\n", infoheader.biBitCount); |
| 1466 |
return NULL; |
| 1467 |
} |
| 1468 |
|
| 1469 |
if (infoheader.biBitCount == 16 && infoheader.biAlphaMask == 0x00008000U) |
| 1470 |
fn = read_16_argb_1555_line; |
| 1471 |
else if (infoheader.biBitCount == 32 && infoheader.biAlphaMask == 0xFF000000U) |
| 1472 |
fn = read_32_argb_8888_line; |
| 1473 |
if (keep_index) { |
| 1474 |
if (!read_RGB_image_indices(f, flags, &infoheader, lr, fn)) |
| 1475 |
return NULL; |
| 1476 |
} |
| 1477 |
else if (infoheader.biBitCount <= 8) { |
| 1478 |
if (!read_RGB_paletted_image(f, flags, &infoheader, pal, lr, fn)) |
| 1479 |
return NULL; |
| 1480 |
} |
| 1481 |
else { |
| 1482 |
if (!read_RGB_image(f, flags, &infoheader, lr, fn)) |
| 1483 |
return NULL; |
| 1484 |
} |
| 1485 |
} |
| 1486 |
break; |
| 1487 |
|
| 1488 |
case BIT_RLE8: |
| 1489 |
loaded_ok = read_RLE8_compressed_image(f, buf, &infoheader); |
| 1490 |
if (!loaded_ok) |
| 1491 |
ALLEGRO_ERROR("Error reading RLE8 data\n"); |
| 1492 |
break; |
| 1493 |
|
| 1494 |
case BIT_RLE4: |
| 1495 |
loaded_ok = read_RLE4_compressed_image(f, buf, &infoheader); |
| 1496 |
if (!loaded_ok) |
| 1497 |
ALLEGRO_ERROR("Error reading RLE4 data\n"); |
| 1498 |
break; |
| 1499 |
|
| 1500 |
case BIT_BITFIELDS: |
| 1501 |
if (infoheader.biBitCount == 16) { |
| 1502 |
if (infoheader.biRedMask == 0x00007C00U && infoheader.biGreenMask == 0x000003E0U && |
| 1503 |
infoheader.biBlueMask == 0x0000001FU && infoheader.biAlphaMask == 0x00000000U) { |
| 1504 |
loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_16_rgb_555_line); |
| 1505 |
} |
| 1506 |
else if (infoheader.biRedMask == 0x00007C00U && infoheader.biGreenMask == 0x000003E0U && |
| 1507 |
infoheader.biBlueMask == 0x0000001FU && infoheader.biAlphaMask == 0x00008000U) { |
| 1508 |
loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_16_argb_1555_line); |
| 1509 |
} |
| 1510 |
else if (infoheader.biRedMask == 0x0000F800U && infoheader.biGreenMask == 0x000007E0U && |
| 1511 |
infoheader.biBlueMask == 0x0000001FU && infoheader.biAlphaMask == 0x00000000U) { |
| 1512 |
loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_16_rgb_565_line); |
| 1513 |
} |
| 1514 |
else { |
| 1515 |
loaded_ok = read_bitfields_image(f, flags, &infoheader, lr); |
| 1516 |
} |
| 1517 |
} |
| 1518 |
else if (infoheader.biBitCount == 24) { |
| 1519 |
if (infoheader.biRedMask == 0x00FF0000U && infoheader.biGreenMask == 0x0000FF00U && |
| 1520 |
infoheader.biBlueMask == 0x000000FFU && infoheader.biAlphaMask == 0x00000000U) { |
| 1521 |
loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_24_rgb_888_line); |
| 1522 |
} |
| 1523 |
else { |
| 1524 |
loaded_ok = read_bitfields_image(f, flags, &infoheader, lr); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
else if (infoheader.biBitCount == 32) { |
| 1528 |
if (infoheader.biRedMask == 0x00FF0000U && infoheader.biGreenMask == 0x0000FF00U && |
| 1529 |
infoheader.biBlueMask == 0x000000FFU && infoheader.biAlphaMask == 0x00000000U) { |
| 1530 |
loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_32_xrgb_8888_line); |
| 1531 |
} |
| 1532 |
else if (infoheader.biRedMask == 0x00FF0000U && infoheader.biGreenMask == 0x0000FF00U && |
| 1533 |
infoheader.biBlueMask == 0x000000FFU && infoheader.biAlphaMask == 0xFF000000U) { |
| 1534 |
loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_32_argb_8888_line); |
| 1535 |
} |
| 1536 |
else if (infoheader.biRedMask == 0xFF000000U && infoheader.biGreenMask == 0x00FF0000U && |
| 1537 |
infoheader.biBlueMask == 0x0000FF00U && infoheader.biAlphaMask == 0x00000000U) { |
| 1538 |
loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_32_rgbx_8888_line); |
| 1539 |
} |
| 1540 |
else if (infoheader.biRedMask == 0xFF000000U && infoheader.biGreenMask == 0x00FF0000U && |
| 1541 |
infoheader.biBlueMask == 0x0000FF00U && infoheader.biAlphaMask == 0x000000FFU) { |
| 1542 |
loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_32_rgba_8888_line); |
| 1543 |
} |
| 1544 |
else { |
| 1545 |
loaded_ok = read_bitfields_image(f, flags, &infoheader, lr); |
| 1546 |
} |
| 1547 |
} |
| 1548 |
break; |
| 1549 |
|
| 1550 |
default: |
| 1551 |
ALLEGRO_WARN("Unknown compression: %ld\n", infoheader.biCompression); |
| 1552 |
loaded_ok = false; |
| 1553 |
break; |
| 1554 |
} |
| 1555 |
|
| 1556 |
if (infoheader.biCompression == BIT_RLE8 |
| 1557 |
|| infoheader.biCompression == BIT_RLE4) { |
| 1558 |
int x, y; |
| 1559 |
unsigned char *data; |
| 1560 |
|
| 1561 |
for (y = 0; y < abs((int)infoheader.biHeight); y++) { |
| 1562 |
data = (unsigned char *)lr->data + lr->pitch * y; |
| 1563 |
for (x = 0; x < (int)infoheader.biWidth; x++) { |
| 1564 |
if (keep_index) { |
| 1565 |
data[0] = buf[y * infoheader.biWidth + x]; |
| 1566 |
data++; |
| 1567 |
} |
| 1568 |
else { |
| 1569 |
data[0] = pal[buf[y * infoheader.biWidth + x]].r; |
| 1570 |
data[1] = pal[buf[y * infoheader.biWidth + x]].g; |
| 1571 |
data[2] = pal[buf[y * infoheader.biWidth + x]].b; |
| 1572 |
data[3] = 255; |
| 1573 |
data += 4; |
| 1574 |
} |
| 1575 |
} |
| 1576 |
} |
| 1577 |
al_free(buf); |
| 1578 |
} |
| 1579 |
|
| 1580 |
if (bmp) { |
| 1581 |
al_unlock_bitmap(bmp); |
| 1582 |
} |
| 1583 |
/* If something went wrong internally */ |
| 1584 |
if (!loaded_ok) { |
| 1585 |
al_destroy_bitmap(bmp); |
| 1586 |
bmp = NULL; |
| 1587 |
} |
| 1588 |
|
| 1589 |
return bmp; |
| 1590 |
} |
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
/* Like save_bmp but writes into the ALLEGRO_FILE given instead of a new file. |
| 1595 |
* The packfile is not closed after writing is completed. On success the |
| 1596 |
* offset into the file is left after the TGA file just written. On failure |
| 1597 |
* the offset is left at the end of whatever incomplete data was written. |
| 1598 |
*/ |
| 1599 |
bool _al_save_bmp_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp) |
| 1600 |
{ |
| 1601 |
int bfSize; |
| 1602 |
int biSizeImage; |
| 1603 |
int bpp; |
| 1604 |
int filler; |
| 1605 |
int i, j; |
| 1606 |
int w, h; |
| 1607 |
ALLEGRO_LOCKED_REGION *lr; |
| 1608 |
ASSERT(f); |
| 1609 |
ASSERT(bmp); |
| 1610 |
|
| 1611 |
w = al_get_bitmap_width(bmp); |
| 1612 |
h = al_get_bitmap_height(bmp); |
| 1613 |
|
| 1614 |
bpp = 24; |
| 1615 |
filler = 3 - ((w * (bpp / 8) - 1) & 3); |
| 1616 |
biSizeImage = (w * 3 + filler) * h; |
| 1617 |
bfSize = 14 + WININFOHEADERSIZE + biSizeImage; |
| 1618 |
|
| 1619 |
al_set_errno(0); |
| 1620 |
|
| 1621 |
/* file_header */ |
| 1622 |
al_fwrite16le(f, 0x4D42); /* bfType ("BM") */ |
| 1623 |
al_fwrite32le(f, bfSize); /* bfSize */ |
| 1624 |
al_fwrite16le(f, 0); /* bfReserved1 */ |
| 1625 |
al_fwrite16le(f, 0); /* bfReserved2 */ |
| 1626 |
al_fwrite32le(f, 14 + WININFOHEADERSIZE); /* bfOffBits */ |
| 1627 |
|
| 1628 |
/* info_header */ |
| 1629 |
al_fwrite32le(f, WININFOHEADERSIZE); /* biSize */ |
| 1630 |
al_fwrite32le(f, w); /* biWidth */ |
| 1631 |
al_fwrite32le(f, h); /* biHeight */ |
| 1632 |
al_fwrite16le(f, 1); /* biPlanes */ |
| 1633 |
al_fwrite16le(f, bpp); /* biBitCount */ |
| 1634 |
al_fwrite32le(f, BIT_RGB); /* biCompression */ |
| 1635 |
al_fwrite32le(f, biSizeImage); /* biSizeImage */ |
| 1636 |
al_fwrite32le(f, 0xB12); /* biXPelsPerMeter (0xB12 = 72 dpi) */ |
| 1637 |
al_fwrite32le(f, 0xB12); /* biYPelsPerMeter */ |
| 1638 |
|
| 1639 |
al_fwrite32le(f, 0); /* biClrUsed */ |
| 1640 |
al_fwrite32le(f, 0); /* biClrImportant */ |
| 1641 |
|
| 1642 |
/* Don't really need the alpha channel, just the _LE. |
| 1643 |
* Note that there exist 32-bit BMPs now so we could try to save those. |
| 1644 |
*/ |
| 1645 |
lr = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, |
| 1646 |
ALLEGRO_LOCK_READONLY); |
| 1647 |
|
| 1648 |
/* image data */ |
| 1649 |
for (i = h - 1; i >= 0; i--) { |
| 1650 |
unsigned char *data = (unsigned char *)lr->data + i * lr->pitch; |
| 1651 |
|
| 1652 |
for (j = 0; j < w; j++) { |
| 1653 |
unsigned char r = data[0]; |
| 1654 |
unsigned char g = data[1]; |
| 1655 |
unsigned char b = data[2]; |
| 1656 |
data += 4; |
| 1657 |
|
| 1658 |
al_fputc(f, b); |
| 1659 |
al_fputc(f, g); |
| 1660 |
al_fputc(f, r); |
| 1661 |
} |
| 1662 |
|
| 1663 |
for (j = 0; j < filler; j++) |
| 1664 |
al_fputc(f, 0); |
| 1665 |
} |
| 1666 |
|
| 1667 |
al_unlock_bitmap(bmp); |
| 1668 |
|
| 1669 |
return al_get_errno() ? false : true; |
| 1670 |
} |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
ALLEGRO_BITMAP *_al_load_bmp(const char *filename, int flags) |
| 1675 |
{ |
| 1676 |
ALLEGRO_FILE *f; |
| 1677 |
ALLEGRO_BITMAP *bmp; |
| 1678 |
ASSERT(filename); |
| 1679 |
|
| 1680 |
f = al_fopen(filename, "rb"); |
| 1681 |
if (!f) { |
| 1682 |
ALLEGRO_ERROR("Unable to open %s for reading.\n", filename); |
| 1683 |
return NULL; |
| 1684 |
} |
| 1685 |
|
| 1686 |
bmp = _al_load_bmp_f(f, flags); |
| 1687 |
|
| 1688 |
al_fclose(f); |
| 1689 |
|
| 1690 |
return bmp; |
| 1691 |
} |
| 1692 |
|
| 1693 |
|
| 1694 |
bool _al_save_bmp(const char *filename, ALLEGRO_BITMAP *bmp) |
| 1695 |
{ |
| 1696 |
ALLEGRO_FILE *f; |
| 1697 |
bool retsave; |
| 1698 |
bool retclose; |
| 1699 |
ASSERT(filename); |
| 1700 |
|
| 1701 |
f = al_fopen(filename, "wb"); |
| 1702 |
if (!f) { |
| 1703 |
ALLEGRO_ERROR("Unable to open %s for writing.\n", filename); |
| 1704 |
return false; |
| 1705 |
} |
| 1706 |
|
| 1707 |
retsave = _al_save_bmp_f(f, bmp); |
| 1708 |
retclose = al_fclose(f); |
| 1709 |
|
| 1710 |
return retsave && retclose; |
| 1711 |
} |
| 1712 |
|
| 1713 |
|
| 1714 |
bool _al_identify_bmp(ALLEGRO_FILE *f) |
| 1715 |
{ |
| 1716 |
uint16_t x; |
| 1717 |
uint16_t y; |
| 1718 |
|
| 1719 |
y = al_fread16le(f); |
| 1720 |
|
| 1721 |
if (y != 0x4D42) |
| 1722 |
return false; |
| 1723 |
|
| 1724 |
if (!al_fseek(f, 14 - 2, ALLEGRO_SEEK_CUR)) |
| 1725 |
return false; |
| 1726 |
|
| 1727 |
x = al_fread16le(f); |
| 1728 |
switch (x) { |
| 1729 |
case WININFOHEADERSIZE: |
| 1730 |
case WININFOHEADERSIZEV2: |
| 1731 |
case WININFOHEADERSIZEV3: |
| 1732 |
case WININFOHEADERSIZEV4: |
| 1733 |
case WININFOHEADERSIZEV5: |
| 1734 |
case OS2INFOHEADERSIZE: |
| 1735 |
return true; |
| 1736 |
} |
| 1737 |
return false; |
| 1738 |
} |
| 1739 |
|
| 1740 |
/* vim: set sts=3 sw=3 et: */ |