Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #inc

ID: 3825786 • Letter: #

Question

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>


class Color
{
public:
Color();
Color(unsigned char, unsigned char, unsigned char);

unsigned char r;
unsigned char g;
unsigned char b;
};
Color::Color()
{
r=0;
g=0;
b=0;

}

Color::Color(unsigned char r, unsigned char g, unsigned char b){
this->r=r;
this->g=g;
this->b=b;

}
const int SIZE=250;

class MyImage{

// Protected members
protected:
int a[SIZE][SIZE*3]; //source
   int m;   //size of matrix m X 3n
   int n;

public:
// Constructors
MyImage();
MyImage(int n, int m);

// Getter & Setter for field m
   int getM()const;
   void setM(int m);


// Getter & Setter for field n
   int getN()const;
   void setN(int n);

// Public methods
void savePicture(string fname);
   void createPicture();
void openPicture(string fname);
MyImage operator+(const MyImage& one)const;

};
MyImage::MyImage(int n, int m){
this->m=m;
this->n=n;

createPicture();
}

MyImage::MyImage(){
n=0;
m=0;

}


// Getter & Setter for field m
int MyImage::getM() const{
return m;
}
void MyImage::setM(int m) {
this->m=m;
}

// Getter & Setter for field n
int MyImage::getN() const{
return n;
}
void MyImage::setN(int n) {
this->n=n;
}

// Public methods
void MyImage::savePicture(string filename){

ofstream f(filename);

int max=255;
f<< "P3"<<endl;
f << n << " " << m << " "<<max<<" ";
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n*3; j++)
f << a[i][j]<< " ";
f << " ";
}

}
void MyImage::createPicture(){
Color black;
Color yellow(255,255,58);


//backcolor: yellow
for (int i = 0; i < m; i++){

for (int j = 0; j < n; j++){
a[i][j*3]=yellow.r;
a[i][j*3+1]=yellow.g;
a[i][j*3+2]=yellow.b;
}
}


// horizontal black lines for the grid
for (int j = 0; j < n*3; j++){
a[49][j]=0;
a[99][j]=0;
}

int j;
// vertical black lines for grid
for (int i = 0; i < m; i++){

j=49;
a[i][j*3]=black.r;
a[i][j*3+1]=black.g;
a[i][j*3+2]=black.b;

j=99;
a[i][j*3]=black.r;
a[i][j*3+1]=black.g;
a[i][j*3+2]=black.b;
}

}

void MyImage::openPicture(string fname){
//fname="xo.ppm";
ifstream in(fname);

if(!in.good()){
cerr<<"File is not exist!";
cin.get();
exit(0);
}
string type;

in>>type;
if(type !="P3"){
cerr<<"Error in input file!";
cin.get();
exit(0);
}


in >> n >> m;
int max;
in>>max;
if(n>SIZE || m>SIZE || max != 255){
cerr<<"Error in input file!";
cin.get();
exit(0);
}

for (int i = 0; i < m; i++)
for (int j = 0; j < n*3; j++)
in >> a[i][j];


}

MyImage MyImage::operator+(const MyImage& one)const{
MyImage result;
result.setN(max(one.n, n));
result.setM(max(one.m, m));
float alpha,beta;

do{
cout<<"Enter the merging parameters alpha and beta (alpha + beta must be 1).";
cout<<" alpha: ";
cin>>alpha;
cout<<" beta: ";
cin>>beta;
}while((alpha + beta-1) > 0.00001);

//fill in wrong color
for (int i = 0; i < result.m; i++)
for (int j = 0; j < result.n*3; j++)
result.a[i][j]=300;

// insert the first image
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
result.a[i][j*3]=a[i][j*3];
result.a[i][j*3+1]=a[i][j*3+1];
result.a[i][j*3+2]=a[i][j*3+2];
}
}

// insert the second image and merge it within the first image
for (int i = 0; i < result.m; i++){
for (int j = 0; j < result.n; j++){
if(result.a[i][j*3] != 300)//wrong color?
result.a[i][j*3]=alpha*result.a[i][j*3]+beta*one.a[i][j*3];
else
result.a[i][j*3]=one.a[i][j*3];

if(result.a[i][j*3+1] != 300)
result.a[i][j*3+1]=alpha*result.a[i][j*3+1]+beta*one.a[i][j*3+1];
else
result.a[i][j*3+1]=one.a[i][j*3+1];

if(result.a[i][j*3+2] != 300)
result.a[i][j*3+2]=alpha*result.a[i][j*3+2]+beta*one.a[i][j*3+2];
else
result.a[i][j*3+2]=one.a[i][j*3+2];
}
}

return result;
}
class GreyImage: public MyImage{

// Private members
float a1, a2, a3;


public:
// Constructors
GreyImage();

// Getter & Setter for field borderColor
Color getBorderColor()const;
void setBorderColor( int r, int g, int b);

// Getter & Setter for field a1
float getA1()const;
void setA1(float a1);

// Getter & Setter for field a2
float getA2()const;
void setA2(float a2);

// Getter & Setter for field a3
float getA3()const;
void setA3(float a3);

// overloaded function
void savePicture(string);
bool validate();

};
GreyImage::GreyImage():
MyImage()
{
a1=a2=0.33;
a3=0.34;
}

// Getter & Setter for field a1
float GreyImage::getA1() const{
return a1;
}
void GreyImage::setA1(float a1) {
this->a1=a1;
}

// Getter & Setter for field a1
float GreyImage::getA2() const{
return a2;
}
void GreyImage::setA2(float a2) {
this->a2=a2;
}


// Getter & Setter for field a1
float GreyImage::getA3() const{
return a3;
}
void GreyImage::setA3(float a3) {
this->a3=a3;
}

//save grey image
void GreyImage::savePicture(string filename){
ofstream f(filename);

int max=255;
f<< "P3"<<endl;
f << n<< " " << m << " "<<max<<" ";


//convert to black and white
for (int i = 0; i < m; i++){

for (int j = 0; j < n; j++){
{
float greyscaleValue = (a[i][j*3] * a1) + (a[i][j*3+1] * a2) + (a[i][j*3+2] * a3);
a[i][j*3]=greyscaleValue;
a[i][j*3+1]=greyscaleValue;
a[i][j*3+2]=greyscaleValue;
}
}

}

for (int i = 0; i < m; i++)
{
for (int j = 0; j < n*3; j++)
f << a[i][j]<< " ";
f << " ";
}

}

bool GreyImage::validate(){
return (fabs(a1+a2+a3-1) <0.00001);
}
class BorderImage: public MyImage, public Color{

// Private members

int b[SIZE][SIZE*3]; //result
Color borderColor;

//thickness
int h;


public:
// Constructors
BorderImage();

// Getter & Setter for field borderColor
Color getBorderColor()const;
void setBorderColor( unsigned char,unsigned char,unsigned char);

// Getter & Setter for field h
int getH()const;
void setH(int h);

// overloaded function
void savePicture(string);
bool validate();


};
BorderImage::BorderImage():
MyImage(),
Color()
{
h=0;
borderColor=Color(0,0,0);
}


// Getter & Setter for field m
Color BorderImage::getBorderColor() const{
return borderColor;
}
void BorderImage::setBorderColor(unsigned char r,unsigned char g,unsigned char b) {
borderColor=Color(r,g,b);
}

// Getter & Setter for field h
int BorderImage::getH() const{
return h;
}
void BorderImage::setH(int h) {
this->h=h;
}

// overloaded function
void BorderImage::savePicture(string filename){

ofstream f(filename);

int max=255;
f<< "P3"<<endl;
f << n+2*h<< " " << m+2*h << " "<<max<<" ";

//backcolor
for (int i = 0; i < m+2*h; i++){

for (int j = 0; j < (n+2*h); j++){
b[i][j*3]=borderColor.r;
b[i][j*3+1]=borderColor.g;
b[i][j*3+2]=borderColor.b;
}
}

// insert a to b
for (int i = h; i < m+h; i++)
for (int j = h; j < n+h; j++){
b[i][j*3]=a[i-h][(j-h)*3];
b[i][j*3+1]=a[i-h][(j-h)*3+1];
b[i][j*3+2]=a[i-h][(j-h)*3+2];
}


for (int i = 0; i < m+2*h; i++)
{
for (int j = 0; j < (n+2*h)*3; j++)
f << b[i][j]<< " ";
f << " ";
}

}
// overloaded function
bool BorderImage::validate(){ // not need because we use type unsigned char
return !((borderColor.r < 0 || borderColor.r > 255) ||
(borderColor.g < 0 || borderColor.g > 255) ||
(borderColor.b < 0 || borderColor.b > 255));

using namespace std;
/*
* A.CREATE AN MyImage/ USE ANY MyImage
* B.ADD A RED BOX AROUND THE MyImage
* C.THE USERS GETS TO CHOOSE THE THICKNESS OF THE RED BOX(PIXEL WIDTH)
* D. OUTPUT MyImage FILE MyImage
*/

using namespace std;

int main() {

//CREATE AN Image
MyImage img0(150,150);
img0.savePicture("xo.ppm");

/*
* #1 Convert a color image to black and white me of the image (in ppm format)
* Ask the user to enter the name of the Image (in ppm format)
* Ask the user to enter the black and white parameters (a1, a2, and a3)
* Validate to make sure parameters sum to one
* Output a ppm image of the resulting black and white image
*/

{
cout<<" Enter the name of the image (in ppm format): ";


GreyImage img;
string fname;
cin>>fname;

img.openPicture(fname);

img.MyImage::savePicture("source1.ppm");

do{
cout<<" Enter the black and white parameters a1, a2, a3 (a1+a2+a3 must be 1). a1: ";
float a1,a2,a3;
cin>>a1;
cout<<"a2: ";
cin>>a2;
cout<<"a3: ";
cin>>a3;
img.setA1(a1);
img.setA2(a2);
img.setA3(a3);
}while(!img.validate());


img.savePicture("grey.ppm");
}


/*#2 Merge two images
* Ask the user to enter names of two images
* Ask the user to enter the merging parameters (alpha and beta)
* Validate to make sure parameters sum to one
* Your Code should be able to handle two images of different sizes
* Output a ppm image of the resulting mergerges (in ppm format)
*/

{
MyImage imgA, imgB, img;
cout<<" Enter the name of two images (in ppm format).";
string fnameA,fnameB;
cout<<" name A: ";
cin>>fnameA;
imgA.openPicture(fnameA);

cout<<"name B: ";
cin>>fnameB;
imgB.openPicture(fnameB);
img=imgA+imgB;
imgA.savePicture("source2A.ppm");
imgB.savePicture("source2B.ppm");
img.savePicture("merge.ppm");

}


/*
* #3 Add a border around an MyImage
* Ask the user to enter the name of the Image (in ppm format)
* Ask the user to enter the pixel size of the board
* Ask the user to enter the color of the board in r, g, b
* Validate that r, g, & b are between 0 and 255 Output a ppm Image of the original Image with the specified boarder
*/


{
cout<<" Enter the name of the image (in ppm format): ";
string fname;
cin>>fname;

BorderImage img;
img.openPicture(fname);
img.MyImage::savePicture("source3.ppm"); //source3

cout<<" Enter the the pixel size of the board: ";
int h;
cin>>h;
img.setH(h);

cout<<" Enter the the color of the board in r, g, b. r: ";
int r,g,b;
cin>>r;
cout<<"g: ";
cin>>g;
cout<<"b: ";
cin>>b;
img.setBorderColor((unsigned char)r, (unsigned char)g, (unsigned char)b);


img.savePicture("border.ppm");
}


cout << "Done!" <<endl;
cout << "You can see result in the file *.png after convert from ppm to png." << endl;
//cout<<"http://www.online-convert.com/result/7570542a6f0da87f2ccc4bcc1dd51bb1"<<endl;

return 0;
}

***** getting 4 errors

hi so only have 4 errors now-
line 63 string not declared
line 65 string not declared
102 variable or field 'save picture' declared void
102 string not declared
62 not:'std::string"

Explanation / Answer

For string not declared and all we need to place using namespace std ; before the class decleration : I have made the changes have a look

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


class Color
{
public:
Color();
Color(unsigned char, unsigned char, unsigned char);

unsigned char r;
unsigned char g;
unsigned char b;
};
Color::Color()
{
r=0;
g=0;
b=0;

}

Color::Color(unsigned char r, unsigned char g, unsigned char b){
this->r=r;
this->g=g;
this->b=b;

}
const int SIZE=250;

class MyImage{

// Protected members
protected:
int a[SIZE][SIZE*3]; //source
   int m;   //size of matrix m X 3n
   int n;

public:
// Constructors
MyImage();
MyImage(int n, int m);

// Getter & Setter for field m
   int getM()const;
   void setM(int m);


// Getter & Setter for field n
   int getN()const;
   void setN(int n);

// Public methods
void savePicture(string fname);
   void createPicture();
void openPicture(string fname);
MyImage operator+(const MyImage& one)const;

};
MyImage::MyImage(int n, int m){
this->m=m;
this->n=n;

createPicture();
}

MyImage::MyImage(){
n=0;
m=0;

}


// Getter & Setter for field m
int MyImage::getM() const{
return m;
}
void MyImage::setM(int m) {
this->m=m;
}

// Getter & Setter for field n
int MyImage::getN() const{
return n;
}
void MyImage::setN(int n) {
this->n=n;
}

// Public methods
void MyImage::savePicture(string filename){

ofstream f(filename);

int max=255;
f<< "P3"<<endl;
f << n << " " << m << " "<<max<<" ";
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n*3; j++)
f << a[i][j]<< " ";
f << " ";
}

}
void MyImage::createPicture(){
Color black;
Color yellow(255,255,58);


//backcolor: yellow
for (int i = 0; i < m; i++){

for (int j = 0; j < n; j++){
a[i][j*3]=yellow.r;
a[i][j*3+1]=yellow.g;
a[i][j*3+2]=yellow.b;
}
}


// horizontal black lines for the grid
for (int j = 0; j < n*3; j++){
a[49][j]=0;
a[99][j]=0;
}

int j;
// vertical black lines for grid
for (int i = 0; i < m; i++){

j=49;
a[i][j*3]=black.r;
a[i][j*3+1]=black.g;
a[i][j*3+2]=black.b;

j=99;
a[i][j*3]=black.r;
a[i][j*3+1]=black.g;
a[i][j*3+2]=black.b;
}

}

void MyImage::openPicture(string fname){
//fname="xo.ppm";
ifstream in(fname);

if(!in.good()){
cerr<<"File is not exist!";
cin.get();
exit(0);
}
string type;

in>>type;
if(type !="P3"){
cerr<<"Error in input file!";
cin.get();
exit(0);
}


in >> n >> m;
int max;
in>>max;
if(n>SIZE || m>SIZE || max != 255){
cerr<<"Error in input file!";
cin.get();
exit(0);
}

for (int i = 0; i < m; i++)
for (int j = 0; j < n*3; j++)
in >> a[i][j];


}

MyImage MyImage::operator+(const MyImage& one)const{
MyImage result;
result.setN(max(one.n, n));
result.setM(max(one.m, m));
float alpha,beta;

do{
cout<<"Enter the merging parameters alpha and beta (alpha + beta must be 1).";
cout<<" alpha: ";
cin>>alpha;
cout<<" beta: ";
cin>>beta;
}while((alpha + beta-1) > 0.00001);

//fill in wrong color
for (int i = 0; i < result.m; i++)
for (int j = 0; j < result.n*3; j++)
result.a[i][j]=300;

// insert the first image
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
result.a[i][j*3]=a[i][j*3];
result.a[i][j*3+1]=a[i][j*3+1];
result.a[i][j*3+2]=a[i][j*3+2];
}
}

// insert the second image and merge it within the first image
for (int i = 0; i < result.m; i++){
for (int j = 0; j < result.n; j++){
if(result.a[i][j*3] != 300)//wrong color?
result.a[i][j*3]=alpha*result.a[i][j*3]+beta*one.a[i][j*3];
else
result.a[i][j*3]=one.a[i][j*3];

if(result.a[i][j*3+1] != 300)
result.a[i][j*3+1]=alpha*result.a[i][j*3+1]+beta*one.a[i][j*3+1];
else
result.a[i][j*3+1]=one.a[i][j*3+1];

if(result.a[i][j*3+2] != 300)
result.a[i][j*3+2]=alpha*result.a[i][j*3+2]+beta*one.a[i][j*3+2];
else
result.a[i][j*3+2]=one.a[i][j*3+2];
}
}

return result;
}
class GreyImage: public MyImage{

// Private members
float a1, a2, a3;


public:
// Constructors
GreyImage();

// Getter & Setter for field borderColor
Color getBorderColor()const;
void setBorderColor( int r, int g, int b);

// Getter & Setter for field a1
float getA1()const;
void setA1(float a1);

// Getter & Setter for field a2
float getA2()const;
void setA2(float a2);

// Getter & Setter for field a3
float getA3()const;
void setA3(float a3);

// overloaded function
void savePicture(string);
bool validate();

};
GreyImage::GreyImage():
MyImage()
{
a1=a2=0.33;
a3=0.34;
}

// Getter & Setter for field a1
float GreyImage::getA1() const{
return a1;
}
void GreyImage::setA1(float a1) {
this->a1=a1;
}

// Getter & Setter for field a1
float GreyImage::getA2() const{
return a2;
}
void GreyImage::setA2(float a2) {
this->a2=a2;
}


// Getter & Setter for field a1
float GreyImage::getA3() const{
return a3;
}
void GreyImage::setA3(float a3) {
this->a3=a3;
}

//save grey image
void GreyImage::savePicture(string filename){
ofstream f(filename);

int max=255;
f<< "P3"<<endl;
f << n<< " " << m << " "<<max<<" ";


//convert to black and white
for (int i = 0; i < m; i++){

for (int j = 0; j < n; j++){
{
float greyscaleValue = (a[i][j*3] * a1) + (a[i][j*3+1] * a2) + (a[i][j*3+2] * a3);
a[i][j*3]=greyscaleValue;
a[i][j*3+1]=greyscaleValue;
a[i][j*3+2]=greyscaleValue;
}
}

}

for (int i = 0; i < m; i++)
{
for (int j = 0; j < n*3; j++)
f << a[i][j]<< " ";
f << " ";
}

}

bool GreyImage::validate(){
return (fabs(a1+a2+a3-1) <0.00001);
}
class BorderImage: public MyImage, public Color{

// Private members

int b[SIZE][SIZE*3]; //result
Color borderColor;

//thickness
int h;


public:
// Constructors
BorderImage();

// Getter & Setter for field borderColor
Color getBorderColor()const;
void setBorderColor( unsigned char,unsigned char,unsigned char);

// Getter & Setter for field h
int getH()const;
void setH(int h);

// overloaded function
void savePicture(string);
bool validate();


};
BorderImage::BorderImage():
MyImage(),
Color()
{
h=0;
borderColor=Color(0,0,0);
}


// Getter & Setter for field m
Color BorderImage::getBorderColor() const{
return borderColor;
}
void BorderImage::setBorderColor(unsigned char r,unsigned char g,unsigned char b) {
borderColor=Color(r,g,b);
}

// Getter & Setter for field h
int BorderImage::getH() const{
return h;
}
void BorderImage::setH(int h) {
this->h=h;
}

// overloaded function
void BorderImage::savePicture(string filename){

ofstream f(filename);

int max=255;
f<< "P3"<<endl;
f << n+2*h<< " " << m+2*h << " "<<max<<" ";

//backcolor
for (int i = 0; i < m+2*h; i++){

for (int j = 0; j < (n+2*h); j++){
b[i][j*3]=borderColor.r;
b[i][j*3+1]=borderColor.g;
b[i][j*3+2]=borderColor.b;
}
}

// insert a to b
for (int i = h; i < m+h; i++)
for (int j = h; j < n+h; j++){
b[i][j*3]=a[i-h][(j-h)*3];
b[i][j*3+1]=a[i-h][(j-h)*3+1];
b[i][j*3+2]=a[i-h][(j-h)*3+2];
}


for (int i = 0; i < m+2*h; i++)
{
for (int j = 0; j < (n+2*h)*3; j++)
f << b[i][j]<< " ";
f << " ";
}

}
// overloaded function
bool BorderImage::validate(){ // not need because we use type unsigned char
return !((borderColor.r < 0 || borderColor.r > 255) ||
(borderColor.g < 0 || borderColor.g > 255) ||
(borderColor.b < 0 || borderColor.b > 255));

using namespace std;
/*
* A.CREATE AN MyImage/ USE ANY MyImage
* B.ADD A RED BOX AROUND THE MyImage
* C.THE USERS GETS TO CHOOSE THE THICKNESS OF THE RED BOX(PIXEL WIDTH)
* D. OUTPUT MyImage FILE MyImage
*/

using namespace std;

int main() {

//CREATE AN Image
MyImage img0(150,150);
img0.savePicture("xo.ppm");

/*
* #1 Convert a color image to black and white me of the image (in ppm format)
* Ask the user to enter the name of the Image (in ppm format)
* Ask the user to enter the black and white parameters (a1, a2, and a3)
* Validate to make sure parameters sum to one
* Output a ppm image of the resulting black and white image
*/

{
cout<<" Enter the name of the image (in ppm format): ";


GreyImage img;
string fname;
cin>>fname;

img.openPicture(fname);

img.MyImage::savePicture("source1.ppm");

do{
cout<<" Enter the black and white parameters a1, a2, a3 (a1+a2+a3 must be 1). a1: ";
float a1,a2,a3;
cin>>a1;
cout<<"a2: ";
cin>>a2;
cout<<"a3: ";
cin>>a3;
img.setA1(a1);
img.setA2(a2);
img.setA3(a3);
}while(!img.validate());


img.savePicture("grey.ppm");
}


/*#2 Merge two images
* Ask the user to enter names of two images
* Ask the user to enter the merging parameters (alpha and beta)
* Validate to make sure parameters sum to one
* Your Code should be able to handle two images of different sizes
* Output a ppm image of the resulting mergerges (in ppm format)
*/

{
MyImage imgA, imgB, img;
cout<<" Enter the name of two images (in ppm format).";
string fnameA,fnameB;
cout<<" name A: ";
cin>>fnameA;
imgA.openPicture(fnameA);

cout<<"name B: ";
cin>>fnameB;
imgB.openPicture(fnameB);
img=imgA+imgB;
imgA.savePicture("source2A.ppm");
imgB.savePicture("source2B.ppm");
img.savePicture("merge.ppm");

}


/*
* #3 Add a border around an MyImage
* Ask the user to enter the name of the Image (in ppm format)
* Ask the user to enter the pixel size of the board
* Ask the user to enter the color of the board in r, g, b
* Validate that r, g, & b are between 0 and 255 Output a ppm Image of the original Image with the specified boarder
*/


{
cout<<" Enter the name of the image (in ppm format): ";
string fname;
cin>>fname;

BorderImage img;
img.openPicture(fname);
img.MyImage::savePicture("source3.ppm"); //source3

cout<<" Enter the the pixel size of the board: ";
int h;
cin>>h;
img.setH(h);

cout<<" Enter the the color of the board in r, g, b. r: ";
int r,g,b;
cin>>r;
cout<<"g: ";
cin>>g;
cout<<"b: ";
cin>>b;
img.setBorderColor((unsigned char)r, (unsigned char)g, (unsigned char)b);


img.savePicture("border.ppm");
}


cout << "Done!" <<endl;
cout << "You can see result in the file *.png after convert from ppm to png." << endl;
//cout<<"http://www.online-convert.com/result/7570542a6f0da87f2ccc4bcc1dd51bb1"<<endl;

return 0;
}
====
Now try, let me know if there is any concern.