Understanding DXSurfaces

A Microsoft® DirectX® surface object, DXSurface, is an abstract image whose pixel data is obtainable in 32-bit ARGB color format, regardless of the underlying pixel representation. Microsoft DirectX Transform uses this data object for both input and output images. DXSurfaces enable you to concentrate on the logical aspects of designing the transform, while letting the supporting software take care of numerous details, such as alpha blending, dithering, and converting among different pixel formats.

You can use DXSurfaces as transform inputs or outputs. However, the power of DXSurfaces and their supporting tools can be used in any Microsoft Windows® graphics application that you create. In many ways, DXSurfaces are easier to use than DirectDrawSurfaces because they always have a known pixel format to use for image manipulation. DXSurfaces enable all the standard, pixel-level access of images, while also exposing generic graphics tools, such as lookup tables and image filters.

The most common ways to construct DXSurfaces are by creating them from image files or by initializing them from DirectDrawSurface objects. A DXSurface also can be a procedural surface, where the sample values are calculated on demand, rather than being stored statically in an array. Because you can represent compressed images as DXSurfaces, they introduce new image compression techniques into the architecture.

This article contains the following sections.

Advantages of Using DXSurfaces

In addition to the advantages of supporting per-pixel alpha, procedural surfaces, and any pixel format and depth, DXSurfaces are the recommended surface for DirectX Transform operations for a number of other reasons.

As previously mentioned, DXSurfaces need to be wrapped on DirectDrawSurfaces before they're passed as parameters to DirectX Transform objects. This occurs so that a DXSurface is always passed to a transform. This means that supplying DXSurfaces directly to DirectX Transform objects will be slightly more efficient than supplying other types of surfaces because the aggregation step is not necessary. Furthermore, if the DXSurface is already in one of two standard pixel formats (ARGB32 or PMARGB32), read and read/write pointers can be obtained to directly access the surface's samples, saving a copy to a buffer.

DXSurfaces also provide additional functionality beyond that of DirectDrawSurfaces by supporting transforms on surfaces containing an alpha channel or a color key.

Features of DXSurfaces

Per-Pixel Alpha
Most common image formats store only RGB color information for each pixel. When an image also stores an alpha value for each pixel, you can do much more sophisticated graphics operations, such as compositing and image fading. DXSurfaces enable direct access to alpha information for pixel formats that support alpha. If the underlying pixel format of a DXSurface does not support alpha, the code translates the pixels to an alpha-supported format as they are read from the surface. This alpha translation also properly accommodates color keys and transparency.
Pixel Format Independence
When you are using DXSurfaces, you never have to worry about the pixel format of the source or destination images. When the image is read from a DXSurface, you can choose to obtain it in a pixel format of ARGB32 or PMARGB32. If the pixel format of the source is the same, the pixel values are copied directly from memory. If the formats are different, the software automatically translates the pixels as they are read from the source format to the destination using highly optimized code.
Image Loading
DirectX Transform uses the image loading routines of Microsoft Internet Explorer to load images into memory. This means that all of the most common image formats can be loaded directly into DXSurfaces. This includes formats such as the following:
  • Bitmap (.bmp)
  • Graphics Interchange Format (.gif)
  • Joint Photographic Experts Group (.jpg)
  • Portable Network Graphics (.png)
  • Microsoft DirectDraw® surface objects
Procedural Surfaces
With DirectX Transform, you do not need to store DXSurfaces as bitmap images. You can define an image through a procedure used to build an image as its pixels are read. This makes access to the surface read-only, but because you are storing only the algorithm for the procedure and its parameters, an image of arbitrary size requires only a small amount of memory. A gradient image is an example of a simple procedural surface, but more sophisticated algorithms can produce natural-looking marble patterns or futuristic textures.
Surface Modifiers
The samples of a DXSurface can be accessed directly or through a DXSurfaceModifier object (Surface Modifier). Surface Modifiers are themselves DXSurfaces and provide another layer of image processing that enables you to set the global image opacity or to process an image through a lookup table. You can also use the Surface Modifier to tile an input image onto a larger surface or to place your input image onto a colored background.

Using DXSurfaces: A Quick Reference

DXSurface objects are created with the IDXSurfaceFactory interface. This interface exists as a service of the IDXTransformFactory interface, which must be created with the Component Object Model (COM) method CoCreateInstance. Transform users often create the DXTransformFactory (Transform Factory) and use it to obtain a pointer to the DXSurfaceFactory (Surface Factory) and any transform interfaces they need. DXSurface users only need the Surface Factory pointer, which can be obtained by using the following statement.

hr = CoCreateInstance( CLSID_DXTransformFactory, NULL,
CLSCTX_INPROC, IID_IDXSurfaceFactory, (void **)&g_pSurfFact );

Multiple DXSurfaces can be created with the IDXSurfaceFactory::CreateSurface method. You can also create DXSurfaces from image files by using the IDXSurfaceFactory::LoadImage method, as shown in the following:

hr = g_pSurfFact->LoadImage( pwcsFileName, NULL, NULL,
&DDPF_PMARGB32, IID_IDXSurface, (void**)g_pSurf );

You can use the IDXARGBReadPtr and IDXARGBReadWritePtr interfaces to read or to modify the samples of the surfaces. The helper functions contain routines to combine arrays of samples and to allocate working buffers.


Top of Page Top of Page
© 2000 Microsoft and/or its suppliers. All rights reserved. Terms of Use.