//*****************************************************************************
//
// 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))
TransBmp.rar_pixel
版权申诉
184 浏览量
2022-09-23
22:35:24
上传
评论
收藏 484KB RAR 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+
最新资源
- 基于yolov8+dlib实现视觉识别的安全驾驶监测系统部署到jetson NX平台源码+模型.zip
- Qt框架+OpenCV+动态爱心+编程教学+520
- 基于opencv+yolov8实现目标追踪及驻留时长统计源码.zip
- 水稻病害基于Yolov8算法优化目标检测识别与AI辅助决策python源码+模型+使用说明.zip
- 海尔618算价表_七海5.20_16.00xlsx(1)(2).xlsx
- WebCrawler.scr
- 【计算机专业毕业设计】大学生就业信息管理系统设计源码.zip
- YOLO 数据集:8种路面缺陷病害检测【包含划分好的数据集、类别class文件、数据可视化脚本】
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于YOLOv8的FPS TPS AI自动锁定源码+使用步骤说明.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0