#include "Image.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
/* Function to create a new image */
Image *NewImage(unsigned int Width, unsigned int Height) {
// Allocate memory for the main Image struct
Image *image = malloc(sizeof(Image));
// Check if malloc worked
if (image == NULL) {
return NULL;
}
// Set the width and height
image->W = Width;
image->H = Height;
// Allocate memory for Red, Green, and Blue arrays
// Each one needs Width * Height amount of space
image->R = malloc(sizeof(unsigned char) * Width * Height);
image->G = malloc(sizeof(unsigned char) * Width * Height);
image->B = malloc(sizeof(unsigned char) * Width * Height);
// If any of the color arrays failed, we need to clean up and exit
if (image->R == NULL || image->G == NULL || image->B == NULL) {
DeleteImage(image);
return NULL;
}
return image;
}
/* Function to free all the memory we used */
void DeleteImage(Image *image) {
// Make sure the image actually exists before freeing
if (image != NULL) {
// Free the color arrays first
if (image->R != NULL) {
free(image->R);
}
if (image->G != NULL) {
free(image->G);
}
if (image->B != NULL) {
free(image->B);
}
// Set pointers to NULL so we don't use them by accident
// (This is required for the assignment)
image->R = NULL;
image->G = NULL;
image->B = NULL;
// Finally free the image struct itself
free(image);
}
}
/* Get and Set functions for RED */
unsigned char GetPixelR(const Image *image, unsigned int x, unsigned int y) {
assert(image != NULL);
assert(x < image->W);
assert(y < image->H);
// Formula for 1D index: x + (y * Width)
int index = x + (y * image->W);
return image->R[index];
}
void SetPixelR(Image *image, unsigned int x, unsigned int y, unsigned char r) {
assert(image != NULL);
assert(x < image->W);
assert(y < image->H);
int index = x + (y * image->W);
image->R[index] = r;
}
/* Get and Set functions for GREEN */
unsigned char GetPixelG(const Image *image, unsigned int x, unsigned int y) {
assert(image != NULL);
assert(x < image->W);
assert(y < image->H);
int index = x + (y * image->W);
return image->G[index];
}
void SetPixelG(Image *image, unsigned int x, unsigned int y, unsigned char g) {
assert(image != NULL);
assert(x < image->W);
assert(y < image->H);
int index = x + (y * image->W);
image->G[index] = g;
}
/* Get and Set functions for BLUE */
unsigned char GetPixelB(const Image *image, unsigned int x, unsigned int y) {
assert(image != NULL);
assert(x < image->W);
assert(y < image->H);
int index = x + (y * image->W);
return image->B[index];
}
void SetPixelB(Image *image, unsigned int x, unsigned int y, unsigned char b) {
assert(image != NULL);
assert(x < image->W);
assert(y < image->H);
int index = x + (y * image->W);
image->B[index] = b;
}
/* Functions to get Width and Height */
unsigned int ImageWidth(const Image *image) {
assert(image != NULL);
return image->W;
}
unsigned int ImageHeight(const Image *image) {
assert(image != NULL);
return image->H;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter