-
November 20th, 2019, 06:24 AM
#1
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).
-
November 20th, 2019, 06:47 AM
#2
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 ...
Last edited by 2kaud; November 20th, 2019 at 09:26 AM.
-
November 20th, 2019, 07:44 AM
#3
Re: Convert custom bitmap to CBitmap/CImage bits format
P.S. The title should be viceversa: from custom format to CImage.
Last edited by 2kaud; November 20th, 2019 at 09:26 AM.
-
November 20th, 2019, 09:22 AM
#4
Re: Convert custom bitmap to CBitmap/CImage bits format
Originally Posted by mesajflaviu
P.S. The title should be viceversa: from custom format to CImage.
Done.
Last edited by 2kaud; November 20th, 2019 at 09:27 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 21st, 2019, 05:49 AM
#5
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 ?
-
November 21st, 2019, 06:37 AM
#6
Re: Convert custom bitmap to CBitmap/CImage bits format
What is CustomObject? Where and how is it defined?
Victor Nijegorodov
-
November 21st, 2019, 06:56 AM
#7
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;
-
November 21st, 2019, 06:59 AM
#8
Re: Convert custom bitmap to CBitmap/CImage bits format
Is it your own class?
What is the physical meaning of "channels"?
Victor Nijegorodov
-
November 21st, 2019, 07:10 AM
#9
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 ...
-
November 21st, 2019, 07:26 AM
#10
Re: Convert custom bitmap to CBitmap/CImage bits format
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".
Victor Nijegorodov
-
November 21st, 2019, 08:35 AM
#11
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 ...
-
November 21st, 2019, 08:40 AM
#12
Re: Convert custom bitmap to CBitmap/CImage bits format
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.
Victor Nijegorodov
-
November 27th, 2019, 07:32 AM
#13
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 ?
-
November 27th, 2019, 08:46 AM
#14
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 ...
-
November 27th, 2019, 09:11 AM
#15
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.
Victor Nijegorodov
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|