2011-01-12 5 views
3

libjpeg를 사용하여 일부 원시 데이터에서 jpeg 파일을 쓰려고합니다.libjpeg (seg 오류)로 jpeg 작성

그것은 여기 jpeg_start_compress()

에 분할 오류를 트리거 코드의 관련 부분입니다 :

void write_sub_image(char *filename, int start, int end) 
{ 
    struct jpeg_compress_struct cinfo; 
    unsigned char *stride; 
    JSAMPROW row_pointer[1]; 
    unsigned long new_width = end-start; 
    int i; 
    FILE *fp; 

    stride = (unsigned char *)malloc(new_width * 3); 

    fp = fopen(filename, "w+"); 

    jpeg_create_compress(&cinfo); 

    jpeg_stdio_dest(&cinfo, fp); 

    cinfo.image_width = new_width; 
    cinfo.image_height = height; 
    cinfo.input_components = 3; 
    cinfo.in_color_space = JCS_RGB; 

    jpeg_set_defaults(&cinfo); 

    jpeg_start_compress(&cinfo, FALSE); 

    for (i=0; i<height; i++) { 
     memcpy (stride, image + (start + i * width) * 3, new_width * 3); 
     row_pointer[0] = stride; 
     jpeg_write_scanlines(&cinfo, &stride, 1); 
    } 

    jpeg_finish_compress(&cinfo); 
    jpeg_destroy_compress(&cinfo); 

    fclose(fp); 
} 

문제는 방어 적이기하지, 그것은 심지어 for 루프에 도착하지 않습니다 .. _start_compress에서 충돌이 발생합니다.

관련성이있는 경우 시스템은 Ubuntu 10.10입니다.

+0

당신이 new_width''것을 확인하고 적이을'height'이 유효 (즉,> 0)? – OrangeDog

+1

실제로 'height'가 선언되지 않았으므로 전체 코드가 될 수는 없습니다. – OrangeDog

+0

외부에 선언되었지만이 코드는 더 큰 코드의 일부이지만 자체적으로 작동 할 수있는 조각이어야합니다. 나는 내 테스트에서 new_width = 96 and height = 200을 확인했다. – Matthieu

답변

4
오류 관리자 설정해야

:

struct jpeg_error_mgr jerr; 
.... 
cinfo.err = jpeg_std_error(&jerr); 
jpeg_set_defaults(&cinfo); 
.... 
+0

는 완벽하게 작동했습니다 ... 코드를 얻은 예에서 어떤 이유로 그것이 없었습니다. – Matthieu

관련 문제