关于'`global namespace''的问题

时间:2023-01-04 20:33:32

我按照<<vc++数字图像处理>>这本书敲了一个头文件封装了一些dib的函数

以下是dibapi.h的内容:

#ifdef _INC_DIBAPI
#define _INC_DIBAPI

DECLARE_HANDLE(HDIB);

#define PALVERSION 0x300

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

#define RECTWIDTH(lpRect)  ((lpRect)->right-(lpRect)->left)

#define RECTHEIGHT(lpRect)  ((lpRect)->bottom-(lpRect)->top))


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

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);

#endif 

以下是dibapi.cpp的内容

#include "stdafx.h"
#include "dibapi.h"
#include <io.h>
#include <error.h>
#include <math.h>
#include <direct.h>

const WORD DIB_HEADER_MARKER=((WORD) ('M' << 8) | 'B');

BOOL WINAPI PaintDIB(HDC hDC,LPRECT lpDCRect,HDIB hDIB,LPRECT lpDIBRect,CPalette* pPal)
{
 LPSTR lpDIBHdr;
 LPSTR lpDIBBits;
 BOOL bSuccess=FALSE;
 HPALETTE hPal=NULL;
 HPALETTE hOldPal=NULL;

 if(hDIB==NULL)
 {
  return FALSE; 
 }

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

 lpDIBBits=::FindDIBBits(lpDIBHdr);

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

  hOldPal=::SelectPalette(hDC,hPal,TRUE);
 }

 ::SetStretchBltMode(hDC,COLORONCOLOR);

 if((RECTWIDTH(lpDCRect)==RECTWIDTH(lpDIBRect))&&(RECTHEIGHT(lpDCRect)==RECTHEIGHT(lpDIBRect)))
 {
  bSuccess=::SetDIBitsToDevice(hDC,lpDCRect->left,lpDCRect->top,RECTWIDTH(lpDCRect),RECTHEIGHT(lpDCRect),lpDIBRect->left,(int)DIBHeight(lpDIBHdr)-lpDIBRect->top-RECTHEIGHT(lpDIBRect),0,(WORD)DIBHeight(lpDIBHdr),lpDIBBits,(LPBITMAPINFO)lpDIBHdr,DIB_RGB_COLORS);

 }
 else
 {
  bSuccess=::StretchDIBits(hDC,lpDCRect->left,lpDCRect->top,RECTWIDTH(lpDCRect),RECTHEIGHT(lpDCRect),lpDIBRect->left,lpDIBRect->top,RECTWIDTH(lpDIBRect),RECTHEIGHT(lpDIBRect),lpDIBBits,(LPBITMAPINFO)lpDIBHdr,DIB_RGB_COLORS,SRCCOPY);

 }
 ::GlobalUnlock((HGLOBAL)hDIB);

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

 return bSuccess;
}


BOOL WINAPI CreateDIBPalette(HDIB hDIB,CPalette* pPal)
{
 LPLOGPALETTE lpPal;

 HANDLE hLogPal;

 HPALETTE hPal=NULL;

 int i;

 WORD wNumColors;

 LPSTR lpbi;

 LPBITMAPINFO lpbmi;

 LPBITMAPCOREINFO lpbmc;

 BOOL bWinStyleDIB;

 BOOL bResult=FALSE;

 if(hDIB==NULL)
 {
  return FALSE; 
 }

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

 lpbmi=(LPBITMAPINFO)lpbi;

 lpbmc=(LPBITMAPCOREINFO)lpbi;

 wNumColors=::DIBNumColors(lpbi);

 if(wNumColors!=0)
 {

  hLogPal=::GlobalAlloc(GHND,sizeof(LPLOGPALETTE)+sizeof(PALETTEENTRY)*wNumColors);

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

   return FALSE;
  
  }

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

  lpPal->palVersion=PALVERSION;

  lpPal->palNumEntries=(WORD)wNumColors;

  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;

   }
  }

  bResult=pPal->CreatePalette(lpPal);

  ::GlobalUnlock((HGLOBAL)hLogPal);

  ::GlobalFree((HGLOBAL)hLogPal);
 }

 ::GlobalUnlock((HGLOBAL)hDIB));

 return bResult;

}


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


DWORD WINAPI DIBWidth(LPSTR lpDIB)
{
 LPBITMAPINFOHEADER lpbmi;
 LPBITMAPINFOHEADER lpbmc;

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

 if(IS_WIN30_DIB(lpDIB))
 {
  return lpbmi->biWidth;
 }
 else
 {
  return (DWORD)lpbmc->bcWidth;
 }
}


DWORD WINAPI DIBHeight(LPSTR lpDIB)
{
 LPBITMAPINFOHEADER lpbmi;

 LPBITMAPCOREHEADER lpbmc;

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

 if(IS_WIN30_DIB(lpDIB))
 {
  return lpbmi->biHeight;
 }
 else
 {
  return (DWORD)lpbmc->bcHeight;
 }
}

WORD WINAPI PaletteSize(LPSTR lpbi)
{
 if(IS_WIN30_DIB(lpbi))
 {
  return (WORD)(::DIBNumColors(lpbi)*sizeof(RGBQUAD)); 
 }
 else
 {
  return (WORD)(::DIBNumColors(lpbi)*sizeof(RGBTRIPLE));
 }
}


WORD WINAPI DIBNumColors(LPSTR lpbi)
{
 WORD wBitCount;

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

 if(IS_WIN30_DIB(lpbi))
 {
  wBitCount=((LPBITMAPINFOHEADER)lpbi)->biBitCount;
 }
 else
 {
  wBitCount=((LPBITMAPCOREHEADER)lpbi)->bcBitCount;
 }

 switch(wBitCount)
 {
 case 1:
  return 2;
 case 4:
  return 16;
 case 8:
  return 256;
 default:
  return 0;
 }
}


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;
}


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

 LPBITMAPINFOHEADER lpBI;

 DWORD dwDIBSize;

 if(hDib==NULL)
 {
  return FALSE;
 }

 lpBI=(LPBITMAPINFOHEADER)::GlobalLock((HGLOBAL)hDib);

 if(lpBI==NULL)
 {
  return FALSE;
 }

 if(!IS_WIN30_DIB(lpBI))
 {

  ::GlobalUnlock((HGLOBAL)hDib);

  return FALSE;
 }

 bmfHdr.bfType=DIB_HEADER_MARKER;

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

 if((lpBI->biCompression==BI_RLE8)||(lpBI->biCompression==BI_RLE4))
 {
  dwDIBSize+=lpBI->biSizeImage;
 }
 else
 {
  DWORD dwBmBitsSzie;
        dwBmBitsSzie=WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount))*lpBI->biHeight;

  dwDIBSize+=dwBmBitsSzie;
  lpBI->biSizeImage=dwBmBitsSzie;
 }
 bmfHdr.bfSize=dwDIBSzie+sizeof(BITMAPFILEHEADER);

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

 bmfHdr.bfOffBits=(DWORD)sizeof(BITMAPFILEHEADER)+lpBI->biSize+PaletteSize((LPSTR)lpBI);

 TRY
 {
  file.Write((LPSTR)&bmfHdr,sizeof(BITMAPFILEHEADER));
  file.WriteHuge(lpBI,dwDIBSize);
 }
 CATCH (CFileException, e)
 {
  ::GlobalUnlock((HGLOBAL)hDib);
  THROW_LAST();

 }
 END_CATCH

 ::GlobalUnlock((HGLOBAL)hDib);

 return TRUE;
}

HDIB WINAPI ReadDIBFile(CFile& file)
{
 BITMAPFILEHEADER bmfHeader;
 DWORD dwBitsSize;
 HDIB hDIB;
 LPSTR pDIB;

 dwBitsSize=file.GetLength();

 if(file.Read((LPSTR)&bmfHeader,sizeof(bmfHeader))!=sizeof(bmfHeader))
 {
  return NULL; 
 }

 if(bmfHeader.bfType!=DIB_HEADER_MARKER)
 {
  return NULL;
 }

 hDIB=(HDIB)::GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,dwBitsSize);
 if(hDIB)
 {
  return NULL;
 }

 pDIB=(LPSTR)::GlobalLock((HGLOBAL)hDIB);
 if(file.ReadHuge(pDIB,dwBitsSize-sizeof(BITMAPFILEHEADER))!=dwBitsSize-sizeof(BITMAPFILEHEADER))
 {
  ::GlobalUnlock((HGLOBAL)hDIB);
  ::GlobalFree((HGLOBAL)hDIB);
  return NULL;
 }
 ::GlobalUnlock((HGLOBAL)hDIB);
 return hDIB;
}

我在编译都立刻出现很多错误,其中像下面这样的我到网上找个很多都没有解决
D:/C++works/MFC App/ImageFrame/dibapi.cpp(25) : error C2039: 'FindDIBBits' : is not a member of '`global namespace''
D:/C++works/MFC App/ImageFrame/dibapi.cpp(154) : error C2039: 'PaletteSize' : is not a member of '`global namespace''
D:/C++works/MFC App/ImageFrame/dibapi.cpp(172) : error C2039: 'bcWidth' : is not a member of 'tagBITMAPINFOHEADER'
D:/C++works/MFC App/ImageFrame/dibapi.cpp(200) : error C2039: 'DIBNumColors' : is not a member of '`global namespace''

感觉像是要把自定义的全局函数加都全局命名空间中,请高手赐教,不胜感激