Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9040417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:58:34+00:00 2026-06-16T09:58:34+00:00

This may be a long post but I really need to know how to

  • 0

This may be a long post but I really need to know how to Convert between 24 and 32 bit bitmaps. For the sake of the length of this post, I removed the PNG part of my question.

Here goes:

I have a struct like the one below that holds all pixel information:

typedef union RGB
{
    uint32_t Color;
    struct
    {
        unsigned char B, G, R, A;
    } RGBA;
} *PRGB;

std::vector<RGB> Pixels;   //Holds all pixels.

All of the bitmap writing works except when going from 24 to 32 or vice-versa. I don’t know what I’m doing wrong or why 24-32 conversions don’t work. My bitmap reading and writing code is as follows:

Bitmap(const void* Pointer, int Width, int Height, uint32_t BitsPerPixel) //Constructor initialization here...
{
    Pixels.clear();
    if (Pointer == nullptr) {throw std::logic_error("Null Pointer Exception. Pointer is NULL.");}
    if (Width < 1 || Height < 1) {throw std::invalid_argument("Invalid Arguments. Width and Height cannot equal 0.");}
    std::memset(&Info, 0, sizeof(BITMAPINFO));
    size = ((width * BitsPerPixel + 31) / 32) * 4 * height;

    Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    Info.bmiHeader.biWidth = width;
    Info.bmiHeader.biHeight = height;
    Info.bmiHeader.biPlanes = 1;
    Info.bmiHeader.biBitCount = BitsPerPixel;
    Info.bmiHeader.biCompression = BI_RGB;
    Info.bmiHeader.biSizeImage = size;
    bFileHeader.bfType = 0x4D42;
    bFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(Info.bmiHeader);
    bFileHeader.bfSize = bFileHeader.bfOffBits + size;

    const unsigned char* BuffPos = static_cast<const unsigned char*>(Pointer);
    height = (height < 0 ? -height : height);
    Pixels.resize(width * height);

    for (int I = 0; I < height; I++)
    {
        for (int J = 0; J < width; J++)
        {
            Pixels[(height - 1 - I) * width + J].RGBA.B = *(BuffPos++);
            Pixels[(height - 1 - I) * width + J].RGBA.G = *(BuffPos++);
            Pixels[(height - 1 - I) * width + J].RGBA.R = *(BuffPos++);
            Pixels[(height - 1 - I) * width + J].RGBA.A = (Info.bmiHeader.biBitCount > 24 ? *(BuffPos++) : 0);
        }
        if(Info.bmiHeader.biBitCount == 24)
            BuffPos += width % 4;
    }
}

bool SaveBitmap(const char* FilePath)
{
    std::vector<unsigned char> ImageData(size);
    unsigned char* BuffPos = ImageData.data();

    for (int I = 0; I < height; ++I)
    {
        for (int J = 0; J < width; ++J)
        {
            *(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.B;
            *(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.G;
            *(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.R;

            if (Info.bmiHeader.biBitCount > 24)
                *(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.A;
        }
        if(Info.bmiHeader.biBitCount == 24)
            BuffPos += width % 4;
    }

    std::fstream hFile(FilePath, std::fstream::out | std::ofstream::binary);
    if (!hFile.is_open()) return false;

    hFile.write(reinterpret_cast<char*>(&bFileHeader), sizeof(BITMAPFILEHEADER));
    hFile.write(reinterpret_cast<char*>(&Info.bmiHeader), sizeof (BITMAPINFOHEADER));
    hFile.write(reinterpret_cast<char*>(&ImageData[0]), Size());
    hFile.close();

    return true;
}

Any idea what the two problems could be? I want it so that if I called Bitmap(24BmpBuff, W, H, 32); It’ll save as 32. If I do Bitmap(32BmpBuff, W, H, 24) it’ll save as 24 bit. I just can’t see it so I’m hoping one of you will.

I also tried making helper functions:

Convert From 24 bit to 32 bit.

void T24To32(std::vector<RGB> &Input, std::vector<RGB> &Output, int Width, int Height)
{
    Output.resize(Input.size());
    for (int I = 0; I < Height; ++I)
    {
        for (int J = 0; J < Width; ++J)
        {
            Output[J].RGBA.B = Input[J].RGBA.B;
            Output[J].RGBA.G = Input[J].RGBA.G;
            Output[J].RGBA.R = Input[J].RGBA.R;
            Output[J].RGBA.A = 0;
        }
    }
}

Take the unsigned char* of pixels and store them upside down within the struct.

void Pack(int width, int height, int BPP, unsigned char* Input, std::vector<RGB> &Pixels)
{
    unsigned char* BuffPos = Input;
    height = (height < 0 ? -height : height);
    Pixels.resize(width * height);

    for (int I = 0; I < height; I++)
    {
        for (int J = 0; J < width; J++)
        {
            Pixels[(height - 1 - I) * width + J].RGBA.B = *(BuffPos++);
            Pixels[(height - 1 - I) * width + J].RGBA.G = *(BuffPos++);
            Pixels[(height - 1 - I) * width + J].RGBA.R = *(BuffPos++);
            Pixels[(height - 1 - I) * width + J].RGBA.A = (BPP > 24 ? *(BuffPos++) : 0);
        }
        if(BPP == 24)
            BuffPos += width % 4;
    }
}

Take the struct of pixels and store them upright in the unsigned char*.

void Unpack(int width, int height, int BPP, std::vector<RGB> Pixels, unsigned char* &Output)
{
    unsigned char* BuffPos = Output;

    for (int I = 0; I < height; ++I)
    {
        for (int J = 0; J < width; ++J)
        {
            *(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.B;
            *(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.G;
            *(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.R;

            if (BPP > 24)
                *(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.A;
        }
        if(BPP == 24)
            BuffPos += width % 4;
    }
}

I use all of the above like so.. Input image(32 bit): enter image description here

Code:

void Bitmap32ToBitmap24(int Width, int Height)
{
    Bitmap Image("C:/Images/Bitmap32.bmp");
    std::vector<unsigned char> Pixels(((Width * 32 + 31) / 32) * 4 * Height);   //Array large enough to hold 32 bit bmp.
    unsigned char* BuffPos = Pixels.data();

    Unpack(Width, Height, 32, Image.Get(), BuffPos);                            //Fill the array of unsigned char with image pixels being upright

    Bitmap BMP(Pixels.data(), Width, Height, 24);                               //Convert image to 24 bit bmp and save it.
    BMP.Save("C:/Images/Output/Bitmap32ToBitmap24.png");
}

Output image (24 bit): enter image description here
24 to 32 results in:
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:58:35+00:00Added an answer on June 16, 2026 at 9:58 am

    In all your code snippets

    if(Info.bmiHeader.biBitCount == 24)
            BuffPos += width % 4;
    

    or

    if(BPP == 24)
            BuffPos += width % 4;
    

    occur. I assume this should add the padding value to each line. But it isn’t the padding, it is the number of pixels per line %4.

    The correct adding value is (4 - ((width * 3) % 4)) % 4. The width*3 is the number of bytes in that line. The %4 calculates the number of bytes which are to many for a 4 byte padding, but to fill up to the next higher limes we need 4-this value. This again is 4 if no padding offset is needed -> %4 to avoid that.

    A faster way to compute the same value is (-width * 3) & 3. See wiki.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This post may seem long.. But it's very simple. So please please help. Thanks.
this may sound pretty straight forward, but still I want to post this question
This may seem a bit crazy, but if you can tell me a better
This may sound impossible but read on. I need to learn jQuery a little
this post is a long one sorry for that but the problem is complex
First post, but I've been lurking around this site for a long while now,
This post may seem overly long for just the short question at the end
I have to work with strings which may contain Lat/Long data, like this: $query
This may be a stupid question but I have a code with the following
This may be have a better name than custom tab completion, but here's the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.