pic.c
上传用户:ghostdhl
上传日期:2010-03-30
资源大小:69k
文件大小:18k
- /*===========================================================================
- FILE: PicApp.c
- SERVICES: Reference User Application
- GENERAL DESCRIPTION: User App Main Control Program
- INITIALIZATION AND SEQUENCING REQUIREMENTS:
- BREW must be initialized.
- (c) COPYRIGHT 2003,2004 Hisense Incorporated.
- All Rights Reserved.
- Hisense Proprietary
- ===========================================================================*/
- /*===============================================================================
- $DateTime: 2003/12/19 16:00:00 $
- $Author: whb $
- $Change: 000001 $
- EDIT HISTORY FOR FILE
- This section contains comments describing changes made to the module.
- Notice that changes are listed in reverse chronological order.
- when who what, where, why
- ---------- --- ---------------------------------------------------------
- 12/19/2003 whb Initial Version
- =============================================================================== */
- /*===============================================================================
- INCLUDES DEFINITIONS
- =============================================================================== */
- #include "AEEModGen.h"
- #include "AEEAppGen.h"
- #include "AEEShell.h"
- #include "AEEStdlib.h"
- #include "pic.bid" //save app id
- #include "AEENet.h"
- #include "AEEFile.h"
- #include "string.h"
- //#include "PicApp_res.h" //necessary resource
- //...
- /*===============================================================================
- Macro definitions
- =============================================================================== */
- #define GLASS_WIDTH 60
- #define GLASS_HEIGHT 60
- #define GLASS_COLOR 0x07FF
- #define RGB565TORGB555(rgb565)
- (rgb565 & 0x001f) |
- ((rgb565 & 0x07E0) >>1) |
- ((rgb565 & 0xf800) >> 1)
- /*===============================================================================
- structure definitions
- =============================================================================== */
- typedef struct _PicApp_type
- {
- AEEApplet a;
- AEERect m_nClntAreaRect;
- AEERect m_nSoftkeyAreaRect;
- uint16 m_nScrWidth;
- uint16 m_nScrHeight;
- uint16 m_nLineHeight; //Normal font
- uint16 m_nLargeLineHeight; //Larege font
- uint16 m_x;
- uint16 m_y;
- }CPicApp;
- typedef unsigned char U8; /* unsigned 8 bit integer */
- typedef unsigned short U16; /* unsigned 16 bit integer */
- typedef unsigned long U32; /* unsigned 32 bit integer */
- typedef signed char S8; /* signed 8 bit integer */
- typedef signed short S16; /* signed 16 bit integer */
- typedef signed long S32; /* signed 32 bit integer */
- #pragma pack(push)
- #pragma pack(1)
- typedef struct tagBITMAPFILEHEADER_
- {
- U32 bfSize;
- U32 bfRsv;
- U32 bfOffBits;
- }BitmapFileHeader_Type;
- #pragma pack(pop)
- #pragma pack(push)
- #pragma pack(1)
- typedef struct tagBITMAPINFOHEADER_
- {
- U32 biSize;
- U32 biWidth;
- U32 biHeight;
- U16 biPlanes;
- U16 biBitCount;
- U32 biCompression;
- U32 biSizeImage;
- U32 biXPelsPerMeter;
- U32 biYPelsPerMeter;
- U32 biClrUsed;
- U32 biClrImportant;
- }BitmapInfoHeader_Type;
- #pragma pack(pop)
- typedef struct tagRGBQUAD_
- {
- U8 rgbBlue;
- U8 rgbGreen;
- U8 rgbRed;
- U8 rgbReserved;
- }RGBQUAD_Type;
- typedef struct tagIF_
- {
- U8 header1;
- U8 header2;
- U8 width;
- U8 height;
- }BMP_IF_Type;
- /*===============================================================================
- Function Prototypes
- =============================================================================== */
- static boolean PicApp_HandleEvent(CPicApp * pApp,
- AEEEvent eCode,
- uint16 wParam,
- uint32 dwParam);
- static boolean PicApp_InitAppData(CPicApp * pApp);
- static void PicApp_FreeAppData(CPicApp * pApp);
- static void PicApp_ReleaseObj(void ** ppObj);
- static boolean PicApp_Start(CPicApp * pApp);
- static boolean PicApp_DrawBMP(IDisplay *pIdisp, U8 *imagedata,int x, int y);
- static int PicApp_SaveScreen(IDisplay *pIdisp, const char *filename);
- static boolean PicApp_DrawRect(IDisplay *pIdisp, const AEERect *pRect,U16 transpCLR);
- static boolean PicApp_HandleKey(CPicApp * pApp, uint16 w, uint32 dw);
- /*===========================================================================
- STATIC/LOCAL DATA
- ===========================================================================*/
- /* GENERATED BY binary2c.pl */
- unsigned char bmpimage[500*1024];
- IFile* UpStoOpenFile(const char *fname, OpenFileMode mode)
- {
- IFileMgr* pIFileMgr = NULL;
- IFile* pIFile = NULL;
- if(!fname)
- {
- return NULL;
- }
- if(SUCCESS != ISHELL_CreateInstance(((AEEApplet *)GETAPPINSTANCE())->m_pIShell,AEECLSID_FILEMGR, (void**)(&pIFileMgr)) )
- {
- return NULL;
- }
- pIFile = IFILEMGR_OpenFile(pIFileMgr,(const char *)fname,mode);
-
- IFILEMGR_Release(pIFileMgr);
- pIFileMgr = NULL;
- return pIFile;
- }
- /* read the data with the size of len from the file fname to buf
- if SUCCESS, return the size read
- Otherwise, return 0
- */
- int UpStoReadFile(IFile *pIFile, unsigned long ofs, void* buf, unsigned long len)
- {
- int bytesRead = 0;
- if(!pIFile || !buf)
- {
- return 0;
- }
- if(SUCCESS != IFILE_Seek(pIFile,_SEEK_START,ofs))
- {
- return 0;
- }
-
- bytesRead = IFILE_Read(pIFile,buf,len);
-
-
- return bytesRead;
-
- }
- int UpStoWriteFile(IFile *pIFile, unsigned long ofs, const void* buf, unsigned long len)
- {
-
- int bytesWritten = 0;
- if(NULL == pIFile|| NULL == buf)
- {
- return 0;
- }
- if(SUCCESS != IFILE_Seek(pIFile,_SEEK_START,ofs))
- {
- return 0;
- }
-
- bytesWritten = IFILE_Write(pIFile,buf,len);
-
- return bytesWritten;
-
- }
- void UpStoCloseFile(IFile* pIFile)
- {
- IFILE_Release(pIFile);
- }
- unsigned long UpStoGetFileSize(const char *filename)
- {
- FileInfo nInfo;
- IFileMgr* pIFileMgr = NULL;
- if(NULL == filename)
- return 0;
- if(SUCCESS != ISHELL_CreateInstance(((AEEApplet *)GETAPPINSTANCE())->m_pIShell,AEECLSID_FILEMGR, (void**)(&pIFileMgr)) )
- {
- return NULL;
- }
- /*Note: the last parameter is a true pointer, instead of a virtual pointer, because some info will be pass to it*/
- if(SUCCESS != IFILEMGR_GetInfo(pIFileMgr,filename, &nInfo))
- {
- IFILEMGR_Release(pIFileMgr);
- pIFileMgr = NULL;
- return 0;
- }
- IFILEMGR_Release(pIFileMgr);
- pIFileMgr = NULL;
- return nInfo.dwSize;
- }
- /*===========================================================================
- FUNCTION: AEEClsCreateInstance
- DESCRIPTION
- This function is invoked while the app is being loaded. All Modules must provide this
- function. Ensure to retain the same name and parameters for this function.
- In here, the module must verify the ClassID and then invoke the AEEApplet_New() function
- that has been provided in AEEAppGen.c.
- After invoking AEEApplet_New(), this function can do app specific initialization. In this
- example, a generic structure is provided so that app developers need not change app specific
- initialization section every time except for a call to IDisplay_InitAppData().
- This is done as follows: InitAppData() is called to initialize AppletData
- instance. It is app developers responsibility to fill-in app data initialization
- code of InitAppData(). App developer is also responsible to release memory
- allocated for data contained in AppletData -- this can be done in
- IDisplay_FreeAppData().
- PROTOTYPE:
- int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
- PARAMETERS:
- clsID: [in]: Specifies the ClassID of the applet which is being loaded
- pIShell: [in]: Contains pointer to the IShell object.
- pIModule: pin]: Contains pointer to the IModule object to the current module to which
- this app belongs
- ppObj: [out]: On return, *ppObj must point to a valid IApplet structure. Allocation
- of memory for this structure and initializing the base data members is done by AEEApplet_New().
- DEPENDENCIES
- none
- RETURN VALUE
- AEE_SUCCESS: If the app needs to be loaded and if AEEApplet_New() invocation was
- successful
- EFAILED: If the app does not need to be loaded or if errors occurred in
- AEEApplet_New(). If this function returns FALSE, the app will not be loaded.
- SIDE EFFECTS
- none
- ===========================================================================*/
- int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
- {
- CPicApp *pApp;
- *ppObj = NULL;
-
- if(ClsId == AEECLSID_PIC)
- {
- if(AEEApplet_New( sizeof(CPicApp),
- ClsId,
- pIShell,
- po,
- (IApplet**)ppObj,
- (AEEHANDLER)PicApp_HandleEvent,
- (PFNFREEAPPDATA)PicApp_FreeAppData)
- == TRUE)
- {
-
- pApp=(CPicApp *)*ppObj;
-
- if (!PicApp_InitAppData( pApp ) )
- {
- IAPPLET_Release( (IApplet*) pApp );
- *ppObj = NULL;
- return EFAILED;
- }
-
- return (AEE_SUCCESS);
- }
- }
- return (EFAILED);
- }
- /*===========================================================================
- FUNCTION PicApp_HandleEvent
- DESCRIPTION
- This is the EventHandler for this app. All events to this app are handled in this
- function. All APPs must supply an Event Handler.
- PROTOTYPE:
- boolean PicApp_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)
- PARAMETERS:
- pi: Pointer to the AEEApplet structure. This structure contains information specific
- to this applet. It was initialized during the AEEClsCreateInstance() function.
- ecode: Specifies the Event sent to this applet
- wParam, dwParam: Event specific data.
- DEPENDENCIES
- none
- RETURN VALUE
- TRUE: If the app has processed the event
- FALSE: If the app did not process the event
- SIDE EFFECTS
- none
- ===========================================================================*/
- static boolean PicApp_HandleEvent(CPicApp * pApp, AEEEvent evt, uint16 w, uint32 dw)
- {
-
- switch (evt)
- {
- case EVT_APP_START:
-
- return PicApp_Start(pApp);
- case EVT_KEY:
- return PicApp_HandleKey(pApp,w,dw);
-
- default:
- break;
- }
- return FALSE;
- }
- static boolean PicApp_InitAppData(CPicApp * pApp)
- {
- /*get the device information*/
-
- AEEDeviceInfo deviceInfo;
-
- ISHELL_GetDeviceInfo(pApp->a.m_pIShell, &deviceInfo);
- /*get the height of font*/
-
- pApp->m_nLineHeight = IDISPLAY_GetFontMetrics(pApp->a.m_pIDisplay,
- AEE_FONT_NORMAL,
- NULL,
- NULL);
- pApp->m_nLargeLineHeight = IDISPLAY_GetFontMetrics(pApp->a.m_pIDisplay,
- AEE_FONT_LARGE,
- NULL,
- NULL);
- SETAEERECT(&pApp->m_nClntAreaRect,
- 0,
- 0,
- deviceInfo.cxScreen,
- deviceInfo.cyScreen-pApp->m_nLargeLineHeight);
-
- SETAEERECT(&pApp->m_nSoftkeyAreaRect,
- pApp->m_nClntAreaRect.x,
- pApp->m_nClntAreaRect.y+pApp->m_nClntAreaRect.dy,
- pApp->m_nClntAreaRect.dx,
- pApp->m_nLargeLineHeight);
- pApp->m_nScrHeight = deviceInfo.cyScreen;
- pApp->m_nScrWidth = deviceInfo.cxScreen;
- pApp->m_x = (pApp->m_nScrWidth - GLASS_WIDTH)/2;
- pApp->m_y = (pApp->m_nScrHeight - GLASS_HEIGHT)/2;
- return TRUE;
- }
- static void PicApp_FreeAppData(CPicApp * pApp)
- {
-
- }
- static void PicApp_ReleaseObj(void ** ppObj)
- {
- if ( ppObj && *ppObj )
- {
- (void) IBASE_Release( ( (IBase *) *ppObj ) );
- *ppObj = NULL;
- }
- }
- static boolean PicApp_Start(CPicApp * pApp)
- {
- IFile *pIFile = NULL;
- unsigned long fileLen = 0;
- const char *filename = "wallpaper.bmp";
- AEERect nRect;
-
- fileLen = UpStoGetFileSize(filename);
- if(NULL != (pIFile = UpStoOpenFile(filename,_OFM_READ)))
- {
- UpStoReadFile(pIFile, 0, bmpimage,fileLen);
- UpStoCloseFile(pIFile);
- pIFile = NULL;
- PicApp_DrawBMP(pApp->a.m_pIDisplay, (U8 *)bmpimage,0,0);
-
- if(pApp->m_x > pApp->m_nScrWidth || pApp->m_x<1) pApp->m_x = 1;
- if(pApp->m_y > pApp->m_nScrHeight || pApp->m_y<1) pApp->m_y = 1;
- if(pApp->m_x + GLASS_WIDTH > pApp->m_nScrWidth) pApp->m_x = pApp->m_nScrWidth - GLASS_WIDTH;
- if(pApp->m_y + GLASS_HEIGHT > pApp->m_nScrHeight) pApp->m_y = pApp->m_nScrHeight - GLASS_HEIGHT;
- SETAEERECT(&nRect,
- pApp->m_x,
- pApp->m_y,
- GLASS_WIDTH,
- GLASS_HEIGHT);
- PicApp_DrawRect(pApp->a.m_pIDisplay, (const AEERect *)&nRect,GLASS_COLOR);
- IDISPLAY_Update(pApp->a.m_pIDisplay);
-
- PicApp_SaveScreen(pApp->a.m_pIDisplay, "test.bmp");
- return TRUE;
- }
- return FALSE;
- }
- static boolean PicApp_Stop(CPicApp * pApp)
- {
- return TRUE;
- }
- /*all Bitmap Data has been placed into imagedata */
- static boolean PicApp_DrawBMP(IDisplay *pIdisp, U8 *imagedata,int x, int y)
- {
- BitmapFileHeader_Type *pbmpfileheader = NULL;
- BitmapInfoHeader_Type *pbmpinfoheader = NULL;
- U8 *pbitmapinfo;
- int bytesPerRow;
- IDIB *pdib=NULL;
- U8 *pbmpinfo=imagedata;
-
- if(NULL == imagedata) return FALSE;
- if(!('B'==*imagedata && 'M'==*(imagedata+1)) ) return FALSE;
- pbmpfileheader = (BitmapFileHeader_Type *)(pbmpinfo+2);
- pbmpinfoheader=(BitmapInfoHeader_Type *)(pbmpinfo+2+sizeof(BitmapFileHeader_Type));
- pbitmapinfo = pbmpinfo+pbmpfileheader->bfOffBits;
- bytesPerRow = ((pbmpinfoheader->biWidth*pbmpinfoheader->biBitCount+31)/32)*4;
- if(SUCCESS!=IDISPLAY_CreateDIBitmap(pIdisp,
- (IDIB **)&pdib,
- (uint8)pbmpinfoheader->biBitCount,
- 0,
- 0
- ))
- return FALSE;
- pdib->pBmp = pbitmapinfo+bytesPerRow*(pbmpinfoheader->biHeight-1);
- pdib->pRGB = (U32 *)(pbmpinfo+14+pbmpinfoheader->biSize);
- pdib->ncTransparent = 0; // 32-bit native color value
- pdib->cx = (uint16)pbmpinfoheader->biWidth; // number of pixels in width
- pdib->cy = (uint16)pbmpinfoheader->biHeight; // number of pixels in height
- pdib->nPitch = -bytesPerRow; // offset from one row to the next
- pdib->cntRGB = (uint16)(pbmpfileheader->bfOffBits-pbmpinfoheader->biSize-14)>>2; // number of palette entries
- pdib->nDepth = (uint8)pbmpinfoheader->biBitCount; // size of pixel in bits
- pdib->nColorScheme = IDIB_COLORSCHEME_888; // IDIB_COLORSCHEME_...
- /* Do work for starting app */
- IDISPLAY_BitBlt( pIdisp,
- x,
- y,
- pdib->cx,
- pdib->cy,
- IDIB_TO_IBITMAP(pdib),
- 0,
- 0,
- AEE_RO_COPY );
- IDIB_Release(pdib);
- return TRUE;
- }
- static int PicApp_SaveScreen(IDisplay *pIdisp, const char *filename)
- {
- IBitmap *pDevBmp = NULL;
- IDIB *pDib = NULL;
- const char *BMPFLAG = "BM";
- int index = 0;
- int byteswriten = 0;
- BitmapFileHeader_Type FileHeader;
- BitmapInfoHeader_Type InfoHeader;
-
- MEMSET(&FileHeader,0, sizeof(BitmapFileHeader_Type));
- MEMSET(&InfoHeader,0, sizeof(BitmapInfoHeader_Type));
- if(SUCCESS == IDISPLAY_GetDeviceBitmap(pIdisp, &pDevBmp))
- {
- IFile *pIFile = NULL;
-
- pDib = (IDIB *)pDevBmp;
-
- FileHeader.bfOffBits = 2 + sizeof(BitmapFileHeader_Type) + sizeof(BitmapInfoHeader_Type) + pDib->cntRGB *sizeof(U32);
- FileHeader.bfSize = FileHeader.bfOffBits + pDib->cy*pDib->nPitch;
-
- InfoHeader.biSize = sizeof(BitmapInfoHeader_Type);
- InfoHeader.biWidth = pDib->cx;
- InfoHeader.biHeight = pDib->cy;
- InfoHeader.biBitCount = pDib->nDepth;
- InfoHeader.biXPelsPerMeter = 0;
- InfoHeader.biYPelsPerMeter = 0;
- InfoHeader.biClrUsed = pDib->cntRGB;
- InfoHeader.biPlanes = 1;
- InfoHeader.biSizeImage = pDib->cy*pDib->nPitch;
- if(NULL != (pIFile = UpStoOpenFile(filename,_OFM_CREATE)))
- {
- byteswriten = UpStoWriteFile(pIFile, index, BMPFLAG,STRLEN(BMPFLAG));
- index += byteswriten;
-
- byteswriten = UpStoWriteFile(pIFile, index, &FileHeader,sizeof(BitmapFileHeader_Type));
- index += byteswriten;
-
- byteswriten = UpStoWriteFile(pIFile, index, &InfoHeader,sizeof(BitmapInfoHeader_Type));
- index += byteswriten;
- if(pDib->cntRGB > 0)
- {
- byteswriten = UpStoWriteFile(pIFile, index, pDib->pRGB,pDib->cntRGB *sizeof(U32));
- index += byteswriten;
- }
-
- {
- U8 *pSrc = pDib->pBmp + pDib->nPitch*(pDib->cy-1);
- U32 row = pDib->cy;
- U8 *tmp = NULL;
- U8 *pDest = NULL;
-
- tmp = MALLOC(pDib->nPitch*pDib->cy);
-
- if(tmp)
- {
- pDest = tmp;
- while(row--)
- {
- MEMCPY(pDest,pSrc,pDib->nPitch);
- if(16 == pDib->nDepth)
- {
- U32 col = pDib->cx;
- U16 *p = (U16 *)pDest;
- while(col--)
- {
- *p = RGB565TORGB555(*p);
- p++;
- }
- }
- pDest += pDib->nPitch;
- pSrc -= pDib->nPitch;
- }
- byteswriten = UpStoWriteFile(pIFile, index, tmp,pDib->nPitch*pDib->cy);
- index += byteswriten;
- }
- else
- {
- while(row--)
- {
- byteswriten = UpStoWriteFile(pIFile, index, pSrc,pDib->nPitch);
- pSrc -= pDib->nPitch;
- index += byteswriten;
- }
- }
-
- }
- UpStoCloseFile(pIFile);
- }
- IBITMAP_Release(pDevBmp);
- }
- return -1;
- }
- static boolean PicApp_DrawRect(IDisplay *pIdisp, const AEERect *pRect, U16 transpCLR)
- {
- IBitmap *pDevBmp = NULL;
- IDIB *pDib = NULL;
-
- if(SUCCESS == IDISPLAY_GetDeviceBitmap(pIdisp, &pDevBmp))
- {
- pDib = (IDIB *)pDevBmp;
-
- if(16 == pDib->nDepth)
- {
- U8 *pDest = (U8 *)pDib->pBmp;
- int x = 0, y = 0;
-
- pDest += pDib->nPitch * (pRect->y-1) + ((pRect->x)<<1);
- for(y=0; y< pRect->dy;y++)
- {
- U16 *pBmp = (U16 *)pDest;
- for(x=0; x<pRect->dx;x++)
- {
- *pBmp = *pBmp & transpCLR;
- pBmp++;
- }
- pDest += pDib->nPitch;
- }
- }
-
- IBITMAP_Release(pDevBmp);
- }
- return TRUE;
- }
- static boolean PicApp_HandleKey(CPicApp * pApp, uint16 w, uint32 dw)
- {
- AEERect nRect;
- switch(w)
- {
- case AVK_UP:
- pApp->m_y--;
- break;
- case AVK_DOWN:
- pApp->m_y++;
- break;
- case AVK_LEFT:
- pApp->m_x--;
- break;
- case AVK_RIGHT:
- pApp->m_x++;
- break;
- default:
- return FALSE;
- }
- PicApp_DrawBMP(pApp->a.m_pIDisplay, (U8 *)bmpimage,0,0);
-
- if(pApp->m_x > pApp->m_nScrWidth || pApp->m_x<1) pApp->m_x = 1;
- if(pApp->m_y > pApp->m_nScrHeight || pApp->m_y<1) pApp->m_y = 1;
- if(pApp->m_x + GLASS_WIDTH > pApp->m_nScrWidth) pApp->m_x = pApp->m_nScrWidth - GLASS_WIDTH;
- if(pApp->m_y + GLASS_HEIGHT > pApp->m_nScrHeight) pApp->m_y = pApp->m_nScrHeight - GLASS_HEIGHT;
- SETAEERECT(&nRect,
- pApp->m_x,
- pApp->m_y,
- GLASS_WIDTH,
- GLASS_HEIGHT);
- PicApp_DrawRect(pApp->a.m_pIDisplay, (const AEERect *)&nRect,GLASS_COLOR);
- IDISPLAY_Update(pApp->a.m_pIDisplay);
- return TRUE;
- }