java - JH Labs Quantize Usage to reduce image color depth -
i trying use quantizefilter in
http://www.jhlabs.com/ip/filters/index.html
to reduce color depth of screen shot.
here 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, left is:
original img:
quantized img:
a further analysis of problem shows inpixels array filled incorrectly; filled 3 times upper one-third of original image.
any pointers how can fix that?
additionally, links + fast quantization algorithm in java? search algorithm take type_int_bgr image , produce new type_int_bgr image less actual difference in pixels, deflated.
for example, if have 2 pixels in original image, values 255, 255, 234 , 1 value 255, 255, 236, should both converted 255,255,240. cheers
the following example convert image correctly:
quantizefilter q = new quantizefilter(); int [] inpixels = new int[image.getwidth()*image.getheight()*3]; int [] outpixels = new int[image.getwidth()*image.getheight()*3]; image.getraster().getpixels( 0, 0, image.getwidth(), image.getheight(), inpixels ); q.quantize(inpixels, outpixels, image.getwidth(), image.getheight(), 64, false, false); writableraster raster = (writableraster) image.getdata(); raster.setpixels(0,0,width,height,outpixels);
also there no reason create files implicitly, imageio.write creates them automatically.
Comments
Post a Comment