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

  • Home
  • SEARCH
  • 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 8281679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:07:05+00:00 2026-06-08T10:07:05+00:00

I’ve been looking for an optimized (i.e., quick) algorithm that converts a 24-bit RGB

  • 0

I’ve been looking for an optimized (i.e., quick) algorithm that converts a 24-bit RGB bitmap to a 16-bit (RGB565) bitmap using dithering. I’m looking for something in C/C++ where I can actually control how the dithering is applied. GDI+ seems to provide some methods, but I can’t tell if they dither or not. And, if they do dither, what mechanism are they using (Floyd-Steinberg?)

Does anyone have a good example of bitmap color-depth conversion with dithering?

  • 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-08T10:07:09+00:00Added an answer on June 8, 2026 at 10:07 am

    As you mentioned, the Floyd-Steinberg dithering method is popular because it’s simple and fast. For the subtle differences between 24-bit and 16-bit color the results will be nearly optimal visually.

    It was suggested that I use the sample picture Lena but I decided against it; despite its long history as a test image I consider it too sexist for modern sensibilities. Instead I present a picture of my own. First up is the original, followed by the conversion to dithered RGB565 (and converted back to 24-bit for display).

    Original
    Floyd-Steinberg Dithered RGB565

    And the code, in C++:

    inline BYTE Clamp(int n)
    {
        n = n>255 ? 255 : n;
        return n<0 ? 0 : n;
    }
    
    struct RGBTriplet
    {
        int r;
        int g;
        int b;
        RGBTriplet(int _r = 0, int _g = 0, int _b = 0) : r(_r), g(_g), b(_b) {};
    };
    
    void RGB565Dithered(const BYTE * pIn, int width, int height, int strideIn, BYTE * pOut, int strideOut)
    {
        std::vector<RGBTriplet> oldErrors(width + 2);
        for (int y = 0;  y < height;  ++y)
        {
            std::vector<RGBTriplet> newErrors(width + 2);
            RGBTriplet errorAhead;
            for (int x = 0;  x < width;  ++x)
            {
                int b = (int)(unsigned int)pIn[3*x] + (errorAhead.b + oldErrors[x+1].b) / 16;
                int g = (int)(unsigned int)pIn[3*x + 1] + (errorAhead.g + oldErrors[x+1].g) / 16;
                int r = (int)(unsigned int)pIn[3*x + 2] + (errorAhead.r + oldErrors[x+1].r) / 16;
                int bAfter = Clamp(b) >> 3;
                int gAfter = Clamp(g) >> 2;
                int rAfter = Clamp(r) >> 3;
                int pixel16 = (rAfter << 11) | (gAfter << 5) | bAfter;
                pOut[2*x] = (BYTE) pixel16;
                pOut[2*x + 1] = (BYTE) (pixel16 >> 8);
                int error = r - ((rAfter * 255) / 31);
                errorAhead.r = error * 7;
                newErrors[x].r += error * 3;
                newErrors[x+1].r += error * 5;
                newErrors[x+2].r = error * 1;
                error = g - ((gAfter * 255) / 63);
                errorAhead.g = error * 7;
                newErrors[x].g += error * 3;
                newErrors[x+1].g += error * 5;
                newErrors[x+2].g = error * 1;
                error = b - ((bAfter * 255) / 31);
                errorAhead.b = error * 7;
                newErrors[x].b += error * 3;
                newErrors[x+1].b += error * 5;
                newErrors[x+2].b = error * 1;
            }
            pIn += strideIn;
            pOut += strideOut;
            oldErrors.swap(newErrors);
        }
    }
    

    I won’t guarantee this code is perfect, I already had to fix one of those subtle errors that I alluded to in another comment. However it did generate the results above. It takes 24-bit pixels in BGR order as used by Windows, and produces R5G6B5 16-bit pixels in little endian order.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.