Poilaupat
Poilaupat

Reputation: 83

System.AccessViolationException while compressing JPEG using jpeglib

I'm rather new with jpeglib. I encounter the following problem. I have to save a rgb raster to a jpeg file. The code :

typedef struct JpegErrorManager {
    struct jpeg_error_mgr pub;
    jmp_buf setjmp_buffer; //callback
} JpegErrorManager;

void JpegErrorExit(j_common_ptr cinfo)
{
    /* cinfo->err actually points to a jpegErrorManager struct */
    JpegErrorManager* myerr = (JpegErrorManager*)cinfo->err;

    /* Create the message */
    (*(cinfo->err->format_message)) (cinfo, jpegLastErrorMsg);

    /* Jump to the setjmp point */
    longjmp(myerr->setjmp_buffer, 1);
}

int Write24ByteRaster2JpegFile(const char *filefullpath, char* raster, int width, int heigth)
{
    struct jpeg_compress_struct info;
    //struct jpeg_error_mgr jerr;
    unsigned char *oneRowImgData;

    FILE* file = fopen(filefullpath, "wb");  
    if (!file) {
        SetError("Error when creating jpeg file");
        return SIL_ERROR;
    }

    //Overriding jpeglib standard behavior on errors by setting specific error handler
    JpegErrorManager jerr;
    info.err = jpeg_std_error(&jerr.pub);       
    jerr.pub.error_exit = JpegErrorExit;    
    if (setjmp(jerr.setjmp_buffer))         //Error "callback"
    {
        char errormsg[210];
        sprintf(errormsg, "JPEGLIB : %s", jpegLastErrorMsg);
        SetError(errormsg);

        jpeg_destroy_compress(&info);
        fclose(file);

        return SIL_ERROR;
    }

    // Compression parameters
    //info.err = jpeg_std_error(&jerr);  //> Uncomment to enable jpeglib std error behavior
    jpeg_stdio_dest(&info, file);
    info.image_width = width;
    info.image_height = heigth;
    info.input_components = 3;
    info.in_color_space = JCS_RGB;  
    jpeg_set_defaults(&info);   

    jpeg_start_compress(&info, TRUE);

    // Define an array, the representative picture of each row of data.
    int buffsz = width * info.input_components;
    oneRowImgData = (unsigned char*)malloc((size_t)buffsz);
    if (!oneRowImgData)
    {
        SetError("Memory allocation error while writing jpeg");
        return SIL_ERROR;
    }
    memset(oneRowImgData, 0, buffsz);

    JSAMPROW row_pointer[1];
    row_pointer[0] = oneRowImgData;
    for (int i = 0; i <info.image_height; ++i)
    {
        oneRowImgData[i] = i % 256; // Dummy image data
        jpeg_write_scanlines(&info, row_pointer, 1);
    }

    free(oneRowImgData);
    jpeg_finish_compress(&info);
    jpeg_destroy_compress(&info);

    fclose(file); 
    file = NULL;

    return SIL_SUCCESS;
}

The error System.AccessViolationException occurs at the call to jpeg_stdio_dest.

I checked : Is it my specific error handler that causes the problem ? It seems not to be the source. First because i use the same error handler in all my other functions (reading jpeg) an it works perfect, and second because i tried to set back the standard jpeglib error behavior and had the same error.

Right issue when writing file ? Il checked if i had the right to create / write file in the path specified in "filefullpath". I have.

I have no idea what to check next. Thanks for your help.

Upvotes: 0

Views: 115

Answers (1)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

Googling around, I found http://www.andrewewhite.net/wordpress/2008/09/02/very-simple-jpeg-writer-in-c-c/ with an example:

struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr       jerr;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);        // <-- first create compress structure
jpeg_stdio_dest(&cinfo, outfile);

In this example, the compress structure is created before assigning the output destination. Maybe this helps.

Upvotes: 1

Related Questions