Hello,
First of all I am new to this forum and am grateful for its presence. I am stuck starting a new course as I am so close to graduating with my AA in Computer Networking.
Here is the assignment. I am NOT asking for the answer but I do need guidance on how to be able to complete it. I have spent numerous hours trying to complete it but I have been unsuccessful.
Here is the assignment guidlines. below you can see what I have been able to do so far.
The format of the PGM image file you’ll create is described here:
http://en.wikipedia.org/wiki/Netpbm_format#PGM_example
The image must:
be at least 300 pixels wide by 200 pixels tall (and NOT square)
consist of grayscale values from 0 to 255
contain at least two rectangles
contain at least two circles.
Your program’s main will declare the array, then call the following two functions (which you’ll write):
void createImage(unsigned char image[][WIDTH], int height );
bool writeImage(const unsigned char image[][WIDTH], int height, const string fileName );
The createImage() function will create all the image’s pixel values. It must make use of the following function (which you’ll write)
void drawrect(unsigned char image[][WIDTH], int imgHeight,
int rectTop, int rectLeft, int rectHeight, int rectWidth, unsigned char grayLevel);
And to have the possibility of getting an A:
void drawcircle(unsigned char image[][WIDTH], int height,
int centerX, int centerY, int radius, unsigned char grayLevel)
The writeImage() function will write the array data to the specified text file (along with the necessary header lines).
Here is my answer that I have been able to accomplish so far....
#include <iostream>
#include <fstream> //for file I/O
using namespace std;
const int WIDTH = 300;
const int HEIGHT = 200;
void initialize(unsigned char img[][WIDTH]) {
for (int row = 0; row < HEIGHT; row++)
for (int col = 0; col < WIDTH; col++)
img[row][col] = 255;
}
void drawRect(unsigned char image[][WIDTH], int imgHeight,
int rectTop, int rectLeft, int rectHeight, int rectWidth, unsigned char grayLevel)
{
}
void drawCircle(unsigned char image[][WIDTH], int height,
int centerX, int centerY, int radius, unsigned char grayLevel) {
}
void createImage(unsigned char image[][WIDTH], int height) {
}
bool writeImage(const unsigned char image[][WIDTH], int height, const string fileName) {
ofstream imgFile;
imgFile.open("picture.txt");
for (int row = 0; row < HEIGHT; row++) {
for (int col = 0; col < WIDTH; col++)
imgFile << static_cast<int>(img[row][col]) << ' ';
imgFile << '\n';
}
imgFile.close();
}
//=============================================================
int main()
{
unsigned char img[HEIGHT][WIDTH];
initialize(img);
drawRect(img);
writeImage(img);
return 0;
}
The troubles I am having is I do not know how to put in 2 rectangles, the grayscale values from 0 to 225, and if possible I would like to know how to make it contain 2 circles.
In advance I am grateful for any assistance you all may have with assisting me as I am getting familiar with C++. Thanks for having me as a member on this site. I am looking forward to learning a lot from you all.
Regards,
Nana