Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am done almost but just loding part last one, *** SEGMENTATION FAULT *** this error message I do not know why help me //image3.cpp

I am done almost but just loding part last one,

 *** SEGMENTATION FAULT *** this error message 

I do not know why

help me

//image3.cpp

#include // for uint8_t #include #include #include #include #include #include

#include "image3.hpp"

using namespace std;

Image::Image() {

cols = 0; rows = 0; pixels = NULL; }

Image::~Image() { cols = 0; rows = 0; delete pixels; }

int Image::resize(unsigned int width, unsigned int height, uint8_t fillcolor) { pixels = (uint8_t**)malloc(sizeof(uint8_t*)*width); if (pixels == NULL) { return 1; }

for (int i = 0; i < width; i++) { pixels[i] = (uint8_t*)malloc(sizeof(uint8_t)*height); if(pixels[i] == NULL) { return 1; } for(int j = 0; j < height; j++) { pixels[i][j] = fillcolor; } } 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(x < cols && y < rows) { pixels[y][x] = color; return 0; } } 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(pixels && colorp != NULL) { if(x < cols && y < rows) { *colorp = pixels[y][x]; return 0; } } return 1; }

/* 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 Image::save( const char* filename ) {

ofstream OutF(filename);

if (OutF.is_open()) { OutF << cols << " " << rows << " "; for (int i = 0; i < cols; i++) { for (int j=0; j < rows; j++) { if ( (i == (cols-1)) && (j == (rows-1)) ) { OutF << unsigned(pixels[i][j]); } else { OutF << unsigned(pixels[i][j]) << " "; } } } OutF.close(); return 0; } else { return 1; } }

/* 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 Image::load( const char* filename ) { ifstream InF(filename);

if (InF.is_open()) { unsigned int input; rows = 0; cols = 0; bool PixelsDecide = false; bool Colwork = false; bool Rowwork = false;

int i = 0; int j = 0;

while (!InF.eof()) { InF>>input; if (!Colwork) { cols=input; Colwork = true; } else if (!Rowwork) { rows=input; Rowwork = true; } else { if (!PixelsDecide) { pixels = (uint8_t**)malloc(sizeof(uint8_t*)*cols); if (pixels == NULL) { return 1; }

for (int i = 0; i < cols; i++) { pixels[i]=(uint8_t*)malloc(sizeof(uint8_t)*rows);

if (pixels[i] == NULL) { return 1; } }

pixels[i][j] = input; j++; PixelsDecide = true; } else { pixels[i][j++] = input;

if (j%rows == 0) { j = 0; i++; } } } }

InF.close(); return 0; } else { return 1; }

}

//image3.hpp

#include // 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 ); };

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

Step: 3

blur-text-image

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

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago