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

📄 cdib.cpp

📁 直方图的相关操作
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		m_hBitmap = ::CreateDIBSection(pDC->GetSafeHdc(), (LPBITMAPINFO) m_lpBMIH,
			DIB_RGB_COLORS,	(LPVOID*) &m_lpImage, NULL, 0);
		ASSERT(m_lpImage != NULL);
		nCount = pFile->Read(m_lpImage, m_dwSizeImage); // image only
	}
	catch(CException* pe)
	{
		AfxMessageBox("ReadSection error");
		pe->Delete();
		return FALSE;
	}
	return TRUE;
}

BOOL CDib::Write(CFile* pFile)
{
	BITMAPFILEHEADER bmfh;
	bmfh.bfType = 0x4d42;  // 'BM'
	int nSizeHdr = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_nColorTableEntries;
	bmfh.bfSize = 0;
	// bmfh.bfSize = sizeof(BITMAPFILEHEADER) + nSizeHdr + m_dwSizeImage;
	// meaning of bfSize open to interpretation (bytes, words, dwords?) -- we won't use it
	bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
	bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
			sizeof(RGBQUAD) * m_nColorTableEntries;	
	try
	{
		pFile->Write((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER));
		pFile->Write((LPVOID) m_lpBMIH,  nSizeHdr);
		pFile->Write((LPVOID) m_lpImage, m_dwSizeImage);
	}
	catch(CException* pe)
	{
		pe->Delete();
		AfxMessageBox("write error");
		return FALSE;
	}
	return TRUE;
}

void CDib::Serialize(CArchive& ar)
{
	DWORD dwPos;
	dwPos = ar.GetFile()->GetPosition();
	TRACE("CDib::Serialize -- pos = %d\n", dwPos);
	ar.Flush();
	dwPos = ar.GetFile()->GetPosition();
	TRACE("CDib::Serialize -- pos = %d\n", dwPos);
	if(ar.IsStoring())
	{
		Write(ar.GetFile());
	}
	else
	{
		Read(ar.GetFile());
	}
}

// helper functions
void CDib::ComputePaletteSize(int nBitCount)
{
	if((m_lpBMIH == NULL) || (m_lpBMIH->biClrUsed == 0))
	{
		switch(nBitCount)
		{
			case 1:
				m_nColorTableEntries = 2;
				break;
			case 4:
				m_nColorTableEntries = 16;
				break;
			case 8:
				m_nColorTableEntries = 256;
				break;
			case 16:
			case 24:
			case 32:
				m_nColorTableEntries = 0;
				break;
			default:
				ASSERT(FALSE);
		}
	}
	else
	{
		m_nColorTableEntries = m_lpBMIH->biClrUsed;
	}
	ASSERT((m_nColorTableEntries >= 0) && (m_nColorTableEntries <= 256)); 
}

void CDib::ComputeMetrics()
{
	if(m_lpBMIH->biSize != sizeof(BITMAPINFOHEADER))
	{
		TRACE("Not a valid Windows bitmap -- probably an OS/2 bitmap\n");
		throw new CUserException;
	}
	m_dwSizeImage = m_lpBMIH->biSizeImage;
	if(m_dwSizeImage == 0)
	{
		DWORD dwBytes = ((DWORD) m_lpBMIH->biWidth * m_lpBMIH->biBitCount) / 32;
		if(((DWORD) m_lpBMIH->biWidth * m_lpBMIH->biBitCount) % 32)
		{
			dwBytes++;
		}
		dwBytes *= 4;
		m_dwSizeImage = dwBytes * m_lpBMIH->biHeight; // no compression
	}
	m_lpvColorTable = (LPBYTE) m_lpBMIH + sizeof(BITMAPINFOHEADER);
}

void CDib::Empty()
{
	// this is supposed to clean up whatever is in the DIB
	DetachMapFile();
	if(m_nBmihAlloc == crtAlloc)
	{
		delete [] m_lpBMIH;
	}
	else if(m_nBmihAlloc == heapAlloc)
	{
		::GlobalUnlock(m_hGlobal);
		::GlobalFree(m_hGlobal);
	}
	if(m_nImageAlloc == crtAlloc)
	{
		delete [] m_lpImage;
	}
	if(m_hPalette != NULL)
	{
		::DeleteObject(m_hPalette);
	}
	if(m_hBitmap != NULL)
	{
		::DeleteObject(m_hBitmap);
	}
	m_nBmihAlloc = m_nImageAlloc = noAlloc;
	m_hGlobal = NULL;
	m_lpBMIH = NULL;
	m_lpImage = NULL;
	m_lpvColorTable = NULL;
	m_nColorTableEntries = 0;
	m_dwSizeImage = 0;
	m_lpvFile = NULL;
	m_hMap = NULL;
	m_hFile = NULL;
	m_hBitmap = NULL;
	m_hPalette = NULL;
}

void CDib::DetachMapFile()
{
	if(m_hFile == NULL)
	{
		return;
	}
	::UnmapViewOfFile(m_lpvFile);
	::CloseHandle(m_hMap);
	::CloseHandle(m_hFile);
	m_hFile = NULL;
}

void CDib::LoadDib(CImage &img)
{
	int  nSize ;
	
	int  i,j;
    Empty();                                      //clear the CDib
    int width=img.GetWidth();
	int height=img.GetHeight();

	int supplement=(4-(width%4))%4;                 //row supplement

	
	DWORD ImageSize=height*(width+supplement);    //Image size for bmp
	nSize = 0x0436-sizeof(BITMAPFILEHEADER);//+ImageSize;//bmfh.bfOffBits - sizeof(BITMAPFILEHEADER);
    m_lpBMIH = (LPBITMAPINFOHEADER) new char[nSize];//Allocate the space
	m_nBmihAlloc = m_nImageAlloc = crtAlloc;
	// info hdr & color table
	
	m_lpBMIH->biBitCount=8;
 	m_lpBMIH->biClrImportant=0;
	m_lpBMIH->biClrUsed=0;
	m_lpBMIH->biCompression=0;
	m_lpBMIH->biHeight=height;
	m_lpBMIH->biPlanes=1;
	m_lpBMIH->biSize=40;
	m_lpBMIH->biSizeImage=0;
	m_lpBMIH->biWidth=width;
	m_lpBMIH->biXPelsPerMeter=3779;
	m_lpBMIH->biYPelsPerMeter=3779;
	//dib.m_lpvColorTable=(LPSTR)(dib.m_lpBMIH+0X036);
	ComputeMetrics();
	ComputePaletteSize(m_lpBMIH->biBitCount);
	if(m_green)//提取绿色分量
	{	
		for(i=0;i<256;i++)
		{
		*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)0;
		*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)i;
		*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)0;
		*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
		}
	   m_green = false;
	}
	else
		if(m_red)//提取红色分量
		{
			for(i=0;i<256;i++)
			{
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)0;
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)0;
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)i;
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
			}
			m_red = false;
		}
		else
			if(m_blue)//提取蓝色分量
			{
				for(i=0;i<256;i++)
				{
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)i;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)0;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)0;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
				}
				m_blue = false;
			}
			else
				for(i=0;i<256;i++)//彩色变灰度
				{
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)i;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)i;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)i;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
				} 
    MakePalette();
    m_lpImage = (LPBYTE) new char[ImageSize];
	
	//m_lpImage =(LPBYTE)m_lpBMIH+0x436-sizeof(BITMAPFILEHEADER);
	for(i=0;i<height;i++)
	{
	    for(j=0;j<width;j++)
		  *((LPSTR)m_lpImage+i*(width+supplement)+j)=img.m_ImageBody[(height-1-i)*width+j];
 	    int x=img.m_ImageBody.GetSize();
		for(j=0;j<supplement;j++)
			*((LPSTR)m_lpImage+i*(width+supplement)+width+j )=0;
	} 
	//dib.m_nBmihAlloc = dib.m_nImageAlloc = crtAlloc; 

}

void CDib::LoadImg(CImage &img,int nCount)
{
	int  nSize ;
	int  i,j;
	int width=img.GetWidth();
	int height=img.GetHeight();
//	int supplement=0;
	int supplement=(4-(width*nCount%4))%4;                 //row supplement
	DWORD ImageSize=height*(width*nCount+supplement);    //Image size for bmp
	int ii=m_lpBMIH->biBitCount;
	if(nCount==1)
	{
		Empty();
		nSize = 0x0436-sizeof(BITMAPFILEHEADER);//+ImageSize;//bmfh.bfOffBits - sizeof(BITMAPFILEHEADER);
		m_lpBMIH = (LPBITMAPINFOHEADER) new char[nSize];//Allocate the space
		m_nBmihAlloc = m_nImageAlloc = crtAlloc;
		m_lpBMIH->biBitCount=8;
		m_lpBMIH->biClrImportant=0;
		m_lpBMIH->biClrUsed=0;
		m_lpBMIH->biCompression=0;
		m_lpBMIH->biHeight=height;
		m_lpBMIH->biPlanes=1;
		m_lpBMIH->biSize=40;
		m_lpBMIH->biSizeImage=0;
		m_lpBMIH->biWidth=width;
		m_lpBMIH->biXPelsPerMeter=3779;
		m_lpBMIH->biYPelsPerMeter=3779;
		ComputeMetrics();
		ComputePaletteSize(m_lpBMIH->biBitCount);
		if(m_green)//提取绿色分量
		{	
			for(i=0;i<256;i++)
			{
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)0;
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)i;
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)0;
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
			}
			m_green = false;
		}
		else
			if(m_red)//提取红色分量
			{
				for(i=0;i<256;i++)
				{
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)0;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)0;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)i;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
				}
				m_red = false;
			}
			else
				if(m_blue)//提取蓝色分量
				{
					for(i=0;i<256;i++)
					{
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)i;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)0;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)0;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
					}
				m_blue = false;
				}
				else
					for(i=0;i<256;i++)//彩色变灰度
					{
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)i;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)i;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)i;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
					} 
					MakePalette();
		m_lpImage = (LPBYTE) new char[ImageSize];
		for(i=0;i<height;i++)
		{
			for(j=0;j<width;j++)
			{
				
			*((LPSTR)m_lpImage+i*(width+supplement)+j)=img.m_ImageBody[(height-1-i)*width+j];
			}
			for(j=0;j<supplement;j++)
				*((LPSTR)m_lpImage+i*(width+supplement)+width+j )=0;
		} 
	}
	if(nCount==3)
	{

		Empty();
		nSize = 0x0436-sizeof(BITMAPFILEHEADER);//+ImageSize;//bmfh.bfOffBits - sizeof(BITMAPFILEHEADER);
		m_lpBMIH = (LPBITMAPINFOHEADER) new char[nSize];//Allocate the space
		m_nBmihAlloc = m_nImageAlloc = crtAlloc;
		m_lpBMIH->biBitCount=24;
		m_lpBMIH->biClrImportant=0;
		m_lpBMIH->biClrUsed=0;
		m_lpBMIH->biCompression=0;
		m_lpBMIH->biHeight=height;
		m_lpBMIH->biPlanes=1;
		m_lpBMIH->biSize=40;
		m_lpBMIH->biSizeImage=0;
		m_lpBMIH->biWidth=width;
		m_lpBMIH->biXPelsPerMeter=3779;
		m_lpBMIH->biYPelsPerMeter=3779;
		ComputeMetrics();
		ComputePaletteSize(m_lpBMIH->biBitCount);
		if(m_green)//提取绿色分量
		{	
			for(i=0;i<256;i++)
			{
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)0;
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)i;
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)0;
				*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
			}
			m_green = false;
		}
		else
			if(m_red)//提取红色分量
			{
				for(i=0;i<256;i++)
				{
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)0;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)0;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)i;
					*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
				}
				m_red = false;
			}
			else
				if(m_blue)//提取蓝色分量
				{
					for(i=0;i<256;i++)
					{
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)i;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)0;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)0;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
					}
					m_blue = false;
				}
				else
					for(i=0;i<256;i++)//彩色变灰度
					{
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+0)=(BYTE)i;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+1)=(BYTE)i;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+2)=(BYTE)i;
						*(LPBYTE)((LPSTR)m_lpvColorTable+i*4+3)=(BYTE)0;
					} 
					MakePalette();

		m_lpImage = (LPBYTE) new char[ImageSize];
		for(i=0;i<height;i++)
		{
			for(j=0;j<width;j++)
				
			{
				BYTE ii=img.m_ImageBody[j*3];
				*((LPSTR)m_lpImage+i*(width*3+supplement)+j*3)=img.m_ImageBody[(height-1-i)*width*3+j*3];
				*((LPSTR)m_lpImage+i*(width*3+supplement)+j*3+1)=img.m_ImageBody[(height-1-i)*width*3+j*3+1];
				*((LPSTR)m_lpImage+i*(width*3+supplement)+j*3+2)=img.m_ImageBody[(height-1-i)*width*3+j*3+2];
			}
			
		//    for(j=0;j<supplement;j++)
	     //     *((LPSTR)m_lpImage+i*(width+supplement)+width+j )=255;
		} 
	}
	
}

⌨️ 快捷键说明

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