?? grayretriever.java
字號:
import java.awt.image.*;
public class GrayRetriever extends PointProcessor
{
//source ---- map to ----
//map中的元素依次對應元素于計算公式中的參數a,b,c,d
int nMap[] = {0,255,0,255};
//灰度比例化
float scale = 1.0f;
// isGrayImage表示是否是灰色圖像。這里灰色圖像的灰度級數為256,其顏色模型仍然為RGBA,只是每個像素的RGB值完全相等而已的。與早期的同一概念相比,這里有不同的內涵。
boolean isGrayImage = true;
//可用于缺省情況和灰度取反,當應用于灰度取反時,需要結合該類的setType()方法
public GrayRetriever()
{
this.type = this.PIXEL_GRAY_SCALE;
}
//用于灰度比例化
public GrayRetriever(float scale)
{
this.type = this.PIXEL_GRAY_SCALE;
this.scale = scale;
}
//用于:線性灰度變換及灰度截斷,
public GrayRetriever(int a,int b,int mapping_a,int mapping_b)
{
this.type = this.PIXEL_GRAY_LINEARIZE ;
nMap[0] = a;
nMap[1] = b;
if(a > b)
{
nMap[0] = b;
nMap[1] = a;
}
nMap[2] = mapping_a;
nMap[3] = mapping_b;
}
//只設置類型不設置參數值
public void setType(int type)
{
this.type = type;
}
public void setType1f(int type,float scale)
{
this.type = this.PIXEL_GRAY_SCALE ;
this.scale = scale;
}
public void setType4i(int type,int a,int b,int mapping_a,int mapping_b)
{
this.type = type;
nMap[0] = a;
nMap[1] = b;
if(a > b)
{
nMap[0] = b;
nMap[1] = a;
}
nMap[2] = mapping_a;
nMap[3] = mapping_b;
}
//setImageProperty()設置圖像是灰色圖像還是彩色圖像,當為彩色圖像時,設為false。
public void setImageProperty(boolean isGrayImage)
{
this.isGrayImage = isGrayImage;
}
//灰度修正
public boolean retrieveRGB(int[] scr,int scansize,int[] dst)
{
if(scansize == 0)return false;
int height = scr.length / scansize;
return (this.retrieveRGB(scr,0,0,scansize,height,scansize,dst));
}
public boolean retrieveRGB(int[] scr,int x,int y,int w,int h,int scansize,int[] dst)
{
if(scansize <= 0) return false;
int nTotal = scr.length;
//原圖像的寬度
int scrWidth = scansize;
int scrHeight = nTotal / scansize;
if((x > (scansize - 1)) || (y > (scrHeight - 1)))return false;
int width = w;
int height = h;
if((x + w) > scansize) width = scansize - x;
if((y + h) > scrHeight) height = scrHeight - y;
ColorModel colorModel = ColorModel.getRGBdefault();
System.arraycopy(scr,0,dst,0, nTotal);
int b_a = nMap[1] - nMap[0];
int d_c = nMap[3] - nMap[2];
int i = 0;int j = 0;
for(i = 0; i < height;i++)
{
int indexBase = (y + i) * scansize;
for(j = 0;j < width;j++)
{
int index = indexBase + x + j;
int r,g,b,gray;
if(this.isGrayImage)
gray = colorModel.getRed(scr[i]);
else
{
r = colorModel.getRed(scr[index]);
g = colorModel.getGreen(scr[index]);
b = colorModel.getBlue(scr[index]);
//產生 1lm (流明) 的白光所需要的三基色的近似值可用下面的灰度方程來表示:
// 1lm(白光) = 0.30lm(紅) + 0.59lm(綠) + 0.11lm(藍)
gray = (r * 30 + g * 59 + b * 11) / 100;
}
switch(this.type)
{
//灰度比例化
case this.PIXEL_GRAY_SCALE :
r = g = b = (int)(gray * this.scale);
break;
case this.PIXEL_GRAY_LINEARIZE:
if(b_a == 0) r = g = b = nMap[2];
else
r = g = b = (d_c * (gray - nMap[0])) / b_a + nMap[2];
break;
//灰度截斷
case this.PIXEL_GRAY_ROUND_OFF :
if(gray <= nMap[0])
r = g = b = nMap[2];
else if(gray > nMap[1])
r = g = b = nMap[3];
else
r = g = b = (d_c * (gray - nMap[0])) / b_a + nMap[2];
break;
//灰度取反
case this.PIXEL_GRAY_REVERSE:
r = g = b = 255 - gray;
break;
default:
r = g = b = gray;
break;
}
r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
dst[index] = 0xFF000000 | (( r << 16 ) | ( g << 8 ) | b );
}
}
return true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -