⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pic.cpp

📁 超声影像工作站系统可与各种型号的B超、彩超连接
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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;
	}

	//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	= 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);
}

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 < (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	(NULL == m_buf)
	{
		CBrush	bs;
		CRect	rf(x, y, x + w, y + h);

		bs.CreateSolidBrush(RGB(100, 100, 100));
		theDC->FillRect(&rf, &bs);
		return;
	}

	if	(0 == zoom)
	{
		DrawBMP(theDC, x, y, w, h, 0);//,cur);
		return;
	}

	if	(ddx <= 0 && ddy <= 0)	return;

	sx	= (int)(sx - dx / zoom);
	sy	= (int)(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)
	{
		BYTE	*tmp	= NULL;
		// 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	(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;

		//theDC->FillSolidRect(x,y,w,h,RGB(240,240,240));
		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	= (int)(m_width * zoom);
		int bh	= (int)(m_height * zoom);
		int tsx, tsy;

		if	(w > bw)
		{
			x	+= (w - bw) / 2;
			w	= bw;
			tsx	= 0;
			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	= 0;
			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	= 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;
		
		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 (m_buf==NULL) 
	{
		//CBrush bs;
		//bs.CreateSolidBrush(RGB(100,100,100)); 
		//CRect rf(x,y,x+w,y+h);
		//theDC->FillRect(&rf,&bs);
		theDC->FillSolidRect(x,y,w,h,RGB(100,100,100));
		return;
	}

	BYTE *tmp;
	if (theDC!=NULL) {

		// DWORD-align for display
			tmp = JpegFile::MakeDwordAlignedBuf(m_buf,
										 m_width,
										 m_height,
										 &m_widthDW,flag);

		// set up a DIB 
		/*BITMAPINFO BMIInfo;
		//BITMAPINFOHEADER bmiHeader;
		BMIInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		BMIInfo.bmiHeader.biWidth = m_width;
		BMIInfo.bmiHeader.biHeight = m_height;
		BMIInfo.bmiHeader.biPlanes = 1;
		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;*/
		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;

		
		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;
			}
		}
		if(cur)
		{
			CPen pen,*oldpen;
			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,*oldpen;
			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(stretch==0)
		{
			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	= (int)(x + (w - t2 * h) / 2);
					w	= (int)(t2 * h);
					
				}
				else
				{
					y	= (int)(y + (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)(x + (w - t2 * h) / 2);
					w	= (int)(t2 * h);
					
				}
				else
				{
					y	= (int)(y + (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(flag!=1)
		{
			theDC->SelectPalette(oldp,false);
			theDC->RealizePalette();
			theDC->SetStretchBltMode(COLORONCOLOR);
		}
//		mark.ReDraw(theDC,x,y,w,h);
/*		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;
	}

}

BOOL CPic::savejpg( char *filename )
{
	if (m_buf==NULL) 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, 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -