问题描述
我想使用CImg库( http://cimg.sourceforge.net/)旋转具有任意角度的图像(该图像由Qt读取,该图像不应执行旋转):
I want to use the CImg library (http://cimg.sourceforge.net/) to rotate an image with an arbitrary angle (the image is read by Qt which should not perform the rotation):
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库在旋转过程中会更改输出格式和/或跨步?
The final data in "out.data()" is ok when the the angle is set to 0. But for other angles the output data is distorted. I assume that the CImg library changes the output format and/or stride during rotation?
此致
推荐答案
CImg不以RGBARGBARGBA的形式以交错模式存储图像的像素缓冲区,而是使用逐通道结构的RRRRRRRR ..... GGGGGGGGG ....... BBBBBBBBB ..... AAAAAAAAA.我假设您的img.bits()
指针指向具有交错通道的像素,所以如果要将其传递给CImg,则需要先置换缓冲区结构,然后才能应用任何CImg方法.试试这个:
CImg does not store the pixel buffer of an image in interleaved mode, as RGBARGBARGBA... but uses a channel by channel structure RRRRRRRR.....GGGGGGGGG.......BBBBBBBBB.....AAAAAAAAA.I assume your img.bits()
pointer points to pixels with interleaved channels, so if you want to pass this to CImg, you'll need to permute the buffer structure before you can apply any of the CImg method.Try this :
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.
我想这应该可以正常工作.
I guess this should work as expected.
这篇关于CImg库在旋转时创建扭曲的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!