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

📄 windipdoc.cpp

📁 System will automatically delete the directory of debug and release, so please
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  // WinDIPDoc.cpp : implementation of the CWinDIPDoc class
//

#include "stdafx.h"
#include "WinDIP.h"

#include "WinDIPDoc.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWinDIPDoc

IMPLEMENT_DYNCREATE(CWinDIPDoc, CDocument)

BEGIN_MESSAGE_MAP(CWinDIPDoc, CDocument)
	//{{AFX_MSG_MAP(CWinDIPDoc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWinDIPDoc construction/destruction

CWinDIPDoc::CWinDIPDoc()
{
	// TODO: add one-time construction code here
	m_InputImg = NULL;
	m_OutputImg = NULL;
	for(int i=0; i<256; i++) //孺归 康惑阑 困茄 迫饭飘
	{
		palRGB[i].rgbBlue = palRGB[i].rgbGreen = palRGB[i].rgbRed = i;
		palRGB[i].rgbReserved = 0;
	}

	menu_flag = 0;
}

CWinDIPDoc::~CWinDIPDoc()
{
	if(m_InputImg)
		delete[] m_InputImg;
	if(m_OutputImg)
		delete[] m_OutputImg;
}

BOOL CWinDIPDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CWinDIPDoc serialization

void CWinDIPDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CWinDIPDoc diagnostics

#ifdef _DEBUG
void CWinDIPDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CWinDIPDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CWinDIPDoc commands

BOOL CWinDIPDoc::OnOpenDocument(LPCTSTR lpszPathName) // OnFileOpen()俊辑 磊悼 龋免 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	// TODO: Add your specialized creation code here
	CFile hFile;
	hFile.Open(lpszPathName,CFile::modeRead | CFile::typeBinary); //lpszPathName : 版肺, 
	hFile.Read(&dibHf,sizeof(BITMAPFILEHEADER)); //颇老 庆歹甫 佬澜.

	// 捞 颇老捞 BMP颇老牢瘤 八荤. 0x4d42 == "BM"
	if(dibHf.bfType != 0x4d42)
	{
		AfxMessageBox("Not BMP file!");
		return FALSE;
	}
	hFile.Read(&dibHi, sizeof(BITMAPINFOHEADER)); //康惑沥焊狼 牢器 庆歹甫 佬澜.
	if(dibHi.biBitCount != 8 && dibHi.biBitCount != 24)
	{
		AfxMessageBox("Gray/True ColorRGB Possible!");
		return FALSE;
	}
	if(dibHi.biBitCount == 8) //迫饭飘甫 佬绢扼!!!
		hFile.Read(palRGB,sizeof(RGBQUAD)*256);

	//皋葛府 且寸
	int ImgSize;
	if(dibHi.biBitCount == 8) //孺归康惑.
		ImgSize = hFile.GetLength() - sizeof(BITMAPFILEHEADER) - sizeof(BITMAPINFOHEADER)
				- sizeof(RGBQUAD)*256;
	else if(dibHi.biBitCount == 24) //拿矾康惑.
		ImgSize = hFile.GetLength() - sizeof(BITMAPFILEHEADER) - sizeof(BITMAPINFOHEADER);
//		ImgSize = dibHi.biHeight * dibHi.biWidth * 3;

	m_InputImg = new unsigned char[ImgSize]; //盔夯 康惑 历厘 滚欺
	m_OutputImg = new unsigned char[ImgSize]; //搬苞 康惑 历厘 滚欺
	hFile.Read(m_InputImg, ImgSize); //康惑 单捞磐 涝仿
	hFile.Close();
	height = dibHi.biHeight;
	width = dibHi.biWidth;

	if(dibHi.biBitCount == 24)
		return TRUE;

	//康惑 单捞磐 措涝(孺归 康惑狼 版快)
	int i,j,index;
	int rwsize = WIDTHBYTES(dibHi.biBitCount * width);
	for(i=0; i<height; i++)
	{
		index = i * rwsize;
		for(j=0; j<width; j++) //r,g,b蔼捞 葛滴 鞍绊 b蔼阑 措涝秦 玲澜.
			m_InputImg[index+j] = (unsigned char)palRGB[(int)m_InputImg[index+j]].rgbBlue;
	}

	return TRUE;   // 己傍利栏肺 颇老阑 佬菌阑 锭 TRUE, 酒聪搁 0
	// 凯扁 芒捞 劝己拳等 饶俊 颇老阑 佬绊, 凯扁 芒阑 摧栏搁, 皋牢 橇饭烙捞 劝己拳等促.
	// 捞 锭, WM_PAINT 皋矫瘤啊 惯积窍咯 窃荐 OnDraw()啊 角青等促. 
}

BOOL CWinDIPDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	// TODO: Add your specialized code here and/or call the base class
	dibHf.bfSize  = sizeof(BITMAPFILEHEADER);
	dibHf.bfType  = 0x4D42;
	dibHf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	
	dibHi.biSize    = sizeof(BITMAPINFOHEADER);
	dibHi.biWidth   = width; // 呈厚
	dibHi.biHeight   = height; // 臭捞
	dibHi.biPlanes   = 1;
	dibHi.biBitCount   = 24; // 祸惑 厚飘
	dibHi.biCompression = 0;
	dibHi.biSizeImage = dibHi.biWidth * dibHi.biHeight * (dibHi.biBitCount/8);
	dibHi.biXPelsPerMeter = 0;
	dibHi.biYPelsPerMeter = 0;
	dibHi.biClrUsed = 0;
	dibHi.biClrImportant = 0;
	
	CFile hFile;
	if(!hFile.Open(lpszPathName,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
		return FALSE;
	hFile.Write(&dibHf,sizeof(BITMAPFILEHEADER));
	hFile.Write(&dibHi,sizeof(BITMAPINFOHEADER));
	if(dibHi.biBitCount == 8)
		hFile.Write(palRGB,sizeof(RGBQUAD)*256);
	hFile.Write(m_OutputImg,dibHi.biSizeImage);
	hFile.Close();
	
	return TRUE;
}

void CWinDIPDoc::C2_4_1Sampling(int blockSize)
{
	int	  x, y, i;
	ColorRGB mean;
	CSize iSize;
	iSize.cx = width;
	iSize.cy = height;
	
	for( i= 0; i < iSize.cx * iSize.cy * 3; i++) //葛电 备埃 clear
		*( m_OutputImg + i) = 255;

	for( y = 0; y < iSize.cy - blockSize; y = y + blockSize )
	{
	   for( x = 0; x < iSize.cx - blockSize; x = x + blockSize )
	   {
			GetMean( m_InputImg, blockSize, x, y, iSize, &mean );
			PutMean( m_OutputImg, blockSize, x, y, iSize, mean );
	   }
	}

	UpdateAllViews(FALSE); //拳搁免仿狼 盎脚. 
	// 泅犁 Document客 楷搬登绢 乐绰 View俊 WM_PAINT皋矫瘤甫 傈崔窍咯, 窃荐 OnDraw啊 角青
}

void CWinDIPDoc::GetMean(BYTE *inputImage, int blockSize, int x, int y, CSize iSize, ColorRGB *mean)
{
	int b, g, r, wx, wy;
	int totalB, totalG, totalR;
    int address;
	int blockSizeSquare;	

	totalB = 0, totalG = 0, totalR = 0;

	blockSizeSquare = blockSize * blockSize;
	
	for( wy = 0; wy < blockSize; wy++ )
	{
		for( wx = 0; wx < blockSize; wx++ )
		{
			address = BmpAddress( x + wx, y + wy, iSize );

			b = *( inputImage + address     );
			g = *( inputImage + address + 1 );
			r = *( inputImage + address + 2 );
					
			totalB += b;
			totalG += g;
			totalR += r;
		}
	}
		
	mean->b = (unsigned char)((double)totalB / (double)blockSizeSquare + 0.5);
	mean->g = (unsigned char)((double)totalG / (double)blockSizeSquare + 0.5);
	mean->r = (unsigned char)((double)totalR / (double)blockSizeSquare + 0.5);	
}

int CWinDIPDoc::BmpAddress(int x, int y, CSize iSize)
{
	int address;

 	address = ((iSize.cy - 1 - y ) * iSize.cx + x  ) * 3;	 
			
	return address;
}

void CWinDIPDoc::PutMean(BYTE *outputImage, int blockSize, int x, int y, CSize iSize, ColorRGB mean)
{
	int wx, wy, address;
	
	for( wy = 0; wy < blockSize; wy++ )
	{
		for( wx = 0; wx < blockSize; wx++ )
		{
			address = BmpAddress( x + wx, y + wy, iSize );

			 *( outputImage + address     ) = mean.b;
			 *( outputImage + address + 1 ) = mean.g;
			 *( outputImage + address + 2 ) = mean.r;
		}
	}
}

void CWinDIPDoc::C2_4_1Quantization(int level)
{
	// 角嚼 腮飘 : 拳家蔼阑 level肺 唱传 饶俊 促矫 蚌茄促.
	int x, y, b, g, r;
	int address;
	CSize iSize;
	iSize.cx = width;
	iSize.cy = height;
	
	for( y = 0; y < iSize.cy; y++ )
	{
		for( x = 0; x < iSize.cx; x++ )
		{
			address = BmpAddress( x, y, iSize );

			b = *( m_InputImg + address     );
			g = *( m_InputImg + address + 1 );
			r = *( m_InputImg + address + 2 );

			*( m_OutputImg + address     ) = b / level * level;
			*( m_OutputImg + address + 1 ) = g / level * level;
			*( m_OutputImg + address + 2 ) = r / level * level;
		}
	}

	UpdateAllViews(FALSE); //拳搁免仿狼 盎脚.
}

void CWinDIPDoc::C2_4_5Shrinking(double shrink)
{
	CSize iSize;
	iSize.cx = width;
	iSize.cy = height;
	int address, blockSize;
	unsigned char b, g, r;
	
	int x, y, i;

	for( i= 0; i < iSize.cx * iSize.cy * 3; i++)
		*( m_OutputImg + i) = 255; //官帕祸 檬扁拳(闰祸)

    //咯扁 沁澜
	blockSize = (int) pow(2, shrink);

	for( y = 0; y < iSize.cy - blockSize; y = y + blockSize )
	{
		for( x = 0; x < iSize.cx - blockSize; x = x + blockSize )
		{
			address = BmpAddress( x, y, iSize );
			*( m_OutputImg + i) = 255;

			b = *( m_InputImg + address     );
			g = *( m_InputImg + address + 1 );
			r = *( m_InputImg + address + 2 );
				
			address = BmpAddress( x / blockSize, y / blockSize, iSize );

			*( m_OutputImg + address     ) = b;
			*( m_OutputImg + address + 1 ) = g;
			*( m_OutputImg + address + 2 ) = r;
		 }
	}
	//

	UpdateAllViews(FALSE); //拳搁免仿狼 盎脚.
}

void CWinDIPDoc::C2_4_5Zooming_Bilinear(int scale)
{
	BYTE pixelColor[2][2][3];
	double dx, dy;
	int outY, outX;
	int y, x;
	int rY, rX;
	int bgr;
	int pixelValue;
	CSize inSize, outSize;
	inSize.cx = width;
	inSize.cy = height;

	for( y = 0; y < (inSize.cy - 1) / scale; y++ )
	{
	   for( x = 0; x < (inSize.cx - 1) / scale; x++ )
	   {
		    GetPixel4( m_InputImg, x, y, pixelColor, inSize );

			outY = y * scale;
			for( rY = 0; rY < scale; rY++, outY++ )
			{
				outX = x * scale;
				for( rX = 0; rX < scale; rX++, outX++ )
				{
					dx = (double)rX / (double)scale; 
					dy = (double)rY / (double)scale;

					for( bgr = 0; bgr < 3; bgr++ )
					{
						pixelValue = Interpolation( pixelColor, dx, dy, bgr );
						if( outX < inSize.cx-1 && outY < inSize.cy-1)
							PutPixel( m_OutputImg, pixelValue, outX, outY, bgr, inSize );
					}
				}
			}
		}
	}
	UpdateAllViews(FALSE); //拳搁免仿狼 盎脚.
}

void CWinDIPDoc::GetPixel4(BYTE *inputImage, int x, int y, BYTE pixelColor[][2][3], CSize iSize)
{
	int address;
	int wx, wy;

	for( wy = 0; wy < 2; wy++ )
	{
		for( wx = 0; wx < 2; wx++ )
		{
			address = BmpAddress( x + wx, y + wy, iSize );

			pixelColor[wx][wy][0] = *( inputImage + address     );
			pixelColor[wx][wy][1] = *( inputImage + address + 1 );
			pixelColor[wx][wy][2] = *( inputImage + address + 2 );
		}
	}
}

int CWinDIPDoc::Interpolation(BYTE pixelColor[][2][3], double dx, double dy, int bgr)
{
	double pixelValue;
	
	pixelValue = (1.0-dx) * (1.0-dy) * (double)pixelColor[0][0][bgr] + 
					dx    * (1.0-dy) * (double)pixelColor[1][0][bgr] +
				 (1.0-dx) *   dy     * (double)pixelColor[0][1][bgr] + 
					dx    *   dy     * (double)pixelColor[1][1][bgr];
			
	return (int)(pixelValue + 0.5);
}


void CWinDIPDoc::PutPixel(BYTE *image, int pixelValue, int x, int y, int bgr, CSize iSize)
{
	int address;

	address = BmpAddress( x, y, iSize );

	*( image + address + bgr ) = pixelValue;
}

void CWinDIPDoc::C2_4_5Zooming_Nearest(int scale)
{
	BYTE pixelColor[2][2][3];
	double dx, dy;
	int outY, outX;
	int y, x;
	int rY, rX;
	int bgr;
	int pixelValue;
	CSize inSize, outSize;
	inSize.cx = width;
	inSize.cy = height;

	double center_x, center_y, size_x, size_y;

	center_x = inSize.cx / 2;
	center_y = inSize.cy / 2;

	size_x = center_x - (center_x / scale);
	size_y = center_y - (center_y / scale);

	for( y = 0; y < (inSize.cy - 1) / scale; y++ )
	{
	   for( x = 0; x < (inSize.cx - 1) / scale; x++ )
	   {
		    GetPixel4( m_InputImg, x + size_x, y + size_y, pixelColor, inSize );
			
			outY = y * scale;
			for( rY = 0; rY < scale; rY++, outY++ )
			{
				outX = x * scale;
				for( rX = 0; rX < scale; rX++, outX++ )
				{
					dx = (double)rX / (double)scale; 
					dy = (double)rY / (double)scale;

					for( bgr = 0; bgr < 3; bgr++ )
					{
						pixelValue = Interpolation( pixelColor, dx, dy, bgr );
						if( outX < inSize.cx-1 && outY < inSize.cy-1)
							PutPixel( m_OutputImg, pixelValue, outX, outY, bgr, inSize );
					}
				
				}
			}
		}
	}
	// 腮飘 : 侨伎蔼阑 焊埃窍瘤 臼绊, scale狼 荐父怒 初酒 犬措茄促. 
	UpdateAllViews(FALSE); //拳搁免仿狼 盎脚.
}

void CWinDIPDoc::C3_2_1Negative()
{
	int x, y, b, g, r;
	int address;
	CSize iSize;
	iSize.cx = width;
	iSize.cy = height;
	
	for( y = 0; y < iSize.cy; y++ )
	{
		for( x = 0; x < iSize.cx; x++ )
		{
			address = BmpAddress( x, y, iSize );

			b = *( m_InputImg + address     );
			g = *( m_InputImg + address + 1 );
			r = *( m_InputImg + address + 2 );
			
			b = 255 - b;
			g = 255 - g;
			r = 255 - r;

			*( m_OutputImg + address     ) = b;
			*( m_OutputImg + address + 1 ) = g;
			*( m_OutputImg + address + 2 ) = r;
		}
	}

	UpdateAllViews(FALSE); //拳搁免仿狼 盎脚.
}

void CWinDIPDoc::C3_1Threshold(int threshold)
{
	//void Threshold(BYTE *inputImage, BYTE *outputImage, ImageSize iSize, int thres )
	CSize iSize;
	iSize.cx = width;
	iSize.cy = height;
	int address;
	int x, y, b, g, r;
	int intensity;

 	for( y = 0; y < iSize.cy; y++ )
	{
		for( x = 0; x < iSize.cx; x++ )
		{
			address = BmpAddress( x, y, iSize );

			b = *( m_InputImg + address     );
			g = *( m_InputImg + address + 1 );
			r = *( m_InputImg + address + 2 );

			//灌扁蔼 汲沥
			intensity = (int)(b + g + r)/3;
			
			if( intensity > threshold ) intensity = 255;
			//酒聪搁 0栏肺 汲沥
			else intensity = 0;
	
			//阿阿狼 灌扁蔼阑 免仿俊 谎妨淋
			*( m_OutputImg + address     ) = intensity;
			*( m_OutputImg + address + 1 ) = intensity;
			*( m_OutputImg + address + 2 ) = intensity;
		}
	}
	UpdateAllViews(FALSE); //拳搁免仿狼 盎脚.
}

void CWinDIPDoc::C3_2_2Logtransform()
{
	CSize iSize;
	iSize.cx = width;
	iSize.cy = height;
	int x, y, b, g, r, address;

	double c = 255 / log( 1 + 255 ); // 肺弊窃荐 拌魂侥俊辑 荤侩窍霸 瞪 拌荐 檬扁拳 
	
 	for( y = 0; y < iSize.cy; y++ )
	{
		for( x = 0; x < iSize.cx; x++ )
		{
			address = BmpAddress( x, y, iSize );

⌨️ 快捷键说明

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