#include "graffLetter.h"


//----------------------------------------------
void graffLetter::setup(int w, int h){
	width = w;
	height = h;
	
	cvImgWhite.allocate(width,height);
	cvImgWhite.set(0);
	cvImgBlack.allocate(width,height);
	cvImgBlack.set(0);
	
	cvImgBottom.allocate(width,height);
	cvImgBottom.set(0);
	cvImgTop.allocate(width,height);
	cvImgTop.set(0);
	
	cvImgMix.allocate(width,height);
	cvImgMix.set(0);
	
	cvTopLayerMask.allocate(width,height);
	cvTopLayerMask.set(0);
	cvBottomLayerMask.allocate(width,height);
	cvBottomLayerMask.set(0);

	brushsize = 40;
	setBrushSize(brushsize);
	
}

//does it all in one go :)
//----------------------------------------------
void graffLetter::setBrushSize(int bSize){
	
	brushsize = bSize;
	
	float ratio1 = 32.0/40.0;
	float ratio2 = 120.0/40.0;
		
	setupBrush(cvWhiteBrush, brushsize);
	setupBrush(cvBlackBrush, (float)bSize * ratio1);
	
	setupShadowBrush(cvDropShadowBrush,(float)bSize * ratio2);	
}

//----------------------------------------------
int graffLetter::getBrushSize(){
	
	return brushsize;
}


//----------------------------------------------
void graffLetter::setupBrush(ofCvGrayscaleImage &brush, int width){
	brush.allocate(width,width);
	unsigned char * temp = new unsigned char[width*width];
	float midPt 	= (float)width/2.0f;
	float radius 	= (float)width/2.0f;
	for (int i = 0; i < width; i++){
		for (int j = 0; j < width; j++){
			float dist = (sqrt((midPt-i)*(midPt-i) + (midPt-j)*(midPt-j)) / radius);
			if (dist < 1){
				float shape = powf((1-dist), 2.0f);
				temp[j*width + i] = 255;//(shape) * 100.0f;	
			} else {
				temp[j*width + i] = 0;
			}
				
		}
	}
	brush.setFromPixels(temp, width, width);
	delete temp;
}

//----------------------------------------------
void graffLetter::setupShadowBrush(ofCvGrayscaleImage &brush, int width){
	
	brush.allocate(width,width);
	unsigned char * temp = new unsigned char[width*width];
	float midPt 	= (float)width/2.0f;
	float radius 	= (float)width/2.0f;
	for (int i = 0; i < width; i++){
		for (int j = 0; j < width; j++){
			float dist = (sqrt((midPt-i)*(midPt-i) + (midPt-j)*(midPt-j)) / radius);
			if (dist < 1){
				float shape = powf((1-dist), 2.0f);
				temp[j*width + i] = (shape) * 100.0f;	
			} else {
				temp[j*width + i] = 0;
			}
				
		}
	}
	brush.setFromPixels(temp, width, width);
	brush.blurHeavily();
	delete temp;
}

//----------------------------------------------
void graffLetter::update(){
	cvImgBottom.set(0);
	cvImgBottom += cvImgWhite;
	cvImgBottom -= cvImgBlack;

	cvImgMix = cvImgTop;
	cvImgMix.composite(cvImgBottom, cvBottomLayerMask);
}

//----------------------------------------------
unsigned char * graffLetter::getImageAsPixels(){
	return cvImgMix.getPixels();
}

//----------------------------------------------
void graffLetter::newLetter(){
	
	cvImgBottom.set(0);
	cvImgBottom += cvImgWhite;
	cvImgBottom -= cvImgBlack;
	
	cvImgTop.composite(cvImgBottom, cvBottomLayerMask);
	cvImgWhite.set(0);
	cvImgBlack.set(0);
	cvBottomLayerMask.set(0);
}

//----------------------------------------------
void graffLetter::draw(){
	cvImgMix.draw(0,0);
	
							
}

//----------------------------------------------
void graffLetter::clear(){
	cvImgBottom.set(0);
	cvImgTop.set(0);
	cvImgWhite.set(0);
	cvImgBlack.set(0);
	cvBottomLayerMask.set(0);
}

//----------------------------------------------
void graffLetter::addLine(float x1, float y1, float x2, float y2){
	
	
	int nDivs 	= 18;
	float dx 	= (float)(x2 - x1)/(float)nDivs;
	float dy 	= (float)(y2 - y1)/(float)nDivs;
	
	// white
	for (int j = 0; j < nDivs; j++){
		cvImgWhite.addIntoMe(cvWhiteBrush, x1 + dx*j-20, y1 + dy*j-20);
		cvBottomLayerMask.addIntoMe(cvWhiteBrush, x1 + dx*j-20, y1 + dy*j-20);
	}
	
	// offset!
	for (int j = 0; j < nDivs; j++){
		cvImgWhite.addIntoMe(cvWhiteBrush, x1 + dx*j-30, y1 + dy*j-30);
		cvBottomLayerMask.addIntoMe(cvWhiteBrush, x1 + dx*j-30, y1 + dy*j-30);
	}
	
	cvBottomLayerMask.addIntoMe(cvDropShadowBrush, x1 - 60, y1 - 60);
	
	
	
	// black 
	for (int j = 0; j < nDivs; j++){
		cvImgBlack.addIntoMe(cvBlackBrush, x1 + dx*j-17, y1 + dy*j-17);
	}
	
}
