123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- // CImageView.cpp: 实现文件
- //
- #include "pch.h"
- #include "CImageView.h"
- // CImageView
- IMPLEMENT_DYNAMIC(CImageView, CWnd)
- CImageView::CImageView()
- {
- m_pData16 = nullptr;
- m_pData8 = nullptr;
- m_width = 0;
- m_height = 0;
- m_bits = 0;
- m_bDrawRect = false;
- m_nROIAverage = 0;
- m_wfRation = 0.0f;
- m_hfRation = 0.0f;
- }
- CImageView::~CImageView()
- {
- if (m_pData16)
- {
- delete[] m_pData16;
- m_pData16 = nullptr;
- }
- if (m_pData8)
- {
- delete[] m_pData8;
- m_pData8 = nullptr;
- }
- if (!m_Image.IsNull())
- {
- m_Image.Destroy();
- }
- }
- BEGIN_MESSAGE_MAP(CImageView, CWnd)
- ON_WM_PAINT()
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- END_MESSAGE_MAP()
- // CImageView 消息处理程序
- void CImageView::OnPaint()
- {
- CPaintDC dc(this);
- CRect rect;
- GetClientRect(&rect);
- SetStretchBltMode(dc.m_hDC, HALFTONE);
- m_Image.StretchBlt(dc, 0, 0, rect.Width(), rect.Height());
- if (m_bDrawRect)
- {
- dc.MoveTo(m_rtDraw.left, m_rtDraw.top);
- dc.LineTo(m_rtDraw.right, m_rtDraw.top);
- dc.LineTo(m_rtDraw.right, m_rtDraw.bottom);
- dc.LineTo(m_rtDraw.left, m_rtDraw.bottom);
- dc.LineTo(m_rtDraw.left, m_rtDraw.top);
- }
- return;
- }
- void CImageView::OnMouseMove(UINT nFlags, CPoint point)
- {
- if (m_bDrawRect)
- {
- m_ptEnd = point;
- CreatRect(m_ptStart, m_ptEnd);
- Invalidate();
- }
- CWnd::OnMouseMove(nFlags, point);
- }
- void CImageView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- m_ptStart = point;
- m_bDrawRect = true;
- CWnd::OnLButtonDown(nFlags, point);
- }
- void CImageView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- m_bDrawRect = false;
- m_nROIAverage = CaculateROIAvg();
- Invalidate();
- ::SendMessage(this->GetParent()->m_hWnd, MSG_RETANGE, NULL, (LPARAM)&m_nROIAverage);
- CWnd::OnLButtonUp(nFlags, point);
- }
- bool CImageView::SetImageData(unsigned short* pdata, int width, int height, int bits)
- {
- if (!pdata)
- return false;
- m_width = width;
- m_height = height;
- m_bits = bits;
- m_pData16 = new unsigned short[width * height];
- memcpy(m_pData16, pdata, sizeof(unsigned short) * width * height);
- CreateImage(m_pData16, width, height);
- CRect rect;
- GetClientRect(&rect);
- m_wfRation = (float)rect.Width() / width;
- m_hfRation = (float)rect.Height() / height;
- return true;
- }
- int CImageView::CreateImage(unsigned short* data, int width, int height)
- {
- if (!data)
- return 1;
- int lAll = width * height;
- if (!m_pData8)
- delete[]m_pData8;
- m_pData8 = new unsigned char[lAll];
- for (int i = 0; i < lAll; ++i)
- {
- m_pData8[i] = 255 - ((data[i] & (0x3FFF)) >> 6);
- }
- CreatBitImage(m_pData8, m_width, m_height);
- return 0;
- }
- int CImageView::CreatBitImage(unsigned char* pdata, int width, int height)
- {
- if (!m_Image.IsNull())
- {
- m_Image.Destroy();
- }
- m_Image.Create(width, height, 8);
- RGBQUAD clrTable[256];
- for (int i = 0; i < 256; ++i)
- {
- clrTable[i].rgbBlue = i;
- clrTable[i].rgbGreen = i;
- clrTable[i].rgbRed = i;
- clrTable[i].rgbReserved = 0;
- }
- m_Image.SetColorTable(0, 256, clrTable);
- //set image data
- int nLine = m_Image.GetPitch();
- BYTE* pDstData = (BYTE*)m_Image.GetPixelAddress(0, 0);
- BYTE* pSrcData = (BYTE*)pdata;
- for (int i = 0; i < height; ++i)
- {
- memcpy(pDstData, pSrcData, width * sizeof(BYTE));
- pDstData += nLine;
- pSrcData += width;
- }
- m_Image.Save(("d:\\test.bmp"));
- return 0;
- }
- int CImageView::CreatRect(CPoint ps, CPoint pe)
- {
- CRect rect;
- int left, top, right, bottom;
- if (ps.x < pe.x)
- {
- left = ps.x;
- right = pe.x;
- }
- else
- {
- left = pe.x;
- right = ps.x;
- }
- if (ps.y < pe.y)
- {
- top = ps.y;
- bottom = pe.y;
- }
- else
- {
- top = pe.y;
- bottom = ps.y;
- }
- m_rtDraw = CRect(left, top, right, bottom);
- int Orgleft = left / m_wfRation;
- if (Orgleft < 0)
- Orgleft = 0;
- if (Orgleft > m_width)
- Orgleft = m_width;
- int Orgright = right / m_wfRation;
- if (Orgright < 0)
- Orgright = 0;
- if (Orgright > m_width)
- Orgright = m_width;
- int OrgTop = top / m_hfRation;
- if (OrgTop < 0)
- OrgTop = 0;
- if (OrgTop > m_height)
- OrgTop = m_height;
- int OrgBottom = bottom / m_hfRation;
- if (OrgBottom < 0)
- OrgBottom = 0;
- if (OrgBottom > m_height)
- OrgBottom = m_height;
- m_rtOrgDraw = CRect(Orgleft, OrgTop, Orgright, OrgBottom);
- return 0;
- }
- int CImageView::CaculateROIAvg()
- {
- int average = 0;
- double total = 0;
- int count = 0;
- for (int i = m_rtOrgDraw.top; i < m_rtOrgDraw.bottom; ++i)
- {
- for (int j = m_rtOrgDraw.left; j < m_rtOrgDraw.right; ++j)
- {
- total += m_pData16[i * m_width + j];
- count++;
- }
- }
- average = total / count;
- return average;
- }
|