title: (C++)(Visual Studio) Change RGB to Grayscale
body: <p>I am accessing the image like so:</p>
  
  <pre><code>pDoc = GetDocument();
  
  int iBitPerPixel = pDoc-&gt;_bmp-&gt;bitsperpixel;    // used to see if grayscale(8 bits) or RGB (24 bits)
  int iWidth = pDoc-&gt;_bmp-&gt;width;
  int iHeight = pDoc-&gt;_bmp-&gt;height;
  BYTE *pImg = pDoc-&gt;_bmp-&gt;point;     // pointer used to point at pixels in the image
  int Wp = iWidth;
  const int area = iWidth * iHeight;
  int r;          // red pixel value
  int g;          // green pixel value
  int b;          // blue pixel value
  int gray;       // gray pixel value
  
  BYTE *pImgGS = pImg;                    // grayscale image pixel array
  </code></pre>
  
  <p>and attempting to change the rgb image to gray like so:</p>
  
  <pre><code>    // convert RGB values to grayscale at each pixel, then put in grayscale array
      for (int i = 0; i&lt;iHeight; i++)
          for (int j = 0; j&lt;iWidth; j++)
          {
              r = pImg[i*iWidth * 3 + j * 3 + 2];
              g = pImg[i*iWidth * 3 + j * 3 + 1];
              b = pImg[i*Wp + j * 3];
  
              r * 0.299;
              g * 0.587;
              b * 0.144;
  
              gray = std::round(r + g + b);
  
              pImgGS[i*Wp + j] = gray;
          }
  </code></pre>
  
  <p>finally, this is how I try to draw the image:</p>
  
  <pre><code>//draw the picture as grayscale
  for (int i = 0; i &lt; iHeight; i++) {
      for (int j = 0; j &lt; iWidth; j++) {
          // this should set every corresponding grayscale picture to the current picture as grayscale
          pImg[i*Wp + j] = pImgGS[i*Wp + j];
      }
  }
  }
  </code></pre>
  
  <p>original image:
  <a href="https://i.stack.imgur.com/880bp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/880bp.png" alt="enter image description here"></a>
  and the resulting image that I get is this:
  <a href="https://i.stack.imgur.com/0pPry.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0pPry.png" alt="enter image description here"></a></p>