My university project, a grayscale image editor in C++ that loads, filters and transforms PGM images.
Introduction
For a university course I wrote a grayscale image editor in C++ that handles the basic image processing tasks. It was mostly practice in file handling, image manipulation, and efficient data structures in C++.
Features
The application supports:
- Loading and saving grayscale images in PGM format.
- Applying filters like mean and median filtering.
- Performing transformations such as flipping, rotating, and resizing.
- Combining images either side-by-side or top-to-bottom.
- Adjusting brightness and generating negative images.
Core Implementation
The editor is built around the grayImage struct, which stores pixel data and has a function for each image operation. Here’s how pixels are set and read:
unsigned short setPixel(unsigned short value, int r, int c) {
if (r >= Rows || c >= Cols || r < 0 || c < 0) {
return -1;
}
Image[r][c] = value;
return value;
}
int getPixel(int r, int c) {
if (r >= Rows || c >= Cols || r < 0 || c < 0) {
return -1;
}
return Image[r][c];
}Loading and Saving Images
The editor reads and writes images in PGM format. The load() and Save() functions handle file I/O:
int load(string File_Name) {
ifstream Input(File_Name.c_str());
if (!Input) {
return 1;
}
string MagicNumber, comment;
int columns, rows, MaxValue, currentValue;
getline(Input, MagicNumber);
getline(Input, comment);
Input >> columns >> rows >> MaxValue;
setRows(rows);
setCols(columns);
Maximum = MaxValue;
for (int i = 0; i < Rows; i++) {
for (int j = 0; j < Cols; j++) {
Input >> currentValue;
Image[i][j] = currentValue;
}
}
Input.close();
Loaded = true;
return 0;
}Applying a Negative Filter
A negative is one of the simplest transformations: subtract each pixel from the maximum value.
void Negative(grayImage& Result) {
for (int row = 0; row < Rows; row++) {
for (int column = 0; column < Cols; column++) {
Result.Image[row][column] = Maximum - Image[row][column];
}
}
Result.Rows = Rows;
Result.Cols = Cols;
Result.Maximum = Maximum;
}Future Improvements
Things I would add next:
- Adding support for colored images (PPM format).
- Implementing more advanced filters (e.g., Gaussian blur, edge detection).
- Providing a GUI using a library like Qt or OpenCV.
Looking back
Writing it taught me a lot about image data, file I/O, and algorithm optimization, and it was my first step toward more advanced image processing.
The full source code is on GitHub.
Feedback or suggestions are welcome through the contact page.
End of case study.