Question: Practice using object - oriented programming in C + + by creating an Image class. The lab simulates basic image processing techniques used in data

Practice using object-oriented programming in C++ by creating an Image class. The lab simulates basic image processing techniques used in data augmentation, such as flipping, rotating, cropping, resizing, and inverting binary images.
Please finish the code below:
// image_class.cpp
#include "image_class.h"
#include
using namespace std;
void Image::setPixel(int row, int col, int value){
if (row >=0 && row < height && col >=0 && col < width){
pixels[row][col]= value;
} else {
cerr << "Error: Pixel coordinates out of bounds." << endl;
}
}
// TASK1: implement constructor and destructor to initialize and clean up image data
Image::Image(int w, int h) : width(w), height(h){
if (w > MAX_WIDTH || h > MAX_HEIGHT){
cerr << "Error: Image dimensions exceed maximum allowed size." << endl;
exit(1);
}
// Initialize pixel data to 0(white)
// write your code here
}
Image::~Image(){
// Destructor
// write your code here
}
// Example1: implement flip method to flip image horizontally or vertically
void Image::flip(bool horizontal){
if (horizontal){
for (int i =0; i < height; ++i){
for (int j =0; j < width /2; ++j){
int temp = pixels[i][j];
pixels[i][j]= pixels[i][width -1- j];
pixels[i][width -1- j]= temp;
}
}
} else {
for (int i =0; i < height /2; ++i){
for (int j =0; j < width; ++j){
int temp = pixels[i][j];
pixels[i][j]= pixels[height -1- i][j];
pixels[height -1- i][j]= temp;
}
}
}
}
// Example2: implement rotate method to rotate image by 90,180, or 270 degrees in clockwise direction
void Image::rotate(int angle){
if (angle !=90 && angle !=180 && angle !=270){
cerr << "Error: Only 90,180,270 degrees are supported." << endl;
return;
}
int rotated[MAX_HEIGHT][MAX_WIDTH];
if (angle ==90){
for (int i =0; i < height; ++i){
for (int j =0; j < width; ++j){
rotated[j][height -1- i]= pixels[i][j];
}
}
int temp = width;
width = height;
height = temp;
} else if (angle ==180){
for (int i =0; i < height; ++i){
for (int j =0; j < width; ++j){
rotated[height -1- i][width -1- j]= pixels[i][j];
}
}
} else if (angle ==270){
for (int i =0; i < height; ++i){
for (int j =0; j < width; ++j){
rotated[width -1- j][i]= pixels[i][j];
}
}
int temp = width;
width = height;
height = temp;
}
// Copy rotated data back to pixels
for (int i =0; i < height; ++i){
for (int j =0; j < width; ++j){
pixels[i][j]= rotated[i][j];
}
}
}
// TASK2: implement crop method to crop image to a specific area
void Image::crop(int x, int y, int newWidth, int newHeight){
if (x + newWidth > width || y + newHeight > height){
cerr << "Error: Crop dimensions are out of bounds." << endl;
return;
}
// write your code here
}
// TASK3: implement invert method to invert image (0 becomes 1,1 becomes 0)
void Image::invert(){
// write your code here
}
// TASK4: implement resize method to resize image by an integer scale factor
void Image::resize(int scale){
if (scale <=0){
cerr << "Error: Scale factor must be a positive integer." << endl;
return;
}
// write your code here
}
// Getter for width
int Image::getWidth() const {
return width;
}
// Getter for height
int Image::getHeight() const {
return height;
}
// display image data as ASCII art for testing purposes
void Image::display() const {
for (int i =0; i < height; ++i){
for (int j =0; j < width; ++j){
cout <<(pixels[i][j]==1?'*' : '')<<'';
}
cout << endl;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!