#include <iostream>
#include <array>
#include <cstdint>
#include <cmath>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

void affine_flip() {
	Mat src = imread("../../resources/images/eastsea.bmp");

	imshow("src", src);

	Mat dst;
	int flipCode[] = { 1, 0, -1 };
	for (int i = 0; i < 3; i++) {
		flip(src, dst, flipCode[i]);

		String desc = format("filpcode: %d", flipCode[i]);
		putText(dst, desc, Point(10, 30), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 1, LINE_AA);

		imshow("dst", dst);
		waitKey();
	}
	destroyAllWindows();
}

void affine_rotation() {
	Mat src = imread("../../resources/images/tekapo.bmp");

	Point2f cp(src.cols / 2.f, src.rows / 2.f);

	array<float_t, 6> data = { 0.f, 0.f, cos(20 * 3.141592 / 180), 0.f, 0.f, sin(20 * 3.141592 / 180) };
	Mat M(2, 3, CV_32F, data.data());

	M += getRotationMatrix2D(cp, 20, 1);
	Mat dst;

	warpAffine(src, dst, M, Size(src.cols + cos(20 * 3.141592 / 180), src.rows + sin(20 * 3.141592 / 180)));

	imshow("src", src);
	imshow("dst", dst);

	waitKey();
	destroyAllWindows();
}

void affine_scale() {
	Mat src = imread("../../resources/images/rose.bmp");

	Mat dst1, dst2, dst3, dst4;
	resize(src, dst1, Size(), 4, 4, INTER_NEAREST);
	resize(src, dst2, Size(1920, 1280));
	resize(src, dst3, Size(1920, 1280), 0, 0, INTER_CUBIC);
	resize(src, dst4, Size(1920 * 2, 1280 * 2), 0, 0, INTER_LANCZOS4);

	imshow("src", src);
	imshow("dst1", dst1(Rect(400, 500, 400, 400)));
	imshow("dst2", dst2(Rect(400, 500, 400, 400)));
	imshow("dst3", dst3(Rect(400, 500, 400, 400)));
	imshow("dst4", dst4(Rect(800, 1000, 400, 400)));

	waitKey();
	destroyAllWindows();
}

void affine_shear() {
	Mat src = imread("../../resources/images/tekapo.bmp");

	double_t mx = tan(15 * 3.141592 / 180); // x축에 대해 각도만큼 기울이는 코드
	double_t my = tan(15 * 3.141592 / 180); // y축에 대해 각도만큼 기울이는 코드
	Mat M = Mat_<double_t>({ 2, 3 }, { 1, mx, 0, my, 1, 0 }), dst;

	warpAffine(src, dst, M, Size(cvRound(src.cols + src.rows * mx), cvRound(src.rows + src.cols * my)));

	imshow("src", src);
	imshow("dst", dst);

	waitKey();
	destroyAllWindows();
}

void affine_translation() {
	Mat src = imread("../../resources/images/tekapo.bmp");

	Mat M = Mat_<double_t>({ 2, 3 }, { 1, 0, 150, 0, 1, 100 }), dst;

	Size _new_size = src.size();
	_new_size.width += 150 * 2;
	_new_size.height += 100 * 2;
	warpAffine(src, dst, M, _new_size);

	imshow("src", src);
	imshow("dst", dst);

	waitKey();
	destroyAllWindows();
}

void affine_transform() {
	Mat src = imread("../../resources/images/tekapo.bmp");

	/*Point2f srcPts[3] = {
		Point2f(0, 0),
		Point2f(src.cols - 1, 0),
		Point2f(src.cols - 1, src.rows - 1)
	}, dstPts[3] = {
		Point2f(50, 50),
		Point2f(src.cols - 100, 100),
		Point2f(src.cols - 50, src.rows - 50)
	};*/

	array<float_t, 6> data = { 0.8f, 0.f, 30.f, 0.f, 0.8f, 30.f };
	Mat M(2, 3, CV_32F, data.data());
	Mat dst = imread("../../resources/images/lenna.bmp");

	warpAffine(src, dst, M, Size(), INTER_LINEAR, BORDER_CONSTANT, Scalar(50, 50, 100));

	vector<Point2f> sP = { Point2f(10, 10), Point2f(30, 70), Point2f(100, 80) }, tP;
	transform(sP, tP, M);
	Mat cM = getAffineTransform(sP, tP);

	cout << "Transform matrix M : " << M << endl;
	cout << "Calculated matrix cM : " << cM << endl;

	imshow("src", src);
	imshow("dst", dst);

	waitKey();
	destroyAllWindows();
}

int main() {
	affine_rotation();
	return 0;
}