title: Using Boost.GIL to convert an image into "raw" bytes
body: <h2>Goal</h2>
  <p>I'm trying to move towards Boost GIL to replace some similar functionality I've implemented that is reaching the end of its maintainable life.</p>
  <p>I have existing code that works with 24 BPP, 8bit RGB images using <code>uint8_t*</code>. I can't change that as the same interface is used to expose  images from different places (e.g. OpenGL buffers) and there's already quite a lot of code.</p>
  <p>Therefore I'm trying to use GIL in small steps, starting by reading a file and copying the pixels byte by byte into a <code>std::vector&lt;uint8_t&gt;</code> which I can use to manage the storage, but still get a <code>uint8_t*</code> by using <code>&amp;vector[0]</code>.</p>
  <p>This can be dropped in transparently behind the existing interfaces until it's at a point where refactoring makes sense.</p>
  <h2>What I've tried</h2>
  <p>I thought this should be a simple case of using <a href="http://stlab.adobe.com/gil/html/group___image_view_s_t_l_algorithms_copy_pixels.html#ga0" rel="nofollow noreferrer"><code>copy_pixels()</code></a> with two appropriately constructed views.</p>
  <p>I've put together a minimal, complete example that illustrates the sum of what I've manage to achieve by looking over the documents and trying things out:</p>
  <pre><code>#include &lt;boost/gil/rgb.hpp&gt;
  #include &lt;boost/gil/extension/io/png_dynamic_io.hpp&gt;
  #include &lt;stdint.h&gt;
  #include &lt;vector&gt;
  
  int main() {
    std::vector&lt;uint8_t&gt; storage;
    {
      using namespace boost::gil;
      rgb8_image_t img;
      png_read_image(&quot;test.png&quot;, img);
  
      // what should replace 3 here to be more &quot;generic&quot;?
      storage.resize(img.width()*img.height()*3);
  
      // doesn't work, the type of the images aren't compatible.
      copy_pixels(const_view(img), 
                  interleaved_view(img.width(), img.height(), &amp;storage[0], 3*img.width()));
    }
  }
  </code></pre>
  <h2>Where I'm stuck</h2>
  <p>This doesn't compile:</p>
  <pre><code>error: cannot convert ‘const boost::gil::pixel&lt;unsigned char, boost::gil::layout&lt;boost::mpl::vector3&lt;boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t&gt; &gt; &gt;’ to ‘unsigned char’ in assignment
  </code></pre>
  <p>Which is fairly self-explanatory - an RGB pixel can't be converted to a single <code>unsigned char</code> automatically. I thought I'd try using <a href="http://www.boost.org/doc/libs/1_47_0/libs/gil/doc/html/g_i_l_0144.html" rel="nofollow noreferrer"><code>copy_and_convert_pixels()</code></a> to fix this, but I can't see a way around the 3:1 (i.e. I have 3 <code>unsigned char</code>s in the output for every pixel in the source image) problem with these conversions. Conversion seems to be more aimed at colourspace conversions (e.g. RGB-&gt;HSV) or packing changes.</p>