title: Logic for padding of an image array
body: <p>Pls. note this isn't any home work question, but work I am doing and where I needed some pointers.</p>
  
  <p>I have a 2D image stored in a 1D array(row major order). I need to pad this image with the replication of the border pixels by 1 row of padding at top, 1 row at bottom, 1 column of padding to right,1 column to left of the image. I am trying to do this in a C code. </p>
  
  <p>e.g. consider the 2D image astored in a 1D array as img[] shown below. Actual image is of size 3x4 (4 rows 3 columns) and after padding by 1 row and 1 column in both directions(up/down/left/right), the final image would be of size 5x6 (6 rows , 5 columns). </p>
  
  <pre><code>int img[30] = {0,0,0,0,0,0,1,2,3,0,0,4,5,6,0,0,7,8,9,0,0,10,11,12,0,0,0,0,0,0};
  </code></pre>
  
  <p>In my case, the size of padding is known, just 1 row at top and bottom, 1 column to right and left of the image. So one way i implemented this is by creating a mapping between the indices of the pixels in the given image array to the padded output pixel index. I stored this mapping in a precalculated lookup table and used that table in padding logic.</p>
  
  <p>After padding the output should look like:</p>
  
  <pre><code>int img[30] = {1,1,2,3,3,1,1,2,3,3,4,4,5,6,6,7,7,8,9,9,7,7,8,9,9};
  </code></pre>
  
  <p>The lookup table i created was:-</p>
  
  <pre><code>int lut[6,6,7,8,8,...];
  </code></pre>
  
  <p>and use this table as follows:</p>
  
  <pre><code>for(i=0;i&lt;30;i++)
  {
  img[i] = img[lut[i]]; //something on this lines...
  }
  </code></pre>
  
  <p>But I am interested in implementing this image padding logic programatically rathar than using lookup table based method. i.e. I want to calculate the pixel indices in code at run time.
  <strong>EDIT</strong> : Also I need to obtain the padded image in place(in same buffer which ofcourse would have provision of storage for the additional padded rows/columns).</p>
  
  <p>Any pointers what would the code look like.</p>
  
  <p>thank you.</p>
  
  <p>-AD</p>