Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
#### Bitmap Types: DDB and DIB Most bitmap formats have subformats that allow an image to be stored similarly to the original format but with slight modifications, and the Windows bitmap format is no exception. Typically, subformats come about as a result of updated technology. New technology may contain enhanced functionality that was not available when the original format was created. Thus, subformats are born containing attributes that allow the image to take advantage of the new technological achievements. Windows bitmaps come in two flavors: DDB and DIB. Device-Dependent Bitmaps The device-dependent bitmap, or DDB, is an older bitmap format and is kept around solely for backward compatibility. This format is so named because it is very dependent on the device upon which it is displayed for certain information. The DDB format does not store any color palette information or resolution information, storing only its width, height, color depth, and array of pixels that make up the image. DDBs always use the system color palette when they are displayed. Coupled with the lack of original resolution information, it is very difficult to accurately display a DDB in its original form on anything other than the original device upon which it was created. Additionally, the image itself can only be modified through the use of GDI functions. The storage and display of a DDB is handled natively by the video driver and video hardware. Naturally, this makes the use and display of DDBs very fast, but they are not very flexible.
| device-dependent bitmap (DDB): An older bitmap image format that stores only height, width, color depth, and image pixels.
Device-Independent Bitmaps The device-independent bitmap, or DIB, stores all the information required to accurately display the image on any device. Such information includes the color palette for the image, the resolution of the device upon which the bitmap was originally created, and a data compression flag. The pixels defining the DIB image are also directly accessible to the developer, making this format incredibly flexible. The DIB bitmap format describes a standardized pixel format, making the storage and display of such images a responsibility of generic GDI functions and global memory. As such, the use and display of DIBs is not quite as fast as DDBs, but their flexibility more than makes up for the loss in speed.
| device-independent bitmap (DIB): A flexible bitmap image format that includes all necessary information to accurately reproduce the image on any device.
#### The TBitmap Object The Delphi TBitmap object encapsulates the functionality of both the DDB and DIB image formats. By default, when a TBitmap object is created, it holds an image in the DDB format. When a bitmap is loaded into it, the image will be stored in the DIB format. The TBitmap object contains many useful properties and methods that make the manipulation of bitmap images incredibly easy. Loading Images Using regular GDI calls to simply load an image from a file may take half a dozen lines of code or more. With the TBitmap object, a simple call to its LoadFromFile method will load the image data and its palette, at which point the bitmap is ready for use. If the image is stored in a resource attached to the executable (or a DLL), the TBitmap’s LoadFromResourceID or LoadFromResourceName methods give the developer flexibility in determining how the image is extracted from the resource. These methods alone more than justify the use of a TBitmap object over hard-core GDI function calls. Displaying Images The TBitmap object itself does not contain any methods that deal directly with its display. This functionality lies in the domain of the TCanvas object. There are several methods by which a bitmap can be displayed, and they all have similar ancestors in the form of GDI function calls. The act of copying bitmap pixels to the screen for display is known as a bitblt, or blit.
| bitblt: bit block transfer. Pronounced “blit,” this is the act of copying the pixels of a bitmap image onto the screen or other graphical output surface for display.
The easiest TCanvas method available for displaying an image is Draw. It simply takes three parameters: a horizontal and vertical position of the upper left-hand corner of the image and the image object itself. This draws the entire image with its upper left-hand corner located at the specified coordinate. Other methods include CopyRect, which allows only a portion of an image to be drawn (scaling as necessary), and StretchDraw, which allows the image to be drawn larger or smaller than its original size. Listing 3-5 illustrates loading a bitmap and displaying it on the surface of a form. Listing 3-5: Loading and displaying a bitmap image
    procedure TForm1.FormCreate(Sender: TObject);

    begin

      {create the bitmap object}

      Bitmap := TBitmap.Create;

      {load in a bitmap image}

      Bitmap.LoadFromFile(ExtractFilePath(ParamStr(0))+’Athena.bmp’);

    end;

    procedure TForm1.FormDestroy(Sender: TObject);

    begin

      {free the bitmap object}

      Bitmap.Free;

    end;

    procedure TForm1.FormPaint(Sender: TObject);

    begin

      {display the bitmap using the Draw method}

      Canvas.Draw(8, 24, Bitmap);

      {display the bitmap using the CopyRect method}

      Canvas.CopyRect(Rect(192, 24, 344, 104),Bitmap.Canvas,Rect(62, 39, 111, 74));

      {display the bitmap using the StretchDraw method}

      Canvas.StretchDraw(Rect(8, 184, 352, 264), Bitmap);

    end;
Figure 3-12: An image displayed using several different methods Direct Pixel Access One of the biggest benefits of using a DIB over a DDB is the fact that individual pixels can be directly accessed. This makes feasible many advanced graphical rendering techniques, such as ray casting. Accessing the individual pixels of a DDB (and even using the Pixels property of a TCanvas) is incredibly slow and could never be used for such advanced graphical output techniques. To access individual pixels using the TBitmap object, the ScanLine property must be used. The ScanLine property is an indexed property that requires a row number and returns a pointer to the block of memory that contains the specified rows of pixels. The actual format of the information stored in this block of memory is dependent upon the value of the TBitmap’s PixelFormat property. The PixelFormat value reflects the number of bits required to describe one pixel in the image. Refer to the color depth topic discussed previously for more information on bits-per-pixel and its relation to the number of colors an image can contain. Thus, it is very important to know how the image information is stored before accessing it. Since this book is primarily concerned with 256-color images and graphical modes, assume that a TBitmap object has been loaded with a 256-color bitmap image. A variable of type PByteArray can be used to access the pixels pointed to by the ScanLine property. PByteArray is defined as a pointer to an array of bytes. The result of accessing the ScanLine property is assigned to our PByteArray variable, at which point the individual pixels can be accessed by a simple index into the array. Since a 256-color bitmap defines one pixel as 8 bits, or a byte, using a PByteArray variable allows the developer to access individual pixels correctly. If the image were of any other color depth, a different approach to accessing the pixels would need to be used. Listing 3-6 demonstrates accessing individual pixels through the ScanLine property.
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.