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 );
};
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;
}






