#include "ofApp.h"
void ofApp::setup() {
quality = OF_IMAGE_QUALITY_WORST;
maxSize = 2048;
glitchStart = .6;
reset();
}
void ofApp::reset() {
generation = 0;
img.load("buses.jpg");
addX = ofRandom(0, 16);
addY = ofRandom(0, 16);
subX = ofRandom(0, addX);
subY = ofRandom(0, addY);
}
void ofApp::update() {
string curFilename = "compressed.jpg";
int size = img.getWidth();
if(size < maxSize) {
img.save(curFilename, quality);
if(ofGetKeyPressed('g')) {
ofBuffer file = ofBufferFromFile(curFilename, true);
int fileSize = file.size();
char * buffer = file.getData();
int whichByte = (int) ofRandom(fileSize * glitchStart, fileSize);
int whichBit = ofRandom(8);
char bitMask = 1 << whichBit;
buffer[whichByte] |= bitMask;
ofBufferToFile(curFilename, file, true);
img.load(curFilename);
} else {
img.load(curFilename);
if(ofGetFrameNum() % 2 == 0) {
img.resize(size + addX, size + addY);
} else {
img.resize(size - subX, size - subY);
}
}
generation++;
}
}
void ofApp::draw() {
ofSetColor(255);
img.draw(0, 0, ofGetWidth(), ofGetHeight());
ofSetColor(0);
ofDrawRectangle(5, 5, 290, 45);
ofSetColor(255);
ofDrawBitmapString("Currently on generation " + ofToString(generation), 10, 20);
ofDrawBitmapString("Click to reset, hold 'g' to glitch.", 10, 40);
}
void ofApp::keyPressed (int key){
}
void ofApp::keyReleased (int key){
}
void ofApp::mouseMoved(int x, int y){
}
void ofApp::mouseDragged(int x, int y, int button){
}
void ofApp::mousePressed(int x, int y, int button){
reset();
}
void ofApp::mouseReleased(int x, int y, int button){
}
void ofApp::mouseEntered(int x, int y){
}
void ofApp::mouseExited(int x, int y){
}
void ofApp::windowResized(int w, int h){
}
void ofApp::gotMessage(ofMessage msg){
}
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Comments