⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alpha.c

📁 brew平台alpha透明算法的实现
💻 C
字号:
/*===========================================================================

FILE: alpha.c
===========================================================================*/


/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "AEEModGen.h"          // Module interface definitions
#include "AEEAppGen.h"          // Applet interface definitions
#include "AEEShell.h"           // Shell interface definitions
#include "alpha.brh"
#include "alpha.bid"
#include "AEEStdlib.h"

#include "snapscreen.h"

#include "alpha.h"

/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static  boolean alpha_HandleEvent(alpha* pMe, AEEEvent eCode, 
                                             uint16 wParam, uint32 dwParam);
boolean alpha_InitAppData(alpha* pMe);
void    alpha_FreeAppData(alpha* pMe);
void AlphaBMPBitBlt(
	IDisplay *pDisplay,
	uint16 xDest, 
	uint16 yDest, 
	uint16 cxDest, 
	uint16 cyDest,
	IBitmap *pSrcBmp,
	uint16 xSrc, 
	uint16 ySrc,
	uint8 alphaVal
);
void AlphaImageBitBlt(
	IDisplay *pDisplay,
	uint16 xDest, 
	uint16 yDest, 
	uint16 cxDest, 
	uint16 cyDest,
	IImage *pSrcImg,
	uint16 xSrc, 
	uint16 ySrc,
	uint8 alphaVal
);
__inline void GetRBG(RGBVAL rgb, uint32* r, uint32* g, uint32* b);

/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
//测试成功
void TestBitBlt(
	IDisplay *pDisplay,
	uint16 xDest, 
	uint16 yDest, 
	uint16 cxDest, 
	uint16 cyDest,
	IBitmap *pSrcBmp,
	uint16 xSrc, 
	uint16 ySrc,
	uint8 alphaVal)
{
	IBitmap *pDevBmp;

	uint32 nResult;
	nResult = IDISPLAY_GetDeviceBitmap(pDisplay, &pDevBmp );
	if (nResult != SUCCESS) {
		return;
	}
	IBITMAP_BltIn(pDevBmp, xDest,yDest, cxDest,cyDest, pSrcBmp, 
		xSrc,ySrc, AEE_RO_COPY);
	IBITMAP_Release(pDevBmp);
}
/*===========================================================================
FUNCTION: AEEClsCreateInstance
===========================================================================*/
int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)
{
	*ppObj = NULL;

	if( ClsId == AEECLSID_ALPHA )
	{
		// Create the applet and make room for the applet structure
		if( AEEApplet_New(sizeof(alpha),
                          ClsId,
                          pIShell,
                          po,
                          (IApplet**)ppObj,
                          (AEEHANDLER)alpha_HandleEvent,
                          (PFNFREEAPPDATA)alpha_FreeAppData) ) // the FreeAppData function is called after sending EVT_APP_STOP to the HandleEvent function
                          
		{
			//Initialize applet data, this is called before sending EVT_APP_START
            // to the HandleEvent function
			if(alpha_InitAppData((alpha*)*ppObj))
			{
				//Data initialized successfully
				return(AEE_SUCCESS);
			}
			else
			{
				//Release the applet. This will free the memory allocated for the applet when
				// AEEApplet_New was called.
				IAPPLET_Release((IApplet*)*ppObj);
				return EFAILED;
			}

        } // end AEEApplet_New

    }

	return(EFAILED);
}


/*===========================================================================
FUNCTION SampleAppWizard_HandleEvent
===========================================================================*/
static boolean alpha_HandleEvent(alpha* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{  
	AEERect rc;
	IBitmap *pBmp = NULL;
	IImage  *pImg = NULL;
    switch (eCode) 
	{
        // App is told it is starting up
        case EVT_APP_START:                        
//		    // Add your code here...
//			SETAEERECT(&rc, 0, 0, 176, 220);
//            
//            pImg = ISHELL_LoadResImage(pMe->pIShell, ALPHA_RES_FILE, IDI_BG);
//            if (pImg) {
//                IIMAGE_Draw(pImg,0,0);
//                IIMAGE_Release(pImg);
//            }else{
//                DBGPRINTF("Fail Load IImage");
//            }
//			
//			
//			pBmp = ISHELL_LoadResBitmap(pMe->pIShell, ALPHA_RES_FILE, IDI_BMP);
//			AlphaBMPBitBlt(pMe->pIDisplay, 144,144, 48, 48, pBmp, 0,0, 10);	
//			AlphaBMPBitBlt(pMe->pIDisplay,  96, 96, 48, 48, pBmp, 0,0, 20);
//			AlphaBMPBitBlt(pMe->pIDisplay,  48, 48, 48, 48, pBmp, 0,0, 40);
//			AlphaBMPBitBlt(pMe->pIDisplay,   0,  0, 48, 48, pBmp, 0,0, 80);
//
//			IBITMAP_Release(pBmp);
/*		
			TestBitBlt(pMe->pIDisplay,   0,  0, 48, 48, pBmp, 0,0, 50);
			TestBitBlt(pMe->pIDisplay,  48, 48, 48, 48, pBmp, 20,20, 30);
			TestBitBlt(pMe->pIDisplay,  96, 96, 48, 48, pBmp, 30,30, 20);
			TestBitBlt(pMe->pIDisplay, 144,144, 48, 48, pBmp, 0,0, 50);
*/	
//            pImg = ISHELL_LoadResImage(pMe->pIShell, ALPHA_RES_FILE, IDI_JPG);
//            if (pImg) {
//                AlphaImageBitBlt(pMe->pIDisplay,   0,144, 48, 48, pImg, 0,0, 80);
//                AlphaImageBitBlt(pMe->pIDisplay,  48, 96, 48, 48, pImg, 0,0, 40);
//                
//                AlphaImageBitBlt(pMe->pIDisplay,  96, 48, 48, 48, pImg, 0,0, 80);
//                SETAEERECT(&rc, 96, 48, 48, 48);
//                IDISPLAY_SetColor(pMe->pIDisplay, CLR_USER_TEXT, 0xFFFFFF00);
//                IDISPLAY_DrawText(pMe->pIDisplay, AEE_FONT_NORMAL,
//                    L"杭州!", -1, rc.x, rc.y, &rc, IDF_TEXT_TRANSPARENT);
//                AlphaImageBitBlt(pMe->pIDisplay, 144,  0, 48, 48, pImg, 0,0, 10);
//            }else{
//                DBGPRINTF("Fail Load JPG");
//            }
//            
//
//			IDISPLAY_Update(pMe->pIDisplay);
            return(TRUE);


        // App is told it is exiting
        case EVT_APP_STOP:
            // Add your code here...

      		return(TRUE);


        // App is being suspended 
        case EVT_APP_SUSPEND:
		    // Add your code here...

      		return(TRUE);


        // App is being resumed
        case EVT_APP_RESUME:
		    // Add your code here...

      		return(TRUE);


        // An SMS message has arrived for this app. Message is in the dwParam above as (char *)
        // sender simply uses this format "//BREW:ClassId:Message", example //BREW:0x00000001:Hello World
        case EVT_APP_MESSAGE:
		    // Add your code here...

      		return(TRUE);

        // A key was pressed. Look at the wParam above to see which key was pressed. The key
            // codes are in AEEVCodes.h. Example "AVK_1" means that the "1" key was pressed.
        case EVT_KEY:
            // Add your code here...
            if (wParam == AVK_STAR) {
                //SaveScreen((AEEApplet*)pMe);
            }
            return(TRUE);


        // If nothing fits up to this point then we'll just break out
        default:
            break;
   }

   return FALSE;
}


// this function is called when your application is starting up
boolean alpha_InitAppData(alpha* pMe)
{
    // Get the device information for this handset.
    // Reference all the data by looking at the pMe->DeviceInfo structure
    // Check the API reference guide for all the handy device info you can get
    pMe->DeviceInfo.wStructSize = sizeof(pMe->DeviceInfo);
    ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&pMe->DeviceInfo);

    // The display and shell interfaces are always created by
    // default, so we'll asign them so that you can access
    // them via the standard "pMe->" without the "a."
    pMe->pIDisplay = pMe->a.m_pIDisplay;
    pMe->pIShell   = pMe->a.m_pIShell;

    // Insert your code here for initializing or allocating resources...


    // if there have been no failures up to this point then return success
    return TRUE;
}

// this function is called when your application is exiting
void alpha_FreeAppData(alpha* pMe)
{
    // insert your code here for freeing any resources you have allocated...

    // example to use for releasing each interface:
    // if ( pMe->pIMenuCtl != NULL )         // check for NULL first
    // {
    //    IMENUCTL_Release(pMe->pIMenuCtl)   // release the interface
    //    pMe->pIMenuCtl = NULL;             // set to NULL so no problems trying to free later
    // }
    //

}
__inline void GetRBG(RGBVAL rgb, uint32* pr, uint32* pg, uint32* pb)
{
	rgb >>= 8;
	*pr = rgb & 0xFF;
	rgb >>= 8;
	*pg = rgb & 0xFF;
	rgb >>= 8;
	*pb = rgb & 0xFF;
}

void AlphaBMPBitBlt(
	IDisplay *pDisplay,
	uint16 xDest, 
	uint16 yDest, 
	uint16 cxDest, 
	uint16 cyDest,
	IBitmap *pSrcBmp,
	uint16 xSrc, 
	uint16 ySrc,
	uint8 alphaVal
	)
{
	uint32 nResult;
	int w, h;
	IBitmap *pDevBmp;
	IBitmap *pCmpBmp;
	AEEBitmapInfo bif;
	AEERect rc;       //剪切举行

	//校验
	if(NULL == pDisplay || NULL == pSrcBmp) {
		return;
	}
	IBITMAP_GetInfo(pSrcBmp, &bif, sizeof(AEEBitmapInfo));
	if (bif.cx<xSrc || bif.cy<ySrc) {
		return;
	}
	IDISPLAY_GetClipRect(pDisplay, &rc);
	if (rc.dx<xDest || rc.dy<yDest) {
		return;
	}
	
	//应画区域,最小重叠区域
	w = MIN(MIN((int16)cxDest, rc.dx - (int16)cxDest), (int16)(bif.cx - xSrc));
	h = MIN(MIN((int16)cyDest, rc.dy - (int16)cyDest), (int16)(bif.cy - ySrc));

	//创建兼容位图,
	nResult = IDISPLAY_GetDeviceBitmap(pDisplay, &pDevBmp );
	if (nResult != SUCCESS) {
		return;
	}
	nResult = IBITMAP_CreateCompatibleBitmap(pDevBmp,&pCmpBmp, (uint16)w, (uint16)h);
	if (SUCCESS != nResult) {
		IBITMAP_Release(pDevBmp);
		return;
	}
	//画在0,0
	nResult = IBITMAP_BltIn(pCmpBmp, 0,0, w, h, pSrcBmp, xSrc, ySrc, AEE_RO_COPY);
	//开始算法
	if (SUCCESS == nResult) {
		int16  x,y;
		uint32 rBG,gBG,bBG;    //背景RGB
		uint32 rSrc,gSrc,bSrc; //源像素RGB 
		NativeColor ncBG,ncSrc, ncDest;  //像素NativeColor
		RGBVAL cDest; //像素的RGBCOLOR
		
		for (x=0; x<w; x++) {
			for (y=0; y<h; y++) {
				//从设备位图到内存位图
				nResult = IBITMAP_GetPixel(pDevBmp, x+xDest, y+yDest, &ncBG);
				GetRBG(IBITMAP_NativeToRGB(pDevBmp,ncBG), &rBG, &gBG,&bBG);
				nResult = IBITMAP_GetPixel(pCmpBmp, x, y, &ncSrc);
				GetRBG(IBITMAP_NativeToRGB(pCmpBmp,ncSrc), &rSrc, &gSrc,&bSrc);
				cDest = MAKE_RGB(
					(rSrc * alphaVal + rBG * (100 - alphaVal) ) / 100,
					(gSrc * alphaVal + gBG * (100 - alphaVal) ) / 100,
					(bSrc * alphaVal + bBG * (100 - alphaVal) ) / 100
					);
				/*test { */
				GetRBG(cDest, &rBG, &gBG,&bBG);
				/* } test */
				ncDest = IBITMAP_RGBToNative(pCmpBmp, cDest);
				IBITMAP_DrawPixel(pCmpBmp, x, y, ncDest,AEE_RO_COPY);
			}
		}
		IDISPLAY_BitBlt(pDisplay, xDest, yDest, w, h, pCmpBmp, 0, 0, AEE_RO_COPY);
	}
	IBITMAP_Release(pDevBmp);
	IBITMAP_Release(pCmpBmp);
}

//
void AlphaImageBitBlt(
	IDisplay *pDisplay,
	uint16 xDest, 
	uint16 yDest, 
	uint16 cxDest, 
	uint16 cyDest,
	IImage *pSrcImg,
	uint16 xSrc, 
	uint16 ySrc,
	uint8 alphaVal
	)
{
	uint32 nResult;
	int w, h;
	IBitmap *pDevBmp;
	IBitmap *pCmpBmp;
	AEEImageInfo bif;
	AEERect rc;       //剪切举行

	//校验
	if(NULL == pDisplay || NULL == pSrcImg) {
		return;
	}
	IIMAGE_GetInfo(pSrcImg, &bif);
	if (bif.cx<xSrc || bif.cy<ySrc) {
		return;
	}
	IDISPLAY_GetClipRect(pDisplay, &rc);
	if (rc.dx<xDest || rc.dy<yDest) {
		return;
	}
	
	//应画区域,最小重叠区域
	w = MIN(MIN((int16)cxDest, rc.dx - (int16)cxDest), (int16)(bif.cx - xSrc));
	h = MIN(MIN((int16)cyDest, rc.dy - (int16)cyDest), (int16)(bif.cy - ySrc));

	//创建兼容位图,
	nResult = IDISPLAY_GetDeviceBitmap(pDisplay, &pDevBmp );
	if (nResult != SUCCESS) {
		return;
	}
	nResult = IBITMAP_CreateCompatibleBitmap(pDevBmp,&pCmpBmp, bif.cx, bif.cy);
	if (SUCCESS != nResult) {
		IBITMAP_Release(pDevBmp);
		return;
	}
	//画在兼容BMP上
	nResult = IDISPLAY_SetDestination(pDisplay, pCmpBmp);
	IIMAGE_Draw(pSrcImg, 0, 0);
	nResult = IDISPLAY_SetDestination(pDisplay, NULL);
	//开始算法
	if (SUCCESS == nResult) {
		int16  x,y;
		uint32 rBG,gBG,bBG;    //背景RGB
		uint32 rSrc,gSrc,bSrc; //源像素RGB 
		NativeColor ncBG,ncSrc, ncDest;  //像素NativeColor
		RGBVAL cDest; //像素的RGBCOLOR
		
		for (x=0; x<w; x++) {
			for (y=0; y<h; y++) {
				//从设备位图到内存位图
				nResult = IBITMAP_GetPixel(pDevBmp, x+xDest, y+yDest, &ncBG);
                if (AEE_SUCCESS != nResult) {
                    goto error1;
                }
				GetRBG(IBITMAP_NativeToRGB(pDevBmp,ncBG), &rBG, &gBG,&bBG);
				nResult = IBITMAP_GetPixel(pCmpBmp, x+xSrc, y+ySrc, &ncSrc);
                if (AEE_SUCCESS != nResult) {
                    goto error1;
                }
				GetRBG(IBITMAP_NativeToRGB(pCmpBmp,ncSrc), &rSrc, &gSrc,&bSrc);
				cDest = MAKE_RGB(
					(rSrc * alphaVal + rBG * (100 - alphaVal) ) / 100,
					(gSrc * alphaVal + gBG * (100 - alphaVal) ) / 100,
					(bSrc * alphaVal + bBG * (100 - alphaVal) ) / 100
					);
				/*test { */
				GetRBG(cDest, &rBG, &gBG,&bBG);
				/* } test */
				ncDest = IBITMAP_RGBToNative(pCmpBmp, cDest);
				IBITMAP_DrawPixel(pCmpBmp, x+xSrc, y+ySrc, ncDest,AEE_RO_COPY);
			}
		}
		IDISPLAY_BitBlt(pDisplay, xDest, yDest, w, h, pCmpBmp, xSrc, xSrc, AEE_RO_COPY);
	}
    
error1:
	IBITMAP_Release(pDevBmp);
	IBITMAP_Release(pCmpBmp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -