Archived

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

a question about DIB

This topic is 5621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


  
      case WM_PAINT:
        PAINTSTRUCT ps;
   	HDC dc = BeginPaint(Handle, &ps);
            BYTE* bits;

            BITMAPINFO bi;

            bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            bi.bmiHeader.biWidth = 800;
            bi.bmiHeader.biHeight = 800;
            bi.bmiHeader.biPlanes = 1;
            bi.bmiHeader.biBitCount = 32;
            bi.bmiHeader.biCompression = BI_RGB;
            bi.bmiHeader.biSizeImage = 0;
            bi.bmiHeader.biXPelsPerMeter = 0;
            bi.bmiHeader.biYPelsPerMeter = 0;                
            bi.bmiHeader.biClrUsed = 0;
            bi.bmiHeader.biClrImportant = 0;
//            bi.bmiColors = NULL;


            HBITMAP bmp = CreateDIBSection(dc, &bi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);

            HDC ddc = CreateCompatibleDC(GetDC(Handle));

            SelectObject(ddc, bmp);

            for(int y = 0; y < 800; y++)
	      {
	      for(int x = 0; x < 800; x += 4)
              	{
               	*(bits + (y * (800)) + x) = 255;
               	*(bits + (y * (800)) + x + 1) = 0;
               	*(bits + (y * (800)) + x + 2) = 255;
                }

            BitBlt(GetDC(Handle), 0, 0, 800, 800, ddc, 0, 0, SRCCOPY);
         EndPaint(Handle, &ps);
  
Why does this code only paint 1/3 of the DIB''s area purple? Furthermore, if I change the bitrate to 24 and the offset in the outer loop to 3 instead of 4, it splits that same 1/3 down to 3 equal recatngles and draws them purple, yellow and cyan, respectively. What am I missing here? Thanks enclosed, Crispy

Share this post


Link to post
Share on other sites
two things:

1. when navigating the bits, you''re dealing with an array of RGBQUADs. however, your code treats each element as a BYTE. you''d need something like

*(bits + ((y * (800)) + x)*sizeof(RGBQUAD) + 2) = 255;

2. you should (with your numbers, pitch probably would be 800*sizeof(RGBQUAD) anyway, but it''s a good practice to always use the pitch) use GetObject to get a BITMAP struct for your bitmap, and use its WidthBytes member instead of 800, like so:

*(bits + ((y * bm.bmWidthBytes) + x)*sizeof(RGBQUAD) + 2) = 255;

hopefully i got the code right.

---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites
Fixed it. Thanks!

quote:
Original post by niyaw
1. when navigating the bits, you're dealing with an array of RGBQUADs. however, your code treats each element as a BYTE. you'd need something like



Look at my sample - I'm not treating them as Byte-sized elements, but as 4-byte clusters (the step of the inner loop is 4), which should be sizeof(RGBQUAD). To be even more exact, the code you gave would only work with 32-bit data - to create a 24-bit DIB you'd have to replace sizeof(RGBQUAD) with 3:

quote:
Excerpt from MSDN/compiler docs -> BITMAPINFO
24 The bitmap has a maximum of 2^24 colors, and the bmiColors member is NULL. Each 3-byte triplet in the bitmap array represents the relative intensities of blue, green, and red, respectively, for a pixel.




quote:
Original post by niyaw
hopefully i got the code right.



Your code is perfect.


Crispy

EDIT: quote tags

[edited by - crispy on July 24, 2002 5:58:51 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Crispy
Look at my sample - I''m not treating them as Byte-sized elements, but as 4-byte clusters (the step of the inner loop is 4), which should be sizeof(RGBQUAD).


mkay. but the width of one line is 800*sizeof(RGBQUAD), so I suppose the working code would be

*(bits + (y * (800*sizeof(RGBQUAD)) + x) + 2) = 255;

?
quote:

To be even more exact, the code you gave would only work with 32-bit data


yeah, i guess i should''ve checked with msdn first.

---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites