Created
August 13, 2014 12:59
-
-
Save kishmiryan-karlen/559c190f6c20856ee323 to your computer and use it in GitHub Desktop.
A function that helps to apply LUT to image. Make sure to change the canvas IDs or to create temporary canvases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Karlen on 13-Aug-14. | |
*/ | |
var imageContext = document.getElementById('imageCanvas').getContext('2d'), | |
lutContext = document.getElementById('lutCanvas').getContext('2d'); | |
function applyLUT(image, lut) { | |
var imageData, lutData, iData, lData; | |
imageContext.clearRect(0, 0, image.naturalWidth, image.naturalHeight); | |
imageContext.drawImage(image, 0, 0); | |
imageData = imageContext.getImageData(0, 0, image.naturalWidth, image.naturalHeight); | |
iData = imageData.data; | |
lutContext.clearRect(0, 0, lut.naturalWidth, lut.naturalHeight); | |
lutContext.drawImage(lut, 0, 0); | |
lutData = lutContext.getImageData(0, 0, lut.naturalWidth, lut.naturalHeight); | |
lData = lutData.data; | |
for (var i = 0, l = iData.length; i < l; i += 4) { | |
var r, g, b; | |
r = Math.floor(iData[i] / 4); | |
g = Math.floor(iData[i + 1] / 4); | |
b = Math.floor(iData[i + 2] / 4); | |
var lutX, lutY, lutIndex; | |
lutX = (b % 8) * 64 + r; | |
lutY = Math.floor(b / 8) * 64 + g; | |
lutIndex = (lutY * lut.naturalWidth + lutX) * 4; | |
var lutR, lutG, lutB; | |
lutR = lData[lutIndex]; | |
lutG = lData[lutIndex + 1]; | |
lutB = lData[lutIndex + 2]; | |
iData[i] = lutR; | |
iData[i + 1] = lutG; | |
iData[i + 2] = lutB; | |
} | |
imageData.data = iData; | |
imageContext.clearRect(0, 0, image.naturalWidth, image.naturalHeight); | |
imageContext.putImageData(imageData, 0, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you use r = Math.floor(iData[i] / 4); to get the color value and then the index in the LUT, you will have steps in gradients and not very clean result, because you get the lower index in a 64 array from a 256 array.
The result is better if you get the lower value, the upper value, and the interpolation between them :