I am trying to use the QuantizeFilter in
http://www.jhlabs.com/ip/filters/index.html
to reduce the color depth of a screen shot.
Here is my very very simple code:
Robot robo = new Robot();
BufferedImage notQuantized = robo.createScreenCapture( new Rectangle ( 0, 0, 300, 300 ) );
BufferedImage Quantized = new BufferedImage( 300, 300, BufferedImage.TYPE_INT_BGR);
File nonquantized = new File ("C:\\nonquantized.png");
File quantized = new File("C:\\quantized.png");
nonquantized.createNewFile();
quantized.createNewFile();
QuantizeFilter bla = new QuantizeFilter();
int [] outPixels = new int[300*300*3];
int [] inPixels = new int[300*300*3];
notQuantized.getRaster().getPixels( 0, 0, 300, 300, inPixels );
bla.quantize( inPixels, outPixels, 300, 300,2, true, true );
Quantized.getRaster().setPixels( 0, 0, 300, 300, outPixels );
ImageIO.write( Quantized, "png", quantized );
ImageIO.write( notQuantized, "png", nonquantized );
However, what I am left with is:
Original img:
Quantized img:
A further analysis of the problem shows that the inPixels array is filled incorrectly; it is filled three times with the upper one-third of the original image.
Any pointers how I can fix that?
Additionally, any links good + fast quantization algorithm in Java? What I search for is an algorithm that will take a TYPE_INT_BGR image and produce a new TYPE_INT_BGR image but with less actual difference in pixels, so it could be easily deflated.
For example, if we have two pixels in the original image, with values like 255, 255, 234 and another one with value like 255, 255, 236, they should both be converted to 255,255,240. Cheers
The following example will convert your image correctly:
Also there is no reason to create the files implicitly, as ImageIO.write creates them automatically.