title: What would be an ideal data structure to represent an image matrix?
body: <p>As I'm doing a computer vision library, I wondered what would be the ideal data structure to save an image's data.</p>
  
  <p>Someone told me that doing an std::vector imbrication is pretty bad since data contiguity isn't guaranteed, thus a simple one is more efficient.</p>
  
  <p>Problem is, the vector's content has to be colorspace-dependant. Plus, for size issues it would be best to have an unsigned char per channel, though I'd be better off having floats to perform operations.</p>
  
  <p>To handle colorspaces, should I increase the size of the vector (like tripling it to handle RGB), so I could access data like this:</p>
  
  <pre><code>data[0] =&gt; pixel 1, Red
  data[1] =&gt; pixel 1, Green
  data[2] =&gt; pixel 1, Blue
  data[3] =&gt; pixel 2, Red
  data[4] =&gt; pixel 2, Green
  ... etc
  </code></pre>
  
  <p>Or is it possible to do this more efficiently (like using a long int or a float to keep all channel's values into one single variable) and if so, how? How could I make this generic to handle 1, 2, 3 or 4 channels so that I can change colorspace (relatively) easily?</p>
  
  <p><strong>The question is not about the container (I am using a single std::vector already), but the content itself.</strong></p>
  
  <p><strong>EDIT</strong>: Since this question has been marked as primarily opinion-based, I will try to make it clearer.</p>
  
  <p>At the moment, I'm storing an image's data in a <code>std::vector&lt;float&gt;</code>. The problem here is that I don't know how many items to store (e.g. 3 times the size to handle RGB/HSV, or implementing a 4-items struct (RGBA) even when I'm only using one (grayscale)?) in order to efficiently change colorspace or perform operations on matrices when I need to. As this is meant to be integrated in a computer vision library, it is <strong>highly likely</strong> to happen at least once every frame.</p>
  
  <p><em>I'm asking for something <strong>efficient</strong> in my case here, which has nothing (or very little) to do with opinion. There could be several good answers and chosing among them relies on opinion, but this is supposed to be my call.</em></p>