title: How to make 8-bit bitmap appear as monochrome in C++?
body: <p>When I set up and create a 24-bit bitmap like this:</p>
  
  <pre><code>   //fileheader
      BITMAPFILEHEADER* bf = new BITMAPFILEHEADER;
      bf-&gt;bfType = 0x4d42;
      bf-&gt;bfSize = 6054400 + 54;
      bf-&gt;bfOffBits = 54;
  
      //infoheader
      BITMAPINFOHEADER* bi = new BITMAPINFOHEADER;
      bi-&gt;biSize = 40;
      bi-&gt;biWidth = 2752;
      bi-&gt;biHeight = -733;
      bi-&gt;biPlanes = 1;
      bi-&gt;biBitCount = 24;
      bi-&gt;biCompression = 0;
      //bi-&gt;biSizeImage = 6054400;
      bi-&gt;biXPelsPerMeter = 2835;
      bi-&gt;biYPelsPerMeter = 2835;
      bi-&gt;biClrUsed = 0;
      bi-&gt;biClrImportant = 0;
  
      pFrame-&gt;GetImage(m_imageData);
  
      //
      //create bitmap...
      //(hbit is a global variable)
  
      BITMAPINFO* bmi;
      bmi = (BITMAPINFO*)bi; 
      HDC hdc = ::GetDC(NULL);
  
      hbit = CreateDIBitmap(hdc, bi, CBM_INIT, m_imageData, bmi, DIB_RGB_COLORS);
  </code></pre>
  
  <p>I get an output image like this:
  <img src="https://i.imgur.com/oM1eL8o.jpg" alt="image1"></p>
  
  <p>But when I change bitcount from 24 to 8 (which also allows for 3x image size, allowing me to go from 733 width to the image's natural width of 2200), I get an image like this (along with a lot of instability):</p>
  
  <p><img src="https://i.imgur.com/keFPyMx.png" alt="image2"></p>
  
  <p>My output looks like this:</p>
  
  <pre><code>    BITMAP* bi = new BITMAP;
      CBitmap bmp;
      bmp.Attach(hbit);
      CClientDC dc(pWnd);
      CDC bmDC;
      bmDC.CreateCompatibleDC(&amp;dc);
      CBitmap *pOldbmp = bmDC.SelectObject(&amp;bmp);
      bmp.GetBitmap(bi);
      dc.BitBlt(384,26,bi-&gt;bmWidth/3,bi-&gt;bmHeight,&amp;bmDC,0,0,SRCCOPY);
      //note: if bitcount is 8, height and width need to be /3, 
            //if 24, only width gets /3 
      bmDC.SelectObject(pOldbmp);
  
      //explicitly delete everything just to be safe
      delete bi;
      DeleteObject(bmp);
      DeleteObject(dc);
      DeleteObject(pOldbmp);
      DeleteObject(bmDC);
  </code></pre>
  
  <p><strong>So my questions are:</strong></p>
  
  <ul>
  <li>Why is this happening when I switch from 24 to 8?</li>
  <li>Is there an easy way to output the image as monochrome rather than color?</li>
  </ul>
  
  <p><strong>One last thing:</strong></p>
  
  <p>My coworker wrote this function a long time ago for a similar issue, but he said I may be able to use it.  I can't get it to work, unfortunately:</p>
  
  <pre><code>void CopyMono8ToBgrx(byte* pDestBlue, byte* pDestGreen, byte *pDestRed, byte* pDestAlpha)
      {
          byte*               pSrc;
          byte*               pSrcEnd;
  
          pSrc = ( byte* ) m_imageData;
          pSrcEnd = pSrc + ( 2752*2200 );
  
          while ( pSrc &lt; pSrcEnd )
          {
              byte data = *pSrc;
  
              *pDestBlue  = data;
              *pDestGreen = data;
              *pDestRed   = data;
              *pDestAlpha = 255;  // alpha is always 255 (fully opaque)
  
              pSrc++;
              pDestBlue   += 4;
              pDestGreen  += 4;
              pDestRed    += 4;
              pDestAlpha  += 4;
          }
      }
  </code></pre>