CImg 库在旋转时创建失真的图像

CImg library creates distorted images on rotation

本文关键字:失真 真的 图像 创建 旋转 CImg      更新时间:2023-10-16

我想使用 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 库在旋转过程中更改了输出格式和/或步幅?

问候

CImg 不会以交错模式存储图像的像素缓冲区,因为 RGBARGBARGBA...但使用逐通道结构RRRRRRRRRR.....嘎��嘭��啊我假设您的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.

我想这应该按预期工作。