Skip to content

Instantly share code, notes, and snippets.

@jemsgit
Last active July 25, 2022 10:54
Show Gist options
  • Save jemsgit/5a0451ea06264cfb8fe3fa82780672e3 to your computer and use it in GitHub Desktop.
Save jemsgit/5a0451ea06264cfb8fe3fa82780672e3 to your computer and use it in GitHub Desktop.
import controlP5.*;
import processing.svg.*;
ControlP5 gui;
PImage p1;
PImage p2;
int imageScaleUp = 1;
boolean isRunning = false;
boolean isRecording = false;
boolean needsReload = true;
boolean showEmpty = false;
String imageName = "lemmy2.jpg";
int gui_offset = 150;
int size = 10;
//////// your vars here ///////
void setup() {
size(500, 700);
//loadMainImage(imageName);
gui = new ControlP5(this);
gui.addToggle("setShowEmplty").setCaptionLabel("ON Show empty OFF").setPosition(10, 100).setValue(false).setMode(ControlP5.SWITCH).setColorCaptionLabel(color(0));
gui.getController("setShowEmplty").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE);
gui.addSlider("setSize").setSize(130, 30).setCaptionLabel("Detail").setPosition(10, 200).setRange(5, 50).setValue(10).setColorCaptionLabel(color(0));
gui.getController("setSize").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE);
gui.addBang("bangLoad").setSize(130, 30).setTriggerEvent(Bang.RELEASE).setCaptionLabel("Load image").setPosition(10, 600).setColorCaptionLabel(color(255));
gui.getController("bangLoad").getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER);
smooth();
background(255);
shapeMode(CORNER);
noFill();
}
void loadMainImage(String inImageName) {
println("loadMainImage");
p1 = loadImage(inImageName);
p1.filter(GRAY);
int tempheight = p1.height;
if (tempheight < 720 + 120)
tempheight = 720 + 120;
surface.setSize(p1.width + gui_offset, tempheight);
needsReload = true;
redrawImage();
}
void redrawImage() {
isRunning = true;
isRecording = false;
}
void draw() {
if (!isRunning) return;
if (isRecording) {
String[] p = splitTokens(imageName, "."); // split by point to know path without suffix
String savePath = p[p.length - 2] + "_" + day() + hour() + minute() + second() + ".svg";
println(savePath);
beginRecord(SVG, savePath);
}
processImage();
//////// your logic here ///////
if (isRecording) {
endRecord();
println("record done");
isRecording = false;
}
}
void keyPressed() {
if (key == 'r') { // save
isRecording = true;
isRunning = true;
redraw();
}
}
void bangDefault() {
gui.getController("sldXSpacing").setValue(2.0);
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the cancel selected.");
} else {
String loadPath = selection.getAbsolutePath();
// If a file was selected, print path to file
println("Selected file: " + loadPath);
String[] p = splitTokens(loadPath, ".");
boolean fileOK = false;
if ( p[p.length - 1].equals("GIF"))
fileOK = true;
if ( p[p.length - 1].equals("gif"))
fileOK = true;
if ( p[p.length - 1].equals("JPG"))
fileOK = true;
if ( p[p.length - 1].equals("jpg"))
fileOK = true;
if ( p[p.length - 1].equals("jpeg"))
fileOK = true;
if ( p[p.length - 1].equals("TGA"))
fileOK = true;
if ( p[p.length - 1].equals("tga"))
fileOK = true;
if ( p[p.length - 1].equals("PNG"))
fileOK = true;
if ( p[p.length - 1].equals("png"))
fileOK = true;
if (fileOK) {
println("File type OK.");
imageName = loadPath;
//isInit = false;
loadMainImage(imageName);
redrawImage();
} else {
// Can't load file
println("ERROR: BAD FILE TYPE");
}
}
}
void bangLoad() {
println(":::LOAD JPG, GIF or PNG FILE:::");
selectInput("Select an image file to open:", "fileSelected"); // Opens file chooser
} //End Load File
void setShowEmplty(boolean val) {
showEmpty = val;
if(p1 != null) {
background(255);
needsReload = true;
redrawImage();
}
}
void setSize(int val) {
size = val;
if(size%2 > 0) {
size++;
}
if(p1 != null) {
background(255);
needsReload = true;
redrawImage();
}
}
//////// your code here ///////
void processImage() {
p1.loadPixels();
float i = size/2;
float j = size/2;
int count = 1;
float shiftY = getVerticalShift();
float shiftX = getHorizontalShift();
while(i < p1.height - size) {
j = size/2;
if(count % 2 < 1) {
j = size;
}
while(j < p1.width - size) {
float bright = brightness(p1.pixels[round(j)+round(i)*p1.width]);
int maxLinesFill = round(map(size/2, 3, 10, 2, 5));
int darkcount = round(map(255 - bright, 0, 255, showEmpty ? 1: 0, maxLinesFill));
if(darkcount > 0) {
drawHex(j + gui_offset, i, darkcount);
}
j += (size - (shiftX * 2));
}
i += (size - shiftY);
count++;
}
isRunning = false;
}
void drawHex(float x, float y, int count) {
float delta_rad = (size/2)/(count*1.0);
float init_rad = size/2;
for(int i = 0; i < count; i++) {
float rad = init_rad - (delta_rad * i * 1.0);
if(rad < 2) {
point(x, y);
continue;
}
drawHexa(x, y, rad);
}
}
void drawHexa(float x, float y, float gs) {
noFill();
beginShape();
for(int i=0;i<6;i++){
float angle = i * 2 * PI / 6 + HALF_PI;
vertex(x + gs*cos(angle),y + gs*sin(angle));
}
endShape(CLOSE);
}
float getVerticalShift(){
return (size*1.0)/4;
}
float getHorizontalShift(){
return (size*1.0)/2 - ((size*1.0)/2 * 0.866);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment