#include <string> #include <iostream> #include <fstream> using namespace std; cl
ID: 2246708 • Letter: #
Question
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class RGB {
public:
RGB(unsigned char red = 0, unsigned char green = 0, unsigned char blue = 0)
: r(red), g(green), b(blue) {}
unsigned char red() {
return r;
}
unsigned char green() {
return g;
}
unsigned char blue() {
return b;
}
private:
unsigned char r, g, b;
};
class PPMImage {
public:
PPMImage(unsigned int width, unsigned int height): w(width), h(height) {
pixmap = new RGB*[h];
for (unsigned int i = 0; i < w; ++i)
pixmap[i] = new RGB[w]; *** please explain
}
~PPMImage() {
for (unsigned int i = 0; i < w; i++)
delete[] pixmap[i]; **** please explain
delete[] pixmap;
w = h = 0;
}
void put(unsigned int y, unsigned int x, RGB rgb) {
if (x > w || y > h)
return;
pixmap[y][x] = rgb;
}
bool printToFile(string filename) {
ofstream out(filename.c_str());
if (!out)
return false;
out << "P3" << endl;
out << w << " " << h << endl;
out << 255 << endl; **** please explain loops etc
for (unsigned int i = 0; i < h; ++i) {
for (unsigned int j = 0; j < w; ++j) {
RGB &rgb = pixmap[i][j];
out << (int)rgb.red() << " " << (int)rgb.green() << " " << (int)rgb.blue() << endl;
}
}
return true;
}
private:
unsigned int w, h;
RGB **pixmap;
};
int main() {
int w = 100, h = 100;
PPMImage img(w, h); ***** please explain
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
RGB rgb;
if (i / 10 % 2 == j / 10 % 2)
rgb = RGB(255, 0, 0);
else rgb = RGB(0, 0, 255);
img.put(i, j, rgb);
}
}
string filename = "img.ppm";
if (img.printToFile(filename))
cout << "Succesfully create image!" << endl;
else cout << "Couldn't write to file '" << filename << "'..." << endl;
return 0;
}*************************** Please explain program - no documentation - ppm image 100 x100 checker board 10x10 blocks thanks C++
Explanation / Answer
#include <iostream>
using namespace std;
All the explantion are Highlighted in Bold, Thanks, let me know if you need more explantion .
If the things are not clear please let me know
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
//RBG is a class
class RGB {
public:
//constructor thet initialises red, grren , blue to 0 if no value is passed
//default values are 0
RGB(unsigned char red = 0, unsigned char green = 0, unsigned char blue = 0)
: r(red), g(green), b(blue) {}
//public methods that returns red
unsigned char red() {
return r;
}
//public methods that returns green
unsigned char green() {
return g;
}
//public methods that returns blue
unsigned char blue() {
return b;
}
private:
//r,g, b are variables of type unsigned character
unsigned char r, g, b;
};
//Class PPMImage
class PPMImage {
public:
//Constructor that initalises pixmap which hold address of RGB objects initialised by new
PPMImage(unsigned int width, unsigned int height): w(width), h(height) {
pixmap = new RGB*[h]; //We created a RGB object of size h
//Loop will run from 0 to w-1 and in this way
//it creates a 2D array where each row is of size w and column is of size h
for (unsigned int i = 0; i < w; ++i)
pixmap[i] = new RGB[w]; //Created a pixmap[w][h] its size is wxh
}
//Its a destructor that is used to destroy objects created by constructor
~PPMImage() {
//The way we created objects , Similary we need to destroy them
//Loop from 0 to w-1 and keep on deleting using delete opearor
//delete[] pixmap[i] will delete ith position of size w
for (unsigned int i = 0; i < w; i++)
delete[] pixmap[i];
delete[] pixmap;
w = h = 0;
}
void put(unsigned int y, unsigned int x, RGB rgb) {
if (x > w || y > h)
return;
pixmap[y][x] = rgb;
}
bool printToFile(string filename) {
//ofstream is a utilyty in C++ that is used to output data in a file
ofstream out(filename.c_str());
if (!out)
return false;
//<< operator is used to append data in file if we use outstream
//P3 is appended to file
out << "P3" << endl;
//w and then h is appended to file
out << w << " " << h << endl;
//255 is appended to file
out << 255 << endl;
//We run the nested loop , Outer loop runs from 0 to h-1
//Inner loop runs from 0 to w-1
//pixmap[i][j] is assigned to variable of class Type and then corresponding functions
//of RGB class are called
for (unsigned int i = 0; i < h; ++i) {
for (unsigned int j = 0; j < w; ++j) {
RGB &rgb = pixmap[i][j];
//R,G AND B ARE appended to file in integer form
out << (int)rgb.red() << " " << (int)rgb.green() << " " << (int)rgb.blue() << endl;
}
}
return true;
}
private:
unsigned int w, h;
RGB **pixmap;
};
int main() {
//w and h are initialised to 100
int w = 100, h = 100;
//img is an object and when we do PPMImage img(w, h), Corresponding constructor of
//PPMImage is called.
PPMImage img(w, h);
//We run the nested loop , Outer loop runs from 0 to h-1
//Inner loop runs from 0 to w-1
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
//Object of rbg is created and default constructor is called and r,g,b as 0
RGB rgb;
//Based on i and j manipulation
if (i / 10 % 2 == j / 10 % 2)
// rgb is instantiated as to 255 as red, 0 as blue and green
rgb = RGB(255, 0, 0);
else
//otherwise rgb is instantiated as to 0 as red, 0 as green and 255 blue
rgb = RGB(0, 0, 255);
img.put(i, j, rgb);
}
}
string filename = "img.ppm";
if (img.printToFile(filename))
cout << "Succesfully create image!" << endl;
else cout << "Couldn't write to file '" << filename << "'..." << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.