{"id":17443,"date":"2009-07-17T10:00:00","date_gmt":"2009-07-17T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2009\/07\/17\/the-disembodiment-of-dibs-from-the-dib-section\/"},"modified":"2009-07-17T10:00:00","modified_gmt":"2009-07-17T10:00:00","slug":"the-disembodiment-of-dibs-from-the-dib-section","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20090717-00\/?p=17443","title":{"rendered":"The disembodiment of DIBs from the DIB section"},"content":{"rendered":"<p>\nSo far this week, we&#8217;ve\n<!-- backref: DIB color table -->\nseparated the DIB metadata (<code>BITMAPINFO<\/code>)\nfrom the pixels of a DIB section<\/a>.\nBut there&#8217;s really no need for the DIB section at all!\nAs long as you have the pixels and the metadata, you\ncan draw bits.\n<\/p>\n<p>\nWe demonstrate this by drawing a rather stupid-looking bitmap\nonto the screen, but doing so without the use of <code>HBITMAP<\/code>s\nat all!\nStart with a brand new\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nscratch program<\/a> and make these changes:\n<\/p>\n<p><pre>\n<font COLOR=\"blue\">const BYTE g_rgbPixels[16][8] = {\n { 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD },\n { 0xDD, 0xDD, 0xD0, 0x00, 0x00, 0xDD, 0xDD, 0xDD },\n { 0xDD, 0xD0, 0x01, 0x11, 0x11, 0x00, 0xDD, 0xDD },\n { 0xDD, 0x01, 0x11, 0x11, 0x11, 0x11, 0x0D, 0xDD },\n { 0xD0, 0x11, 0x11, 0x11, 0x11, 0x11, 0x10, 0xDD },\n { 0xD0, 0x11, 0x00, 0x00, 0x00, 0x01, 0x10, 0xDD },\n { 0x01, 0x11, 0x09, 0x99, 0x99, 0x01, 0x11, 0x0D },\n { 0x01, 0x11, 0x09, 0x99, 0x99, 0x01, 0x11, 0x0D },\n { 0x01, 0x11, 0x09, 0x99, 0x99, 0x01, 0x11, 0x0D },\n { 0x01, 0x11, 0x09, 0x99, 0x99, 0x01, 0x11, 0x0D },\n { 0x01, 0x11, 0x09, 0x99, 0x99, 0x01, 0x11, 0x0D },\n { 0xD0, 0x11, 0x00, 0x00, 0x00, 0x01, 0x10, 0xDD },\n { 0xD0, 0x11, 0x11, 0x11, 0x11, 0x11, 0x10, 0xDD },\n { 0xDD, 0x01, 0x11, 0x11, 0x11, 0x11, 0x0D, 0xDD },\n { 0xDD, 0xD0, 0x01, 0x11, 0x11, 0x00, 0xDD, 0xDD },\n { 0xDD, 0xDD, 0xD0, 0x00, 0x00, 0xDD, 0xDD, 0xDD },\n};\nstruct BITMAPINFO16 {\n BITMAPINFOHEADER bmiHeader;\n RGBQUAD bmiColors[16];\n} g_bmi = {\n { sizeof(g_bmi.bmiHeader), \/\/ biSize\n                        16, \/\/ biWidth\n                        16, \/\/ biHeight\n                         1, \/\/ biPlanes\n                         4, \/\/ biBitCount\n                    BI_RGB, \/\/ biCompression\n                         0, \/\/ biSizeImage\n                         0, \/\/ biXPelsPerMeter\n                         0, \/\/ biYPelsPerMeter\n                        16, \/\/ biClrUsed\n                        16, \/\/ biClrImportant\n },\n {\n  { 0x00, 0x00, 0x00, 0x00 },\/\/ bmiColors[0]\n  { 0x80, 0x00, 0x00, 0x00 },\/\/ bmiColors[1]\n  { 0x00, 0x80, 0x00, 0x00 },\/\/ bmiColors[2]\n  { 0x80, 0x80, 0x00, 0x00 },\/\/ bmiColors[3]\n  { 0x00, 0x00, 0x80, 0x00 },\/\/ bmiColors[4]\n  { 0x80, 0x00, 0x80, 0x00 },\/\/ bmiColors[5]\n  { 0x00, 0x80, 0x80, 0x00 },\/\/ bmiColors[6]\n  { 0xC0, 0xC0, 0xC0, 0x00 },\/\/ bmiColors[7]\n  { 0x80, 0x80, 0x80, 0x00 },\/\/ bmiColors[8]\n  { 0xFF, 0x00, 0x00, 0x00 },\/\/ bmiColors[9]\n  { 0x00, 0xFF, 0x00, 0x00 },\/\/ bmiColors[10]\n  { 0xFF, 0xFF, 0x00, 0x00 },\/\/ bmiColors[11]\n  { 0x00, 0x00, 0xFF, 0x00 },\/\/ bmiColors[12]\n  { 0xFF, 0x00, 0xFF, 0x00 },\/\/ bmiColors[13]\n  { 0x00, 0xFF, 0xFF, 0x00 },\/\/ bmiColors[14]\n  { 0xFF, 0xFF, 0xFF, 0x00 },\/\/ bmiColors[15]\n }\n};<\/font>\nvoid\nPaintContent(HWND hwnd, PAINTSTRUCT *pps)\n{\n<font COLOR=\"blue\"> StretchDIBits(pps-&gt;hdc, 0, 0,\n               g_bmi.bmiHeader.biWidth*4,\n               g_bmi.bmiHeader.biHeight*4,\n               0, 0,\n               g_bmi.bmiHeader.biWidth,\n               g_bmi.bmiHeader.biHeight,\n               g_rgbPixels, (BITMAPINFO*)&amp;g_bmi,\n               DIB_RGB_COLORS, SRCCOPY);<\/font>\n}\n<\/pre>\n<p>\nWe are drawing a bitmap without using an <code>HBITMAP<\/code>!\nThe technique is the same one we&#8217;ve been using all week:\nBuilding a <code>BITMAPINFO<\/code> and using it\nto guide the interpretation of a chunk of memory containing\npixels.\nJust to make things easier to see, I magnified the image,\nbut the point is the same.\n<\/p>\n<p>\nOkay, now let&#8217;s look at the limitations and caveats of this technique.\nFirst, of course, is that this works only when the memory contains\nthe source bitmap for a\n<code>BitBlt<\/code> or\n<code>StretchBlt<\/code> operation.\nIt&#8217;s nice when you have a bunch of bitmaps that you only intend to\ndraw <i>from<\/i>,\nbut\nyou can&#8217;t use this technique to draw <i>into<\/i> a chunk of memory.\nFor that, you&#8217;ll want to create a DIB section and draw into that.\n<\/p>\n<p>\nAnother problem with this technique is that it doesn&#8217;t\nplay friendly with\nremote desktops,\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/01\/03\/508694.aspx\">\none of those Windows programming taxes<\/a>.\nAfter all, pumping a chunk of pixels to the screen is the\nlogical equivalent of a <code>BitBlt<\/code> of a bitmap\nnobody has ever seen before.\nYou&#8217;ve got pixels just coming out of nowhere.\nThere&#8217;s nothing to cache since there is no <code>HBITMAP<\/code>\nto associate the pixels with.\nOn remote desktops, it&#8217;s probably okay to\nuse this technique for small bitmaps\n(like ours) where the cost of sending the pixels is insigificant,\nor if your render target is not the screen (for example,\nif you are rendering to an off-screen bitmap because you&#8217;re doing some\nimage manipulation behind the scenes).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So far this week, we&#8217;ve separated the DIB metadata (BITMAPINFO) from the pixels of a DIB section. But there&#8217;s really no need for the DIB section at all! As long as you have the pixels and the metadata, you can draw bits. We demonstrate this by drawing a rather stupid-looking bitmap onto the screen, but [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-17443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>So far this week, we&#8217;ve separated the DIB metadata (BITMAPINFO) from the pixels of a DIB section. But there&#8217;s really no need for the DIB section at all! As long as you have the pixels and the metadata, you can draw bits. We demonstrate this by drawing a rather stupid-looking bitmap onto the screen, but [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/17443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=17443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/17443\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=17443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=17443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=17443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}