Consolidate image loading code and move to shared/
This commit is contained in:
+2
-2
@@ -16,7 +16,6 @@ weston_LDADD = \
|
||||
weston_SOURCES = \
|
||||
compositor.c \
|
||||
compositor.h \
|
||||
image-loader.c \
|
||||
screenshooter.c \
|
||||
screenshooter-protocol.c \
|
||||
screenshooter-server-protocol.h \
|
||||
@@ -52,7 +51,8 @@ module_LTLIBRARIES = \
|
||||
if ENABLE_X11_COMPOSITOR
|
||||
x11_backend = x11-backend.la
|
||||
x11_backend_la_LDFLAGS = -module -avoid-version
|
||||
x11_backend_la_LIBADD = $(COMPOSITOR_LIBS) $(X11_COMPOSITOR_LIBS)
|
||||
x11_backend_la_LIBADD = $(COMPOSITOR_LIBS) $(X11_COMPOSITOR_LIBS) \
|
||||
../shared/libconfig-parser.la
|
||||
x11_backend_la_CFLAGS = $(X11_COMPOSITOR_CFLAGS) $(GCC_CFLAGS)
|
||||
x11_backend_la_SOURCES = compositor-x11.c
|
||||
endif
|
||||
|
||||
+10
-6
@@ -43,6 +43,7 @@
|
||||
#include <EGL/egl.h>
|
||||
|
||||
#include "compositor.h"
|
||||
#include "../shared/config-parser.h"
|
||||
|
||||
struct x11_compositor {
|
||||
struct weston_compositor base;
|
||||
@@ -310,26 +311,29 @@ static void
|
||||
x11_output_set_icon(struct x11_compositor *c,
|
||||
struct x11_output *output, const char *filename)
|
||||
{
|
||||
uint32_t *icon, *pixels, stride;
|
||||
uint32_t *icon;
|
||||
int32_t width, height;
|
||||
pixman_image_t *image;
|
||||
|
||||
pixels = weston_load_image(filename, &width, &height, &stride);
|
||||
if (!pixels)
|
||||
image = load_image(filename);
|
||||
if (!image)
|
||||
return;
|
||||
width = pixman_image_get_width(image);
|
||||
height = pixman_image_get_height(image);
|
||||
icon = malloc(width * height * 4 + 8);
|
||||
if (!icon) {
|
||||
free(pixels);
|
||||
pixman_image_unref(image);
|
||||
return;
|
||||
}
|
||||
|
||||
icon[0] = width;
|
||||
icon[1] = height;
|
||||
memcpy(icon + 2, pixels, width * height * 4);
|
||||
memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
|
||||
xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
|
||||
c->atom.net_wm_icon, c->atom.cardinal, 32,
|
||||
width * height + 2, icon);
|
||||
free(icon);
|
||||
free(pixels);
|
||||
pixman_image_unref(image);
|
||||
}
|
||||
|
||||
static int
|
||||
|
||||
@@ -1,177 +0,0 @@
|
||||
/*
|
||||
* 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <png.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
WL_EXPORT uint32_t *
|
||||
weston_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;
|
||||
}
|
||||
+22
-2
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "compositor.h"
|
||||
#include "screenshooter-server-protocol.h"
|
||||
@@ -39,6 +40,8 @@ screenshooter_shoot(struct wl_client *client,
|
||||
{
|
||||
struct weston_output *output = output_resource->data;
|
||||
struct wl_buffer *buffer = buffer_resource->data;
|
||||
uint8_t *tmp, *d, *s;
|
||||
int32_t stride, i;
|
||||
|
||||
if (!wl_buffer_is_shm(buffer))
|
||||
return;
|
||||
@@ -47,10 +50,27 @@ screenshooter_shoot(struct wl_client *client,
|
||||
buffer->height < output->current->height)
|
||||
return;
|
||||
|
||||
stride = wl_shm_buffer_get_stride(buffer);
|
||||
tmp = malloc(stride * buffer->height);
|
||||
if (tmp == NULL) {
|
||||
wl_resource_post_no_memory(resource);
|
||||
return;
|
||||
}
|
||||
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
glReadPixels(0, 0, output->current->width, output->current->height,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
wl_shm_buffer_get_data(buffer));
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, tmp);
|
||||
|
||||
d = wl_shm_buffer_get_data(buffer);
|
||||
s = tmp + stride * (buffer->height - 1);
|
||||
|
||||
for (i = 0; i < buffer->height; i++) {
|
||||
memcpy(d, s, stride);
|
||||
d += stride;
|
||||
s -= stride;
|
||||
}
|
||||
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
struct screenshooter_interface screenshooter_implementation = {
|
||||
|
||||
Reference in New Issue
Block a user