From b41d76c15122b2a11baa559e0eb7f7fc5b9f2640 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Sat, 23 Apr 2011 15:03:15 -0400 Subject: [PATCH] compositor: Use libpng directly instead of gdb-pixbuf --- compositor/Makefile.am | 1 + compositor/compositor-x11.c | 12 +-- compositor/compositor.c | 81 ++------------------ compositor/compositor.h | 3 +- compositor/image-loader.c | 176 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 191 insertions(+), 82 deletions(-) create mode 100644 compositor/image-loader.c diff --git a/compositor/Makefile.am b/compositor/Makefile.am index b69807a..a94410c 100644 --- a/compositor/Makefile.am +++ b/compositor/Makefile.am @@ -38,6 +38,7 @@ endif compositor_SOURCES = \ compositor.c \ compositor.h \ + image-loader.c \ shell.c \ switcher.c \ screenshooter.c \ diff --git a/compositor/compositor-x11.c b/compositor/compositor-x11.c index 1996737..dd32061 100644 --- a/compositor/compositor-x11.c +++ b/compositor/compositor-x11.c @@ -240,12 +240,13 @@ struct wm_normal_hints { #define WM_NORMAL_HINTS_MAX_SIZE 32 static void -x11_output_set_icon(struct x11_compositor *c, struct x11_output *output, - const char *filename, int width, int height) +x11_output_set_icon(struct x11_compositor *c, + struct x11_output *output, const char *filename) { - uint32_t *icon, *pixels; + uint32_t *icon, *pixels, stride; + int32_t width, height; - pixels = wlsc_load_image(filename, width, height); + pixels = wlsc_load_image(filename, &width, &height, &stride); if (!pixels) return; icon = malloc(width * height * 4 + 8); @@ -332,8 +333,7 @@ x11_compositor_create_output(struct x11_compositor *c, int width, int height) c->atom.wm_class, c->atom.string, 8, sizeof class, class); - x11_output_set_icon(c, output, - DATADIR "/wayland/wayland.png", 128, 128); + x11_output_set_icon(c, output, DATADIR "/wayland/wayland.png"); xcb_map_window(c->conn, output->window); diff --git a/compositor/compositor.c b/compositor/compositor.c index 4d5f95e..987267e 100644 --- a/compositor/compositor.c +++ b/compositor/compositor.c @@ -260,73 +260,6 @@ destroy_surface(struct wl_resource *resource, struct wl_client *client) free(surface); } -uint32_t * -wlsc_load_image(const char *filename, int width, int height) -{ - GdkPixbuf *pixbuf; - GError *error = NULL; - int stride, i, n_channels; - unsigned char *pixels, *end, *argb_pixels, *s, *d; - - pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, - width, height, - FALSE, &error); - if (error != NULL) { - fprintf(stderr, "failed to load image: %s\n", error->message); - g_error_free(error); - return NULL; - } - - stride = gdk_pixbuf_get_rowstride(pixbuf); - pixels = gdk_pixbuf_get_pixels(pixbuf); - n_channels = gdk_pixbuf_get_n_channels(pixbuf); - - argb_pixels = malloc (height * width * 4); - if (argb_pixels == NULL) { - g_object_unref(pixbuf); - return NULL; - } - - if (n_channels == 4) { - for (i = 0; i < height; i++) { - s = pixels + i * stride; - end = s + width * 4; - d = argb_pixels + i * width * 4; - while (s < end) { - unsigned int t; - -#define MULT(_d,c,a,t) \ - do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0) - - MULT(d[0], s[2], s[3], t); - MULT(d[1], s[1], s[3], t); - MULT(d[2], s[0], s[3], t); - d[3] = s[3]; - s += 4; - d += 4; - } - } - } else if (n_channels == 3) { - for (i = 0; i < height; i++) { - s = pixels + i * stride; - end = s + width * 3; - d = argb_pixels + i * width * 4; - while (s < end) { - d[0] = s[2]; - d[1] = s[1]; - d[2] = s[0]; - d[3] = 0xff; - s += 3; - d += 4; - } - } - } - - g_object_unref(pixbuf); - - return (uint32_t *) argb_pixels; -} - static void wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface) { @@ -393,14 +326,15 @@ enum sprite_usage { static struct wlsc_sprite * create_sprite_from_png(struct wlsc_compositor *ec, - const char *filename, int width, int height, - uint32_t usage) + const char *filename, uint32_t usage) { uint32_t *pixels; struct wlsc_sprite *sprite; + int32_t width, height; + uint32_t stride; - pixels = wlsc_load_image(filename, width, height); - if(pixels == NULL) + pixels = wlsc_load_image(filename, &width, &height, &stride); + if (pixels == NULL) return NULL; sprite = malloc(sizeof *sprite); @@ -459,7 +393,6 @@ static void create_pointer_images(struct wlsc_compositor *ec) { int i, count; - const int width = 32, height = 32; count = ARRAY_LENGTH(pointer_images); ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites); @@ -467,7 +400,6 @@ create_pointer_images(struct wlsc_compositor *ec) ec->pointer_sprites[i] = create_sprite_from_png(ec, pointer_images[i].filename, - width, height, SPRITE_USE_CURSOR); } } @@ -484,8 +416,7 @@ background_create(struct wlsc_output *output, const char *filename) if (background == NULL) return NULL; - sprite = create_sprite_from_png(output->compositor, filename, - output->width, output->height, 0); + sprite = create_sprite_from_png(output->compositor, filename, 0); if (sprite == NULL) { free(background); return NULL; diff --git a/compositor/compositor.h b/compositor/compositor.h index bbbae98..5ae02b5 100644 --- a/compositor/compositor.h +++ b/compositor/compositor.h @@ -357,6 +357,7 @@ void screenshooter_create(struct wlsc_compositor *ec); uint32_t * -wlsc_load_image(const char *filename, int width, int height); +wlsc_load_image(const char *filename, + int32_t *width_arg, int32_t *height_arg, uint32_t *stride_arg); #endif diff --git a/compositor/image-loader.c b/compositor/image-loader.c new file mode 100644 index 0000000..26d116b --- /dev/null +++ b/compositor/image-loader.c @@ -0,0 +1,176 @@ +/* + * Copyright © 2011 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "compositor.h" + +static inline int +multiply_alpha(int alpha, int color) +{ + int temp = (alpha * color) + 0x80; + + return ((temp + (temp >> 8)) >> 8); +} + +static void +premultiply_data(png_structp png, + png_row_infop row_info, + png_bytep data) +{ + unsigned int i; + png_bytep p; + + for (i = 0, p = data; i < row_info->rowbytes; i += 4, p += 4) { + png_byte alpha = p[3]; + uint32_t w; + + if (alpha == 0) { + w = 0; + } else { + png_byte red = p[0]; + png_byte green = p[1]; + png_byte blue = p[2]; + + if (alpha != 0xff) { + red = multiply_alpha(alpha, red); + green = multiply_alpha(alpha, green); + blue = multiply_alpha(alpha, blue); + } + w = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0); + } + + * (uint32_t *) p = w; + } +} + +static void +read_func(png_structp png, png_bytep data, png_size_t size) +{ + FILE *fp = png_get_io_ptr(png); + + if (fread(data, 1, size, fp) < 0) + png_error(png, NULL); +} + +uint32_t * +wlsc_load_image(const char *filename, + int32_t *width_arg, int32_t *height_arg, uint32_t *stride_arg) +{ + png_struct *png; + png_info *info; + png_byte *data; + png_byte **row_pointers = NULL; + png_uint_32 width, height; + int depth, color_type, interlace, stride; + unsigned int i; + FILE *fp; + + fp = fopen(filename, "rb"); + if (fp == NULL) + return NULL; + + png = png_create_read_struct(PNG_LIBPNG_VER_STRING, + NULL, NULL, NULL); + if (!png) { + fclose(fp); + return NULL; + } + + info = png_create_info_struct(png); + if (!info) { + png_destroy_read_struct(&png, &info, NULL); + fclose(fp); + return NULL; + } + + png_set_read_fn(png, fp, read_func); + png_read_info(png, info); + png_get_IHDR(png, info, + &width, &height, &depth, + &color_type, &interlace, NULL, NULL); + + if (color_type == PNG_COLOR_TYPE_PALETTE) + png_set_palette_to_rgb(png); + + if (color_type == PNG_COLOR_TYPE_GRAY) + png_set_expand_gray_1_2_4_to_8(png); + + if (png_get_valid(png, info, PNG_INFO_tRNS)) + png_set_tRNS_to_alpha(png); + + if (depth == 16) + png_set_strip_16(png); + + if (depth < 8) + png_set_packing(png); + + if (color_type == PNG_COLOR_TYPE_GRAY || + color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + png_set_gray_to_rgb(png); + + if (interlace != PNG_INTERLACE_NONE) + png_set_interlace_handling(png); + + png_set_filler(png, 0xff, PNG_FILLER_AFTER); + png_set_read_user_transform_fn(png, premultiply_data); + png_read_update_info(png, info); + png_get_IHDR(png, info, + &width, &height, &depth, + &color_type, &interlace, NULL, NULL); + + stride = width * 4; + data = malloc(stride * height); + if (!data) { + png_destroy_read_struct(&png, &info, NULL); + fclose(fp); + return NULL; + } + + row_pointers = malloc(height * sizeof row_pointers[0]); + if (row_pointers == NULL) { + free(data); + png_destroy_read_struct(&png, &info, NULL); + fclose(fp); + return NULL; + } + + for (i = 0; i < height; i++) + row_pointers[i] = &data[i * stride]; + + png_read_image(png, row_pointers); + png_read_end(png, info); + + free(row_pointers); + png_destroy_read_struct(&png, &info, NULL); + fclose(fp); + + *width_arg = width; + *height_arg = height; + *stride_arg = stride; + + return (uint32_t *) data; +} -- 2.7.4