RGB

Started by
2 comments, last by whats a username 22 years, 8 months ago
In C++ and DirectX 7 I want to get the red, green, and blue values for any pixel on a surface. Does anyone know of a function or tutorial for that. Thank you so much
Advertisement
not a simple thing...

1) first you have to lock the surface and extract info about the surface

2) get a pointer to the surface buffer

3) calculate the index of the pixel you want

4) read the byte(8 bit) word (16 bit) Dword(24,32 bit) at this local and compare the color value...

code for 16 bits... (not tested may not be complete but will get you started)

  // initializes a direct draw struct#define DD_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }#define _RGB16BIT(r,g,b) ((b%32)+((g%32)<<6)+((r%32)<<11))//make sure the surface has been created properly//for 16 bit depth before using this function BOOL CheckPixelForBlack(LPDIRECTDRAWSURFACE7 lpdds, int x, int y){  DDSURFACEDESC2 ddsd;  WORD lpitch;  WORD *video_buffer;  BOOL returnValue = FALSE;  // lock the surface  DD_INIT_STRUCT(ddsd);  lpdds->Lock (NULL,&ddsd,               DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR,NULL);   // get the memory pitch for 16 bits  lpitch = ddsd.lPitch/2;   video_buffer =  (WORD *)ddsd.lpSurface;  if (video_buffer[x+y*lpitch]==_RGB16bit(0,0,0))  {    returnValue =TRUE;  }  else  {    returnValue = FALSE;  }  // unlock the surface memory  lpdds->Unlock(video_buffer);  return  returnValue;}  




big kid with adult powers
==========================big kid with adult powers==========================
BTW..

THose #defines should be on separate lines and in my if statement
_RGB16bit(0,0,0) should be _RGB16BIT(0,0,0)

big kid with adult powers
==========================big kid with adult powers==========================
Thanks Choff. I modified Choff''s code to make a function that returns a number based on the pixel color shown below.

// initializes a direct draw struct
#define DD_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }
int GetPixelColor(LPDIRECTDRAWSURFACE7 lpdds, int x, int y)
{
DDSURFACEDESC2 ddsd;
WORD lpitch;
WORD *video_buffer;
int returnValue;
// lock the surface
DD_INIT_STRUCT(ddsd);
lpdds->Lock (NULL,&ddsd, DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR,NULL);
// get the memory pitch for 16 bits
lpitch = ddsd.lPitch/2;
video_buffer = (WORD *)ddsd.lpSurface;

returnValue = video_buffer[x+y*lpitch];

// unlock the surface memory
lpdds->Unlock(NULL);
return returnValue;
}

The RGB macro returns a different number for a color than the above function above does.
How would I get the red green and blue values out of the number returned by the function above?

This topic is closed to new replies.

Advertisement