Libraries - Estia-1a/projetGenieInfo_public GitHub Wiki
estia-image library
In order for you to focus mainly on image manipulation, we created a library to handle the manipulation of image files. The library proposes 3 functions to read an image file, to write an image file, and to free memory.
Read an image file
/**
* Return some data about an image file
* @param[in] filename Name of the file to be open
* @param[out] data Reference to the pointer that points to the pixel array
* @param[out] width Width of the image
* @param[out] height Height of the image
* @param[out] channel_count Number of channels
* @return 0 on failure and non-0 on success.
*/
int read_image_data(const char *filename, unsigned char **data, int *width, int *height, int *channel_count);
This function considers a file name (filename
) and provides information of this image: the data itself, i.e. the RGB values of each pixels, but also its size (width and height) and the number of channels encoding the color (3 in our case: RGB. But it could be more if we considered the alpha transparency for instance).
To use it, simply call
read_image_data(the_file_name, &data, &w, &h, &n);
With correctly declared variables the_file_name
, data
, w
, h
, and n
.
Write an image file
/**
* Writes into a png image file
* @param[in] filename Name of the file to be written into
* @param[in] data Reference to the pixel array to be written
* @param[in] width Width of the image
* @param[in] height Height of the image
* @return 0 on failure and non-0 on success.
*/
int write_image_data(const char *filename, unsigned char *data, int width, int height);
This function is similar to the read_image_data
function.
Just use the filename
(e.g., "./images/output/my_new_image_name.jpg
) and its pixels (data
):
write_image_data(output_image_name, new_data, new_w, new_h);
With correctly declared and populated variables.
Free memory
/**
* Frees stbi-related buffers and resources
* @param[in] data The buffer allocated by readImageData buffer to be freed
*/
void free_image_data(unsigned char *data);
When programming with the c
language, you have to be careful to memory allocation.
Dealing with images obviously implies memory usage.
You should use the free_image_data
function whenever you want to discard a variable containing image data (unsigned char*
)