TODO: Integrate into LoadBmpCustomAlpha as a master bitmap loader
| 518 | |
| 519 | // TODO: Integrate into LoadBmpCustomAlpha as a master bitmap loader |
| 520 | bool LoadBmpCustomAlpha(const char *fname, GLubyte **bits, GLsizei *width_, GLsizei *height_, uint8_t alphaColour) { |
| 521 | GLsizei width, height; |
| 522 | bool retval = false; |
| 523 | // load file and check if it looks reasonable |
| 524 | FILE *fp = fopen(fname, "rb"); |
| 525 | if (fp) { |
| 526 | fseek(fp, 0L, 2); |
| 527 | long size = ftell(fp); |
| 528 | if (size > (long) sizeof(CP_BITMAPFILEHEADER)) { |
| 529 | unsigned char *data = new unsigned char[size]; |
| 530 | if (data) { |
| 531 | fseek(fp, 0L, 0); |
| 532 | if (fread(data, size, 1, fp) == 1) { |
| 533 | CP_BITMAPFILEHEADER *file_header = (CP_BITMAPFILEHEADER *) data; |
| 534 | if (file_header->bfType == MAKEuint16_t('B', 'M')) { |
| 535 | if (file_header->bfSize == (uint32_t) size) { |
| 536 | CP_BITMAPINFO *info = (CP_BITMAPINFO *) (data + sizeof(CP_BITMAPFILEHEADER)); |
| 537 | // we only handle uncompressed bitmaps |
| 538 | if (info->bmiHeader.biCompression == CP_BI_RGB) { |
| 539 | width = info->bmiHeader.biWidth; |
| 540 | *width_ = width; |
| 541 | if (width > 0) { |
| 542 | height = info->bmiHeader.biHeight; |
| 543 | *height_ = height; |
| 544 | if (height) { |
| 545 | if (height < 0) height = (-height); |
| 546 | // we want RGBA. let's alloc enough space |
| 547 | *bits = new GLubyte[width * height * 4L]; |
| 548 | if (*bits) { |
| 549 | retval = true; |
| 550 | GLubyte *current_bits = *bits; |
| 551 | GLubyte *pixel = data + file_header->bfOffBits; |
| 552 | GLsizei h = height, w = width; |
| 553 | long padding; |
| 554 | switch (info->bmiHeader.biBitCount) { |
| 555 | // 8-bit palette bitmaps |
| 556 | case 8: |
| 557 | padding = w % 2; |
| 558 | CP_RGBQUAD rgba; |
| 559 | for (; h > 0; h--) { |
| 560 | for (w = width; w > 0; w--) { |
| 561 | rgba = info->bmiColors[*pixel]; |
| 562 | pixel++; |
| 563 | *current_bits++ = rgba.rgbRed; |
| 564 | *current_bits++ = rgba.rgbGreen; |
| 565 | *current_bits++ = rgba.rgbBlue; |
| 566 | if(rgba.rgbRed == 0 && rgba.rgbGreen == alphaColour && rgba.rgbBlue == 0){ |
| 567 | *current_bits++ = 0; |
| 568 | } else { |
| 569 | *current_bits++ = 255; |
| 570 | } |
| 571 | } |
| 572 | pixel += padding; |
| 573 | } |
| 574 | break; |
| 575 | // 24-bit bitmaps |
| 576 | case 24: |
| 577 | padding = (w * 3) % 2; |
no outgoing calls
no test coverage detected