Function:Encode

From CUVI Wiki
Jump to: navigation, search

JPEG Encoding: Encodes an image pixel array data to a JPEG.

Function

CuviStatus encode(const CuviImage& src,
                  CuviImageFormat outputFormat,
                  Cuvi8u*& compressedData,
                  Cuvi32u* compressedLength,
                  Cuvi32u quality,
                  bool copyToCPU,
                  const CuviStream& stream = CuviStream());


Parameters

Name Type Description
src const CuviImage& Input Image
outputFormat CuviImageFormat Encoding format, currently supports the following:
CUVI_JPEG
compressedData Cuvi8u*& Encoded data
compressedLength Cuvi32u* Length of encoded data
quality Cuvi32u Quality/Compression
copyToCPU bool Whether or not to copy the data to CPU memory
stream const CuviStream& GPU stream ID for execution


Example

    CuviImage img;
    img.create(inputPath, CUVI_LOAD_IMAGE_DEFAULT);
 
    Cuvi8u* compressedImage;
    unsigned int compressedLength = 0;
    unsigned int quality = 100;
 
    bool copyToCPU = true;
 
    CuviStatus err = cuvi::codecs::encode(img, CUVI_JPEG, compressedImage, &compressedLength, quality, copyToCPU);
 
    if(err!= CUVI_SUCCESS)
    {
        cout <<"Failed to encode image, error: "<<err<<endl;
        return;
    }
 
    cout<<"Compressed image length: "<<compressedLength<<endl; 
 
    cout<<"Writing JPEG output to "<<outputPath<<endl;
    ofstream outputFile(outputPath.c_str(), std::ios::out | std::ios::binary);
 
    if(copyToCPU)
    {
        outputFile.write(reinterpret_cast<const char *>(compressedImage), static_cast<int>(compressedLength));
        free(compressedImage);
        compressedImage = 0;
    }
    else
    {
        Cuvi8u* compressedImageCPU = (Cuvi8u*)malloc(compressedLength);
        cudaMemcpy(compressedImageCPU, compressedImage, (size_t)compressedLength*sizeof(Cuvi8u), cudaMemcpyDeviceToHost);
        outputFile.write(reinterpret_cast<const char *>(compressedImageCPU), static_cast<int>(compressedLength));
        free(compressedImageCPU);
        cudaFree(compressedImage);
 
        compressedImageCPU = 0;
        compressedImage = 0;
    }