?? 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(CString strBMPName)
{
CFile File;
//按只讀方式打開文件
BOOL bResult = File.Open(strBMPName, CFile::modeRead);
if (!bResult)
{
CString strErrorMessage;
strErrorMessage = "打開文件:" + strBMPName + "失敗 !";
AfxMessageBox(strErrorMessage);
return FALSE;
}
//取得文件長度
int nFileLength = File.GetLength();
//按文件長度申請空間
m_pFileBuffer = (char *)LocalAlloc(LPTR, nFileLength);
if (!m_pFileBuffer)
{
AfxMessageBox("申請必須的內存空間失敗 !");
return FALSE;
}
//讀取文件所有數據
int nBytesRead = File.Read(m_pFileBuffer, nFileLength);
if (nBytesRead != nFileLength)
{
AfxMessageBox("讀取文件內容失敗 !");
return FALSE;
}
//文件頭指針賦值
m_lpBMFileHeader = (LPBITMAPFILEHEADER)m_pFileBuffer;
//判斷文件類型
if (m_lpBMFileHeader->bfType != 0x4d42) // 'BM'
{
CString strErrorMessage;
strErrorMessage = "文件:" + strBMPName + "不是有效的 BMP文件 !";
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:
case 24:
case 32:
m_bSetPalette = FALSE;
break;
default:
AfxMessageBox("文件色彩數不可識別 !");
return FALSE;
}
//申請臨時空間以處理調色盤
char *pPalette = m_pFileBuffer + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
if (!pPalette)
{
AfxMessageBox("申請必須的內存空間失敗 !");
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)
//
// Author: 老魏
//
// Date: 1999.04.06
//
// Description: 將給定 BMP文件轉換為文本文件, 僅支持 256色格式和真彩格式
//
// Arguments:
// CString strBMPName: BMP文件名
//
// Return:
// TRUE: 成功
// FALSE: 失敗
//
// Side Effects: no
//
// Notes:
//
// Known restrictions:
//
// Err NO. Date Name Description
//
// Update:
// Date Name Description
// ======== ============ =============================
//
//****************************************************
BOOL CJDib::ConvertToText(CString strBMPName)
{
//字符調色盤
BYTE CharPalette[16];
CharPalette[0] = '#';
CharPalette[1] = 'M';
CharPalette[2] = '@';
CharPalette[3] = 'H';
CharPalette[4] = 'X';
CharPalette[5] = '$';
CharPalette[6] = '%';
CharPalette[7] = '+';
CharPalette[8] = '/';
CharPalette[9] = ';';
CharPalette[10] = ':';
CharPalette[11] = '=';
CharPalette[12] = '-';
CharPalette[13] = ',';
CharPalette[14] = '.';
CharPalette[15] = ' ';
CString strTxtName = strBMPName;
//計算文本文件名
strTxtName = strTxtName.Left(strTxtName.Find('.'));
strTxtName += ".txt";
CStdioFile TxtFile;
//創建文本文件
if (!TxtFile.Open(strTxtName, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
{
CString strErrorMessage;
strErrorMessage = "創建文本文件:" + strTxtName + "失敗 !";
AfxMessageBox(strErrorMessage);
return FALSE;
}
//讀取 BMP文件數據
if (!Read(strBMPName))
{
return FALSE;
}
TxtFile.WriteString("Generated by 老魏!\n");
//取得 BMP數據指針
BYTE *pData = (BYTE *)m_pData;
BYTE *pLine = pData;
for (int i = m_lpBMInfoHeader->biHeight - 1; i >= 0; i--)
{
//計算每行的數據指針
pLine = pData + i * m_nBytesPerLine;
CString strLineText;
for (int j = 0; j < m_lpBMInfoHeader->biWidth; j++)
{
int nRed, nGreen, nBlue, nValue;
//計算當前象素的 RGB三分量的值
switch (m_lpBMInfoHeader->biBitCount)
{
case 24:
nRed = *pLine++;
nGreen = *pLine++;
nBlue = *pLine++;
break;
case 8:
nRed = m_lpLogPalette->palPalEntry[*pLine].peRed;
nGreen = m_lpLogPalette->palPalEntry[*pLine].peGreen;
nBlue = m_lpLogPalette->palPalEntry[*pLine].peBlue;
pLine++;
break;
default:
AfxMessageBox("抱歉, 當前版本僅支持 8bit和24bit文件 !");
return FALSE;
}
//計算灰度值
nValue = (nRed * 30 + nGreen * 59 + nBlue * 11) / 100;
//轉換到文本
strLineText += CharPalette[nValue / 16];
}
//寫入文本文件
TxtFile.WriteString(strLineText);
TxtFile.WriteString("\n");
}
TxtFile.Close();
return TRUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -