Files
libimg/src/transformations.c
T

68 lines
1.2 KiB
C

#include <stdlib.h>
#include "image/transformations.h"
unsigned int*
ppm_flip_h(const unsigned int* buf, const PPM img)
{
unsigned int* flip = malloc(img.len * sizeof(unsigned int));
if (!flip)
{
return NULL;
}
int idx = 0;
for (int i = 0; i < img.height; i++)
{
for (int k = img.width - 1; k >= 0; k--)
{
flip[idx] = buf[(i * img.width) + k];
idx++;
}
}
return flip;
}
unsigned int*
ppm_flip_v(const unsigned int* buf, const PPM img)
{
unsigned int* flip = malloc(img.len * sizeof(unsigned int));
if (!flip)
{
return NULL;
}
int idx = 0;
for (int i = img.height - 1; i >= 0; i--)
{
for (int k = 0; k < img.width; k++)
{
flip[idx] = buf[(i * img.width) + k];
idx++;
}
}
return flip;
}
unsigned int*
u32_rotate(const unsigned int* buf, PPM* m_img, const int len)
{
unsigned int* mask = malloc(len * sizeof(unsigned int));
int old_width = m_img->width;
int old_height = m_img->height;
for (int row = 0; row < old_height; row++)
{
for (int col = 0; col < old_width; col++)
{
int new_height = col;
int new_width = (old_height - 1 - row);
mask[(new_height * old_height) + new_width] =
buf[(row * old_width) + col];
}
}
m_img->width = old_height;
m_img->height = old_width;
return mask;
}