Advertisement

Some DWORD/WORD/BYTE mixing.

Started by March 06, 2000 04:59 PM
0 comments, last by RoTTer 24 years, 7 months ago
Hello! Im not good (actually, I s*ck) in the differences of WORD/DWORD/etc. I actually know their sizes (uhm, I think I do) : BYTE is 1 byte (doh), WORD is 2, and DWORD is 4, right? But Im trying to use them in my BMP drawing functions and I think I am not getting it right (especially cause actually no BMP is being drawn on screen, only some weird stuff ). Well, my idea on drawing is this : I firstly load the bitmap from HD with a function I copied from the book (Lamothe''s). So I assume this is right. But as the BMP Im loading is 24bbp, and my surface is 16bbp, I have to convert. What do I do? Well, something probably stupid, but the only solution I came to this : I read the 4 first bytes of the BMP (actually from the BMP itself, not the header/stuff like that), and save it in a DWORD. Then I start 4 vars : DWORD red; DWORD green; DWORD blue; So I assume the first byte of the DWORD is the byte that represents the red color (ok, ok, this may be the blue one, Im not sure, but thats not the point right now.. if colors gets distorted, but work, I just reverse this), so to read the first byte of the DWORD I do this : (DWORD fourbytes ---> 1st 4 bytes of the BMP data) red = fourbytes >> 24; making "red" be : 00000000 00000000 00000000 RRRRRRRR, right? (Being RRRR... = whatever the 1st byte was) then I made something a bit more complex to get the "green" : green = ((fourbytes - (red << 24)) >> 16); this is composed by 2 steps : 1st) green = ((RRR... GGG... B... X... - (RRR... 000... 0... 0...)) >> 16) resulting : green = ((00000000 GGG... BBB.... X....) >> 16) and 2nd) green = (000... 000... 000.... GGG....) And to get the blue I made something very similar to get the green, but it would take more steps and bve more confuse, so Ill just leave it. You got the point already. So what I have is : red = 00000000 00000000 00000000 RRRRRRRR; green = 00000000 00000000 00000000 GGGGGGGG; blue = 00000000 00000000 00000000 BBBBBBBB; Now my 1st problem is : How to convert them to BYTE/UCHAR? I really dont know... I just actually, in my program, declared reg/green/blue as BYTEs, and the compile will give a warning that data may be lost. I just hope I lose the first 3 bytes (all the Zeroes). Ok, if I am lucky and done right Ill have now 3 vars of 1 byte each that has the red/green/blue info of the first pixel of the BMP. So what do I do to write? I tried this (didnt seem to work, tho, of course) : int Display_Bitmap (BITMAP_FILE_PTR bitmap_temp) { int start_x = 100; // 100,100 is for testing purposes int start_y = 100; UCHAR *vid_buffer = (UCHAR *) ddsd.lpSurface; int lpitch16 = (int)(ddsd.lPitch >> 1); for (int temp = 0; temp < bitmap_temp->bitmapinfoheader.biHeight; temp++) { /*_____*/for (int x_temp = 0; x_temp < bitmap_temp->bitmapinfoheader.biWidth*(bitmap_temp->bitmapinfoheader.biBitCount/8); x_temp=x_temp+3) /*_____*/{ /*__________*/DWORD dw4bytes = bitmap_temp->buffer[x_temp + temp*bitmap_temp->bitmapinfoheader.biWidth*(bitmap_temp->bitmapinfoheader.biBitCount/8)]; /*__________*/USHORT pixel = 0;//Convert_24bit_to_16bit (dw4bytes); /*__________*/vid_buffer[(start_x+x_temp) + (start_y+temp)*lpitch16] = pixel; /*_____*/} } return (1); } -------- Now Im *SURE* Ive messed somewhere in the DWORD/WORD/BYTE thing. I mean, I declare something as DWORD, and want to move 3 bytes the pointer so I ask it to move 3 and it will actually move 12 bytes. Im not sure how all this works, so I pasted here the code. What I meant with the : for (int x_temp = 0; x_temp < bitmap_temp->bitmapinfoheader.biWidth*(bitmap_temp->bitmapinfoheader.biBitCount/8); x_temp=x_temp+3) Acutally, with the "x_temp=x_temp+3" was to after taking the 1st 4 bytes as the DWORD, now take the 3rd, 4th, 5th and 6th byte. Then in the next step take the 6th, 7th, 8th and 9th... And keep doing this until I reach the end of the bitmap. Im sure I messed somewhere, cause this is so messy! I couldnt understand myself it (and I didnt copy from the book, I actually wrote -that crap-, I usually understand stuff I make up -even if crappy- better than what I copy from book). Well, if you took all the time to read this message and try to understand my problem, and know how could I from the bitmap buffer I already have a pointer to, read it (its 24 bbp), convert to 16bbp, and draw it on the screen wherever I want it to be (in my example I wanted it to be in 100,100, but that was arbitrary, I will want to be able to draw it anywhere). Thanks a lot (really a lot =]), -RoTTer, the PITA
Rather than try answer this here (cause honestly, I''m having a hard time understanding what you''re doing), have you looked in our DirectX section at the articles about dealing with 16 bit color? They should help.

Also, when casting to a smaller type, it''s the most significant bits that are lost, so if you''re casting from a DWORD to a BYTE with all the data in the rightmost bits of the DWORD, you''re fine.

This topic is closed to new replies.

Advertisement