]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
Add resize and rotate methods to image object
authorCharlie Brej <cbrej@cs.man.ac.uk>
Sat, 20 Sep 2008 22:12:02 +0000 (18:12 -0400)
committerRay Strode <rstrode@redhat.com>
Sat, 20 Sep 2008 22:12:02 +0000 (18:12 -0400)
Some splash plugins may want to rotate and scale
sprites on screen, so these functions may be useful.

src/libply/ply-image.c
src/libply/ply-image.h

index d6b92dbb091334af9896ca63c6a1257056dc2761..1cc4476ef47dc14313ea60ee7fcea25f1ccea047 100644 (file)
  *
  * Some implementation taken from the cairo library.
  *
- * Written by: Kristian Høgsberg <krh@redhat.com>
+ * Written by: Charlie Brej <cbrej@cs.man.ac.uk>
+ *             Kristian Høgsberg <krh@redhat.com>
  *             Ray Strode <rstrode@redhat.com>
- *             Carl D. Worth (cworth@cworth.org>
+ *             Carl D. Worth <cworth@cworth.org>
  */
 #include "config.h"
 #include "ply-image.h"
@@ -39,6 +40,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <math.h>
 
 #include <png.h>
 
@@ -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"
index bcb200681e6da0e28267a8bae1284d7d01813d65..780050e4e9074f024d73fb1e4b13049c6484617c 100644 (file)
@@ -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