IWICColorTransform::Initialize 方法 (wincodec.h)
使用 IWICBitmapSource 初始化 IWICColorTransform,并将其从一个 IWICColorContext 转换为另一个 IWICColorContext。
语法
HRESULT Initialize(
[in] IWICBitmapSource *pIBitmapSource,
[in] IWICColorContext *pIContextSource,
[in] IWICColorContext *pIContextDest,
[in] REFWICPixelFormatGUID pixelFmtDest
);
参数
[in] pIBitmapSource
类型: IWICBitmapSource*
用于初始化颜色转换的位图源。
[in] pIContextSource
类型: IWICColorContext*
颜色上下文源。
[in] pIContextDest
类型: IWICColorContext*
颜色上下文目标。
[in] pixelFmtDest
类型: REFWICPixelFormatGUID
所需像素格式的 GUID。
此参数仅限于本机 WIC 像素格式的子集,请参阅列表的备注。
返回值
类型: HRESULT
如果该方法成功,则返回 S_OK。 否则,将返回 HRESULT 错误代码。
注解
pIContextSource 和 pixelFmtDest 参数当前支持的格式为:
- GUID_WICPixelFormat8bppGray
- GUID_WICPixelFormat16bppGray
- GUID_WICPixelFormat16bppBGR555
- GUID_WICPixelFormat16bppBGR565
- GUID_WICPixelFormat24bppBGR
- GUID_WICPixelFormat24bppRGB
- GUID_WICPixelFormat32bppBGR
- GUID_WICPixelFormat32bppBGRA
- GUID_WICPixelFormat32bppPBGRA
- GUID_WICPixelFormat32bppPRGBA (Windows 8 及更高版本)
- GUID_WICPixelFormat32bppRGBA
- GUID_WICPixelFormat32bppBGR101010
- GUID_WICPixelFormat32bppCMYK
- GUID_WICPixelFormat48bppBGR
- GUID_WICPixelFormat64bppBGRA (Windows 8 及更高版本)
- GUID_WICPixelFormat64bppPBGRA (Windows 8 及更高版本)
- GUID_WICPixelFormat64bppPRGBA (Windows 8 及更高版本)
- GUID_WICPixelFormat64bppRGBA (Windows 8 及更高版本)
示例
以下示例执行从一个 IWICColorContext 到另一个 IWICColorContext 的颜色转换。
IWICImagingFactory *pFactory = NULL;
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pBitmapFrame = NULL;
IWICColorContext *pContextSrc = NULL;
IWICColorContext *pContextDst = NULL;
IWICColorTransform *pColorTransform = NULL;
UINT uiFrameCount = 0;
HRESULT hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL, CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*) &pFactory);
if (SUCCEEDED(hr))
{
hr = pFactory->CreateDecoderFromFilename(
L"test.jpg",
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnDemand,
&pDecoder);
}
if (SUCCEEDED(hr))
{
hr = pDecoder->GetFrameCount(&uiFrameCount);
}
if (SUCCEEDED(hr) && (uiFrameCount > 0))
{
hr = pDecoder->GetFrame(0, &piBitmapFrame);
}
if (SUCCEEDED(hr))
{
hr = pFactory->CreateColorContext(&pContextSrc);
}
if (SUCCEEDED(hr))
{
hr = pContextSrc->InitializeFromFilename(
L"c:\\windows\\system32\\spool\\drivers\\color\\kodak_dc.icm");
}
if (SUCCEEDED(hr))
{
hr = pFactory->CreateColorContext(&pContextDst);
}
if (SUCCEEDED(hr))
{
hr = pContextDst->InitializeFromFilename(
L"c:\\windows\\system32\\spool\\drivers\\color\\sRGB Color Space Profile.icm");
}
hr = E_FAIL;
if (SUCCEEDED(hr))
{
// Transform from src icm to the destination icm.
hr = pColorTransform->Initialize( pBitmapFrame,
pContextSrc,
pContextDst,
GUID_WICPixelFormat32bppBGRA);
}
if (SUCCEEDED(hr))
{
UINT uiWidth = 0, uiHeight = 0;
UINT cbStride = 0;
UINT cbBufferSize = 0;
BYTE *pbBuffer = NULL;
hr = pColorTransform->GetSize(&uiWidth, &uiHeight);
if (SUCCEEDED(hr))
{
WICRect rc = { 0 };
rc.X = 0;
rc.Y = 0;
rc.Width = uiWidth;
rc.Height = 1; // scanline
for (UINT i = 0; SUCCEEDED(hr) && (i < uiHeight); i++)
{
hr = pColorTransform->CopyPixels(&rc, cbStride, cbBufferSize - (rc.Y * cbStride), pbBuffer);
pbBuffer += cbStride;
rc.Y += 1;
}
}
}
if (pFactory)
{
pFactory->Release();
}
if (pDecoder)
{
pDecoder->Release();
}
if (pBitmapFrame)
{
pBitmapFrame->Release();
}
if (pContextSrc)
{
pContextSrc->Release();
}
if (pContextDst)
{
pContextDst->Release();
}
if (pColorTransform)
{
pColorTransform->Release();
}
return hr;
要求
最低受支持的客户端 | Windows XP SP2、Windows Vista [桌面应用 |UWP 应用] |
最低受支持的服务器 | Windows Server 2008 [桌面应用 | UWP 应用] |
目标平台 | Windows |
标头 | wincodec.h |
Library | Windowscodecs.lib |
DLL | Windowscodecs.dll |