Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
5.22 Mб
Скачать

Introducing the cDib Class

Although MFC features classes for many graphical objects, you won't find one for DIBs. You don't, of course, necessarily need a special class to handle DIBs, but having such a class helps you organize your code into reusable modules. For that reason, in this chapter you develop a simple class called CDib, which reads DIBs from disk into memory and returns important information about the DIB.

The CDib class's interface is represented by header file. (see Listing 11.5).

Listing 11.5 CDIB.H The CDib Class's Header File

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

// CDIB.H: Header file for the DIB class.

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

#ifndef __CDIB_H

#define __CDIB_H

class CDib : public CObject

{protected:

LPBITMAPFILEHEADER m_pBmFileHeader;

LPBITMAPINFO m_pBmInfo;

LPBITMAPINFOHEADER m_pBmInfoHeader;

RGBQUAD* m_pRGBTable;

BYTE* m_pDibBits;

UINT m_numColors;

public:

CDib(const char* fileName);

~CDib();

DWORD GetDibSizeImage();

UINT GetDibWidth();

UINT GetDibHeight();

UINT GetDibNumColors();

LPBITMAPINFOHEADER GetDibInfoHeaderPtr();

LPBITMAPINFO GetDibInfoPtr();

LPRGBQUAD GetDibRGBTablePtr();

BYTE* GetDibBitsPtr();

protected:

void LoadBitmapFile(const char* fileName);

};

The CDib class's data members consist mostly of pointers to the various parts of a DIB, as shown in Listing 11.6.

Listing 11.6 Data Members of the CDib Class

LPBITMAPFILEHEADER m_pBmFileHeader;

LPBITMAPINFO m_pBmInfo;

LPBITMAPINFOHEADER m_pBmInfoHeader;

RGBQUAD* m_pRGBTable;

BYTE* m_pDibBits;

The pointers in Listing 11.6 store the addresses of the DIB's BITMAPFILEHEADER, BITMAPINFO, and BITMAPINFOHEADER structures, as well as the addresses of the DIB's color table and image data. The final data member holds the number of colors in the DIB:

UINT m_numColors;

Like most classes, CDib has both a constructor and a destructor:

CDib(const char* fileName);

~CDib();

As you can see, you can create a CDib object by passing the file name of the bitmap that you want to load to the CDib constructor.

To enable you to obtain important information about a DIB after it is loaded, the CDib class features eight public member functions, as shown in Listing 11.7.

Listing 11.7 Member Functions of the CDib Class

DWORD GetDibSizeImage();

UINT GetDibWidth();

UINT GetDibHeight();

UINT GetDibNumColors();

LPBITMAPINFOHEADER GetDibInfoHeaderPtr();

LPBITMAPINFO GetDibInfoPtr();

LPRGBQUAD GetDibRGBTablePtr();

BYTE* GetDibBitsPtr();

Table 11.3 lists each of the public member functions and what they do.

Table 11.3 The CDib Class's public Member Functions

Name

Description

GetDibSizeImage()

Returns the size in bytes of the image

GetDibWidth()

Returns the DIB's width in pixels

GetDibHeight()

Returns the DIB's height in pixels

GetDibNumColors()

Returns the number of colors in the DIB

GetDibInfoHeaderPtr()

Returns a pointer to the DIB's BITMAPINFOHEADER structure

GetDibInfoPtr()

Returns a pointer to the DIB's BITMAPINFO structure

GetDibRGBTablePtr()

Returns a pointer to the DIB's color table

GetDibBitsPtr()

Returns a pointer to the DIB's image data

Finally, the CDib class has a single protected member function that it calls internally to load a DIB file:

void LoadBitmapFile(const char* fileName);

You'll never call this member function directly.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]