12.07.2015 Views

Digital Image Processing (using Java)

Digital Image Processing (using Java)

Digital Image Processing (using Java)

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Copy input image because error diffusion modifies itWritableRaster input = image.copyData(null);WritableRaster output = output<strong>Image</strong>.getRaster();final int threshold = 128;int value, error;for (int y = 1; y < h; ++y)for (int x = 1; x < w; ++x) {// Threshold value and compute errorvalue = input.getSample(x, y, 0);if (value < threshold) {output.setSample(x, y, 0, 0); //set to blackerror = value;}else {output.setSample(x, y, 0, 1); //set to whiteerror = value - 255;}// Disperse error to pixels that are ahead of usvalue = input.getSample(x+1, y, 0);input.setSample(x+1, y, 0, clamp(value + 0.4375f * error));value = input.getSample(x-1, y+1, 0);input.setSample(x-1, y+1, 0, clamp(value + 0.1875f * error));value = input.getSample(x, y+1, 0);input.setSample(x, y+1, 0, clamp(value + 0.3125f * error));value = input.getSample(x+1, y+1, 0);input.setSample(x+1, y+1, 0, clamp(value + 0.0625f * error));}}return output<strong>Image</strong>;// Rounds a float to the nearest int between 0 and 255- 16 -

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!