// ModificarImagen.cpp : Defines the entry point for the console application.
//

/*CUDA*/
#include "cuda_runtime.h"

/*OpenCV*/
#include <opencv2/highgui/highgui.hpp>file://localhost/Developer-old/SDKs/MacOSX10.5.sdk/usr/local/lib/libopencv_calib3d.dylib

#include "ImageModification.h"

int main(int argc, char **argv){
	cudaSetDevice(0);
	/*Host Variables*/
	int imageW, imageH;				  //Size of Image
	cv::Mat frame_Original;			  // Original Image
	/*Device Variables*/
	uchar3 *Image_dev;

	cv::namedWindow("Original Frame",CV_WINDOW_AUTOSIZE);
	cv::namedWindow("Modified Frame",CV_WINDOW_AUTOSIZE);

	/*Load Image*/
	frame_Original = cv::imread(argc>1?argv[1]:"../../Images/Estacion.jpg",1);
	
	cv::imshow("Original Frame", frame_Original);	

	/*Size of Image*/
	imageW=frame_Original.cols;
	imageH=frame_Original.rows;
	size_t size=3*imageW*imageH;

	/*Create device memory*/
	cudaMalloc((void **)&Image_dev,size);

	/*Copy Memory (Host-->Device)*/
	cudaMemcpy(Image_dev,frame_Original.data,size,cudaMemcpyHostToDevice);
	
	/*Define the size of the grid and thread blocks*/
	dim3 threads(512,1,1);
	int N=imageW * imageH;
	dim3 grid(N/threads.x + (N%threads.x == 0 ? 0:1),1,1);
	/*Launch the Kernel Function*/
	CUDA_Modificar_Imagen(Image_dev,N,grid,threads);

	/*Copy Memory (Device-->Host)*/
	cudaMemcpy(frame_Original.data,Image_dev,size,cudaMemcpyDeviceToHost);

	cv::imshow("Modified Frame", frame_Original);
	
	cv::waitKey(0);

	/*Clean Memory*/
	/*Host*/
	cv::destroyWindow("Original Frame");
	cv::destroyWindow("Modified Frame");
	
	
	/*Device*/
	cudaFree(Image_dev);

	return(0);
}

