#include "ofApp.h"
void ofApp::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
}
void ofApp::update(){
}
void ofApp::draw()
{
ofDrawBitmapString("Press spacebar to open an image, \"s\" to save the processed output", 20, 15);
for (unsigned int i=0; i<loadedImages.size(); i++){
loadedImages[i].draw(0, 20);
}
for (unsigned int i=0; i<processedImages.size(); i++){
processedImages[i].draw(processedImages[i].getWidth(), 20);
}
}
void ofApp::keyPressed(int key){
}
void ofApp::keyReleased(int key){
if (key == ' '){
ofFileDialogResult openFileResult= ofSystemLoadDialog("Select a jpg or png");
if (openFileResult.bSuccess){
ofLogVerbose("User selected a file");
processOpenFileSelection(openFileResult);
}else {
ofLogVerbose("User hit cancel");
}
}
if (key == 's'){
if (processedImages.size()==0){
return;
}
ofFileDialogResult saveFileResult = ofSystemSaveDialog(ofGetTimestampString() + "." + ofToLower(originalFileExtension), "Save your file");
if (saveFileResult.bSuccess){
processedImages[0].save(saveFileResult.filePath);
}
}
}
bool sortColorFunction (ofColor i,ofColor j) {
return (i.getBrightness()<j.getBrightness());
}
void ofApp::processOpenFileSelection(ofFileDialogResult openFileResult){
ofLogVerbose("getName(): " + openFileResult.getName());
ofLogVerbose("getPath(): " + openFileResult.getPath());
ofFile file (openFileResult.getPath());
if (file.exists()){
processedImages.clear();
loadedImages.clear();
ofLogVerbose("The file exists - now checking the type via file extension");
string fileExtension = ofToUpper(file.getExtension());
if (fileExtension == "JPG" || fileExtension == "PNG") {
originalFileExtension = fileExtension;
ofImage image;
image.load(openFileResult.getPath());
if (image.getWidth()>ofGetWidth() || image.getHeight() > ofGetHeight())
{
image.resize(image.getWidth()/2, image.getHeight()/2);
}
loadedImages.push_back(image);
int w = image.getWidth();
int h = image.getHeight();
ofImage processedImage = image;
for (int y = 0; y < h; y++){
vector<ofColor> colorsToSort;
for (int x = 0; x < w; x++){
ofColor color = image.getColor(x, y);
colorsToSort.push_back(color);
}
sort (colorsToSort.begin(), colorsToSort.end(), sortColorFunction);
for (int x = 0; x < w; x++)
{
processedImage.setColor(x, y, colorsToSort[x]);
}
}
processedImages.push_back(processedImage);
}
}
}
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){
}
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