title: Convert PPM Image to Greyscale C++
body: <p>Trying to convert a PPM Image to greyscale by indexing the pointer containing the pixel data:</p>
  
  <pre><code>void PPMObject::greyScale()
  {
      const float r = 0.299F;
      const float g = 0.587F;
      const float b = 0.114F;
  
      int size = this-&gt;width * this-&gt;height * 3;
      for (int i = 0; i &lt; size; i++)
      {
          this-&gt;m_Ptr[i] = (this-&gt;m_Ptr[i] * r) + (this-&gt;m_Ptr[i] * g) + (this-&gt;m_Ptr[i] * b);
          this-&gt;m_Ptr[i+1] = (this-&gt;m_Ptr[i+1] * r) + (this-&gt;m_Ptr[i+1] * g) + (this-&gt;m_Ptr[i+1] * b);
          this-&gt;m_Ptr[i+2] = (this-&gt;m_Ptr[i+2] * r) + (this-&gt;m_Ptr[i+2] * g) + (this-&gt;m_Ptr[i+2] * b);
      }
  }
  </code></pre>
  
  <p>Where I've read the PPM Image file using the >> overload:</p>
  
  <pre><code>istream&amp; operator &gt;&gt;(istream &amp;inputStream, PPMObject &amp;other)
  {
      inputStream &gt;&gt; other.magicNum &gt;&gt; other.width &gt;&gt; other.height &gt;&gt; other.maxColorValue;
      inputStream.get();
      size_t size = other.width * other.height * 3;
      other.m_Ptr = new char[size];
      inputStream.read(other.m_Ptr, size);
      return inputStream;
  }
  </code></pre>
  
  <p>I write the data as follows:</p>
  
  <pre><code>ostream&amp; operator &lt;&lt;(ostream &amp;outputStream, const PPMObject &amp;other)
  {
      outputStream &lt;&lt; other.magicNum  &lt;&lt; " "
      &lt;&lt; other.width          &lt;&lt; " "
      &lt;&lt; other.height         &lt;&lt; " "
      &lt;&lt; other.maxColorValue  &lt;&lt; " "
      ;   
      outputStream.write(other.m_Ptr, other.width * other.height * 3);
      return outputStream;
  }
  </code></pre>
  
  <p>There are no problems with reading or writing the PPM file. </p>
  
  <p>The problem is simply converting the PPM Image to greyscale -- indexing is not the approach. The file is unchanged. </p>
  
  <p>The question is likely: How do I get the values from the pointer to manipulate them?</p>
  
  <p>E.g., Where are the pixels located in the char pointer? </p>
  
  <p>Averaging the RGB component values is certainly another approach but then how do I assign the average back into the pointer?</p>