title: Calculating the vertical gradient of 2D image causes strange output
body: <p>I want to apply a simple derive/gradient filter, [-1, 0, 1], to an image from a .ppm file.</p>
  
  <p>The raw binary data from the .ppm file is read into a one-dimensional array:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>uint8_t* raw_image_data;
  size_t n_rows, n_cols, depth;
  
  // Open the file as an input binary file
  std::ifstream file;
  file.open("test_image.ppm", std::ios::in | std::ios::binary);
  if (!file.is_open())  { /* error */ }
  
  std::string temp_line;
  // Check that it's a valid P6 file
  if (!(std::getline(file, temp_line) &amp;&amp; temp_line == "P6")) {}
  // Then skip all the comments (lines that begin with a #)
  while (std::getline(file, temp_line) &amp;&amp; temp_line.at(0) == '#');
  
  // Try read in the info about the number of rows and columns
  try {
      n_rows = std::stoi(temp_line.substr(0, temp_line.find(' ')));
      n_cols = std::stoi(temp_line.substr(temp_line.find(' ')+1,temp_line.size()));
      std::getline(file, temp_line);
      depth = std::stoi(temp_line);
  } catch (const std::invalid_argument &amp; e) { /* stoi has failed */}
  
  // Allocate memory and read in all image data from ppm
  raw_image_data = new uint8_t[n_rows*n_cols*3];
  file.read((char*)raw_image_data, n_rows*n_cols*3);
  file.close();
  </code></pre>
  
  <p>I then read a grayscale image from the data into a two-dimensional array, called <code>image_grayscale</code>:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>uint8_t** image_grayscale;
  image_grayscale = new uint8_t*[n_rows];
  for (size_t i = 0; i &lt; n_rows; ++i) {
      image_grayscale[i] = new uint8_t[n_cols];
  }
  
  // Convert linear array of raw image data to 2d grayscale image
  size_t counter = 0;
  for (size_t r = 0; r &lt; n_rows; ++r) {
      for (size_t c = 0; c &lt; n_cols; ++c) {
          image_grayscale[r][c] = 0.21*raw_image_data[counter]
              + 0.72*raw_image_data[counter+1]
              + 0.07*raw_image_data[counter+2];
          counter += 3;
      }
  }
  </code></pre>
  
  <p>I want to write the resulting filtered image to another two-dimensional array, <code>gradient_magnitude</code>:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>uint32_t** gradient_magnitude;
  // Allocate memory
  gradient_magnitude = new uint32_t*[n_rows];
  for (size_t i = 0; i &lt; n_rows; ++i) {
      gradient_magnitude[i] = new uint32_t[n_cols];
  }
  
  // Filtering operation
  int32_t grad_h, grad_v;
  for (int r = 1; r &lt; n_rows-1; ++r) {
      for (int c = 1; c &lt; n_cols-1; ++c) {
          grad_h = image_grayscale[r][c+1] - image_grayscale[r][c-1];
          grad_v = image_grayscale[r+1][c] - image_grayscale[r-1][c];
          gradient_magnitude[r][c] = std::sqrt(pow(grad_h, 2) + pow(grad_v, 2));
      }
  }
  </code></pre>
  
  <p>Finally, I write the filtered image to a .ppm output.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>std::ofstream out;
  out.open("output.ppm", std::ios::out | std::ios::binary);
  
  // ppm header
  out &lt;&lt; "P6\n" &lt;&lt; n_rows &lt;&lt; " " &lt;&lt; n_cols &lt;&lt; "\n" &lt;&lt; "255\n";
  
  // Write data to file
  for (int r = 0; r &lt; n_rows; ++r) {
      for (int c = 0; c &lt; n_cols; ++c) {
          for (int i = 0; i &lt; 3; ++i) {
              out.write((char*) &amp;gradient_magnitude[r][c],1);
          }
      }
  }
  out.close();
  </code></pre>
  
  <p>The output image, however, is a mess.</p>
  
  <p>When I simply set <code>grad_v = 0;</code> in the loop (i.e. solely calculate the horizontal gradient), the output is seemingly correct:</p>
  
  <p><a href="https://i.stack.imgur.com/AyjaJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AyjaJ.jpg" alt="Horizontal Gradient"></a></p>
  
  <p>When I instead set <code>grad_h = 0;</code> (i.e. solely calculate the vertical gradient), the output is strange:</p>
  
  <p><a href="https://i.stack.imgur.com/AdU34.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AdU34.jpg" alt="Vertical Gradient"></a></p>
  
  <p>It seems like part of the image has been circularly shifted, but I cannot understand why. Moreover, I have tried with many images and the same issue occurs.</p>
  
  <p>Can anyone see any issues? Thanks so much!</p>