Question

I am trying to test the performance between WIC and the encode function from QT. But I can not find a way to encode from a QImage (QT object) to PNG using WIC.

Encoder from WIC need to be initialized from IStream, which should provide same data as image file. But what I have is sort of bitmap.

Anyone can help my out? Any comment would be appreciated.

Was it helpful?

Solution

It can be easily done. I did not know how to use encoder. IWICBitmapFrameEncode.WritePixels is where I should put raw pixels:

factory = CreateComObject(CLSID_WICImagingFactory);

IWICBitmapEncoder pngEncoder = factory.CreateEncoder(GUID_ContainerFormatPng, null);
pngEncoder.Initialize(targetStream, WICBitmapEncoderNoCache);

IPropertyBag2 propertyBag;
IWICBitmapFrameEncode frame = pngEncoder.CreateNewFrame(ref propertyBag);

frame.Initialize(propertyBag);

//Set the size of the image
frame.SetSize(640, 480);

//Request the encoder use this format.
Guid format = GUID_WICPixelFormat32bppPBGRA;

//If the encoder doesn't support that format, it will set format to the closest it does support
frame.SetPixelFormat(ref format);

//if the frame encoder doesn't support the pixel format we want to give it
//then we just cannot proceed
Assert(format == GUID_WICPixelFormat32bppPBGRA); 

frame.WritePixels(
     lineCount, 
     stride, 
     bufferSize, 
     pointerToPixelData);

frame.Commit();
pngEncoder.Commit();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top