Dib.cpp
上传用户:pass2008
上传日期:2021-07-05
资源大小:3299k
文件大小:18k
- #include "StdAfx.h"
- #include "Dib.h"
- #include "math.h"
- CDib::CDib(void)
- {
- m_pDib = NULL;
- }
- CDib::~CDib(void)
- {
- // 如果位图已经被加载,释放内存
- if (m_pDib != NULL)
- {
- delete[] m_pDib;
- }
- }
- BOOL CDib::imageLoadOperation(LPCTSTR lpszPathName, UINT nOperatorSel)
- {
- CxImage xImage;
- int imgForm = analysisImageFormat(lpszPathName);
- switch (imgForm) //先进行格式统一处理,调用cxImage图像处理库
- {
- case IMG_FORMAT_BMP:
- xImage.Load(lpszPathName, CXIMAGE_FORMAT_BMP);
- break;
- case IMG_FORMAT_JPEG:
- xImage.Load(lpszPathName, CXIMAGE_FORMAT_JPG);
- break;
- case IMG_FORMAT_GIF:
- xImage.Load(lpszPathName, CXIMAGE_FORMAT_GIF);
- break;
- case IMG_FORMAT_PNG:
- xImage.Load(lpszPathName, CXIMAGE_FORMAT_PNG);
- break;
- case IMG_FORMAT_TIFF:
- xImage.Load(lpszPathName, CXIMAGE_FORMAT_TIF);
- break;
- default:
- return FALSE;
- }
- CString lpszNewPathName = alterFileExtension(lpszPathName, imgForm);
- xImage.Save(lpszNewPathName, CXIMAGE_FORMAT_BMP); //先保存为位图格式
- xImage.Load(lpszNewPathName, CXIMAGE_FORMAT_BMP);
- if (xImage.IsValid())
- {
- if (xImage.GetBpp()>8)
- {
- xImage.DecreaseBpp(8, true);
- }
- else if (xImage.GetBpp()<8)
- {
- xImage.IncreaseBpp(8);
- }
- xImage.GrayScale(); //转存为8位灰度位图
- if (!xImage.IsGrayScale())
- {
- return FALSE;
- }
- }
- else
- {
- return FALSE;
- }
- xImage.Save(lpszNewPathName, CXIMAGE_FORMAT_BMP);
- loadIntoMemory(lpszNewPathName); //载入内存,自己编写边缘算子函数进行处理
- imageEdgeOperation(nOperatorSel);
- saveAfterProcess(lpszNewPathName);
- xImage.Load(lpszNewPathName, CXIMAGE_FORMAT_BMP);
- switch (imgForm)
- {
- case IMG_FORMAT_BMP:
- break;
- case IMG_FORMAT_JPEG:
- xImage.Save(lpszPathName, CXIMAGE_FORMAT_JPG);
- ::DeleteFile(lpszNewPathName);
- break;
- case IMG_FORMAT_GIF:
- xImage.Save(lpszPathName, CXIMAGE_FORMAT_GIF);
- ::DeleteFile(lpszNewPathName);
- break;
- case IMG_FORMAT_PNG:
- xImage.Save(lpszPathName, CXIMAGE_FORMAT_PNG);
- ::DeleteFile(lpszNewPathName);
- break;
- case IMG_FORMAT_TIFF:
- xImage.Save(lpszPathName, CXIMAGE_FORMAT_TIF);
- ::DeleteFile(lpszNewPathName);
- break;
- }
- return TRUE;
- }
- CString CDib::alterFileExtension(LPCTSTR lpszPathName, int nImgFormat)
- {
- CString strImgForm(lpszPathName);
- int index;
- switch (nImgFormat)
- {
- case IMG_FORMAT_BMP:
- break;
- case IMG_FORMAT_JPEG:
- strImgForm.Replace(_T(".jpg"), _T(".bmp"));
- strImgForm.Replace(_T(".jpeg"), _T(".bmp"));
- strImgForm.Replace(_T(".JPG"), _T(".BMP"));
- strImgForm.Replace(_T(".JPEG"), _T(".BMP"));
- break;
- case IMG_FORMAT_GIF:
- strImgForm.Replace(_T(".gif"), _T(".bmp"));
- strImgForm.Replace(_T(".GIF"), _T(".BMP"));
- break;
- case IMG_FORMAT_PNG:
- strImgForm.Replace(_T(".png"), _T(".bmp"));
- strImgForm.Replace(_T(".PNG"), _T(".BMP"));
- break;
- case IMG_FORMAT_TIFF:
- strImgForm.Replace(_T(".tif"), _T(".bmp"));
- strImgForm.Replace(_T(".tiff"), _T(".bmp"));
- strImgForm.Replace(_T(".TIF"), _T(".BMP"));
- strImgForm.Replace(_T(".TIFF"), _T(".BMP"));
- break;
- }
- return strImgForm;
- }
- int CDib::analysisImageFormat(LPCTSTR lpszPathName)
- {
- CString strImgForm(lpszPathName);
- int index;
- index = (((strImgForm.Find(_T(".bmp"))!=-1) || (strImgForm.Find(_T(".BMP"))!=-1)) ? 0 : -1);
- //index = strImgForm.Find(".bmp");
- if (index > -1)
- {
- return IMG_FORMAT_BMP;
- }
- index = (((strImgForm.Find(_T(".gif"))!=-1) || (strImgForm.Find(_T(".GIF"))!=-1)) ? 0 : -1);
- //index = strImgForm.Find(".gif");
- if (index > -1)
- {
- return IMG_FORMAT_GIF;
- }
- index = (((strImgForm.Find(_T(".png"))!=-1) || (strImgForm.Find(_T(".PNG"))!=-1)) ? 0 : -1);
- //index = strImgForm.Find(".png");
- if (index > -1)
- {
- return IMG_FORMAT_PNG;
- }
- index = (((strImgForm.Find(_T(".jpg"))!=-1) || (strImgForm.Find(_T(".jpeg"))!=-1) || (strImgForm.Find(_T(".JPG"))!=-1) || (strImgForm.Find(_T(".JPEG"))!=-1)) ? 0 : -1);
- if (index > -1)
- {
- return IMG_FORMAT_JPEG;
- }
- index = (((strImgForm.Find(_T(".tif"))!=-1) || (strImgForm.Find(_T(".tiff"))!=-1) || (strImgForm.Find(_T(".TIF"))!=-1) || (strImgForm.Find(_T(".TIFF"))!=-1)) ? 0 : -1);
- if (index > -1)
- {
- return IMG_FORMAT_TIFF;
- }
- return IMG_FORMAT_DEFAULT;
- }
- BOOL CDib::loadIntoMemory(LPCTSTR lpszPathName)
- {
- CFile imgFile;
- // 打开位图文件
- if (!imgFile.Open(lpszPathName, CFile::modeReadWrite | CFile::shareExclusive))
- {
- return FALSE;
- }
- // 获得位图文件大小,并减去BITMAPFILEHEADER的长度
- DWORD dwDibSize;
- dwDibSize = imgFile.GetLength() - sizeof(BITMAPFILEHEADER);
- // 为DIB位图分配内存
- unsigned char* pDib;
- pDib = new unsigned char[dwDibSize];
- if (pDib == NULL)
- {
- return FALSE;
- }
- BITMAPFILEHEADER BFH;
- // 读取位图文件数据
- try
- {
- // 文件格式是否正确有效
- if (imgFile.Read(&BFH, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER) ||
- BFH.bfType != 'MB' ||
- imgFile.Read(pDib, dwDibSize) != dwDibSize)
- {
- delete[] pDib;
- return FALSE;
- }
- }
- catch (CFileException* e)
- {
- e->Delete();
- delete[] pDib;
- return FALSE;
- }
- // delete先前加载的位图
- if (m_pDib != NULL)
- {
- delete m_pDib;
- }
- // 将临时Dib数据指针和Dib大小变量赋给类成员变量
- m_pDib = pDib;
- m_dwDibSize = dwDibSize;
- // 为相应类成员变量赋BITMAPINFOHEADER和调色板指针
- m_pBIH = (BITMAPINFOHEADER*)m_pDib;
- m_pPalette = (RGBQUAD*)&m_pDib[sizeof(BITMAPINFOHEADER)];
- m_Width = m_pBIH->biWidth;
- m_Height = m_pBIH->biHeight;
- // 计算调色板中实际颜色数量
- 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;
- }
- // 为相应类成员变量赋image data指针
- m_pDibBits = &m_pDib[sizeof(BITMAPINFOHEADER) + m_nPaletteEntries * sizeof(RGBQUAD)];
- // delete先前的调色板
- if (m_Palette.GetSafeHandle() != NULL)
- {
- m_Palette.DeleteObject();
- }
- // 如果位图中存在调色板,创建LOGPALETTE 及CPalette
- if (m_nPaletteEntries != 0)
- {
- LOGPALETTE* pLogPal = (LOGPALETTE*)new char[sizeof(LOGPALETTE) + m_nPaletteEntries * sizeof(PALETTEENTRY)];
- if (pLogPal != NULL)
- {
- pLogPal->palVersion = 0x300;
- pLogPal->palNumEntries = m_nPaletteEntries;
- 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;
- }
- //创建CPalette并释放LOGPALETTE的内存
- m_Palette.CreatePalette(pLogPal);
- delete[] pLogPal;
- }
- }
- return TRUE;
- }
- BOOL CDib::imageEdgeOperation(UINT nOperatorSel)
- {
- switch (nOperatorSel)
- {
- case 0:
- LaplaceOperator();
- break;
- case 1:
- CannyOperator(0.1, 0.9, 0.3);
- break;
- default:
- break;
- }
- return TRUE;
- }
- BOOL CDib::LaplaceOperator(void)
- {
- unsigned char* lpSrc0, *lpSrc1, *lpSrc2, *lpSrc3, *lpSrc4, *lpSrc5, *lpSrc6, *lpSrc7, *lpSrc8;
- BYTE* p_temp = new BYTE[m_Height * m_Width];
- memset(p_temp, 255, m_Height * m_Width);
- for(int i=1; i<m_Height-1; i++)
- {
- for(int j=1; j<m_Width-1; j++)
- {
- lpSrc0 = m_pDibBits + i * m_Width + j;
- lpSrc1 = m_pDibBits + i * m_Width + j - 1;
- lpSrc2 = m_pDibBits + i * m_Width + j + 1;
- lpSrc3 = m_pDibBits + (i-1) * m_Width + j;
- lpSrc4 = m_pDibBits + (i+1) * m_Width + j;
- lpSrc5 = m_pDibBits + (i-1) * m_Width + j - 1;
- lpSrc6 = m_pDibBits + (i-1) * m_Width + j + 1;
- lpSrc7 = m_pDibBits + (i+1) * m_Width + j - 1;
- lpSrc8 = m_pDibBits + (i+1) * m_Width + j + 1;
- p_temp[i * m_Width + j] = abs(*lpSrc0 * 8 + *lpSrc1 * (-1) + *lpSrc2 * (-1) +
- *lpSrc3 * (-1) + *lpSrc4 * (-1) + *lpSrc5 * (-1) +
- *lpSrc6 * (-1) + *lpSrc7 * (-1) + *lpSrc8 * (-1));
- }
- }
- memcpy(m_pDibBits, p_temp, m_Height * m_Width);
- delete[] p_temp;
- return TRUE;
- }
- void CDib::CreatGauss(double sigma, double** pdKernel, int* pnWidowSize)
- {
- LONG i;
- // 数组中心点
- int nCenter;
- // 数组中一点到中心点距离
- double dDis;
- // 中间变量
- double dValue;
- double dSum;
- dSum = 0;
- // [-3*sigma,3*sigma] 以内数据,会覆盖绝大部分滤波系数
- *pnWidowSize = (int)(1+2*ceil(3*sigma));
- nCenter = (*pnWidowSize)/2;
- *pdKernel = new double[*pnWidowSize];
- //生成高斯数据
- for (i=0; i<(*pnWidowSize); i++)
- {
- dDis = double(i - nCenter);
- dValue = exp(-(1/2)*dDis*dDis/(sigma*sigma))/(sqrt(2*3.1415926)*sigma);
- (*pdKernel)[i] = dValue;
- dSum += dValue;
- }
- //归一化
- for (i=0; i<(*pnWidowSize); i++)
- {
- (*pdKernel)[i] /= dSum;
- }
- }
- void CDib::GaussianSmooth(unsigned char* pResult, double sigma)
- {
- LONG x, y;
- LONG i;
- int nWindowSize; // 高斯滤波器长度
- int nLen; // 窗口长度
- double* pdKernel; // 一维高斯滤波器
- double dDotMul; // 高斯系数与图像数据的点乘
- double dWeightSum; // 滤波系数总和
- double* pdTemp = new double[m_Height*m_Width];
- //产生一维高斯数据
- CreatGauss(sigma, &pdKernel, &nWindowSize);
- nLen = nWindowSize/2;
- for (y=0; y<m_Height; y++) // x方向滤波
- {
- for (x=0; x<m_Width; x++)
- {
- dDotMul = 0;
- dWeightSum = 0;
- for (i=(-nLen); i<=nLen; i++)
- {
- //判断是否在图像内部
- if ((i+x)>=0 && (i+x)<m_Width)
- {
- dDotMul += (double)(*(m_pDibBits+y*m_Width+(i+x))) * pdKernel[nLen+i];
- dWeightSum += pdKernel[nLen+i];
- }
- }
- pdTemp[y*m_Width+x] = dDotMul/dWeightSum;
- }
- }
- for (x=0; x<m_Width; x++) // y方向滤波
- {
- for (y=0; y<m_Height; y++)
- {
- dDotMul = 0;
- dWeightSum = 0;
- for (i=(-nLen); i<=nLen; i++)
- {
- if ((i+y)>=0 && (i+y)<m_Height)
- {
- dDotMul += (double)pdTemp[(y+i)*m_Width+x]*pdKernel[nLen+i];
- dWeightSum += pdKernel[nLen+i];
- }
- }
- pResult[y*m_Width+x] = (unsigned char)dDotMul/dWeightSum;
- }
- }
- delete[] pdKernel;
- pdKernel = NULL;
- delete[] pdTemp;
- pdTemp = NULL;
- }
- void CDib::Grad(unsigned char* pGray, int* pGradX, int* pGradY, int* pMag)
- {
- LONG y, x;
- for (y=1; y<m_Height-1; y++) // x方向的方向导数
- {
- for (x=1; x<m_Width-1; x++)
- {
- pGradX[y*m_Width + x] = (int)(pGray[y*m_Width+x+1] - pGray[y*m_Width+x-1]);
- }
- }
- for (x=1; x<m_Width-1; x++) // y方向方向导数
- {
- for (y=1; y<m_Height-1; y++)
- {
- pGradY[y*m_Width + x] = (int)(pGray[(y+1)*m_Width+x] - pGray[(y-1)*m_Width+x]);
- }
- }
- //求梯度
- double dSqt1, dSqt2; //中间变量
- for (y=0; y<m_Height; y++)
- {
- for (x=0; x<m_Width; x++)
- {
- //二阶范数求梯度
- dSqt1 = pGradX[y*m_Width + x]*pGradX[y*m_Width + x];
- dSqt2 = pGradY[y*m_Width + x]*pGradY[y*m_Width + x];
- pMag[y*m_Width + x] = (int)(sqrt(dSqt1+dSqt2)+0.5);
- }
- }
- }
- //非最大抑制
- void CDib::NonmaxSuppress(int* pMag, int* pGradX, int* pGradY, unsigned char* pNSRst)
- {
- LONG y,x;
- int nPos;
- int gx, gy; // 梯度分量
- //中间变量
- int g1, g2, g3, g4;
- double weight;
- double dTmp, dTmp1, dTmp2;
- //设置图像边缘为不可能的分界点
- for (x=0; x<m_Width; x++)
- {
- pNSRst[x] = 0;
- pNSRst[(m_Height-1)*m_Width+x] = 0;
- }
- for (y=0; y<m_Height; y++)
- {
- pNSRst[y*m_Width] = 0;
- pNSRst[y*m_Width + m_Width - 1] = 0;
- }
- for (y=1; y<m_Height-1; y++)
- {
- for (x=1; x<m_Width-1; x++)
- {
- nPos = y*m_Width + x; //当前点
- if (pMag[nPos] == 0) // 如果当前像素梯度幅度为0,则不是边界点
- {
- pNSRst[nPos] = 0;
- }
- else
- {
- dTmp = pMag[nPos]; // 当前点的梯度幅度
- //x,y方向导数
- gx = pGradX[nPos];
- gy = pGradY[nPos];
- if (abs(gy) > abs(gx)) //如果方向导数y分量比x分量大,说明导数方向趋向于y分量
- {
- weight = fabs((double)gx)/fabs((double)gy); // 计算插值比例
- g2 = pMag[nPos-m_Width];
- g4 = pMag[nPos+m_Width];
- //如果x,y两个方向导数的符号相同
- //C 为当前像素,与g1-g4 的位置关系为:
- //g1 g2
- // C
- // g4 g3
- if(gx*gy>0)
- {
- g1 = pMag[nPos-m_Width-1];
- g3 = pMag[nPos+m_Width+1];
- }
- //如果x,y两个方向的方向导数方向相反
- //C是当前像素,与g1-g4的关系为:
- // g2 g1
- // C
- // g3 g4
- else
- {
- g1 = pMag[nPos-m_Width+1];
- g3 = pMag[nPos+m_Width-1];
- }
- }
- else // 如果方向导数x分量比y分量大,说明导数的方向趋向于x分量
- {
- weight = fabs((double)gy)/fabs((double)gx); // 插值比例
- g2 = pMag[nPos+1];
- g4 = pMag[nPos-1];
- //如果x,y两个方向的方向导数符号相同
- //当前像素C与 g1-g4的关系为
- // g3
- // g4 C g2
- // g1
- if(gx * gy > 0)
- {
- g1 = pMag[nPos+m_Width+1];
- g3 = pMag[nPos-m_Width-1];
- }
- //如果x,y两个方向导数的方向相反
- // C与g1-g4的关系为
- // g1
- // g4 C g2
- // g3
- else
- {
- g1 = pMag[nPos-m_Width+1];
- g3 = pMag[nPos+m_Width-1];
- }
- }
- //利用 g1-g4 对梯度进行插值
- {
- dTmp1 = weight*g1 + (1-weight)*g2;
- dTmp2 = weight*g3 + (1-weight)*g4;
- //当前像素的梯度是局部的最大值
- //该点可能是边界点
- if(dTmp>=dTmp1 && dTmp>=dTmp2)
- {
- pNSRst[nPos] = 128;
- }
- else
- {
- //不可能是边界点
- pNSRst[nPos] = 0;
- }
- }
- }
- }
- }
- }
- void CDib::EstimateThreshold(int* pMag, int* pThrHigh, int* pThrLow, unsigned char* pGray, double dRatHigh, double dRatLow)
- {
- LONG y, x, k;
- int nHist[256]; //该数组的大小和梯度值的范围有关,如果采用本程序的算法那么梯度的范围不会超过pow(2,10)
- int nEdgeNum; // 可能边界数
- int nMaxMag; // 最大梯度数
- int nHighCount;
- nMaxMag = 0;
- for (k=0; k<256; k++) // 初始化
- {
- nHist[k] = 0;
- }
- for (y=0; y<m_Height; y++) // 统计直方图,利用直方图计算阈值
- {
- for (x=0; x<m_Width; x++)
- {
- if (pGray[y*m_Width+x]==128)
- {
- nHist[pMag[y*m_Width+x]]++;
- }
- }
- }
- nEdgeNum = nHist[0];
- nMaxMag = 0;
- for (k=1; k<256; k++) //统计经过“非最大值抑制”后有多少像素
- {
- if (nHist[k] != 0)
- {
- nMaxMag = k;
- }
- //梯度为0的点是不可能为边界点的
- //经过non-maximum suppression后有多少像素
- nEdgeNum += nHist[k];
- }
- nHighCount = (int)(dRatHigh * nEdgeNum + 0.5); // 梯度比高阈值*pThrHigh 小的像素点总书目
- k=1;
- nEdgeNum = nHist[1];
- while ((k<(nMaxMag-1)) && (nEdgeNum<nHighCount)) //计算高阈值
- {
- k++;
- nEdgeNum += nHist[k];
- }
- *pThrHigh = k;
- *pThrLow = (int)((*pThrHigh) * dRatLow + 0.5); //低阈值
- }
- //利用函数寻找边界起点
- void CDib::Hysteresis(int* pMag, double dRatLow, double dRatHigh, unsigned char* pResult)
- {
- LONG y,x;
- int nThrHigh, nThrLow;
- int nPos;
- EstimateThreshold(pMag, &nThrHigh, &nThrLow, pResult, dRatHigh, dRatLow); //估计TraceEdge 函数需要的低阈值,以及Hysteresis函数使用的高阈值
- //寻找大于dThrHigh的点,这些点用来当作边界点,
- //然后用TraceEdge函数跟踪该点对应的边界
- for (y=0; y<m_Height; y++)
- {
- for (x=0; x<m_Width; x++)
- {
- nPos = y*m_Width + x;
- //如果该像素是可能的边界点,并且梯度大于高阈值,该像素作为一个边界的起点
- if ((pResult[nPos]==128) && (pMag[nPos]>=nThrHigh))
- {
- //设置该点为边界点
- pResult[nPos] = 255;
- TraceEdge(y, x, nThrLow, pResult, pMag);
- }
- }
- }
- //其他点已经不可能为边界点
- for (y=0; y<m_Height; y++)
- {
- for (x=0; x<m_Width; x++)
- {
- nPos = y*m_Width + x;
- if (pResult[nPos] != 255)
- {
- pResult[nPos] = 0;
- }
- }
- }
- }
- //根据Hysteresis 执行的结果,从一个像素点开始搜索,搜索以该像素点为边界起点的一条边界的
- //一条边界的所有边界点,函数采用了递归算法
- // 从(x,y)坐标出发,进行边界点的跟踪,跟踪只考虑pResult中没有处理并且可能是边界
- // 点的像素(=128),像素值为0表明该点不可能是边界点,像素值为255表明该点已经是边界点
- void CDib::TraceEdge(int y, int x, int nThrLow, unsigned char* pResult, int* pMag)
- {
- //对8邻域像素进行查询
- int xNum[8] = {1, 1, 0, -1, -1, -1, 0, 1};
- int yNum[8] = {0, 1, 1, 1, 0, -1, -1, -1};
- LONG yy, xx, k;
- for (k=0; k<8; k++)
- {
- yy = y+yNum[k];
- xx = x+xNum[k];
- if (pResult[yy*m_Width+xx]==128 && pMag[yy*m_Width+xx]>=nThrLow)
- {
- pResult[yy*m_Width+xx] = 255; // 该点设为边界点
- TraceEdge(yy, xx, nThrLow, pResult, pMag); // 以该点为中心再进行跟踪
- }
- }
- }
- // Canny算子
- BOOL CDib::CannyOperator(double sigma, double dRatLow, double dRatHigh)
- {
- unsigned char* pResult = new unsigned char[m_Height*m_Width];
- memset(pResult, 0, m_Height*m_Width);
- unsigned char* pGaussSmooth = new unsigned char[m_Height*m_Width]; // 经过高斯滤波后的图像
- memset(pGaussSmooth, 0, m_Height*m_Width);
- int* pGradX = new int[m_Height*m_Width]; // x方向导数的指针
- int* pGradY = new int[m_Height*m_Width]; // y方向
- int* pGradMag = new int[m_Height*m_Width]; // 梯度的幅度
- GaussianSmooth(pGaussSmooth, sigma); // 对原图高斯滤波
- Grad(pGaussSmooth, pGradX, pGradY, pGradMag); // 计算方向导数和梯度的幅度
- NonmaxSuppress(pGradMag, pGradX, pGradY, pResult); // 应用非最大抑制
- Hysteresis(pGradMag, dRatLow, dRatHigh, pResult); // 应用Hysteresis,找到所有边界
- delete[] pGradX;
- pGradX = NULL;
- delete[] pGradY;
- pGradY = NULL;
- delete[] pGradMag;
- pGradMag = NULL;
- delete[] pGaussSmooth;
- pGaussSmooth = NULL;
- memcpy(m_pDibBits, pResult, m_Height * m_Width);
- delete[] pResult;
- return TRUE;
- }
- BOOL CDib::saveAfterProcess(LPCTSTR lpszPathName)
- {
- if (m_pDib == NULL)
- {
- return FALSE;
- }
- CFile imgFile;
- if (!imgFile.Open(lpszPathName, CFile::modeCreate | CFile::modeWrite))
- {
- return FALSE;
- }
- try
- {
- 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);
- imgFile.Write(&BFH, sizeof(BITMAPFILEHEADER));
- imgFile.Write(m_pDib, m_dwDibSize);
- }
- catch (CFileException* e)
- {
- e->Delete();
- return FALSE;
- }
- return TRUE;
- }