Hello everyone. I have this header in my project - always my tiny 2d engine. I would like to import some simple BMP pictures in my projects. So this code reads a picture (24 or 32 bits), filling data vector according to 3 or 4 bits (RGB and the last one for alpha only for 32 bits). However if I can display pictures as expected, I have a color modification - and sometimes a stretch (like an incrementation on one axis). I guess that I forgot something somewhere... Do you have any idea about what I did wrong? Maybe I have to reverse 4 bits of data? The is an omnipresence of blue color - the third bit. Maybe I have to use color masks for each bits?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
usingnamespace std;
#pragma pack(push,1) // alignment
// BMP file header
struct BMPHeader {
uint16_t file_type{ 0 }; // type of the file, for BMP should be 0x4D42
uint32_t file_size{ 0 }; // size of the files in bytes
uint16_t reserved_1{ 0 }; // reserved
uint16_t reserved_2{ 0 }; // reserved
uint32_t offset{ 0 }; // offset for pixel data from beginning of file
};
// info
struct DIBHeader {
uint32_t size{ 0 }; // size of DIB-header in bytes
int32_t width{ 0 }; // width in pixels
int32_t height{ 0 }; // height in pixels
uint16_t planes{ 1 }; // number of color planes used
uint16_t bit_count{ 0 }; // number of bits per pixel
uint32_t compression{ 0 }; // compression
uint32_t size_image{ 0 }; // size of the raw bitmap data (including padding)
int32_t x_pixels_per_meter{ 0 }; // horizontal resolution of the image (pixel per meter, signed int)
int32_t y_pixels_per_meter{ 0 }; // vertical resolution of the image (pixel per meter, signed int)
uint32_t colors_used{ 0 }; // number of colors in the color palette
uint32_t colors_important{ 0 }; // number of important colors used
};
// mask for colors
struct BMPColorHeader {
uint32_t red_mask{ 0x00FF0000 }; // bit mask for the red channel
uint32_t green_mask{ 0x0000FF00 }; // bit mask for the green channel
uint32_t blue_mask{ 0x000000FF }; // bit mask for the blue channel
uint32_t alpha_mask{ 0xFFF000000 }; // bit mask for the alpha channel
uint32_t color_space_type{ 0x73524742 }; // default "sRGB" (0x73524742)
uint32_t unused[16]{ 0 }; // unused data for sRGB color space
};
#pragma pack(pop)
struct BMP {
BMPHeader file_header;
DIBHeader dib_header;
BMPColorHeader bmp_color_header;
vector<uint32_t> data; // data
gck::Sprite* read(const string filename)
{ // read file
ifstream f{ filename, ios_base::binary };
if (f)
{
f.read((char*)&file_header, sizeof(file_header));
// bad file
if (file_header.file_type != 0x4D42)
throw runtime_error("Error! Not BMP file");
f.read((char*)&dib_header, sizeof(dib_header));
// resize vector to total number of pixels
data.resize(dib_header.width * dib_header.height);
// jump to pixel data location
f.seekg(file_header.offset, f.beg);
int bBit;
// how many bits
switch (dib_header.bit_count)
{
case 24: // 24 bits (no alpha)
bBit = 3;
break;
case 32: // 32 bits (with alpha)
bBit = 4;
break;
default:
throw runtime_error("Error! Only 24 and 32 bits are supported!");
}
// store data in data
for (size_t i = 0; i < dib_header.width * dib_header.height; i++)
{ // minimal base
uint32_t pixel{ NULL };
f.read((char*)&pixel, bBit);
data[i] = pixel; // store pixel
}
gck::Sprite* spr = nullptr;
spr = new gck::Sprite(dib_header.width, dib_header.height);
for (int x = 0; x < dib_header.width; x++)
for (int y = 0; y < dib_header.height; y++)
spr->SetPixel(gck::vi2d(x, y), gck::Pixel(x, y, data[y * dib_header.width + x]));
return spr;
}
}
};
I am trying to simplify process this way :
1 2 3 4
gck::Sprite* spr = nullptr;
string filename = "test3.bmp";
BMP s;
spr = s.read(filename); // my final sprite
Your image is upside down, because by convention, the bottom-left corner is at 0,0
Not the top-left corner.
Though watch out for the height being negative, as this is used to signify an image with 0,0 at the top-left.
Thank you for your answer :/
I did not take row padding into account of what? Data?
There is something wrong when process is reading data. I did a test creating a little BMP file - a simple rectangle 5x3 with a red pixels row, a green pixels row and a blue pixels row. It stores the BMP data, but if I read them in a loop I have something (more or less) logical, but not exact.
1 2 3 4 5 6 7 8 9 10
// store data in data
for (size_t i = 0; i < dib_header.width * dib_header.height; i++)
{ // minimal base
uint32_t pixel{ NULL };
f.read((char*)&pixel, bBit);
data[i] = pixel; // store pixel
}
for (int a = 0; a < data.size(); a++)
cout << data[a] << endl;
I have 15 output values as expected and something which shows 3 different rows. Salem said that I have to take row padding into account, but even reading the previous link I don't understand How I can fix this shifting. Any idea?
https://ibb.co/JrxSjb8
For a 5x3 image of RBG data, each row takes 15 bytes (underlined in respective colours).
But each row begins on a 4-byte boundary, so you have to skip over a byte (this is the row padding) after reading each row (circled in yellow).
pseudocode
1 2 3 4 5 6 7 8 9 10 11 12
for ( h = 0 ; h < dib_header.height ; h++ ) {
for ( w = 0 ; w < dib_header.width ; w++ ) {
// read 3 bytes for a pixel
}
pad = (dib_header.width * 3) % 4;
if ( pad != 0 ) {
while ( pad != 4 ) {
// read a byte and throw it away
pad++;
}
}
}
Thank you everyone for your comments.
Thank you salem c for your clever explanation. Now I understand better, but I have to modify my reader - it's another challenge. I really appreciated your explanation displaying a BMP file according to my previous attempt. It helped me to understand what you meant by row padding. Thank you ++
Duthomhas. Thank you for the link, but I am trying to do something without any other dependencies or code which has been created by another one. However I will use it so as to modify mine.