📄 colorsfake.cpp
字号:
// ColorsFake.cpp: implementation of the CColorsFake class.
//
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/**********************************************************************
* Copyright (c) 2003, Medical Image Processing Lab, Sichuan University
* All rights reserved.
*
* 文件名称:Equalize.cpp
* 摘 要:图像直方图伪彩色处理CColorFake源文件
*
* 当前版本:1.1
* 作 者:jian wei zhang
* 完成日期:2004年11月7日
************************************************************************/
#include "stdafx.h"
#include "photostar.h"
#include "ColorsFake.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CColorsFake::CColorsFake()
{
}
CColorsFake::~CColorsFake()
{
}
BOOL CColorsFake::OnColorsFake()
{
if (m_pImageObject == NULL)
{
return FALSE;
}
int iWidth = m_pImageObject->GetWidth();
int iHeight = m_pImageObject->GetHeight();
int iNumbits=m_pImageObject->GetNumBits();
/******************************the old image*****************************/
unsigned char *pOldBuffer, *pNewBuffer, *pOldBits, *pNewBits;
BITMAPFILEHEADER *pOldBFH, *pNewBFH;
BITMAPINFOHEADER *pOldBIH, *pNewBIH;
RGBQUAD *pOldPalette, *pNewPalette;
int nWidthBytes, nNumColors;
pOldBuffer = (unsigned char *) m_pImageObject->GetDIBPointer(&nWidthBytes, m_pImageObject->GetNumBits());
if (pOldBuffer == NULL)
{
return FALSE;
}
pOldBFH = (BITMAPFILEHEADER *) pOldBuffer;
pOldBIH = (BITMAPINFOHEADER *) &pOldBuffer[sizeof(BITMAPFILEHEADER)];
nNumColors = m_pImageObject->GetNumColors();
pOldPalette = (RGBQUAD *) &pOldBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
pOldBits = (unsigned char *) &pOldBuffer[sizeof(BITMAPFILEHEADER)
+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
/*****************************the new image*****************************/
DWORD dwNewSize;
HGLOBAL hNewDib;
//Allocate a global memory block for the new image
dwNewSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ nNumColors * sizeof(RGBQUAD)
+ nWidthBytes * (m_pImageObject->GetHeight());
hNewDib = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwNewSize);
if (hNewDib == NULL)
{
m_pImageObject->m_nLastError = IMAGELIB_MEMORY_ALLOCATION_ERROR;
::GlobalUnlock(m_pImageObject->GetDib());
return FALSE;
}
//Get the pointer to the new memory block
pNewBuffer = (unsigned char *) ::GlobalLock(hNewDib);
if (pNewBuffer == NULL)
{
::GlobalFree( hNewDib );
m_pImageObject->m_nLastError = IMAGELIB_MEMORY_LOCK_ERROR;
::GlobalUnlock(m_pImageObject->GetDib());
return FALSE;
}
pNewBFH = (BITMAPFILEHEADER *) pNewBuffer;
pNewBIH = (BITMAPINFOHEADER *) &pNewBuffer[sizeof(BITMAPFILEHEADER)];
pNewPalette = (RGBQUAD *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
pNewBits = (unsigned char *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
//Make a copy of the old image
memcpy(pNewBuffer,pOldBuffer,dwNewSize);
/*******************************Colors Fake****************************/
int k,maxworth,minworth,i,j;
int nSize=1<<iNumbits;
maxworth=minworth=pOldBits[(iHeight-1)*nWidthBytes];
for(j=0;j<iHeight;j++)
{
for(i=0;i<iWidth;i++)
{
if(pOldBits[(iHeight-1-j)*nWidthBytes+i]>maxworth)
{
maxworth=pOldBits[(iHeight-1-j)*nWidthBytes+i];
}
if(pOldBits[(iHeight-1-j)*nWidthBytes+i]<minworth)
{
minworth=pOldBits[(iHeight-1-j)*nWidthBytes+i];
}
}
}
for(k=minworth;k<(maxworth-minworth)/2+minworth;k++)
{
pNewPalette[k].rgbRed=0;
}
for(k=(maxworth-minworth)/2+minworth;k<(maxworth-minworth)*3/4+minworth;k++)
{
m_nred=k*255/((maxworth-minworth)*3/4-(maxworth-minworth)/2);
pNewPalette[k].rgbRed=(int)m_nred;
}
for(k=(maxworth-minworth)*3/4+minworth;k<maxworth;k++)
{
pNewPalette[k].rgbRed=255;
}
for(k=minworth;k<(maxworth-minworth)/4+minworth;k++)
{
m_ngreen=k*255/(maxworth-minworth)/4;
pNewPalette[k].rgbGreen=(int)m_ngreen;
}
for(k=(maxworth-minworth)/4+minworth;k<(maxworth-minworth)*3/4+minworth;k++)
{
pNewPalette[k].rgbGreen=255;
}
for(k=maxworth;k>=(maxworth-minworth)*3/4+minworth;k--)
{
m_ngreen=(k-maxworth)*(-255)/((maxworth-minworth)/2-(maxworth-minworth)/4);
pNewPalette[k].rgbGreen=(int)m_ngreen;
}
for(k=minworth;k<(maxworth-minworth)/4+minworth;k++)
{
pNewPalette[k].rgbBlue=maxworth;
}
for(k=(maxworth-minworth)/2+minworth;k>=(maxworth-minworth)/4+minworth;k--)
{
m_nblue=(k-(maxworth-minworth)/2-minworth)*-255/((maxworth-minworth)/2-(maxworth-minworth)/4);
pNewPalette[k].rgbBlue=(int)m_nblue;
}
for(k=(maxworth-minworth)/2+minworth;k<maxworth;k++)
{
pNewPalette[k].rgbBlue=minworth;
}
char* pszFileName = "d:\\数字信号作业\\m.dat";
CFile myFile;
CFileException fileException;
if ( !myFile.Open( pszFileName, CFile::modeNoInherit |
CFile::modeReadWrite ), &fileException )
{
TRACE( "Can't open file %s, error = %u\n",
pszFileName, fileException.m_cause );
}
int szBuffer[256];
myFile.Read( szBuffer, sizeof( szBuffer ) );
//Free the memory for the old image
::GlobalUnlock(m_pImageObject->GetDib());
::GlobalFree(m_pImageObject->GetDib());
//Update the DIB in the object pointed by m_pImaeObject
::GlobalUnlock(hNewDib);
m_pImageObject->SetDib(hNewDib);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -