import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class VKImage ... {
private BufferedImage bufferdImage = null;
private int height = 0;
private int width = 0;
public int[][] rgb;
public int[][] crgb;
private int[][] flag;
public static void saveBMPImage(BufferedImage bi,File f)throws IOException ...{
String format = "BMP";
javax.imageio.ImageIO.write(bi,format,f);
}
public static void saveBMPImage(int[][] irgb,File f)throws IOException ...{
BufferedImage bi = new BufferedImage(irgb.length,irgb[0].length,BufferedImage.TYPE_INT_RGB);
for(int i=0;i<irgb.length;i++)...{
for(int j=0;j<irgb[0].length;j++)...{
bi.setRGB(i, j, irgb[i][j]);
}
}
String format = "BMP";
javax.imageio.ImageIO.write(bi,format,f);
}
public VKImage(URL location) throws IOException...{
this.bufferdImage = javax.imageio.ImageIO.read(location);
init();
}
public VKImage(String filename)throws IOException...{
this.bufferdImage = javax.imageio.ImageIO.read(new File(filename));
init();
}
public VKImage(File file)throws IOException...{
this.bufferdImage = javax.imageio.ImageIO.read(file);
init();
}
private void init()...{
this.height = bufferdImage.getHeight();
this.width = bufferdImage.getWidth();
rgb = new int[width][height];
crgb = new int[width][height];
flag = new int[width][height];
for(int i=0;i<width;i++)...{
for(int j=0;j<height;j++)...{
rgb[i][j] = bufferdImage.getRGB(i,j);
crgb[i][j] = rgb[i][j];
}
}
}
//
public void reset()...{
for(int i=0;i<width;i++)...{
for(int j=0;j<height;j++)...{
crgb[i][j] = rgb[i][j];
}
}
}
public int getHeight()...{
return height;
}
public int getWidth()...{
return width;
}
int tt;
private void intensifyPoint(int x,int y,int by,int px,int py)...{
tt++;
if(tt>2000)...{
tt--;
return;
}
flag[x][y] = 1;
if(x-1>=0&&y-1>=0&&flag[x-1][y-1]==0&&(Math.abs(crgb[x-1][y-1]-crgb[x][y])<=by))...{
intensifyPoint(x-1,y-1,by,px,py);
}
if(x-1>=0&&flag[x-1][y]==0&&(Math.abs(crgb[x-1][y]-crgb[x][y])<=by))...{
intensifyPoint(x-1,y,by,px,py);
}
if(x-1>=0&&y+1<height&&flag[x-1][y+1]==0&&(Math.abs(crgb[x-1][y+1]-crgb[x][y])<=by))...{
intensifyPoint(x-1,y+1,by,px,py);
}
if(y-1>=0&&flag[x][y-1]==0&&(Math.abs(crgb[x][y-1]-crgb[x][y])<=by))...{
intensifyPoint(x,y-1,by,px,py);
}
if(y+1<height&&flag[x][y+1]==0&&(Math.abs(crgb[x][y+1]-crgb[x][y])<=by))...{
intensifyPoint(x,y+1,by,px,py);
}
if(x+1<width&&y-1>=0&&flag[x+1][y-1]==0&&(Math.abs(crgb[x+1][y-1]-crgb[x][y])<=by))...{
intensifyPoint(x+1,y-1,by,px,py);
}
if(x+1<width&&flag[x+1][y]==0&&(Math.abs(crgb[x+1][y]-crgb[x][y])<=by))...{
intensifyPoint(x+1,y,by,px,py);
}
if(x+1<width&&y+1<height&&flag[x+1][y+1]==0&&(Math.abs(crgb[x+1][y+1]-crgb[x][y])<=by))...{
intensifyPoint(x+1,y+1,by,px,py);
}
crgb[x][y] = crgb[px][py];
tt--;
}
private int intensifyOnce(int by)...{
int x = -1;
int y = -1;
for(int i=0;i<width;i++)...{
for(int j=0;j<height;j++)...{
if(flag[i][j] == 0)...{
x = i;
y = j;
break;
}
}
}
if(x==-1)...{
return 0;
}
intensifyPoint(x,y,by,x,y);
return 1;
}
public void intensify(int by)...{
for(int i=0;i<width;i++)...{
for(int j=0;j<height;j++)...{
flag[i][j] = 0;
}
}
while(true)...{
int ret = intensifyOnce(by);
if(ret==0)...{
break;
}
}
}
}