0

我想用 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* 数据?

谢谢。

4

1 回答 1

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);
}
于 2012-05-29T01:37:53.967 回答