一句話,濾鏡的實現就是對像素點(RGBA)進行再運算,輸出新的像素點。 F(r,g,b,a)=G(r,g,b,a);
這個公式包含四個變換,即RGB顏色空間中RGB三個分量的變換以及透明度Alhpa的變換,這里我們簡寫為A的變換。
舉個灰度變換的例子,它對應的F——G變換如下:
F(r) = b * 0.114 + g * 0.587 + r * 0.299;
F(g) = b * 0.114 + g * 0.587 + r * 0.299;
F(b) = b * 0.114 + g * 0.587 + r * 0.299;
F(a) = a;
這個灰度化也就是一個基本變換。有了這個基本變換,圖像也就達到了一定的效果,但是,一些復雜的濾鏡,並非簡單的基本變換,而是一些復雜的效果疊加,也就說
這個G函數是多個函數的聯合體。
還有一種方法,對於一些濾鏡產品,我們直觀看到的濾鏡效果是已經生成的新像素點組成,也就是說如果我們想破解這種濾鏡,
我們可以通過顏色映射構建一個映射表,然后通過查表來快速實現該變換效果,
這種萬能濾鏡破解的方法,請參考 http://blog.csdn.net/trent1985/article/details/42212459
本人看了以上博主的很多濾鏡相關的博文,得到很多啟發(感謝!)
以下是Java實現Lomo效果的代碼,可以直接用。
public static void Lomo(String fromPath, String toPath) throws IOException {
BufferedImage fromImage = ImageIO.read(new File(fromPath));
int width = fromImage.getWidth();
int height = fromImage.getHeight();
BufferedImage toImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int a, r, g, b, grayValue = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int rgb = fromImage.getRGB(i, j);
// 過濾
a = rgb & 0xff000000;
r = (rgb & 0xff0000) >> 16;
g = (rgb & 0xff00) >> 8;
b = (rgb & 0xff);
b = ModeSmoothLight(b, b);
g = ModeSmoothLight(g, g);
r = ModeSmoothLight(r, r);
b = ModeExclude(b, 80);
g = ModeExclude(g, 15);
r = ModeExclude(r, 5);
grayValue = a | (r << 16) | (g << 8) | b;
toImage.setRGB(i, j, grayValue);
}
}
ImageIO.write(toImage, "jpg", new File(toPath));
}
private static int ModeSmoothLight(int basePixel, int mixPixel) {
int res = 0;
res = mixPixel > 128
? ((int) ((float) basePixel + ((float) mixPixel + (float) mixPixel - 255.0f)
* ((Math.sqrt((float) basePixel / 255.0f)) * 255.0f - (float) basePixel) / 255.0f))
: ((int) ((float) basePixel + ((float) mixPixel + (float) mixPixel - 255.0f)
* ((float) basePixel - (float) basePixel * (float) basePixel / 255.0f) / 255.0f));
return Math.min(255, Math.max(0, res));
}
private static int ModeExclude(int basePixel, int mixPixel) {
int res = 0;
res = (mixPixel + basePixel) - mixPixel * basePixel / 128;
return Math.min(255, Math.max(0, res));
}
public static void main(String[] args) throws Exception {
Lomo("C:\\2.jpg", "C:\\test2.jpg");
}