| 645 | } |
| 646 | |
| 647 | bool LoadBmpWithAlpha(const char *fname, const char *afname, GLubyte **bits, GLsizei *width_, GLsizei *height_) { |
| 648 | GLsizei width, height; |
| 649 | bool retval = false; |
| 650 | // load file and check if it looks reasonable |
| 651 | FILE *fp = fopen(fname, "rb"); |
| 652 | FILE *fp_a = fopen(afname, "rb"); |
| 653 | if (fp && fp_a) { |
| 654 | fseek(fp, 0L, 2); |
| 655 | fseek(fp_a, 0L, 2); |
| 656 | long size = ftell(fp); |
| 657 | long size_a = ftell(fp_a); |
| 658 | unsigned char *data = new unsigned char[size]; |
| 659 | unsigned char *data_a = new unsigned char[size_a]; |
| 660 | if (data && data_a) { |
| 661 | fseek(fp, 0L, 0); |
| 662 | fseek(fp_a, 0L, 0); |
| 663 | if ((fread(data, size, 1, fp) == 1) && (fread(data_a, size_a, 1, fp_a) == 1)) { |
| 664 | CP_BITMAPFILEHEADER *file_header = (CP_BITMAPFILEHEADER *) data; |
| 665 | CP_BITMAPFILEHEADER *file_header_a = (CP_BITMAPFILEHEADER *) data_a; |
| 666 | if (file_header->bfType == MAKEuint16_t('B', 'M')) { |
| 667 | if (file_header->bfSize == (uint32_t) size) { |
| 668 | CP_BITMAPINFO *info = (CP_BITMAPINFO *) (data + sizeof(CP_BITMAPFILEHEADER));// we only handle uncompressed bitmaps |
| 669 | CP_BITMAPINFO *info_a = (CP_BITMAPINFO *) (data_a + sizeof(CP_BITMAPFILEHEADER));// we only handle uncompressed bitmaps |
| 670 | if (info->bmiHeader.biCompression == CP_BI_RGB) { |
| 671 | width = info->bmiHeader.biWidth; |
| 672 | *width_ = width; |
| 673 | if (width > 0) { |
| 674 | height = info->bmiHeader.biHeight; |
| 675 | *height_ = height; |
| 676 | if (height) { |
| 677 | if (height < 0) |
| 678 | height = (-height);// we want RGBA. let's alloc enough space |
| 679 | * |
| 680 | bits = new GLubyte[width * height * 4L]; |
| 681 | if (*bits) { |
| 682 | retval = true; |
| 683 | GLubyte *current_bits = *bits; |
| 684 | GLubyte *pixel = data + file_header->bfOffBits; |
| 685 | GLubyte *pixel_a = data_a + file_header_a->bfOffBits; |
| 686 | GLsizei h = height, w = width; |
| 687 | long padding, padding_a; |
| 688 | switch (info->bmiHeader.biBitCount) {// 24-bit bitmaps |
| 689 | case 8: |
| 690 | padding_a = w % 2; |
| 691 | padding = w % 2; |
| 692 | CP_RGBQUAD rgba; |
| 693 | for (; h > 0; h--) { |
| 694 | for (w = width; w > 0; w--) { |
| 695 | rgba = info->bmiColors[*pixel]; |
| 696 | pixel++; |
| 697 | pixel_a++; |
| 698 | *current_bits++ = rgba.rgbRed; |
| 699 | *current_bits++ = rgba.rgbGreen; |
| 700 | *current_bits++ = rgba.rgbBlue; |
| 701 | *current_bits++ = rgba.rgbRed; |
| 702 | } |
| 703 | pixel += padding; |
| 704 | pixel_a += padding_a; |
no outgoing calls
no test coverage detected