Task E. Scale 200% Program scale.cpp. Scale the original picture to 200% of its
ID: 3710270 • Letter: T
Question
Task E. Scale 200%
Program scale.cpp. Scale the original picture to 200% of its size. It can be done by increasing the size of the picture by the factor of 2, and copying each pixel of the input as a small 2x2 square in the output. (We don’t do any interpolation of colors as more advanced scaling procedures would do.)
I`m adding the code and you just need to do something in the main function, I tried but looks like it`s not working...If you could comment what you did to make it work....
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <fstream>
using namespace std;
const int MAX_H = 512;
const int MAX_W = 512;
// Reads a PGM file.
// Notice that: height and width are passed by reference!
void readImage(int image[MAX_H][MAX_W], int &height, int &width) {
char c;
int x;
ifstream instr;
instr.open("inImage.pgm");
// read the header P2
instr >> c;
assert(c == 'P');
instr >> c;
assert(c == '2');
// skip the comments (if any)
while ((instr>>ws).peek() == '#') {
instr.ignore(4096, ' ');
}
instr >> width;
instr >> height;
assert(width <= MAX_W);
assert(height <= MAX_H);
int max;
instr >> max;
assert(max == 255);
for (int row = 0; row < height; row++)
for (int col = 0; col < width; col++)
instr >> image[row][col];
instr.close();
return;
}
// Writes a PGM file
// Need to provide the array data and the image dimensions
void writeImage(int image[MAX_H][MAX_W], int height, int width) {
ofstream ostr;
ostr.open("outImage.pgm");
if (ostr.fail()) {
cout << "Unable to write file ";
exit(1);
};
// print the header
ostr << "P2" << endl;
// width, height
ostr << width << ' ';
ostr << height << endl;
ostr << 255 << endl;
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
assert(image[row][col] < 256);
assert(image[row][col] >= 0);
ostr << image[row][col] << ' ';
ostr << endl;
}
}
ostr.close();
return;
}
int main() {
int img[MAX_H][MAX_W];
int h, w;
int avg = 0;
readImage(img, h, w); // read it from the file "inImage.pgm"
// h and w were passed by reference and
// now contain the dimensions of the picture
// and the 2-dimesional array img contains the image data
// Now we can manipulate the image the way we like
// for example we copy its contents into a new array
int out[MAX_H][MAX_W];
for(int row = 0; row < h; row++) {
for(int col = 0; col < w; col ++) {
avg = (out[row][col] + out[row][col + 1] + out[row + 1][col] + out[row + 1][col + 1])/4; // find ave
out[row][col] = out[row][col + 1] = out[row + 1][col] = out[row + 1][col + 1] = avg;
col += 2; // skip a col
row += 2; // skip a row
}
}
// and save this new image to file "outImage.pgm"
writeImage(out, h, w);
}
ExampleExplanation / Answer
#include #include #include #include using namespace std; const int MAX_H = 512; const int MAX_W = 512; // Reads a PGM file. // Notice that: height and width are passed by reference! void readImage(int image[MAX_H][MAX_W], int& height, int& width) { char c; int x; ifstream instr; instr.open("inImage.pgm"); // read the header P2 instr >> c; assert(c == 'P'); instr >> c; assert(c == '2'); // skip the comments (if any) while ((instr >> ws).peek() == '#') { instr.ignore(4096, ' '); } instr >> width; instr >> height; assert(width max; assert(max == 255); for (int row = 0; row image[row][col]; instr.close(); return; } // Writes a PGM file // Need to provide the array data and the image dimensions void writeImage(int image[MAX_H][MAX_W], int height, int width) { ofstream ostr; ostr.open("outImage.pgm"); if (ostr.fail()) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.