Reputation: 113
When I run this code to load a jpeg file I get a crash in jpeg_read_scanlines I'm using windows 7 64 bit with VC++ 2010
The image I'm loading is a 100x75 jpg image.
If you need any more details just ask
The crash message is: Unhandled exception at 0x012db29e in LibTest.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.
void JPG_Load (const char *path, image_t *img)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
int infile;
JSAMPARRAY buffer;
int row_stride;
unsigned char *out;
infile = fopen(path,"rb");
if (infile == 0) {
memset (img, 0, sizeof(image_t));
return;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, (FILE *)infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
out = malloc(cinfo.output_width*cinfo.output_height*cinfo.output_components);
img->pixels = out;
img->width = cinfo.output_width;
img->height = cinfo.output_height;
img->bytesPerPixel = cinfo.out_color_components;
while (cinfo.output_scanline < cinfo.output_height) {
buffer = (JSAMPARRAY)out+(row_stride*cinfo.output_scanline);
jpeg_read_scanlines(&cinfo, buffer, 1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
}
image_t is defined as:
typedef struct {
int width;
int height;
int bytesPerPixel;
byte *pixels;
} image_t;
Upvotes: 0
Views: 2288
Reputation: 213338
Don't do this.
buffer = (JSAMPARRAY)out+(row_stride*cinfo.output_scanline); // WRONG
You are casting to JSAMPARRAY
, which is basically void **
. The result is garbage, since that's not the kind of data you have: you have an array of bytes.
The jpeg_read_scanlines
function, if you look at the documentation, does not take a pointer to your buffer. It takes a pointer to an array of scanlines, and each scanline is a pointer to row data.
while (cinfo.output_scanline < cinfo.output_height) {
unsigned char *rowp[1];
rowp[0] = (unsigned char *) out + row_stride * cinfo.output_scanline;
jpeg_read_scanlines(&cinfo, rowp, 1);
}
Recommendation: Adding a cast to fix a compiler error only works if you know the cast is correct. Don't cast to any type unless you know what the type is.
Upvotes: 3