| Herd Software Development
|=
DaVinci Graphics Library
|==
DaVinci Documentation Home Search Order


Image editing with Windows API functions

Windows' own API provides a number of useful image editing functions. All of the actual drawing functions in the API are executed on a device context handle (HDC or, in many object-oriented languages, TDC). "Drawing functions" refers to functions such as LineTo or Rectangle, but commonly used functions such as BitBlt or PatBlt, which aren't normally associated with drawing, also use device contexts as their targets.

The 16 bit Windows API does not provide functions to rotate or perform color depth conversions on image data. Leonardo provides RotateDIB and TransformDIB as "hole-fillers", and in order to make 16 and 32 bit development as seamless as possible, these functions are also provided in 32 bit Leonardo DLLs.

The following limited examples demonstrate how DIBs can be processed using Windows' own functions.

1. Using a DDB

In order to process an image returned by ipImportInd directly via the API, you can convert the imported DIB to a DDB. using the BitmapFromDIB function.

Before you can draw directly onto the DDB you need a device context handle. This is generated using CreateCompatibleDC (NULL). Then you load your DDB into the device context using SelectObject:
{ HDC     hDC   = CreateCompatibleDC(NULL);
  HBITMAP hbm   = BitmapFromDIB(hdib, NULL),
          hbmold= (HBITMAP) SelectObject( hbm );
  // Perform the drawing operation.....
  MoveToEx(hDC, 0, 0, NULL);
  LineTo  (hDC, 100, 100);

  // Cleanup
  SelectObject( hDC, hbmold);
  DeleteDC(hDC);
}

Note that a DDB is limited to the color resolution of the DC it has been created for (normally the current video driver), or optionally monochrome. This can result in serious loss of image quality when, for example, TrueColor images are being processed on 16 color displays. You have probably noticed that most better image editing suites won't even let you start the application unless you have a minimum of 256 colors set for your display.

2. Using DIB.DRV (16 bit only)

The DIB.DRV driver installed with Windows 3.1 allows you to generate a device context for a DIB. The main advantage to this is that the driver itself is hardware-independent, permitting DIBs to be processed at 24 bit color depth regardless of the current display driver's color depth or capability, and without loss of quality in the resulting image. This functionality is also available from an OWL 2.x TDC object constructed through a TDib object.

{ LPBITMAPINFOHEADER	lpBMI = (LPBITMAPINFOHEADER) GlobalLock(hDIB);
  // Use DIB.DRV to generate the device context
  HDC hDC = CreateDC("DIB", NULL, NULL, (LPDEVMODE) lpBMI);
  // execute drawing operation .....
  MoveToEx(hDC, 0, 0, NULL);
  LineTo (hDC, 100, 100);
  // Cleanup
  DeleteDC(hDC);
  GlobalUnlock(hDIB);

}

This functionality is not supported in 32 bit application development. It has been replaced with the CreateDIBSection function which is designed to mimic this behavior. DIB.DRV is not shipped with Windows 95; its functionality has been integrated into other core components of Win32 operating systems. DIB.DRV is redistributable, however, for developers whose applications depend upon it.

WinG (Windows Games API) provides methods of working with DIBs in a very similar fashion to the methods used by DIB.DRV. WinG is also available in both 16 and 32 bit native versions.

Neither WinG nor DIB.DRV will solve all your problems. One of the most obvious deficiencies is that neither will allow you to BitBlt a DIB with biBitCount = 24 (TrueColor) into a DIB with biBitCount = 8 (256 color).

3. Using CreateDIBSection

CreateDIBSection makes it possible to draw directly on DIBs within Win32 applications. Details can be found in Win32 API documentation.