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. The DXSurface enables 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 a DXSurface as transform input or output. However, the power of a DXSurface and its supporting tools can be used in any Windows graphics application that you create. In many ways, a DXSurface is easier to use than a DirectDrawSurface because it always has a known pixel format to use for image manipulation. The DXSurface enables 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 a DXSurface are by creating it from image files or by initializing it 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 a compressed image as a DXSurface, it introduces new image compression techniques into the architecture.
This article contains the following sections.
- Advantages of Using DXSurfaces
- Features of DXSurfaces
- Per-Pixel Alpha
- Pixel Format Independence
- Image Loading
- Procedural Surfaces
- Surface Modifiers
- Using DXSurfaces: A Quick Reference
Advantages of Using DXSurfaces
In addition to the advantages of supporting per-pixel alpha, procedural surfaces, and any pixel format and depth, a DXSurface is the recommended surface for Microsoft DirectX Transform operations for a number of other reasons.
As previously mentioned, a DXSurface must be wrapped on a DirectDrawSurface before it's passed as a parameter to Microsoft DirectX Transform objects. This occurs so that a DXSurface is always passed to a transform. This means that supplying a DXSurface directly to Microsoft DirectX Transform objects is 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.
A DXSurface also provides additional functionality beyond that of the DirectDrawSurface 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. A DXSurface enables 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
Microsoft DirectX Transform uses the image loading routines of Windows Internet Explorer to load images into memory. This means that all 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 Microsoft DirectX Transform, you do not need to store a DXSurface as a bitmap image. 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 code example.
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.