CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jan 2009
    Posts
    399

    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).

  2. #2
    Join Date
    Jan 2009
    Posts
    399

    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.

  3. #3
    Join Date
    Jan 2009
    Posts
    399

    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.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Convert custom bitmap to CBitmap/CImage bits format

    Quote Originally Posted by mesajflaviu View Post
    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)

  5. #5
    Join Date
    Jan 2009
    Posts
    399

    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 ?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Convert custom bitmap to CBitmap/CImage bits format

    What is CustomObject? Where and how is it defined?
    Victor Nijegorodov

  7. #7
    Join Date
    Jan 2009
    Posts
    399

    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;

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Convert custom bitmap to CBitmap/CImage bits format

    Is it your own class?
    What is the physical meaning of "channels"?
    Victor Nijegorodov

  9. #9
    Join Date
    Jan 2009
    Posts
    399

    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 ...

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Convert custom bitmap to CBitmap/CImage bits format

    Quote Originally Posted by mesajflaviu View Post
    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

  11. #11
    Join Date
    Jan 2009
    Posts
    399

    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 ...

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Convert custom bitmap to CBitmap/CImage bits format

    Quote Originally Posted by mesajflaviu View Post
    ...
    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

  13. #13
    Join Date
    Jan 2009
    Posts
    399

    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 ?

  14. #14
    Join Date
    Jan 2009
    Posts
    399

    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 ...

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

Page 1 of 2 12 LastLast

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
  •  





Click Here to Expand Forum to Full Width

Featured