🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How do i convert a 24 bit BMP to a 16 bit DD surface?

Started by
0 comments, last by GameDev.net 24 years, 6 months ago
assuming that 16 bit images can only be stored as 24 bit images in memory with a 64K color limit, how can i load a 24 BPP image into a 16 BPP DirectX surface ??
This would help me save runtime memory and allow me to do alpha blending without having to set my primary surface and backbuffers to 24bbp.. Source code or links to it is most desireable but any good suggestions will help
thanks
Advertisement
You might find something interesting. Copy and save the following...
(excerpts from the MS DXSDK Help file, v7)

a)

D3DXLoadTextureFromFile
The D3DXLoadTextureFromFile function loads a texture from a file into a mipmap level, performing the necessary color conversion and re-scaling.

HRESULT D3DXLoadTextureFromFile(
LPDIRECT3DDEVICE7 pd3dDevice,
LPDIRECTDRAWSURFACE7 pTexture,
DWORD mipMapLevel,
LPSTR pSrcName,
RECT* pSrcRect,
RECT* pDestRect,
D3DX_FILTERTYPE filterType
);
Parameters
pd3dDevice
A pointer to the Direct3D device, an IDirect3DDevice7 interface, associated with the texture.
pTexture
A pointer to the DirectDrawSurface that was created with either the D3DXCreateTextureFromFile or D3DXCreateTexture functions. This parameter points to the texture surface object.
mipMapLevel
Indicates mipmap level. See remarks.
pSrcName
A pointer to the file name string. The following file types are supported: BMP, DIB, and DDS; for more information, see remarks.
pSrcRect
A pointer to a RECT structure, identifying the source rectangle. If NULL is specified for this parameter, the whole surface is used.
pDestRect
A pointer to a RECT structure, identifying the destination rectangle. If NULL is specified for this parameter, the whole surface is used.
filterType
A member of the D3DX_FILTERTYPE enumerated type, indicating the filter used for mipmap generation.
Return Values
If the function succeeds, the return value is S_OK.

If the function fails, the return value may be NULL or one of the following values:

D3DERR_TEXTURE_NO_SUPPORT
D3DXERR_D3DXNOTSTARTEDYET
D3DXERR_INVALIDFILEFORMAT
D3DXERR_INVALIDPARAMETER
D3DXERR_NOMATCHFOUND
D3DXERR_NOMEMORY
D3DXERR_NULLPOINTER
D3DXERR_UNSUPPORTEDFILEFORMAT
DDERR_DCALREADYCREATED
DDERR_GENERIC
DDERR_INCOMPATIBLEPRIMARY
DDERR_INVALIDCAPS
DDERR_INVALIDOBJECT
DDERR_INVALIDPARAMS
DDERR_INVALIDPIXELFORMAT
DDERR_INVALIDRECT
DDERR_INVALIDSURFACETYPE
DDERR_NOALPHAHW
DDERR_NOCOOPERATIVELEVELSET
DDERR_NODIRECTDRAWHW
DDERR_NOEMULATION
DDERR_NOEXCLUSIVEMODE
DDERR_NOFLIPHW
DDERR_NOMIPMAPHW
DDERR_NOOVERLAYHW
DDERR_NOTFOUND
DDERR_NOTLOCKED
DDERR_NOZBUFFERHW
DDERR_OUTOFMEMORY
DDERR_OUTOFVIDEOMEMORY
DDERR_PRIMARYSURFACEALREADYEXISTS
DDERR_SURFACEBUSY
DDERR_SURFACELOST
DDERR_UNSUPPORTED
DDERR_UNSUPPORTEDMODE
DDERR_WASSTILLDRAWING


Remarks
This function provides a mechanism to copy pixels from a file to a surface.

Concerning the mipMapLevel parameter:

If the specified mipmap level does not exist, as error is returned.
If D3DX_DEFAULT is specified and an equal number of mipmap levels exist, then all of the source mipmap levels are loaded.
If the source has mipmap levels and the destination does not, the top level is used.
If the destination has mipmap levels and the source does not, expansion is used.
If there are unequal numbers of mipmap levels between the source and destination, expansion is used.
In the pSrcName parameter, TGA is supported for the following cases: 16-, 24-, and 32-bpp direct color and 8-bpp palettized. Also, 8- and 16-bpp gray scale is supported. RLE versions of the preceding TGA formats are also supported. Note that colorkeying and premultiplied alpha are not currently supported for TGA files.

If the destination surface was created by Direct3DX, then the RECT structure should be specified in the original coordinates of that surface.

This function supports a full range of color-conversion for all surface formats that can be described by a D3DX_SURFACEFORMAT structure. It also implements filtered re-scaling and updates mipmaps automatically.

By taking pDestRect in the space of the original image, the application does not have to worry about how the surface may have been resized as part of the D3DXCreateTexture routine. For example, if an image was 100x150 pixels, then it may end up being 128x256 or 256x256 on different configurations. In both cases, the RECT structure to use to update the upper left quadrant is (0, 0, 50, 75); as if no resizing had taken place.

Requirements
Windows NT/2000: Requires Windows 2000.
Windows 95/98: Requires Windows 95 or later. Available as a redistributable for Windows 95.
Windows CE: Unsupported.
Version: Requires DirectX 7.0.
Header: Declared in d3dxcore.h.
Library: Use d3dx.lib.

See Also
D3DXCreateTextureFromFile, D3DXLoadTextureFromMemory, D3DXLoadTextureFromSurface

Built on Thursday, September 02, 1999

b:
D3DXCreateTextureFromFile
The D3DXCreateTextureFromFile function creates a texture object from a stand-alone file or from the resource.

HRESULT D3DXCreateTextureFromFile(
LPDIRECT3DDEVICE7 pd3dDevice,
LPDWORD pFlags,
LPDWORD pWidth,
LPDWORD pHeight,
D3DX_SURFACEFORMAT* pPixelFormat,
LPDIRECTDRAWPALETTE pDDPal,
LPDIRECTDRAWSURFACE7* ppDDSurf,
LPDWORD pNumMipMaps,
LPSTR pSrcName,
D3DX_FILTERTYPE filterType
);
Parameters
pd3dDevice
The Direct3D device, an IDirect3DDevice7 interface, associated with the texture.
pFlags
A pointer to a DWORD value. This parameter allows you to specify the D3DX_TEXTURE_NOMIPMAP flag if mipmap generation is not desired. This value can be NULL. The D3DX_TEXTURE_NOMIPMAP flag may be returned in the case where mipmap creation is not supported.
pWidth
A pointer to a DWORD value, indicating the width in pixels. If you specify zero or D3DX_DEFAULT, the width will be taken from the file. This parameter returns the corrected width.
pHeight
A pointer to a DWORD value, indicating the height in pixels. If you specify zero or D3DX_DEFAULT, the height will be taken from the file. This parameter returns the corrected height.
pPixelFormat
A pointer to a member of the D3DX_SURFACEFORMAT enumerated type. D3DX_DEFAULT is not valid for this parameter. The input value for this parameter should be the desired texture format. The return value will be the closest texture format that is supported by the hardware. If D3DX_SF_UNKNOWN is passed in, pixel format closest to the bitmap will be chosen.
pDDPal
A pointer to the DirectDraw palette, an IDirectDrawPalette interface, that is set (if present) on paletted surfaces. It is ignored even if it is set, for non-paletted surfaces.
ppDDSurf
The address of a pointer to a DirectDrawSurface object, the texture created that matched the specifications and the current hardware.
pNumMipMaps
A pointer to a DWORD value. The number of mipmaps generated.
pSrcName
A pointer to the file name string. The following file types are supported: BMP, DIB, and DDS; for more information, see remarks.
filterType
A member of the D3DX_FILTERTYPE enumerated type, indicating what filtering function should be used for any necessary resizing or mipmap generation.
Return Values
If the function succeeds, the return value is S_OK.

If the function fails, the return value may be NULL or one of the following values:

D3DERR_TEXTURE_NO_SUPPORT
D3DXERR_D3DXNOTSTARTEDYET
D3DXERR_INVALIDFILEFORMAT
D3DXERR_INVALIDPARAMETER
D3DXERR_NOMATCHFOUND
D3DXERR_NOMEMORY
D3DXERR_NULLPOINTER
D3DXERR_UNSUPPORTEDFILEFORMAT
DDERR_DCALREADYCREATED
DDERR_GENERIC
DDERR_INCOMPATIBLEPRIMARY
DDERR_INVALIDCAPS
DDERR_INVALIDOBJECT
DDERR_INVALIDPARAMS
DDERR_INVALIDPIXELFORMAT
DDERR_INVALIDRECT
DDERR_INVALIDSURFACETYPE
DDERR_NOALPHAHW
DDERR_NOCOOPERATIVELEVELSET
DDERR_NODIRECTDRAWHW
DDERR_NOEMULATION
DDERR_NOEXCLUSIVEMODE
DDERR_NOFLIPHW
DDERR_NOMIPMAPHW
DDERR_NOOVERLAYHW
DDERR_NOTFOUND
DDERR_NOTLOCKED
DDERR_NOZBUFFERHW
DDERR_OUTOFMEMORY
DDERR_OUTOFVIDEOMEMORY
DDERR_PRIMARYSURFACEALREADYEXISTS
DDERR_SURFACEBUSY
DDERR_SURFACELOST
DDERR_UNSUPPORTED
DDERR_UNSUPPORTEDMODE
DDERR_WASSTILLDRAWING


Remarks
Only BMP and DIB files are supported from the resource portion of the executable.


>>In the pSrcName parameter, TGA is supported >>for the following cases: 16-, 24-, and >>32-bpp direct color and 8-bpp palettized. >>Also, 8- and 16-bpp gray scale is supported. >>RLE versions of the preceding TGA formats >>are also supported. Note that colorkeying >>and premultiplied alpha are not currently >>supported for TGA files.

Specifying a numeric ID for the pSrcName parameter instructs the D3DXLoadTextureFromFile function to load bitmap images from the executable's resource; otherwise, pSrcName is assumed to be a file name. Note that you cannot use the string resource name, since the D3DXCreateTextureFromFile function treats a string pointer as a file name.

Unless the pFlags parameter is set to specifically prevent creating mipmaps, mipmaps are generated until reaching the minimum level size of 1x1.

For more information on mipmaps, see Texture Filtering With Mipmaps.

Width, height and pixel format are altered based on available hardware. For example, mipmap dimensions may be required to be powers of 2 and width may be required to be equal to height for square textures. Also, if the specified pixel format is unavailable, a best fit will be made.

Requirements
Windows NT/2000: Requires Windows 2000.
Windows 95/98: Requires Windows 95 or later. Available as a redistributable for Windows 95.
Windows CE: Unsupported.
Version: Requires DirectX 7.0.
Header: Declared in d3dxcore.h.
Library: Use d3dx.lib.

See Also
D3DXLoadTextureFromFile

Built on Thursday, September 02, 1999Requirements
Windows NT/2000: Requires Windows 2000.
Windows 95/98: Requires Windows 95 or later. Available as a redistributable for Windows 95.
Windows CE: Unsupported.
Version: Requires DirectX 7.0.
Header: Declared in d3dxcore.h.
Library: Use d3dx.lib.
See Also
D3DXLoadTextureFromFile

This topic is closed to new replies.

Advertisement