【发布时间】:2017-12-05 08:50:26
【问题描述】:
我正在尝试使用 WIC 将图像加载到内存缓冲区中以进行进一步处理,然后在完成后将其写回文件。具体来说:
- 将图像加载到 IWICBitmapFrameDecode 中。
- 加载的 IWICBitmapFrameDecode 报告其像素格式为 GUID_WICPixelFormat24bppBGR。我想使用 32bpp RGBA,所以我打电话给WICConvertBitmapSource。
- 在转换后的帧上调用CopyPixels以获取内存缓冲区。
- 使用 WritePixels 将内存缓冲区写回 IWICBitmapFrameEncode。
这会生成可识别的图像,但生成的图像大部分是蓝色的,就好像红色通道被解释为蓝色一样。
如果我调用WriteSource 直接写入转换后的帧,而不是写入内存缓冲区,它可以工作。如果我从原始未转换的帧中调用 CopyPixels(并相应地更新我的步幅和像素格式),它就可以工作。只是 WICConvertBitmapSource 的组合加上内存缓冲区(CopyPixels + WritePixels)的使用导致了问题,但我无法弄清楚我做错了什么。
这是我的代码。
int main() {
IWICImagingFactory *pFactory;
IWICBitmapDecoder *pDecoder = NULL;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*)&pFactory
);
// Load the image.
pFactory->CreateDecoderFromFilename(L"input.png", NULL, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &pDecoder);
IWICBitmapFrameDecode *pFrame = NULL;
pDecoder->GetFrame(0, &pFrame);
// pFrame->GetPixelFormat shows that the image is 24bpp BGR.
// Convert to 32bpp RGBA for easier processing.
IWICBitmapSource *pConvertedFrame = NULL;
WICConvertBitmapSource(GUID_WICPixelFormat32bppRGBA, pFrame, &pConvertedFrame);
// Copy the 32bpp RGBA image to a buffer for further processing.
UINT width, height;
pConvertedFrame->GetSize(&width, &height);
const unsigned bytesPerPixel = 4;
const unsigned stride = width * bytesPerPixel;
const unsigned bitmapSize = width * height * bytesPerPixel;
BYTE *buffer = new BYTE[bitmapSize];
pConvertedFrame->CopyPixels(nullptr, stride, bitmapSize, buffer);
// Insert image buffer processing here. (Not currently implemented.)
// Create an encoder to turn the buffer back into an image file.
IWICBitmapEncoder *pEncoder = NULL;
pFactory->CreateEncoder(GUID_ContainerFormatPng, nullptr, &pEncoder);
IStream *pStream = NULL;
SHCreateStreamOnFileEx(L"output.png", STGM_WRITE | STGM_CREATE, FILE_ATTRIBUTE_NORMAL, true, NULL, &pStream);
pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
IWICBitmapFrameEncode *pFrameEncode = NULL;
pEncoder->CreateNewFrame(&pFrameEncode, NULL);
pFrameEncode->Initialize(NULL);
WICPixelFormatGUID pixelFormat = GUID_WICPixelFormat32bppRGBA;
pFrameEncode->SetPixelFormat(&pixelFormat);
pFrameEncode->SetSize(width, height);
pFrameEncode->WritePixels(height, stride, bitmapSize, buffer);
pFrameEncode->Commit();
pEncoder->Commit();
pStream->Commit(STGC_DEFAULT);
return 0;
}
【问题讨论】:
-
首先,确保您检查了每个返回它的函数的
HRESULT。您可以使用SUCCEEED或FAILED宏来做到这一点,或者您可以使用ThrowIfFailed 之类的东西。 -
您在这里对运行时像素格式做了很多假设,而您没有检查。一个很好的例子是查看WICTextureLoader 和ScreenGrab。有关使用 WIC 的更多示例代码,请查看 DirectXTex。
标签: c++ image image-processing wic