码迷,mamicode.com
首页 > 其他好文 > 详细

使用WIC组件转换图片格式

时间:2014-05-30 13:18:18      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:c   style   code   a   color   int   

#include <windows.h>
#include <Wincodec.h>

#pragma comment(lib, "Windowscodecs.lib" )

HRESULT PNG2WDP(WCHAR* szPngFileName, WCHAR* szWdpFileName)
{
    IWICImagingFactory *piFactory = NULL;

    IWICBitmapEncoder *piEncoder = NULL;
    IWICBitmapDecoder *piDecoder = NULL;

    IWICBitmapFrameEncode *piBitmapFrame = NULL;
    IWICBitmapFrameDecode *piBitmapFrameIn = NULL;

    IPropertyBag2 *pPropertybag = NULL;

    IWICStream *piStream = NULL;
    IWICStream *piStreamIn = NULL;

    UINT uiWidth = 0;
    UINT uiHeight = 0;
   
    ULONG counter = 0;

    CoInitialize(NULL);

    HRESULT hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_IWICImagingFactory,
        (LPVOID*) &piFactory);

    if (SUCCEEDED(hr))
    {
        hr = piFactory->CreateStream(&piStream);
    }

    if (SUCCEEDED(hr))
    {
        hr = piStream->InitializeFromFilename(szWdpFileName, GENERIC_WRITE);
    }

    if (SUCCEEDED(hr))
    {
        hr = piFactory->CreateStream(&piStreamIn);
    }

    if (SUCCEEDED(hr))
    {
        hr = piStreamIn->InitializeFromFilename(szPngFileName, GENERIC_READ);
    }

    if (SUCCEEDED(hr))
    {
        hr = piFactory->CreateEncoder(GUID_ContainerFormatWmp, NULL, &piEncoder);
    }

    if (SUCCEEDED(hr))
    {
        hr = piEncoder->Initialize(piStream, WICBitmapEncoderNoCache);
    }

    if (SUCCEEDED(hr))
    {
        hr = piEncoder->CreateNewFrame(&piBitmapFrame, &pPropertybag);
    }

    if(SUCCEEDED(hr))
    {
        hr = piFactory->CreateDecoder(GUID_ContainerFormatPng, NULL, &piDecoder);
    }

    if (SUCCEEDED(hr))
    {
        hr = piDecoder->Initialize(piStreamIn, WICDecodeMetadataCacheOnDemand);
    }

    if(SUCCEEDED(hr))
    {
        hr = piDecoder->GetFrame(0, &piBitmapFrameIn);
    }

    if (SUCCEEDED(hr))
    {       
        // This is how you customize the TIFF output.
       
PROPBAG2 option = { 0 };
        //option.pstrName = L"TiffCompressionMethod";
        //VARIANT varValue;   
        //VariantInit(&varValue);
        //varValue.vt = VT_UI1;
        //varValue.bVal = WICTiffCompressionZIP;     
        //hr = pPropertybag->Write(1, &option, &varValue);       
       
if (SUCCEEDED(hr))
        {
            hr = piBitmapFrame->Initialize(pPropertybag);
        }
    }

    if (SUCCEEDED(hr))
    {
        hr = piBitmapFrameIn->GetSize(&uiWidth, &uiHeight);
        hr = piBitmapFrame->SetSize(uiWidth, uiHeight);
    }

    WICPixelFormatGUID formatGUID = GUID_WICPixelFormat32bppBGRA;
    if (SUCCEEDED(hr))
    {
        hr = piBitmapFrameIn->GetPixelFormat(&formatGUID);
        hr = piBitmapFrame->SetPixelFormat(&formatGUID);
    }

    if (SUCCEEDED(hr))
    {
        // We‘re expecting to write out 24bppRGB. Fail if the encoder cannot do it.
       
hr = IsEqualGUID(formatGUID, GUID_WICPixelFormat32bppBGRA) ? S_OK : E_FAIL;
        if( FAILED(hr) )
        {
            hr = IsEqualGUID(formatGUID, GUID_WICPixelFormat24bppBGR) ? S_OK : E_FAIL;
        }
    }

    if (SUCCEEDED(hr))
    {           
        {
            IWICBitmap *pIBitmap = NULL;
            IWICBitmapLock *pILock = NULL;
           
            WICRect rcLock = { 0, 0, uiWidth, uiHeight };

            // Create the bitmap from the image frame.
           
if (SUCCEEDED(hr))
            {
                hr = piFactory->CreateBitmapFromSource(
                    piBitmapFrameIn,          // Create a bitmap from the image frame
                   
WICBitmapCacheOnDemand,  // Cache metadata when needed
                   
&pIBitmap);              // Pointer to the bitmap
               
hr = pIBitmap->Lock(&rcLock, WICBitmapLockWrite, &pILock);
               
                BYTE *pv = NULL;
                UINT cbStride = 0;
                UINT cbBufferSize = 0;

                // Retrieve a pointer to the pixel data.
               
if (SUCCEEDED(hr))
                {
                    hr = pILock->GetDataPointer(&cbBufferSize, &pv);
                }
                cbStride = cbBufferSize / uiHeight;
                hr = piBitmapFrame->WritePixels(uiHeight, cbStride, cbBufferSize, pv);

                counter = pILock->Release();
                counter = pIBitmap->Release();

                pv = NULL;
            }           
        }
    //    else
       
{
    //        hr = E_OUTOFMEMORY;
       
}
    }

    if (SUCCEEDED(hr))
    {
        hr = piBitmapFrame->Commit();
    }   

    if (SUCCEEDED(hr))
    {
        hr = piEncoder->Commit();
    }

    if (piFactory)
        counter = piFactory->Release();

    if (piBitmapFrame)
        counter = piBitmapFrame->Release();

    if(piBitmapFrameIn)
        counter = piBitmapFrameIn->Release();
   
    if(piDecoder)
        counter = piDecoder->Release();

    if (piEncoder)
        counter = piEncoder->Release();

    if (piStream)
        counter = piStream->Release();

    if(piStreamIn)
        counter = piStreamIn->Release();

    if( pPropertybag )
        counter = pPropertybag->Release();

    CoUninitialize();
    return hr;
}

使用WIC组件转换图片格式,布布扣,bubuko.com

使用WIC组件转换图片格式

标签:c   style   code   a   color   int   

原文地址:http://www.cnblogs.com/LinuxHunter/p/3756165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!