2011-06-20 96 views
2

以下代碼從framebuffer(readpixels,openGL庫的函數之後)創建一個jpeg。從幀緩衝區創建的JPEG旋轉180度

問題是創建的jpeg(test.jpeg)向左旋轉180度。

你能告訴我錯誤在哪裏嗎?

void CTaksiOGL::GetFrameFullSize(CVideoFrame& frame) 
{ 

    // Gets current frame to be stored in video file. 
    // select back buffer 
    s_glReadBuffer(GL_BACK); 

    // read the pixels data 
    s_glReadPixels(0, 0, frame.m_Size.cx, frame.m_Size.cy, GL_RGB, GL_UNSIGNED_BYTE, frame.m_pPixels); 



    // Adjust pixel encodings: RGB -> BGR 

    SwapColorComponents(frame.m_pPixels, frame.m_Size.cx, frame.m_Size.cy); 



    FILE* outfile = fopen("C:\\testgrab\\test.jpeg", "wb"); 

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



    GLubyte* flip = new GLubyte[sizeof(GLubyte)*frame.m_Size.cx*frame.m_Size.cy*3]; 



    cinfo.image_width  = frame.m_Size.cx; 
    cinfo.image_height  = frame.m_Size.cy; 
    cinfo.input_components = 3; 
    cinfo.in_color_space = JCS_RGB; 

    int width=frame.m_Size.cx; 
    int height=frame.m_Size.cy; 

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

    JSAMPROW row_pointer;   /* pointer to a single row */ 

    // OpenGL writes from bottom to top. 
     // libjpeg goes from top to bottom. 
     // flip lines. 



    for (int x = 0; x < width; x++) { 
     for (int y = 0; y < height; y++) { 
       flip[(y*height+x)*3] = frame.m_pPixels[((width-1-y)*height+x)*3]; 
       flip[(y*height+x)*3+1] = frame.m_pPixels[((width-1-y)*height+x)*3+1]; 
       flip[(y*height+x)*3+2] = frame.m_pPixels[((width-1-y)*height+x)*3+2]; 
      } 
     } 


    while (cinfo.next_scanline < cinfo.image_height) { 
     row_pointer = (JSAMPROW) &frame.m_pPixels[cinfo.next_scanline*width*3]; 
     jpeg_write_scanlines(&cinfo, &row_pointer, 1); 
    } 

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

    fclose(outfile); 


} 
+0

你應該修正你的indendation,這樣我們可以更好地幫助你 –

+2

你的意思是90度(四分之一回合)?沒有任何東西是「向左180度」,180度與上下顛倒圖像相同,並且無論您將圖像向左還是向右旋轉,結果都是相同的。 – Lindydancer

+0

向左180度? \ *咯咯的笑容* –

回答

2

聽起來有一個座標系變化:也許當你使用的字節複製從flipframe你必須扭轉y座標順序。

4

有一個bug:flip[(y*height+x)*3] - >索引原始2d數組的規範方式是y*with+x。沒有這個修復,你遲早會遇到緩衝區溢出。單個「y」不是「高度」像素寬,而是「寬度」像素。當然,對於作業的右側也是如此。

除了:你確定圖像旋轉?那麼,如果你不僅鏡像y,而且還鏡像x,那麼這個問題就會被固定下來(當然在前面提到的固定之後)。

如果這些公式混淆你,則逐步導出座標,例如,

const int x0 = x, 
      x1 = (width-1) - x; 
... 
target [x0 + y0*width] = source [x1 + y1*width]; 
2

glReadPixels從幀緩衝器返回的像素數據,從它的左下角是在位置(x,y)的像素,到客戶端存儲器中,起始位置的數據。顯示時,大多數框架(我知道Qt是這樣做的),以其他方式處理 - (0,0)是左上角。

因此,也許您在顯示(或轉換)圖像時沒有考慮到這一點。