2012-07-31 58 views
0

當我運行這段代碼加載一個JPEG文件我得到jpeg_read_scanlines 崩潰我使用Windows用VC++ 2010的libjpeg崩潰

我負載是一個100x75 JPG圖像的圖像7 64位。

如果您需要任何更多的細節只是問

崩潰的消息是:在0x012db29e 未處理異常LibTest.exe:0000005:訪問衝突寫入位置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被定義爲:

typedef struct { 
int width; 
int height; 
int bytesPerPixel; 
byte *pixels; 
} image_t; 

回答

3

不要這樣做。

buffer = (JSAMPARRAY)out+(row_stride*cinfo.output_scanline); // WRONG 

您鑄造JSAMPARRAY,這基本上是void **。結果是垃圾,因爲這不是你擁有的數據類型:你有一個字節數組。

jpeg_read_scanlines函數,如果您查看文檔,不會將指針指向緩衝區。它需要一個指向掃描線陣列的指針,每條掃描線都是指向行數據的指針。

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); 
} 

建議:添加投修復編譯錯誤只有當你知道演員是正確的工作。除非你知道類型是什麼,否則不要投射到任何類型。

+0

謝謝,這工作完美。 :) – 2012-07-31 03:00:28