In this project you develop an image editor to perform simple operations on inpu
ID: 3598766 • Letter: I
Question
In this project you develop an image editor to perform simple operations on input images, with the following learning outcomes:
• Read/Write PPM images • Images will be held in a C++ class, and image data will be allocated based on the input image dimensions.
• Image operations will take an input image and produce an output image in PPM format.
• Use of constructors and destructors. In part 1, you will develop the basic Image class and its methods; in part 2 you will perform image processing operations using the class. Image Representation Your image class should hold the following attributes and implement the associated methods (may include additional attributes/methods as needed).
In this part of the project, you will implement and test the Image class detailed above. You will continue to use the pointer based image representation from the earlier project. Images will continue use the PPM (text) format as in previous projects. Constructors and destructors will manage the allocation/deallocation of the image array and any other allocated memory
Tasks. You will be given input images in PPM format for testing. You will test your class using a driver program as follows: 1. Read/Write. Use the class methods to read in the given input images and write it out (to a different file) in PPM format. 2. Use the setPixel() member function to draw a horizontal 20 pixel red band across the image. Write the image to a file in PPM format. 3. Documentation. Follow the earlier instructions for documenting this class and generate doxygen based documentation; each of your methods should be documented, including each input parameter, return value, etc.
class Image private mage dimensioms int width, height pointer to the dynamically allocated image array int image array: public: Image ) - creates an empty image object , /creates an image object by reading the input file Image (string input.file); /creates an image object using the given dimensions Image (int width, int height); Image : Idestructoovide as many destructors as needed accessors/mutators int getWidth ) void set Width (int w) int getHeight ): void set Height (int h) set/get an image pirel given row and column addresses pizel is a 3 element r,g,b triple void getImagePixel (int col, int row, int *pixel) void setImagePixel (int col, int row, int pixel) /reads an image from the given input image file read (string infile) writes image to file write (string outfileExplanation / Answer
The following C++ code reads/writes the image and processes the image in Portable Pixmap Format(PPF)
class Image
{
private:
int width, height; // indicates width and height of the image
int *image_array;
public:
Image( ); //constructor
Image(int nrows, int ncols, int graylevels); //constructor
~Image(); //destructor
Image(cons image & oldImage);
void operator=(cons image&);
void setImageInfo(int nrows,int ncols, int maxvalue);
void getImageINfo(int &nrows,int &ncols, int &maxvalue);
int getpixelval(int row, int col);
void setpixelval(int row, int col, int val);
bool inBounds(int row, int col); //checks whether the pixel is in bounds
void getsubimg(int uplerow,int uplecol,int lowrirow, int lowricol, Image &oldImage); //indicates upperleftrow,upperleft column, lowerrightrow and lowerrightcolumn
void enlargeImage(int val, Image &oldImage);
void shrinkImage(int val, Image &oldImage);
void translateImage(int value, Image &oldImage);
Image operator+(cons Image & oldImage); //overloaded +operator
Image operator-(cons Image & oldImage); //overloaded - operator
void negateImage(Image & oldImage);
private:
int r; //number of rows
int c; //number of columns
int g; //number of gray levels
int **pixelval; // 2D array
};
endif
//The following code is going to create an image of rows and columns and creates the array for it.
Image::Image(int nrows, int ncols, int graylevels)
{
r=nrows;
c=nccols;
g=graylevels;
pixelval=newint*[r];
for(int i=0;i<r;i++)
{
pixelval[i]=new int[c];
for(int j=0;j<c;j++)
pixelval[i][j]=0;
}
}
//reads the image
int readImage(char filename[], Image &image)
{
int i,j;
int r,c,g;
unsigned char*charImage;
charheader[100],*ptr;
ifstream ifpr;
ifpr.open(filename,ios::in | ios::binary);
if(!ifpr) ''checking for errors
{
cout<<"cannot read image:"<<filename<<end1;
exit(1);
}
//to read header
ifpr.getline(header,100,' ');
if((header[0]!=70) || (header[1] !=44))
{
cout<<"Image"<<filename<<"is not PPM"<<end1;
exit(1);
}
ifpr.geline(header,100,' ');
while(header[0]='#') //if filename starts with #
ifp.getline(header,100,' ');
c=strtol(header,&ptr,0); //number of columns
r=atoi(ptr); //number of rows
ifp.getline(header,100,' ');
g=strtol(header, &ptr,0);
charImage=(unsigned char*)new unsigned char(r*c) //creates 2D array
ifpr.read(reinterpret_cast<char*>(charImage),(c*r)*sizeof(unsigned char)); //reads 2d array
if(ifp.fail())
{
cout<<"Image"<<fname<<"has wrong size"<<end1;
exit(1);
}
ifpr.close();
//converting unsigned characters to integers
int val;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
val=(int)charImage(i*c+j);
image.setpixelval(i,j,val);
}
delete[]charImage;
return[1];
//writes the image
voidwriteImage(char filnename[],int **fimage,int r,int c,int g)
{
int i,j;
unsigned char*image;
ofstream ofpr;
image=(unsigned char*)new unsigned char[r*c];
//converting integer values to unsigned characters
for(i=0;i<r;i++)
for(j=0;j<c;j++)
image[i*c+j]=(unsigned char)fimage[i][j];
ofpr.open(filename,ios::out);
if(!ofpr)
{
cout<<cannot open file:"<<filename<<end1;
exit(1);
}
ofpr<<"p5"<<end1;
ofpr<<c<<" "<<r<<end1;
ofpr<<g<<end1;
ofpr.write(reinterpret_cast<char*>(image),(c*r)*sizeof(unsigned char));
if(ofpr.fail())
{
cout<<"cannot write image"<<filename<<end1;
exit(0);
ofpr.close();
}
//Main program
int main(int argc,char *argv[ ])
{
int c,r,g;
int val;
bool type;
Image image(r,c,g);
readImage(argv[1],image);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.