maintask.c
资源名称:bmp.rar [点击查看]
上传用户:kaigelee
上传日期:2022-07-13
资源大小:6432k
文件大小:13k
源码类别:
图片显示
开发平台:
Visual C++
- #include <windows.h>
- #include <direct.h>
- #include <stdio.h>
- #include <io.h>
- #include "pub/osg.h"
- #include "pub/system_drv.h"
- enum
- {
- MSG_PGDOWN,
- MSG_PGUP,
- MSG_KeyUP,
- MSG_KeyDOWN,
- MSG_KeyLEFT,
- MSG_KeyRIGHT,
- MSG_KeySELECT
- };
- typedef struct _T_Message
- {
- INT32U param0;
- INT32U param1;
- INT32U param2;
- INT32U param3;
- }T_Message;
- UINT32 Test_queueID;
- /*位图文件头*/
- typedef struct BmpFileHeader
- {
- INT16U fileId; //文件标识
- INT32U fileSize; //文件大小
- INT32U fileRe; // 保留
- INT32U dataEx; //位图数据偏
- INT32U informationHeadSize; //信息头长度
- INT32U width; //宽
- INT32U height; //高
- INT16U page; //面数
- INT16U pel_bit; //每个象素的位数
- INT32U rel; //压缩信息
- INT32U bmpSize; //位图数据大小
- INT32U HDdefination; //水平分辨率
- INT32U UPdefination; //垂直分辨率
- INT32U colornum; //位图使用的颜色数
- INT32U importantclor; //重要的颜色数
- }BmpFileHeader;
- /*文件头的读取*/
- void ReadBmpFileHeader(char *direction,BmpFileHeader *bmpfileheader)
- {
- FILE *fp;
- INT16U tmp1[3];
- INT32U tmp2[12];
- fp=fopen(direction,"rb");
- if(fp==NULL)
- {
- printf("error on open");
- exit(1);
- }
- fread(&tmp1[0],2,1,fp);
- bmpfileheader->fileId= tmp1[0];
- fread(&tmp2[0],4,1,fp);
- bmpfileheader->fileSize=tmp2[0];
- fread(&tmp2[1],4,1,fp);
- bmpfileheader->fileRe=tmp2[1];
- fread(&tmp2[2],4,1,fp);
- bmpfileheader->dataEx=tmp2[2];
- fread(&tmp2[3],4,1,fp);
- bmpfileheader->informationHeadSize=tmp2[3];
- fread(&tmp2[4],4,1,fp);
- bmpfileheader->width=tmp2[4];
- fread(&tmp2[5],4,1,fp);
- bmpfileheader->height=tmp2[5];
- fread(&tmp1[1],2,1,fp);
- bmpfileheader->page=tmp1[1];
- fread(&tmp1[2],2,1,fp);
- bmpfileheader->pel_bit=tmp1[2];
- fread(&tmp2[6],4,1,fp);
- bmpfileheader->rel=tmp2[6];
- fread(&tmp2[7],4,1,fp);
- bmpfileheader->bmpSize=tmp2[7];
- fread(&tmp2[8],4,1,fp);
- bmpfileheader->HDdefination=tmp2[8];
- fread(&tmp2[9],4,1,fp);
- bmpfileheader->UPdefination=tmp2[9];
- fread(&tmp2[10],4,1,fp);
- bmpfileheader->colornum=tmp2[10];
- fread(&tmp2[11],4,1,fp);
- bmpfileheader->importantclor=tmp2[11];
- fclose(fp);
- }
- /*一个延时函数用来进行特效处理*/
- void delay(int a,int b)
- { int i,j;
- for(i=0;i<a;i++)
- {
- for(j=0;j<b;j++);
- }
- }
- void show_picture(char *filename)
- {
- int m,n;
- int i,j,padding,k,a,x,b;
- INT32U data,red,green,blue; //用于存储24位色的数据
- INT32U *pdata;
- INT32U *pdata1;
- INT32U pal;
- INT32U pal1[2]; //存储1位色的调色板
- INT8U data1; //存储1位色的索引值
- INT32U pal4[16]; //存储4位色的调色板
- INT8U data4; //存储4位色的索引值
- INT32U pal8[256]; //存储8位色的调色板
- INT8U data8; //存储8位色的索引值
- INT16U data16; //16位色数据
- BmpFileHeader bmpfileheader;
- FILE *fp;
- fp=fopen(filename,"rb");//打开文件
- if(fp==NULL)
- {
- printf("error on open");
- exit(1);
- }
- ReadBmpFileHeader(filename,&bmpfileheader);//读取文件头信息
- m=bmpfileheader.width;
- n=bmpfileheader.height;
- pdata=(INT32U*)malloc(4*m*n);
- pdata1=pdata;
- switch(bmpfileheader.pel_bit)
- {
- /*显示一位数据*/
- case 1:
- GFMDrawRectangle(0,0,726,576, 0xff000000, 1);
- fseek(fp,54,0);
- fread(&pal1[0],4,1,fp);//读调色板数据
- fread(&pal1[1],4,1,fp);
- rewind(fp);
- fseek(fp,bmpfileheader.dataEx,0);
- padding=(8*(bmpfileheader.bmpSize/bmpfileheader.height)-bmpfileheader.width)/8;//一行中没用的字节数
- for(i=0;i<n;i++)
- {
- for(j=0;j<m/8;j++)//每8个像素进行读取
- {
- fread(&data1,1,1,fp);
- for(k=0;k<8;k++)//每一个字节进行读取
- {
- a=(data1&0x80)==0x80? 1:0 ;//读取位图数据,每一位代表一个颜色索引值
- data1<<=1;
- *(pdata+k+8*j+(n-1-i)*m)=pal1[a];
- //GFMSetPixels(pal1[a],k+j*8,n-i,1);//逐点显示
- }
- }
- fread(&data1,1,1,fp);//当宽度不为8的整数倍时,剩余的像素的读取
- /*for(x=0;x<m-8*j;x++)
- {
- a=(data1&0x80)==0x80? 1:0 ;
- data1<<=1;
- GFMSetPixels(pal1[a],x+8*j,n-i,1);
- }*/
- fread(&data1,padding,1,fp);
- }
- GFMWriteRectangle(0,0,m,n,pdata1);
- rewind(fp);
- break;
- /*显示4位数据*/
- case 4:
- GFMDrawRectangle(0,0,726,576, 0xff000000, 1);
- fseek(fp,54,0);
- fread(pal4,4,16,fp);//读调色板数据
- rewind(fp);
- fseek(fp,bmpfileheader.dataEx,0);
- if(bmpfileheader.rel==0)
- {
- padding=(2*(bmpfileheader.bmpSize/bmpfileheader.height)-bmpfileheader.width)/2;//一行中没用的字节数
- for(i=0;i<n;i++)
- {
- for(j=0;j<m/2;j++)//每两个像素进行读取
- {
- fread(&data4,1,1,fp);
- a=data4>>4; //先读前面4位
- b=data4&(0x0f);//读后面4位
- *(pdata+2*j+(n-1-i)*m)=pal4[a];
- *(pdata+1+2*j+(n-1-i)*m)=pal4[a];
- // GFMSetPixels(pal4[a],0+j*2,n-i,1);
- // GFMSetPixels(pal4[b],1+j*2,n-i,1);
- }
- fread(&data4,1,1,fp);
- /* if(m%2==1) //还剩一个像素时,将这个像素显示
- {
- data4=data4&0xf0;
- data4>>=4;
- GFMSetPixels(pal4[data4],2+2*j,n-i,1);
- } */
- fread(&data4,padding,1,fp);
- }
- GFMWriteRectangle(0,0,m,n,pdata1);
- }
- rewind(fp);
- break;
- /*显示8位数据*/
- case 8:
- GFMDrawRectangle(0,0,726,576, 0xff000000, 1);
- fseek(fp,54,0);
- fread(pal8,4,256,fp);//读调色板数据
- rewind(fp);
- fseek(fp,bmpfileheader.dataEx,0);
- if(bmpfileheader.rel==0)
- {
- padding = (4-(m)%4)%4;//一行中没用的字节数
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- fread(&data8,1,1,fp);//读取一个字节
- *(pdata+j+(n-1-i)*m)=pal8[data8];
- }
- fread(&data8,padding,1,fp);
- }
- for(i=0;i<10;i++)
- {
- for(j=0;j<10;j++)
- {
- GFMDrawRectangle(0,0,726,576, 0xff000000, 1);
- GFMWriteRectangle(i*10,j*10,m,n,pdata1);
- }
- delay(3333,33333);
- }
- }
- rewind(fp);
- break;
- case 16:
- GFMDrawRectangle(0,0,726,576, 0xff000000, 1);
- if(bmpfileheader.rel==0)//显示16位色1555模式
- {
- fseek(fp,bmpfileheader.dataEx,0);
- padding=((bmpfileheader.bmpSize/bmpfileheader.height)/2-bmpfileheader.width)*2;
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- fread(&data16,2,1,fp);
- red=data16&0x7c00;//依次读取五位数据
- red<<=9;
- green=data16&0x03e0;
- green<<=6;
- blue=data16&0x001f;
- blue<<=3;
- data=red+green+blue;//将RGB值相加
- *(pdata+j+(n-1-i)*m)=data;
- // GFMSetPixels(data,j,n-1-i,1);
- }
- fread(&data,padding,1,fp);
- }
- GFMWriteRectangle(0,0,m,n,pdata1);
- rewind(fp);
- }
- else if(bmpfileheader.rel==3)
- {
- fseek(fp,54,0);
- fread(&pal,4,1,fp);//读文件头与位图数据之间4个字节的数据
- rewind(fp);
- if(pal==0xf800)//565模式
- {
- fseek(fp,bmpfileheader.dataEx,0);
- padding=((bmpfileheader.bmpSize/bmpfileheader.height)/2-bmpfileheader.width)*2;
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- fread(&data16,2,1,fp);
- red=data16&0xf800;
- red<<=8;//读前五位数据
- green=data16&0x07e0;
- green<<=5;//读前六位数据
- blue=data16&0x001f;
- blue<<=3;//读后五位数据
- data=red+green+blue;//将RGB值相加
- *(pdata+j+(n-1-i)*m)=data;
- // GFMSetPixels(data,j,n-1-i,1);
- }
- fread(&data,padding,1,fp);
- }
- GFMWriteRectangle(0,0,m,n,pdata1);
- rewind(fp);
- }
- else if(pal==0x0f00)//4444模式
- {
- fseek(fp,bmpfileheader.dataEx,0);
- padding=((bmpfileheader.bmpSize/bmpfileheader.height)/2-bmpfileheader.width)*2;
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- fread(&data16,2,1,fp);
- red=data16&0x0f00;
- red<<=12;//读前4位数据
- green=data16&0x00f0;
- green<<=8;//读前4位数据
- blue=data16&0x000f;
- blue<<=4;//读前4位数据
- data=red+green+blue;//将RGB值相加
- *(pdata+j+(n-1-i)*m)=data;
- // GFMSetPixels(data,j,n-1-i,1);
- }
- fread(&data,padding,1,fp);
- }
- GFMWriteRectangle(0,0,m,n,pdata1);
- rewind(fp);
- }
- }
- break;
- /*显示24位色*/
- case 24:
- GFMDrawRectangle(0,0,726,576, 0xff000000, 1);
- fseek(fp,bmpfileheader.dataEx,0);
- padding = (4-(m*3)%4)%4;
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- fread(&data,3,1,fp);
- *(pdata+j+(n-1-i)*m)=data;
- }
- fread(&data,padding,1,fp);
- }
- GFMWriteRectangle(0,0,m,n,pdata1);
- rewind(fp);
- break;
- }
- }
- /*************************************************************************/
- /* 在MainTask_Init中创建的任务 */
- /* 这个任务在接收队列的消息,而消息是从 */
- /* MainmenuKeyProcess 中发送出来的 */
- /* 这个demo中是捕获右键,并循环显示“图片”目录下的文件名 */
- /*************************************************************************/
- void demoTask(void)
- {
- T_Message RecvMsg;
- INT8U acPath[200],acMask[200],filename[200];
- long done;
- struct _finddata_t fileinfo;
- getcwd(acPath, sizeof(acPath));
- sprintf(acPath, "%s\图片",acPath);
- sprintf(acMask, "%s\*.BMP",acPath);
- done = _findfirst(acMask, &fileinfo);
- if(done != -1)
- {
- FNTDrawText(0xff00ff00,24,50, 300, fileinfo.name );
- /*取到了文件名,和acpath一起组成完整的路径,用fopen打开进行分析和显示*/
- sprintf(filename,"%s\%s",acPath,fileinfo.name);
- show_picture(filename);
- }
- while(1)
- {
- DTOS_DRV_ReceiveMessage( Test_queueID, &RecvMsg);
- switch(RecvMsg.param0)
- {
- case MSG_PGDOWN:
- break;
- case MSG_PGUP:
- break;
- case MSG_KeyUP:
- break;
- case MSG_KeyDOWN:
- break;
- case MSG_KeyLEFT:
- break;
- case MSG_KeyRIGHT:
- if(done != -1)
- {
- if(_findnext(done, &fileinfo) == 0)
- {
- GFMDrawRectangle(50,300,669,24, 0xff000000, 1);
- FNTDrawText(0xff00ff00,24,50, 300, fileinfo.name );
- /*取到了文件名,和acpath一起组成完整的路径,用fopen打开进行分析和显示*/
- sprintf(filename,"%s\%s",acPath,fileinfo.name);
- show_picture(filename);
- }
- else
- {
- done = _findfirst(acMask, &fileinfo);
- if(done != -1)
- {
- GFMDrawRectangle(50,300,669,24, 0xff000000, 1);
- FNTDrawText(0xff00ff00,24,50, 300, fileinfo.name );
- /*取到了文件名,和acpath一起组成完整的路径,用fopen打开进行分析和显示*/
- sprintf(filename,"%s\%s",acPath,fileinfo.name);
- show_picture(filename);
- }
- }
- }
- break;
- case MSG_KeySELECT:
- break;
- default:;
- }
- }
- }
- void MainmenuKeyProcess(INT32U keycode)
- {
- T_Message msg;
- switch(keycode)
- {
- case VK_NEXT:
- msg.param0 = MSG_PGDOWN;
- DTOS_DRV_SendMessage( Test_queueID , &msg);
- break;
- case VK_PRIOR:
- msg.param0 = MSG_PGUP;
- DTOS_DRV_SendMessage( Test_queueID , &msg);
- break;
- case VK_UP:
- msg.param0 = MSG_KeyUP;
- DTOS_DRV_SendMessage( Test_queueID , &msg);
- break;
- case VK_DOWN:
- msg.param0 = MSG_KeyDOWN;
- DTOS_DRV_SendMessage( Test_queueID , &msg);
- break;
- case VK_LEFT:
- msg.param0 = MSG_KeyLEFT;
- DTOS_DRV_SendMessage( Test_queueID , &msg);
- break;
- case VK_RIGHT:
- msg.param0 = MSG_KeyRIGHT;
- DTOS_DRV_SendMessage( Test_queueID , &msg);
- break;
- case VK_SELECT:
- msg.param0 = MSG_KeySELECT;
- DTOS_DRV_SendMessage( Test_queueID , &msg);
- break;
- default:
- msg.param0 = 1;
- msg.param1 = 1;
- msg.param2 = 1;
- msg.param3 = 1;
- DTOS_DRV_SendMessage( Test_queueID , &msg);
- break;
- }
- }
- /*********************************************************/
- /* 这里是程序的入口 */
- /*********************************************************/
- void MainTask_Init(int mode)
- {
- /*********************************************************/
- /* 注册键盘按键的回调函数,即每次有键按下都会 */
- /* 调用所注册的回调,在这里是 MainmenuKeyProcess */
- /*********************************************************/
- registerDVBKeycodeCallback(MainmenuKeyProcess);
- /*********************************************************/
- /* 创建一个消息队列,队列深度为100,即容纳100个消息, */
- /* 每个消息的大小为sizeof(T_Message)=16byte */
- /*********************************************************/
- Test_queueID = DTOS_DRV_CreatQueue( 100 ,sizeof(T_Message));
- /*********************************************************/
- /* 创建一个任务,名称为DEMO,任务为demoTask */
- /*********************************************************/
- DTOS_DRV_CreateTask( demoTask,0,0,"DEMO");
- }