📄 bmpreader.cpp
字号:
// BmpReader.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "stdio.h"
#define MAX_LOADSTRING 100
#define WIDTHBYTES(i) ((i+31)/32*4)
#define CONVERT_565 0
#define STNTEST 0
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
HGLOBAL hImgData=NULL;
HPALETTE hPalette=NULL;
HBITMAP hBitmap=NULL;
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
BOOL LoadBmpFile (HWND hWnd);
BOOL SaveRawAsBmp(char* filename,char *rawfilename, int wid,int hei,int bpp);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_BMPREADER, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BMPREADER);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BMPREADER);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_BMPREADER;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc,hMemDC;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case ID_SHOWBMP:
LoadBmpFile (hWnd); //a_240x320.bmp");
break;
case ID_RAW_TO_BMP:
SaveRawAsBmp("d:/tmp/pmp_outlook_images/video.bmp",
"d:/tmp/pmp_outlook_images/video.dat",1024,768,16);
SaveRawAsBmp("d:/tmp/pmp_outlook_images/mediaface.bmp",
"d:/tmp/pmp_outlook_images/mediaface.dat",1024,768,16);
SaveRawAsBmp("d:/tmp/pmp_outlook_images/musicface.bmp",
"d:/tmp/pmp_outlook_images/musicface.dat",1024,768,16);
SaveRawAsBmp("d:/tmp/pmp_outlook_images/recordface.bmp",
"d:/tmp/pmp_outlook_images/recordface.dat",1024,768,16);
SaveRawAsBmp("d:/tmp/pmp_outlook_images/picface.bmp",
"d:/tmp/pmp_outlook_images/picface.dat",1024,768,16);
SaveRawAsBmp("d:/tmp/pmp_outlook_images/docface.bmp",
"d:/tmp/pmp_outlook_images/docface.dat",1024,768,16);
SaveRawAsBmp("d:/tmp/pmp_outlook_images/clockface.bmp",
"d:/tmp/pmp_outlook_images/clockface.dat",1024,768,16);
SaveRawAsBmp("d:/tmp/pmp_outlook_images/medemface.bmp",
"d:/tmp/pmp_outlook_images/medemface.dat",1024,768,16);
SaveRawAsBmp("d:/tmp/pmp_outlook_images/controlface.bmp",
"d:/tmp/pmp_outlook_images/controlface.dat",1024,768,16);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
if (hBitmap) //hBitmap一开始是NULL,当不为NULL时表示有图
{
hMemDC = CreateCompatibleDC(hdc); //建立一个内存设备上下文
if (hPalette) //有调色板
{
//将调色板选入屏幕设备上下文
SelectPalette (hdc, hPalette, FALSE);
//将调色板选入内存设备上下文
SelectPalette (hMemDC, hPalette, FALSE);
RealizePalette (hdc);
}
//将位图选入内存设备上下文
SelectObject(hMemDC, hBitmap);
//显示位图
BitBlt(hdc, 0, 0, bi.biWidth, bi.biHeight, hMemDC, 0, 0, SRCCOPY);
//释放内存设备上下文
DeleteDC(hMemDC);
}
//释放屏幕设备上下文
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
BOOL LoadBmpFile (HWND hWnd)
{
FILE *cf,*cf1;//文件变量,用来对文件操作
BITMAPFILEHEADER bmfh;//BMP文件头变量
BITMAPINFOHEADER bmih;//BMP文件信息变量
RGBTRIPLE *rgbBlock1;//24位真彩色数据格式
unsigned short * rgb555;
int temp=0,count=0,i=0,cx=0,cy=0,bpp=0;
unsigned short colorR=0,colorG=0,colorB=0,color1555=0,color565;
char szFilename[200];
HDC hdc=GetDC(hWnd);
//读入文件1
sprintf(szFilename,"e:/logo1.bmp");//flowers_640.480.bmp");
if (!(cf=fopen(szFilename,"rb")))//找到文件后,打开文件
{
MessageBox(hWnd,"Can not open the file!",NULL,MB_OK);
return FALSE;
}
sprintf(szFilename,"e:/logo1.txt");
#if STNTEST
if (!(cf1=fopen(szFilename,"wb+")))//找到文件后,打开文件
#else
if (!(cf1=fopen(szFilename,"wt")))//找到文件后,打开文件
#endif
{
MessageBox(hWnd,"Can not open the file!",NULL,MB_OK);
return FALSE;
}
fread(&bmfh,1,sizeof(bmfh),cf);//读取文件头
fread(&bmih,1,sizeof(bmih),cf);//读取文件信息头
cx=bmih.biWidth;
cy=bmih.biHeight;
bpp=bmih.biBitCount;
if(bpp==24)
{
rgbBlock1 = new RGBTRIPLE[100]; //B,G,R
fseek(cf,0,SEEK_SET);
fseek(cf,bmfh.bfOffBits,SEEK_CUR);
count=0;
while(!feof(cf))
{
temp=fread(rgbBlock1,1,100*3,cf);//
for(i=0;i<temp/3;i++)
{
//RGBTRIPLE is B G R
SetPixel(hdc,count%cx,count/cx,
RGB(rgbBlock1[i].rgbtRed,rgbBlock1[i].rgbtGreen,rgbBlock1[i].rgbtBlue));
#if CONVERT_565
colorB=(rgbBlock1[i].rgbtBlue)>>3;
colorG=(rgbBlock1[i].rgbtGreen)>>2;
colorR=(rgbBlock1[i].rgbtRed)>>3;
color565=colorR<<11|colorG<<5|colorB;
count++;
fprintf(cf1,"0x%04x, ",color565);//LCD 1555 is I|B|G|R (Red is lsb);
if(count%200==0)
fprintf(cf1,"\n");
#else
#define _RGB_MODE
#ifdef _RGB_MODE
fprintf(cf1,"0x%02x%02x%02x, ",rgbBlock1[i].rgbtRed,
rgbBlock1[i].rgbtGreen,rgbBlock1[i].rgbtBlue);//
#else
fprintf(cf1,"0x%02x%02x%02x, ",rgbBlock1[i].rgbtBlue,
rgbBlock1[i].rgbtGreen,rgbBlock1[i].rgbtRed);//
#endif
count++;
if(count%200==0)
fprintf(cf1,"\n");
#endif
}
}
delete []rgbBlock1;//释放内存
}//end of bpp=24
if(bpp==16) //555 format
{
rgb555= new unsigned short[100];
fseek(cf,0,SEEK_SET);
fseek(cf,bmfh.bfOffBits,SEEK_CUR);
count=0;
while(!feof(cf))
{
temp=fread(rgb555,sizeof(unsigned short),100,cf);//
for(i=0;i<temp;i++)
{
colorB=(rgb555[i]&0x1F)<<3;
colorG=(rgb555[i]&0x3E0)>>5<<3;
colorR=(rgb555[i]&0x7C00)>>10<<3;
//RGBTRIPLE is B G R
SetPixel(hdc,count%cx,count/cx,
RGB(colorR,colorG,colorB));
count++;
color1555=((rgb555[i]&0x1F)<<10)|(rgb555[i]&0x3E0)|((rgb555[i]&0x7C00)>>10);
#if STNTEST
unsigned short tmp=0;
int nwrite =0;
/*for STN test 06.4 */
for(int w=0;w<15;w++)
{
tmp|=(color1555&0x1)<<(15-w-1);
color1555=color1555>>1;
}
//fprintf(cf1,"0x%04x, ",tmp);//LCD 1555 is I|B|G|R (Red is lsb);
fwrite(&tmp,sizeof(tmp),1,cf1);
#else
fprintf(cf1,"0x%04x, ",color1555);//LCD 1555 is I|B|G|R (Red is lsb);
if(count%200==0)
fprintf(cf1,"\n");
#endif
} //for
} //while
delete []rgb555;
}//end of 16bpp
if(bpp==4)
{
// bpp=4, read the palatte and the image data
unsigned int temp=0;
int count=0;
fseek(cf,0,SEEK_SET);
fseek(cf,54,SEEK_CUR);
count=0;
while(!feof(cf))
{
fread(&temp,4,1,cf);//
fprintf(cf1,"0x%04x, ",temp);
count++;
if(count%10==0)
{
fprintf(cf1,"\n");
}
} //while
} //bpp=4
fclose(cf);
fclose(cf1);
// TRACE("\nwrite %d*%d*3=%d\n",bmih.biWidth,bmih.biHeight,bmih.biSizeImage);
// TRACE("\nwrite %d bytes\n");
return TRUE;
}
BOOL SaveRawAsBmp(char* filename,char *rawfilename, int wid,int hei,int bpp)
{
//定义图形大小
int iWidth = wid;
int iHeight = hei;
int iPixel = bpp;
FILE *fraw,*fbmp;
//图形格式参数
LPBITMAPINFO lpbmih = new BITMAPINFO;
lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbmih->bmiHeader.biWidth = iWidth;
lpbmih->bmiHeader.biHeight = iHeight;
lpbmih->bmiHeader.biPlanes = 1;
lpbmih->bmiHeader.biBitCount = iPixel;
lpbmih->bmiHeader.biCompression = BI_RGB;
lpbmih->bmiHeader.biSizeImage = 0;
lpbmih->bmiHeader.biXPelsPerMeter = 0;
lpbmih->bmiHeader.biYPelsPerMeter = 0;
lpbmih->bmiHeader.biClrUsed = 0;
lpbmih->bmiHeader.biClrImportant = 0;
//保存到文件并创建位图结构
BITMAPFILEHEADER bmfh;
ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER));
*((char *)&bmfh.bfType) = 'B';
*(((char *)&bmfh.bfType) + 1) = 'M';
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh.bfSize = bmfh.bfOffBits + (iWidth * iHeight) * iPixel / 8;
TCHAR szBMPFileName[128];
TCHAR szRawFileName[128];
#define RAW_BUF_LEN 1024
BYTE raw_store[RAW_BUF_LEN];
unsigned short raw_pixel_565,raw_pixel_1555;
int read_count =0;
int total=0;
int iBMPBytes = iWidth * iHeight * iPixel / 8;
strcpy(szBMPFileName,filename);
strcpy(szRawFileName,rawfilename);
if(!(fraw=fopen(szRawFileName,"rb")))
{
return FALSE;
}
if(!(fbmp=fopen(szBMPFileName,"wb+")))
{
return FALSE;
}
if(fwrite(&bmfh,1,sizeof(BITMAPFILEHEADER),fbmp)!=sizeof(BITMAPFILEHEADER))
{
goto exit;
}
if(fwrite(lpbmih,1,sizeof(BITMAPINFOHEADER),fbmp)!=sizeof(BITMAPINFOHEADER))
{
goto exit;
}
total+=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPFILEHEADER);
while(!feof(fraw))
{
/*
if( !(read_count = fread(raw_store,1,RAW_BUF_LEN,fraw)) )
{
goto exit;
}
*/
if( !(read_count = fread(&raw_pixel_565,1,2,fraw)) )
{
goto exit;
}
raw_pixel_1555= (raw_pixel_565&0xFFC0)>>1 | (raw_pixel_565 & 0x1f);
if(fwrite(&raw_pixel_1555,1,read_count,fbmp)!=read_count)
{
goto exit;
}
total+=read_count;
if(total>=iWidth*iHeight*bpp/8)
{
break;
}
}
fclose(fraw);
fclose(fbmp);
delete lpbmih;
lpbmih = NULL;
return TRUE;
exit:
fclose(fraw);
fclose(fbmp);
delete lpbmih;
lpbmih=NULL;
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -