逐像素旋转和翻转图像

Rotate and Flip an image pixel by pixel

本文关键字:翻转 图像 旋转 像素      更新时间:2023-10-16

我有下图,我想旋转和翻转它,使其适合全屏分辨率。我正在进行以下转换,以旋转,但它不起作用。

这是源图像。

size_t bpp = Ogre::PixelUtil::getNumElemBytes(source.getFormat());
        const unsigned char *srcData = source.getData();
        unsigned char *dstData = new unsigned char[width * height * bpp];

        size_t srcPitch = source.getRowSpan();
        size_t dstPitch = width * bpp;
        ImageDescriptor sourceImage(source.getWidth(), source.getHeight(), bpp);
        ImageDescriptor rotatedTarget(source.getHeight(), source.getWidth(), bpp);  // note width/height swap
        unsigned char *rotated = new unsigned char[source.getHeight() * source.getWidth() * bpp];
        for (std::size_t row = 0; row < rotatedTarget.mHeight; ++row) {
            for (std::size_t col = 0; col < rotatedTarget.mWidth; ++col) {
                for (std::size_t chan = 0; chan < rotatedTarget.mChannels; ++chan) {
                    rotated[rotatedTarget.offset(col, row, chan)] =
                    srcData[sourceImage.offset(row, col, chan)];
                }
            }
        }


struct ImageDescriptor {
        std::size_t mWidth;
        std::size_t mHeight;
        std::size_t mChannels;
        ImageDescriptor(std::size_t width, std::size_t height, std::size_t channels)
        {
            mWidth = width;
            mHeight = height;
            mChannels = channels;
        }
        std::size_t stride() const { return mWidth * mChannels; }
        const std::size_t offset(std::size_t row, std::size_t col, std::size_t chan) {
            assert(0 <= row && row < mHeight);
            assert(0 <= col && col < mWidth);
            assert(0 <= chan && chan < mChannels);
           // return row*stride() + col*mChannels + chan;
            // or, depending on your coordinate system ...
             return (mHeight - row - 1)*stride() + col*mChannels + chan;
        }
        std::size_t size() const { return mHeight * stride(); }
    };

这是结果图像。

答案结果。

有什么想法吗?

很简单,反转行,更改

rotated[rotatedTarget.offset(col, row, chan)] =
    srcData[sourceImage.offset(row, col, chan)];

rotated[rotatedTarget.offset(rotatedTarget.mWidth-col-1, row, chan)] =
    srcData[sourceImage.offset(row, col, chan)];

反正就是这样。我对你的代码有点困惑。