CImg库在旋转时创建扭曲的图像

时间:2014-01-18 10:06:30

标签: c++ image-rotation rgba qimage cimg

我想使用CImg库(http://cimg.sourceforge.net/)以任意角度旋转图像(图像由Qt读取,不应执行旋转):

QImage img("sample_with_alpha.png");
img = img.convertToFormat(QImage::Format_ARGB32);

float angle = 45;

cimg_library::CImg<uint8_t> src(img.bits(), img.width(), img.height(), 1, 4);
cimg_library::CImg<uint8_t> out = src.get_rotate(angle);

// Further processing:
// Data: out.data(), out.width(), out.height(), Stride: out.width() * 4

当“角度”设置为0时,“out.data()”中的最终数据正常。但是对于其他角度,输出数据会失真。我假设CImg库在旋转期间更改输出格式和/或步幅?

此致

1 个答案:

答案 0 :(得分:4)

CImg不以交错模式存储图像的像素缓冲区,如RGBARGBARGBA ...但是使用通道结构的通道结构RRRRRRRR ..... GGGGGGGGG ....... BBBBBBBBB .....AAAAAAAAA 。 我假设你的img.bits()指针指向具有交错通道的像素,因此如果你想将它传递给CImg,你需要置换缓冲区结构,然后才能应用任何CImg方法。 试试这个:

cimg_library::CImg<uint8_t> src(img.bits(), 4,img.width(), img.height(), 1);
src.permute_axes("yzcx");
cimg_library::CImg<uint8_t> out = src.get_rotate(angle);
// Here, the out image should be OK, try displaying it with out.display();
// But you still need to go back to an interleaved image pointer if you want to
// get it back in Qt.
out.permute_axes("cxyz");   // Do the inverse permutation.
const uint8_t *p_out = out.data();  // Interleaved result.

我想这应该按预期工作。