0

I am getting NULL pointer exception in the function jpeg_read_header() in following code while the input image file is successfully read by FILE object.

#include "jpeglib.h"
#include <iostream>

using namespace std;



void decode_frame(char *filename)
{
    unsigned char* raw_image;
    JSAMPROW row_pointer[1];
    unsigned long location = 0;

    struct jpeg_error_mgr jerr;
    struct jpeg_decompress_struct cinfo ;

    FILE *infile = fopen(filename, "rb" );

    if (infile == NULL )
    {
        printf("Error opening jpeg file %s\n!", filename );
        return -1;
    }
    cinfo.err = jpeg_std_error(&jerr);

    /* create decompressor */
    jpeg_create_decompress(&cinfo);

    /* this makes the library read from infile */
    jpeg_stdio_src(&cinfo, infile );

    /* read jpeg header */
    jpeg_read_header(&cinfo, TRUE);

    /* decompress */
    jpeg_start_decompress(&cinfo);

    /*allocate memory */
    raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );

    /* now actually read the jpeg into the raw buffer */
    row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
    /* read scanlines */
    while (cinfo.output_scanline < cinfo.output_height) {
        jpeg_read_scanlines( &cinfo, row_pointer, 1 );
        for( int i=0; i < cinfo.image_width*cinfo.num_components;i++) 
            raw_image[location++] = row_pointer[0][i];
    }  

    /* clean up */
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    fclose( infile );
    free( row_pointer[0] );
}

int main(){
    char *f = "Example.jpg";
    decode_frame(f);
    return 0;
}

The value of cinfo alfter jpeg_stdio_src(&cinfo, infile ) function call is :

cinfo{err=0x0020f8fc mem=0x021cb320 progress=0x00000000 ...}
progress             0x00000000 {progress_monitor=??? pass_counter=??? pass_limit=??? ...}
client_data          0xcccccccc  void *
is_decompressor      1 
global_state         200    
src 0x021c4480       {next_input_byte=0x00000000 <Bad Ptr> bytes_in_buffer=0 init_source=0x686ccb50 ...}    
image_width          0  
image_height         0  
num_components       0
jpeg_color_space     JCS_UNKNOWN    
out_color_space      JCS_UNKNOWN
scale_num            0      
scale_denom          0      
output_gamma         0.00000000000000000    
buffered_image       0  
raw_data_out         0
dct_method           JDCT_ISLOW     

Where I am getting wrong? This is the first time I am using image library.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
cbinder
  • 2,388
  • 2
  • 21
  • 36

1 Answers1

0

The global state 200 means (according to libjpeg source code):

#define DSTATE_START 200 /* after create_decompress */

Calling jpeg_stdio_src has only for effect to prepare the decompressor structure, i.e it allocates the internal cinfo->src buffer and initialize other decompressor state.

In other words the JPEG file parsing has not yet started: so you have no problem here.

You need at least to execute jpeg_read_header to make sure the cinfo structure is filled with all metadata info (image_width, image_height, jpeg_color_space, etc). If something goes wrong at that step, this might be related to your JPEG file that might be broken (?).

Do you see something like Not a JPEG file printed on stderr?

deltheil
  • 15,496
  • 2
  • 44
  • 64
  • I am getting NULL pointer exception in jpeg_read_header() fn only... regarding jpeg file being broken , I have tried several jpeg but the result is same. – cbinder Nov 14 '13 at 05:25
  • What is your platform? With a few modifications (to build your sample as pure C code) it runs on Mac OS X. Have you seen [this](http://stackoverflow.com/questions/391917/jpeg-support-with-ijg-getting-access-violation)? – deltheil Nov 14 '13 at 09:20
  • Can you pls suggest the steps to build jpeg.lib on VS2005, for VS2010 , we can use this : nmake /f makefile.vc setup-v10 to generate .sln file, but I need to build it on VS2005, its an old project. – cbinder Nov 19 '13 at 06:02
  • I've no experience with libjpeg on VS2005. Anyway you should definitely open a new question (or find if it does not already exist) since this is a different topic. – deltheil Nov 19 '13 at 08:17