📄 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(NULL != m_buf)
{
delete m_buf;
m_buf = NULL;
}
if(1 != flag) 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::JpegFileToGry(CString(filename), &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 = NULL;
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);
CreatePlate(1);
}
void
CPic::loadjpg(char *filename)
{
if (NULL != m_buf)
{
delete[] m_buf;
m_buf = NULL;
}
// 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 (NULL == m_buf) return;
int ff[256];
for (int j = 0; j < 256; j++) ff[j]=0;
for (int hh = 0; hh < m_height; hh++)
{
for (int ww = 0; ww < 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 = 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);
}
}
void
CPic::ZoomBMP(CDC *theDC, int x, int y, int w, int h, int dx, int dy)
{
if (NULL == m_buf)
{
CBrush bs;
bs.CreateSolidBrush(RGB(100, 100, 100));
CRect rf(x, y, x + w, y + h);
theDC->FillRect(&rf, &bs);
return;
}
if (0 == zoom)
{
DrawBMP(theDC, x, y, w, h, 0);
return;
}
if (ddx <= 0 && ddy <= 0) return;
sx -= dx/zoom;
sy -= 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 (NULL == theDC) return;
BYTE *tmp = NULL;
// DWORD-align for display
tmp = JpegFile::MakeDwordAlignedBuf(m_buf,
m_width,
m_height,
&m_widthDW,
flag);
BYTE *tempxx = new BYTE[sizeof(BITMAPINFOHEADER) + 256 * 4];
if (NULL == tempxx)
{
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;
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);
//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 = m_width * zoom;
int bh = m_height * zoom;
int tsx;
int 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 = sx / zoom;
bw = 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 = h / zoom;
tsy = sy / zoom;
tsy = 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 (1 != flag)
{
theDC->SelectPalette(oldp,FALSE);
theDC->RealizePalette();
theDC->SetStretchBltMode(COLORONCOLOR);
}
delete tmp;
tmp = NULL;
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;
tempxx = NULL;
}
void
CPic::DrawBMP(CDC *theDC, int x, int y, int w, int h, int stretch, bool cur)
{
if (NULL == m_buf)
{
theDC->FillSolidRect(x, y, w, h, RGB(100, 100, 100));
return;
}
BYTE *tmp = NULL;
if (NULL == theDC) return;
tmp = JpegFile::MakeDwordAlignedBuf(m_buf,
m_width,
m_height,
&m_widthDW,
flag);
BYTE *tempxx = new BYTE[sizeof(BITMAPINFOHEADER) + 256 * 4];
if (NULL == tempxx)
{
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;
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);
//RGBQUAD* mtt=(RGBQUAD*)(BMIInfo->bmiColors);
//BMIInfo->bmiColors = new RGBQUAD[256];
for (int k = 0; k < 256; ++k)
{
BMIInfo->bmiColors[k].rgbBlue = (BYTE)m_Bra[k];
BMIInfo->bmiColors[k].rgbGreen = (BYTE)m_Gra[k];
BMIInfo->bmiColors[k].rgbRed = (BYTE)m_Rra[k];
BMIInfo->bmiColors[k].rgbReserved = 0;
}
}
if (0 != 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)
{
/*x+=(w-BMIInfo->bmiHeader.biWidth)/2;
y+=(h-BMIInfo->bmiHeader.biHeight)/2;
w=BMIInfo->bmiHeader.biWidth;
h=BMIInfo->bmiHeader.biHeight;*/
float t1, t2;
t1 = (float)w / (float)h;
t2 = (float)BMIInfo->bmiHeader.biWidth / (float)BMIInfo->bmiHeader.biHeight;
if (t1 > t2)
{
x += (w - t2 * h) / 2;
w = t2 * h;
}
else
{
y += (h - w / t2) / 2;
h = w / t2;
}
}
else
{
float t1,t2;
t1=(float)w/(float)h;
t2=(float)BMIInfo->bmiHeader.biWidth/(float)BMIInfo->bmiHeader.biHeight;
if(t1>t2)
{
x+=(w-t2*h)/2;
w = t2*h;
}
else
{
y+=(h-w/t2)/2;
h = 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(flag!=1)
{
theDC->SelectPalette(oldp,false);
theDC->RealizePalette();
theDC->SetStretchBltMode(COLORONCOLOR);
}
mark.ReDraw(theDC,x,y,w,h);
meas.SetScale(x,y,w,h);
meas.ReDraw(theDC);
/* SetDIBitsToDevice( (HDC)*theDC,
x,y,
w,h,
0,0,
0, BMIInfo->bmiHeader.biHeight, m_buf,
BMIInfo, DIB_RGB_COLORS);*/
delete []tmp;
//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 )
{
if (NULL == m_buf) return false;
BOOL ok;
if(flag==1)//color
{
unsigned char *tbuf=new unsigned char[ m_width * 3*m_height];
memcpy(tbuf,m_buf, m_width * 3*m_height);
JpegFile::VertFlipBuf(tbuf, m_width * 3, m_height);
// we swap red and blue for display, undo that.
JpegFile::BGRFromRGB(tbuf, m_width, m_height);
// save RGB packed buffer to JPG
ok=JpegFile::RGBToJpegFile(filename,
tbuf,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -