/*
  utils.cpp
  
  Author: Sergi Diaz Planas (2017)
*/
#include "utils.h"

// Conversion from opencv rectangle to dlib rectangle.
dlib::rectangle openCVRectToDlib(cv::Rect r) {
    return dlib::rectangle((long)r.tl().x, (long)r.tl().y, (long)r.br().x - 1, (long)r.br().y - 1);
}

// Convert from opencv Mat to base64 string
std::string mat_to_base64(cv::Mat mat) {
    std::vector<uchar> buffer;

    cv::imencode(".jpg", mat, buffer);
    uchar *enc_str = new uchar[buffer.size()];
    for (unsigned int i=0; i<buffer.size(); i++) {
        enc_str[i] = buffer[i];
    }
    return base64_encode(enc_str, buffer.size());
}

// Convert from base64 string to opencv Mat
cv::Mat base_64_to_mat(std::string base64_string) {
    cv::Mat mat;

    std::string decoded_string = base64_decode(base64_string);
    std::vector<uchar> data(decoded_string.begin(), decoded_string.end());

    mat = imdecode(data, cv::IMREAD_UNCHANGED);
    return mat;
}



