1

I am trying to store and retrieve images using libjpeg-turbo, I have tried the following code but it leads to loss when I compare it against the initial image used.

CompressSave() takes a cv::Mat which contains the image data which needs to be saved as a jpg file.

I am passing the image data that needs to be stored to CompressSave() which will compress the data and store it as a JPG. DeCompressRead() is used for reading the data and decompress from the stored image.

Have I missed any parameters?

I have used File handling since save tjSave is causing this to crash.

void CompressSave(cv::Mat img, std::string filename)
{
    const int JPEG_QUALITY = 100;
    const int COLOR_COMPONENTS = 3;
    int _width = img.rows;
    int _height = img.cols;
    long unsigned int _jpegSize = 0;
    unsigned char* _compressedImage = NULL; //!< Memory is allocated by tjCompress2 if _jpegSize == 0
    unsigned char* buffer = img.data;

    tjhandle _jpegCompressor = tjInitCompress();

    tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
        &_compressedImage, &_jpegSize, TJSAMP_444, JPEG_QUALITY,
        TJFLAG_ACCURATEDCT);

    tjDestroy(_jpegCompressor);

    FILE *file = fopen(filename.c_str(), "wb");
    if (!file) {
        std::cout << "Could not open JPEG file: " << strerror(errno);
        return;
    }
    if (fwrite(_compressedImage, _jpegSize, 1, file) < 1) {
        cerr << "Could not write JPEG file: " << strerror(errno);
        return;
    }
    fclose(file);

    //to free the memory allocated by TurboJPEG (either by tjAlloc(), 
    //or by the Compress/Decompress) after you are done working on it:
    tjFree(_compressedImage);
}

unsigned char* DeCompressRead(std::string fileName)
{
    const int JPEG_QUALITY = 100;
    const int COLOR_COMPONENTS = 3;
    int _width;
    int _height;
    long unsigned int _jpegSize = 0;
    unsigned char* _compressedImage; //!< Memory is allocated by tjCompress2 if _jpegSize == 0

    long size;
    int inSubsamp, inColorspace;
    unsigned long jpegSize;
    unsigned char* jpegBuf = nullptr;

    unsigned char *imgBuf = nullptr;
    int align(0);
    //int pixelFormat;
    int flags(0);

    FILE *jpegFile = nullptr;
    jpegFile = fopen(fileName.c_str(), "rb");

    fseek(jpegFile, 0, SEEK_END);
    size = ftell(jpegFile);
    fseek(jpegFile, 0, SEEK_SET);

    jpegSize = (unsigned long)size;
    jpegBuf = (unsigned char *)tjAlloc(jpegSize);

    fread(jpegBuf, jpegSize, 1, jpegFile);
    fclose(jpegFile);  jpegFile = NULL;


    tjhandle _jpegDecompressor = tjInitDecompress();
    int jpegSubsamp;
    tjDecompressHeader2(_jpegDecompressor, jpegBuf, jpegSize, &_width, &_height, &jpegSubsamp);

    int pixelFormat = TJPF_RGB;
    imgBuf = (unsigned char *)tjAlloc(_width * _height * tjPixelSize[pixelFormat]);
    tjDecompress2(_jpegDecompressor, jpegBuf, jpegSize, imgBuf, _width, 0/*pitch*/, _height, TJPF_RGB, TJFLAG_ACCURATEDCT);

    tjDestroy(_jpegDecompressor);

    return imgBuf;
}
Deepak Kar
  • 21
  • 4
  • 1
    I don't understand your question. The title seems to be suggesting you want to somehow make a lossless JPEG then suddenly you start talking about PNG files and then present a lump of code with no explanation. Sorry - I don't get it. – Mark Setchell Nov 17 '18 at 21:03
  • @MarkSetchell, I am sorry for the confusion. I have updated my question. I am basically trying to use libjpeg-turbo for lossless compression/decompression of the image data that I have. – Deepak Kar Nov 20 '18 at 05:03
  • 2
    Not sure why you think it will be lossless? Regular JPEG is inherently lossy. – Mark Setchell Nov 20 '18 at 07:15

0 Answers0