2

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.

Community
  • 1
  • 1
Stuti Rastogi
  • 1,162
  • 2
  • 16
  • 26
  • where did you assigned value to structsize? – Pras May 10 '17 at 06:11
  • I don't, it is done in jpeglib.h `#define jpeg_create_decompress(cinfo) \ jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \ (size_t) sizeof(struct jpeg_decompress_struct)) EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo, int version, size_t structsize)); EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo, int version, size_t structsize));` – Stuti Rastogi May 10 '17 at 06:13
  • But structsize comes equal in both cases at 664, so that is getting passed correctly I assume. `sizeof(struct jpeg_decompress_struct)` is giving different values. But at this point, I am not sure of anything that is happening! – Stuti Rastogi May 10 '17 at 06:14
  • `%zd` ---> `%zu` – LPs May 10 '17 at 06:18
  • @LPs thanks but did not work, still the same 664 and 632 output. – Stuti Rastogi May 10 '17 at 06:21
  • Are you sure that lib and application are compiled for the same arch? I mean x86 on both cases or x64 on both? If not struct can use different alignmenet: 4 instead of 8 and vice versa. – LPs May 10 '17 at 06:26
  • Yes I double checked that. Both are Release x64 configuration. Just now I checked whether my linking was to the correct x64 Release jpeg.lib and yes it was. – Stuti Rastogi May 10 '17 at 06:29
  • 1
    I saw some define that defines size of buffer in `jpeglib.h` Are you sure that `.h` pointed by lib project is the same with same numbers as the one used by the application project? – LPs May 10 '17 at 06:33
  • 1
    You are right! I opened the jpeglib.h included in both, and the locations are different. There was one more copy in my project directory itself. Now everything equals 632. Thank you so so much!! – Stuti Rastogi May 10 '17 at 06:42

1 Answers1

1

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.

Stuti Rastogi
  • 1,162
  • 2
  • 16
  • 26