#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	 
	ofBackground(255,255,255);	
	
	//this is the image we are processing
	imageIn.loadImage("ofTheo.jpg");

	//this is an array of bytes we will use to store the processed pixels.
	//we allocate the width of the image times the height times 4 because it 
	//may have an alpha channel
	processedPixels = new unsigned char[imageIn.width * imageIn.height * 4];
		
}

//--------------------------------------------------------------
void testApp::update(){

	//this points to the the pixels of the loaded image
	//ptr can be treated as an array that is the size of the number of 
	//bytes in the image.
	unsigned char * ptr = imageIn.getPixels();
	
	//we assume its just an rgb image
	// the image goes RGBRGBRGB so each pixel has 3 bytes R G and B 
	//so the number of bytes we process is 3 times the dimensions of the image
	int num = imageIn.width * imageIn.height * 3;
	
	//we can go through for each byte and mask it to get only the
	//least important pixels.
	for(int i = 0; i < num; i++){
	
		//first we set our output pixel to black
		//to make sure it is clean to start off with
		processedPixels[i] = 0;
		

		//don't really know how this works
		//but got it from here
		//http://mindprod.com/jgloss/masking.html
		//gives us just the smallest three pixels
		processedPixels[i] = ptr[i] & 0x7;
		
		//this is not visible to the naked eye so - to see it 
		//I multiple by 20
		//if you want to save the image out as is you could comment this out.
		if( processedPixels[i] != 0 ) processedPixels[i] *= 20;
		
	}
	
	//we give the pixels to our other ofImage object - so we can display it on screen and also save it to disk
	imageOut.setFromPixels(processedPixels, imageIn.width, imageIn.height, OF_IMAGE_COLOR);

}

//--------------------------------------------------------------
void testApp::draw(){

	//we make both images be drawn to the screen - we squash them in so they are shown as big as possible.
	imageIn.draw(0, 0, ofGetWidth()/2, ofGetHeight());
	imageOut.draw(ofGetWidth()/2, 0, ofGetWidth()/2, ofGetHeight());
}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
	if(key == 's'){
		//save as jpeg to avoid loss of data due to 
		//compression
		imageOut.saveImage("output.png");
	}
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){ 
	
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(){
}
