2012-07-31 2 views
0

나는 내가 7 64 비트 VC++ 2010libjpeg 충돌

이미지 I로드는 100x75 JPG 이미지입니다 해요로 윈도우를 사용하고 내가 jpeg_read_scanlines 을에서 충돌을 얻을 JPEG 파일을로드하려면이 코드를 실행하면 . 당신이 더 이상 정보가 필요한 경우

물었

충돌 메시지는 다음과 같습니다 LibTest.exe에서 0x012db29e에서 처리되지 않은 예외 :가 0xc0000005 : 액세스 위반 쓰기 위치 0xcdcdcdcd은.

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

답변

3

이 작업을 수행하지 마십시오

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는 다음과 같이 정의된다.

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

당신은 기본적으로 void **JSAMPARRAY에 캐스팅된다. 결과는 가비지입니다. 데이터의 종류가 아닙니다. 즉, 바이트 배열을가집니다.

설명서를 보면 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

감사합니다. 완벽하게 작동했습니다. :) –