Skip to content

Instantly share code, notes, and snippets.

@perilstar
Created December 20, 2017 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perilstar/2161935aab35fd60e577ac7316fbbcc2 to your computer and use it in GitHub Desktop.
Save perilstar/2161935aab35fd60e577ac7316fbbcc2 to your computer and use it in GitHub Desktop.

the written out part

use case

You're a digital artist trying to make glitch artwork with pixel sorting. You're using Processing for this, because that's how everyone does it. However, you want to streamline more edits into your process, so you use this code to modify the hue, saturation, brightness, etc. of the image as you're sorting it.

unique

I didn't see anyone else implement a gaussian blur function did you

how it uses the different things you asked for

  1. Variables
    I mean, there are variables everywhere.
  2. Conditionals
    I used a switch statement to allow for multiple blur strengths.
  3. Functions
    Each operation is a function.
  4. Lists
    The kernels are lists, and so are the pixels.
  5. Iteration
    Iteration is used to modify all the pixels in the image, and is used heavily in the blur function to get values from the gaussian kernel.
  6. Abstraction
    All you need to know to use this code is what each function does. The algorithm that the applyContrast() function implements is hidden when it's used to modify an image, and since it always works it's out of the programmer's concern.
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment