旋转图像不起作用

Rotating image not working

本文关键字:不起作用 图像 旋转      更新时间:2023-10-16

我正在尝试使用openFrameworks旋转图像,但我遇到了问题。我的旋转图像是红色而不是其原始颜色。

void testApp::setup(){
image.loadImage("abe2.jpg");
rotatedImage.allocate(image.width, image.height, OF_IMAGE_COLOR);
imageCenterX = image.getWidth() / 2;
imageCenterY = image.getHeight() / 2;
w = image.getWidth();
h = image.getHeight();
int degrees = 180;
float radians = (degrees*(PI / 180));
for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
        int index = image.getPixelsRef().getPixelIndex(x, y);
        int newX = (cos(radians) * (x - imageCenterX) - sin(radians) * (y - imageCenterY) + imageCenterX);
        int newY = (sin(radians) * (x - imageCenterX) + cos(radians) * (y - imageCenterY) + imageCenterY);
        int newIndex = rotatedImage.getPixelsRef().getPixelIndex(newX, newY);
        rotatedImage.getPixelsRef()[newIndex] = image.getPixelsRef()[index];
    }
}
rotatedImage.update();
}
 void testApp::update(){
 }
void testApp::draw(){
image.draw(0,0);
rotatedImage.draw(0,400);
}

有人可以告诉我我做错了什么吗?

如果图像具有三种颜色分量(红色、绿色、蓝色),则需要转换所有三种颜色分量。以下应该可以解决问题:

rotatedImage.getPixelsRef()[newIndex] = image.getPixelsRef()[index];
rotatedImage.getPixelsRef()[newIndex+1] = image.getPixelsRef()[index+1];
rotatedImage.getPixelsRef()[newIndex+2] = image.getPixelsRef()[index+2];