]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
script: add Image.Crop(x, y, width, height)
authorJ-P Nurmi <jpnurmi@gmail.com>
Tue, 10 Mar 2020 22:10:08 +0000 (23:10 +0100)
committerRay Strode <halfline@gmail.com>
Wed, 11 Mar 2020 13:44:05 +0000 (13:44 +0000)
The script plugin currently allows pixel buffers to be resized and
scaled, but provides no mechanism for script theme authors to just crop
the buffer, which is necessary for images that cannot be stretched,
such as progress bars with gradients.

This commit adds that feature as a new Crop method.

src/plugins/splash/script/script-lib-image.c
src/plugins/splash/script/script-lib-image.script

index c53e0b0ec8a7064be5f8624b1ad50b4d656cecb5..cc671d44e6e3ad83309ed96c86955e437872d3f7 100644 (file)
@@ -131,6 +131,25 @@ static script_return_t image_rotate (script_state_t *state,
         return script_return_obj_null ();
 }
 
+static script_return_t image_crop (script_state_t *state,
+                                   void           *user_data)
+{
+        script_lib_image_data_t *data = user_data;
+        ply_pixel_buffer_t *image = script_obj_as_native_of_class (state->this, data->class);
+        int x = script_obj_hash_get_number (state->local, "x");
+        int y = script_obj_hash_get_number (state->local, "y");
+        int width = script_obj_hash_get_number (state->local, "width");
+        int height = script_obj_hash_get_number (state->local, "height");
+
+        if (image) {
+                ply_rectangle_t clip_area = { 0, 0, width, height };
+                ply_pixel_buffer_t *new_image = ply_pixel_buffer_new (width, height);
+                ply_pixel_buffer_fill_with_buffer_with_clip (new_image, image, -x, -y, &clip_area);
+                return script_return_obj (script_obj_new_native (new_image, data->class));
+        }
+        return script_return_obj_null ();
+}
+
 static script_return_t image_scale (script_state_t *state,
                                     void           *user_data)
 {
@@ -263,6 +282,15 @@ script_lib_image_data_t *script_lib_image_setup (script_state_t *state,
                                     data,
                                     "angle",
                                     NULL);
+        script_add_native_function (image_hash,
+                                    "_Crop",
+                                    image_crop,
+                                    data,
+                                    "x",
+                                    "y",
+                                    "width",
+                                    "height",
+                                    NULL);
         script_add_native_function (image_hash,
                                     "_Scale",
                                     image_scale,
index ab4b7cfe8a07bb76a43e1719faa051d688022b01..8568ceb69ace35222d4e2029b9888ad05c66afb0 100644 (file)
@@ -9,6 +9,11 @@ Image.Rotate = fun (angle)
   return Image.Adopt (this._Rotate(angle));
 };
 
+Image.Crop = fun (x, y, width, height)
+{
+  return Image.Adopt (this._Crop(x, y, width, height));
+};
+
 Image.Scale = fun (width, height)
 {
   return Image.Adopt (this._Scale(width, height));