Wince PNGマップクラス

20384 ワード

Winceの下でPNGマップクラスをサポートして、仕事のプロジェクトは必要で、winceの応用開発の中でPNGピクチャーを使う必要があって、主に半透明の効果をします.これは昨年初めに書いたもので、最近はWinceでAndroid/iPhoneのようなLauncherを実現するブログをいくつか書く予定で、主にスライド、アイコン交換、アイコン階層分類を実現する機能です.PNGマップを使用する必要があります.これは初期のバージョンで、復号とAlphaの混合はマイクロソフトのIImageライブラリを使用しています.
Alphaブレンドは主にマイクロソフトのAlphaBlendを使用しています(...)インタフェース、後期に私は自分で書いたAlpha混合アルゴリズムを使うブログを書きます.マイクロソフトのAlphaBlendの速度は本当に高くないので、自分で書いたAlphaアルゴリズムを使うと速いです.
必要な友达に参考にして、头のファイルのインタフェースはすべて详しい注釈を书いて、类のファイルの実现も必要な注釈を书いて、もし问题があれば伝言することができて、みんなは少し讨论します.
/*************************************************
2012, Apical. Co., Ltd.
Edited by OWL
Class name:    CPngBitBlt    

Description:    
  PNG  

*************************************************/

#pragma once

#include "./CtrlConfig.h"

class CPngBitBlt
{
public:
    CPngBitBlt(void);
    ~CPngBitBlt(void);


public:

    /*
    Function:  PNG  
    filename:png    
    hbit:   png    
    */
    BOOL LoadPngImage(LPCTSTR filename,HBITMAP * hbit);

    /*
    Function: png    DC  
    pDC:  dc
    pSrDC:  dc
    dRc:      
    sRc:     
    Alpha:   (0~255)
    */
    void BiltPNG(CDC* pDC,CDC* pSrDC,CRect dRc,CRect sRc,int Alpha);

    /*
    Function: png    DC  
    pDC:  dc
    pSrDC:  dc
    X:   X  
    Y:  Y  
    width:      
    Heigth:      
    Alpha:   (0~255)
    */
    void BiltPNG(CDC* pDC, CDC* pSrDC, int X, int Y, int Width, int Height, int Alpha);

    /**********************************************************************   
       :  DrawImage   
      :                DC     
      :   
    [in]hdc:                DC 
    [in]iconPath:                 
    [in]rect                     
       :     
      :      bmp、jpg、png、gif        ,IImage         
    **********************************************************************/    
    void DrawImage2DC(HDC hdc,LPCWSTR iconPath,RECT rect);
};

 
 
#include "StdAfx.h"
#include "PngBitBlt.h"

#include <imaging.h>
#include <initguid.h>
#include <imgguids.h>
#include <wingdi.h>

#pragma comment(lib,"Imaging.lib")
#pragma comment(lib,"uuid.lib")

CPngBitBlt::CPngBitBlt(void)
{
}

CPngBitBlt::~CPngBitBlt(void)
{
}


BOOL CPngBitBlt::LoadPngImage(LPCTSTR filename,HBITMAP * hbit)   
{   
    IImagingFactory* pImageFactory = 0;   
    IImage* pImage = 0;   
    ImageInfo imageInfo;   
    CoInitializeEx(0, COINIT_MULTITHREADED);   
    HBITMAP hBitmap = 0;   
    LPBYTE lpByte;   
  //COM
if (SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory, 0, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void**)&pImageFactory))) { if (SUCCEEDED(pImageFactory->CreateImageFromFile(filename, &pImage))&& SUCCEEDED(pImage->GetImageInfo(&imageInfo))) { //HDC bmpDC = CreateCompatibleDC(hdc); //LPBYTE lpByte; BITMAPINFO *pbinfo ; pbinfo = (BITMAPINFO *)calloc(1, sizeof(BITMAPINFO) + 4 * sizeof(INT)) ; if(!pbinfo) return FALSE ; pbinfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER); pbinfo->bmiHeader.biWidth = imageInfo.Width ; pbinfo->bmiHeader.biHeight = imageInfo.Height ; pbinfo->bmiHeader.biPlanes = 1; pbinfo->bmiHeader.biBitCount = 32; pbinfo->bmiHeader.biCompression = BI_ALPHABITFIELDS; pbinfo->bmiHeader.biSizeImage = 0 ; pbinfo->bmiHeader.biXPelsPerMeter = 11811; pbinfo->bmiHeader.biYPelsPerMeter = 11811; pbinfo->bmiHeader.biClrUsed = 0; pbinfo->bmiHeader.biClrImportant = 0; int *pMask = (int*)&(pbinfo->bmiColors[0]) ; *pMask++ = 0x00FF0000 ; *pMask++ = 0x0000FF00 ; *pMask++ = 0x000000FF ; *pMask++ = 0xFF000000 ; hBitmap = CreateDIBSection(NULL, pbinfo, DIB_RGB_COLORS, (void **)&lpByte, NULL, 0) ; free(pbinfo) ; if(!hBitmap || !lpByte) return FALSE ; RECT rect = {0, 0, imageInfo.Width, imageInfo.Height}; IBitmapImage *pBitmapImage; BitmapData bitmapData; bitmapData.Width = imageInfo.Width; bitmapData.Height = imageInfo.Height; bitmapData.PixelFormat = imageInfo.PixelFormat; pBitmapImage = NULL; pImageFactory->CreateBitmapFromImage(pImage, imageInfo.Width, imageInfo.Height, PIXFMT_32BPP_ARGB, InterpolationHintDefault, &pBitmapImage); pBitmapImage->LockBits(&rect, ImageLockModeRead,PIXFMT_32BPP_ARGB, &bitmapData); //transferring the pixels memcpy(lpByte, bitmapData.Scan0, imageInfo.Width * imageInfo.Height * 4); pBitmapImage->UnlockBits(&bitmapData); pBitmapImage->Release(); pImage->Release(); // DeleteDC(bmpDC); } pImageFactory->Release(); } CoUninitialize(); //ProcessThePixelsWithAlphaChannel Here // vertical flip and ProcessThePixelsWithAlphaChannel here for (UINT y=0; y<imageInfo.Height/2; y++) { BYTE * pPixel = (BYTE *) lpByte + imageInfo.Width * 4 * y; BYTE * pDstPixel = (BYTE*) lpByte + imageInfo.Width * 4 * (imageInfo.Height-y-1); for (UINT x=0; x<imageInfo.Width; x++) { pPixel[0] = pPixel[0] * pPixel[3] / 255; pPixel[1] = pPixel[1] * pPixel[3] / 255; pPixel[2] = pPixel[2] * pPixel[3] / 255; pDstPixel[0] = pDstPixel[0] * pDstPixel[3] / 255; pDstPixel[1] = pDstPixel[1] * pDstPixel[3] / 255; pDstPixel[2] = pDstPixel[2] * pDstPixel[3] / 255; INT* pOrigin = (INT*)pPixel; INT* pDst = (INT*)pDstPixel; INT temp = *pOrigin; *pOrigin = *pDst; *pDst = temp; pPixel += 4; pDstPixel += 4; } } *hbit= hBitmap; if (!hbit) { return FALSE; } return TRUE; } void CPngBitBlt::BiltPNG(CDC* pDC,CDC* pSrDC,CRect dRc,CRect sRc,int Alpha) { BLENDFUNCTION blendFunction = {0}; blendFunction.AlphaFormat = AC_SRC_ALPHA; blendFunction.BlendFlags = 0; blendFunction.BlendOp = AC_SRC_OVER; blendFunction.SourceConstantAlpha = Alpha; AlphaBlend(pDC->m_hDC,dRc.left, dRc.top, dRc.Width(), dRc.Height(),pSrDC->m_hDC,sRc.left,sRc.top, sRc.Width(), sRc.Height(),blendFunction); } void CPngBitBlt::BiltPNG(CDC* pDC, CDC* pSrDC, int X, int Y, int Width, int Height, int Alpha) { CRect dRc = CRect(X, Y, X+Width, Y+Height); CRect sRc = CRect(0, 0, Width, Height);
  // BLENDFUNCTION , Alpha BLENDFUNCTION blendFunction
= {0}; blendFunction.AlphaFormat = AC_SRC_ALPHA; blendFunction.BlendFlags = 0; blendFunction.BlendOp = AC_SRC_OVER; blendFunction.SourceConstantAlpha = Alpha; AlphaBlend(pDC->m_hDC,dRc.left, dRc.top, dRc.Width(), dRc.Height(),pSrDC->m_hDC,sRc.left,sRc.top, sRc.Width(), sRc.Height(),blendFunction); } void CPngBitBlt::DrawImage2DC(HDC hdc,LPCWSTR iconPath,RECT rect) { IImage * m_pImage=NULL; IImagingFactory * m_pImagingFactory=NULL; HRESULT hr; // COM if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED))) { goto END; } // COM if(FAILED(hr = CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void**) &m_pImagingFactory))) { goto END; } // if(FAILED(hr = m_pImagingFactory->CreateImageFromFile(iconPath, &m_pImage))) { goto END; } // if(FAILED(hr = m_pImage->Draw(hdc,&rect,NULL))) { goto END; } END: // if(m_pImage != NULL) { m_pImage->Release(); m_pImage = NULL; } if(m_pImagingFactory != NULL) { m_pImagingFactory->Release(); m_pImagingFactory = NULL; } CoUninitialize(); }

必要な方は参考にして、質問があれば伝言してもいいです.
友達を転載する必要があります、原文の住所を添付してください:http://www.cnblogs.com/mythou/archive/2013/06/13/3133606.html