]> git.mdlowis.com Git - proto/labwc.git/commitdiff
buffer: add buffer_resize()
authortokyo4j <hrak1529@gmail.com>
Fri, 30 May 2025 11:55:31 +0000 (20:55 +0900)
committerHiroaki Yamamoto <hrak1529@gmail.com>
Fri, 6 Jun 2025 17:12:56 +0000 (02:12 +0900)
include/buffer.h
src/buffer.c
src/img/img.c

index c034a874e1e73432a107284eeab36adcbd362d2d..a756a7b55fa8c24d9046466f526190074cb51883 100644 (file)
@@ -70,4 +70,11 @@ struct lab_data_buffer *buffer_create_cairo(uint32_t logical_width,
 struct lab_data_buffer *buffer_create_from_data(void *pixel_data, uint32_t width,
        uint32_t height, uint32_t stride);
 
+/*
+ * Resize a buffer to the given size. The source buffer is rendered at the
+ * center of the output buffer and shrunk if it overflows from the output buffer.
+ */
+struct lab_data_buffer *buffer_resize(struct lab_data_buffer *src_buffer,
+       int width, int height, double scale);
+
 #endif /* LABWC_BUFFER_H */
index 1badaf6ed0644448cc3db3ccef45bdc147f633ba..085601168427fa54134a698a92e25d7356e521eb 100644 (file)
@@ -29,6 +29,7 @@
 #include <drm_fourcc.h>
 #include <wlr/interfaces/wlr_buffer.h>
 #include "buffer.h"
+#include "common/box.h"
 #include "common/mem.h"
 
 static const struct wlr_buffer_impl data_buffer_impl;
@@ -146,3 +147,37 @@ buffer_create_from_data(void *pixel_data, uint32_t width, uint32_t height,
        buffer->surface_owns_data = false;
        return buffer;
 }
+
+struct lab_data_buffer *
+buffer_resize(struct lab_data_buffer *src_buffer, int width, int height,
+               double scale)
+{
+       assert(src_buffer);
+       cairo_surface_t *surface = src_buffer->surface;
+
+       int src_w = cairo_image_surface_get_width(surface);
+       int src_h = cairo_image_surface_get_height(surface);
+
+       struct lab_data_buffer *buffer =
+               buffer_create_cairo(width, height, scale);
+       cairo_t *cairo = cairo_create(buffer->surface);
+
+       struct wlr_box container = {
+               .width = width,
+               .height = height,
+       };
+
+       struct wlr_box dst_box = box_fit_within(src_w, src_h, &container);
+       double scene_scale = (double)dst_box.width / (double)src_w;
+       cairo_translate(cairo, dst_box.x, dst_box.y);
+       cairo_scale(cairo, scene_scale, scene_scale);
+       cairo_set_source_surface(cairo, surface, 0, 0);
+       cairo_pattern_set_filter(cairo_get_source(cairo), CAIRO_FILTER_GOOD);
+       cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
+       cairo_paint(cairo);
+
+       cairo_surface_flush(buffer->surface);
+       cairo_destroy(cairo);
+
+       return buffer;
+}
index 5e4b473feab5973cc269618d0e211856ba80f0b0..03b9130a9b85b28ccf615ff30c644b0f2bcdf455 100644 (file)
@@ -112,42 +112,6 @@ lab_img_add_modifier(struct lab_img *img,  lab_img_modifier_func_t modifier)
        *mod = modifier;
 }
 
-/*
- * Takes a source surface from PNG/XBM/XPM file and output a buffer for the
- * given size. The source surface is placed at the center of the output buffer
- * and shrunk if it overflows from the output buffer.
- */
-static struct lab_data_buffer *
-render_cairo_surface(cairo_surface_t *surface, int width, int height,
-       double scale)
-{
-       assert(surface);
-       int src_w = cairo_image_surface_get_width(surface);
-       int src_h = cairo_image_surface_get_height(surface);
-
-       struct lab_data_buffer *buffer =
-               buffer_create_cairo(width, height, scale);
-       cairo_t *cairo = cairo_create(buffer->surface);
-
-       struct wlr_box container = {
-               .width = width,
-               .height = height,
-       };
-
-       struct wlr_box dst_box = box_fit_within(src_w, src_h, &container);
-       double scene_scale = (double)dst_box.width / (double)src_w;
-       cairo_translate(cairo, dst_box.x, dst_box.y);
-       cairo_scale(cairo, scene_scale, scene_scale);
-       cairo_set_source_surface(cairo, surface, 0, 0);
-       cairo_pattern_set_filter(cairo_get_source(cairo), CAIRO_FILTER_GOOD);
-       cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
-       cairo_paint(cairo);
-
-       cairo_destroy(cairo);
-
-       return buffer;
-}
-
 struct lab_data_buffer *
 lab_img_render(struct lab_img *img, int width, int height, double scale)
 {
@@ -158,13 +122,11 @@ lab_img_render(struct lab_img *img, int width, int height, double scale)
        case LAB_IMG_PNG:
        case LAB_IMG_XBM:
        case LAB_IMG_XPM:
-               buffer = render_cairo_surface(img->data->buffer->surface,
-                       width, height, scale);
+               buffer = buffer_resize(img->data->buffer, width, height, scale);
                break;
 #if HAVE_RSVG
        case LAB_IMG_SVG:
-               buffer = img_svg_render(img->data->svg, width, height,
-                       scale);
+               buffer = img_svg_render(img->data->svg, width, height, scale);
                break;
 #endif
        default: