1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int IMAGE_WIDTH = 15;
const int IMAGE_HEIGHT = 10;
const int MAX_VALUE = 255;
const string FILE_NAME = "color_image.ppm";
struct ColorValues {
unsigned char red;
unsigned char green;
unsigned char blue;
};
void createImage(ColorValues array[][IMAGE_WIDTH], int height);
bool writeImage(ColorValues array[][IMAGE_WIDTH], int height, const string fileName );
int main() {
ColorValues imageArray[IMAGE_HEIGHT][IMAGE_WIDTH] = {};
createImage(imageArray, IMAGE_HEIGHT);
writeImage(imageArray, IMAGE_HEIGHT, FILE_NAME);
return 0;
}
//draws a rectangle in the array
void drawRect(ColorValues imageArray[][IMAGE_WIDTH], int imgHeight,
int rectTop, int rectLeft, int rectHeight, int rectWidth,
int redLevel, int greenLevel, int blueLevel){
ColorValues value;
value.red = redLevel;
value.green = greenLevel;
value.blue = blueLevel;
for(int rowIndex = rectTop; rowIndex < rectTop + rectHeight; rowIndex++)
for(int colIndex = rectLeft; colIndex < rectLeft + rectWidth; colIndex++){
imageArray[rowIndex][colIndex] = value;
}
}
//adds shapes to the image array
void createImage(ColorValues imageArray[][IMAGE_WIDTH], int height){
drawRect(imageArray, height, 2, 2, 5, 7, 240, 60, 100);
}
//outputs the array to a file
bool writeImage(ColorValues imageArray[][IMAGE_WIDTH], int height, const string fileName ){
ofstream outputFile;
outputFile.open(fileName);
if (! outputFile) {
cout << "File " << fileName << " could not be opened.\n";
cout << "program terminated.\n";
return false; }
outputFile << "P3 \n";
outputFile << IMAGE_WIDTH << " " << IMAGE_HEIGHT << "\n";
outputFile << MAX_VALUE << "\n";
for(int arrayRow = 0; arrayRow < IMAGE_HEIGHT; arrayRow++){
for(int arrayColumn = 0; arrayColumn < IMAGE_WIDTH; arrayColumn++){
outputFile << imageArray[arrayRow][arrayColumn] << " ";
}
outputFile << endl;
}
outputFile.close();
return true;
}
|