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 8069977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:14:36+00:00 2026-06-05T13:14:36+00:00

If I do the following using Qt: Load a bitmap in QImage::Format_RGB32. Convert its

  • 0

If I do the following using Qt:

  1. Load a bitmap in QImage::Format_RGB32.
  2. Convert its pixels to RGB565 (no Qt format for this so I must do it “by hand”).
  3. Create a new bitmap the same size as the one loaded in step 1.
  4. Convert the RGB565 buffer pixels back to RGB88 in to the image created in step 3.
  5. The image created from step 4 looks like the image from step 1, however they’re not exactly the same if you compare the RGB values.

Repeating steps 2 to 5 results in the final image losing colour – it seems to become darker and darker.

Here are my conversion functions:

qRgb RGB565ToRGB888( unsigned short int aTextel )
{
    unsigned char r = (((aTextel)&0x01F) <<3);
    unsigned char g = (((aTextel)&0x03E0) >>2);
    unsigned char b = (((aTextel)&0x7C00 )>>7);
    return qRgb( r, g, b, 255 );
}

unsigned short int RGB888ToRGB565( QRgb aPixel )
{
    int red = ( aPixel >> 16) & 0xFF;
    int green = ( aPixel >> 8 ) & 0xFF;
    int blue =  aPixel & 0xFF;

    unsigned short  B = (blue >> 3) & 0x001F;
    unsigned short  G = ((green >> 2) < 5) & 0x07E0;
    unsigned short  R = ((red >> 3) < 11) & 0xF800;

    return (unsigned short int) (R | G | B);
}

An example I found from my test image which doesn’t convert properly is 4278192128 which gets converted back from RGB565 to RGB888 as 4278190080.

Edit: I should also mention that the original source data is RGB565 (which my test RGB888 image was created from). I am only converting to RGB888 for display purposes but would like to convert back to RGB565 afterwards rather than keeping two copies of the data.

  • 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-05T13:14:38+00:00Added an answer on June 5, 2026 at 1:14 pm

    Beforehand I want to mention that the component order in your two conversion functions aren’t the same. In 565 -> 888 conversion, you assume that the red component uses the low order bits (0x001F), but when encoding the 5 bits of the red component, you put them at the high order bits (0xF800). Assuming that you want a component order analogous to 0xAARRGGBB (binary representation in RGB565 is then 0bRRRRRGGGGGGBBBBB), you need to change the variable names in your RGB565ToRGB888 method. I fixed this in the code below.

    Your RGB565 to RGB888 conversion is buggy. For the green channel, you extract 5 bits, which gives you only 7 bit instead of 8 bit in the result. For the blue channel you take the following bits which is a consequential error. This should fix it:

    QRgb RGB565ToRGB888( unsigned short int aTextel )
    {
        // changed order of variable names
        unsigned char b = (((aTextel)&0x001F) << 3);
        unsigned char g = (((aTextel)&0x07E0) >> 3); // Fixed: shift >> 5 and << 2
        unsigned char r = (((aTextel)&0xF800) >> 8); // shift >> 11 and << 3
        return qRgb( r, g, b, 255 );
    }
    

    In the other function, you accidentally wrote less-than operators instead of left-shift operators. This should fix it:

    unsigned short int RGB888ToRGB565( QRgb aPixel )
    {
        int red   = ( aPixel >> 16) & 0xFF;  // why not qRed(aPixel) etc. ?
        int green = ( aPixel >> 8 ) & 0xFF;
        int blue  =   aPixel        & 0xFF;
    
        unsigned short  B =  (blue  >> 3)        & 0x001F;
        unsigned short  G = ((green >> 2) <<  5) & 0x07E0; // not <
        unsigned short  R = ((red   >> 3) << 11) & 0xF800; // not <
    
        return (unsigned short int) (R | G | B);
    }
    

    Note that you can use the already existing (inline) functions qRed, qGreen, qBlue for component extraction analogous to qRgb for color construction from components.

    Also note that the final bit masks in RGB888ToRGB565 are optional, as the component values are in the 8-bit-range and you cropped them by first right-, then left-shifting the values.

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

Sidebar

Related Questions

I am using following code snippet to load url to bitmap in for loop.
I'm using the following code to load an image from file into a bitmap
I'm following this tutorial http://www.gamedev.net/page/resources/_/technical/game-programming/how-to-load-a-bitmap-r1966 and I got the problem of wrong value for
I am using following code to load an image from internal memory to tab
I am using following function to load the page. I have loads of links
I have following method which I am using to load ActiveX control dynamically, Dim
I have couple resource DLLs that I currently load when application starts using following
I am reading javascript web application and the author is using following code: mod.load
I'm using the following code to load a grayscale JPG image, and query a
I'm using the following HQL query to try and load a set of objects

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.