I have a greyscale image (150 x 200) and I need to mirror it about its vertical axis.
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...
#include <QCoreApplication>
#include <iostream>
#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 << "Error loading file \"Parrot.png\"" << endl;
}
MirrorImage(PixelGrid);
{
if (saveImage(PixelGrid, "ParrotMirror.png"))
{
cout << "\nFile \"ParrotMirror.png\" saved successfully" << endl;
}
else
{
cout << "\nCould not save \"ParrotMirror.png\"" << endl;
}
}
return a.exec();
}
void MirrorImage (unsigned char PixelGrid[WIDTH][HEIGHT])
{
for (int row = 0; row < WIDTH; ++row)
{
int swapRow = WIDTH - 1 - row; // Mirror pixel
int col = 0;
PixelGrid[row][col] = PixelGrid[swapRow][col];
}
}
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.
Aucun commentaire:
Enregistrer un commentaire