libimg/src/rotate.c

87 lines
1.8 KiB
C

#include <libimg.h>
#include "libimg-internal.h"
static void img_rotate_90(img_t src, img_t dst)
{
if(!img_target(dst, src.height, src.width)) return;
img_pixel_t *src_px = src.pixels;
img_pixel_t *dst_px = dst.pixels + src.height - 1;
for(int y = 0; y < src.height; y++)
{
for(int x=0, dy=0; x < src.width; x++, dy+=dst.stride)
if(src_px[x] != IMG_ALPHA)
dst_px[dy] = src_px[x];
src_px += src.stride;
dst_px--;
}
}
static void img_rotate_180(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 + (src.height - 1) * dst.stride +
src.width - 1;
for(int y = 0; y < src.height; y++)
{
for(int x=0; x < src.width; x++)
if(src_px[x] != IMG_ALPHA)
dst_px[-x] = src_px[x];
src_px += src.stride;
dst_px -= dst.stride;
}
}
static void img_rotate_270(img_t src, img_t dst)
{
if(!img_target(dst, src.height, src.width)) return;
img_pixel_t *src_px = src.pixels;
img_pixel_t *dst_px = dst.pixels + (src.width - 1) * dst.stride;
for(int y = 0; y < src.height; y++)
{
for(int x=0, dy=0; x < src.width; x++, dy+=dst.stride)
if(src_px[x] != IMG_ALPHA)
dst_px[-dy] = src_px[x];
src_px += src.stride;
dst_px++;
}
}
void img_rotate(img_t src, img_t dst, int angle)
{
if(src.pixels == dst.pixels)
{
img_rotate_inplace(src, angle);
return;
}
if(angle == 0)
{
if(!img_target(dst, src.width, src.height)) return;
img_render(src, dst);
}
if(angle == 90) img_rotate_90(src, dst);
if(angle == 180) img_rotate_180(src, dst);
if(angle == 270) img_rotate_270(src, dst);
}
img_t img_rotate_create(img_t src, int angle)
{
int w = src.width, h = src.height;
if(angle == 90 || angle == 270) w = src.height, h = src.width;
img_t dst = img_create(w, h);
img_fill(dst, IMG_ALPHA);
img_rotate(src, dst, angle);
return dst;
}