📄 scan_bmp.cpp
字号:
// Scan_Bmp.cpp: implementation of the Scan_Bmp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "math.h"
#include "Scan_Bmp.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Scan_Bmp::Scan_Bmp()
{
/* HANDLE m_Bmp_hBuf;
BYTE *m_Bmp_pBmpValue;
BITMAPINFOHEADER *m_Bmp_pBmpInfo;
RGBQUAD *m_Bmp_pRGB;
HDRAWDIB m_Bmp_hDrawDib;
BYTE *m_Bmp_pHuge;
*/
m_Bmp_hBuf = NULL;
m_Bmp_pBmpValue = NULL;//图像的值
m_Bmp_pBmpInfo = NULL;
m_Bmp_pRGB = NULL;
m_Bmp_hDrawDib = NULL;
m_Bmp_pHuge = NULL;
m_Bmp_pBmpValue=NULL;
m_Bmp_hBuf = GlobalAlloc(GHND, 1064L);
ASSERT(m_Bmp_hBuf != NULL);
m_Bmp_pHuge = (BYTE *)GlobalLock(m_Bmp_hBuf);
m_Bmp_pBmpInfo = (BITMAPINFOHEADER *)m_Bmp_pHuge;
m_Bmp_pBmpInfo->biSize = sizeof(BITMAPINFOHEADER);
m_Bmp_pBmpInfo->biXPelsPerMeter = 0;
m_Bmp_pBmpInfo->biYPelsPerMeter = 0;
m_Bmp_pBmpInfo->biPlanes = 1;
m_Bmp_pBmpInfo->biBitCount = 8;
m_Bmp_pBmpInfo->biCompression = 0;
m_Bmp_pBmpInfo->biClrUsed = 256;
m_Bmp_pBmpInfo->biClrImportant = 256;
m_Bmp_pRGB = (RGBQUAD *)(m_Bmp_pHuge + sizeof(BITMAPINFOHEADER));
for(int i = 0 ; i < 256 ; i++)
{
(m_Bmp_pRGB + i)->rgbBlue = i;
(m_Bmp_pRGB + i)->rgbGreen = i;
(m_Bmp_pRGB + i)->rgbRed = i;
(m_Bmp_pRGB + i)->rgbReserved = 0;
}
m_Bmp_hDrawDib = DrawDibOpen();
ASSERT(m_Bmp_hDrawDib != NULL);
}
Scan_Bmp::~Scan_Bmp()
{
if(m_Bmp_hBuf != NULL)
{
GlobalUnlock(m_Bmp_hBuf);
GlobalFree(m_Bmp_hBuf);
m_Bmp_hBuf = NULL;
}
//m_Bmp_pBmpValue
if( m_Bmp_pBmpValue != NULL)
{
GlobalFree( m_Bmp_pBmpValue );
m_Bmp_pBmpValue = NULL;
}
//m_Bmp_pBmpInfo
m_Bmp_pBmpInfo = NULL;
//m_Bmp_pRGB
m_Bmp_pRGB = NULL;
//m_Bmp_pHuge
m_Bmp_pHuge = NULL;
//m_Bmp_hDrawDib
if( m_Bmp_hDrawDib != NULL)
{
DrawDibClose(m_Bmp_hDrawDib);
m_Bmp_hDrawDib = NULL;
}
}
void Scan_Bmp::DrawDib(CDC* pDC, int scale, WORD x, WORD y)
{
if(m_Bmp_pBmpValue == NULL)
{
AfxMessageBox("没有图像内容!");
return;
}
m_Width = ((BITMAPINFOHEADER *)m_Bmp_pHuge)->biWidth;
m_Height = ((BITMAPINFOHEADER *)m_Bmp_pHuge)->biHeight;
DrawDibRealize(m_Bmp_hDrawDib, pDC->GetSafeHdc(), TRUE);
DrawDibDraw(m_Bmp_hDrawDib, pDC->GetSafeHdc(),
x,
y,
m_Width / scale,
m_Height / scale,
(BITMAPINFOHEADER*)m_Bmp_pHuge,
(LPVOID) (m_Bmp_pBmpValue),
0,
0,
m_Width,
m_Height,
DDF_BACKGROUNDPAL
);
}
void Scan_Bmp::ReAlloc()
{
}
void Scan_Bmp::DrawDib(CDC * pDC, short width, short height)
{
if(m_Bmp_pBmpValue == NULL)
{
AfxMessageBox("没有图像内容!");
return;
}
m_Width = ((BITMAPINFOHEADER *)m_Bmp_pHuge)->biWidth;
m_Height = ((BITMAPINFOHEADER *)m_Bmp_pHuge)->biHeight;
DrawDibRealize(m_Bmp_hDrawDib, pDC->GetSafeHdc(), TRUE);
DrawDibDraw(m_Bmp_hDrawDib, pDC->GetSafeHdc(),
0,
0,
width,
height,
(BITMAPINFOHEADER*)m_Bmp_pHuge,
(LPVOID) (m_Bmp_pBmpValue),
0,
0,
m_Width,
m_Height,
DDF_BACKGROUNDPAL
);
}
BOOL Scan_Bmp::WriteFile(CString filename)
{
BITMAPFILEHEADER head;
CFile ffile;
head.bfType = 'MB';
head.bfSize = 14 + 1064 + m_Bmp_pBmpInfo->biWidth*m_Bmp_pBmpInfo->biHeight;
head.bfReserved1 = 0;
head.bfReserved2 = 0;
head.bfOffBits = 14 + 1064;
if(!ffile.Open(filename, CFile::modeCreate | CFile::modeWrite))
{
CString mess;
mess.Format("Scan_Bmp写文件 %s 出错", filename);
AfxMessageBox(mess);
return FALSE;
}
ffile.Write(&head, sizeof(BITMAPFILEHEADER));
ffile.Write(m_Bmp_pHuge, 1064);
ffile.Write(m_Bmp_pBmpValue, m_Bmp_pBmpInfo->biWidth*m_Bmp_pBmpInfo->biHeight);
ffile.Flush();
ffile.Close();
return TRUE;
}
BOOL Scan_Bmp::ReadFile(CString filename)
{
BITMAPFILEHEADER head;
CFile ffile;
if(!ffile.Open(filename, CFile::modeRead))
{
CString mess;
mess.Format("Scan_Bmp打开文件 %s 出错", filename);
AfxMessageBox(mess);
return FALSE;
}
ffile.Read(&head, sizeof(BITMAPFILEHEADER));
ffile.Read(m_Bmp_pHuge, 1064);
if(m_Bmp_pBmpValue)
GlobalFree(m_Bmp_pBmpValue);
m_Width = ((BITMAPINFOHEADER *)m_Bmp_pHuge)->biWidth;
m_Height = ((BITMAPINFOHEADER *)m_Bmp_pHuge)->biHeight;
m_Bmp_pBmpValue=(BYTE *)GlobalAlloc(GPTR, m_Width*m_Height);
ffile.Read(m_Bmp_pBmpValue, m_Width*m_Height);
ffile.Close();
return TRUE;
}
void Scan_Bmp::ScaleImage(Scan_Bmp &bmp, int width, int height)
{
bmp.m_Bmp_pBmpInfo->biWidth = width;
bmp.m_Bmp_pBmpInfo->biHeight = height;
bmp.m_Bmp_pBmpInfo->biSizeImage = bmp.m_Bmp_pBmpInfo->biWidth * height;
bmp.m_Height = height;
bmp.m_Width = bmp.m_Bmp_pBmpInfo->biWidth;
int newwidth = bmp.m_Bmp_pBmpInfo->biWidth;
if(bmp.m_Bmp_pBmpValue != NULL)
{
GlobalFree( bmp.m_Bmp_pBmpValue );
bmp.m_Bmp_pBmpValue = NULL;
}
bmp.m_Bmp_pBmpValue = (BYTE *)GlobalAlloc(GPTR, width * height);
CString str;
if(bmp.m_Bmp_pBmpValue == NULL)
{
AfxMessageBox("内存不够(Scale image Scan_Bmp)");
return;
}
Resample( bmp, width, height );
}
void Scan_Bmp::Resample(Scan_Bmp &bmp, int width, int height)
{
int RspWidth , RspHeight;
unsigned char *hpSource, *hpTarget;
RspWidth = width;
RspHeight = height;
hpSource = m_Bmp_pBmpValue;
hpTarget = bmp.m_Bmp_pBmpValue;
//BITMAPCOREHEADER SourceBMPInfo,unsigned char *hpSource,
float dx=(float)(RspWidth)/(float)(m_Bmp_pBmpInfo->biWidth);
//Actually enlarge scale
float dy=(float)(RspHeight)/(float)(m_Bmp_pBmpInfo->biHeight) ;
//Actually enlarge scale
int xpow = (int)((dx>=1.0f) ? 128 :128.0f/dx); //把dx化为整数的比例因子.*
int ypow =(int)( (dy>=1.0f) ? 128 :128.0f/dy); //把dy化为整数的比例因子.*
int scale_x = (int)(log(xpow)/log(2.0f)+0.5f);
int scale_y = (int)(log(ypow)/log(2.0f)+0.5f);
int scale_xpow =(int)( pow(2.0f,scale_x));
int scale_ypow =(int) (pow(2.0f,scale_y));
long Delt_x =(long) (dx *scale_xpow); // 把dx化为整数.
long Delt_y =(long)( dy *scale_ypow); // 把dy化为整数.
long xx16 = (long)( (float)(Delt_x)/16.0f+0.5f );
long yy16 = (long)( (float)(Delt_y)/16.0f+0.5f );
long RowBytesIn=(m_Bmp_pBmpInfo->biWidth + 3L ) / 4L * 4L;
long RowBytesOut=(RspWidth + 3L) / 4L * 4L;
//----------------------------------------------------------------
long HeightScaleY= RspHeight*scale_ypow;
long WidthScaleX = RspWidth*scale_xpow;
long AllBytesIn = RowBytesIn * m_Bmp_pBmpInfo->biHeight;
long AllWidthIn = m_Bmp_pBmpInfo->biWidth;
int CX,CX1,CY,CY1;
long WidthInX1,WidthInX2;
long AllBytesInX1,AllBytesInX2,AllBytesOut=0;
for(long HeightOut=0;HeightOut < HeightScaleY;HeightOut+=scale_ypow)
{
CY = (int)(HeightOut % Delt_y / yy16) ;
CY1 = 16 - CY;
AllBytesInX1 = HeightOut / Delt_y * RowBytesIn;
AllBytesInX2 = AllBytesInX1 + RowBytesIn;
if( AllBytesInX2>= AllBytesIn)
{
AllBytesInX1 = AllBytesIn-RowBytesIn;
AllBytesInX2 = AllBytesInX1;
}
for(long WidthOut=0;WidthOut<WidthScaleX;WidthOut+=scale_xpow)
{
CX = (int)(WidthOut % Delt_x /xx16) ;
CX1 = 16 - CX;
WidthInX1 = WidthOut / Delt_x;
WidthInX2 = WidthInX1+1;
if( WidthInX2 >= AllWidthIn )
{
WidthInX1 = AllWidthIn-1;
WidthInX2 = WidthInX1;
}
*( hpTarget + AllBytesOut + (WidthOut>>scale_x) )
= ( *(hpSource+AllBytesInX1+WidthInX1) *CX1*CY1
+ *(hpSource+AllBytesInX2+WidthInX2) *CX *CY
+ *(hpSource+AllBytesInX2+WidthInX1) *CX1*CY
+ *(hpSource+AllBytesInX1+WidthInX2) *CX *CY1 )>>8;
}
AllBytesOut += RowBytesOut;
}
}
void Scan_Bmp::ChangeLight(unsigned short light)
{
RGBQUAD* pRGB=m_Bmp_pRGB ;
for(int i=0;i<256;i++)
{
if(light > 128)
{
if(i + (light - 128) >= 255)
{
(pRGB+i)->rgbBlue = 255;
(pRGB+i)->rgbGreen=255;
(pRGB+i)->rgbRed=255;
(pRGB+i)->rgbReserved=0;
}else
{
(pRGB+i)->rgbBlue = i + (light - 128);
(pRGB+i)->rgbGreen=i + (light - 128);
(pRGB+i)->rgbRed=i + (light - 128);
(pRGB+i)->rgbReserved=0;
}
}else
{
if(i + (light - 128) < 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -