Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE READ THE TASK AND NOTE SECTION CAREFULLY BEFORE YOU START. I NEED THE FULL TASK. IF YOU ARE NOT SURE WHAT TO DO, PLEASE

PLEASE READ THE TASK AND NOTE SECTION CAREFULLY BEFORE YOU START. I NEED THE FULL TASK. IF YOU ARE NOT SURE WHAT TO DO, PLEASE SKIP MY QUESTION. I WILL GIVE NEGATIVE RATING FOR WRONG OR INCOMPLETE ANSWER. THE LINK FOR IMAGE AND A SAMPLE CODE WHERE YOU CAN FIND THE FUNCTIONS ARE PROVIDED BELOW.

THANK YOU.

Task:

First, we need a gray-scale image to work on. Save the one supplied by your instructor to $PWD. This file format has 8-bit images - i.e., each pixel is a grayscale value between 0 and 255 inclusive.

Since all our tasks will involve reading an image file, processing the image, and writing the processed image back to a different file, we will first get the basics setup. Write a small program that uses the given readimage/writeimage functions to input the PPM file into an array, copies the image to a 2nd array, and writes the 2nd array back. View the resulting image to make sure its the same as the original image.

NOTE:

Following functions are given in the sample code. You may use them as they are.

writeImage: writes the argument image (a 2-D array) to outImage.pgm in PGM grayscale format.

readImage: reads a PGM grayscale format from inImage.pgm into an array, and updates the height/width arguments appropriately (since they are passed by reference).

IMAGE LINK:

https://ufile.io/md6ka

// SAMPLE CODE

#include

#include

#include

#include

#include

#include

using namespace std;

const int MAXWIDTH = 512;

const int MAXHEIGHT = 512;

// reads a PPM file.

// Notice that: width and height are passed by reference!

void readImage(int image[][MAXHEIGHT], int &width, int &height,string name) {

char c;

int x;

ifstream instr;

instr.open((name+".pgm").c_str());

// 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 <= MAXWIDTH);

assert(height <= MAXHEIGHT);

int max;

instr >> max;

assert(max == 255);

for (int row = 0; row < height; row++)

for (int col = 0; col < width; col++)

instr >> image[col][row];

instr.close();

return;

}

void writeImage(int image[][MAXHEIGHT], int width, int height,string name) {

ofstream ostr;

ostr.open((name+".pgm").c_str());

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[col][row] < 256);

assert(image[col][row] >= 0);

ostr << image[col][row] << ' ';

// lines should be no longer than 70 characters

if ((col+1)%16 == 0) ostr << endl;

}

ostr << endl;

}

ostr.close();

return;

}

int main() {

int im1[MAXWIDTH][MAXHEIGHT];

int w1,h1;

readImage(im1,w1,h1,"inImage");

writeImage(im1,w1,h1,"outImage");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions