libimg/src/flip.c

78 lines
1.5 KiB
C

#include <libimg.h>
#include "libimg-internal.h"
void img_hflip(img_t src, img_t dst)
{
if(!img_target(dst, src.width, src.height)) return;
img_pixel_t *src_px = src.pixels;
img_pixel_t *dst_px = dst.pixels;
for(int y = 0; y < src.height; y++)
{
for(int x = 0; x < (src.width+1) >> 1; x++)
{
int a = src_px[x];
int b = src_px[src.width-1 - x];
if(b != IMG_ALPHA)
dst_px[x] = b;
if(a != IMG_ALPHA)
dst_px[src.width-1 - x] = a;
}
src_px += src.stride;
dst_px += dst.stride;
}
}
img_t img_hflip_create(img_t src)
{
img_t dst = img_create(src.width, src.height);
img_fill(dst, IMG_ALPHA);
img_hflip(src, dst);
return dst;
}
void img_vflip(img_t src, img_t dst)
{
if(!img_target(dst, src.width, src.height)) return;
int src_fullheight = (src.height - 1) * src.stride;
int dst_fullheight = (src.height - 1) * dst.stride;
img_pixel_t *src_px = src.pixels;
img_pixel_t *dst_px = dst.pixels;
for(int x = 0; x < src.width; x++)
{
int src_offset = 0;
int dst_offset = 0;
for(int y = 0; y < (src.height+1) >> 1; y++)
{
int a = src_px[src_offset];
int b = src_px[src_fullheight - src_offset];
if(b != IMG_ALPHA)
dst_px[dst_offset] = b;
if(a != IMG_ALPHA)
dst_px[dst_fullheight - dst_offset] = a;
src_offset += src.stride;
dst_offset += dst.stride;
}
src_px++;
dst_px++;
}
}
img_t img_vflip_create(img_t src)
{
img_t dst = img_create(src.width, src.height);
img_fill(dst, IMG_ALPHA);
img_hflip(src, dst);
return dst;
}