66 last_error =
"could not open file: ";
67 last_error += file_name;
70 unsigned char bmp_header[14];
71 if (fread(bmp_header, 1, 14, fp) != 14) {
72 last_error =
"could not read bmp header"; fclose(fp); fp = 0;
return false;
74 if (GET_2B(bmp_header,0) != 0x4D42) {
75 last_error =
"bmp header does not start with BM"; fclose(fp); fp = 0;
return false;
77 unsigned char bmp_info[64];
78 if (fread(bmp_info, 1, 4, fp) != 4) {
79 last_error =
"could not read first 4 bytes of bmp info"; fclose(fp); fp = 0;
return false;
81 unsigned int info_size = GET_4B(bmp_info,0);
82 if (!(info_size == 12 || info_size == 40 || info_size == 64)) {
83 last_error =
"unknown size of bmp info"; fclose(fp); fp = 0;
return false;
85 if (fread(bmp_info+4, 1, info_size-4, fp) != info_size-4) {
86 last_error =
"could not read bmp info"; fclose(fp); fp = 0;
return false;
88 unsigned int width = 0, height = 0, planes = 1, bits_per_pixel = 1;
89 switch ((
int) info_size) {
91 width = GET_2B(bmp_info,4);
92 height = GET_2B(bmp_info,6);
93 planes = GET_2B(bmp_info,8);
94 bits_per_pixel = GET_2B(bmp_info,10);
100 width = GET_4B(bmp_info,4);
101 height = GET_4B(bmp_info,8);
102 planes = GET_2B(bmp_info,12);
103 bits_per_pixel = GET_2B(bmp_info,14);
105 if (GET_4B(bmp_info,16) != 0) {
106 last_error =
"compressed bmp files not supported"; fclose(fp); fp = 0;
return false;
108 if (bits_per_pixel == 8 && GET_4B(bmp_info,32) != 256) {
109 last_error =
"only bmp palettes with 256 entries supported allowed"; fclose(fp); fp = 0;
return false;
114 last_error =
"bad number of planes in bmp file"; fclose(fp); fp = 0;
return false;
116 if (!(bits_per_pixel == 8 || bits_per_pixel == 24)) {
117 last_error =
"bad number of bits per pixel in bmp file"; fclose(fp); fp = 0;
return false;
121 int skip_bytes = GET_4B(bmp_header,10) - (info_size + 14);
124 if (bits_per_pixel == 8) {
125 int nr_entries = (info_size == 12) ? 3 : 4;
126 palette.resize(nr_entries*256);
127 if (fread(&palette[0], 1, nr_entries*256, fp) != (
size_t)(nr_entries*256)) {
128 last_error =
"could not read bmp color map"; palette.clear(); fclose(fp); fp = 0;
return false;
130 skip_bytes -= nr_entries*256;
131 for (
unsigned int i = 0; i < 256; ++i) {
132 unsigned char tmp = palette[nr_entries*i];
133 palette[3*i] = palette[nr_entries*i+2];
134 palette[3*i+1] = palette[nr_entries*i+1];
135 palette[3*i+2] = tmp;
139 if (skip_bytes < 0) {
140 last_error =
"bmp incorrect bfOffBits value?"; palette.clear(); fclose(fp); fp = 0;
return false;
142 while (--skip_bytes >= 0) {
145 if (bits_per_pixel == 8 && palette_formats) {
178 unsigned char* data_ptr = dv.
get_ptr<
unsigned char>();
180 if (palette.empty()) {
181 if (fread(data_ptr, 3, n, fp) != n) {
182 last_error =
"bmp read error";
return false;
184 for (
size_t x = 0; x < n; ++x, data_ptr +=3)
185 std::swap(data_ptr[0], data_ptr[2]);
189 if (fread(data_ptr, 1, n, fp) != n) {
190 last_error =
"bmp read error";
return false;
194 unsigned char* dest_ptr = data_ptr + 2*n;
195 for (
size_t x = 0; x < n; ++x) {
198 const unsigned char* p = &palette[3*(*data_ptr)];
205 while ((n & 3) != 0) {