首页 技术 正文
技术 2022年11月20日
0 收藏 366 点赞 4,760 浏览 5047 个字

#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;
}

bool IsRGBA(const std::wstring& strFileName)
{
    unsigned char buf[4] = {0};
    std::fstream fs(strFileName, std::ios::in | std::ios::binary);
    fs.seekg(4, SEEK_SET);
    fs.read((char*)&buf, 4);
    int pos = (buf[3] << 24) + (buf[2] << 16) + (buf[1] << 8) + buf[0];
    while( pos > 0 )
    {
        fs.seekg(pos, SEEK_SET);
        fs.read((char*)&buf, 2);
        int numEntry = (buf[1] << 8) + buf[0];
        fs.seekg(12 * numEntry, SEEK_CUR);
        fs.read((char*)&buf, 4);
        pos = (buf[3] << 24) + (buf[2] << 16) + (buf[1] << 8) + buf[0];
    }
    fs.seekg(15, SEEK_CUR);
    fs.read((char*)&buf, 1);
    if( buf[0] == 0x0F )
        return true;
    else
        return false;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918