Created
May 21, 2012 19:07
-
-
Save companje/2764020 to your computer and use it in GitHub Desktop.
Export to PDF with ofxCairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "testApp.h" | |
//-------------------------------------------------------------- | |
void testApp::setup(){ | |
w = ofGetWidth(); | |
h = ofGetHeight(); | |
cam.initGrabber(w,h,true); | |
pdf = new ofxCairo(); | |
saving = false; | |
size = 4; | |
step = 7; | |
ofSetFrameRate(30); | |
ofBackground(255,255,255); | |
ofSetWindowTitle("move mouse for changing detail, press 's' to save"); | |
} | |
//-------------------------------------------------------------- | |
void testApp::update(){ | |
cam.update(); | |
} | |
//-------------------------------------------------------------- | |
void testApp::draw(){ | |
ofSetColor(255,255,255); | |
//if saving mode is on begin PDF writing | |
if (saving) { | |
filename = ofToString(ofRandomuf()) + ".pdf"; | |
pdf->begin(filename, w, h, OF_OUTPUT_PDF); | |
} | |
//distance between circles and their size depends on mouse pos | |
size = ofMap(mouseX,0,ofGetWidth(),1,10); | |
step = ofMap(mouseY,0,ofGetHeight(),10,3); | |
for (int y=0; y<h; y+=step) { | |
for (int x=0; x<w; x+=step) { | |
//get pixel color from camera | |
int i = (y*w+x)*3; | |
int r = cam.getPixels()[i+0]; | |
int g = cam.getPixels()[i+1]; | |
int b = cam.getPixels()[i+2]; | |
//calculate brightness | |
float bri = (r+g+b) / 765.0; | |
//draw black circles | |
pdf->setColor(0); | |
pdf->circle(x,y,(1-bri)*size); | |
} | |
} | |
//if saving mode was on finish pdf writing and open preview | |
if (saving) { | |
pdf->end(); | |
delete pdf; | |
pdf = new ofxCairo(); | |
saving = false; | |
system(("open " + ofToDataPath(filename)).c_str()); | |
} | |
} | |
//-------------------------------------------------------------- | |
void testApp::keyPressed(int key){ | |
//press s to enter saving mode | |
if (key=='s') saving=true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxCairo.h" | |
class testApp : public ofBaseApp { | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void keyPressed (int key); | |
int w,h; | |
ofxCairo *pdf; | |
ofVideoGrabber cam; | |
bool saving; | |
int step, size; | |
string filename; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment