From 3e34fba65399fa665384441fcb78357ff19a83d5 Mon Sep 17 00:00:00 2001 From: Charlie Brej Date: Sat, 20 Sep 2008 18:12:02 -0400 Subject: [PATCH] Add resize and rotate methods to image object Some splash plugins may want to rotate and scale sprites on screen, so these functions may be useful. --- src/libply/ply-image.c | 85 +++++++++++++++++++++++++++++++++++++++++- src/libply/ply-image.h | 2 + 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/libply/ply-image.c b/src/libply/ply-image.c index d6b92dbb..1cc4476e 100644 --- a/src/libply/ply-image.c +++ b/src/libply/ply-image.c @@ -20,9 +20,10 @@ * * Some implementation taken from the cairo library. * - * Written by: Kristian Høgsberg + * Written by: Charlie Brej + * Kristian Høgsberg * Ray Strode - * Carl D. Worth (cworth@cworth.org> + * Carl D. Worth */ #include "config.h" #include "ply-image.h" @@ -39,6 +40,7 @@ #include #include #include +#include #include @@ -266,6 +268,85 @@ ply_image_get_height (ply_image_t *image) return image->height; } +ply_image_t * +ply_image_resize (ply_image_t *image, + long width, + long height) +{ + ply_image_t *new_image; + int x, y; + int old_x, old_y, old_width, old_height; + float scale_x, scale_y; + + new_image = ply_image_new (image->filename); + + new_image->layout.address = malloc (height * width * 4); + + new_image->width = width; + new_image->height = height; + + old_width = ply_image_get_width (image); + old_height = ply_image_get_height (image); + + scale_x = ((double) old_width) / width; + scale_y = ((double) old_height) / height; + + for (y = 0; y < height; y++) + { + old_y = y * scale_y; + for (x=0; x < width; x++) + { + old_x = x * scale_x; + new_image->layout.as_pixels[x + y * width] = image->layout.as_pixels[old_x + old_y * old_width]; + } + } + return new_image; +} + +ply_image_t * +ply_image_rotate (ply_image_t *image, + long center_x, + long center_y, + double theta_offset) +{ + ply_image_t *new_image; + int x, y; + int old_x, old_y; + int width; + int height; + + width = ply_image_get_width (image); + height = ply_image_get_height (image); + + new_image = ply_image_new (image->filename); + + new_image->layout.address = malloc (height * width * 4); + new_image->width = width; + new_image->height = height; + + for (y = 0; y < height; y++) + { + for (x = 0; x < width; x++) + { + double d; + double theta; + + d = sqrt ((x - center_x) * (x - center_x) + (y - center_y) * (y - center_y)); + theta = atan2 (y - center_y, x - center_x); + + theta -= theta_offset; + old_x = center_x + d * cos (theta); + old_y = center_y + d * sin (theta); + + if (old_x < 0 || old_x >= width || old_y < 0 || old_y >= height) + new_image->layout.as_pixels[x +y * width] = 0x00000000; + else + new_image->layout.as_pixels[x + y * width] = image->layout.as_pixels[old_x + old_y * width]; + } + } + return new_image; +} + #ifdef PLY_IMAGE_ENABLE_TEST #include "ply-frame-buffer.h" diff --git a/src/libply/ply-image.h b/src/libply/ply-image.h index bcb20068..780050e4 100644 --- a/src/libply/ply-image.h +++ b/src/libply/ply-image.h @@ -36,6 +36,8 @@ uint32_t *ply_image_get_data (ply_image_t *image); ssize_t ply_image_get_size (ply_image_t *image); long ply_image_get_width (ply_image_t *image); long ply_image_get_height (ply_image_t *image); +ply_image_t *ply_image_resize (ply_image_t *image, long width, long height); +ply_image_t *ply_image_rotate (ply_image_t *oldimage, long center_x, long center_y, double theta_offset); #endif -- 2.47.3