Program scale.cpp. Scale the original picture to 200% of its size. It can be don
ID: 3711039 • Letter: P
Question
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.) For Example:
This is my code, and it's output.
#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, temp;
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*2; row++) {
for(int col = 0; col < w*2; col++) {
temp = img[row/2][col/2];
out[row][col] = temp;
out[row+1][col] = temp;
out[row][col+1] = temp;
out[row+1][col+1] = temp;
col += 1; // to skip a col
}
row += 1;
}
// and save this new image to file "outImage.pgm"
writeImage(out, h, w);
}
For some reason, the arrays reach their limit and stop the program. There's no reason to change any of the functions, my problem is clearly the For loop. I've tried changing the height (h) and width (w) in the for loop to (h*2) and (w*2) but I end up getting a segmentation fault (core dumped) error. Thanks in advance!
Explanation / Answer
#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, temp;
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[2*h][2*w];
int out_row = 0, out_col=0;
for(int row=0;row<h;row++)
{
out_col = 0;
for(int col=0;col<w;col++)
{
temp = img[row][col];
out[out_row][out_col] = temp;
out[out_row+1][out_col] = temp;
out[out_row][out_col+1] = temp;
out[out_row+1][out_col+1] = temp;
out_col = out_col+2;
}
out_row = out_row + 2;
}
// and save this new image to file "outImage.pgm"
writeImage(out, 2*h, 2*w); // since the height and width of out is twice that of img
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.