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

📄 readbar.c

📁 此源码提供了将BREW应用中将资源文件bar中的所有图片读出并且保存成相应的图片格式。
💻 C
字号:
/*===========================================================================

FILE: ReadBar.c
===========================================================================*/


/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "AEEModGen.h"          // Module interface definitions
#include "AEEAppGen.h"          // Applet interface definitions
#include "AEEShell.h"           // Shell interface definitions
#include "AEEFile.h"			// File interface definitions
#include "AEEMenu.h"
#include "ReadBar_res.h"
#include "ReadBar.bid"
#include "AEESTDLIB.h"

#include "ReadBar.h"

/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static boolean ReadBar_HandleEvent(IApplet * pi, AEEEvent eCode, 
                                      uint16 wParam, uint32 dwParam);

/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */

/*===========================================================================

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)
{
   *ppObj = NULL;
		
   if(ClsId == AEECLSID_READBAR){
      if(AEEApplet_New(sizeof(MyApp), ClsId, pIShell,po,(IApplet**)ppObj,
         (AEEHANDLER)ReadBar_HandleEvent,(PFNFREEAPPDATA)FreeApp)
         == TRUE)
      {
		 // Add your code here .....
         if(InitApp(*ppObj))
            return (AEE_SUCCESS);
      }
   }
	return (EFAILED);
}

/*===========================================================================

FUNCTION ReadBar_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 ReadBar_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 ReadBar_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{  
   MyApp * pMe = (MyApp *)pi;

   switch (eCode) 
	{
      case EVT_APP_START:
		  
		  BuildMenu(pMe);
		 
		    
		    // Add your code here .....

      		return(TRUE);
      case EVT_APP_STOP:

		    // Add your code here .....

         return TRUE;

	  case  EVT_KEY:
		  switch(wParam)
		  {
		  case   AVK_UP:
			  if(pMe->m_AppState == APPSTATE_MENU)
			  {
				  IMENUCTL_HandleEvent(pMe->m_pMenu,eCode,wParam,dwParam);
			  }
			  break;
		  case  AVK_DOWN:
			  if(pMe->m_AppState == APPSTATE_MENU)
			  {
				  IMENUCTL_HandleEvent(pMe->m_pMenu,eCode,wParam,dwParam);
			  }
			
			  break;
		  case  AVK_SELECT:
			  if(pMe->m_AppState == APPSTATE_MENU)
			  {
				  IMENUCTL_HandleEvent(pMe->m_pMenu,eCode,wParam,dwParam);
			  }
			  else
			  {
				  BuildMenu(pMe);
			  }
			  break;
		  case AVK_CLR:
			  if(pMe->m_AppState == APPSTATE_MENU)
			  {
				  return FALSE;
			  }
			  else
			  {
				  BuildMenu(pMe);
			  }
			  break;
		  }
		  return TRUE;

		case  EVT_COMMAND:
            IMENUCTL_SetActive(pMe->m_pMenu ,FALSE);
			ReadBarFile(pMe,wParam);
			  
			  return TRUE;
      default:
         break;
   }
   return FALSE;
}


boolean    InitApp( MyApp *pMe)
{
	
	if(	ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_FILEMGR,(void**)&pMe->m_pFileMgr)!=SUCCESS)
		return  FALSE;

	if(ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_MENUCTL,(void**)&pMe->m_pMenu)!= SUCCESS)
		return  FALSE;
	

    ResetBmp(pMe);


	return  TRUE;
}


void       FreeApp(MyApp *pMe)
{

	if(pMe->m_pFileMgr)
	{
		IFILEMGR_Release(pMe->m_pFileMgr);
		pMe->m_pFileMgr = NULL;
	}

	if(pMe->m_pFile)
	{
		IFILE_Release(pMe->m_pFile);
		pMe->m_pFile = NULL;
	}

	if(pMe->m_pMenu)
	{
		IMENUCTL_Release(pMe->m_pMenu);
		pMe->m_pMenu = NULL;
	}

	ResetBmp(pMe);
}


boolean    ReadBarFile( MyApp *pMe ,uint16  wItemID)
{
    int  i,j,pngcount;
	CtlAddItem  ai;
	int  count;
	void  *point= NULL;
	int   Bsize;


	IMENUCTL_GetItem(pMe->m_pMenu,wItemID,&ai);
	WSTRTOSTR(ai.pText,pMe->m_SelBarName,sizeof(pMe->m_SelBarName));

	ResetBmp(pMe);

	i=0;
	count =0;
	pngcount =0;
	for(j=5001;j<6001;j++)
	{
		for(i = 0;i<MAX_BITMAP;i++)
		{
			if(pMe->m_pBmp[i])
				continue;
			else
			{
				break;
			}
		}

		if(i==MAX_BITMAP)
			break;

		pMe->m_pBmp[i] = ISHELL_LoadResBitmap(pMe->a.m_pIShell, pMe->m_SelBarName,(uint16)j);
		if(pMe->m_pBmp[i]!=NULL)
		{
			count++;
		}
		else
		{			
			point = ISHELL_LoadResDataEx(pMe->a.m_pIShell, pMe->m_SelBarName,(uint16)j,RESTYPE_IMAGE,NULL,&Bsize);
			if(point)
			{
				SaveToFile2(pMe,(byte*)point+12,Bsize);
				ISHELL_FreeResData(pMe->a.m_pIShell,point);
				point = NULL;
				
				pngcount++;
			}
		}
	}
	
	
	for(i=0;i<MAX_BITMAP;i++)
	{
		if(pMe->m_pBmp[i]== NULL)
			break;
	}
	pMe->m_BmpCount = i;
	
	if(pMe->m_BmpCount>0)
	{
		
		for(i=0;i<pMe->m_BmpCount;i++)
		{
			j=SaveToFile(pMe,pMe->m_pBmp[i]);
			if(j == FALSE)
				j=j;
		}
		
		ShowResult(pMe,TRUE);
	}
	else
	{
		if(pngcount>0)
		{
			ShowResult(pMe,TRUE);
		}
		else
		{
			ShowResult(pMe,FALSE);
		}
	}

	return TRUE;
}


boolean     SaveToFile(MyApp *pMe,IBitmap * pBmp)
{
	IDIB   * pDib;
	BITMAPFILEHEADER   bfh;   //文件头
	BITMAPINFOHEADER   bih;   //信息头
	RGBQUAD     *      pRq;   //调色板
	char               bm[4], DirName[32];
    char               FileName[32],TotalName[64];
	int                i=1 , nPitch;
	dword              dwLength; //实际图片的宽度,要为4的整数倍
    byte              *pData ;
	byte               *pRes;

    pRq = NULL;
	MEMSET(&bfh,0,sizeof(bfh));
	MEMSET(&bih,0,sizeof(bih));

	for(i=0;i<(int)STRLEN(pMe->m_SelBarName);i++)
	{
		if(pMe->m_SelBarName[i] == '.')
			break;
	}

	MEMSET(DirName,0,sizeof(DirName));
	MEMCPY(DirName,pMe->m_SelBarName,i);


	if(pBmp == NULL)
	{
		return  FALSE;
	}

	if(pMe->m_pFileMgr == NULL)
	{
		return  FALSE;
	}

	if(pMe->m_pFile)
	{
		IFILE_Release(pMe->m_pFile);
		pMe->m_pFile = NULL;
	}

	//检查文件夹是否存在
	if(IFILEMGR_Test(pMe->m_pFileMgr,DirName)!= SUCCESS)
	{
		if(IFILEMGR_MkDir(pMe->m_pFileMgr,DirName)!=SUCCESS)
			return   FALSE;
	}
    
	SPRINTF(FileName,"/%d.bmp",i);
	STRCPY(TotalName,DirName);
	STRCAT(TotalName,FileName);

	while(IFILEMGR_Test(pMe->m_pFileMgr,TotalName)== SUCCESS)  //如果存在这个文件,就从起名字
	{
		i++;
		SPRINTF(FileName,"/%d.bmp",i);
		STRCPY(TotalName,DirName);
    	STRCAT(TotalName,FileName);
	}

	pMe->m_pFile = IFILEMGR_OpenFile(pMe->m_pFileMgr,TotalName,_OFM_CREATE);
	if(pMe->m_pFile == NULL)
	{		
		return   FALSE;
	}

	pDib = NULL;
	IBITMAP_QueryInterface(pBmp,AEECLSID_DIB,&pDib);
	if(pDib == NULL)
	{
		IFILE_Release(pMe->m_pFile);
		pMe->m_pFile = NULL;
		return  FALSE;
	}
	

	//填写BITMAPINFOHEADR;
	bih.biBitCount = pDib->nDepth;
	bih.biClrImportant = pDib->cntRGB;
	bih.biClrUsed = pDib->cntRGB;
	bih.biCompression = 0;
	bih.biHeight = pDib->cy;
	bih.biPlanes = 1;
	bih.biSize = sizeof(BITMAPINFOHEADER);
	bih.biSizeImage = 0;
	bih.biWidth = pDib->cx;
	bih.biXPelsPerMeter = 2834;
	bih.biYPelsPerMeter = 2834;

	//填写调色板
	pRq = (RGBQUAD*)MALLOC(sizeof(RGBQUAD)*pDib->cntRGB);

	MEMCPY(pRq,pDib->pRGB,sizeof(RGBQUAD)*pDib->cntRGB);

	//填写文件头

	dwLength = pDib->cx*bih.biBitCount/32;
	if(pDib->cx*bih.biBitCount%32!=0)
		dwLength++;
	dwLength*=4;   //  实际位图文件中一行的长度;

	bih.biSizeImage = dwLength *pDib->cy;

	bfh.bfOffBits = 54+sizeof(RGBQUAD)*pDib->cntRGB;
	bfh.bfSize = 54+sizeof(RGBQUAD)*pDib->cntRGB+bih.biSizeImage;



 	STRCPY(bm,"BM");
	IFILE_Write(pMe->m_pFile,bm,2);  //前面两个作为标识;
    IFILE_Write(pMe->m_pFile,&bfh,sizeof(bfh));
	IFILE_Write(pMe->m_pFile,&bih,sizeof(bih));
	IFILE_Write(pMe->m_pFile,pRq,sizeof(RGBQUAD)*pDib->cntRGB);
	

	// 复制数据;
	nPitch = pDib->nPitch;
	pRes = pDib->pBmp;

    pData = MALLOC(dwLength*pDib->cy);
	MEMSET(pData,0,dwLength*pDib->cy);

	for(i=0; i<pDib->cy;i++)
	{		
		MEMCPY(pData+dwLength*(pDib->cy-i-1),pRes,dwLength);	
		pRes =  (byte*)pRes + nPitch;

	}

	IFILE_Write(pMe->m_pFile,pData,dwLength*pDib->cy);
	FREE(pData);
	
	
   IDIB_Release(pDib);
   pDib = NULL;

   IFILE_Release(pMe->m_pFile);
   pMe->m_pFile = NULL;


	return  TRUE;

}





void       SearchBarFile(MyApp *pMe)
{
	int    i,filecount;
	FileInfo   fi;
	char   filetype[16]=".bar";
	char   FileName[32];
	
	if(pMe->m_pFileMgr==NULL)
		return ;

	if(IFILEMGR_EnumInit(pMe->m_pFileMgr,"",FALSE)!=SUCCESS)
	{
		return ;
	}

	i=0;
    filecount=0;
	while(IFILEMGR_EnumNext(pMe->m_pFileMgr,&fi))
	{
		if(STRISTR(fi.szName,filetype))
		{
			STRCPY(FileName,fi.szName);
			AddToMenu(pMe,FileName,(uint16)filecount);
			filecount++;
		}
		i++;
	}

	pMe->m_BarNameCount = filecount;

}



void       AddToMenu(MyApp *pMe ,char * pFileName, uint16  wItemID)
{
	CtlAddItem    ai;
	AECHAR    buf[32];

	MEMSET(&ai,0,sizeof(CtlAddItem));

	STRTOWSTR(pFileName,buf,sizeof(buf));

	ai.pText = buf;
	ai.wItemID = wItemID;

	IMENUCTL_AddItemEx(pMe->m_pMenu,&ai);
}




void   BuildMenu(MyApp *pMe)
{
	AECHAR    buf[32];

	IMENUCTL_DeleteAll(pMe->m_pMenu);
	SearchBarFile(pMe);

	IDISPLAY_ClearScreen(pMe->a.m_pIDisplay);

	pMe->m_AppState = APPSTATE_MENU;

	if(pMe->m_BarNameCount<=0)
	{
		ISHELL_LoadResString(pMe->a.m_pIShell,"ReadBar.dat",IDS_NOBARFILE,buf,sizeof(buf));
        IDISPLAY_DrawText(pMe->a.m_pIDisplay,AEE_FONT_NORMAL,buf,-1,0,0,NULL,
			IDF_ALIGN_MIDDLE|IDF_ALIGN_CENTER);
	}
	else
	{
		IMENUCTL_SetActive(pMe->m_pMenu,TRUE);
	}

	IDISPLAY_Update(pMe->a.m_pIDisplay);

}



void   ResetBmp(MyApp* pMe)
{
	int  i;

	for(i=0;i<MAX_BITMAP;i++)
	{
		if(pMe->m_pBmp[i])
		{
			IBITMAP_Release(pMe->m_pBmp[i]);
			pMe->m_pBmp[i]=NULL;
		}
	}
}



void    ShowResult(MyApp * pMe,boolean flag)
{
	AECHAR  buf[32];

	IDISPLAY_ClearScreen(pMe->a.m_pIDisplay);

	pMe->m_AppState = APPSTATE_RESULT;

	if(flag)
	{
		ISHELL_LoadResString(pMe->a.m_pIShell,"ReadBar.dat",IDS_SAVE_SUCCESS,buf,sizeof(buf));
	}
	else
	{
		ISHELL_LoadResString(pMe->a.m_pIShell,"ReadBar.dat",IDS_SAVE_FAIL,buf,sizeof(buf));
	}

	IDISPLAY_DrawText(pMe->a.m_pIDisplay,AEE_FONT_NORMAL,buf,-1,0,0,NULL,
		IDF_ALIGN_CENTER|IDF_ALIGN_MIDDLE);
	IDISPLAY_Update(pMe->a.m_pIDisplay);
}



boolean    SaveToFile2(MyApp *pMe  ,void * point ,int  len)
{

	char               DirName[32];
    char               FileName[32],TotalName[64];
    int                i;

	for(i=0;i<(int)STRLEN(pMe->m_SelBarName);i++)
	{
		if(pMe->m_SelBarName[i] == '.')
			break;
	}

	MEMSET(DirName,0,sizeof(DirName));
	MEMCPY(DirName,pMe->m_SelBarName,i);

	if(pMe->m_pFile)
	{
		IFILE_Release(pMe->m_pFile);
		pMe->m_pFile = NULL;
	}

	//检查文件夹是否存在
	if(IFILEMGR_Test(pMe->m_pFileMgr,DirName)!= SUCCESS)
	{
		if(IFILEMGR_MkDir(pMe->m_pFileMgr,DirName)!=SUCCESS)
			return   FALSE;
	}
    
	SPRINTF(FileName,"/%d.png",i);
	STRCPY(TotalName,DirName);
	STRCAT(TotalName,FileName);

	while(IFILEMGR_Test(pMe->m_pFileMgr,TotalName)== SUCCESS)  //如果存在这个文件,就从起名字
	{
		i++;
		SPRINTF(FileName,"/%d.png",i);
		STRCPY(TotalName,DirName);
    	STRCAT(TotalName,FileName);
	}

	pMe->m_pFile = IFILEMGR_OpenFile(pMe->m_pFileMgr,TotalName,_OFM_CREATE);
	if(pMe->m_pFile == NULL)
	{		
		return   FALSE;
	}

	IFILE_Write(pMe->m_pFile,point,len);

	IFILE_Release(pMe->m_pFile);
	pMe->m_pFile = NULL;


	return  TRUE;

}

⌨️ 快捷键说明

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