2011-07-19 42 views
2

目前我正在使用glreadpixels()捕獲屏幕。捕獲的圖像通常是鏡像圖像,因此我將圖像翻轉回正常。 現在我想旋轉捕獲的數據(圖像)90'degree。 任何想法該怎麼做?如何使用圖像的像素數據按90度旋轉圖像?

I M使用來捕獲畫面數據的代碼是:

CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

int backingWidth = screenBounds.size.width; 
int backingHeight =screenBounds.size.height; 

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth); 
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight); 


NSInteger myDataLength = backingWidth * backingHeight * 4; 
GLuint *buffer; 
if((buffer= (GLuint *) malloc(myDataLength)) == NULL) 
    NSLog(@"error initializing the buffer"); 
glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 
// code for flipping back (mirroring the image data)  
for(int y = 0; y < backingHeight/2; y++) { 
    for(int xt = 0; xt < backingWidth; xt++) { 
     GLuint top = buffer[y * backingWidth + xt]; 
     GLuint bottom = buffer[(backingHeight - 1 - y) * backingWidth + xt]; 
     buffer[(backingHeight - 1 - y) * backingWidth + xt] = top; 
     buffer[y * backingWidth + xt] = bottom; 
    } 
} 

不知道如何通過旋轉在90'degree緩衝器捕獲的數據? 感謝

回答

2
size_t at (size_t x, size_t y, size_t width) 
{ 
    return y*width + x; 
} 

void rotate_90_degrees_clockwise (
    const pixel * in, 
    size_t in_width, 
    size_t in_height, 
    pixel * out) 
{ 
    for (size_t x = 0; x < in_width; ++x) { 
     for (size_t y = 0; y < in_height; ++i) 
      out [at (in_height-y, in_width-x, in_height)] 
       = in [at (x, y, in_width)]; 
    } 
} 

有時候,沒有什麼比用鉛筆和紙一分鐘:-)

這可以優化,如果你保持x_in和Y_IN與X_OUT和Y_OUT - 一個遞增和遞減的其他 - 並在循環之間緩存x,但這是基本思想。

+0

嘿,我想這代碼,但它並不工作正常。所獲得的圖像是一個扭曲的一個..我也製作這些幀的視頻,在每個循環中分配另一個緩衝區將導致性能問題,我也得到這個特殊代碼的例外 – Tornado

+0

a)它是如何失真?嘗試一下,例如3 * 5 int數組。 b)你不需要爲每一幀新的緩衝區,你可以重新使用舊的。 c)上面的代碼中沒有任何內容可以拋出,其他錯誤也會出錯。 – spraff

+0

K現在我正在使用舊的同一緩衝區,並刪除了異常。現在唯一剩下的就是扭曲的圖像。嘿,我不知道如何檢查您的代碼3 * 5 int數組...因爲我的圖像數據存儲在GLuint類型的緩衝區,我認爲代表一個平面陣列..新的OpenGl的東西 – Tornado

2

k終於我想出了整個事情。對於其他人誰不想在這裏做同樣是90度,180度,270度RESP從像素數據的圖像旋轉代碼: -

// Rotate 90 
// height and width specifies corresponding height and width of image    
    for(int h = 0, dest_col = height - 1; h < height; ++h, --dest_col) 
     { 
     for(int w = 0; w < width; w++) 
      { 
       dest[ (w * height) + dest_col] = source[h*width + w]; 
      } 
     } 




     // Rotate 180 

     for(int h=0, dest_row=(height-1); h < height; --dest_row, ++h) 
      { 
      for (int w=0, dest_col=(width-1); w < width; ++w, --dest_col) 
      { 
      dest[ dest_row * width + dest_col] = source[h*width + w]; 
      } 
      } 



    // Rotate 270 

     for(int h = 0, dest_col=0; h < height; ++dest_col, ++h) 
      { 
      for(int w=0, dest_row=width-1; w < width; --dest_row, ++w) 
      { 
       dest[(dest_row * height) + dest_col] = source[ h * width + w]; 
      } 
      }