| 31 | typename image_type |
| 32 | > |
| 33 | void load_bmp ( |
| 34 | image_type& image_, |
| 35 | std::istream& in_ |
| 36 | ) |
| 37 | { |
| 38 | image_view<image_type> image(image_); |
| 39 | try |
| 40 | { |
| 41 | unsigned long bytes_read_so_far = 0; |
| 42 | unsigned long bfSize; |
| 43 | unsigned long bfOffBits; |
| 44 | unsigned long biSize; |
| 45 | unsigned long biWidth; |
| 46 | int32 biHeight; |
| 47 | unsigned short biBitCount; |
| 48 | unsigned long biCompression; |
| 49 | unsigned long a, b, c, d, i; |
| 50 | |
| 51 | std::streambuf& in = *in_.rdbuf(); |
| 52 | unsigned char buf[100]; |
| 53 | |
| 54 | |
| 55 | // first make sure the BMP starts with BM |
| 56 | if (in.sgetn(reinterpret_cast<char*>(buf),2) != 2) |
| 57 | throw image_load_error("bmp load error 1: header error"); |
| 58 | bytes_read_so_far += 2; |
| 59 | |
| 60 | if (buf[0] != 'B' || buf[1] != 'M') |
| 61 | throw image_load_error("bmp load error 2: header error"); |
| 62 | |
| 63 | // now read the BITMAPFILEHEADER |
| 64 | if (in.sgetn(reinterpret_cast<char*>(buf),12) != 12) |
| 65 | throw image_load_error("bmp load error 3: header error"); |
| 66 | |
| 67 | bytes_read_so_far += 12; |
| 68 | |
| 69 | i = 0; |
| 70 | a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; |
| 71 | bfSize = a | (b<<8) | (c<<16) | (d<<24); |
| 72 | |
| 73 | // Ignore the next 4 bytes (Reserved Area) |
| 74 | |
| 75 | i = 8; |
| 76 | a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; |
| 77 | bfOffBits = a | (b<<8) | (c<<16) | (d<<24); |
| 78 | |
| 79 | // load the BITMAPINFOHEADER |
| 80 | if (in.sgetn(reinterpret_cast<char*>(buf),40) != 40) |
| 81 | throw image_load_error("bmp load error 5: file too short"); |
| 82 | bytes_read_so_far += 40; |
| 83 | |
| 84 | |
| 85 | i = 0; |
| 86 | a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; |
| 87 | biSize = a | (b<<8) | (c<<16) | (d<<24); |
| 88 | |
| 89 | i += 4; |
| 90 | a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; |