
/*
 * Copyright (C) 2010-2022 TunaCode Pvt. Ltd.  All rights reserved.
 *
 * NOTICE TO USER:
 *
 *
 * TUNACODE MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE
 * CODE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED
 * WARRANTY OF ANY KIND.  TUNACODE DISCLAIMS ALL WARRANTIES WITH REGARD TO`
 * THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
 * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL
 * TUNACODE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS,  WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION,  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOURCE CODE.
 */


#include <cuvi.h>
#include <fstream>
#include <iostream>
#include <string> 


using namespace std;


void rotate1()
{
	//Cropped rotation with and without ROI.

	string ipath = "datasets/check.png";
	string opath = "datasets/check_";
	

	CuviStatus st;
	CuviImage input, output;


	st = input.create(ipath, CUVI_LOAD_IMAGE_COLOR);
	if(CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }





	//Rotate image about the origin at an angle 15.2
	CuviPoint2D<int> anchor(0, 0);
	float angle = 15.2;
	string outputname = opath + "origin_15.2.png";
	st = cuvi::geometryTransforms::rotate(input, output, angle,anchor);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }
	// Save results to disk
	st = cuvi::io::saveImage(output, outputname);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }



	//Rotate image about the center at an angle 15.2
	anchor = CuviPoint2D<int>((int)input.width() / 2, (int)input.height() / 2);
	angle = 15.2;
	outputname = opath + "center_15.2.png";
	st = cuvi::geometryTransforms::rotate(input, output, angle, anchor);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }
	// Save results to disk
	st = cuvi::io::saveImage(output, outputname);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }


	//Rotate ROI of image about the origin at an angle 15.2
	CuviRect roi(100, 75, 200, 150);
	anchor = CuviPoint2D<int>(0,0);
	angle = 15.2;
	outputname = opath + "roi_origin_15.2.png";
	st = cuvi::geometryTransforms::rotate(input, output, angle, anchor, roi);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }
	// Save results to disk
	st = cuvi::io::saveImage(output, outputname);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }



	//Rotate ROI of image about the center at an angle 15.2
	roi = CuviRect(100, 75, 200, 150);
	anchor = CuviPoint2D<int>(100, 75);
	angle = 15.2;
	outputname = opath + "roi_center_15.2.png";
	st = cuvi::geometryTransforms::rotate(input, output, angle, anchor, roi);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }
	// Save results to disk
	st = cuvi::io::saveImage(output, outputname);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }



	return;
}

void rotate2()
{
	//Non cropping. ROI is not supported. Also center of rotation doesn't matter.
	
	string ipath = "datasets/check.png";
	string opath = "datasets/check_";


	CuviStatus st;
	CuviImage input, output;


	st = input.create(ipath, CUVI_LOAD_IMAGE_COLOR);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }



	//Rotate image about an angle 15.2
	float angle = 15.2;
	string outputname = opath + "NoCrop_15.2.png";
	st = cuvi::geometryTransforms::rotateNoCrop(input, output, angle);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }
	// Save results to disk
	st = cuvi::io::saveImage(output, outputname);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }

	

	//Rotate image about the center at an angle 90
	angle = 90;
	outputname = opath + "NoCrop_90.png";
	st = cuvi::geometryTransforms::rotateNoCrop(input, output, angle);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }
	// Save results to disk
	st = cuvi::io::saveImage(output, outputname);
	if (CUVI_SUCCESS != st) { cout << "CUVI Error: " << st << endl;	return; }



	return;
}



int main()
{

	Cuvi32s device = cuvi::device::getCurentDevice();
	if (device < 0) {
		cout << "Error: None or Unsupported CUDA Device Found" << endl;
		system("pause");
		return 0;
	}

	rotate1(); //cropping rotation
	rotate2(); //Non cropping rotation
	
	
	system("pause");
	return 0;

	
	
}