How to zoom out an image by Bilinear Interpolation in java? -
i trying read image, zoom in 80*60 , zoom out resulted image 5 times bilinear interpolation method. error : exception in thread "main" java.lang.arrayindexoutofboundsexception: 4800 . can me please?
this have done :
import java.awt.color; import java.awt.graphics2d; import java.awt.image; import java.awt.renderinghints; import java.awt.image.bufferedimage; import java.awt.image.databufferbyte; import java.awt.image.writableraster; import java.io.bytearrayinputstream; import java.io.file; import java.io.ioexception; import java.io.inputstream; import javax.imageio.imageio; public class biinterpolationtest { public static int zh; public static int zw; public static void main(string[] args) throws ioexception { // todo auto-generated method stub int[][] savedimage; file f = new file ("f:\\java\\gray scale images\\3.jpg"); savedimage = readimage(f); bufferedimage grayimage = new bufferedimage(savedimage.length, savedimage[0].length, bufferedimage.type_byte_gray); (int =0 ; i<savedimage.length ; ++){ (int j=0 ; j<savedimage[0].length ; j++){ int rgb = savedimage[i][j]; rgb = (rgb<<16)|(rgb<<8)|(rgb); grayimage.setrgb(i, j, rgb); bufferedimage zoomin =scaledimage(grayimage, 80,60); zh = zoomin.getheight(); zw = zoomin.getwidth(); byte[] temparr; temparr = extractbytes(zoomin); // todo auto-generated catch block byte [] zoomout; zoomout = bilinerainterpolation(temparr , 5); inputstream in = new bytearrayinputstream(zoomout); bufferedimage bimageinterpolated; try { bimageinterpolated = imageio.read(in); imageio.write(bimageinterpolated, "jpg", new file( "f:/new-darksouls.jpg")); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } } public static bufferedimage scaledimage(image img, int w , int h){ bufferedimage resizedimage = new bufferedimage(w , h , bufferedimage.type_byte_gray); graphics2d g2 = resizedimage.creategraphics(); g2.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); g2.drawimage(img, 0, 0, w, h, null); g2.dispose(); return resizedimage; } ///////////////////////////////////// public static byte[] extractbytes (bufferedimage image) throws ioexception { bufferedimage bufferedimage = new bufferedimage(image.getheight(), image.getwidth(), bufferedimage.type_byte_gray); // databufferbytes raster writableraster raster = bufferedimage .getraster(); databufferbyte data = (databufferbyte) raster.getdatabuffer(); return ( data.getdata() ); } public static int[][] readimage(file filename){ bufferedimage img; try { img = imageio.read(filename); // gray_scaled image output int width = img.getwidth(); int height = img.getheight(); imagepro.fw=width; imagepro.fh = height; int [][] readimageval = new int [width][height]; (int = 0; i<height ; i++){ (int j =0 ; j<width ; j++){ color c = new color(img.getrgb(j, i)); int r= (int)(c.getred() * 0.299)&0xff; int g = (int)(c.getgreen() * 0.587)&0xff; int b = (int)(c.getblue() *0.114)&0xff; int avg = ((r+b+g)); readimageval[j][i] = avg; } } return readimageval; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } public static byte[] bilinerainterpolation(byte[] imgdata, int ratio) { int wf = zw*ratio; int hf = zh*ratio; byte[] temparr = new byte[wf*hf] ; int a, b, c, d, x, y, index, g ; float x_ratio = ((float)(zw))/wf ; float y_ratio = ((float)(zh))/hf ; float x_diff, y_diff ; int in = 0 ; (int i=0;i<hf;i++) { (int j=0;j<wf;j++) { x = (int)(x_ratio * j) ; y = (int)(y_ratio * i) ; x_diff = (x_ratio * j) - x ; y_diff = (y_ratio * i) - y ; index = y*zw+x ; // range 0 255 bitwise , 0xff = imgdata[index] & 0xff ; b = imgdata[index+1] & 0xff ; c = imgdata[index+zw] & 0xff ; d = imgdata[index+zw+1] & 0xff ; g = (int)( a*(1-x_diff)*(1-y_diff) + b*(x_diff)*(1-y_diff) + c*(y_diff)*(1-x_diff) + d*(x_diff*y_diff) ) ; temparr[in++] = (byte) g ; } } return temparr ; } }
Comments
Post a Comment