Stuti Rastogi
Stuti Rastogi

Reputation: 1180

jpeg_decompress_struct Struct size mismatch

I am trying to use libJPEG version 9b on Visual Studio Community 2017 to decode an encoded image buffer (I have this from openCV).

I have followed this example and have written my own function which calls jpeg_create_decompress with the pointer to jpeg_decompress_struct as an argument.

My function always exits at

if (structsize != SIZEOF(struct jpeg_decompress_struct))
    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
         (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);

in the jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize) method in jdapimin.c.

I have written different codes and just tried different examples available online and have run into the same issue.

Mat readFile = imread("ottawa.jpg", IMREAD_COLOR);
cout << "Read Matrix size: " << readFile.size() << endl;

vector<uchar> encodedBuffer;          // output buffer to store compressed image
vector<int> compression_params;

int jpegqual = ENCODE_QUALITY; // Compression Parameter
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(jpegqual);

imencode(".jpg", readFile, encodedBuffer, compression_params);       // compress the image

cout << "Encoding with OpenCV complete..." << endl;

int matSize = encodedBuffer.size();

cout << "Size of matrix: " << matSize << endl;

// Variables for the decompressor itself
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;

unsigned long jpg_size = encodedBuffer.size();
unsigned char *jpg_buffer = &encodedBuffer.front();

cinfo.err = jpeg_std_error(&jerr);
cout << endl << "In decode function:" << endl;
cout << "cinfo size: " << sizeof(cinfo) << endl;
cout << "jpeg_decompress_struct size: " << sizeof(struct jpeg_decompress_struct) << endl;
jpeg_create_decompress(&cinfo);

I am printing the sizes of the structs in both the caller (main) and the jpeg_CreateDecompress function.

In the jdapimin.c file:

printf("\nIn jdapimin.c:\n");
printf("Structsize: %zd\n", structsize);
printf("jpeg_decompress_struct: %zd\n", sizeof(struct jpeg_decompress_struct));

This is the output: Output

This is entirely my problem. I cannot understand how it is possible that the same sizeof function returns different values! Why is there a difference of 32? I have tried casting to size_t, removing the casting. I don't even have a remotest idea where this could be going wrong.

I am sorry for the long post, and would really appreciate any leads. Thanks!

EDIT: I checked this post, but I had compiled the library myself and linked the .lib in Visual Studio. So not sure, how to check if multiple libJPEGs are installed.

Upvotes: 2

Views: 1294

Answers (1)

Stuti Rastogi
Stuti Rastogi

Reputation: 1180

Problem solved:

I opened the jpeglib.h in both my files and checked the locations. The jpeglib.h file that was included in jdapimin.c and in my program was at different locations. There was a copy in my project directory which my program was using. I deleted those .h files, and both were now using the one in my include directory path. This solved the problem and all values were now 632.

Thanks to @LPs' suggestions in the comments.

Upvotes: 1

Related Questions