DOCS: Added documentation to transformations

TRANSFORMATIONS: Added Checks for null buffers and null image. Also now free old buffer to prevent leaks
This commit is contained in:
Andrew Haynes
2026-08-31 22:41:16 -04:00
parent 854f5c2ba6
commit 3d00cfd2a7
3 changed files with 54 additions and 5 deletions
+28
View File
@@ -3,12 +3,40 @@
#include "utils.h" #include "utils.h"
/**
* @brief Flips image on X-Axis
* @details Creates a new buffer to move pixels to and flips pixels on X-Axis,
* freeing the old buffer at the end.
* @param[in] buf Pointer to original pixel buffer, later freed.
* @param[in] img Loaded PPM image for calculations
* @return unsigned int Pointer to new pixel buffer
* @retval NULL If original buffer is invalid or if new buffer fails to create
* */
unsigned int* unsigned int*
ppm_flip_h(const unsigned int* buf, const PPM img); ppm_flip_h(const unsigned int* buf, const PPM img);
/**
* @brief Flips image on Y-Axis
* @details Creates a new buffer to move pixels to and flips pixels on Y-Axis,
* freeing the old buffer at the end.
* @param[in] buf Pointer to original pixel buffer, later freed.
* @param[in] img Loaded PPM image for calculations
* @return unsigned int Pointer to new pixel buffer
* @retval NULL If original buffer is invalid or if new buffer fails to create
* */
unsigned int* unsigned int*
ppm_flip_v(const unsigned int* buf, const PPM img); ppm_flip_v(const unsigned int* buf, const PPM img);
/**
* @brief Rotates image on 90 Clockwise
* @details Creates a new buffer to move pixels to and rotates pixels 90 degrees
* clockwise around center of origin, freeing the old buffer at the end.
* @param[in] buf Pointer to original pixel buffer, later freed.
* @param[in] img Pointer to loaded PPM image for calculations
* @return unsigned int Pointer to new pixel buffer
* @retval NULL If original buffer is invalid, if image pointer is invalid or if
* new buffer fails to create and is invalid
* */
unsigned int* unsigned int*
u32_rotate(const unsigned int* buf, PPM* m_img); u32_rotate(const unsigned int* buf, PPM* m_img);
+7 -5
View File
@@ -1,6 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "image/filters.h" #include "image/filters.h"
#include "image/transformations.h"
#include "image/utils.h" #include "image/utils.h"
// #include "image/filters.h" // #include "image/filters.h"
// #include "image/transformations.h" // #include "image/transformations.h"
@@ -18,11 +19,12 @@ main()
}; };
unsigned int* buf = ppm_load(img.name, &img); unsigned int* buf = ppm_load(img.name, &img);
for (int i = 0; i < img.len; i++) // for (int i = 0; i < img.len; i++)
{ // {
// you need to replace the old value with the new updated value // // you need to replace the old value with the new updated value
buf[i] = u32_grayscale(buf[i]); // buf[i] = u32_grayscale(buf[i]);
} // }
buf = u32_rotate(buf, &img);
ppm_save(buf, img, "../IMG_1649_new.ppm"); ppm_save(buf, img, "../IMG_1649_new.ppm");
// PPM out_2 = inherit_from_jpeg(&out_1, '6'); // PPM out_2 = inherit_from_jpeg(&out_1, '6');
+19
View File
@@ -5,6 +5,10 @@
unsigned int* unsigned int*
ppm_flip_h(const unsigned int* buf, const PPM img) ppm_flip_h(const unsigned int* buf, const PPM img)
{ {
if (!buf)
{
return NULL;
}
unsigned int* flip = malloc(img.len * sizeof(unsigned int)); unsigned int* flip = malloc(img.len * sizeof(unsigned int));
if (!flip) if (!flip)
{ {
@@ -19,6 +23,7 @@ ppm_flip_h(const unsigned int* buf, const PPM img)
idx++; idx++;
} }
} }
free((void*)buf);
return flip; return flip;
} }
@@ -26,6 +31,10 @@ ppm_flip_h(const unsigned int* buf, const PPM img)
unsigned int* unsigned int*
ppm_flip_v(const unsigned int* buf, const PPM img) ppm_flip_v(const unsigned int* buf, const PPM img)
{ {
if (!buf)
{
return NULL;
}
unsigned int* flip = malloc(img.len * sizeof(unsigned int)); unsigned int* flip = malloc(img.len * sizeof(unsigned int));
if (!flip) if (!flip)
{ {
@@ -41,6 +50,7 @@ ppm_flip_v(const unsigned int* buf, const PPM img)
idx++; idx++;
} }
} }
free((void*)buf);
return flip; return flip;
} }
@@ -48,7 +58,15 @@ ppm_flip_v(const unsigned int* buf, const PPM img)
unsigned int* unsigned int*
u32_rotate(const unsigned int* buf, PPM* m_img) u32_rotate(const unsigned int* buf, PPM* m_img)
{ {
if (!buf || !m_img)
{
return NULL;
}
unsigned int* mask = malloc(m_img->len * sizeof(unsigned int)); unsigned int* mask = malloc(m_img->len * sizeof(unsigned int));
if (!mask)
{
return NULL;
}
int old_width = m_img->width; int old_width = m_img->width;
int old_height = m_img->height; int old_height = m_img->height;
for (int row = 0; row < old_height; row++) for (int row = 0; row < old_height; row++)
@@ -64,5 +82,6 @@ u32_rotate(const unsigned int* buf, PPM* m_img)
} }
m_img->width = old_height; m_img->width = old_height;
m_img->height = old_width; m_img->height = old_width;
free((void*)buf);
return mask; return mask;
} }