使用freeImage将UINT16单色图像改为8bit单色Qimage

时间:2012-04-28 16:22:28

标签: image image-processing qt4 freeimage

我想在C ++中将 UINT16单色图像转换为 8位图像

我在

中有这个图像
char *buffer;

我想将新转换的缓冲区提供给QImage(Qt)。

我正在尝试使用 freeImagePlus

fipImage fimage;
if (fimage.loadfromMemory(...) == false)
    //error

loadfromMemory需要一个fipMemoryIO地址:

  

loadfromMemory(fipMemoryIO& memIO,int flag = 0)

所以我做了

fipImage fimage;
BYTE *buf = (BYTE*)malloc(gimage.GetBufferLength() * sizeof(BYTE));
// 'buf' is empty, I have to fill it with 'buffer' content
// how can I do it?
fipMemoryIO memIO(buf, gimage.GetBufferLength());

fimage.loadFromMemory(memIO);
if (fimage.convertTo8Bits() == true)
    cout << "Good";

然后我会做类似

的事情
fimage.saveToMemory(...

fimage.saveToHandle(...

我不明白什么是FREE_IMAGE_FORMAT,这是这两个函数中的任何一个的第一个参数。我在freeImage文档中找不到这些类型的信息。

然后我结束了

imageQt = new QImage(destiny, dimX, dimY, QImage::Format_Indexed8);

如何用初始缓冲区的内容填充'buf'?

将fipImage中的数据转换为QImage的uchar *数据?

感谢。

1 个答案:

答案 0 :(得分:0)

在普通的旧C ++中转换很简单,不需要外部库,除非它们明显更快你关心这样的加速。以下是我如何进行转换,至少是第一次切换。由于输出小于输入,因此数据在输入缓冲区内转换。

QImage from16Bit(void * buffer, int width, int height) {
   int size = width*height*2; // length of data in buffer, in bytes
   quint8 * output = reinterpret_cast<quint8*>(buffer);
   const quint16 * input = reinterpret_cast<const quint16*>(buffer);
   if (!size) return QImage;
   do {
      *output++ = *input++ >> 8;
   } while (size -= 2);
   return QImage(output, width, height, QImage::Format_Indexed8);
}
相关问题