Feedback!

Bitmap Converter

Views: 2032 Difficulty: 2 Status: Development
256x256

An open source bitmap converter program built with Open Frameworks

Want to show images on a JoyGamer or other Arduino powered SD Card reading, 128 by 160 pixel device? This little open-source program built with openFrameworks converts all kinds of images (e.g. .jpg, .png, .gif) into bitmaps (.bmp) of appropriate size and type to display on the Joy Gamer's little TFT LCD Screen. Adafruit sells these screens raw or with cool breakout boards.

Make Bitmaps for the Joy Gamer

Screenshot_2015-05-25_15.25.17
Create little Bitmaps for the Joy Gamer.

The App

This is the cpp file that does all the work in this little app. There are 2 ways to make bitmaps.
1) Press spacebar and select an image from your computer. It will display in the window and you can press 's' to save it. This is nice but you can only make 1 Bitmap at a time. So we also developed option 2...
2) You can also create bitmaps from every image in a folder. Change the lines near the top of the file:
	directoryName = "/Users/samfriedman/Pictures/small/";
        newFileName = "tl_";
Change the directoryName path to point to the folder with images that you want to make bitmaps from. The bitmaps will be numbered and have the prefix of the variable newFileName, so you may want to adjust that variable as well. Re-compile the project, run it and press 'd'.
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	ofSetLogLevel(OF_LOG_VERBOSE);
	directoryName = "/Users/samfriedman/Pictures/small/";
    newFileName = "tl_";
}

//--------------------------------------------------------------
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, i*160 + 20);

	}
	
	for (unsigned int i=0; i<processedImages.size(); i++){
		processedImages[i].draw(processedImages[i].getWidth()+5, i*160 + 20);
	}


	
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
	
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){
	if (key == 'd'){
        dir.listDir(directoryName);
        dir.sort(); // in linux the file system doesn't return file lists ordered in alphabetical order
        
        processedImages.clear();
        loadedImages.clear();
        
        // you can now iterate through the files and load them into the ofImage vector
        for(int i = 0; i < (int)dir.size(); i++){
            //Load the selected image
            ofImage image;
            int lastindex = dir.getPath(i).find_last_of(".");
            string rawname = dir.getPath(i).substr(0, lastindex);
            
            image.loadImage(dir.getPath(i));
            image.resize(128, 160);
            loadedImages.push_back(image);
            
            //Make some short variables
            int w = image.getWidth();
            int h = image.getHeight();
            
            //Make a new image to save manipulation by copying the source
            ofImage processedImage = image;
            
            //Walk through the pixels
            for (int y = 0; y < h; y++){
                for (int x = 0; x < w; x++){
                    
                    //Capture the colors for the row
                    ofColor color = image.getColor(x, y);
                    processedImage.setColor(x, y, color);
                }
            }
            //Store the processed image
            processedImages.push_back(processedImage);\
            processedImages[i].saveImage(dir.getOriginalDirectory()+newFileName+ofToString(i)+".bmp");

        }

    }
	if (key == ' '){
		
		//Open the Open File Dialog
		ofFileDialogResult openFileResult= ofSystemLoadDialog("Select a jpg or png"); 
		//Check if the user opened a file
		if (openFileResult.bSuccess){
			
			ofLogVerbose("User selected a file");
			
			//We have a file, check it and process it
			processOpenFileSelection(openFileResult);
			
		}else {
			ofLogVerbose("User hit cancel");
		}
	}
	
	if (key == 's'){
		
		if (processedImages.size()==0){
			//User is trying to save without anything to output - bail
			return;
		}
		
		//
		ofFileDialogResult saveFileResult = ofSystemSaveDialog(ofGetTimestampString() + ".bmp" , "Save your file as a bitmap");
		if (saveFileResult.bSuccess){
			processedImages[0].saveImage(saveFileResult.filePath);
		}
	}
	
	
}

//Sort function for stl::sort http://www.cplusplus.com/reference/algorithm/sort/
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()){
		//Limiting this example to one image so we delete previous ones
		processedImages.clear();
		loadedImages.clear();
		
		ofLogVerbose("The file exists - now checking the type via file extension");
		string fileExtension = ofToUpper(file.getExtension());
		
		//We only want images
		if (fileExtension == "JPG" || fileExtension == "PNG" || fileExtension == "BMP" || fileExtension == "GIF") {
			
			//Save the file extension to use when we save out
			originalFileExtension = fileExtension;
			
			//Load the selected image
			ofImage image;
			image.loadImage(openFileResult.getPath());
			image.resize(128, 160);
			loadedImages.push_back(image);
			
			//Make some short variables 
			int w = image.getWidth();
			int h = image.getHeight();
			
			//Make a new image to save manipulation by copying the source
			ofImage processedImage = image;
			
			//Walk through the pixels
			for (int y = 0; y < h; y++){
				for (int x = 0; x < w; x++){
					
					//Capture the colors for the row
					ofColor color = image.getColor(x, y); 
					processedImage.setColor(x, y, color);
				}
			}
			//Store the processed image
			processedImages.push_back(processedImage);
		}
        ofLogVerbose("The file has been copied.");

	}
	
}
//--------------------------------------------------------------
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::windowResized(int w, int h){
	
}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
	
}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 
	
}

Get the REPO

Download the latest version of the BMPforTFT OpenFrameworks project! Unzip this archive and copy the folder to you myApps directory within your OpenFrameworks installation (e.g. of_v0.8.4/apps/myApps/ ).
Permalink: http://lucidtronix.com/tutorials/66
Mixing pixels from two face images together....
Use the Joy Gamer to make a dynamic picture frame....
Display bitmap images and text files and folders with this TFT LCD screen and SD Card navigator....
Put images onto your circuit boards with eagle silkscreen footprint library....
Print bitmaps, cellular automata, drawings and text.....
Control the hue, value and saturation on this pocket-sized joystick drawing machine....
From simple rules emerge mysterious and intricate patterns....
Handheld gaming machine, based on the Arduino Leonardo, equipped with joystick, SD card and more....