📄 jdib.cpp
字号:
//*****************************************************************************
//
// JDib.cpp
//
//=============================================================================
//
// Copyright:
//
// Author: 老魏
//
// Date: 1999.04.06
//
// Description: CJDib 类实现文件
//
// Side Effects:
//
// Class:
//
// Function:
//
// Notes:
//
// Update:
//
// Date Name Description
//
// ======== ===================================================================
// Known restrictions:
//
// Known bugs:
//
//*****************************************************************************
#include "stdafx.h"
#include "JDib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CJDib
//**************************************************************
// Name: CJDib()
//
// Author: 老魏
//
// Date: 1999.04.06
//
// Description: 构造函数, 初始化成员变量
//
// Arguments: 无
//
// Return: 无
//
// Side Effects: no
//
// Notes:
//
// Known restrictions:
//
// Err NO. Date Name Description
//
// Update:
// Date Name Description
// ======== ============ =============================
//
//****************************************************
CJDib::CJDib()
{
m_bSetPalette = TRUE;
m_lpBMFileHeader = NULL;
m_lpBMInfoHeader = NULL;
m_lpLogPalette = NULL;
m_nBytesPerLine = 0;
m_pData = NULL;
m_pFileBuffer = NULL;
}
//**************************************************************
// Name: ~CJDib()
//
// Author: 老魏
//
// Date: 1999.04.06
//
// Description: 析构函数, 释放不为空的数据和调色盘内存空间
//
// Arguments: 无
//
// Return: 无
//
// Side Effects: no
//
// Notes:
//
// Known restrictions:
//
// Err NO. Date Name Description
//
// Update:
// Date Name Description
// ======== ============ =============================
//
//****************************************************
CJDib::~CJDib()
{
if (m_lpLogPalette)
{
LocalFree(m_lpLogPalette);
}
if (m_pFileBuffer)
{
LocalFree(m_pFileBuffer);
}
}
//**************************************************************
// Name: Read(CString strBMPName)
//
// Author: 老魏
//
// Date: 1999.04.06
//
// Description: 读取 BMP文件数据, 各种指针赋值和计算逻辑调色盘
// 不支持 JPEG 或 PNG格式
//
// Arguments:
// CString strBMPName: BMP文件名
//
// Return:
// TRUE: 成功
// FALSE: 失败
//
// Side Effects: no
//
// Notes:
//
// Known restrictions:
//
// Err NO. Date Name Description
//
// Update:
// Date Name Description
// ======== ============ =============================
//
//****************************************************
BOOL CJDib::Read()
{
CFile File;
//按只读方式打开文件
BOOL bResult = File.Open(m_strBMPName, CFile::modeRead);
if (!bResult)
{
CString strErrorMessage;
strErrorMessage = "Open file:" + m_strBMPName + "fail!";
AfxMessageBox(strErrorMessage);
return FALSE;
}
//取得文件长度
int nFileLength = File.GetLength();
//按文件长度申请空间
m_pFileBuffer = (char *)LocalAlloc(LPTR, nFileLength);
if (!m_pFileBuffer)
{
AfxMessageBox("Memory alloc fail!");
return FALSE;
}
//读取文件所有数据
int nBytesRead = File.Read(m_pFileBuffer, nFileLength);
if (nBytesRead != nFileLength)
{
AfxMessageBox("Read file content fail!");
return FALSE;
}
//文件头指针赋值
m_lpBMFileHeader = (LPBITMAPFILEHEADER)m_pFileBuffer;
//判断文件类型
if (m_lpBMFileHeader->bfType != 0x4d42) // 'BM'
{
CString strErrorMessage;
strErrorMessage = "File:" + m_strBMPName + "is not a valid BMP file!";
AfxMessageBox(strErrorMessage);
return FALSE;
}
//信息头指针赋值
m_lpBMInfoHeader = (LPBITMAPINFOHEADER)(m_pFileBuffer + sizeof(BITMAPFILEHEADER));
//计算每行占用的字节数 (m_lpBMInfoHeader的biSizeImage成员有时为空不可用)
//m_nBytesPerLine = m_lpBMInfoHeader->biSizeImage / m_lpBMInfoHeader->biHeight;
m_nBytesPerLine = m_lpBMInfoHeader->biWidth * m_lpBMInfoHeader->biBitCount / 8;
if (m_nBytesPerLine % 4 != 0)
m_nBytesPerLine = (m_nBytesPerLine / 4 + 1) * 4;
//数据指针赋值
m_pData = m_pFileBuffer + m_lpBMFileHeader->bfOffBits;
//为调色盘申请空间
m_lpLogPalette = (LPLOGPALETTE)LocalAlloc(LPTR, sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY));
m_lpLogPalette->palVersion = 0x300;
//判断是否需使用调色盘
switch (m_lpBMInfoHeader->biBitCount)
{
case 0: //JPEG 或 PNG 格式
m_bSetPalette = FALSE;
break;
case 1:
m_lpLogPalette->palNumEntries = 2;
m_bSetPalette = TRUE;
break;
case 4:
m_lpLogPalette->palNumEntries = 16;
m_bSetPalette = TRUE;
break;
case 8:
m_lpLogPalette->palNumEntries = 256;
m_bSetPalette = TRUE;
break;
case 16:
// 16bit, if biCompression == BI_RGB, there's no color table
if(m_lpBMInfoHeader->biCompression == BI_RGB)
{
m_bSetPalette = FALSE;
}
else
{
AfxMessageBox("This BMP type is unrecognizable!");
}
break;
case 24: // 24bit
case 32: // 32bit
m_bSetPalette = FALSE;
break;
default:
AfxMessageBox("Color number not valid!");
return FALSE;
}
//申请临时空间以处理调色盘
char *pPalette = m_pFileBuffer + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
if (!pPalette)
{
AfxMessageBox("Memory alloc fail!");
return FALSE;
}
RGBQUAD rgbQuad[256];
//调色盘赋值
memcpy(rgbQuad, pPalette, sizeof(PALETTEENTRY) * m_lpLogPalette->palNumEntries);
for (int i = 0; i < m_lpLogPalette->palNumEntries; i ++)
{
m_lpLogPalette->palPalEntry[i].peBlue = rgbQuad[i].rgbBlue;
m_lpLogPalette->palPalEntry[i].peGreen = rgbQuad[i].rgbGreen;
m_lpLogPalette->palPalEntry[i].peRed = rgbQuad[i].rgbRed;
m_lpLogPalette->palPalEntry[i].peFlags = rgbQuad[i].rgbReserved;
}
return TRUE;
}
/***************************************************************
* Name: ConvertToText(CString strBMPName)
* Description: 将给定 BMP文件转换为文本文件, 仅支持 256色格式和真彩格式
* Arguments:
* Return: TRUE if success, otherwise FALSE.
* Side Effects: no
* Notes:
* Known restrictions:
* Err NO. Date Name Description
* Update:
* Date Name Description
****************************************************************/
BOOL CJDib::ConvertToText()
{
CStdioFile TxtFile;
//创建文本文件
if (!TxtFile.Open(m_strTXTName, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
{
CString strErrorMessage;
strErrorMessage = "Create txt file:" + m_strTXTName + "fail!";
AfxMessageBox(strErrorMessage);
return FALSE;
}
//读取 BMP文件数据
if (!Read())
{
return FALSE;
}
TxtFile.WriteString("/// Copyright (c) Besta(XI'AN) Corporation. All rights reserved.\n");
TxtFile.WriteString("/// Abstract: This file contains a 16 bpp bitmap image displayed during boot-up.\n");
TxtFile.WriteString("/// Generated by Jack Zhao!\n\n");
TxtFile.WriteString("#include <windows.h> \n\n");
TxtFile.WriteString("const USHORT ScreenBitmap[] = {\n\t");
//取得 BMP数据指针
BYTE *pData = (BYTE *)m_pData;
BYTE *pLine = pData;
int num = 0;
for (int i = m_lpBMInfoHeader->biHeight - 1; i >= 0; i--)
{
//计算每行的数据指针
pLine = pData + i * m_nBytesPerLine;
CString strLineText, strTemp;
for (int j = 0; j < m_lpBMInfoHeader->biWidth; j++)
{
int nRed, nGreen, nBlue;
DWORD dwColor;
WORD wColor;
//计算当前象素的 RGB三分量的值
switch (m_lpBMInfoHeader->biBitCount)
{
case 24:
nRed = *pLine++;
nGreen = *pLine++;
nBlue = *pLine++;
dwColor = ((DWORD)nRed << 16) | ((DWORD)nGreen << 8) | nBlue;
strTemp.Format("0x%08x, ", dwColor);
break;
case 16:
wColor = *(USHORT *)pLine;
wColor = (wColor & 0x3ff) | ( (wColor & 0x7c00) << 1 ); // X+555 ==> 565
pLine += 2;
strTemp.Format("0x%04x, ", wColor);
break;
case 8:
nRed = m_lpLogPalette->palPalEntry[*pLine].peRed;
nGreen = m_lpLogPalette->palPalEntry[*pLine].peGreen;
nBlue = m_lpLogPalette->palPalEntry[*pLine].peBlue;
pLine++;
AfxMessageBox("8 bits Color output not supported!");
break;
default:
AfxMessageBox("Color bits not supported!");
return FALSE;
}
strLineText += strTemp;
num++;
if( !(num % 8) )
{
if( (j == (m_lpBMInfoHeader->biWidth-1)) && (i == 0) )
{
strLineText += "\n";
}
else
{
strLineText += "\n\t";
}
}
}
TxtFile.WriteString(strLineText);
}
TxtFile.WriteString("};\n");
TxtFile.Close();
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -