Skip to main content
GameDev.net gamedev.net
🔒 Locked

Getting pixels from BMPs

Started by nickwinters Nov 6, 2004 at 12:43 PM 3 replies 450+ views
Original Post
nickwinters
nickwinters
I have the following code: IDirect3DTexture9* texture=0; NLogFile *lf=NLogFile::getInstance(); D3DLOCKED_RECT r; D3DXCreateTextureFromFile(EngineStatics::GetDevice(), filename, &texture); texture->LockRect(0, &r, 0, D3DLOCK_DISCARD); char* pr=(char*)r.pBits; while(pr!=0) { //Insert code for storing the pixel value here pr++; } My problem is that the loop never ends. Am I using the wrong terminate condition? Or am I incrementing the pointer incorrectly? Right now, I'm using the char* value as the pointer. Is this the correct type for reading in the pixel values for a bmp? Or is there a better way to do it? I'm using a grayscale bmp. Thanks. -Nick
Dim_Yimma_H
Dim_Yimma_H
r.pBits is an address and highly probably positive and larger than 0. If pr starts with that value and is repeatedly incremented it will never reach 0. Since you do the test (pr!=0) as condition for looping it will just keep looping. Instead find out the length of the image data and test (pr!=length) or (pr
\Jimmy H

Edit: missed a detail - (pr <= r.pBits + length)
Scet
Scet
I would suggest using an array rather than incrementing the pointer. Here's my code for accessing pixels.

//These are part of a class but I'm simplifying itD3DLOCKED_RECT Rc;D3DSURFACE_DESC Srfc;IDirect3DTexture8* Txr;struct Colour{unsigned long Val;} *ColourPntr;bool Texture::Lock(){if(FAILED(Txr->GetLevelDesc(0,&Srfc))){return 0;}if(FAILED(Txr->LockRect(0,&Rc,NULL,D3DLOCK_NOSYSLOCK))){return 0;}ColourPntr=(Colour *)Rc.pBits;return 1;}bool Texture::Unlock(){if(FAILED(Txr->UnlockRect(0))){return 0;}return 1;}bool Texture::SetTexel(unsigned long X,unsigned long Y,unsigned long TColour){if(X<Srfc.Width && Y<Srfc.Height){ColourPntr[(Y*Srfc.Height)+X].Val=TColour;}return 1;}unsigned long Texture::GetTexel(unsigned long X,unsigned long Y){if(X<Srfc.Width && Y<Srfc.Height){return ColourPntr[(Y*Srfc.Height)+X].Val;}return 0;}


Then you could easily loop though it

Texture Txr;//Load/Create TextureTxr.Lock();for(unsigned long X=0;X<Txr.Width;X++){for(unsigned long Y=0;Y<Txr.Height;Y++){//Store pixel infoSomething = Txr.GetTexel(X,Y);}}Txr.Unlock();
nickwinters
nickwinters
Yup, wrong ending condition, and wrong data type.

Thanks guys!

-Nick
nickwinters
nickwinters
I've taken them as unsigned longs, converted them to binary, broken them up into bytes, and have gotten the values. Is the format ARGB?

Thanks.

-Nick

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.