/* #########################################
 * Traffic monitoring using Raspberry PI
 * Author: Michal Zacpal
 * Library: Tools for camera
 * 2015
 * ######################################### */

#include "CameraTools.h"
/*
 *Grab img from USB Cam
 */
Mat grab_pic(VideoCapture cam)
{
    Mat I;  // Return image
    if (!cam.read(I)){
        I.eye(1,1,CV_8UC3);
        return I;
    }
    return I;
}
/*
 *Grab img from RPi cam
 */
Mat grab_pic_R(RaspiCam_Cv cam)
{
    Mat I;  // Return image
    if (!cam.grab()){
        I.eye(1,1,CV_8UC3);
        return I;
    }
    cam.retrieve(I);
    return I;
}
/*
 *  Return camera Settings
 */
QString get_cam_info(VideoCapture cam)
{
    QString output_message;
    if (!cam.isOpened()){
        output_message = "Cam is not opened!";
    }
    output_message = "Frame width:" + QString::number(cam.get(CV_CAP_PROP_FRAME_WIDTH));
    output_message = output_message + "\nFrame height:" + QString::number(cam.get(CV_CAP_PROP_FRAME_HEIGHT));
    output_message = output_message + "\nBrightness:" + QString::number(cam.get(CV_CAP_PROP_BRIGHTNESS));
    output_message = output_message + "\nContrast:" + QString::number(cam.get(CV_CAP_PROP_CONTRAST));
    output_message = output_message + "\nHue:" + QString::number(cam.get(CV_CAP_PROP_HUE));
    output_message = output_message + "\nGain:" + QString::number(cam.get(CV_CAP_PROP_GAIN));
    output_message = output_message + "\nExposure:" + QString::number(cam.get(CV_CAP_PROP_EXPOSURE));
    return output_message;
}

/*
 *  Save picture
 *  iCam =  0 - Front cam;  iCam = 1 - Rear cam ; iCam = 2 - Test pictures
 */
bool save_pic(QString path, Mat matImg,int iCam)
{

    switch (iCam)
    {
        case 0: path = path + "/Front_" + GetSystemTime(3); break;
        case 1: path = path + "/Rear_" + GetSystemTime(3); break;
        case 2: path = path + "/Test_" + GetSystemTime(3); break;
    }

    QDir dir(path);
    if (!dir.exists()) {
        if (!dir.mkpath(path))
            return false;
    }
    QString QsFileName = path + "/" + GetSystemTime(4) + ".png";
    vector<int> vCompressionParams;
    vCompressionParams.push_back(CV_IMWRITE_PNG_COMPRESSION);
    vCompressionParams.push_back(3);
    imwrite(QsFileName.toStdString().c_str(),matImg,vCompressionParams);
    return true;
}
