我也是这个话题的新手,但我希望它对我目前所发现的有所帮助。您的图像(LUT)是 3D 数组的表示,想象一个 64x64x64 的立方体。此表示是 2D 的,一个平面是 64x64 正方形。你需要这个平面的 64 块,这就是 png 中有 8x8 个正方形的原因。
如果你仔细看,左上角的正方形在 X 轴上有红色(RGB 的 R),在 Y 轴上有绿色(G)。蓝色 (B) 是 Z 轴(我想它是向上的),它不能用 2D 表示,所以它出现在接下来的 64x64 方格上。在最后一个(右下)方格中,您可以看到它大部分是蓝色的,其中红色和绿色坐标为 0。
所以下一个问题是如何使用这个 LUT 投影图像。我已经在 Java 中尝试过,在我看来,您将不得不丢失一些信息,因为 64x64x64 远小于 256x256x256,在这种情况下,您必须将每个像素颜色值除以 4。
步骤:
- 遍历原始图像的像素。在一次迭代中,您将拥有原始图像的一个像素。获取该像素的 R、G、B 值。
- 将这些值除以 4,得到小于 64 的值。
- 从 LUT 中获取相应的像素,就像它是 64x64x64 立方体一样。所以如果RGB=(255,0,255)在原始像素上,除法得到(63,0,63),然后检查B值,得到png上对应的正方形,就是右下角(在 b=63 的情况下),并获取它的 r,g=(63,0) 像素,这在您的 png 图像上呈紫色。
我现在用java代码检查了android,我认为它足够快。下面是我写的代码,我希望它足以将它移植到javascript。 (希望cmets足够理解)
public Bitmap applyEffectToBitmap(Bitmap src, Bitmap lutbitmap) {
//android specific code, storing the LUT image's pixels in an int array
int lutWidth = lutBitmap.getWidth();
int lutColors[] = new int[lutWidth * lutBitmap.getHeight()];
lutBitmap.getPixels(lutColors, 0, lutWidth, 0, 0, lutWidth, lutBitmap.getHeight());
//android specific code, storing the source image's pixels in an int array
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
int[] pix = new int[srcWidth * srcHeight];
src.getPixels(pix, 0, srcWidth, 0, 0, srcWidth, srcHeight);
int R, G, B;
//now we can iterate through the pixels of the source image
for (int y = 0; y < srcHeight; y++){
for (int x = 0; x < srcWidth; x++) {
//index: because the pix[] is one dimensional
int index = y * srcWidth+ x;
//pix[index] returns a color, we need it's r g b values, thats why the shift operator is used
int r = ((pix[index] >> 16) & 0xff) / 4;
int g = ((pix[index] >> 8) & 0xff) / 4;
int b = (pix[index] & 0xff) / 4;
//the magic happens here: the 3rd step above: blue pixel describes the
//column and row of which square you'll need the pixel from
//then simply add the r and green value to get the coordinates
int lutX = (b % 8) * 64 + r;
int lutY = (b / 8) * 64 + g;
int lutIndex = lutY * lutWidth + lutX;
//same pixel getting as above, but now from the lutColors int array
R = ((lutColors[lutIndex] >> 16) & 0xff);
G = ((lutColors[lutIndex] >> 8) & 0xff);
B = ((lutColors[lutIndex]) & 0xff);
//overwrite pix array with the filtered values, alpha is 256 in my case, but you can use the original alpha
pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
}
}
//at the end of operations pix[] has the transformed pixels sou can set them to your new bitmap
//some android specific code is here for creating bitmaps
Bitmap result = Bitmap.createBitmap(srcWidth, srcHeight, src.getConfig());
result.setPixels(pix, 0, srcWidth, 0, 0, srcWidth, srcHeight);
return result ;
}
现在我也成功地用 javascript 实现了,检查是否使用了 Math.floor() 函数:
for (var i=0;i<imgData.data.length;i+=4){
var r=Math.floor(imgData.data[i]/4);
var g=Math.floor(imgData.data[i+1]/4);
var b=Math.floor(imgData.data[i+2]/4);
var a=255;
var lutX = (b % 8) * 64 + r;
var lutY = Math.floor(b / 8) * 64 + g;
var lutIndex = (lutY * lutWidth + lutX)*4;
var Rr = filterData.data[lutIndex];
var Gg = filterData.data[lutIndex+1];
var Bb = filterData.data[lutIndex+2];
imgData.data[i] = Rr;
imgData.data[i+1] = Gg;
imgData.data[i+2] = Bb;
imgData.data[i+3] = 255;
}
在这里查看:http://jsfiddle.net/gxu080ve/1/ lut 图像是字节码,抱歉。
此代码仅适用于 64x64x64 3DLUT 图像。如果您的 LUT 有其他尺寸,参数会有所不同; / 4、* 64、% 8、/ 8 必须更改为其他尺寸,但在这个问题的范围内,LUT 是 64x64x64。