Skip to content

Instantly share code, notes, and snippets.

@reportbase
Created June 15, 2015 19:28
Show Gist options
  • Save reportbase/edc7959244d18128c3c9 to your computer and use it in GitHub Desktop.
Save reportbase/edc7959244d18128c3c9 to your computer and use it in GitHub Desktop.
Linear interpolate of image buffer
function data_a_interpolate(data, alpha, width, height, red, green, blue)
{
var m = 0;
for(var n = 0; n < width * height * 4; n += 4, m++)
{
data[n + 0] = Math.linear_interpolate(alpha, red, data[n + 0]);
data[n + 1] = Math.linear_interpolate(alpha, green, data[n + 1]);
data[n + 2] = Math.linear_interpolate(alpha, blue, data[n + 2]);
data[n + 3] = 0xff;
}
}
function data_interpolate(data, arr, width, height, red, green, blue)
{
var m = 0;
for(var n = 0; n < width * height * 4; n += 4, m++)
{
data[n + 0] = Math.linear_interpolate(arr[m + 0], red, 0x00);
data[n + 1] = Math.linear_interpolate(arr[m + 1], green, 0x00);
data[n + 2] = Math.linear_interpolate(arr[m + 2], blue, 0x00);
data[n + 3] = 0xff;
}
}
//if alpha is 255, then all weight is val1, if alpha is 0, then all weight is val2
Math.linear_interpolate = function(alpha, val1, val2)
{
return ((val1 + 1) * alpha >> 8) - ((val2 + 1) * alpha >> 8) + val2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment