Help me fix the error shown above in my code of imagecpp ple

Help me fix the error shown above in my code of image.cpp please~

Image.cpp(the code i wrote):

#include
#include \"image.hpp\"
#include

using namespace std;

Image::Image() {
   cols = 256;
   rows = 256;
   pixels = new uint8_t[rows * cols];
   for (int i = 0; i < rows; i++) {
       for (int j = 0; j < cols; j++) {
           pixels[i + i * j] = \'A\';
       }
   }

}
Image:: ~Image(){
   delete[] pixels;
}
int Image::get_pixel(unsigned int x, unsigned int y, uint8_t* colorp) {

   colorp= &pixels[x+x*y];

   if (colorp) {
       return 0;
   }
   return -1;
}
int Image::set_pixel(unsigned int x, unsigned int y, uint8_t color) {
   pixels[x + x * y] = color;
   if (color) {
       return 0;
   }
   return -1;
}

int Image::resize(unsigned int width, unsigned int height, uint8_t fillcolor) {
   pixels = new uint8_t[rows * cols];
   for (int i = 0; i < rows; i++) {
       for (int j = 0; j < cols; j++) {
           pixels[i + i * j] = fillcolor;
       }
   }
   if (pixels) {
       return 0;
   }
   return -1;
}

Image.hpp(resourse):

#include // for uint8_t

class Image {

public:
// made these public so I can peek at them
unsigned int cols;
unsigned int rows;
uint8_t* pixels;

/* Constructs an image of 0x0 pixels. */
Image();

/* Frees all memory allocated for img */
~Image();

/* Changes the size of an image, allocating memory as necessary, and
     setting all pixels to fillcolor. Returns 0 on success, or a non-zero error code.*/
int resize( unsigned int width, unsigned int height, uint8_t fillcolor );

/* Sets the color of the pixel at (x,y) to color. Returns 0 on success, else a non-zero
     error code. If (x,y) is not a valid pixel, the call fails and the image does not change.*/
int set_pixel( unsigned int x, unsigned int y, uint8_t color );

/* Gets the color of the pixel at (x,y) and stores at the address pointed to
     by colorp. Returns 0 on success, else a non-zero error code. */
int get_pixel( unsigned int x, unsigned int y, uint8_t* colorp );
};

Running Program Done. Program returned 1 Error: Your program returned nonzero status code 1 stdout Testing normal usage Constructing Image i Error: Empty image does not have zero cols & rows i resize( 640, 480, 0 i resize 100, 100, 72 resize( 32, 32, 72) 0K Checking for correct value in every pixel after resize after resize, pixel[257] does not have fillcolor value Error Error: get pixel returned wrong pixel value (0 e) 0 Doing lots of get pixel and set pixel on bounds pixel coords get pixel returned wrong pixel value (1 0) 0 Error: get pixel returned wrong pixel value (20) 0 Error: get pixel returned wrong pixel value (3 0) 0 Error: get pixel returned wrong pixel value (4 0) 0 Error: get pixel returned wrong pixel value (5 0) 0 Error: get pixel returned wrong pixel value (6 0) 0 Error: get pixel returned wrong pixel value (7 0) 0 Error: get pixel returned wrong pixel value (8 0) 0 Error: get pixel returned wrong pixel value (9 0) 0 Error: get pixel returned wrong pixel value (10 0) 0 Error: get pixel returned wrong pixel value (11 0) 0 Error: get pixel returned wrong pixel value (120) 0 Error: get pixel returned wrong pixel value (13 0) 0 Error: get pixel returned wrong pixel value (14 0) 0 Error: get pixel returned wrong pixel value (15 0) 0 Error: get pixel turned wrong pixel value (16 0) 0 Error: get pixel returned wrong pixel value (17 0) 0 Error: get pixel returned wrong pixel value (18 0) 0 Error: get pixel returned wrong pixel value (19 0) 0 Error: get pixel returned wrong pixel value (20 0) 0 Error: get pixel returned wrong p value (21 e) 0 Error: get pixel returned wrong pixel value (22 0) 0 Error: get pixel returned wrong pixel value (23 0) 0 Error: get pixel returned wrong pixel value (24 e) 0 Error: get pixel returned wrong pixel value (25 0) 0 Error: get pixel returned wrong pixel value (26 0) 0 Error: get pixel returned wrong pixel value (27 0) 0 Error: get pixel returned wrong pixel value (28 0) 0 Error: get pixel returned wrong pixel value (29 0) 0 Error: get pixel returned wrong pixel value (30 0) 0 Error: get pixel returned wrong pixel value (31 0) 0 Error: get pixel returned wrong pixel value (32 0) 0 Error: get pixel returned wrong pixel value (33 0) 0 Error: get pixel returned wrong pixel value (34 0) 0 Error: get pixel returned wrong pixel value (35 0) 0 Error: get pixel returned wrong pixel value (36 0) 0 Error: get pixel returned wrong pixel value (37 0) 0 Error: get pixel returned wrong pixel value (38 0) 0 Error: get pixel returned wrong pixel value (39 0) 0 Error: get pixel returned wrong pixel value (40 0) 0 Error: get pixel returned wrong pixel value (41 0) 0 Error: get pixel returned wrong pixel value (42 0) 0 Error: get pixel returned wrong pixel value (43 0) 0 Error: get pixel returned wrong pixel value (44 e) 0 Error: get pixel returned wrong pixel value (45 0) 0 Error: get pixel returned wrong pixel value (46 0) 0 Error: get pixel returned wrong pixel value (47 0) 0 Error: get pixel returned wrong pixel value (48 0) 0 Error: get pixel returned wrong pixel value (49 0) 0 Error: get pixel returned wrong pixel value (50 0) 0 Error: get pixel returned wrong pixel value (51 0) 0 2 [201 10.pdf] Menu asonwillhudson Gaja Mozilla Firefox

Solution

Image.cpp


#include <iostream>
#include <stdio.h>
#include \"image.hpp\"
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>

using namespace std;

/* Constructs an image of 0x0 pixels. */
Image::Image() {
   cols = 0;
   rows = 0;
   pixels = new uint8_t[cols*rows];
}

/* Frees all memory allocated for img */
Image::~Image() {
   if(pixels) {
       delete[] pixels;
   }
}

/* Changes the size of an image, allocating memory as necessary, and
setting all pixels to fillcolor. Returns 0 on success, or a non-zero error code.*/
int Image::resize(unsigned int width, unsigned int height, uint8_t fillcolor) {
   cols = width;
   rows = height;

   if (pixels) {delete [] pixels;}

   pixels = new uint8_t[cols * rows];
   if (pixels == NULL) {return 1;}

   //printf(\"%s\ \", \"\ \");
   for (unsigned int pos = 0; pos < cols*rows; pos++) {
       pixels[pos] = fillcolor;
       //printf(\"pixel value inside: %d\ \", pixels[pos]);
   }

   return 0;
}

/* Sets the color of the pixel at (x,y) to color. Returns 0 on success, else a non-zero
     error code. If (x,y) is not a valid pixel, the call fails and the image does not change.*/
int Image::set_pixel(unsigned int x, unsigned int y, uint8_t color) {
   if (pixels) {

       if (y >= rows || x >= cols) {
           //printf(\"%s\ \", \"pixel out of bounds\");
           return 1;
       }

       pixels[y * cols + x] = color;

       return 0;

       /*printf(\"%s\ \", \"\ \");
       for (unsigned int pos = 0; pos < cols*rows; pos++) {
           printf(\"pixel value inside: %d\ \", pixels[pos]);
       }*/
   }

   return 1;
}

/* Gets the color of the pixel at (x,y) and stores at the address pointed to
     by colorp. Returns 0 on success, else a non-zero error code. */
int Image::get_pixel(unsigned int x, unsigned int y, uint8_t* colorp) {

   if (colorp != NULL) {
       if (pixels) {
           if (y >= rows || x >= cols) {
               //printf(\"%s\ \", \"pixel out of bounds\");
               return 1;
           }

           *colorp = pixels[y * cols + x];
           //printf(\"get pixel value: %d\ \", *colorp);
           return 0;
       }
       return 1;
   }
   return 1;
}

int Image::save(const char* filename) {
   if (filename == NULL) {
       printf(\"%s\ \", \"Error: No name supplied!\");
       return 1;
   }

   //open the file and start a string stream
   ofstream openedFile;
   openedFile.open(filename);

   //strings to store the rows and cols
   char colsString[7];
   char rowsString[7];

   //convert to strings
   sprintf(colsString, \"%d\", cols);
   sprintf(rowsString, \"%d\", rows);

   //write to file
   openedFile << colsString << std::endl;
   openedFile << rowsString << std::endl;

   //convert pixels to strings, and write to file
   for (unsigned int pos = 0; pos < rows*cols; pos++) {
       char pxString[7];
       sprintf(pxString, \"%d\", pixels[pos]);

       openedFile << pxString << std::endl;
   }

   //we\'re done so close everything and return success
   openedFile.close();
   return 0;
}

int Image::load(const char* filename) {
   if (filename != NULL) {

       //set up string variable to read and store each line inside
       string line;
       ifstream openedFile(filename);

       if (openedFile) {

           if (openedFile.is_open()) {

               int pos = 0;

               //once everything is allocated, set to 1 so we don\'t delete and allocate new memory
               //upon the next iteration of the loop
               int gotNewMemory = 0;

                while (getline(openedFile,line)) {

                   //variable to store the chars as ints
                   int numLine;
                   istringstream (line) >> numLine;

                   //get the col
                   if (pos == 0) {
                       cols = numLine;
                       pos++;
                   }

                   //get the row
                   else if (pos == 1) {
                       rows = numLine;
                       pos++;
                   }

                   //get the pixels
                   else if (pos >= 2 && pos-2 < cols*rows) {

                       //allocate memory to store everything on the first iteration
                       if (gotNewMemory == 0) {
                           if (pixels) {delete [] pixels;}
                           pixels = new uint8_t[cols * rows];
                           gotNewMemory = 1;
                       }

                       pixels[pos-2] = numLine;
                       //printf(\"%d\ \", numLine);
                       pos++;
                   }
                }
               openedFile.close();
           }

           // printf(\"cols: %d\ \", cols);
           // printf(\"rows: %d\ \", rows);
           // printf(\"px: %d\ \", pixels[0]);
           // printf(\"px: %d\ \", pixels[1]);
           // printf(\"px: %d\ \", pixels[2]);
           // printf(\"px: %d\ \", pixels[3]);
           // printf(\"px: %d\ \", pixels[4]);
           // printf(\"px: %d\ \", pixels[5]);
           // printf(\"px: %d\ \", pixels[6]);
           // printf(\"px: %d\ \", pixels[7]);
           // printf(\"px: %d\ \", pixels[8]);

           return 0;
       }

       return 1;
   }

   return 1;
}

Image.hpp


#include <stdint.h> // for uint8_t

class Image {

public:
unsigned int cols;


unsigned int rows;
uint8_t* pixels;

/* Constructs an image of 0x0 pixels. */
Image();

/* Frees all memory allocated for img */
~Image();

/* Changes the size of an image, allocating memory as necessary, and
     setting all pixels to fillcolor. Returns 0 on success, or a
     non-zero error code.*/
int resize( unsigned int width, unsigned int height, uint8_t fillcolor );

/* Sets the color of the pixel at (x,y) to color. Returns 0 on
     success, else a non-zero error code. If (x,y) is not a valid
     pixel, the call fails and the image does not change.*/
int set_pixel( unsigned int x, unsigned int y, uint8_t color );

/* Gets the color of the pixel at (x,y) and stores at the address
     pointed to by colorp. Returns 0 on success, else a non-zero error
     code. */
int get_pixel( unsigned int x, unsigned int y, uint8_t* colorp );

/* Saves the image in the file filename. In a format that can be
     loaded by load(). Returns 0 on success, else a non-zero error
     code. */
int save( const char* filename );

/* Load the an image from the file filename, replacing the current
     image size and data. The file is in a format that was saved by
     save(). Returns 0 success, else a non-zero error code . */
int load( const char* filename );
};


main.cpp


#include \"image.hpp\"

int main() {
   uint8_t x = 0;
   Image j;
   j.resize(3, 3, 100);
   j.set_pixel(0, 0, 63);
   j.set_pixel(0, 1, 5);
   j.set_pixel(0, 2, 24);
   j.set_pixel(1, 0, 3);
   j.set_pixel(1, 1, 69);
   j.set_pixel(1, 2, 254);
   j.set_pixel(2, 0, 84);
   j.set_pixel(2, 1, 44);
   j.set_pixel(2, 2, 1);
   //j.get_pixel(0, 6, &x);
   //cout << \"value outside ::: \" << unsigned(x) << endl;
   j.save(\"saved.txt\");
   Image k;
   //k.resize(2, 2, 55);
   k.load(\"saved.txt\");
   k.get_pixel(2, 1, &x);
   //cout << \"value outside ::: \" << unsigned(x) << endl;
   return 0;
}

Help me fix the error shown above in my code of image.cpp please~ Image.cpp(the code i wrote): #include #include \
Help me fix the error shown above in my code of image.cpp please~ Image.cpp(the code i wrote): #include #include \
Help me fix the error shown above in my code of image.cpp please~ Image.cpp(the code i wrote): #include #include \
Help me fix the error shown above in my code of image.cpp please~ Image.cpp(the code i wrote): #include #include \
Help me fix the error shown above in my code of image.cpp please~ Image.cpp(the code i wrote): #include #include \
Help me fix the error shown above in my code of image.cpp please~ Image.cpp(the code i wrote): #include #include \
Help me fix the error shown above in my code of image.cpp please~ Image.cpp(the code i wrote): #include #include \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site