dibsect.cpp
上传用户:lbr_007
上传日期:2019-05-31
资源大小:282k
文件大小:9k
- #include "stdafx.h"
- #include "dibsect.h"
- void DIBSection::InitObject(void)
- {
- m_hbmp = NULL ;
- m_pBits = NULL ;
- m_pdc = NULL ;
- m_total_width = 0;
- m_bitcount = 0;
- m_size.cx = m_size.cy = m_total_width = 0;
- m_geoFlag = false;
- }
- DIBSection::DIBSection()
- {
- InitObject();
- }
- DIBSection::DIBSection(const DIBSection& dib)
- {
- InitObject();
- Copy(dib);
- }
- DIBSection::~DIBSection()
- {
- Close();
- }
- void DIBSection::Copy(const DIBSection& dib)
- {
- Close();
- if (dib.IsCreated())
- {
- int w = dib.Width();
- int h = dib.Height();
- int bitcount = dib.GetBitCount();
- Create(w,h,bitcount);
- if (IsCreated())
- {
- memcpy(m_pBits,dib.GetConstBits(),(h * m_total_width * bitcount / 8));
- }
- if (dib.IsGeoImage())
- {
- SetGeoReference(dib.GetConstGeoReference());
- }
- }
- }
- DIBSection& DIBSection::operator = (const DIBSection& dib)
- {
- Copy(dib);
- return *this;
- }
- void DIBSection::Close(void)
- {
- if (m_hbmOld)
- {
- ::SelectObject(m_pdc->GetSafeHdc(), m_hbmOld);
- }
- if (m_hbmp) DeleteObject(m_hbmp);
- if (m_pdc) delete m_pdc;
- InitObject();
- }
- void DIBSection::Create(int cx, int cy, int nbits)
- {
- ASSERT(cx > 0);
- ASSERT(cy > 0);
- ASSERT((nbits == 8) || (nbits == 16) ||
- (nbits == 24) || (nbits == 32)) ;
- Close();
- // Save size for drawing later.
- m_size.cx = cx ;
- m_size.cy = cy ;
- // Initialize the bitmapinfo header
- int size = sizeof(BITMAPINFOHEADER) ;
- memset(&m_bih, 0, size);
- // Populate bitmapinfo header
- m_bih.biSize = size;
- m_bih.biWidth = ((((int) cx * 8) + 31) & ~31) >> 3;
- m_bih.biHeight = cy;
- m_bih.biPlanes = 1;
- m_bih.biBitCount = nbits;
- m_bih.biCompression = BI_RGB;
- m_total_width = m_bih.biWidth;
- m_bitcount = nbits;
- // Create a new DC.
- m_pdc = new CDC ;
- m_pdc->CreateCompatibleDC(NULL);
- // Create the DIB section.
- m_hbmp = CreateDIBSection( m_pdc->GetSafeHdc(),
- (BITMAPINFO*)&m_bih,
- DIB_RGB_COLORS,
- &m_pBits,
- NULL,
- 0);
- ASSERT(m_hbmp);
- ASSERT(m_pBits);
- // Select the bitmap into the buffer
- if (m_hbmp)
- {
- m_hbmOld = (HBITMAP)::SelectObject(m_pdc->GetSafeHdc(),
- m_hbmp);
- }
- }
- void DIBSection::Draw(CDC* pdcDest, int x, int y)
- {
- pdcDest->BitBlt( 0, 0,
- m_size.cx, m_size.cy,
- m_pdc,
- x, y,
- SRCCOPY);
- }
- void DIBSection::SetPixel(UINT32 x, UINT32 y, COLORREF cr)
- {
- if ((x < Width()) && (y < Height()))
- {
- unsigned char * bits = (unsigned char *)GetBits();
- UINT32 offset = x * GetBitCount()/8 + (Height() - y - 1)*GetTotalWidth()*GetBitCount()/8;
- bits[offset++] = GetBValue(cr);
- bits[offset++] = GetGValue(cr);
- bits[offset] = GetRValue(cr);
- }
- }
- void DIBSection::GetPixel(UINT32 x, UINT32 y, COLORREF& cr)
- {
- if ((x < Width()) && (y < Height()))
- {
- unsigned char * src = (unsigned char *)GetBits();
- unsigned char * dst = (unsigned char *)&cr;
- UINT32 offset = x * GetBitCount()/8 + (Height() - y - 1)*GetTotalWidth()*GetBitCount()/8;
- dst[3] = 0;
- dst[2] = src[offset++];
- dst[1] = src[offset++];
- dst[0] = src[offset];
- }
- }
- void DIBSection::ResizeImage(DIBSection& dst_dib, int w, int h)
- {
- if ((w > 0) && (h > 0))
- {
- dst_dib.Create(w,h,GetBitCount());
- if (dst_dib.IsCreated())
- {
- UINT32 width = dst_dib.Width();
- UINT32 height = dst_dib.Height();
- UINT32 src_width = Width();
- UINT32 src_height = Height();
- if ((dst_dib.IsCreated() && (width > 0) && (height > 0)) &&
- (IsCreated() && (src_width > 0) && (src_height > 0)))
- {
- double horizontal_scale = (double)src_width/(double)dst_dib.Width();
- double vertical_scale = (double)src_height/(double)dst_dib.Height();
- unsigned char * src_ptr = (unsigned char *)GetBits();
- unsigned char * dst_ptr = (unsigned char *)dst_dib.GetBits();
- UINT32 src_bytecount = GetBitCount()/8;
- UINT32 src_dibwidth = GetTotalWidth();
- UINT32 dst_bytecount = dst_dib.GetBitCount()/8;
- UINT32 dst_dibwidth = dst_dib.GetTotalWidth();
- UINT32 dst_bitcount = dst_dib.GetBitCount();
- UINT32 src_row, src_col;
- UINT32 src_index, dst_index;
- for (UINT32 row=0;row<height;row++){
- src_row = (UINT32)(row * vertical_scale + 0.50);
- if (src_row >= src_height) src_row = src_height - 1;
- for (UINT32 col=0;col<width;col++){
- src_col = (UINT32)(col * horizontal_scale + 0.50);
- if (src_col >= src_width) src_col = src_width-1;
- src_index = src_row * src_dibwidth * src_bytecount + src_col*src_bytecount;
- dst_index = row*dst_dibwidth*dst_bytecount+col*dst_bytecount;
- memcpy(&dst_ptr[dst_index],
- &src_ptr[src_index],
- dst_bytecount);
- }
- }
- }
- }
- }
- }
- void DIBSection::PatBlt(DWORD pattern)
- {
- if (IsCreated())
- {
- if ((Width() > 0) && (Height() > 0))
- {
- m_pdc->PatBlt(0,0,Width(),Height(),pattern);
- }
- }
- }
- void DIBSection::HistogramEqualization(DIBSection& dib)
- {
- INT32 x, y, i, j, sum;
- INT32 dr, dg, db, delta;
- INT32 ihist[256];
- float fsum;
- float hist[256];
- INT32 histeq[256];
- INT32 w = dib.Width();
-
- INT32 h = dib.Height();
- INT32 bitcount = dib.GetBitCount();
- INT32 bytespp = bitcount / 8; // bytes per pixel
- INT32 width = w * bytespp;
- memset(ihist,0,(size_t)(sizeof(int)*256));
- unsigned char * img_ptr = (unsigned char *)dib.GetBits();
- unsigned char * intensity = new unsigned char[(w * h)];
- UINT32 row, column;
- UINT32 r, g, b;
- INT32 index, image_index;
- // first pass is for calculating the intensity of each pixel
- // for this pass image_index is used for original image, and index
- // is used for intensity image
- for (row=0;row<h;row++){
- for (column=0;column<w;column++){
- index = row * w + column;
- image_index = row * width + column*bytespp;
- r = (UINT32)img_ptr[image_index];
- g = (UINT32)img_ptr[++image_index];
- b = (UINT32)img_ptr[++image_index];
- i = (UINT32)(0.50 + (r + g + b)/3.0);
- if (i>255) i = 255;
- intensity[index] = (unsigned char)i;
- }
- }
- sum = 0;
- for (y=0;y<h;y++){
- for (x=0;x<w;x++){
- j = intensity[x + y*w];
- ihist[j] += 1;
- sum++;
- }
- }
- for (i=0;i<256;i++){
- hist[i] = (float)ihist[i] / (float)sum;
- }
- for (i=0;i<256;i++){
- fsum = 0.0f;
- for (j=0;j<=i;j++){
- fsum += hist[j];
- }
- histeq[i] = (INT32)(fsum*255.0f + 0.50f);
- }
- for (row=0;row<h;row++){
- for (column=0;column<w;column++){
- index = row * w + column;
- image_index = row * width + column*bytespp;
- //image_index += add_one;
- delta = histeq[intensity[index]] - intensity[index];
- dr = (UINT32)img_ptr[image_index] + delta;
- dg = (UINT32)img_ptr[image_index+1] + delta;
- db = (UINT32)img_ptr[image_index+2] + delta;
- if (dr < 0) dr = 0;
- if (dr > 255) dr = 255;
- if (dg < 0) dg = 0;
- if (dg > 255) dg = 255;
- if (db < 0) db = 0;
- if (db > 255) db = 255;
- img_ptr[image_index] = (unsigned char)dr;
- img_ptr[++image_index] = (unsigned char)dg;
- img_ptr[++image_index] = (unsigned char)db;
- }
- }
- delete [] intensity;
- //delete [] intensity_copy;
- }
- void DIBSection::Greyscale(DIBSection& dib)
- {
- INT32 i;
- INT32 w = dib.Width();
- INT32 h = dib.Height();
- INT32 bitcount = dib.GetBitCount();
- INT32 bytespp = bitcount / 8; // bytes per pixel
- INT32 width = w * bytespp;
- unsigned char * img_ptr = (unsigned char *)dib.GetBits();
- UINT32 row, column;
- UINT32 r, g, b;
- //INT32 val, i1, i2;
- INT32 image_index;
- // first pass is for calculating the intensity of each pixel
- // for this pass image_index is used for original image, and index
- // is used for intensity image
- for (row=0;row<h;row++){
- for (column=0;column<w;column++){
- image_index = row * width + column*bytespp;
- r = (UINT32)img_ptr[image_index];
- g = (UINT32)img_ptr[image_index+1];
- b = (UINT32)img_ptr[image_index+2];
- i = (UINT32)(0.50 + (r + g + b)/3.0);
- if (i>255) i = 255;
- memset(&img_ptr[image_index],i,3);
- }
- }
- }
- void DIBSection::Lighten(INT32 amt)
- {
- INT32 w = Width();
- INT32 h = Height();
- INT32 bitcount = GetBitCount();
- INT32 bytespp = bitcount / 8; // bytes per pixel
- INT32 width = w * bytespp;
- unsigned char * img_ptr = (unsigned char *)GetBits();
- UINT32 row, column;
- INT32 r, g, b;
- INT32 image_index;
- // first pass is for calculating the intensity of each pixel
- // for this pass image_index is used for original image, and index
- // is used for intensity image
- for (row=0;row<h;row++){
- for (column=0;column<w;column++){
- image_index = row * width + column*bytespp;
- r = (INT32)img_ptr[image_index];
- g = (INT32)img_ptr[image_index+1];
- b = (INT32)img_ptr[image_index+2];
- r += amt;
- g += amt;
- b += amt;
- r = (r > 0) ? ((r<256)?r:255) : 0;
- g = (g > 0) ? ((g<256)?g:255) : 0;
- b = (b > 0) ? ((b<256)?b:255) : 0;
- img_ptr[image_index] = (unsigned char)r;
- img_ptr[image_index+1] = (unsigned char)g;
- img_ptr[image_index+2] = (unsigned char)b;
- }
- }
- }