#include "hdrplus/hdrplus_pipeline.h"

int main( int argc, char** argv )
{
    int rotation = atoi(argv[1]);
    bool frontCamera = atoi(argv[2]) != 0;

    std::vector<std::string> paths;
    for (int idx = 4; idx < argc; idx++)
    {
        paths.push_back(argv[idx]);
    }

    cv::Mat mat;

    hdrplus::hdrplus_pipeline pipeline;
    pipeline.run_pipeline( paths, 0, mat);

    if (mat.empty())
    {
        printf("run_pipeline return empty mat");
    }
    mat = hdrplus::convert16bit2_8bit_(mat.clone());

    if (rotation > 0)
    {
        if (rotation == 1)  // 0
        {
            cv::Mat tempPic;
            cv::transpose(mat, tempPic);
            cv::flip(tempPic, mat, 0);
        }
        else if (rotation == 2) // 90
        {
            cv::Mat tempPic;
            cv::transpose(mat, tempPic);
            cv::flip(tempPic, mat, 1);
        }
        else if (rotation == 3) // 180
        {
            if (frontCamera)
            {
                cv::flip(mat, mat, 0);
            }
            else
            {
                cv::flip(mat, mat, -1);
            }
        }
        else if (rotation == 4) // 270
        {
            cv::Mat tempPic;
            cv::transpose(mat, tempPic);
            cv::flip(tempPic, mat, 0);
        }

    }
    cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR);

    if (mat.empty())
    {
        printf("mat is empty before save");
    }

    bool res = cv::imwrite(argv[3], mat);
    if (!res)
    {
        printf("Failed to write file %s err=%d", argv[3], errno);
    }

    return 0;
}