xxxxxxxxxx
236
function CopyImage(source, target, xScale, yScale, exponent)
{
source.loadPixels();
target.loadPixels();
for (let sy = 0; sy < source.height; ++sy)
{
for (let sx = 0; sx < source.width; ++sx)
{
let i = (sy * source.width + sx) * 4;
let dx = sx * xScale;
let dy = sy * yScale;
ExplodePixel(source, target, sx, sy, dx, dy, xScale, yScale, exponent);
}
}
target.updatePixels();
}
function ExplodePixel(sourceImg, destImg, sx, sy, dx, dy, xScale, yScale, exponent)
{
let c = GetPixel(sourceImg, sx + 0, sy + 0, transparent);
let cT = color(red(c), green(c), blue(c), 0);
let l = GetPixel(sourceImg, sx - 1, sy + 0, cT);
let t = GetPixel(sourceImg, sx + 0, sy - 1, cT);
let r = GetPixel(sourceImg, sx + 1, sy + 0, cT);
let b = GetPixel(sourceImg, sx + 0, sy + 1, cT);
if (alpha(c) == 0)
{
let arrayOfColors = [];
if (alpha(l) != 0)
arrayOfColors.push(l);
if (alpha(t) != 0)
arrayOfColors.push(t);
if (alpha(r) != 0)
arrayOfColors.push(r);
if (alpha(b) != 0)
arrayOfColors.push(b);
c = AverageColorWeightedByAlpha(arrayOfColors);
cT = color(red(c), green(c), blue(c), 0);
}
let tlT = lerpColor(t, l, 0.5);
let blT = lerpColor(b, l, 0.5);
let trT = lerpColor(t, r, 0.5);
let brT = lerpColor(b, r, 0.5);
let tl = GetPixel(sourceImg, sx - 1, sy - 1, tlT);
let bl = GetPixel(sourceImg, sx - 1, sy + 1, blT);
let tr = GetPixel(sourceImg, sx + 1, sy - 1, trT);
let br = GetPixel(sourceImg, sx + 1, sy + 1, brT);
let leftWidth = int(xScale / 2);
let rightWidth = xScale - leftWidth;
let topHeight = int(yScale / 2);
let bottomHeight = yScale - topHeight;
// top left corner
for (let y = 0; y < topHeight; ++y)
{
let yInterpolant = (y + bottomHeight) / yScale;
if (yScale % 2 == 0)
yInterpolant += 0.5 / yScale;
yInterpolant = PowInOut(yInterpolant, exponent);
let lyColor = lerpColor(tl, l, yInterpolant);
let cyColor = lerpColor(t, c, yInterpolant);
for (let x = 0; x < leftWidth; ++x)
{
let xInterpolant = (x + rightWidth) / xScale;
if (xScale % 2 == 0)
xInterpolant += 0.5 / xScale;
xInterpolant = PowInOut(xInterpolant, exponent);
let pixelColor = lerpColor(lyColor, cyColor, xInterpolant);
SetPixel(destImg, dx + x, dy + y, pixelColor);
}
}
// top right corner
for (let y = 0; y < topHeight; ++y)
{
let yInterpolant = (y + bottomHeight) / yScale;
if (yScale % 2 == 0)
yInterpolant += 0.5 / yScale;
yInterpolant = PowInOut(yInterpolant, exponent);
let cyColor = lerpColor(t, c, yInterpolant);
let ryColor = lerpColor(tr, r, yInterpolant);
for (let x = 0; x < rightWidth; ++x)
{
let xInterpolant = x / xScale;
if (xScale % 2 == 0)
xInterpolant += 0.5 / xScale;
xInterpolant = PowInOut(xInterpolant, exponent);
let pixelColor = lerpColor(cyColor, ryColor, xInterpolant);
SetPixel(destImg, dx + x + leftWidth, dy + y, pixelColor);
}
}
// bottom left corner
for (let y = 0; y < bottomHeight; ++y)
{
let yInterpolant = y / yScale;
if (yScale % 2 == 0)
yInterpolant += 0.5 / yScale;
yInterpolant = PowInOut(yInterpolant, exponent);
let cyColor = lerpColor(c, b, yInterpolant);
let ryColor = lerpColor(r, br, yInterpolant);
for (let x = 0; x < rightWidth; ++x)
{
let xInterpolant = x / xScale;
if (xScale % 2 == 0)
xInterpolant += 0.5 / xScale;
xInterpolant = PowInOut(xInterpolant, exponent);
let pixelColor = lerpColor(cyColor, ryColor, xInterpolant);
SetPixel(destImg, dx + x + leftWidth, dy + y + topHeight, pixelColor);
}
}
// bottom right corner
for (let y = 0; y < bottomHeight; ++y)
{
let yInterpolant = y / yScale;
if (yScale % 2 == 0)
yInterpolant += 0.5 / yScale;
yInterpolant = PowInOut(yInterpolant, exponent);
let lyColor = lerpColor(l, bl, yInterpolant);
let cyColor = lerpColor(c, b, yInterpolant);
for (let x = 0; x < leftWidth; ++x)
{
let xInterpolant = (x + rightWidth) / xScale;
if (xScale % 2 == 0)
xInterpolant += 0.5 / xScale;
xInterpolant = PowInOut(xInterpolant, exponent);
let pixelColor = lerpColor(lyColor, cyColor, xInterpolant);
SetPixel(destImg, dx + x, dy + y + topHeight, pixelColor);
}
}
}
function GetPixel(img, x, y, defaultColor)
{
if (0 > x || x >= img.width || 0 > y || y >= img.height)
return defaultColor;
let i = (y * img.width + x) * 4;
let pixels = img.pixels;
let r = pixels[i + 0];
let g = pixels[i + 1];
let b = pixels[i + 2];
let a = pixels[i + 3];
return color(r, g, b, a);
}
function SetPixel(img, x, y, c)
{
if (0 > x || x >= img.width || 0 > y || y >= img.height)
return;
let i = (y * img.width + x) * 4;
let pixels = img.pixels;
pixels[i + 0] = red(c);
pixels[i + 1] = green(c);
pixels[i + 2] = blue(c);
pixels[i + 3] = alpha(c);
}
function AverageColorWeightedByAlpha(arrayOfColors)
{
let output;
let weightedR = 0;
let weightedG = 0;
let weightedB = 0;
let weightedA = 0;
let alphaSum;
for (const c of arrayOfColors)
{
let a = alpha(c);
alphaSum += a;
weightedR += red(c) * a;
weightedG += green(c) * a;
weightedB += blue(c) * a;
weightedA += a * a;
}
let r = weightedR / alphaSum;
let g = weightedG / alphaSum;
let b = weightedB / alphaSum;
let a = weightedA / alphaSum;
push();
{
colorMode(RGB);
output = color(r, g, b, a);
}
pop();
return output;
}