| 6092 | } |
| 6093 | |
| 6094 | static void* stbi__bmp_load( |
| 6095 | stbi__context* s, int* x, int* y, int* comp, int req_comp, |
| 6096 | stbi__result_info* ri) { |
| 6097 | stbi_uc* out; |
| 6098 | unsigned int mr = 0, mg = 0, mb = 0, ma = 0, all_a; |
| 6099 | stbi_uc pal[256][4]; |
| 6100 | int psize = 0, i, j, width; |
| 6101 | int flip_vertically, pad, target; |
| 6102 | stbi__bmp_data info; |
| 6103 | STBI_NOTUSED(ri); |
| 6104 | |
| 6105 | info.all_a = 255; |
| 6106 | if (stbi__bmp_parse_header(s, &info) == NULL) |
| 6107 | return NULL; // error code already set |
| 6108 | |
| 6109 | flip_vertically = ((int)s->img_y) > 0; |
| 6110 | s->img_y = abs((int)s->img_y); |
| 6111 | |
| 6112 | if (s->img_y > STBI_MAX_DIMENSIONS) |
| 6113 | return stbi__errpuc("too large", "Very large image (corrupt?)"); |
| 6114 | if (s->img_x > STBI_MAX_DIMENSIONS) |
| 6115 | return stbi__errpuc("too large", "Very large image (corrupt?)"); |
| 6116 | |
| 6117 | mr = info.mr; |
| 6118 | mg = info.mg; |
| 6119 | mb = info.mb; |
| 6120 | ma = info.ma; |
| 6121 | all_a = info.all_a; |
| 6122 | |
| 6123 | if (info.hsz == 12) { |
| 6124 | if (info.bpp < 24) |
| 6125 | psize = (info.offset - info.extra_read - 24) / 3; |
| 6126 | } else { |
| 6127 | if (info.bpp < 16) |
| 6128 | psize = (info.offset - info.extra_read - info.hsz) >> 2; |
| 6129 | } |
| 6130 | if (psize == 0) { |
| 6131 | if (info.offset != |
| 6132 | s->callback_already_read + (s->img_buffer - s->img_buffer_original)) { |
| 6133 | return stbi__errpuc("bad offset", "Corrupt BMP"); |
| 6134 | } |
| 6135 | } |
| 6136 | |
| 6137 | if (info.bpp == 24 && ma == 0xff000000) |
| 6138 | s->img_n = 3; |
| 6139 | else |
| 6140 | s->img_n = ma ? 4 : 3; |
| 6141 | if (req_comp && req_comp >= 3) // we can directly decode 3 or 4 |
| 6142 | target = req_comp; |
| 6143 | else |
| 6144 | target = s->img_n; // if they want monochrome, we'll post-convert |
| 6145 | |
| 6146 | // sanity-check size |
| 6147 | if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0)) |
| 6148 | return stbi__errpuc("too large", "Corrupt BMP"); |
| 6149 | |
| 6150 | out = (stbi_uc*)stbi__malloc_mad3(target, s->img_x, s->img_y, 0); |
| 6151 | if (!out) |
no test coverage detected