Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 46

dibapi.

cpp in wienerfilter.rar

use Wiener filtering algorithm for image restoration of the


source vc...Original Link

// dibapi.cpp
//
// Source file for Device-Independent Bitmap (DIB) API. Provides
// the following functions:
//
// PaintDIB() - Painting routine for a DIB
// CreateDIBPalette() - Creates a palette from a DIB
// FindDIBBits() - Returns a pointer to the DIB bits
// DIBWidth() - Gets the width of the DIB
// DIBHeight() - Gets the height of the DIB
// PaletteSize() - Gets the size required to store the DIB's
palette
// DIBNumColors() - Calculates the number of colors
// in the DIB's color table
// CopyHandle() - Makes a copy of the given global memory block
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1997 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"
#include "dibapi.h"
#include <io.h>
#include <errno.h>
#include <math.h>

#define DIB_HEADER_MARKER ((WORD) ('M' << 8) | 'B')

#ifdef _MAC
#define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
#define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
void ByteSwapHeader(BITMAPFILEHEADER* bmiHeader);
void ByteSwapInfo(LPSTR lpHeader, BOOL fWin30Header);
#endif

/
***********************************************************************
**
*
* PaintDIB()
*
* Parameters:
*
* HDC hDC - DC to do output to
*
* LPRECT lpDCRect - rectangle on DC to do output to
*
* HDIB hDIB - handle to global memory with a DIB spec
* in it followed by the DIB bits
*
* LPRECT lpDIBRect - rectangle of DIB to output into lpDCRect
*
* CPalette* pPal - pointer to CPalette containing DIB's palette
*
* Return Value:
*
* BOOL - TRUE if DIB was drawn, FALSE otherwise
*
* Description:
* Painting routine for a DIB. Calls StretchDIBits() or
* SetDIBitsToDevice() to paint the DIB. The DIB is
* output to the specified DC, at the coordinates given
* in lpDCRect. The area of the DIB to be output is
* given by lpDIBRect.
*

***********************************************************************
*/

BOOL WINAPI PaintDIB(HDC hDC,


LPRECT lpDCRect,
HDIB hDIB,
LPRECT lpDIBRect,
CPalette* pPal)
{
LPSTR lpDIBHdr; // Pointer to BITMAPINFOHEADER
LPSTR lpDIBBits; // Pointer to DIB bits
BOOL bSuccess=FALSE; // Success/fail flag
HPALETTE hPal=NULL; // Our DIB's palette
HPALETTE hOldPal=NULL; // Previous palette

/* Check for valid DIB handle */


if (hDIB == NULL)
return FALSE;

/* Lock down the DIB, and get a pointer to the beginning of the
bit
* buffer
*/
lpDIBHdr = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
lpDIBBits = ::FindDIBBits(lpDIBHdr);

// Get the DIB's palette, then select it into DC


if (pPal != NULL)
{
hPal = (HPALETTE) pPal->m_hObject;

// Select as background since we have


// already realized in forground if needed
hOldPal = ::SelectPalette(hDC, hPal, TRUE);
}

/* Make sure to use the stretching mode best for color pictures
*/
::SetStretchBltMode(hDC, COLORONCOLOR);

/* Determine whether to call StretchDIBits() or


SetDIBitsToDevice() */
if ((RECTWIDTH(lpDCRect) == RECTWIDTH(lpDIBRect)) &&
(RECTHEIGHT(lpDCRect) == RECTHEIGHT(lpDIBRect)))
bSuccess = ::SetDIBitsToDevice(hDC,
// hDC
lpDCRect->left, // DestX

lpDCRect->top, // DestY

RECTWIDTH(lpDCRect), // nDestWidth

RECTHEIGHT(lpDCRect), // nDestHeight

lpDIBRect->left, // SrcX

(int)DIBHeight(lpDIBHdr) -

lpDIBRect->top -

RECTHEIGHT(lpDIBRect), // SrcY
0,
// nStartScan

(WORD)DIBHeight(lpDIBHdr), // nNumScans

lpDIBBits, // lpBits

(LPBITMAPINFO)lpDIBHdr, // lpBitsInfo

DIB_RGB_COLORS); // wUsage
else
bSuccess = ::StretchDIBits(hDC, //
hDC
lpDCRect->left,
// DestX
lpDCRect->top,
// DestY

RECTWIDTH(lpDCRect), // nDestWidth

RECTHEIGHT(lpDCRect), // nDestHeight
lpDIBRect-
>left, // SrcX
lpDIBRect->top,
// SrcY

RECTWIDTH(lpDIBRect), // wSrcWidth

RECTHEIGHT(lpDIBRect), // wSrcHeight
lpDIBBits,
// lpBits

(LPBITMAPINFO)lpDIBHdr, // lpBitsInfo
DIB_RGB_COLORS,
// wUsage
SRCCOPY);
// dwROP

::GlobalUnlock((HGLOBAL) hDIB);

/* Reselect old palette */


if (hOldPal != NULL)
{
::SelectPalette(hDC, hOldPal, TRUE);
}

return bSuccess;
}

/
***********************************************************************
**
*
* CreateDIBPalette()
*
* Parameter:
*
* HDIB hDIB - specifies the DIB
*
* Return Value:
*
* HPALETTE - specifies the palette
*
* Description:
*
* This function creates a palette from a DIB by allocating memory for
the
* logical palette, reading and storing the colors from the DIB's color
table
* into the logical palette, creating a palette from this logical
palette,
* and then returning the palette's handle. This allows the DIB to be
* displayed using the best possible colors (important for DIBs with
256 or
* more colors).
*

***********************************************************************
*/

BOOL WINAPI CreateDIBPalette(HDIB hDIB, CPalette* pPal)


{
LPLOGPALETTE lpPal; // pointer to a logical palette
HANDLE hLogPal; // handle to a logical palette
HPALETTE hPal = NULL; // handle to a palette
int i; // loop index
WORD wNumColors; // number of colors in color table
LPSTR lpbi; // pointer to packed-DIB
LPBITMAPINFO lpbmi; // pointer to BITMAPINFO structure
(Win3.0)
LPBITMAPCOREINFO lpbmc; // pointer to BITMAPCOREINFO structure
(old)
BOOL bWinStyleDIB; // flag which signifies whether this
is a Win3.0 DIB
BOOL bResult = FALSE;

/* if handle to DIB is invalid, return FALSE */

if (hDIB == NULL)
return FALSE;

lpbi = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);

/* get pointer to BITMAPINFO (Win 3.0) */


lpbmi = (LPBITMAPINFO)lpbi;

/* get pointer to BITMAPCOREINFO (old 1.x) */


lpbmc = (LPBITMAPCOREINFO)lpbi;
/* get the number of colors in the DIB */
wNumColors = ::DIBNumColors(lpbi);

if (wNumColors != 0)
{
/* allocate memory block for logical palette */
hLogPal = ::GlobalAlloc(GHND, sizeof(LOGPALETTE)
+
sizeof(PALETTEENTRY)
*
wNumColors);

/* if not enough memory, clean up and return NULL */


if (hLogPal == 0)
{
::GlobalUnlock((HGLOBAL) hDIB);
return FALSE;
}

lpPal = (LPLOGPALETTE) ::GlobalLock((HGLOBAL) hLogPal);

/* set version and number of palette entries */


lpPal->palVersion = PALVERSION;
lpPal->palNumEntries = (WORD)wNumColors;

/* is this a Win 3.0 DIB? */


bWinStyleDIB = IS_WIN30_DIB(lpbi);
for (i = 0; i < (int)wNumColors; i++)
{
if (bWinStyleDIB)
{
lpPal->palPalEntry[i].peRed = lpbmi-
>bmiColors[i].rgbRed;
lpPal->palPalEntry[i].peGreen = lpbmi-
>bmiColors[i].rgbGreen;
lpPal->palPalEntry[i].peBlue = lpbmi-
>bmiColors[i].rgbBlue;
lpPal->palPalEntry[i].peFlags = 0;
}
else
{
lpPal->palPalEntry[i].peRed = lpbmc-
>bmciColors[i].rgbtRed;
lpPal->palPalEntry[i].peGreen = lpbmc-
>bmciColors[i].rgbtGreen;
lpPal->palPalEntry[i].peBlue = lpbmc-
>bmciColors[i].rgbtBlue;
lpPal->palPalEntry[i].peFlags = 0;
}
}

/* create the palette and get handle to it */


bResult = pPal->CreatePalette(lpPal);
::GlobalUnlock((HGLOBAL) hLogPal);
::GlobalFree((HGLOBAL) hLogPal);
}

::GlobalUnlock((HGLOBAL) hDIB);

return bResult;
}

/
***********************************************************************
**
*
* FindDIBBits()
*
* Parameter:
*
* LPSTR lpbi - pointer to packed-DIB memory block
*
* Return Value:
*
* LPSTR - pointer to the DIB bits
*
* Description:
*
* This function calculates the address of the DIB's bits and returns a
* pointer to the DIB bits.
*
***********************************************************************
*/

LPSTR WINAPI FindDIBBits(LPSTR lpbi)


{
return (lpbi + *(LPDWORD)lpbi + ::PaletteSize(lpbi));
}

/
***********************************************************************
**
*
* DIBWidth()
*
* Parameter:
*
* LPSTR lpbi - pointer to packed-DIB memory block
*
* Return Value:
*
* DWORD - width of the DIB
*
* Description:
*
* This function gets the width of the DIB from the BITMAPINFOHEADER
* width field if it is a Windows 3.0-style DIB or from the
BITMAPCOREHEADER
* width field if it is an other-style DIB.
*

***********************************************************************
*/

DWORD WINAPI DIBWidth(LPSTR lpDIB)


{
LPBITMAPINFOHEADER lpbmi; // pointer to a Win 3.0-style DIB
LPBITMAPCOREHEADER lpbmc; // pointer to an other-style DIB
/* point to the header (whether Win 3.0 and old) */

lpbmi = (LPBITMAPINFOHEADER)lpDIB;
lpbmc = (LPBITMAPCOREHEADER)lpDIB;

/* return the DIB width if it is a Win 3.0 DIB */


if (IS_WIN30_DIB(lpDIB))
return lpbmi->biWidth;
else /* it is an other-style DIB, so return its width */
return (DWORD)lpbmc->bcWidth;
}

/
***********************************************************************
**
*
* DIBHeight()
*
* Parameter:
*
* LPSTR lpbi - pointer to packed-DIB memory block
*
* Return Value:
*
* DWORD - height of the DIB
*
* Description:
*
* This function gets the height of the DIB from the BITMAPINFOHEADER
* height field if it is a Windows 3.0-style DIB or from the
BITMAPCOREHEADER
* height field if it is an other-style DIB.
*

***********************************************************************
*/

DWORD WINAPI DIBHeight(LPSTR lpDIB)


{
LPBITMAPINFOHEADER lpbmi; // pointer to a Win 3.0-style DIB
LPBITMAPCOREHEADER lpbmc; // pointer to an other-style DIB

/* point to the header (whether old or Win 3.0 */

lpbmi = (LPBITMAPINFOHEADER)lpDIB;
lpbmc = (LPBITMAPCOREHEADER)lpDIB;

/* return the DIB height if it is a Win 3.0 DIB */


if (IS_WIN30_DIB(lpDIB))
return lpbmi->biHeight;
else /* it is an other-style DIB, so return its height */
return (DWORD)lpbmc->bcHeight;
}

/
***********************************************************************
**
*
* PaletteSize()
*
* Parameter:
*
* LPSTR lpbi - pointer to packed-DIB memory block
*
* Return Value:
*
* WORD - size of the color palette of the DIB
*
* Description:
*
* This function gets the size required to store the DIB's palette by
* multiplying the number of colors by the size of an RGBQUAD (for a
* Windows 3.0-style DIB) or by the size of an RGBTRIPLE (for an other-
* style DIB).
*

***********************************************************************
*/
WORD WINAPI PaletteSize(LPSTR lpbi)
{
/* calculate the size required by the palette */
if (IS_WIN30_DIB (lpbi))
return (WORD)(::DIBNumColors(lpbi) * sizeof(RGBQUAD));
else
return (WORD)(::DIBNumColors(lpbi) * sizeof(RGBTRIPLE));
}

/
***********************************************************************
**
*
* DIBNumColors()
*
* Parameter:
*
* LPSTR lpbi - pointer to packed-DIB memory block
*
* Return Value:
*
* WORD - number of colors in the color table
*
* Description:
*
* This function calculates the number of colors in the DIB's color
table
* by finding the bits per pixel for the DIB (whether Win3.0 or other-
style
* DIB). If bits per pixel is 1: colors=2, if 4: colors=16, if 8:
colors=256,
* if 24, no colors in color table.
*

***********************************************************************
*/
WORD WINAPI DIBNumColors(LPSTR lpbi)
{
WORD wBitCount; // DIB bit count

/* If this is a Windows-style DIB, the number of colors in the


* color table can be less than the number of bits per pixel
* allows for (i.e. lpbi->biClrUsed can be set to some value).
* If this is the case, return the appropriate value.
*/

if (IS_WIN30_DIB(lpbi))
{
DWORD dwClrUsed;

dwClrUsed = ((LPBITMAPINFOHEADER)lpbi)->biClrUsed;
if (dwClrUsed != 0)
return (WORD)dwClrUsed;
}

/* Calculate the number of colors in the color table based on


* the number of bits per pixel for the DIB.
*/
if (IS_WIN30_DIB(lpbi))
wBitCount = ((LPBITMAPINFOHEADER)lpbi)->biBitCount;
else
wBitCount = ((LPBITMAPCOREHEADER)lpbi)->bcBitCount;

/* return number of colors based on bits per pixel */


switch (wBitCount)
{
case 1:
return 2;

case 4:
return 16;

case 8:
return 256;

default:
return 0;
}
}

///////////////////////////////////////////////////////////////////////
///
//// Clipboard support

//---------------------------------------------------------------------
//
// Function: CopyHandle (from SDK DibView sample clipbrd.c)
//
// Purpose: Makes a copy of the given global memory block. Returns
// a handle to the new memory block (NULL on error).
//
// Routine stolen verbatim out of ShowDIB.
//
// Parms: h == Handle to global memory to duplicate.
//
// Returns: Handle to new global memory block.
//
//---------------------------------------------------------------------

HGLOBAL WINAPI CopyHandle (HGLOBAL h)


{
if (h == NULL)
return NULL;

DWORD dwLen = ::GlobalSize((HGLOBAL) h);


HGLOBAL hCopy = ::GlobalAlloc(GHND, dwLen);

if (hCopy != NULL)
{
void* lpCopy = ::GlobalLock((HGLOBAL) hCopy);
void* lp = ::GlobalLock((HGLOBAL) h);
memcpy(lpCopy, lp, dwLen);
::GlobalUnlock(hCopy);
::GlobalUnlock(h);
}

return hCopy;
}
void WINAPI ConvertShort(unsigned short *array, long length)
{
unsigned long b1, b2;
unsigned char *ptr;

ptr = (unsigned char *)array;


while (length--) {
b1 = *ptr++;
b2 = *ptr++;
*array++ = (unsigned short)((b1 << 8) | (b2));
}
}

void WINAPI ConvertLong(unsigned long *array, long length)


{
unsigned long b1, b2, b3, b4;
unsigned char *ptr;

ptr = (unsigned char *)array;


while (length--) {
b1 = *ptr++;
b2 = *ptr++;
b3 = *ptr++;
b4 = *ptr++;
*array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
}
}

/
***********************************************************************
**
*
* SaveDIB()
*
* Saves the specified DIB into the specified CFile. The CFile
* is opened and closed by the caller.
*
* Parameters:
*
* HDIB hDib - Handle to the dib to save
*
* CFile& file - open CFile used to save DIB
*
* Return value: TRUE if successful, else FALSE or CFileException
*

***********************************************************************
**/

BOOL WINAPI SaveDIB(HDIB hDib, CFile& file)


{
BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
LPBITMAPINFOHEADER lpBI; // Pointer to DIB info structure
DWORD dwDIBSize;

if (hDib == NULL)
return FALSE;

/*
* Get a pointer to the DIB memory, the first of which contains
* a BITMAPINFO structure
*/
lpBI = (LPBITMAPINFOHEADER) ::GlobalLock((HGLOBAL) hDib);
if (lpBI == NULL)
return FALSE;

if (!IS_WIN30_DIB(lpBI))
{
::GlobalUnlock((HGLOBAL) hDib);
return FALSE; // It's an other-style DIB (save not
supported)
}

/*
* Fill in the fields of the file header
*/

/* Fill in file type (first 2 bytes must be "BM" for a bitmap)


*/
bmfHdr.bfType = DIB_HEADER_MARKER; // "BM"

// Calculating the size of the DIB is a bit tricky (if we want


to
// do it right). The easiest way to do this is to call
GlobalSize()
// on our global handle, but since the size of our global
memory may have
// been padded a few bytes, we may end up writing out a few too
// many bytes to the file (which may cause problems with some
apps).
//
// So, instead let's calculate the size manually (if we can)
//
// First, find size of header plus size of color table. Since
the
// first DWORD in both BITMAPINFOHEADER and BITMAPCOREHEADER
conains
// the size of the structure, let's use this.

dwDIBSize = *(LPDWORD)lpBI + ::PaletteSize((LPSTR)lpBI); //


Partial Calculation

// Now calculate the size of the image

if ((lpBI->biCompression == BI_RLE8) || (lpBI->biCompression ==


BI_RLE4))
{
// It's an RLE bitmap, we can't calculate size, so trust
the
// biSizeImage field

dwDIBSize += lpBI->biSizeImage;
}
else
{
DWORD dwBmBitsSize; // Size of Bitmap Bits only

// It's not RLE, so size is Width (DWORD aligned) *


Height
dwBmBitsSize = WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI-
>biBitCount)) * lpBI->biHeight;

dwDIBSize += dwBmBitsSize;

// Now, since we have calculated the correct size, why


don't we
// fill in the biSizeImage field (this will fix any .BMP
files which
// have this field incorrect).

lpBI->biSizeImage = dwBmBitsSize;
}

// Calculate the file size by adding the DIB size to


sizeof(BITMAPFILEHEADER)

bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);


bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;

/*
* Now, calculate the offset the actual bitmap bits will be in
* the file -- It's the Bitmap file header plus the DIB header,
* plus the size of the color table.
*/
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + lpBI-
>biSize

+ PaletteSize((LPSTR)lpBI);
#ifdef _MAC
ByteSwapHeader(&bmfHdr);

// First swap the size field


*((LPDWORD)lpBI) = SWAPLONG(*((LPDWORD)lpBI));

// Now swap the rest of the structure (we don't save < Win30
files)
ByteSwapInfo((LPSTR)lpBI, TRUE);
#endif
TRY
{
// Write the file header
file.Write((LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER));
//
// Write the DIB header and the bits
//
file.WriteHuge(lpBI, dwDIBSize);
}
CATCH (CFileException, e)
{
#ifdef _MAC
// Swap everything back
*((LPDWORD)lpBI) = SWAPLONG(*((LPDWORD)lpBI));
ByteSwapInfo((LPSTR)lpBI, TRUE);
#endif
::GlobalUnlock((HGLOBAL) hDib);
THROW_LAST();
}
END_CATCH

#ifdef _MAC
// Swap everything back
*((LPDWORD)lpBI) = SWAPLONG(*((LPDWORD)lpBI));
ByteSwapInfo((LPSTR)lpBI, TRUE);
#endif

::GlobalUnlock((HGLOBAL) hDib);
return TRUE;
}

/
***********************************************************************
**

Function: ReadDIBFile (CFile&)

Purpose: Reads in the specified DIB file into a global chunk of


memory.
Returns: A handle to a dib (hDIB) if successful.
NULL if an error occurs.

Comments: BITMAPFILEHEADER is stripped off of the DIB. Everything


from the end of the BITMAPFILEHEADER structure
on is
returned in the global memory handle.

***********************************************************************
**/

HDIB WINAPI ReadDIBFile(CFile& file)


{
BITMAPFILEHEADER bmfHeader;
DWORD dwBitsSize;
HDIB hDIB;
LPSTR pDIB;

/*
* get length of DIB in bytes for use when reading
*/

dwBitsSize = file.GetLength();

/*
* Go read the DIB file header and check if it's valid.
*/
if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) !=
sizeof(bmfHeader))
return NULL;

#ifdef _MAC
ByteSwapHeader(&bmfHeader);
#endif
if (bmfHeader.bfType != DIB_HEADER_MARKER)
return NULL;

/*
* Allocate memory for DIB
*/
hDIB = (HDIB) ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
dwBitsSize);
if (hDIB == 0)
{
return NULL;
}
pDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);

/*
* Go read the bits.
*/
if (file.ReadHuge(pDIB, dwBitsSize - sizeof(BITMAPFILEHEADER)) !
=
dwBitsSize - sizeof(BITMAPFILEHEADER) )
{
::GlobalUnlock((HGLOBAL) hDIB);
::GlobalFree((HGLOBAL) hDIB);
return NULL;
}
#ifdef _MAC
// First swap the size field
*((LPDWORD)pDIB) = SWAPLONG(*((LPDWORD)pDIB));

// Now swap the rest of the structure


ByteSwapInfo(pDIB, IS_WIN30_DIB(pDIB));
#endif
::GlobalUnlock((HGLOBAL) hDIB);
return hDIB;
}
// add Nov.27,1999
HDIB WINAPI NewDIB(long width, long height,unsigned short biBitCount)
// add "biBitCount" by hchen Jan 25,00
{
long dwindth = (width*biBitCount/8+3)/4*4; // align 4 bytes
WORD color_num;
switch(biBitCount)
{
case 1:
color_num=2;
break;
case 4:
color_num=16;
break;
case 8:
color_num=256;
break;
default:
color_num=0;
break;
}

long dwBitsSize = dwindth *height + 40 + color_num*4;// 40


LPBIMHEAD

LPSTR pDIB;
HDIB hDIB = (HDIB) ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
dwBitsSize);
if (hDIB == 0)
{
return NULL;
}
pDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);

LPBITMAPINFO lpmf = (LPBITMAPINFO)pDIB;


lpmf->bmiHeader.biSize = 40;//40
lpmf->bmiHeader.biWidth = width; //i_info.width;
lpmf->bmiHeader.biHeight = height;
lpmf->bmiHeader.biPlanes = 1;
lpmf->bmiHeader.biBitCount = biBitCount;// 24
lpmf->bmiHeader.biCompression = 0;
lpmf->bmiHeader.biSizeImage = dwindth *height;
lpmf->bmiHeader.biXPelsPerMeter = 2925;
lpmf->bmiHeader.biYPelsPerMeter = 2925;
lpmf->bmiHeader.biClrUsed = 0;
lpmf->bmiHeader.biClrImportant= 0;

if(color_num!=0)
{
for(int i=0;i<color_num;i++)
{
lpmf->bmiColors[i].rgbRed =(BYTE)i;
lpmf->bmiColors[i].rgbGreen =(BYTE)i;
lpmf->bmiColors[i].rgbBlue =(BYTE)i;
}
}

::GlobalUnlock((HGLOBAL) hDIB);

return hDIB;
}

#ifdef _MAC
void ByteSwapHeader(BITMAPFILEHEADER* bmfHeader)
{
bmfHeader->bfType = SWAPWORD(bmfHeader->bfType);
bmfHeader->bfSize = SWAPLONG(bmfHeader->bfSize);
bmfHeader->bfOffBits = SWAPLONG(bmfHeader->bfOffBits);
}

void ByteSwapInfo(LPSTR lpHeader, BOOL fWin30Header)


{
// Note this doesn't swap the bcSize/biSize field. It assumes
that the
// size field was swapped during read or while setting the
fWin30Header
// flag.

if (fWin30Header)
{
LPBITMAPINFOHEADER lpBMIH = &(LPBITMAPINFO(lpHeader)-
>bmiHeader);

//lpBMIH->biSize = SWAPLONG(lpBMIH->biSize);
lpBMIH->biWidth = SWAPLONG(lpBMIH->biWidth);
lpBMIH->biHeight = SWAPLONG(lpBMIH->biHeight);
lpBMIH->biPlanes = SWAPWORD(lpBMIH->biPlanes);
lpBMIH->biBitCount = SWAPWORD(lpBMIH->biBitCount);
lpBMIH->biCompression = SWAPLONG(lpBMIH->biCompression);
lpBMIH->biSizeImage = SWAPLONG(lpBMIH->biSizeImage);
lpBMIH->biXPelsPerMeter = SWAPLONG(lpBMIH-
>biXPelsPerMeter);
lpBMIH->biYPelsPerMeter = SWAPLONG(lpBMIH-
>biYPelsPerMeter);
lpBMIH->biClrUsed = SWAPLONG(lpBMIH->biClrUsed);
lpBMIH->biClrImportant = SWAPLONG(lpBMIH-
>biClrImportant);
}
else
{
LPBITMAPCOREHEADER lpBMCH =
&(LPBITMAPCOREINFO(lpHeader)->bmciHeader);

lpBMCH->bcWidth = SWAPWORD(lpBMCH->bcWidth);
lpBMCH->bcHeight = SWAPWORD(lpBMCH->bcHeight);
lpBMCH->bcPlanes = SWAPWORD(lpBMCH->bcPlanes);
lpBMCH->bcBitCount = SWAPWORD(lpBMCH->bcBitCount);
}
}

#endif

...

dibapi.h in wienerfilter.rar

use Wiener filtering algorithm for image restoration of the


source vc...Original Link

// dibapi.h
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1997 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#ifndef _INC_DIBAPI
#define _INC_DIBAPI

/* Handle to a DIB */
DECLARE_HANDLE(HDIB);

/* DIB constants */
#define PALVERSION 0x300

/* DIB Macros*/

#define IS_WIN30_DIB(lpbi) ((*(LPDWORD)(lpbi)) ==


sizeof(BITMAPINFOHEADER))
#define RECTWIDTH(lpRect) ((lpRect)->right - (lpRect)->left)
#define RECTHEIGHT(lpRect) ((lpRect)->bottom - (lpRect)->top)

// WIDTHBYTES performs DWORD-aligning of DIB scanlines. The "bits"


// parameter is the bit count for the scanline (biWidth * biBitCount),
// and this macro returns the number of DWORD-aligned bytes needed
// to hold those bits.

#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)

/* Function prototypes */
BOOL WINAPI PaintDIB (HDC, LPRECT, HDIB, LPRECT, CPalette* pPal);
BOOL WINAPI CreateDIBPalette(HDIB hDIB, CPalette* cPal);
LPSTR WINAPI FindDIBBits (LPSTR lpbi);
DWORD WINAPI DIBWidth (LPSTR lpDIB);
DWORD WINAPI DIBHeight (LPSTR lpDIB);
WORD WINAPI PaletteSize (LPSTR lpbi);
WORD WINAPI DIBNumColors (LPSTR lpbi);
HGLOBAL WINAPI CopyHandle (HGLOBAL h);

BOOL WINAPI SaveDIB (HDIB hDib, CFile& file);


HDIB WINAPI ReadDIBFile(CFile& file);

HDIB WINAPI NewDIB(long width, long height, unsigned short


biBitCount= 0); //add
void WINAPI ConvertShort(unsigned short *array, long length);
void WINAPI ConvertLong(unsigned long *array, long length);

#endif //!_INC_DIBAPI
...

http://www.hackchina.com/en/r/54301/dibapi.h__html

/ / MyDIPView.cpp: implementation of the CMyDIPView class


//

# Include "stdafx.h"
# Include "MyDIP.h"

# Include "MyDIPDoc.h"
# Include "MyDIPView.h"
# Include "math.h"
# Include <complex>
using namespace std;
# Define PI 3.14159
# Ifdef _DEBUG
# Define new DEBUG_NEW
# Undef THIS_FILE
static char THIS_FILE [] = __FILE__;
# Endif
# Define SWAP (a, b) tempr = (a); (a) = (b); (b) = tempr
/************************************************* ************************
 *
 * Function Name:
 * FFT ()
 *
 * Parameters:
 * Complex <double> * TD - a pointer to point to an array of time-domain
 * Complex <double> * FD - pointer to point to an array of frequency domain
 * R -2 power of numbers, ie, the number of iterations
 *
 * Returns:
 * None.
 *
 * Note:
 * This function is used for fast Fourier transform.
 *
 ************************************************** **********************/
VOID WINAPI FFT (complex <double> * TD, complex <double> * FD, int r)
{
/ / Fourier transform points
LONG count;

/ / Loop variable
int i, j, k;

/ / Intermediate variable
int bfsize, p;

/ / Angle
double angle;

complex <double> * W, * X1, * X2, * X;

/ / Calculate the Fourier transform points


count = 1 <<r;

/ / Allocate the necessary memory operations


W = new complex <double> [count / 2];
X1 = new complex <double> [count];
X2 = new complex <double> [count];

/ / Calculate the weighted coefficient


for (i = 0; i <count / 2; i + +)
{
angle =-i * PI * 2 / count;
W [i] = complex <double> (cos (angle), sin (angle));
}

/ / Write the time-domain points X1


memcpy (X1, TD, sizeof (complex <double>) * count);
/ / Butterfly algorithm using Fast Fourier Transform
for (k = 0; k <r; k + +)
{
for (j = 0; j <1 <<k; j + +)
{
bfsize = 1 <<(r-k);
for (i = 0; i <bfsize / 2; i + +)
{
p = j * bfsize;
X2 [i + p] = X1 [i + p] + X1 [i + p + bfsize / 2];
X2 [i + p + bfsize / 2] = (X1 [i + p] - X1 [i + p + bfsize / 2]) * W [i * (1 <<k)];
}
}
X = X1;
X1 = X2;
X2 = X;
}

/ / Reorder
for (j = 0; j <count; j + +)
{
p = 0;
for (i = 0; i <r; i + +)
{
if (j & (1 <<i))
{
p + = 1 <<(r-i-1);
}
}
FD [j] = X1 [p];
}

/ / Free memory
delete W;
delete X1;
delete X2;
}
VOID WINAPI FFT_INV (complex <double> * TD, complex <double> * FD, int
power)
{
int count;
int i, j, k, bfsize, p;
double angle;
complex <double> * W, * X1, * X2, * X;
count = 1 <<power;
W = new complex <double> [count / 2];
X1 = new complex <double> [count];
X2 = new complex <double> [count];
for (i = 0; i <count / 2; i + +)
{
angle = i * PI * 2/count;
W [i] = complex <double> (cos (angle), sin (angle));
}
memcpy (X1, TD, sizeof (complex <double>) * 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] = X1 [i + p] + X1 [i + p + bfsize / 2];
X2 [i + p + bfsize / 2] = (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];
}
delete W;
delete X1;
delete X2;
}
BOOL fourn (double * data, unsigned long nn [], int ndim, int isign)
{
int idim;
unsigned long i1, i2, i3, i2rev, i3rev, ip1, ip2, ip3, ifp1, ifp2;
unsigned long ibit, k1, k2, n, nprev, nrem, ntot;
double tempi, tempr;
double theta, wi, wpi, wpr, wr, wtemp;
for (ntot = 1, idim = 1; idim <= ndim; idim + +)
ntot *= nn [idim];
nprev = 1;
for (idim = ndim; idim> = 1; idim -)
{
n = nn [idim];
nrem = ntot / (n * nprev);
ip1 = nprev <<1; ip2 = ip1 * n;
ip3 = ip2 * nrem;
i2rev = 1;
for (i2 = 1; i2 <= ip2; i2 + = ip1)
{
if (i2 <i2rev)
{
for (i1 = i2; i1 <= i2 + ip1-2; i1 + = 2) {
for (i3 = i1; i3 <= ip3; i3 + = ip2)
{
i3rev = i2rev + i3-i2;
SWAP (data [i3], data [i3rev]);
SWAP (data [i3 +1], data [i3rev +1]);
}
}
}
ibit = ip2>> 1;
while (ibit> = ip1 & & i2rev> ibit)
{
i2rev-= ibit;
ibit>> = 1;
}
i2rev + = ibit;
}
ifp1 = ip1;
while (ifp1 <ip2)
{
ifp2 = ifp1 <<1;
theta = isign * 6.28318530717959 / (ifp2/ip1);
wtemp = sin (0.5 * theta);
wpr =- 2.0 * wtemp * wtemp;
wpi = sin (theta);
wr = 1.0;
wi = 0.0;
for (i3 = 1; i3 <= ifp1; i3 + = ip1) {
for (i1 = i3; i1 <= i3 + ip1-2; i1 + = 2) {
for (i2 = i1; i2 <= ip3; i2 + = ifp2) {
k1 = i2;
k2 = k1 + ifp1;
tempr = wr * data [k2]-wi * data [k2 +1];
tempi = wr * data [k2 +1] + wi * data [k2];
data [k2] = data [k1]-tempr;
data [k2 +1] = data [k1 +1]-tempi;
data [k1] + = tempr;
data [k1 +1] + = tempi;
}
}
wr = (wtemp = wr) * wpr-wi * wpi + wr;
wi = wi * wpr + wtemp * wpi + wi;
}
ifp1 = ifp2;
}
nprev *= n;
}
return true;
}

BOOL WINAPI WienerDIB (LPSTR lpDIBBits, LONG lWidth, LONG lHeight)


{
/ / Pointer to point to source image
LPSTR lpSrc;
/ / Loop variable
long i;
long j;
/ / Pixel value
unsigned char pixel;
/ / Image number of bytes per row
LONG lLineBytes;
/ / Array used for FFT
double * fftSrc, * fftKernel;
double a, b, c, d;
/ / Length and width of two-dimensional FFT
unsigned long nn [3];
/ / Image normalization factor
double MaxNum;

/ / Calculate the number of bytes per row image


lLineBytes = WIDTHBYTES (lWidth * 8);

double dPower = log ((double) lLineBytes) / log (2.0);


if (dPower! = (int) dPower)
{
return false;
}
dPower = log ((double) lHeight) / log (2.0);
if (dPower! = (int) dPower)
{
return false;
}

fftSrc = new double [lHeight * lLineBytes * 2 +1];


fftKernel = new double [lHeight * lLineBytes * 2 +1];

nn [1] = lHeight;
nn [2] = lLineBytes;
for (j = 0; j <lHeight; j + +)
{
for (i = 0; i <lLineBytes; i + +)
{
/ / Point to source image j penultimate line, the first i pixels pointer
lpSrc = (char *) lpDIBBits + lLineBytes * j + i;

pixel = (unsigned char) * lpSrc;

fftSrc [(2 * lLineBytes) * j + 2 * i + 1] = (double) pixel;


fftSrc [(2 * lLineBytes) * j + 2 * i + 2] = 0.0;

if (i <5 & & j <5)


{
fftKernel [(2 * lLineBytes) * j + 2 * i + 1] = 1/25.0;
}
else
{
fftKernel [(2 * lLineBytes) * j + 2 * i + 1] = 0.0;
}
fftKernel [(2 * lLineBytes) * j + 2 * i + 2] = 0.0;
}
}

/ / FFT of the source image


fourn (fftSrc, nn, 2,1);
/ / FFT of the convolution kernel image
fourn (fftKernel, nn, 2,1);
for (j = 0; j <lHeight; j + +)
{
for (i = 0; i <lLineBytes; i + +)
{
a = fftSrc [(2 * lLineBytes) * j +2 * i +1];
b = fftSrc [(2 * lLineBytes) * j +2 * i +2];
c = fftKernel [(2 * lLineBytes) * j +2 * i +1];
d = fftKernel [(2 * lLineBytes) * j +2 * i +2];

if (c * c + d * d> 1e-3)
{
fftSrc [(2 * lLineBytes) * j +2 * i +1] = (a * c + b * d) / (c * c + d * d);
fftSrc [(2 * lLineBytes) * j +2 * i +2] = (b * c - a * d) / (c * c + d * d);
}
}
}

/ / FFT of the results of anti-image


fourn (fftSrc, nn, 2, -1);

/ / Determine the normalization factor


MaxNum = 0.0;
for (j = 0; j <lHeight; j + +)
{
for (i = 0; i <lLineBytes; i + +)
{
fftSrc [(2 * lLineBytes) * j + 2 * i + 1] =
sqrt (fftSrc [(2 * lLineBytes) * j + 2 * i + 1] * fftSrc [(2 * lLineBytes) * j + 2 * i + 1] \
+ FftSrc [(2 * lLineBytes) * j + 2 * i + 2] * fftSrc [(2 * lLineBytes) * j + 2 * i + 2]);
if (MaxNum <fftSrc [(2 * lLineBytes) * j + 2 * i + 1])
MaxNum = fftSrc [(2 * lLineBytes) * j + 2 * i + 1];
}
}

/ / Convert to image
for (j = 0; j <lHeight; j + +)
{
for (i = 0; i <lLineBytes; i + +)
{
/ / Point to source image j penultimate line, the first i pixels pointer
lpSrc = (char *) lpDIBBits + lLineBytes * j + i;

* LpSrc = (unsigned char) (fftSrc [(2 * lLineBytes) * j + 2 * i + 1] * 255.0/MaxNum);


}
}

delete fftSrc;
delete fftKernel;
/ / Return
return true;
}

/************************************************* ************************
 *
 * Function Name:
 * Fourier ()
 *
 * Parameters:
 * LPSTR lpDIBBits - pointer to point to the source DIB image
 * LONG lWidth - source image width (pixels number)
 * LONG lHeight - source image height (pixel number)
 *
 * Returns:
 * BOOL - successful return TRUE, otherwise returns FALSE.
 *
 * Note:
 * This function is used to Fourier transform the image.
 *
 ************************************************** **********************/
BOOL WINAPI Fourier (LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{

/ / Pointer to point to source image


unsigned char * lpSrc;

/ / Intermediate variable
double dTemp;

/ / Loop variable
LONG i;
LONG j;

/ / The Fourier transform of the width and height (integer power of 2)


LONG w;
LONG h;

int wp;
int hp;

/ / Image number of bytes per row


LONG lLineBytes;

/ / Calculate the number of bytes per row image


lLineBytes = WIDTHBYTES (lWidth * 8);

/ / Assign initial
w = 1;
h = 1;
wp = 0;
hp = 0;

/ / Calculate the Fourier transform of the width and height (integer power of 2)
while (w * 2 <= lWidth)
{
w *= 2;
wp + +;
}

while (h * 2 <= lHeight)


{
h *= 2;
hp + +;
}

/ / Allocate memory
complex <double> * TD = new complex <double> [w * h];
complex <double> * FD = new complex <double> [w * h];

/ / Line
for (i = 0; i <h; i + +)
{
/ / Column
for (j = 0; j <w; j + +)
{
/ / Pointer to DIB i-line, the first pointer j pixels
lpSrc = (unsigned char *) lpDIBBits + lLineBytes * (lHeight - 1 - i) + j;

/ / Assign to the time-domain


TD [j + w * i] = complex <double> (* (lpSrc), 0);
}
}

for (i = 0; i <h; i + +)
{
/ / Y direction of the Fast Fourier Transform
FFT (& TD [w * i], & FD [w * i], wp);
}

/ / Save the transform results


for (i = 0; i <h; i + +)
{
for (j = 0; j <w; j + +)
{
TD [i + h * j] = FD [j + w * i];
}
}

for (i = 0; i <w; i + +)
{
/ / X direction of the Fast Fourier Transform
FFT (& TD [i * h], & FD [i * h], hp);
}

/ / Line
for (i = 0; i <h; i + +)
{
/ / Column
for (j = 0; j <w; j + +)
{
/ / Calculate the spectrum
dTemp = sqrt (FD [j * h + i]. real () * FD [j * h + i]. real () +
FD [j * h + i]. Imag () * FD [j * h + i]. Imag ()) / 100;

/ / Determine whether more than 255


if (dTemp> 255)
{
/ / For more than a direct set to 255
dTemp = 255;
}

/ / Pointer to DIB section (i <h / 2? I + h / 2: ih / 2) line, the first (j <w / 2? J + w /


2: jw / 2) pixels pointer
/ / Here do not directly take i and j, will be transformed in order to move the
center of origin
/ / LpSrc = (unsigned char *) lpDIBBits + lLineBytes * (lHeight - 1 - i) + j;
lpSrc = (unsigned char *) lpDIBBits + lLineBytes *
(LHeight - 1 - (i <h / 2? I + h / 2: ih / 2)) + (j <w / 2? J + w / 2: jw / 2);

/ / Update the source image


* (LpSrc) = (BYTE) (dTemp);
}
}

/ / Delete the temporary variables


delete TD;
delete FD;

/ / Return
return TRUE;
}
BOOL WINAPI Fourier_inv (LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{

/ / Pointer to point to source image


unsigned char * lpSrc;

/ / Intermediate variable
double dTemp;

/ / Loop variable
LONG i;
LONG j;

/ / The Fourier inverse transform of the width and height (integer power of 2)
LONG w;
LONG h;

int wp;
int hp;

/ / Image number of bytes per row


LONG lLineBytes;

/ / Calculate the number of bytes per row image


lLineBytes = WIDTHBYTES (lWidth * 8);

/ / Assign initial
w = 1;
h = 1;
wp = 0;
hp = 0;

/ / Calculate the Fourier inverse transform of the width and height (integer power
of 2)
while (w * 2 <= lWidth)
{
w *= 2;
wp + +;
}

while (h * 2 <= lHeight)


{
h *= 2;
hp + +;
}

/ / Allocate memory
complex <double> * TD = new complex <double> [w * h];
complex <double> * FD = new complex <double> [w * h];

/ / Line
for (i = 0; i <h; i + +)
{
/ / Column
for (j = 0; j <w; j + +)
{
/ / Pointer to DIB i-line, the first pointer j pixels
lpSrc = (unsigned char *) lpDIBBits + lLineBytes * (lHeight - 1 - i) + j;

/ / Assign to the time-domain


TD [j + w * i] = complex <double> (* (lpSrc), 0);
}
}

for (i = 0; i <h; i + +)
{
/ / Y direction of the Fast Fourier inverse transform
FFT_INV (& TD [w * i], & FD [w * i], wp);
}

/ / Save the transform results


for (i = 0; i <h; i + +)
{
for (j = 0; j <w; j + +)
{
TD [i + h * j] = FD [j + w * i];
}
}

for (i = 0; i <w; i + +)
{
/ / X direction of the Fast Fourier inverse transform
FFT_INV (& TD [i * h], & FD [i * h], hp);
}

/ / Line
for (i = 0; i <h; i + +)
{
/ / Column
for (j = 0; j <w; j + +)
{
/ / Calculate the spectrum
dTemp = sqrt (FD [j * h + i]. real () * FD [j * h + i]. real () +
FD [j * h + i]. Imag () * FD [j * h + i]. Imag ()) / 100;

/ / Determine whether more than 255


if (dTemp> 255)
{
/ / For more than a direct set to 255
dTemp = 255;
}

/ / Pointer to DIB section (i <h / 2? I + h / 2: ih / 2) line, the first (j <w / 2? J + w /


2: jw / 2) pixels pointer
/ / Here do not directly take i and j, will be transformed in order to move the
center of origin
/ / LpSrc = (unsigned char *) lpDIBBits + lLineBytes * (lHeight - 1 - i) + j;
lpSrc = (unsigned char *) lpDIBBits + lLineBytes *
(LHeight - 1 - (i <h / 2? I + h / 2: ih / 2)) + (j <w / 2? J + w / 2: jw / 2);

/ / Update the source image


* (LpSrc) = (BYTE) (dTemp);
}
}

/ / Delete the temporary variables


delete TD;
delete FD;

/ / Return
return TRUE;
}

////////////////////////////////////////////////// ///////////////////////////
/ / CMyDIPView

IMPLEMENT_DYNCREATE (CMyDIPView, CScrollView)

BEGIN_MESSAGE_MAP (CMyDIPView, CScrollView)


/ / {{AFX_MSG_MAP (CMyDIPView)
ON_COMMAND (ID_MENUITEM32779, OnMenuitem32779)
/ /}} AFX_MSG_MAP
/ / Standard printing commands
ON_COMMAND (ID_FILE_PRINT, CScrollView:: OnFilePrint)
ON_COMMAND (ID_FILE_PRINT_DIRECT, CScrollView:: OnFilePrint)
ON_COMMAND (ID_FILE_PRINT_PREVIEW, CScrollView::
OnFilePrintPreview)
END_MESSAGE_MAP ()
////////////////////////////////////////////////// ///////////////////////////
/ / CMyDIPView construction / destruction

CMyDIPView:: CMyDIPView ()
{
/ / TODO: add construction code here

CMyDIPView:: ~ CMyDIPView ()
{
}

BOOL CMyDIPView:: PreCreateWindow (CREATESTRUCT & cs)


{
/ / TODO: Modify the Window class or styles here by modifying
/ / The CREATESTRUCT cs

return CScrollView:: PreCreateWindow (cs);


}

////////////////////////////////////////////////// ///////////////////////////
/ / CMyDIPView drawing

void CMyDIPView:: OnDraw (CDC * pDC)


{
CMyDIPDoc * pDoc = GetDocument ();
ASSERT_VALID (pDoc);
if (pDoc-> m_hDIB == NULL)
return;
/ / TODO: add draw code for native data here
int i, j;
    unsigned char * lpSrc;
LPSTR lpDIB = (LPSTR):: GlobalLock ((HGLOBAL) pDoc-> m_hDIB);
int cxDIB = (int):: DIBWidth (lpDIB); / / Size of DIB - x
int cyDIB = (int):: DIBHeight (lpDIB); / / Size of DIB - y
LPSTR lpDIBBits =:: FindDIBBits (lpDIB);
/ / Calculate the number of bytes per row image
long lLineBytes = WIDTHBYTES (cxDIB * 8);
/ / Per line
for (i = 0; i <cyDIB; i + +)
{
/ / Each column
for (j = 0; j <cxDIB; j + +)
{
/ / Pointer to DIB i-line, the first pointer j pixels
lpSrc = (unsigned char *) lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
/ / Calculate the new gray value
/ / * (LpSrc) = BYTE (255 -* lpSrc);
}
}
:: GlobalUnlock ((HGLOBAL) pDoc-> m_hDIB);
CRect rect (0,0, cxDIB, cyDIB), rcDIB (0,0, cxDIB, cyDIB);
:: PaintDIB (pDC-> m_hDC, & rect, pDoc-> m_hDIB, & rcDIB, pDoc-> m_palDIB);
}

////////////////////////////////////////////////// ///////////////////////////
/ / CMyDIPView printing

BOOL CMyDIPView:: OnPreparePrinting (CPrintInfo * pInfo)


{
/ / Default preparation
return DoPreparePrinting (pInfo);
}

void CMyDIPView:: OnBeginPrinting (CDC * / * pDC * /, CPrintInfo * / * pInfo * /)


{
/ / TODO: add extra initialization before printing
}

void CMyDIPView:: OnEndPrinting (CDC * / * pDC * /, CPrintInfo * / * pInfo * /)


{
/ / TODO: add cleanup after printing
}

////////////////////////////////////////////////// ///////////////////////////
/ / CMyDIPView diagnostics

# Ifdef _DEBUG
void CMyDIPView:: AssertValid () const
{
CScrollView:: AssertValid ();
}

void CMyDIPView:: Dump (CDumpContext & dc) const


{
CScrollView:: Dump (dc);
}

CMyDIPDoc * CMyDIPView:: GetDocument () / / non-debug version is inline


{
ASSERT (m_pDocument-> IsKindOf (RUNTIME_CLASS (CMyDIPDoc)));
return (CMyDIPDoc *) m_pDocument;
}
# Endif / / _DEBUG

////////////////////////////////////////////////// ///////////////////////////
/ / CMyDIPView message handlers

void CMyDIPView:: OnSize (UINT nType, int cx, int cy)


{
CScrollView:: OnSize (nType, cx, cy);
}

void CMyDIPView:: OnInitialUpdate ()


{
CScrollView:: OnInitialUpdate ();

SetScrollSizes (MM_TEXT, GetDocument () -> m_sizeDoc);


}

/ / Contrast stretching
/ / DEL void CMyDIPView:: OnMenuitem32777 ()
/ / DEL {
/ / DEL
/ / DEL / / access to documents
/ / DEL CMyDIPDoc * pDoc = GetDocument ();
/ / DEL int i, j;
/ / DEL int r1 = 60, r2 = 200;
/ / DEL double k = 1.5;
/ / DEL unsigned char * lpSrc;
/ / DEL ASSERT_VALID (pDoc);
/ / DEL if (pDoc-> m_hDIB == NULL)
/ / DEL return;
/ / DEL LPSTR lpDIB = (LPSTR):: GlobalLock ((HGLOBAL) pDoc-> m_hDIB);
/ / DEL LPSTR lpDIBBits =:: FindDIBBits (lpDIB);
/ / DEL int cxDIB = (int):: DIBWidth (lpDIB); / / Size of DIB - x
/ / DEL int cyDIB = (int):: DIBHeight (lpDIB); / / Size of DIB - y
/ / DEL long lLineBytes = WIDTHBYTES (cxDIB * 8); / / calculate the number of
bytes per row image
/ / DEL / / per line
/ / DEL for (i = 0; i <cyDIB; i + +)
/ / DEL {
/ / DEL / / each column
/ / DEL for (j = 0; j <cxDIB; j + +)
/ / DEL {
/ / DEL / / i points to the first line of DIB, the first pointer j pixels
/ / DEL lpSrc = (unsigned char *) lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
/ / DEL / / calculate the new gray value
/ / DEL if (* lpSrc <r1) * lpSrc = BYTE (* lpSrc / k);
/ / DEL else if (* lpSrc <r2) * lpSrc = BYTE ((* lpSrc-r1) * k + r1 / k);
/ / DEL else * lpSrc = BYTE ((* lpSrc-r2) / k +255- (255-r2) / k);
/ / DEL}
/ / DEL}
/ / DEL:: GlobalUnlock ((HGLOBAL) pDoc-> m_hDIB);
/ / DEL Invalidate (TRUE);
/ / DEL}

void CMyDIPView:: OnMenuitem32779 ()


{
/ / Get the document
CMyDIPDoc * pDoc = GetDocument ();

/ / Pointer to a pointer to DIB


LPSTR lpDIB;

/ / Pointer to pointer to DIB pixels


LPSTR lpDIBBits;

/ / Lock DIB
lpDIB = (LPSTR):: GlobalLock ((HGLOBAL) pDoc-> GetHDIB ());

/ / Find the starting position DIB image pixel


lpDIBBits =:: FindDIBBits (lpDIB);

/ / Determine whether it is 8-bpp bitmap (here for convenience, only deal with 8-
bpp bitmap Fourier transform, the other can be on)
if (:: DIBNumColors (lpDIB)! = 256)
{
/ / Prompt the user
MessageBox ("currently only supports 256-color bitmap in the Fourier
transform!", "Prompted"
MB_ICONINFORMATION | MB_OK);

/ / Unlock
:: GlobalUnlock ((HGLOBAL) pDoc-> GetHDIB ());

/ / Return
return;
}
/ / Change the cursor shape
BeginWaitCursor ();

/ / Call the Fourier () function Fourier transform


if (:: WienerDIB (lpDIBBits,:: DIBWidth (lpDIB),:: DIBHeight (lpDIB)))
{

/ / Set the dirty tag


pDoc-> SetModifiedFlag (TRUE);

/ / Update view
pDoc-> UpdateAllViews (NULL);
}
else
{
/ / Prompt the user
MessageBox ("Failed to allocate memory!", "Prompted",
MB_ICONINFORMATION | MB_OK);
}

/ / Unlock
:: GlobalUnlock ((HGLOBAL) pDoc-> GetHDIB ());

/ / Restore cursor
EndWaitCursor ();
}

 ...

// MyDIPView.h : interface of the CMyDIPView class


//
///////////////////////////////////////////////////////////////////////
//////
#if !
defined(AFX_MYDIPVIEW_H__9BEE6960_11E8_4B4E_A88D_7314312B6D29__INCLUDED
_)
#define
AFX_MYDIPVIEW_H__9BEE6960_11E8_4B4E_A88D_7314312B6D29__INCLUDED_

#if _MSC_VER > 1000


#pragma once
#endif // _MSC_VER > 1000

class CMyDIPView : public CScrollView


{
protected: // create from serialization only
CMyDIPView();
DECLARE_DYNCREATE(CMyDIPView)

// Attributes
public:
CMyDIPDoc* GetDocument();

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyDIPView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CMyDIPView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions


protected:
//{{AFX_MSG(CMyDIPView)
afx_msg void OnMenuitem32779();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
void OnInitialUpdate();
void OnSize(UINT nType, int cx, int cy);
};

#ifndef _DEBUG // debug version in MyDIPView.cpp


inline CMyDIPDoc* CMyDIPView::GetDocument()
{ return (CMyDIPDoc*)m_pDocument; }
#endif

///////////////////////////////////////////////////////////////////////
//////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.

#endif // !
defined(AFX_MYDIPVIEW_H__9BEE6960_11E8_4B4E_A88D_7314312B6D29__INCLUDED
_)
...

You might also like