2

現在、glreadpixels()を使用して画面をキャプチャしています。キャプチャされた画像は一般的に鏡像であるため、画像を通常の状態に戻しました。次に、キャプチャしたデータ(画像)を90度回転させます。それを行う方法はありますか?

画面データをキャプチャするために使用するコードは次のとおりです。

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度回転させる方法はありますか?ありがとう

4

2 に答える 2

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

時々、鉛筆と紙で1分に勝るものはありません:-)

これは、x_inとy_inをx_outとy_outに対して維持し、一方をインクリメントしてもう一方をデクリメントし、ループの間にxをキャッシュすることで最適化できますが、これが基本的な考え方です。

于 2011-07-19T10:40:14.240 に答える
2

kついに私はすべてを理解しました。ここで同じことをしたい他の人のために、ピクセルデータから画像を90度、180度、270度それぞれ回転させるためのコードがあります:-

// 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];
    }
}
于 2011-07-22T05:24:12.497 に答える