bmp565 wince显示
// #include "stdafx.h"
#include <Windows.h>
#include <vector>
HWND g_hWnd;
HDC g_hTopDc = NULL;
HBITMAP g_hBitMap = NULL;
UINT g_iWidth, g_iHeight;
LRESULT MainWndProc(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
struct BitmapFileHeader {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
};
//Bitmap information header
//provides information specific to the image data
struct BitmapInfoHeader {
DWORD biSize;
long biWidth;
long biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
};
//Colour palette
struct RGBQuad {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
};
enum Format { // I: integer format, F: floating point format
I8BITS,
I16BITS,
I32BITS,
I16BITS_5_5_5_1,
I16BITS_5_6_5,
F16BITS,
F32BITS,
F64BITS
};
int m_width,m_height,m_nChannels,m_bytesPerPixel,m_bytesPerChannel;
std::vector<unsigned char> m_data;
Format m_format;
BITMAPINFO* m_lpBitsInfo;
BYTE *tmpData;
BYTE *g_fileData;
bool loadBMP(const std::string &fname)
{
// read bitmap header
BitmapFileHeader fheader;
BitmapInfoHeader iheader;
for (int i=0; i<14; ++i) ((char*)&fheader)[i] = 3;
FILE *fp = fopen(fname.c_str(), "rb");
if (!fp)
return false;
long lBegin = ftell(fp);
fseek(fp,0,SEEK_END);
long lEnd = ftell(fp);
long lSize = lEnd - lBegin;
fseek(fp,0,SEEK_SET);
fread(&fheader.bfType, 2, 1, fp);
fread(&fheader.bfSize, 4, 1, fp);
fread(&fheader.bfReserved1, 2, 1, fp);
fread(&fheader.bfReserved2, 2, 1, fp);
fread(&fheader.bfOffBits, 4, 1, fp);
g_fileData = (BYTE *)malloc(lSize);
fread(g_fileData,1,lSize - sizeof(BitmapFileHeader),fp);
fseek(fp,-lSize + sizeof(BitmapFileHeader),SEEK_CUR);
fread(&iheader, 40, 1, fp);
m_width = iheader.biWidth;
m_height = abs(iheader.biHeight);
// ASSERT(iheader.biPlanes == 1);
if (iheader.biCompression != BI_RGB &&
iheader.biCompression != BI_BITFIELDS ) {
fclose(fp);
return false;
}
m_nChannels = 3;
if (iheader.biCompression == BI_RGB )
{
if (iheader.biBitCount != 24) {
fclose(fp);
return false;
}
else {
m_bytesPerPixel = iheader.biBitCount / 8;
m_bytesPerChannel = m_bytesPerPixel / m_nChannels;
m_format = I8BITS;
}
}
else if (iheader.biCompression == BI_BITFIELDS)
{
if (iheader.biBitCount == 16) { // 5551
m_nChannels = 4;
m_bytesPerPixel = 4;
//m_bytesPerChannel = m_bytesPerPixel / m_nChannels;
m_format = I8BITS;
}
else if (iheader.biBitCount == 32) // 5551
{
fclose(fp);
return false;
}
else{
fclose(fp);
return false;
}
}
else {
fclose(fp);
return false;
}
// load the palette, if any
RGBQuad *palette = 0;
if (iheader.biBitCount < 16) {
LONG numCols = 1 << iheader.biBitCount;
palette = new RGBQuad[numCols];
fread(palette, 4, numCols, fp);
// TEMP: palette not used!
}
else if (iheader.biBitCount == 16)
{
//the color table consists of three DWORD color masks that specify the red, green, and blue components of each pixel.
//在555格式下,红、绿、蓝的掩码分别是:0x7C00、0x03E0、0x001F
//在565格式下,它们则分别为:0xF800、0x07E0、0x001F。
//你在读取一个像素之后,可以分别用掩码“与”上像素值,从而提取出想要的颜色分量(当然还要再经过适当的左右移操作)。
//palette = new RGBQuad[3];
unsigned int redMask,greenMask,blueMask;
if (iheader.biCompression == BI_BITFIELDS)
{
fread(&redMask, 4, 1, fp);
fread(&greenMask, 4, 1, fp);
fread(&blueMask, 4, 1, fp);
}
if (redMask == 0x7C00)
{
m_format = I16BITS_5_5_5_1;
}
else if (redMask == 0xF800)
{
m_format = I16BITS_5_6_5;
}
else {
fclose(fp);
return false;
}
}
// load bitmap data to temporary storage
long size = fheader.bfSize - fheader.bfOffBits;
fseek(fp,fheader.bfOffBits,SEEK_SET);
if (size <= 0)
printf("bad bitmap size\n");
tmpData = new BYTE[size];
fread(tmpData, size, 1, fp);
//BMP24ToRGB565.rar 可以进行 保存成565
//800 *480
//拉伸成 1024 * 512 因为opengl es 1.0下要2的幂
int w = 1024, h = 512;
int colorplanes = iheader.biBitCount/8;
BYTE* data = new BYTE[w*h*colorplanes];
for (int i = 0; i < m_height; i++)
{
memcpy(data+i*w*colorplanes+(h-m_height)*w*colorplanes/*+(w-m_width)*colorplanes+(h-m_height)*m_width*colorplanes*/,tmpData+(i)*m_width*colorplanes,colorplanes*m_width);
//data[m_width]
}
delete[] tmpData;
tmpData = data;
m_width = 1024;
m_height = 512;
delete[] palette;
return true;
}
void CreateMemDC()
{
BITMAP m_bitBuffer = {0};
BITMAPINFO* m_bmi = NULL;
BYTE buf444f[100] = {0};
m_bmi = (BITMAPINFO*)buf444f;
//创建绘图缓冲区
HDC hScreen = ::GetDC(NULL);
g_hTopDc = ::CreateCompatibleDC(hScreen);
m_bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_bmi->bmiHeader.biWidth = m_width;
m_bmi->bmiHeader.biHeight = m_height;
m_bmi->bmiHeader.biPlanes = 1;
m_bmi->bmiHeader.biBitCount = 16;
m_bmi->bmiHeader.biCompression = BI_BITFIELDS;
// color mask
DWORD* pBitFields = (DWORD*)m_bmi->bmiColors;
pBitFields[0] = 0xf800;
pBitFields[1] = 0x07e0;
pBitFields[2] = 0x001f;
//m_bitBuffer11.bmType = 31;
m_bitBuffer.bmBitsPixel = m_bmi->bmiHeader.biBitCount;
m_bitBuffer.bmHeight = m_bmi->bmiHeader.biHeight;
m_bitBuffer.bmWidth = m_bmi->bmiHeader.biWidth;
m_bitBuffer.bmPlanes = 1;
HBITMAP m_hbitmap = CreateDIBSection(NULL, m_bmi, DIB_RGB_COLORS, &(m_bitBuffer.bmBits), NULL, NULL);
HBITMAP m_hOldBitmap = (HBITMAP)GetCurrentObject(g_hTopDc, OBJ_BITMAP);
m_hOldBitmap = (HBITMAP)SelectObject(g_hTopDc, m_hbitmap );
::ReleaseDC(NULL, hScreen);
}
#ifdef _WIN32_WCE
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpCmdLine, int nCmdShow)
#else
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR/*LPTSTR*/ lpCmdLine, int nCmdShow)
#endif
{
DWORD dwBegin = GetTickCount();
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc)); //与memset()函数功能相当
wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
//wc.style &= ~(WS_SYSMENU | WS_CAPTION) ;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = NULL; // LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IE));
wc.hCursor = 0;//LoadCursor(NULL, IDC_SIZEALL);
wc.hbrBackground = 0;//(HBRUSH)GetSysColorBrush(LTGRAY_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = L"SCREEN_PROTECT_WINDOW_CLASS";
if(!RegisterClass(&wc))
return 0;
DWORD dwStyle =
WS_VISIBLE /* | WS_THICKFRAME*/;
g_hWnd = ::CreateWindowEx(0,
L"SCREEN_PROTECT_WINDOW_CLASS",
L"SCREEN_PROTECT_WINDOW",
dwStyle,
0,
0,
1024,
512,
//rcArea.right - rcArea.left - 40,
//rcArea.bottom - rcArea.top - 30,
NULL, NULL, hInst, 0);
if (!g_hWnd)
{
DWORD err = GetLastError();
return FALSE;
}
MSG Msg;
//加载启动画面
#ifdef _WIN32_WCE
loadBMP("\\usb disk\\test16\\2.bmp");
#else
loadBMP("c:\\2.bmp");
#endif
CreateMemDC();
m_lpBitsInfo = (BITMAPINFO *)g_fileData;
m_lpBitsInfo->bmiHeader.biHeight = 512;
m_lpBitsInfo->bmiHeader.biWidth = 1024;
BOOL b = StretchDIBits(g_hTopDc,
0, 0,
m_width,
m_height,
0, 0,
m_width,
m_height,
tmpData, m_lpBitsInfo, DIB_RGB_COLORS, SRCCOPY );
::ShowWindow(g_hWnd,SW_SHOWNORMAL);
::UpdateWindow(g_hWnd);
DWORD dwEnd = GetTickCount();
dwBegin = dwEnd - dwBegin;
wchar_t buf[10]={0};
_ltow(dwBegin,buf,10);
//MessageBox(0,buf,buf,0);
while (g_hWnd && GetMessage(&Msg, NULL, 0, 0))
{
if(Msg.message == WM_QUIT)
break;
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
exit:
return 0;
}
LRESULT MainWndProc(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
)
{
switch (Msg)
{
case WM_LBUTTONDBLCLK:
PostQuitMessage(0);
break;
case WM_PAINT:
{
HDC hdc = ::GetDC(g_hWnd);
HDC hScreen = hdc;
StretchBlt(hScreen, 0, 0,
m_width,
m_height,
g_hTopDc,
0, 0,
m_width,
m_height,
SRCCOPY);
//BitBlt(hdc,0,0,800,480,g_hTopDc,0,0,SRCCOPY);
ReleaseDC(g_hWnd,hdc);
}
break;
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}