How to edit a texture after I create it from file?

Started by
7 comments, last by FutureCode 13 years, 10 months ago
I mean that I want to replace some pixels and add some alpha value.
What should I do?
Advertisement
You can lock it - make the changes - and then unlock it again.

Here's what I usually use:

//// Special class for locking surfaces and accessing pixels.// Supported pixel formats are 8, 16, 24 and 32-bit modes.//template <class PixelColor, unsigned int PITCH_DIVISOR> class LckS {public:	LPDIRECT3DSURFACE9 surf;	D3DLOCKED_RECT lr;	union	{		PixelColor *d;		BYTE *c;	};	DWORD pitch;	inline LckS( LPDIRECT3DSURFACE9 _s, RECT *srcRect )	{		surf = _s;		surf->LockRect( &lr, srcRect, 0 );		d = static_cast<PixelColor *>(lr.pBits);		pitch =(DWORD)(lr.Pitch >> PITCH_DIVISOR);	}	inline ~LckS()	{		surf->UnlockRect( );	}		inline bool isLocked()	{		return (d != 0);	}};#define LOCALLOCK(surface, srcRect, pixelFormat, divisor) LckS< pixelFormat, divisor > __l( surface, srcRect )#define PIX __l.d#define PITCH (__l.pitch)#define PIXEL(x,y) __l.d[(x) + (y)*PITCH]#define IS_LOCKED (PIX != 0)


And here is an example of using the lock class:

LPDIRECT3DTEXTURE9 pTexture; // This is your previously loaded texture.//// Retrieve the surface image of 'pTexture'//LPDIRECT3DSURFACE9 pTexSurface;HR (pTexture->GetSurfaceLevel(0, &pTexSurface) );//// Lock and fill the surface//{    LOCALLOCK(pTexSurface, NULL, D3DCOLOR, 2);    //    // Fill with white all at once.    //    memset( PIX, 0xff, GetWidth() * GetHeight() * sizeof(D3DCOLOR) );    //    // Fill with gray using a pixel-by-pixel technique.    //    for (UINT i = 0; i < (UINT)GetHeight(); i++)    {        for (UINT j = 0; j < (UINT)GetWidth(); j++)        {            PIXEL(j, i) = D3DCOLOR_COLORVALUE( 0.5f, 0.5f, 0.5f, 1.0f );        }    }} // <========== LckS goes out of scope hereSAFE_RELEASE( pTexSurface );pTexture->GenerateMipSubLevels();
So I've got a D3DLOCKED_RECT.
Could you give me an example of how to replace a pixel?
My texture is using A8R8G8B8.
Quote:Original post by FutureCode
So I've got a D3DLOCKED_RECT.
Could you give me an example of how to replace a pixel?

My texture is using A8R8G8B8.

void SetPixel(const D3DLOCKED_RECT& rect, int x, int y, D3DCOLOR clr){   // Cast the locked bits pointer to a BYTE* so we can do some pointer arithmetic on it   BYTE* pBits = (BYTE*)rect.pBits   // Skip to the start of the row we want   pBits += rect.Pitch * y;   // Skip to the pixel we want   const int cnBytesPerPixel = 4; // 32-bit format   pBits += cnBytesPerPixel * x;   // Cast pointer to a D3DCOLOR (32-bit value) so we can write the whole colour in one go   D3DCOLOR* pPixel = (D3DCOLOR)pBits;   // Replace the pixel   *pPixel = clr;}
That code is just an example, you normally wouldn't want to call a function to set a single colour (Since you're usually setting several pixels at once). That code also assumes that the (x, y) position passed is relative to the top left of the RECT locked (So it's in "real" coordinates if you lock the entire surface).
For A8R8G8B8, just take the example above and replace the '2' in LOCALLOCK with a '4', because there are 4 bytes in one pixel and the locked rect's pitch will be the number of bytes in one row of the image's surface.
Thank you, I've just made it.
And I finally found how those programmers use transparency using *.bmp images...
Quote:Original post by FutureCode
Thank you, I've just made it.
And I finally found how those programmers use transparency using *.bmp images...
I'd avoid using alpha in .bmp files, it's not officially supported, although a lot of applications allow it (Technically the other 8 bits in 32-bit BMPs are "reserved", not alpha).

If you're using alpha, I'd recommend using PNG or DDS files.
I mean that, they read a bmp which doesn't contains alpha, and draw it with varied alpha...

This topic is closed to new replies.

Advertisement