title: Fastest way to bin an image along non-consecutive dimension
body: <p>I have an image which is 700x2000 uint8. Now I want to bin, that is to calculate the sum over every 4 rows, which are not consecutive in memory.</p>
  
  <p>Right now I am allocating a new array for this sum of the line and add every line to it. </p>
  
  <pre><code>float* binnedLine = new float[lineSize];
  char* binnedLineInt = new char[lineSize];
  
  for (int j = 0; j &lt; nlines; j++) 
  {
      if (j % binsize == 0) 
      {
          for (int i = 0; i &lt; lineSize; i++) 
          {
              binnedLine[i] = (static_cast&lt;float&gt; (static_cast&lt;char&gt;(data[j*lineSize + i]))) / binsize;
              binnedLineInt[i] = static_cast&lt;char&gt;(binnedLine[i]);
          };
      }
      else 
      {
          for (int i = 0; i &lt; lineSize; i++) 
          {
              binnedLine[i] += (static_cast&lt;float&gt; (static_cast&lt;char&gt;(data[j*lineSize + i]))) / binsize;
              binnedLineInt[i] = static_cast&lt;char&gt;(binnedLine[i]);
          };
      }
  
      ...
  }
  </code></pre>
  
  <p>However the performance is not really sufficient for my purposes (it takes 3ms per frame). Is there a more efficient way?</p>