#include <iostream>
#include "bmpimage.h"

const std::string FILENAME = "../elef.bmp";
const std::string FILENAME_OUT = "../elef_out.bmp";

//
void lab01() {
    auto og_image = readBMPImage("../elef.bmp");
    auto pixels1 = og_image.pixels_copy();
    auto pixels2 = og_image.pixels_copy();
    for (int i = 0; i < og_image.height(); ++i) {
        for (int j = 0; j < og_image.width(); ++j) {
            pixels1(i, j) = pixels1(i, j) / 2;
            pixels2(i, j) = pixels2(i, j) * 2;
        }
    }
    auto darkenImg = BMPImage(pixels1);
    auto lighterImg = BMPImage(pixels2);
    auto invert_img = invertColors(og_image); // TODO
    auto grayscale_img = grayscale(og_image);
    og_image.appendRight(lighterImg).appendRight(darkenImg).appendRight(invert_img).appendRight(grayscale_img).save(
            "../lab01/elef1.bmp");
}
//
void lab02_01() {
    auto img = readBMPImage("../tea.bmp");
    auto img_512 = downscale2x(img);
    auto img_256 = downscale2x(img_512);
    auto img_128 = downscale2x(img_256);
    img_512.save("../lab02/tea_512.bmp");
    img_256.save("../lab02/tea_256.bmp");
    img_128.save("../lab02/tea_128.bmp");
    auto newImg = img.appendRight(img_512).appendRight(img_256).appendRight(img_128);
    newImg.save("../lab02/tea_downscale.bmp");
//    auto img_from_512 = upscale2x(img_512);
////    auto img_from_256 = upscale2x(upscale2x(img_256));
////    auto img_from_128 = upscale2x(upscale2x(upscale2x(img_128)));
//    auto tmp = upscale2x(img_256);
//    auto img_from_256 = upscale2x(tmp);
//    auto tmp2 = upscale2x(img_128);
//    auto tmp3 = upscale2x(tmp2);
//    auto img_from_128 = upscale2x(tmp3);
//    img_from_512.save("../lab02/tea_from_512.bmp");
//    img_from_256.save("../lab02/tea_from_256.bmp");
//    img_from_128.save("../lab02/tea_from_128.bmp");
//    img.appendRight(img_from_512)->appendRight(img_from_256)->appendRight(img_from_128)->save("../lab02/tea_upscale.bmp");
////    newImg2->save("../lab02/tea_upscale.bmp");
}
//
void lab02_02() {
    auto img = readBMPImage("../tea.bmp");
    auto gsImg = grayscale(img);
    gsImg.save("../lab02/tea_grayscale_256.bmp");
    auto gsPixels = gsImg.pixels_copy();
    auto width = gsImg.width();
    auto height = gsImg.height();
    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            gsPixels(i,j) = (gsPixels(i,j) / 2) * 2;
        }
    }
    auto gsImg128 = BMPImage(gsPixels);
    gsPixels = gsImg.pixels_copy();
    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            gsPixels(i,j) = (gsPixels(i,j) / 4) * 4;
        }
    }
    auto gsImg64 = BMPImage(gsPixels);
    gsPixels = gsImg.pixels_copy();
    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            gsPixels(i,j) = (gsPixels(i,j) / 8) * 8;
        }
    }
    auto gsImg32 = BMPImage(gsPixels);
    gsImg128.save("../lab02/tea_grayscale_128.bmp");
    gsImg64.save("../lab02/tea_grayscale_64.bmp");
    gsImg32.save("../lab02/tea_grayscale_32.bmp");
    gsImg.appendRight(gsImg128).appendRight(gsImg64).appendRight(gsImg32).save("../lab02/tea_grayscale.bmp");
}
//
void lab02_03(){
    auto img = readBMPImage("../tea.bmp");
    auto img_1_5 = upscale1_5x(img);
    auto img_2 = upscale2x(img);
    auto img_3 = upscale2x(img_1_5);
    img_1_5.save("../lab02/lab02_03/tea_x1_5.bmp");
    img_2.save("../lab02/lab02_03/tea_x2.bmp");
    img_3.save("../lab02/lab02_03/tea_x3.bmp");
    img.appendRight(img_1_5).appendRight(img_2).appendRight(img_3).save("../lab02/lab02_03/tea_upscale.bmp"); //.appendRight(img_3)
//    img.appendRight(img_1_5).save("../lab02/lab02_03/tea_upscale.bmp");
}

void test() {
//    Pixel **test;
    PixelArray test(3, 3);
//    test = new Pixel *[3];
//    test[0] = new Pixel[3];
//    test[1] = new Pixel[3];
//    test[2] = new Pixel[3];
    test(0, 0) = Pixel{0, 0, 0};
    test(0, 1) = Pixel{127, 127, 127};
    test(0, 2) = Pixel{255, 255, 255};
    test(1, 0) = Pixel{127, 127, 127};
    test(1, 1) = Pixel{63, 63, 63};
    test(1, 2) = Pixel{127, 127, 127};
    test(2, 0) = Pixel{255, 255, 255};
    test(2, 1) = Pixel{127, 127, 127};
    test(2, 2) = Pixel{0, 0, 0};
    auto test_img = BMPImage(test);
    test_img.save("../test.bmp");
    auto test_scale = upscale2x(test_img);
    test_scale.save("../test_scale.bmp");
}

int main() {
//    lab01();
//    lab02_01();
    lab02_02();
//    lab02_03();
//    test();
    return 0;
}
