I have a image as a BYTE array(in RGB format) and I needed to encode it. I did, but colors have changed.
/*Create stream and initialized*/
hr = piFactory->CreateStream(&piStream);
hr = piStream->InitializeFromFilename(L"..\\test.jpg",GENERIC_WRITE);
/*created an encoder. I want to save JPG*/
hr = piFactory->CreateEncoder(GUID_ContainerFormatJpeg, NULL, &piEncoder);
did some manipulations, also tried to set pixel format.
hr = piBitmapFrame->SetPixelFormat(&formatGUID);
but it always set to "GUID_WICPixelFormat24bppBGR"
and i write image data as here.
/*pBitmap contains image data*/
hr = piBitmapFrame->WritePixels(lHeight, cbStride, cbBufferSize, pBitmap);
the problem was colors of the image has changed, and I found that if i change RGB to BGR then the output looks fine. so i did this
for(int i = 0; i < DataSize; i += 3)
{
inBuff[i] = pBitmap[i+2];
inBuff[i + 1] = pBitmap[i+1];
inBuff[i + 2] = pBitmap[i];
}
pBitmap = inBuff
But i dont want to expend more time here looping through whole image. I need to tell WIC "treat the data as RGB(or BGR)".
is that possible? if it is then how?
You can create a bitmap first. at that time you could say the format it needs to be. Here is "24bppRGB" format:
hr = piFactory->CreateBitmapFromMemory(
lWidth,
lHeight,
GUID_WICPixelFormat24bppRGB,
cbStride,
cbBufferSize,
pBitmap,
&piBitmapSrc
);
This is how You write image data into the frame:
hr = piBitmapFrame->WritePixels(lHeight, cbStride, cbBufferSize, pBitmap);
Instead write image to the frame like this (because WritePixels()
doesn't accept bitmap):
hr = piBitmapFrame->WriteSource(
piBitmapSrc,
NULL
);
And that help to avoid the loop. I'm not sure what happens inside the CreateBitmapFromMemory()
though. Since you are concerned with time, I don't know if this will help (it still makes your code better).