
/*
 * Copyright (C) 2010-2022 TunaCode Pvt. Ltd.  All rights reserved.
 *
 * NOTICE TO USER:
 *
 *
 * TUNACODE MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE
 * CODE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED
 * WARRANTY OF ANY KIND.  TUNACODE DISCLAIMS ALL WARRANTIES WITH REGARD TO`
 * THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
 * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL
 * TUNACODE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS,  WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION,  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOURCE CODE.
 */


#include <cuvi.h>
#include <iostream>
#include <fstream>
//#include <cuda_runtime.h>

using namespace std;

//This function reads input image into CuviImage, calls encode which encodes and writes encoded image into outputPath
void decodeEncodeJPEG(string inputPath, string outputPath)
{
    CuviImage img;

    CuviStatus err = cuvi::codecs::decode(inputPath, img);

    if(err!= CUVI_SUCCESS)
    {
        cout <<"Failed to decode image, error: "<<err<<endl;
        return;
    }

    cout<<"Decoded image dimensions: "<<img.width()<<"x"<<img.height()<<endl;
    cout<<"Encoding image and writing to "<<outputPath<<endl;

    err = cuvi::codecs::encode(img, CUVI_JPEG, outputPath);

    if(err!= CUVI_SUCCESS)
    {
        cout <<"Failed to encode image, error: "<<err<<endl;
        return;
    }

    return;
}

//This function reads input image into CuviImage, calls encode which encodes and writes encoded image into outputPath
void encodeJPEGFile(string inputPath, string outputPath)
{
    CuviImage img;
    img.create(inputPath, CUVI_LOAD_IMAGE_COLOR);

    Cuvi8u* compressedImage;
    unsigned int compressedLength = 0;
    unsigned int quality = 70;

    bool copyToCPU = true;

    CuviStatus err = cuvi::codecs::encode(img, CUVI_JPEG, outputPath, quality);

    if(err!= CUVI_SUCCESS)
    {
        cout <<"Failed to encode image, error: "<<err<<endl;
        return;
    }

    return;
}


int main()
{
    Cuvi32s device = cuvi::device::getCurentDevice();
    if (device < 0) {
        cout << "Error: None or Unsupported CUDA Device Found" << endl;
        system("pause");
        return 0;
    }

    encodeJPEGFile("datasets/encode.png", "datasets/encoded.jpg"); //Create an empty white image and then encode it 
    
    //decodeEncodeJPEG("datasets/encoded.jpg", "datasets/encoded2.jpg");

    cout << "Program executed successfully." << endl;
    return 0;   
}