Skip to content

Commit

Permalink
Função ImageCreate + Função ImageDestroy
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoA04 committed Nov 18, 2023
1 parent e120f86 commit c79a5f1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions image8bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ Image ImageCreate(int width, int height, uint8 maxval) { ///
assert (height >= 0);
assert (0 < maxval && maxval <= PixMax);
// Insert your code here!
Image img = (Image)malloc(sizeof(Image));
if(img == NULL){
errno = 1;
errCause = "Memory allocation failed.";
return NULL;
}
img->width = width;
img->height = height;
img->pixel = (uint8*)malloc(sizeof(uint8) * width * height);
if(img->pixel == NULL){
errno = 2;
errCause = "Memory allocation failed.";
free(img);
return NULL;
}
for(int i = 0; i < width * height; ++i){
img->pixel[i] = 0;
}

return img;
}

/// Destroy the image pointed to by (*imgp).
Expand All @@ -182,6 +202,11 @@ Image ImageCreate(int width, int height, uint8 maxval) { ///
void ImageDestroy(Image* imgp) { ///
assert (imgp != NULL);
// Insert your code here!
if (*imgp != NULL) {
free((*imgp)->pixel);
free(*imgp);
*imgp = NULL;
}
}


Expand Down

0 comments on commit c79a5f1

Please sign in to comment.