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

Other cDib Member Functions

In the previous section, some CDib data members were initialized by calling CDib member functions. One of those member functions was GetDibSizeImage(), which calculates the size of the DIB in bytes. This function first checks whether the biSizeImage member of the BITMAPINFOHEADER structure already contains a value other than 0:

if (m_pBmInfoHeader->biSizeImage == 0)

If it does, the function simply returns the value stored in biSizeImage:

return m_pBmInfoHeader->biSizeImage;

Otherwise, the function must calculate the image size using the information it already has. First, it gets the DIB's width and height in pixels:

DWORD byteWidth = (DWORD) GetDibWidth();

DWORD height = (DWORD) GetDibHeight();

Then it multiplies the width by the height to get the size of the entire bitmap image:

DWORD imageSize = byteWidth * height;

Notice that all these calculations are done with DWORDs to avoid the truncation errors that might occur with regular integers. DIBs can be big!

Another member function that must do some calculations is GetDibNumColors(), shown in Listing 11.11.

Listing 11.11 Getting the Number of Colors

UINT CDib::GetDibNumColors()

{

if ((m_pBmInfoHeader->biClrUsed == 0) &&

(m_pBmInfoHeader->biBitCount < 9))

return (1 << m_pBmInfoHeader->biBitCount);

else

return (int) m_pBmInfoHeader->biClrUsed;

}

This function first checks the BITMAPINFOHEADER structure's biClrUsed member for a value other than 0. If it finds a value other than 0, it simply returns that value from the function. Otherwise, it calculates the number of colors by shifting the value 1 to the left, using the biBitCount member as the shift count. This results in a value of 2 for 1-bit (monochrome) DIBs, 16 for 4-bit DIBs, and 256 for 8-bit DIBs.

The remaining CDib member functions just return the values of the class's data members. For example, the GetDibRGBTablePtr() function (Listing 11.12) returns a pointer to the DIB's color table, a value that has already been stored in the data member m_pRGBTable.

Listing 11.12 Getting a Pointer to the Color Table

LPRGBQUAD CDib::GetDibRGBTablePtr()

{ return m_pRGBTable;}

Creating ShowDib program

Now that you have a class for handling DIBs, it's time to put the class to work. In this section, you'll use Visual C++'s AppWizard to create an application that can load and display DIBs.

  1. Start Visual C++, and select the File menu's New command. The New property sheet appears.

  2. In the Project Name edit box, type the project name ShowDib, and in the Project Path box, select the directory in which you want to store the project. Finally, make sure that the selected project type in the left-hand pane is MFC AppWizard (exe).

  3. Select the OK button. The Step 1 dialog box appears. Choose the Single Document option.

  4. Click the Next button three times. You see Step 4 of 6 dialog box. Turn off all of the features except Use 3D Controls.

  5. Click the Finish button. When the New Project Information dialog box appears, click the OK button to close the dialog box and to generate the files needed for the project.

  6. Select the Build, Build command (or press F7) to compile and link the new ShowDib application.

  7. To run the program after it's compiled, select the Build, Execute command or press Ctrl+F5 on your keyboard.

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