Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
DIB.cpp
Package: dip.rar [view]
Upload User: hongxinly
Upload Date: 2022-06-13
Package Size: 2588k
Code Size: 21k
Category:
Special Effects
Development Platform:
Visual C++
- // DIB.cpp
- #include "ShowDIB.h"
- #include "ShowDIBDoc.h"
- #include "ShowDIBView.h"
- #include "stdafx.h"
- #include "DIB.h"
- #include"math.h"
- #include"afxwin.h"
- #include"stdlib.h"
- #define PI (double)3.14159265359
- CDib::CDib()
- {
- // Set the Dib pointer to
- // NULL so we know if it's
- // been loaded.
- m_pDib = NULL;
- m_pWordData = NULL;
- }
- CDib::~CDib()
- {
- // If a Dib has been loaded,
- // delete the memory.
- if( m_pDib != NULL )
- delete [] m_pDib;
- if( m_pWordData != NULL )
- delete [] m_pWordData;
- }
- BOOL CDib::Load( const char *pszFilename )
- {
- CFile cf;
- int i,j;
- // Attempt to open the Dib file for reading.
- if( !cf.Open( pszFilename, CFile::modeRead ) )
- return( FALSE );
- // Get the size of the file and store
- // in a local variable. Subtract the
- // size of the BITMAPFILEHEADER structure
- // since we won't keep that in memory.
- DWORD dwDibSize;
- dwDibSize =
- cf.GetLength() - sizeof( BITMAPFILEHEADER );
- // Attempt to allocate the Dib memory.
- unsigned char *pDib;
- pDib = new unsigned char [dwDibSize];
- if( pDib == NULL )
- return( FALSE );
- BITMAPFILEHEADER BFH;
- // Read in the Dib header and data.
- try{
- // Did we read in the entire BITMAPFILEHEADER?
- if( cf.Read( &BFH, sizeof( BITMAPFILEHEADER ) )
- != sizeof( BITMAPFILEHEADER ) ||
- // Is the type 'MB'?
- BFH.bfType != 'MB' ||
- // Did we read in the remaining data?
- cf.Read( pDib, dwDibSize ) != dwDibSize ){
- // Delete the memory if we had any
- // errors and return FALSE.
- delete [] pDib;
- return( FALSE );
- }
- }
- // If we catch an exception, delete the
- // exception, the temporary Dib memory,
- // and return FALSE.
- catch( CFileException *e ){
- e->Delete();
- delete [] pDib;
- return( FALSE );
- }
- cf.Close();
- // If we got to this point, the Dib has been
- // loaded. If a Dib was already loaded into
- // this class, we must now delete it.
- if( m_pDib != NULL )
- delete m_pDib;
- // Store the local Dib data pointer and
- // Dib size variables in the class member
- // variables.
- m_pDib = pDib;
- m_dwDibSize = dwDibSize;
- // Pointer our BITMAPINFOHEADER and RGBQUAD
- // variables to the correct place in the Dib data.
- m_pBIH = (BITMAPINFOHEADER *) m_pDib;
- m_pPalette =
- (RGBQUAD *) &m_pDib[sizeof(BITMAPINFOHEADER)];
- // Calculate the number of palette entries.
- m_nPaletteEntries = 1 << m_pBIH->biBitCount;
- if( m_pBIH->biBitCount > 8 )
- m_nPaletteEntries = 0;
- else if( m_pBIH->biClrUsed != 0 )
- m_nPaletteEntries = m_pBIH->biClrUsed;
- // Point m_pDibBits to the actual Dib bits data.
- m_pDibBits =
- &m_pDib[sizeof(BITMAPINFOHEADER)+m_nPaletteEntries*sizeof(RGBQUAD)];
- // If we have a valid palette, delete it.
- if( m_Palette.GetSafeHandle() != NULL )
- m_Palette.DeleteObject();
- // If there are palette entries, we'll need
- // to create a LOGPALETTE then create the
- // CPalette palette.
- if( m_nPaletteEntries != 0 ){
- // Allocate the LOGPALETTE structure.
- LOGPALETTE *pLogPal = (LOGPALETTE *) new char
- [sizeof(LOGPALETTE)+
- m_nPaletteEntries*sizeof(PALETTEENTRY)];
- if( pLogPal != NULL ){
- // Set the LOGPALETTE to version 0x300
- // and store the number of palette
- // entries.
- pLogPal->palVersion = 0x300;
- pLogPal->palNumEntries = m_nPaletteEntries;
- // Store the RGB values into each
- // PALETTEENTRY element.
- for( int i=0; i<m_nPaletteEntries; i++ ){
- pLogPal->palPalEntry[i].peRed =
- m_pPalette[i].rgbRed;
- pLogPal->palPalEntry[i].peGreen =
- m_pPalette[i].rgbGreen;
- pLogPal->palPalEntry[i].peBlue =
- m_pPalette[i].rgbBlue;
- }
- // Create the CPalette object and
- // delete the LOGPALETTE memory.
- m_Palette.CreatePalette( pLogPal );
- delete [] pLogPal;
- }
- }
- m_BitCount = 8;
- m_nWidth = m_pBIH->biWidth;
- m_nHeight = m_pBIH->biHeight;
- m_nWidth_Step = 4*(!!(m_nWidth%4)); // if m_nWidth%4 == 0, we get 0, otherwise we get 4;
- m_nWidth_Step += (m_nWidth>>2)<<2;
- if (m_pBIH->biBitCount == 24)
- {
- MessageBox(NULL,"请自行编写转化为灰度图像的程序","说明",MB_OK);
- }
- temp =new int [m_nHeight*m_nWidth];
- tmp_Height = m_nHeight;
- tmp_Width = m_nWidth;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- temp[i*m_nWidth_Step+j] = m_pDibBits[i*m_nWidth_Step+j];
- }
- }
- return( TRUE );
- }
- BOOL CDib::Save( const char *pszFilename )
- {
- // If we have no data, we can't save.
- if( m_pDib == NULL )
- return( FALSE );
- CFile cf;
- // Attempt to create the file.
- if( !cf.Open( pszFilename,
- CFile::modeCreate | CFile::modeWrite ) )
- return( FALSE );
- // Write the data.
- try{
- // First, create a BITMAPFILEHEADER
- // with the correct data.
- BITMAPFILEHEADER BFH;
- memset( &BFH, 0, sizeof( BITMAPFILEHEADER ) );
- BFH.bfType = 'MB';
- BFH.bfSize = sizeof( BITMAPFILEHEADER ) + m_dwDibSize;
- BFH.bfOffBits = sizeof( BITMAPFILEHEADER ) +
- sizeof( BITMAPINFOHEADER ) +
- m_nPaletteEntries * sizeof( RGBQUAD );
- // Write the BITMAPFILEHEADER and the
- // Dib data.
- cf.Write( &BFH, sizeof( BITMAPFILEHEADER ) );
- cf.Write( m_pDib, m_dwDibSize );
- }
- // If we get an exception, delete the exception and
- // return FALSE.
- catch( CFileException *e ){
- e->Delete();
- return( FALSE );
- }
- return( TRUE );
- }
- BOOL CDib::Draw( CDC *pDC, int nX, int nY, int nWidth, int nHeight, int Style )
- {
- long i,j;
- // If we have not data we can't draw.
- if( m_pDib == NULL )
- return( FALSE );
- // if this is an 16/12 bit image,
- // we should map the 16/12 data to 8 bit
- long vWidth = (4 - m_pBIH->biWidth % 4) % 4 + m_pBIH->biWidth;
- if ( m_BitCount != 8)
- {
- for (i = 0; i< m_pBIH->biHeight; i++)
- for (j = 0; j < m_pBIH->biWidth; j++)
- {
- *(m_pDibBits+i*vWidth+j) = (m_pWordData[i*m_pBIH->biWidth+j] >> (m_BitCount-8));
- }
- }
- // Check for the default values of -1
- // in the width and height arguments. If
- // we find -1 in either, we'll set them
- // to the value that's in the BITMAPINFOHEADER.
- if( nWidth == -1 )
- nWidth = m_pBIH->biWidth;
- if( nHeight == -1 )
- nHeight = m_pBIH->biHeight;
- if (Style) // non_zero: Use StretchDIBits to draw the Dib.
- {
- StretchDIBits( pDC->m_hDC, nX, nY,
- nWidth, nHeight,
- 0, 0,
- m_pBIH->biWidth, m_pBIH->biHeight,
- m_pDibBits,
- (BITMAPINFO *) m_pBIH,
- BI_RGB, SRCCOPY );
- }
- else
- {
- SetDIBitsToDevice( pDC->m_hDC, nX, nY,
- m_pBIH->biWidth, m_pBIH->biHeight,
- 0, 0,
- 0, m_pBIH->biHeight,//nHeight,
- m_pDibBits,
- (BITMAPINFO *) m_pBIH,
- BI_RGB);
- }
- return( TRUE );
- }
- BOOL CDib::SetPalette( CDC *pDC )
- {
- // If we have not data we
- // won't want to set the palette.
- if( m_pDib == NULL )
- return( FALSE );
- // Check to see if we have a palette
- // handle. For Dibs greater than 8 bits,
- // this will be NULL.
- if( m_Palette.GetSafeHandle() == NULL )
- return( TRUE );
- // Select the palette, realize the palette,
- // then finally restore the old palette.
- CPalette *pOldPalette;
- pOldPalette = pDC->SelectPalette( &m_Palette, FALSE );
- pDC->RealizePalette();
- pDC->SelectPalette( pOldPalette, FALSE );
- return( TRUE );
- }
- // 一个特别简单的二值化程序以128为界划分
- bool CDib::ConvertToTwoValue()
- {
- // DWORD size;
- unsigned char *p;
- p = m_pDibBits;
- int i,j;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- if(p[i*m_nWidth_Step+j]<128)
- p[i*m_nWidth_Step+j] = 0;
- else
- p[i*m_nWidth_Step+j] = 255;
- }
- }
- return true;
- }
- // 将彩色图像转化为灰度图像
- void CDib::RGB2GRAY()
- {
- }
- /*************************求最大值*************************
- ***********************************************************/
- int CDib::GetMax()
- {
- int Max = 0;
- unsigned char *p;
- p = m_pDibBits;
- int i,j;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- if(p[i*m_nWidth_Step+j]>Max)
- Max = p[i*m_nWidth_Step+j];
- }
- }
- return Max;
- }
- /************************求最小值*************************
- **********************************************************/
- int CDib::GetMin()
- {
- int Min = 0;
- unsigned char *p;
- p = m_pDibBits;
- int i,j;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- if(p[i*m_nWidth_Step+j]<Min)
- Min = p[i*m_nWidth_Step+j];
- }
- }
- return Min;
- }
- /************************求平均值************************
- *********************************************************/
- float CDib::GetAve()
- {
- float Ave = 0;
- long sum=0,num=0;
- unsigned char *p;
- p = m_pDibBits;
- int i,j;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- sum += p[i*m_nWidth_Step+j];
- num ++;
- }
- }
- Ave =(float) sum /(float)num;
- return Ave;
- }
- //图象反转
- void CDib::Reverse()
- {
- unsigned char *p;
- p = m_pDibBits;
- int i,j;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- p[i*m_nWidth_Step+j] = 255 -p[i*m_nWidth_Step+j];
- }
- }
- }
- /*********************灰度线性变换***************************
- *执行的变换是分段线性变换,参数(Ax,Ay),(Bx,By)是分段点的坐标*
- *************************************************************/
- void CDib::Pointlinear()
- {
- unsigned char *p;
- p = m_pDibBits;
- float Ax=50,Ay=10,Bx=200,By=245;//参数,通过调节这四个值可以得到不同的拉伸效果
- int Tran[256];//变换表
- float i,j;
- for(i=0;i<256;i++)
- {
- if((0<=i)&&(i<Ax))
- Tran[(int)i]=(int)(i*Ay/Ax);
- else if((Ax<=i)&&(i<Bx))
- Tran[(int)i]=(int)(i*(By-Ay)/(Bx-Ax)+(Ax*By-Ay*Bx)/(Ax-Bx));
- else if((Bx<=i)&&(i<256))
- Tran[(int)i]=(int)(i*(255-By)/(255-Bx)+255*(By-Bx)/(255-Bx));
- }
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- p[(int)i*m_nWidth_Step+(int)j] = Tran[p[(int)i*m_nWidth_Step+(int)j]];
- }
- }
- }
- /******************统计直方图************************
- * 统计图像的直方图,并且把它画出来 *
- *****************************************************/
- void CDib::Histogram()
- {
- unsigned char *p;
- float histogram[256],max;
- p = m_pDibBits;
- int i,j,rk;
- for(i=0;i<256;i++)
- histogram[i]=0;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- rk=p[i*m_nWidth_Step+j];
- histogram[rk] ++;//个数统计
- }
- }
- max=histogram[0];
- for(i=0;i<256;i++)
- if(histogram[i]>max)
- max=histogram[i];//统计直方图最大值
- for(i=0;i<256;i++)
- histogram[i]=(m_nHeight-1)*histogram[i]/max;//归一化处理,显示时保持最高的一列高度恒定
- int NOP=0;
- for(j=0;j<256;j++)
- {
- for(i=m_nHeight;i>0;i--)
- {
- if(i<(int)histogram[j])p[i*m_nWidth_Step+j]=0;
- else p[i*m_nWidth_Step+j]=255;//显示,想出这个方法的邹磊同学很NB
- }
- }
- }
- /**********************直方图均衡************************
- *********************************************************/
- void CDib::HistEqua()
- {
- unsigned char *p;
- float histogram[256];
- p = m_pDibBits;
- int i,j,rk;
- for(rk=0;rk<256;rk++)
- histogram[rk]=0;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- rk=p[i*m_nWidth_Step+j];
- histogram[rk] ++;//个数统计
- }
- }
- for(i=0;i<256;i++)
- histogram[i]=histogram[i]/(256*256);//转化为概率
- float Pr[256];
- int T[256];
- Pr[0]=histogram[0];
- T[0]=(int)(Pr[0]*256);
- for(i=1;i<256;i++)
- {
- Pr[i]=Pr[i-1]+histogram[i];
- T[i]=(int)(Pr[i]*256);
- }
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- p[i*m_nWidth_Step+j] =T[p[i*m_nWidth_Step+j]];
- }
- }
- }
- //平移变换
- void CDib::GeomTrans()
- {
- }
- //缩放变换
- void CDib::GeomZoom()
- {
- }
- /*******************旋转变换*************************
- * 将图象逆时针旋转Degrees角,Degrees是可变参量 *
- *****************************************************/
- int * CDib::GeomRota()
- {
- unsigned char *p;
- p = m_pDibBits;
- double Degrees=60+0;//旋转的角度
- double radians = (Degrees/90.0)*(PI/2);//从度转化为弧度
- float cosine = (float)cos(radians);
- float sine = (float)sin(radians);
- //计算旋转后的图像的宽和长
- int h = (int)(m_nHeight*fabs(cosine)+m_nWidth*fabs(sine));
- int w = (int)(m_nHeight*fabs(sine)+m_nWidth*fabs(cosine));
- // if(q!=NULL)
- // delete[] q;////////////////////////////////////////////////为什么只要用了DELETE就不正常?
- q=new int [w*h+2];
- int y,x,sx,sy;
- float sourcex,sourcey,x1,y1;
- int NOP=0;
- for(y=0;y<h;y++)
- {
- for(x=0;x<w;x++)
- {
- if(0<=Degrees&&Degrees<90)
- {
- x1=x-m_nHeight*sine;
- y1=(float)y;
- }
- else if(90<=Degrees&&Degrees<180)
- {
- x1=(float)(x-w);
- y1=y+m_nWidth*cosine;
- }
- else if(180<=Degrees&&Degrees<270)
- {
- x1=x+m_nHeight*cosine;
- y1=(float)(y-h);
- }
- else if(180<=Degrees&&Degrees<=360)
- {
- x1=(float)x;
- y1=y+m_nHeight*sine;
- }
- else
- MessageBox(NULL,"请输入0-360°的角度值","警告",MB_OK);
- /***********当使用双线性内插时***********************************************************************
- sourcex =x1*cosine+y1*sine;
- sourcey =y1*cosine-x1*sine;
- sx=(int)ceil(sourcex);
- sy=(int)ceil(sourcey);
- float dx=sourcex-sx;
- float dy=sourcey-sy;
- if(sx>=0&&sx<m_nWidth&&sy>=0&&sy<m_nHeight)
- {
- q[y*w+x+2]=(int)(dx*dy*p[sy*m_nWidth_Step+sx]+dx*(1-dy)*p[(sy+1)*m_nWidth_Step+sx]
- +(1-dx)*dy*p[sy*m_nWidth_Step+sx+1]+(1-dx)*(1-dy)*p[(sy+1)*m_nWidth_Step+sx+1]);
- }
- else q[y*w+x+2]=0;
- *******************************************************************************************************/
- /***********当不使用双线性内插时***********************************************************************/
- sourcex =x1*cosine+y1*sine;
- sourcey =y1*cosine-x1*sine;
- sx=(int)ceil(sourcex);
- sy=(int)ceil(sourcey);
- if(sx>=0&&sx<m_nWidth&&sy>=0&&sy<m_nHeight)
- {
- q[y*w+x+2]=p[sy*m_nWidth_Step+sx];
- }
- else q[y*w+x+2]=0;
- }
- }
- q[0]=w;
- q[1]=h;
- return q;
- }
- //复数
- typedef struct
- {
- double re;
- double im;
- }COMPLEX;
- //复数加法
- COMPLEX Add(COMPLEX c1, COMPLEX c2)
- {
- COMPLEX c;
- c.re=c1.re+c2.re;
- c.im=c1.im+c2.im;
- return c;
- }
- //复数减法
- COMPLEX Sub(COMPLEX c1, COMPLEX c2)
- {
- COMPLEX c;
- c.re=c1.re-c2.re;
- c.im=c1.im-c2.im;
- return c;
- }
- //复数乘法
- COMPLEX Mul(COMPLEX c1, COMPLEX c2)
- {
- COMPLEX c;
- c.re=c1.re*c2.re-c1.im*c2.im;
- c.im=c1.re*c2.im+c2.re*c1.im;
- return c;
- }
- //FFT变换
- void DOFFT(COMPLEX * TD, COMPLEX * FD, int power)
- {
- int count;
- int i,j,k,bfsize,p;
- double angle;
- COMPLEX *W,*X1,*X2,*X;
- /*计算傅立叶变换点数*/
- count=1<<power;
- /*分配运算所需存储器*/
- W=(COMPLEX *)malloc(sizeof(COMPLEX)*count/2);
- X1=(COMPLEX *)malloc(sizeof(COMPLEX)*count);
- X2=(COMPLEX *)malloc(sizeof(COMPLEX)*count);
- /*计算加权系数*/
- for(i=0;i<count/2;i++)
- {
- angle=-i*PI*2/count;
- W[i].re=cos(angle);
- W[i].im=sin(angle);
- }
- /*将时域点写入存储器*/
- memcpy(X1,TD,sizeof(COMPLEX)*count);
- /*蝶形运算*/
- for(k=0;k<power;k++)
- {
- for(j=0;j<1<<k;j++)
- {
- bfsize=1<<(power-k);
- for(i=0;i<bfsize/2;i++)
- {
- p=j*bfsize;
- X2[i+p]=Add(X1[i+p],X1[i+p+bfsize/2]);
- X2[i+p+bfsize/2]=Mul(Sub(X1[i+p],X1[i+p+bfsize/2]),W[i*(1<<k)]);
- }
- }
- X=X1;
- X1=X2;
- X2=X;
- }
- /*重新排序*/
- for(j=0;j<count;j++)
- {
- p=0;
- for(i=0;i<power;i++)
- {
- if (j&(1<<i)) p+=1<<(power-i-1);
- }
- FD[j]=X1[p];
- }
- /*释放存储器*/
- free(W);
- free(X1);
- free(X2);
- }
- /**************************FFT*************************
- ******************************************************/
- void CDib::FFT()
- {
- unsigned char *p;
- p = m_pDibBits;
- int i,j;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- p[i*m_nWidth_Step+j] =p[i*m_nWidth_Step+j];
- }
- }
- int w=1,h=1,wp=0,hp=0;
- while(w*2<=m_nWidth)
- {
- w*=2;
- wp++;
- }
- while(h*2<=m_nHeight)
- {
- h*=2;
- hp++;
- }
- COMPLEX *TD=new COMPLEX[w*h];
- COMPLEX *FD=new COMPLEX[w*h];
- int x,y;
- for(y=0;y<h;y++)
- {
- for(x=0;x<w;x++)
- {
- TD[x+w*y].re=p[x+w*y];
- TD[x+w*y].im=0;
- }
- }
- for(y=0;y<h;y++)
- {
- DOFFT(&TD[w*y],&FD[w*y],wp);
- }
- for(y=0;y<h;y++)
- {
- for(x=0;x<w;x++)
- {
- TD[y+h*x]=FD[x+w*y];
- }
- }
- for(x=0;x<w;x++)
- {
- DOFFT(&TD[x*h],&FD[x*h],hp);
- }
- for(y=0;y<h;y++)
- {
- for(x=0;x<w;x++)
- {
- p[x+w*y]=(int)sqrt(FD[x*h+y].re*FD[x*h+y].re+FD[x*h+y].im*FD[x*h+y].im)/100;
- if (p[x+w*y]>255) p[x+w*y]=255;
- }
- }
- }
- void DODCT(double *f, double *F, int power)
- {
- int i,count;
- COMPLEX *X;
- double s;
- /*计算离散余弦变换点数*/
- count=1<<power;
- /*分配运算所需存储器*/
- X=(COMPLEX *)malloc(sizeof(COMPLEX)*count*2);
- /*延拓*/
- memset(X,0,sizeof(COMPLEX)*count*2);
- /*将时域点写入存储器*/
- for(i=0;i<count;i++)
- {
- X[i].re=f[i];
- }
- /*调用快速傅立叶变换*/
- DOFFT(X,X,power+1);
- /*调整系数*/
- s=1/sqrt(count);
- F[0]=X[0].re*s;
- s*=sqrt(2);
- for(i=1;i<count;i++)
- {
- F[i]=(X[i].re*cos(i*PI/(count*2))+X[i].im*sin(i*PI/(count*2)))*s;
- }
- /*释放存储器*/
- free(X);
- }
- /****************************DCT*****************************
- ************************************************************/
- void CDib::DCT()
- {
- unsigned char *p;
- p = m_pDibBits;
- int w=1,h=1,wp=0,hp=0;
- while(w*2<=m_nWidth)
- {
- w*=2;
- wp++;
- }
- while(h*2<=m_nHeight)
- {
- h*=2;
- hp++;
- }
- int x,y;
- double *f=new double[w*h];
- double *W=new double[w*h];
- for(y=0;y<h;y++)
- {
- for(x=0;x<w;x++)
- {
- f[x+y*w]=p[x+w*y];
- }
- }
- for(y=0;y<h;y++)
- {
- DODCT(&f[w*y],&W[w*y],wp);
- }
- for(y=0;y<h;y++)
- {
- for(x=0;x<w;x++)
- {
- f[x*h+y]=W[x+w*y];
- }
- }
- for(x=0;x<w;x++)
- {
- DODCT(&f[x*h],&W[x*h],hp);
- }
- double a;
- for(y=0;y<h;y++)
- {
- for(x=0;x<w;x++)
- {
- p[x+w*y]=(int)fabs(W[x*h+y]);
- if (a>255) a=255;
- }
- }
- }
- void CDib::Walsh()
- {
- }
- void CDib::Wavelet()
- {
- }
- //均值滤波
- void CDib::EnhaAverF()
- {
- unsigned char *p;
- int ave[256*256];
- int some[3*3];
- float sum;
- p = m_pDibBits;
- int i,j,k,l;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- for(k=0;k<3;k++)
- for(l=0;l<3;l++)
- some[k*3+l]=p[(i+k-1)*m_nWidth_Step+j+l-1];
- sum=0;
- for(l=0;l<3;l++)
- for(k=0;k<3;k++)
- sum+=some[k*3+l];
- ave[i*m_nWidth_Step+j]=(int)(sum/9);
- }
- }
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- p[i*m_nWidth_Step+j] =ave[i*m_nWidth_Step+j];
- }
- }
- }
- /****************************中值滤波***************************
- ***************************************************************/
- void CDib::EnhaMidianF()
- {
- unsigned char *p;
- int *mid;
- mid=new int [m_nHeight*m_nWidth];
- int some[3*3],tmp;
- p = m_pDibBits;
- int i,j,k,l;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- for(k=0;k<3;k++)
- for(l=0;l<3;l++)
- some[k*3+l]=p[(i+k-1)*m_nWidth_Step+j+l-1];
- for(l=0;l<9;l++)
- for(k=0;k<8;k++)
- {
- if(some[k]<some[k+1])
- {
- tmp=some[k];
- some[k]=some[k+1];
- some[k+1]=tmp;
- }
- }
- mid[i*m_nWidth_Step+j]=some[4];
- }
- }
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- p[i*m_nWidth_Step+j] =mid[i*m_nWidth_Step+j];
- }
- }
- }
- /*************************Sobel**********************
- ****************************************************/
- void CDib::Sobel()
- {
- unsigned char *p;
- int S[256*256];
- p = m_pDibBits;
- int i,j;
- for (i=1;i<m_nHeight-1;++i)
- {
- for (j=1;j<m_nWidth-1;++j)
- {
- S[i*m_nWidth_Step+j]=2*p[(i+1)*m_nWidth_Step+j]-2*p[(i-1)*m_nWidth_Step+j]
- +p[(i+1)*m_nWidth_Step+j-1]-p[(i-1)*m_nWidth_Step+j-1]+p[(i+1)*m_nWidth_Step+j+1]-p[(i-1)*m_nWidth_Step+j+1];
- }
- }
- for (i=1;i<m_nHeight-1;++i)
- {
- for (j=1;j<m_nWidth-1;++j)
- {
- p[i*m_nWidth_Step+j] =S[i*m_nWidth_Step+j];
- if(p[i*m_nWidth_Step+j]>255)p[i*m_nWidth_Step+j]=255;
- if(p[i*m_nWidth_Step+j]<0)p[i*m_nWidth_Step+j]=0;
- }
- }
- }
- /***********************Laplace*************************
- *******************************************************/
- void CDib::Laplace()
- {
- unsigned char *p;
- int L[256*256];
- p = m_pDibBits;
- int i,j;
- for (i=1;i<m_nHeight-1;++i)
- {
- for (j=1;j<m_nWidth-1;++j)
- {
- L[i*m_nWidth_Step+j]=4*p[i*m_nWidth_Step+j]-p[(i-1)*m_nWidth_Step+j]
- -p[(i+1)*m_nWidth_Step+j]-p[i*m_nWidth_Step+j-1]-p[i*m_nWidth_Step+j+1];
- }
- }
- for (i=1;i<m_nHeight-1;++i)
- {
- for (j=1;j<m_nWidth-1;++j)
- {
- p[i*m_nWidth_Step+j] =L[i*m_nWidth_Step+j];
- if(p[i*m_nWidth_Step+j]>255)p[i*m_nWidth_Step+j]=255;
- if(p[i*m_nWidth_Step+j]<0)p[i*m_nWidth_Step+j]=0;
- }
- }
- }
- /*********************随机噪声**********************
- ***************************************************/
- void CDib::RandomNoise()
- {
- unsigned char *p;
- p = m_pDibBits;
- int i,j,k,N=256*3,Noise;
- for(k=0;k<N;k++)
- {
- i=rand()%m_nHeight;
- j=rand()%m_nWidth;
- Noise=rand()%512;
- p[i*m_nWidth_Step+j] += Noise-256;
- }
- }
- /*********************盐椒噪声*********************
- **************************************************/
- void CDib::SaltNoise()
- {
- unsigned char *p;
- p = m_pDibBits;
- int i,j,k,N=256*3,Noise;
- for(k=0;k<N;k++)
- {
- i=rand()%m_nHeight;
- j=rand()%m_nWidth;
- Noise=rand()%2;
- if(Noise==0)
- p[i*m_nWidth_Step+j] =0;
- else p[i*m_nWidth_Step+j] =255;
- }
- }
- //逆滤波恢复
- void CDib::RestoreInverse()
- {
- }
- //维纳滤波恢复
- void CDib::RestoreWiener()
- {
- }
- /************************图像重建************************
- ********************************************************/
- void CDib::Reconstruction()
- {
- unsigned char *p;
- p=m_pDibBits;
- int i,j;
- for (i=0;i<m_nHeight;++i)
- {
- for (j=0;j<m_nWidth;++j)
- {
- p[i*m_nWidth_Step+j] =temp[i*m_nWidth_Step+j];
- }
- }
- }
- void CDib::Huffman()
- {
- }
- void CDib::Runlength()
- {
- }
- void CDib::Algorithem()
- {
- }
- void CDib::LZW()
- {
- }
- void CDib::JPEG()
- {
- }