Visual Computing Library  devel
Loading...
Searching...
No Matches
save.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_IO_IMAGE_BMP_SAVE_H
24#define VCL_IO_IMAGE_BMP_SAVE_H
25
26#include <vclib/io/file_info.h>
27
28namespace vcl {
29
42inline void saveImageToBmp(
43 const std::string& filename,
44 int w,
45 int h,
46 const unsigned char* data)
47{
48 std::string fn = filename;
49 std::string ext = FileInfo::extension(filename);
50 ext = toLower(ext);
51 if (ext != ".bmp") {
52 fn += ".bmp";
53 }
54
55 std::ofstream file(fn, std::ios::binary);
56 if (!file) {
57 throw std::runtime_error("Failed to open file for writing: " + fn);
58 }
59
60 // BMP file header
61 unsigned char fileHeader[14] = {
62 'B',
63 'M', // Signature
64 0,
65 0,
66 0,
67 0, // File size in bytes
68 0,
69 0, // Reserved
70 0,
71 0, // Reserved
72 54,
73 0,
74 0,
75 0 // Offset to pixel data
76 };
77
78 // BMP info header
79 unsigned char infoHeader[40] = {
80 40, 0, 0, 0, // Header size
81 0, 0, 0, 0, // Image width
82 0, 0, 0, 0, // Image height
83 1, 0, // Planes
84 32, 0, // Bits per pixel
85 0, 0, 0, 0, // Compression (none)
86 0, 0, 0, 0, // Image size (can be 0 for uncompressed)
87 0, 0, 0, 0, // X pixels per meter
88 0, 0, 0, 0, // Y pixels per meter
89 0, 0, 0, 0, // Total colors (0 = default)
90 0, 0, 0, 0 // Important colors (0 = all)
91 };
92
93 // Fill in file size
94 int fileSize = 54 + w * h * 4; // Header size + pixel data size
95 fileHeader[2] = (unsigned char) (fileSize);
96 fileHeader[3] = (unsigned char) (fileSize >> 8);
97 fileHeader[4] = (unsigned char) (fileSize >> 16);
98 fileHeader[5] = (unsigned char) (fileSize >> 24);
99
100 // Fill in image width and height
101 infoHeader[4] = (unsigned char) (w);
102 infoHeader[5] = (unsigned char) (w >> 8);
103 infoHeader[6] = (unsigned char) (w >> 16);
104 infoHeader[7] = (unsigned char) (w >> 24);
105 infoHeader[8] = (unsigned char) (h);
106 infoHeader[9] = (unsigned char) (h >> 8);
107 infoHeader[10] = (unsigned char) (h >> 16);
108 infoHeader[11] = (unsigned char) (h >> 24);
109
110 // Write headers
111 file.write(reinterpret_cast<const char*>(fileHeader), sizeof(fileHeader));
112 file.write(reinterpret_cast<const char*>(infoHeader), sizeof(infoHeader));
113
114 // Write pixel data (BMP stores pixels bottom-up)
115 for (int y = h - 1; y >= 0; --y) {
116 file.write(reinterpret_cast<const char*>(data + y * w * 4), w * 4);
117 }
118
119 file.close();
120}
121
122} // namespace vcl
123
124#endif // VCL_IO_IMAGE_BMP_SAVE_H
static std::string extension(const std::string &filename)
Get the extension of a file.
Definition file_info.h:260