| 54 | } |
| 55 | |
| 56 | void BmpWrite(unsigned char* image, int imageWidth, int imageHeight, const char* filename) { |
| 57 | unsigned char header[54] = {0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, |
| 58 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 32, 0, 0, 0, 0, 0, 0, 0, |
| 59 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 60 | |
| 61 | int64_t file_size = static_cast<int64_t>(imageWidth) * static_cast<int64_t>(imageHeight) * 4 + 54; |
| 62 | header[2] = static_cast<unsigned char>(file_size & 0x000000ff); |
| 63 | header[3] = (file_size >> 8) & 0x000000ff; |
| 64 | header[4] = (file_size >> 16) & 0x000000ff; |
| 65 | header[5] = (file_size >> 24) & 0x000000ff; |
| 66 | |
| 67 | int64_t width = imageWidth; |
| 68 | header[18] = width & 0x000000ff; |
| 69 | header[19] = (width >> 8) & 0x000000ff; |
| 70 | header[20] = (width >> 16) & 0x000000ff; |
| 71 | header[21] = (width >> 24) & 0x000000ff; |
| 72 | |
| 73 | int64_t height = -imageHeight; |
| 74 | header[22] = height & 0x000000ff; |
| 75 | header[23] = (height >> 8) & 0x000000ff; |
| 76 | header[24] = (height >> 16) & 0x000000ff; |
| 77 | header[25] = (height >> 24) & 0x000000ff; |
| 78 | |
| 79 | char fname_bmp[128]; |
| 80 | snprintf(fname_bmp, 128, "%s.bmp", filename); |
| 81 | |
| 82 | FILE* fp; |
| 83 | if (!(fp = fopen(fname_bmp, "wb"))) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | fwrite(header, sizeof(unsigned char), 54, fp); |
| 88 | fwrite(image, sizeof(unsigned char), (size_t)(int64_t)imageWidth * imageHeight * 4, fp); |
| 89 | fclose(fp); |
| 90 | } |
| 91 | |
| 92 | int main() { |
| 93 | auto startTime = GetTimer(); |