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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:25:51+00:00 2026-05-22T00:25:51+00:00

How to convert a 24 Bit PNG to 3 Bit PNG using Floyd–Steinberg dithering

  • 0

How to convert a 24 Bit PNG to 3 Bit PNG using Floyd–Steinberg dithering? java.awt.image.BufferedImage should be used to get and set RGB values.

On wikipedia, an example is given on how to convert a 16 Bit to a 8 Bit image:

find_closest_palette_color(oldpixel) = (oldpixel + 128) / 256

Based on this, are there any ideas on how to fit the example above in order to achieve the goal?

  • 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-05-22T00:25:51+00:00Added an answer on May 22, 2026 at 12:25 am

    Use image.getRGB(x, y) and image.setRGB(x, y, color) and use the pseudocode from the wikipedia article. Note that code on the wiki does not say how to “subtract”, “add” and “multiply” colors. (The T3 class below handles “color” manipulation.)

    The code below will produce this screenshot:

    screenshot

    class Test {
      private static BufferedImage floydSteinbergDithering(BufferedImage img) {
    
        C3[] palette = new C3[] {
            new C3(  0,   0,   0),
            new C3(  0,   0, 255),
            new C3(  0, 255,   0),
            new C3(  0, 255, 255),
            new C3(255,   0,   0),
            new C3(255,   0, 255),
            new C3(255, 255,   0),
            new C3(255, 255, 255)
        };
    
        int w = img.getWidth();
        int h = img.getHeight();
    
        C3[][] d = new C3[h][w];
    
        for (int y = 0; y < h; y++) 
          for (int x = 0; x < w; x++) 
            d[y][x] = new C3(img.getRGB(x, y));
    
        for (int y = 0; y < img.getHeight(); y++) {
          for (int x = 0; x < img.getWidth(); x++) {
    
            C3 oldColor = d[y][x];
            C3 newColor = findClosestPaletteColor(oldColor, palette);
            img.setRGB(x, y, newColor.toColor().getRGB());
    
            C3 err = oldColor.sub(newColor);
    
            if (x+1 < w)         d[y  ][x+1] = d[y  ][x+1].add(err.mul(7./16));
            if (x-1>=0 && y+1<h) d[y+1][x-1] = d[y+1][x-1].add(err.mul(3./16));
            if (y+1 < h)         d[y+1][x  ] = d[y+1][x  ].add(err.mul(5./16));
            if (x+1<w && y+1<h)  d[y+1][x+1] = d[y+1][x+1].add(err.mul(1./16));
          }
        }
    
        return img;
      }
    
      private static C3 findClosestPaletteColor(C3 c, C3[] palette) {
        C3 closest = palette[0];
    
        for (C3 n : palette) 
          if (n.diff(c) < closest.diff(c))
            closest = n;
    
        return closest;
      }
    
      public static void main(String[] args) throws IOException {
    
        final BufferedImage normal  = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png")).getSubimage(100, 100, 300, 300);
        final BufferedImage dietered = floydSteinbergDithering(ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"))).getSubimage(100, 100, 300, 300);
    
        JFrame frame = new JFrame("Test");
        frame.setLayout(new GridLayout(1, 2));
    
        frame.add(new JComponent() {
          @Override
          protected void paintComponent(Graphics g) {
             super.paintComponent(g);
             g.drawImage(normal, 0, 0, this);
          }
        });
        frame.add(new JComponent() {
          @Override
          protected void paintComponent(Graphics g) {
             super.paintComponent(g);
             g.drawImage(dietered, 0, 0, this);
          }
        });
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
      }
    
    
      static class C3 {
        int r, g, b;
    
        public C3(int c) {
          Color color = new Color(c);
          this.r = color.getRed();
          this.g = color.getGreen();
          this.b = color.getBlue();
        }
        public C3(int r, int g, int b) {
          this.r = r;
          this.g = g;
          this.b = b;
        }
    
        public C3 add(C3 o) {
          return new C3(r + o.r, g + o.g, b + o.b);
        }
        public C3 sub(C3 o) {
          return new C3(r - o.r, g - o.g, b - o.b);
        }
        public C3 mul(double d) {
          return new C3((int) (d * r), (int) (d * g), (int) (d * b));
        }
        public int diff(C3 o) {
          return Math.abs(r - o.r) +  Math.abs(g - o.g) +  Math.abs(b - o.b);
        }
    
        public int toRGB() {
          return toColor().getRGB();
        }
        public Color toColor() {
          return new Color(clamp(r), clamp(g), clamp(b));
        }
        public int clamp(int c) {
          return Math.max(0, Math.min(255, c));
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert the input 24 bit PNG image to 8 bit, I
I'm using this code to convert a jpg image into a png 8. The
I'm using ffmpeg to convert .avi file into a .png image sequence. This one
I'd like to, in code and on demand, convert a 32-bit RGBA Image object
I need to convert a PNG Image loaded from a file into an 8
I need to convert different files formats into .png grayscale (8 or 16 bit)
I need to convert 24- and 32-bits jpeg and png-files to a lower bit
I am having different result when cropping two png files. Imagick Version using convert
I have a problem trying to get a .png image to my android phone.
I'm trying to convert the below bit of code into VB (and obviously apply

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.