18 #ifndef PIC_IO_BMP_HPP 19 #define PIC_IO_BMP_HPP 28 #include "../base.hpp" 76 if(((width * bpp / 8) % 4) != 0) {
77 padding = 4 - ((width * bpp / 8) % 4);
95 int &width,
int &height,
int &channels)
97 FILE *file = fopen(nameFile.c_str(),
"rb");
109 fread(&bmpfh.
bfType,
sizeof(
unsigned short), 1, file);
110 fread(&bmpfh.
bfSize,
sizeof(
unsigned int), 1, file);
111 fread(&bmpfh.
bfReserved1,
sizeof(
unsigned short), 1, file);
112 fread(&bmpfh.
bfReserved2,
sizeof(
unsigned short), 1, file);
113 fread(&bmpfh.
bfOffBits,
sizeof(
unsigned int), 1, file);
127 if(!(channels == 3 || channels == 1)) {
138 data =
new unsigned char[width * height * channels];
144 unsigned char *pads = NULL;
147 pads =
new unsigned char[padding];
150 unsigned char tmp[3];
152 for(
int j = (height - 1); j > -1; j--) {
156 for(
int i = 0; i < width; i++) {
157 int c = (cj + i) * 3;
158 fread(tmp,
sizeof(
unsigned char), 3, file);
160 data[c + 2] = tmp[0];
161 data[c + 1] = tmp[1];
167 fread(&data[cj],
sizeof(
unsigned char), width, file);
171 fread(pads,
sizeof(
unsigned char), padding, file);
189 int width,
int height,
int channels)
216 bmpih.
biSize =
sizeof(bmpih);
220 FILE *file = fopen(nameFile.c_str(),
"wb");
229 fwrite(&bmpfh.
bfType,
sizeof(
unsigned short), 1, file);
230 fwrite(&bmpfh.
bfSize,
sizeof(
unsigned int), 1, file);
231 fwrite(&bmpfh.
bfReserved1,
sizeof(
unsigned short), 1, file);
232 fwrite(&bmpfh.
bfReserved2,
sizeof(
unsigned short), 1, file);
233 fwrite(&bmpfh.
bfOffBits,
sizeof(
unsigned int), 1, file);
244 unsigned char *pads = NULL;
247 pads =
new unsigned char[padding];
250 unsigned char tmp[3];
260 for(
int j = (height - 1); j > -1; j--) {
263 for(
int i = 0; i < width; i++) {
264 int c = (cj + i) * channels;
266 tmp[0] = data[c + shiftB];
267 tmp[1] = data[c + shiftG];
269 fwrite(tmp,
sizeof(
unsigned char), 3, file);
273 fwrite(pads,
sizeof(
unsigned char), padding, file);
unsigned short biBitCount
Definition: bmp.hpp:53
unsigned short bfReserved2
Definition: bmp.hpp:41
unsigned int biSize
Definition: bmp.hpp:49
unsigned int bfOffBits
Definition: bmp.hpp:42
unsigned int bfSize
Definition: bmp.hpp:39
The BITMAPFILEHEADER struct.
Definition: bmp.hpp:37
#define BI_RGB
Definition: bmp.hpp:62
unsigned int biCompression
Definition: bmp.hpp:54
unsigned int biClrUsed
Definition: bmp.hpp:58
unsigned short bfReserved1
Definition: bmp.hpp:40
int biYPelsPerMeter
Definition: bmp.hpp:57
unsigned short bfType
Definition: bmp.hpp:38
int biWidth
Definition: bmp.hpp:50
unsigned int biClrImportant
Definition: bmp.hpp:59
unsigned short biPlanes
Definition: bmp.hpp:52
#define PIC_INLINE
Definition: base.hpp:33
PIC_INLINE unsigned char * ReadBMP(std::string nameFile, unsigned char *data, int &width, int &height, int &channels)
ReadBMP reads an image as .bmp file.
Definition: bmp.hpp:94
unsigned int biSizeImage
Definition: bmp.hpp:55
int biHeight
Definition: bmp.hpp:51
PIC_INLINE bool WriteBMP(std::string nameFile, const unsigned char *data, int width, int height, int channels)
WriteBMP writes an image as a .bmp file.
Definition: bmp.hpp:188
int BitmapPadding(int bpp, int width)
BitmapPadding (SYSTEM: X POS Y POS).
Definition: bmp.hpp:72
Definition: bilateral_separation.hpp:25
The BITMAPINFOHEADER struct.
Definition: bmp.hpp:48
int biXPelsPerMeter
Definition: bmp.hpp:56