Convert custom bitmap to CBitmap/CImage bits format
I got an custom bitmap format, serialized (read) into file, just like that:
Code:
std::ifstream file(filename, std::ios::binary);
int Height, Width;
file >> Width >> Height;
float scale;
file >> scale;
scale = fabs(scale);
for (int h = 0; h < Height; ++h)
{
for (int w = 0; w < Width; ++w)
{
for (int c = 0; c < C; ++c)
{
float x;
file.read((char*)&x, sizeof(float));
image[((Height - 1 - h) * Width + w) * C + c] = x * scale;
}
}
}
where image is an object that has as constructor CImage(int width, int height, int channels).
Now, I should load this data into a CImage (or CBitmap) MFC object in order to display it. And I know that CImage has CImage::SetPixel method: https://docs.microsoft.com/en-us/cpp...-2019#setpixel, but I don't know how to use it in this case ...
Can you help me to setup CImage pixels to have that custom data valid into CImage object ? I had take a look over CImage::GetBits and CImage::GetPitch methods to see how to arrange data into CImage, but I didn't get it ... (just order way, and data range).
Re: Convert custom bitmap to CBitmap/CImage bits format
I guess I found a way ... but I cannot test yet ...
The solution that I wrote it:
Code:
CImage image;
image.Create((int)W, (int)H, (int)scale);
for (int h = 0; h < H; ++h)
{
for (int w = 0; w < W; ++w)
{
for (int c = 0; c < C; ++c)
{
float x;
file.read((char*)&x, sizeof(float));
image.SetPixel(w, h, (COLORREF)(x * scale));
}
}
}
I don't know if is correct though ...
Re: Convert custom bitmap to CBitmap/CImage bits format
P.S. The title should be viceversa: from custom format to CImage.
Re: Convert custom bitmap to CBitmap/CImage bits format
Quote:
Originally Posted by
mesajflaviu
P.S. The title should be viceversa: from custom format to CImage.
Done.
Re: Convert custom bitmap to CBitmap/CImage bits format
I still have a little question, very valuable question: how to convert COLORREF into channel ? The custom bitmap object need width, height and channel:
Code:
CustomObject(int width, int height, int channels)
the CImage has GetPixel method, that has width and height parameters, but return COLORREF ... and I need to convert it into channel (in fact float, or int) ... how can I do that ?
Re: Convert custom bitmap to CBitmap/CImage bits format
What is CustomObject? Where and how is it defined?
Re: Convert custom bitmap to CBitmap/CImage bits format
CustomObject is a custom bitmap class, and is defined as below:
Code:
CustomObject()
: width(0),
height(0),
channels(0) {}
CustomObject(int width, int height, int channels)
: data(width * height * channels),
width(width),
height(height),
channels(channels) {}
where data is std::vector<float> data;
Re: Convert custom bitmap to CBitmap/CImage bits format
Is it your own class?
What is the physical meaning of "channels"?
Re: Convert custom bitmap to CBitmap/CImage bits format
No, it is not developed by me ... is a part of a project.
The only members of this CustomObject class is
Code:
private:
std::vector<float> data;
int width;
int height;
int channels;
and I need to populate CustomObject with a CImage data ...
Re: Convert custom bitmap to CBitmap/CImage bits format
Quote:
Originally Posted by
mesajflaviu
No, it is not developed by me ... is a part of a project.
The only members of this CustomObject class is
Code:
private:
std::vector<float> data;
int width;
int height;
int channels;
and I need to populate CustomObject with a CImage data ...
Then ask the project designers about the meaning of "channels". :rolleyes:
Re: Convert custom bitmap to CBitmap/CImage bits format
The only way to know what is this member is to look into code source. And channel member is used like this:
Code:
std::vector<float> data;
data = width * height * channel;
and when this object is serialized on file, the source code tell the user:
Code:
// header
int getChannels() const { return channels; }
and
Code:
// cpp, serialize on file
if (customobject.getChannels() != 3)
throw std::invalid_argument("image must have 3 channels");
that is all I know about this member: channel. I know, it is not enough ... that is why I struggle to put this data into CImage ...
Re: Convert custom bitmap to CBitmap/CImage bits format
Quote:
Originally Posted by
mesajflaviu
...
Code:
// cpp, serialize on file
if (customobject.getChannels() != 3)
throw std::invalid_argument("image must have 3 channels");
that is all I know about this member: channel. I know, it is not enough ... that is why I struggle to put this data into CImage ...
Then pass in 3 as the channels.
Re: Convert custom bitmap to CBitmap/CImage bits format
The difference between CImage and CCustomBitmap is that CImage store bytes on bi-dimensional array (width and height), and CCustomBitmap store bits on three-dimensional array (width, height and channel). From this reason, I don't know if I could make a conversion between CImage and CCustomBitmap and viceversa .... or I am wrong ?
See how is de-serialize (read) CCustomBitmap object:
Code:
int Channel = 3;
int Height, Width;
file >> Width >> Height;
float fScale;
file >> fScale; // fScale is always -1
fScale = fabs(fScale); // now fScale is 1.0
// Read the pixels
ImageBuffer image(Width, Height, Channel);
for (int h = 0; h < Height; ++h)
{
for (int w = 0; w < Width; ++w)
{
for (int c = 0; c < Channel; ++c)
{
float x;
file.read((char*)&x, sizeof(float));
image[((Height - 1 - h) * Width + w) * Channel + c] = x * fScale; // fScale is 1
}
}
}
and I tried to de-serialize (read) the CCustomBitmap from CImage
Code:
CImage image;
int Channel = 3;
// Read the pixels
int Width = image.GetWidth(), Height = image.GetHeight();
ImageBuffer imageBuff(Width, Height, Channel);
for (int h = 0; h < Height; ++h)
{
for (int w = 0; w < Width; ++w)
{
for (int c = 0; c < Channel; ++c)
{
int x = static_cast<int>(image.GetPixel(w, h));
imageBuff[((Height - 1 - h) * Width + w) * Channel + c] = static_cast<float>(x);
}
}
}
but is not correct (I load this result into an app that can read CCustomBitmap file and the image is white) ... Do I have a chance to make it ?
Re: Convert custom bitmap to CBitmap/CImage bits format
On the third level, I tried to put the same CImage data:
Code:
for (int c = 0; c < Channel; ++c)
{
int x = static_cast<int>(image.GetPixel(w, h));
imageBuff[((Height - 1 - h) * Width + w) * Channel + c] = static_cast<float>(x);
}
but seem to not work ...
Re: Convert custom bitmap to CBitmap/CImage bits format
Sorry to not help you. But it is only because I have no idea what the physical meaning of the "channel" is.