Archived

This topic is now archived and is closed to further replies.

Muzlack

Color Modulation

Recommended Posts

here is some color modulation code... What is wrong with it? In an example, it turns out a bunch of purple and blue lines.
  
memset(&ddsd,0,sizeof(ddsd));
		ddsd.dwSize=sizeof(ddsd);
	
		pSrcTexture.texture->Lock(NULL,&ddsd,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);

		unsigned short* data = (unsigned short*) ddsd.lpSurface;
		for(int y1=0; y1<ddsd.dwHeight; y1++ ) {
			for(int x1=0; x1<ddsd.dwWidth; x1++) {
				
				unsigned short color=data[y1*ddsd.lPitch+x1];
				short r1 = color & 0xF800 >> 11;
				short g1 = color & 0x07E0 >> 5;
				short b1 = color  & 0x001F;

				short r2=(r*r1)/255;
				short b2=(b*b1)/255;
				short g2=(g*g1)/255;
				
				
				data[y1*ddsd.dwWidth+x1]=RGB16(r2,b2,g2);
				
				
			}
		}

		pSrcTexture.texture->Unlock(NULL);
  

Share this post


Link to post
Share on other sites
I''ll assume you''re using 16 bit color in DirectX.
I believe one problem is how memory is being addressed with two parts of this code.

unsigned short color=data[y1*ddsd.lPitch+x1]; // Here
short r1 = color & 0xF800 >> 11;
short g1 = color & 0x07E0 >> 5;
short b1 = color & 0x001F;
short r2=(r*r1)/255;
short b2=(b*b1)/255;
short g2=(g*g1)/255;
data[y1*ddsd.dwWidth+x1]=RGB16(r2,b2,g2); // And Here

Because a surface sometimes is formated wider than its width you need to use its pitch to address pixels in it.

Note: Since the pitch is returned in bytes you will have to divide it by the number of bytes per pixel to use for addressing pixels, example - divide by 2 for 16 bit color because there are 2 bytes per pixel

You should have something like this:

color=data[y1*(ddsd.lPitch>>1)+x1];

data[y1*(ddsd.lPitch>>1)+x1]=RGB16(r2,b2,g2);

Hope this helps!!

Share this post


Link to post
Share on other sites