이 작업을 수행하지 마십시오
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);
}
권장 사항 : 당신이 캐스트가 정확한지 알고있는 경우 컴파일러 오류를 수정하기 위해 캐스트를 추가하면에만 작동합니다. 유형이 무엇인지 모르는 경우 유형에 캐스트하지 마십시오.
감사합니다. 완벽하게 작동했습니다. :) –