Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created November 11, 2017 09:50
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 KrabCode/aaa9061da05aefea7d2363e79ccef552 to your computer and use it in GitHub Desktop.
Save KrabCode/aaa9061da05aefea7d2363e79ccef552 to your computer and use it in GitHub Desktop.
Pixel sorts an image, animates the sort from top to bottom, records a fancy pantsy .mp4 and it can even take a picture!
import com.hamoid.VideoExport;
import processing.core.PApplet;
import processing.core.PImage;
import java.util.Arrays;
public class MainApp extends PApplet{
public static void main(String[] args)
{
PApplet.main("MainApp", args);
}
PImage backup, img;
VideoExport vx;
boolean rec = false;
boolean in = true;
int start = 0;
int end = 0;
int scaledown = 1;
boolean saved = false;
public void settings()
{
backup = loadImage("C:\\PerlinDistort\\5.jpg");
backup.resize(backup.width/scaledown, backup.height/scaledown);
size(backup.width, backup.height); //true scaled size
// fullScreen(); //stretch to fullscreen
}
public void setup(){
vx = new VideoExport(this);
vx.setFrameRate(60);
vx.startMovie();
rec = true;
start = 0;
end = 0;
}
int savedImageIndex = 0;
public void draw()
{
//make a fresh working copy
try {
img = (PImage) backup.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
img = distort(img);
image(img, 0, 0, width, height);
//record video
if(rec){
vx.saveFrame();
}else{
if(vx != null)
{
vx.endMovie();
}
}
//save one image if you must
if(key=='k' && !saved){
saved = true;
img.save("saved.jpg");
}
println(start + ":" + end);
}
private PImage distort(PImage img) {
img = colSort(img,
start, end);
if(in){
end++;
}else{
//start++;
}
if(end > img.height){
in = false;
rec = false;
}
if(start > img.width && end > img.width){
start = 0;
end = 0;
in = true;
}
if(key == 'f'){
rec = false;
}
return img;
}
private PImage rowSort(PImage img, int start, int end) {
for(int y = 0; y < img.height; y++){
sortLine(img, start, end, y);
}
return img;
}
private PImage colSort(PImage img, int start, int end) {
for(int x = 0; x < img.width; x++){
sortColumn(img, start, end, x);
}
return img;
}
private void sortLine(PImage img, int start, int end, int y) {
if(end > start){
int band = end - start;
int[] pixs = new int[band];
for(int x = 0; x < band; x++){
pixs[x] = img.get(start + x, y);
}
Arrays.sort(pixs);
for(int x = 0; x < band; x++){
img.set(start + x, y, pixs[x]);
}
}
}
private void sortColumn(PImage img, int start, int end, int x) {
if(end > start){
int band = end - start;
int[] pixs = new int[band];
for(int y = 0; y < band; y++){
pixs[y] = img.get(x, start + y);
}
Arrays.sort(pixs);
for(int y = 0; y < band; y++){
img.set(x, start + y, pixs[y]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment