0

I want to convert a UINT16 monochrome image to a 8 bits image, in C++.

I have that image in a

char *buffer;

I'd like to give the new converted buffer to a QImage (Qt).

I'm trying with freeImagePlus

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

loadfromMemory needs a fipMemoryIO adress:

loadfromMemory(fipMemoryIO &memIO, int flag = 0)

So I do

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";

Then I would do something like

fimage.saveToMemory(...

or

fimage.saveToHandle(...

I don't understand what is a FREE_IMAGE_FORMAT, which is the first argument to any of those two functions. I can't find information of those types in the freeImage documentation.

Then I'd finish with

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

How can I fill 'buf' with the content of the initial buffer?

And get the data from the fipImage to a uchar* data for a QImage?

Thanks.

Mark A.
  • 193
  • 1
  • 3
  • 14

1 Answers1

0

The conversion is simple to do in plain old C++, no need for external libraries unless they are significantly faster and you care about such a speedup. Below is how I'd do the conversion, at least as a first cut. The data is converted inside of the input buffer, since the output is smaller than the input.

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);
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313