title: RGB565 to Grayscale
body: <p>I am trying to convert an RGB565 to the Grayscale image.</p>
  
  <p>I used the below code to do the conversion. I have a rgb565 pixel. I am getting R,G and B values and doing the conversion operation. Once the grayscale is obtained, I am reconstructing a 16 bit pixel values wherein R, G and B will have the same grayscale value.</p>
  
  <pre><code> int16_t pixel = *buffer;
   int16_t red = ((pixel &amp; 0xF800)&gt;&gt;11);
   int16_t green = ((pixel &amp; 0x07E0)&gt;&gt;5);
   int16_t blue = (pixel &amp; 0x001F);
   int16_t grayscale = (0.2126 * red) + (0.7152 * green) + (0.0722 * blue);
   *buffer=(grayscale&lt;&lt;11)+(grayscale&lt;&lt;5)+grayscale;
  </code></pre>
  
  <p>Here, I expected a grayscale image. However, what is happening is -></p>
  
  <p>Green color has 6 bits of space (has more magnitude). The resulting grayscale value should be 5bits so that, i will fit for R G and B. However, the calculation done provides a 6 bit grayscale because of which I am not getting a proper grayscale image.</p>
  
  <p>Could anyone help me to convert a rgb565 to grayscale and store it as 16 bits.</p>