C++ ppm Hi, I am trying to create a program that will run like this. with output
ID: 3749696 • Letter: C
Question
C++ ppm
Hi, I am trying to create a program that will run like this.
with output file showing
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
typedef struct ppm {
string type;
int width;
int height;
int max;
int im[100][100][3];
}PPM;
void readImage(PPM &image, ifstream &ifs) {
ifs >> image.type;
if (image.type == "P1" || image.type == "P2" || image.type == "P3") {
ifs >> image.width >> image.height;
if (image.width > 1000 || image.height > 1000) {
cout << " Image Hieght or Width is out of Bound";
exit(-1);
}
ifs >> image.max;
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
ifs >> image.im[i][j][0] >> image.im[i][j][1] >> image.im[i][j][2];
}
}
}
else {
cout << " Image Type is not Valid";
exit(-1);
}
}
void convertGrayScale(PPM &image) {
if (image.type == "P2") {
return;
}
image.type = "P2";
int tot = 0;
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
tot = image.im[i][j][0] + image.im[i][j][1] + image.im[i][j][2];
image.im[i][j][0] = tot / 3;
image.im[i][j][1] = tot / 3;
image.im[i][j][2] = tot / 3;
}
}
}
void invertRed(PPM &image) {
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
image.im[i][j][0] = image.max - image.im[i][j][0];
}
}
}
void invertBlue(PPM &image) {
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
image.im[i][j][1] = image.max - image.im[i][j][1];
}
}
}
void invertGreen(PPM &image) {
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
image.im[i][j][2] = image.max - image.im[i][j][2];
}
}
}
void invertAll(PPM &image) {
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
image.im[i][j][0] = image.max - image.im[i][j][0];
image.im[i][j][1] = image.max - image.im[i][j][1];
image.im[i][j][2] = image.max - image.im[i][j][2];
}
}
}
void writeImage(PPM &image, ofstream &ofs) {
ofs << image.type << endl;
ofs << image.width << " " << image.height << endl;
ofs << image.max << endl;
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
ofs << image.im[i][j][0] << " " << image.im[i][j][1] << " " << image.im[i][j][2] << " ";
}
ofs << endl;
}
}
void menu() {
cout << " 1- Convert to GrayScale";
cout << " 2- Invert Red";
cout << " 3- Invert Green";
cout << " 4- Invert Blue";
cout << " 5- Invert All";
}
int main() {
PPM image;
string ofile;
string ifile;
ifstream ifs;
ofstream ofs;
cout << " Enter image file name (ppm): ";
cin >> ifile;
ifs.open(ifile.c_str());
if (!ifs.is_open()) {
cout << " In File Opening error ";
return -1;
}
cout << " Enter output image file name: ";
cin >> ofile;
ofs.open(ofile.c_str());
if (!ofs.is_open()) {
cout << " Out File Opening error ";
return -1;
}
readImage(image, ifs);
ifs.close();
int choice;
menu();
cin >> choice;
switch (choice) {
case 1:convertGrayScale(image); break;
case 2:invertRed(image); break;
case 3:invertBlue(image); break;
case 4:invertGreen(image); break;
case 5:invertAll(image); break;
}
writeImage(image, ofs);
ofs.close();
}
Please help me add these new functions in.
Adding noise:
Remove a color
Scaling the image
Changing contrast
.7 Select C:UsersiChadDesktopC++ progNew folderNmageModder-SampleProgram.exe Portable Pixmap (PPM) Image Modder Enter image file name (ppm):cake.ppm Enter output file name(ppm):cake Magic: P3 Width, Height: 720, 540 Number of Pixels: 388800 Max Color: 255 Image Processing Choices 1. Convert to grayscale 2. Invert Red 3. Invert Green 4. Invert Blue 5. Invert All Enter Choice: 1Explanation / Answer
You can follow the below template for changing contrast of an image.
float fvalue=(259.0*(contrast+255.0))/(255.0*(259.0-contrast));
RGB_color=GetPixelColor(a,b);
size_t newRed=truncate((size_t)(fvalue*(color.red-128)+128));
size_t newGreen=truncate((size_t)(fvalue*(color.green-128)+128));
size_t newBlue=truncate((size_t)(fvalue*(color.blue-128)+128));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.