import java.awt.image.BufferedImage;
public class pixel
{
int width;
int height;
byte[] R;
byte[] G;
byte[] B;
BufferedImage image;
pixel(BufferedImage image)
{
this.image=image;
width=image.getWidth();
height=image.getHeight();
R=new byte[width*height];
G=new byte[width*height];
B=new byte[width*height];
}
public void getRGB()
{
int c=0;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
c = image.getRGB(col,row);
R[index] = (byte)((c&0xff0000)>>16);
G[index] = (byte)((c&0xff00)>>8);
B[index] = (byte)(c&0xff);
}
}
}
public void setRGB()
{
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
image.setRGB(col,row,0xFF000000|((R[index]<<16)+(G[index]<<8)+(B[index])));
}
}
}
public void gray()
{
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
R[index]=G[index]=B[index]=(byte)(((R[index]&0x0FF)*29+(G[index]&0x0FF)*150+(B[index]&0x0FF)*77)/256);
}
}
}
public void binary()
{
double sumB=0.;
double sumS=0.;
long sumBT=0;
long sumST=0;
int t0,t1,t2,t=127;
do{
t0=t;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
if((R[index]&0x0FF)>t)
{
sumB+=(R[index]&0x0FF);
sumBT++;
}
else
{
sumS+=(R[index]&0x0FF);
sumST++;
}
}
}
t2=(int)(sumB/sumBT);
t1=(int)(sumS/sumST);
sumB=0.;
sumBT=0;
sumS=0.;
sumST=0;
t=((t1+t2)/2);
}while(t!=t0);
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
if((R[index]&0x0FF)>t)
{
R[index]=(byte)0xFF;
}
else
{
R[index]=0;
}
}
}
do{
t0=t;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
if((G[index]&0x0FF)>t)
{
sumB+=(G[index]&0x0FF);
sumBT++;
}
else
{
sumS+=(G[index]&0x0FF);
sumST++;
}
}
}
t2=(int)(sumB/sumBT);
t1=(int)(sumS/sumST);
sumB=0.;
sumBT=0;
sumS=0.;
sumST=0;
t=((t1+t2)/2);
}while(t!=t0);
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
if((G[index]&0x0FF)>t)
{
G[index]=(byte)0xFF;
}
else
{
G[index]=0;
}
}
}
do{
t0=t;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
if((B[index]&0x0FF)>t)
{
sumB+=(B[index]&0x0FF);
sumBT++;
}
else
{
sumS+=(B[index]&0x0FF);
sumST++;
}
}
}
t2=(int)(sumB/sumBT);
t1=(int)(sumS/sumST);
sumB=0.;
sumBT=0;
sumS=0.;
sumST=0;
t=((t1+t2)/2);
}while(t!=t0);
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
if((B[index]&0x0FF)>t)
{
B[index]=(byte)0xFF;
}
else
{
B[index]=0;
}
}
}
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int index = row * width + col;
if(((R[index]&0x0FF)+(G[index]&0x0FF)+(B[index]&0x0FF))>200)
{
R[index]=G[index]=B[index]=(byte)0xFF;
}
else
{
R[index]=G[index]=B[index]=0;
}
}
}
}
public void test1()
{
for (int row = 20; row < 120; row++)
{
for (int col = 20; col < 120; col++)
{
int index = row * width + col;
{
R[index]=G[index]=B[index]=(byte)0xFF;
}
}
}
}
public void median()
{
byte[] mR =new byte[9];
byte[] mG =new byte[9];
byte[] mB =new byte[9];
byte[] RT=new byte[width*height];
byte[] GT=new byte[width*height];
byte[] BT=new byte[width*height];
for (int row = 0+1; row < height-1; row++)
{
for (int col = 0+1; col < width-1; col++)
{
for(int y=0;y<3;y++)
{
for(int x=0;y<3;y++)
{
int index = (row+y-1) * width + col+x-1;
int index0=y * 3 + x;
mR[index0]=R[index];
mG[index0]=G[index];
mB[index0]=B[index];
}
}
for(int t=9;t>4;t--)//一半冒泡排序
{
for(int n=1;n<t;n++)
{
if((mR[n-1]&0x0FF)>(mR[n]&0x0FF))
{
byte a=mR[n];
mR[n]=mR[n-1];
mR[n-1]=a;
}
if((mG[n-1]&0x0FF)>(mG[n]&0x0FF))
{
byte a=mG[n];
mG[n]=mG[n-1];
mG[n-1]=a;
}
if((mB[