|
import java.util.*; |
|
|
|
PImage img; |
|
float i = 3; |
|
|
|
float[][] GAUSS_KERNEL_LOW = |
|
{ |
|
{0, 0, 0, 0, 0}, |
|
{0, 0.002284, 0.043222, 0.002284, 0}, |
|
{0, 0.043222, 0.817974, 0.043222, 0}, |
|
{0, 0.002284, 0.043222, 0.002284, 0}, |
|
{0, 0, 0, 0, 0} |
|
}; |
|
|
|
float[][] GAUSS_KERNEL_MED = |
|
{ |
|
{0.003765, 0.015019, 0.023792, 0.015019, 0.003765}, |
|
{0.015019, 0.059912, 0.094907, 0.059912, 0.015019}, |
|
{0.023792, 0.094907, 0.150342, 0.094907, 0.023792}, |
|
{0.015019, 0.059912, 0.094907, 0.059912, 0.015019}, |
|
{0.003765, 0.015019, 0.023792, 0.015019, 0.003765} |
|
}; |
|
|
|
float[][] GAUSS_KERNEL_HIGH = |
|
{ |
|
{0.018917, 0.031324, 0.037057, 0.031324, 0.018917}, |
|
{0.031324, 0.051868, 0.061361, 0.051868, 0.031324}, |
|
{0.037057, 0.061361, 0.072593, 0.061361, 0.037057}, |
|
{0.031324, 0.051868, 0.061361, 0.051868, 0.031324}, |
|
{0.018917, 0.031324, 0.037057, 0.031324, 0.018917} |
|
}; |
|
|
|
float[][] GAUSS_KERNEL_REALLY_HIGH = |
|
{ |
|
{0.039206,0.039798,0.039997,0.039798,0.039206}, |
|
{0.039798,0.040399,0.040601,0.040399,0.039798}, |
|
{0.039997,0.040601,0.040804,0.040601,0.039997}, |
|
{0.039798,0.040399,0.040601,0.040399,0.039798}, |
|
{0.039206,0.039798,0.039997,0.039798,0.039206} |
|
}; |
|
|
|
enum BlurStrength { |
|
LOW, |
|
MED, |
|
HIGH, |
|
REALLY_HIGH |
|
} |
|
|
|
void blur(BlurStrength s) { |
|
float[][] kernel = {{3.0}}; |
|
switch (s) { |
|
case LOW: |
|
kernel = GAUSS_KERNEL_LOW; |
|
break; |
|
case MED: |
|
kernel = GAUSS_KERNEL_MED; |
|
break; |
|
case HIGH: |
|
kernel = GAUSS_KERNEL_HIGH; |
|
break; |
|
case REALLY_HIGH: |
|
kernel = GAUSS_KERNEL_REALLY_HIGH; |
|
break; |
|
} |
|
|
|
int w = img.width; |
|
int h = img.height; |
|
for (int i = 0; i < w; i++) { |
|
for (int j = 0; j < h; j++) { |
|
float newH = 0; |
|
float newS = 0; |
|
float newB = 0; |
|
for (int k = 0; k < 5; k++) { |
|
for (int l = 0; l < 5; l++) { |
|
int offsetX = Math.min(Math.max((i + k - 2), 0), w-1); |
|
int offsetY = Math.min(Math.max((j + l - 2), 0), h-1); |
|
|
|
newH += kernel[k][l] * hue(img.pixels[w*(offsetY) + offsetX]); |
|
newS += kernel[k][l] * saturation(img.pixels[w*(offsetY) + offsetX]); |
|
newB += kernel[k][l] * brightness(img.pixels[w*(offsetY) + offsetX]); |
|
} |
|
} |
|
img.pixels[(w*j)+i] = color(newH, newS, newB); |
|
} |
|
} |
|
} |
|
void hueShift(float offset) { |
|
for (int i = 0; i < img.pixels.length; i++) { |
|
color c = img.pixels[i]; |
|
float h = hue(c); |
|
h += offset; |
|
h %= 255; |
|
img.pixels[i] = color(h, saturation(c), brightness(c)); |
|
} |
|
} |
|
|
|
void applySaturation(float scale) { |
|
for (int i = 0; i < img.pixels.length; i++) { |
|
color c = img.pixels[i]; |
|
float s = saturation(c); |
|
float newS = Math.max(Math.min(scale * s, 255), 0); |
|
c = color(hue(c), newS, brightness(c)); |
|
img.pixels[i] = c; |
|
} |
|
} |
|
|
|
void applyContrast(float cf) { |
|
for (int i = 0; i < img.pixels.length; i++) { |
|
color c = img.pixels[i]; |
|
float l = brightness(c); |
|
l /= 255; |
|
l -= 0.5; |
|
l *= cf; |
|
l += 0.5; |
|
l *= 255; |
|
l = Math.max(0, Math.min(255, l)); |
|
c = color(hue(c), saturation(c), l); |
|
img.pixels[i] = c; |
|
} |
|
} |
|
|
|
void applyTint(color newC, float a) { |
|
float alpha = a / 255f; |
|
for (int i = 0; i < img.pixels.length; i++) { |
|
color c = img.pixels[i]; |
|
float newH = clamp(lerp(hue(c), hue(newC), alpha), 0, 255); |
|
float newS = clamp(lerp(saturation(c), saturation(newC), alpha), 0, 255); |
|
float newB = clamp(lerp(brightness(c), brightness(newC), alpha), 0, 255); |
|
img.pixels[i] = color(newH, newS, newB); |
|
} |
|
} |
|
|
|
void applyHue(float h) { |
|
for (int i = 0; i < img.pixels.length; i++) { |
|
color c = img.pixels[i]; |
|
img.pixels[i] = color(h, saturation(c), brightness(c)); |
|
} |
|
} |
|
|
|
float clamp(float v, float a, float b) { |
|
return Math.max(a, Math.min(b, v)); |
|
} |
|
|
|
void setup() { |
|
colorMode(HSB, 255); |
|
} |
|
|
|
void draw() { |
|
image(img, 0, 0); |
|
} |