title: Floyd-SteinBerg dithering program bug. java
body: <p>Hey im trying to create a program which uses Floyd-Steinberg's dithering algorithm to produce a dithered version of a image.</p>
  <p>the code for my program is below.</p>
  <p>but I believe I am getting an error with with the rounding in the &quot;calculateErr&quot; multiplication with the diviser.
  the image I am using to test is this cat one: <a href="https://i.stack.imgur.com/Qy4Zb.jpg" rel="nofollow noreferrer">Cat Image</a></p>
  <p>and for some reason it ends up looking like this <a href="https://i.stack.imgur.com/VYn5b.jpg" rel="nofollow noreferrer">Dithered cat image</a></p>
  <p>any solutions to what I am doing wrong would be greatly appreciated.</p>
  <pre><code>import java.awt.*;
  import javax.imageio.ImageIO;
  import javax.swing.JFrame;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  
  public class Dithering extends Canvas {
      public BufferedImage ditheredIMG = new BufferedImage(481,480,
              BufferedImage.TYPE_BYTE_GRAY);
  
  public void paint(Graphics g) {
      BufferedImage i = null;
      try {
          i = displayImage(g);
      } catch (IOException e) {
          throw new RuntimeException(e);
      }
      getPixels(g, i);
      g.drawImage(ditheredIMG,480,0,this);
  
  
  
  }
  public static void main(String[] args) {
      Dithering d = new Dithering();
      JFrame f =new JFrame();
      f.add(d);
      f.setSize( 960,481);
      f.setVisible(true);
  }
  
  
  public BufferedImage displayImage(Graphics g) throws IOException {
      final File file = new File(&quot;Cat.jpg&quot;);
      final BufferedImage i = ImageIO.read(file);
      g.drawImage(i, 0,0,this);
      return i;
  }
  
  public void getPixels(Graphics g, BufferedImage i) {
      for (int y = 1; y &lt; i.getHeight()-1; y++){
          for (int x = 1; x &lt; i.getWidth()-1; x++) {
              int pixelValue = i.getRGB(x, y);
              int  red   = (pixelValue &amp; 0x00ff0000) &gt;&gt; 16;
              int  green = (pixelValue &amp; 0x0000ff00) &gt;&gt; 8;
              int  blue  =  pixelValue &amp; 0x000000ff;
              int newRed = quantisePixel(red);
              int newGreen = quantisePixel(green);
              int newBlue = quantisePixel(blue);
              int newPixel = (newRed &lt;&lt; 16) | (newGreen &lt;&lt; 8) | newBlue;
  
              ditheredIMG.setRGB(x+1,y, (int) (calculateErr(pixelValue, newPixel) * (7/16.0)));
              ditheredIMG.setRGB(x-1,y+1, (int) (calculateErr(pixelValue, newPixel) * (3/16.0)));
              ditheredIMG.setRGB(x,y+1, (int) (calculateErr(pixelValue, newPixel) * (5/16.0)));
              ditheredIMG.setRGB(x+1,y+1, (int) (calculateErr(pixelValue, newPixel)* (1/16.0)));
         
          }
      }
  }
  
  public int calculateErr(int oldVal, int newVal){
      return oldVal-newVal;
  }
  
  public static int quantisePixel(int val){
      if(val &gt; 127){
          return 255;
      } else{
          return 0;
      }
  
  }
  </code></pre>
  <p>}</p>