2012-02-23 4 views
3

을 libjpeg 사용하여, 플립 나는 왜 그런지 몰라?!이미지는 JPEG에 OpenGL을 출력</p> <p>코드는 완벽하게 작동하지만 최종 이미지가 뒤집혀 ... 아래 코드는 나를 libjpg을 사용하여 JPEG 이미지에 OpenGL을 출력을 변환하는 데 도움지만 결과 이미지가 수직 뒤집어

unsigned char *pdata = new unsigned char[width*height*3]; 
    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pdata); 

    FILE *outfile; 
    if ((outfile = fopen("sample.jpeg", "wb")) == NULL) { 
     printf("can't open %s"); 
     exit(1); 
     } 

    struct jpeg_compress_struct cinfo; 
    struct jpeg_error_mgr  jerr; 

    cinfo.err = jpeg_std_error(&jerr); 
    jpeg_create_compress(&cinfo); 
    jpeg_stdio_dest(&cinfo, outfile); 

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

    jpeg_set_defaults(&cinfo); 
    /*set the quality [0..100] */ 
    jpeg_set_quality (&cinfo, 100, true); 
    jpeg_start_compress(&cinfo, true); 

    JSAMPROW row_pointer; 
    int row_stride = width * 3; 

    while (cinfo.next_scanline < cinfo.image_height) { 
    row_pointer = (JSAMPROW) &pdata[cinfo.next_scanline*row_stride]; 
    jpeg_write_scanlines(&cinfo, &row_pointer, 1); 
    } 

    jpeg_finish_compress(&cinfo); 

    fclose(outfile); 

    jpeg_destroy_compress(&cinfo); 

답변

5

OpenGL의 좌표계의 원점은 이미지의 왼쪽 하단에 있습니다. LIBJPEG는 이미지의 원점이 이미지의 왼쪽 상단에 있다고 가정합니다. 코드를 수정하려면 다음과 같이 변경하십시오.

while (cinfo.next_scanline < cinfo.image_height) 
{ 
    row_pointer = (JSAMPROW) &pdata[(cinfo.image_height-1-cinfo.next_scanline)*row_stride]; 
    jpeg_write_scanlines(&cinfo, &row_pointer, 1); 
}