📄 bk_2_pic.cpp
字号:
// Pic.cpp: implementation of the CPic class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Bxt.h"
#include "Pic.h"
#include "jpegfile.h"
#include "bmpfile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPic::CPic()
{
m_buf = NULL;
imageID = 0;
zoom = 0;
}
CPic::~CPic()
{
if (NULL != m_buf)
{
delete [] m_buf;
m_buf = NULL;
}
if (1 != flag)
DeleteObject(pScrnPalette);
}
void
CPic::Close()
{
if (m_buf!=NULL)
{
delete [] m_buf;
m_buf=NULL;
}
if(flag!=1)
DeleteObject(pScrnPalette);
}
void CPic::loadjpgGry( char *filename )
{
if (NULL != m_buf)
{
delete [] m_buf;
m_buf = NULL;
}
// CString fmark = _T(filename);
// fmark = fmark.Left(fmark.GetLength() - 3);
// fmark += _T("mrk");
// mark.Open(fmark.GetBuffer(fmark.GetLength()));
// CString fmeas = _T(filename);
// fmeas = fmeas.Left(fmeas.GetLength() - 3);
// fmeas += _T("mes");
// meas.LoadFile(fmeas.GetBuffer(fmeas.GetLength()));
//m_buf=JpegFile::JpegFileToRGB(CString(filename), &m_width, &m_height);
m_buf=JpegFile::JpegFileToGry(CString(filename), &m_width, &m_height);
//JpegFile::BGRFromRGB(m_buf, m_width, m_height);
JpegFile::VertFlipBuf(m_buf, m_width , m_height);
m_filename = _T(filename);
fileType = 1;
flag = 0;//gray;
pScrnPalette = new CPalette;
LOGPALETTE *lplgpl;
BYTE Palbuf[2048];
lplgpl = (LOGPALETTE *)Palbuf;
lplgpl->palNumEntries = 256;//theDC->GetDeviceCaps(SIZEPALETTE);
lplgpl->palVersion = 0x300;
for( int k = 0; k < (int)lplgpl->palNumEntries; ++k){
lplgpl->palPalEntry[k].peRed = (BYTE)k;
lplgpl->palPalEntry[k].peGreen = (BYTE)k;
lplgpl->palPalEntry[k].peBlue = (BYTE)k;
lplgpl->palPalEntry[k].peFlags = PC_NOCOLLAPSE;
}
pScrnPalette->CreatePalette(lplgpl);
}
void
CPic::loadjpg(char *filename)
{
if (NULL != m_buf)
{
delete [] m_buf;
m_buf = NULL;
}
// CString fmark = _T(filename);
// fmark = fmark.Left(fmark.GetLength() - 3);
// fmark += _T("mrk");
// mark.Open(fmark.GetBuffer(fmark.GetLength()));
// CString fmeas = _T(filename);
// fmeas = fmeas.Left(fmeas.GetLength() - 3);
// fmeas += _T("mes");
// meas.LoadFile(fmeas.GetBuffer(fmeas.GetLength()));
// read to buffer tmp
m_buf = JpegFile::JpegFileToRGB(CString(filename), &m_width, &m_height);
//////////////////////
// set up for display
// do this before DWORD-alignment!!!
// this works on packed (not DWORD-aligned) buffers
// swap red and blue for display
JpegFile::BGRFromRGB(m_buf, m_width, m_height);
// vertical flip for display
JpegFile::VertFlipBuf(m_buf, m_width * 3, m_height);
m_filename = _T(filename);
fileType = 1;
flag = 1; // color;
}
void CPic::DrawFilt(CDC *theDC,int x,int y,int w,int h)
{
if(m_buf==NULL)return;
int ff[256];
for(int j=0;j<256;j++)
ff[j]=0;
for (int hh = 0; hh < (int)m_height; hh++)
{
for (int ww = 0; ww < (int)m_width; ww += flag * 2 + 1)
{
ff[m_buf[hh*m_width*(flag*2+1)+ww]]++;
}
}
float max=0;
for (int i = 0; i < 256; i++)
{
if (max < ff[i]) max = (float)ff[i];
}
int avr = m_width * m_height / 256;
theDC->FillSolidRect(x,y,w,h,RGB(240,240,240));
theDC->MoveTo(x,y);
for(i=0;i<256;i++)
{
if(ff[i]>avr)
theDC->LineTo(x+(i+1)*w/256,0);
else
theDC->LineTo(x+(i+1)*w/256,h-ff[i]*h/avr);
//theDC->LineTo((i+1)*w/256,h-ff[i]*h/max);
}
//theDC->MoveTo(0,0);
//theDC->LineTo(w,h);
}
void CPic::ZoomBMP(CDC *theDC,int x,int y,int w,int h,int dx,int dy)
{
//zoom = zoompic;
if (m_buf==NULL)
{
CBrush bs;
bs.CreateSolidBrush(RGB(100,100,100));
CRect rf(x,y,x+w,y+h);
theDC->FillRect(&rf,&bs);
return;
}
if(zoom==0)
{
DrawBMP(theDC,x,y,w,h,0);//,cur);
return;
}
if(ddx<=0 && ddy<=0)return;
sx -= int(dx/zoom);
sy -= int(dy/zoom);
if(sx<0 && sy<0)
{
sx=0;sy=0;
return;
}
if(sx>ddx && sy>ddy)
{
sx=ddx;
sy=ddy;
return;
}
if(sx<0)sx=0;
if(sx>ddx)sx=ddx;
if(sy<0)sy=0;
if(sy>ddy)sy=ddy;
if (theDC!=NULL) {
BYTE *tmp;
// DWORD-align for display
tmp = JpegFile::MakeDwordAlignedBuf(m_buf,
m_width,
m_height,
&m_widthDW,flag);
// set up a DIB
/*BITMAPINFOHEADER bmiHeader;
bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmiHeader.biWidth = m_width;
bmiHeader.biHeight = m_height;
bmiHeader.biPlanes = 1;
bmiHeader.biBitCount = 24;
bmiHeader.biCompression = BI_RGB;
bmiHeader.biSizeImage = 0;
bmiHeader.biXPelsPerMeter = 0;
bmiHeader.biYPelsPerMeter = 0;
bmiHeader.biClrUsed = 0;
bmiHeader.biClrImportant = 0;*/
BYTE *tempxx=new BYTE[sizeof(BITMAPINFOHEADER)+256*4];
if(tempxx==NULL) {AfxMessageBox("Out of Memory!");return;}
BITMAPINFO *BMIInfo = (BITMAPINFO *) tempxx;
memset(BMIInfo,0,sizeof(BITMAPINFOHEADER)+256*4);
BMIInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BMIInfo->bmiHeader.biWidth = m_width;
BMIInfo->bmiHeader.biHeight = m_height;
BMIInfo->bmiHeader.biPlanes = 1;
BMIInfo->bmiHeader.biBitCount = 8;
BMIInfo->bmiHeader.biCompression = BI_RGB;
BMIInfo->bmiHeader.biSizeImage = 0;
BMIInfo->bmiHeader.biXPelsPerMeter = 0;
BMIInfo->bmiHeader.biYPelsPerMeter = 0;
BMIInfo->bmiHeader.biClrUsed = 0;
BMIInfo->bmiHeader.biClrImportant = 0;
//theDC->FillSolidRect(x,y,w,h,RGB(240,240,240));
CPalette *oldp;
if(flag==1)
{
BMIInfo->bmiHeader.biBitCount = 24;
}
else
{
BMIInfo->bmiHeader.biBitCount = 8;
oldp = theDC->SelectPalette(pScrnPalette, FALSE);
theDC->RealizePalette();
theDC->SetStretchBltMode(COLORONCOLOR);
//RGBQUAD* mtt=(RGBQUAD*)(BMIInfo->bmiColors);
//BMIInfo->bmiColors = new RGBQUAD[256];
for( int k = 0; k < 256; ++k){
BMIInfo->bmiColors[k].rgbBlue = (BYTE)k;
BMIInfo->bmiColors[k].rgbGreen = (BYTE)k;
BMIInfo->bmiColors[k].rgbRed = (BYTE)k;
BMIInfo->bmiColors[k].rgbReserved = 0;
}
}
int bw = int(m_width * zoom);
int bh = int(m_height * zoom);
int tsx,tsy;
if(w>bw)
{
x+=(w-bw)/2;
w=bw;
tsx=sx=0;
bw=BMIInfo->bmiHeader.biWidth;
}
else
{
//sx = sx +dx;
//if(sx<0) sx = 0;
//if( (sx+w)>bw ) sx = bw-w;
tsx = int(sx / zoom);
bw = int(w / zoom);
}
if(h>bh)
{
y+=(h-bh)/2;
h=bh;
tsy=sy=0;
bh=BMIInfo->bmiHeader.biHeight;
}
else
{
//sy = sy +dy;
//if(sy<0) sy = 0;
//if( (sy+h)>bh ) sy = bh -h;
bh = int(h / zoom);
tsy = int(sy / zoom);
tsy = int(BMIInfo->bmiHeader.biHeight - bh - tsy);
}
theDC->SetStretchBltMode(COLORONCOLOR);
int lines = StretchDIBits(theDC->m_hDC,
x, y,
w,
h,
tsx,tsy,
bw,
bh,
tmp,
BMIInfo,
DIB_RGB_COLORS,
SRCCOPY);
if(flag!=1)
{
theDC->SelectPalette(oldp,FALSE);
theDC->RealizePalette();
theDC->SetStretchBltMode(COLORONCOLOR);
}
delete [] tmp;
if(imageID>0)
{
char ids[5];
sprintf(ids,"%d",imageID);
theDC->SetTextColor(RGB(255,255,255));
theDC->SetBkColor(RGB(0,0,255));
theDC->TextOut(10,10,ids);
float zz=(float)w/bw*100;
sprintf(ids,"%.0f%%",zz);
theDC->TextOut(10,50,ids);
}
delete [] tempxx;
}
}
void
CPic::DrawBMP(CDC *theDC, int x, int y, int w, int h, int stretch, bool cur)
{
// 设备指针为空直接返回
if (NULL == theDC) return;
// 如果没有图象,直接填充一个矩形区域并返回
if (NULL == m_buf)
{
theDC->FillSolidRect(x, y, w, h, RGB(100, 100, 100));
return;
}
// 创建双字节对齐的缓冲
BYTE *tmp = JpegFile::MakeDwordAlignedBuf(m_buf, m_width, m_height, &m_widthDW, flag);
// 为位图信息头和 256 色调色板分配内存
BYTE *tempxx = new BYTE[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
if (NULL == tempxx)
{
AfxMessageBox("Out of Memory!");
return;
}
// BITMAPINFO结构为
BITMAPINFO *BMIInfo = (BITMAPINFO *)tempxx;
memset(BMIInfo, 0, sizeof(BITMAPINFOHEADER) + 256 * 4);
BMIInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BMIInfo->bmiHeader.biWidth = m_width;
BMIInfo->bmiHeader.biHeight = m_height;
BMIInfo->bmiHeader.biPlanes = 1;
BMIInfo->bmiHeader.biBitCount = 8;
BMIInfo->bmiHeader.biCompression = BI_RGB;
BMIInfo->bmiHeader.biSizeImage = 0;
BMIInfo->bmiHeader.biXPelsPerMeter = 0;
BMIInfo->bmiHeader.biYPelsPerMeter = 0;
BMIInfo->bmiHeader.biClrUsed = 0;
BMIInfo->bmiHeader.biClrImportant = 0;
CPalette *oldp = NULL;
if (1 == flag)
{
BMIInfo->bmiHeader.biBitCount = 24;
}
else
{
BMIInfo->bmiHeader.biBitCount = 8;
oldp = theDC->SelectPalette(pScrnPalette, FALSE);
theDC->RealizePalette();
theDC->SetStretchBltMode(COLORONCOLOR);
for (int k = 0; k < 256; k++)
{
BMIInfo->bmiColors[k].rgbBlue = (BYTE)k;
BMIInfo->bmiColors[k].rgbGreen = (BYTE)k;
BMIInfo->bmiColors[k].rgbRed = (BYTE)k;
BMIInfo->bmiColors[k].rgbReserved = 0;
}
}
if (cur)
{
CPen pen;
CPen *oldpen = NULL;
pen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
oldpen = theDC->SelectObject(&pen);
theDC->Rectangle(x, y, x + w + 1, y + h + 1);
theDC->SelectObject(oldpen);
DeleteObject(&pen);
}
else
{
CPen pen;
CPen *oldpen = NULL;
pen.CreatePen(PS_SOLID, 2, RGB(240, 240, 240));
oldpen = theDC->SelectObject(&pen);
theDC->Rectangle(x, y, x + w + 1, y + h + 1);
theDC->SelectObject(oldpen);
DeleteObject(&pen);
}
if (0 == stretch)
{
theDC->FillSolidRect(x, y, w, h, RGB(240, 240, 240));
if (w > BMIInfo->bmiHeader.biWidth && h > BMIInfo->bmiHeader.biHeight)
{
float t1;
float t2;
t1 = (float)w / (float)h;
t2 = (float)BMIInfo->bmiHeader.biWidth / (float)BMIInfo->bmiHeader.biHeight;
if (t1 > t2)
{
x += int((w - t2 * h) / 2);
w = int(t2 * h);
}
else
{
y += int((h - w / t2) / 2);
h = int(w / t2);
}
}
else
{
float t1,t2;
t1=(float)w/(float)h;
t2=(float)BMIInfo->bmiHeader.biWidth/(float)BMIInfo->bmiHeader.biHeight;
if(t1>t2)
{
x += int((w - t2 * h) / 2);
w = int(t2 * h);
}
else
{
y += int((h - w / t2) / 2);
h = int(w / t2);
}
}
}
theDC->SetStretchBltMode(COLORONCOLOR);
int lines = StretchDIBits(theDC->m_hDC,
x, y,
w,
h,
0,0,
BMIInfo->bmiHeader.biWidth,
BMIInfo->bmiHeader.biHeight,
tmp,
BMIInfo,
DIB_RGB_COLORS,
SRCCOPY);
if (1 != flag)
{
theDC->SelectPalette(oldp,false);
theDC->RealizePalette();
theDC->SetStretchBltMode(COLORONCOLOR);
}
// meas.ReDraw(theDC->GetSafeHdc());
delete [] tmp;
tmp = NULL;
//delete pScrnPalette;
if (imageID > 0)
{
char ids[5];
sprintf(ids,"%d",imageID);
theDC->SetTextColor(RGB(255,255,255));
theDC->SetBkColor(RGB(0,0,255));
theDC->TextOut(10,10,ids);
float zz=(float)w/(BMIInfo->bmiHeader.biWidth)*100;
sprintf(ids,"%.0f%%",zz);
theDC->TextOut(10,50,ids);
}
delete [] tempxx;
tempxx = NULL;
}
BOOL
CPic::savejpg(char *filename)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -