title: How to Swap pixels to mirror an image c++
body: <p>I have a greyscale image (150 x 200) and I need to mirror it about its vertical axis.</p>
  
  <p>The original image is loaded, then I call my function, then I save the new image under ParrotMirror.png.  However with my code the image has not mirrored, could anyone possibly explain what's incorrect about my code? The idea is that pixel element 0 is swapped with 149, and 1 with 148, and 2 with 147 etc...</p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  #include &lt;iostream&gt;
  #include "ImageHandle.h"
  
  using namespace std;
  
  int CountBlackPixels (unsigned char PixelGrid[WIDTH][HEIGHT]);
  
  void InvertImage (unsigned char PixelGrid[WIDTH][HEIGHT]);
  
  void ReducePixelLevel (unsigned char PixelGrid[WIDTH][HEIGHT]);
  
  void MirrorImage (unsigned char PixelGrid[WIDTH][HEIGHT]);
  
  
  int main(int argc, char *argv[])
  
  {
      QCoreApplication a(argc, argv);
  
      unsigned char PixelGrid[WIDTH][HEIGHT];     // Image loaded from file
  
      // If the file "Parrot.png" cannot be loaded ...
      if (!loadImage(PixelGrid, "Parrot.png"))
      {
          // Display an error message
          cout &lt;&lt; "Error loading file \"Parrot.png\"" &lt;&lt; endl;
      }
  
      MirrorImage(PixelGrid);
      {
          if (saveImage(PixelGrid, "ParrotMirror.png"))
          {
              cout &lt;&lt; "\nFile \"ParrotMirror.png\" saved successfully" &lt;&lt; endl;
          }
          else
          {
              cout &lt;&lt; "\nCould not save \"ParrotMirror.png\"" &lt;&lt; endl;
          }
  
      }
  
      return a.exec();
  }
  
  void MirrorImage (unsigned char PixelGrid[WIDTH][HEIGHT])
  {
  
      for (int row = 0; row &lt; WIDTH; ++row)
      {
          int swapRow = WIDTH - 1 - row; // Mirror pixel
  
          int col = 0;
  
          PixelGrid[row][col] = PixelGrid[swapRow][col];
  
      }
  }
  </code></pre>
  
  <p>I have only shown parts of my code relevant to the question, the reason the original image must be reloaded is because there are other functions modifying the image and saving too.</p>
  
  <p>EDIT: This is how my function now stands..</p>
  
  <pre><code>void MirrorImage (unsigned char PixelGrid[WIDTH][HEIGHT])
  {
  
      for (int row = 0; row &lt; WIDTH; row++)
      {
          for (int col = 0; col &lt; HEIGHT / 2; col++)
          {
              int swapRow = WIDTH - 1 - row; // Mirror pixel
  
              unsigned char temp = PixelGrid[row][col];
              PixelGrid[row][col] = PixelGrid[swapRow][col];
              temp = PixelGrid[swapRow][col];
  
          }
      }
  }
  </code></pre>
  
  <p>which mirrors the image about the centre, so left hand side is half of original image and right hand side half of the mirrored image.</p>