[PATCH 6/6] image demo client: Add support for color management

Bryce Harrington bryce at osg.samsung.com
Wed Oct 15 22:09:03 PDT 2014


On Mon, Oct 13, 2014 at 07:40:51PM +0200, Niels Ole Salscheider wrote:
> You can now use "C" to switch between the following modes:
> - Assume sRGB input color space
> - Assume that the input color space is the blending color space
> - Attach a false-color ICC profile to the surface
> 
> Signed-off-by: Niels Ole Salscheider <niels_ole at salscheider-online.de>
> ---
>  clients/image.c | 273 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 273 insertions(+)
> 
> diff --git a/clients/image.c b/clients/image.c
> index aee8112..0941125 100644
> --- a/clients/image.c
> +++ b/clients/image.c
> @@ -36,12 +36,219 @@
>  #include <cairo.h>
>  #include <assert.h>
>  #include <linux/input.h>
> +#include <sys/stat.h>
>  
>  #include <wayland-client.h>
>  
>  #include "window.h"
>  #include "../shared/cairo-util.h"
>  
> +#include "cms-client-protocol.h"
> +
> +const unsigned char icc_profile[798] =
> +{
> +    0x00, 0x00, 0x03, 0x1e,
> +    0x6c, 0x63, 0x6d, 0x73,
> +    0x02, 0x30, 0x00, 0x00,
> +    0x6d, 0x6e, 0x74, 0x72,
> +    0x52, 0x47, 0x42, 0x20,
> +    0x58, 0x59, 0x5a, 0x20,
> +    0x07, 0xd8, 0x00, 0x05,
> +    0x00, 0x19, 0x00, 0x05,
> +    0x00, 0x34, 0x00, 0x33,
> +    0x61, 0x63, 0x73, 0x70,
> +    0x41, 0x50, 0x50, 0x4c,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x6c, 0x63, 0x6d, 0x73,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0xf6, 0xd6,
> +    0x00, 0x01, 0x00, 0x00,
> +    0x00, 0x00, 0xd3, 0x2d,
> +    0x6c, 0x63, 0x6d, 0x73,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x0c,
> +    0x64, 0x6d, 0x6e, 0x64,
> +    0x00, 0x00, 0x01, 0x14,
> +    0x00, 0x00, 0x00, 0x6a,
> +    0x64, 0x65, 0x73, 0x63,
> +    0x00, 0x00, 0x01, 0x80,
> +    0x00, 0x00, 0x00, 0x66,
> +    0x64, 0x6d, 0x64, 0x64,
> +    0x00, 0x00, 0x01, 0xe8,
> +    0x00, 0x00, 0x00, 0x67,
> +    0x77, 0x74, 0x70, 0x74,
> +    0x00, 0x00, 0x02, 0x50,
> +    0x00, 0x00, 0x00, 0x14,
> +    0x72, 0x58, 0x59, 0x5a,
> +    0x00, 0x00, 0x02, 0x64,
> +    0x00, 0x00, 0x00, 0x14,
> +    0x62, 0x58, 0x59, 0x5a,
> +    0x00, 0x00, 0x02, 0x78,
> +    0x00, 0x00, 0x00, 0x14,
> +    0x67, 0x58, 0x59, 0x5a,
> +    0x00, 0x00, 0x02, 0x8c,
> +    0x00, 0x00, 0x00, 0x14,
> +    0x72, 0x54, 0x52, 0x43,
> +    0x00, 0x00, 0x02, 0xa0,
> +    0x00, 0x00, 0x00, 0x0e,
> +    0x67, 0x54, 0x52, 0x43,
> +    0x00, 0x00, 0x02, 0xb0,
> +    0x00, 0x00, 0x00, 0x0e,
> +    0x62, 0x54, 0x52, 0x43,
> +    0x00, 0x00, 0x02, 0xc0,
> +    0x00, 0x00, 0x00, 0x0e,
> +    0x63, 0x68, 0x72, 0x6d,
> +    0x00, 0x00, 0x02, 0xd0,
> +    0x00, 0x00, 0x00, 0x24,
> +    0x63, 0x70, 0x72, 0x74,
> +    0x00, 0x00, 0x02, 0xf4,
> +    0x00, 0x00, 0x00, 0x2a,
> +    0x64, 0x65, 0x73, 0x63,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x10,
> +    0x28, 0x6c, 0x63, 0x6d,
> +    0x73, 0x20, 0x69, 0x6e,
> +    0x74, 0x65, 0x72, 0x6e,
> +    0x61, 0x6c, 0x29, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x64, 0x65, 0x73, 0x63,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x0c,
> +    0x66, 0x61, 0x6b, 0x65,
> +    0x42, 0x52, 0x47, 0x2e,
> +    0x69, 0x63, 0x63, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x64, 0x65, 0x73, 0x63,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x0d,
> +    0x72, 0x67, 0x62, 0x20,
> +    0x62, 0x75, 0x69, 0x6c,
> +    0x74, 0x2d, 0x69, 0x6e,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x58, 0x59, 0x5a, 0x20,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0xf3, 0x4f,
> +    0x00, 0x01, 0x00, 0x00,
> +    0x00, 0x01, 0x16, 0xc2,
> +    0x58, 0x59, 0x5a, 0x20,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x24, 0x9e,
> +    0x00, 0x00, 0x0f, 0x84,
> +    0x00, 0x00, 0xb6, 0xc2,
> +    0x58, 0x59, 0x5a, 0x20,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x62, 0x97,
> +    0x00, 0x00, 0xb7, 0x88,
> +    0x00, 0x00, 0x18, 0xda,
> +    0x58, 0x59, 0x5a, 0x20,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x6f, 0xa0,
> +    0x00, 0x00, 0x38, 0xf5,
> +    0x00, 0x00, 0x03, 0x90,
> +    0x63, 0x75, 0x72, 0x76,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x01,
> +    0x02, 0x33, 0x00, 0x00,
> +    0x63, 0x75, 0x72, 0x76,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x01,
> +    0x02, 0x33, 0x00, 0x00,
> +    0x63, 0x75, 0x72, 0x76,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x00, 0x00, 0x01,
> +    0x02, 0x33, 0x00, 0x00,
> +    0x63, 0x68, 0x72, 0x6d,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x00, 0x03, 0x00, 0x00,
> +    0x00, 0x00, 0x26, 0x66,
> +    0x00, 0x00, 0x0f, 0x5c,
> +    0x00, 0x00, 0xa3, 0xd7,
> +    0x00, 0x00, 0x54, 0x7b,
> +    0x00, 0x00, 0x4c, 0xcd,
> +    0x00, 0x00, 0x99, 0x9a,
> +    0x74, 0x65, 0x78, 0x74,
> +    0x00, 0x00, 0x00, 0x00,
> +    0x32, 0x30, 0x30, 0x38,
> +    0x20, 0x4b, 0x61, 0x69,
> +    0x2d, 0x55, 0x77, 0x65,
> +    0x20, 0x42, 0x65, 0x68,
> +    0x72, 0x6d, 0x61, 0x6e,
> +    0x6e, 0x2c, 0x20, 0x75,
> +    0x73, 0x65, 0x20, 0x66,
> +    0x72, 0x65, 0x65, 0x6c,
> +    0x79, 0x00
> +};
> +
>  struct image {
>  	struct window *window;
>  	struct widget *widget;
> @@ -60,6 +267,12 @@ struct image {
>  
>  	bool initialized;
>  	cairo_matrix_t matrix;
> +
> +	int color_mode;
> +	struct wl_cms *cms;
> +	struct wl_cms_colorspace *profile_colorspace;
> +	struct wl_cms_colorspace *srgb_colorspace;
> +	struct wl_cms_colorspace *blending_colorspace;
>  };
>  
>  static double
> @@ -305,6 +518,30 @@ key_handler(struct window *window, struct input *input, uint32_t time,
>  		clamp_view(image);
>  		window_schedule_redraw(image->window);
>  		break;
> +	case XKB_KEY_C:
> +		if (image->cms) {
> +			struct wl_surface *surface;
> +			surface = window_get_wl_surface(image->window);
> +
> +			image->color_mode = (image->color_mode + 1) % 3;
> +			switch (image->color_mode) {
> +			case 0:
> +				wl_cms_set_colorspace(image->cms, surface,
> +						      image->srgb_colorspace);
> +				break;
> +			case 1:
> +				wl_cms_set_colorspace(image->cms, surface,
> +						      image->blending_colorspace);
> +				break;
> +			case 2:
> +				wl_cms_set_colorspace(image->cms, surface,
> +						      image->profile_colorspace);
> +				break;
> +			}
> +
> +			window_schedule_redraw(image->window);
> +		}
> +		break;
>  	}
>  }
>  
> @@ -353,12 +590,28 @@ close_handler(void *data)
>  	free(image);
>  }
>  
> +static void
> +global_handler(struct display *display, uint32_t name,
> +	       const char *interface, uint32_t version, void *data)
> +{
> +	struct image *image = data;
> +
> +	if (strcmp(interface, "wl_cms") == 0) {
> +		image->cms =
> +			display_bind(display, name,
> +				     &wl_cms_interface, 1);
> +	}
> +}
> +
>  static struct image *
>  image_create(struct display *display, const char *filename,
>  	     int *image_counter)
>  {
>  	struct image *image;
>  	char *b, *copy, title[512];;
> +	int fd;
> +	FILE *profilefile;
> +	char template[24];
>  
>  	image = zalloc(sizeof *image);
>  	if (image == NULL)
> @@ -401,6 +654,26 @@ image_create(struct display *display, const char *filename,
>  	window_set_key_handler(image->window, key_handler);
>  	widget_schedule_resize(image->widget, 500, 400);
>  
> +	display_set_user_data(display, image);
> +	display_set_global_handler(display, global_handler);
> +
> +	if (image->cms) {
> +		image->color_mode = 0;
> +		image->srgb_colorspace = wl_cms_srgb_colorspace(image->cms);
> +		image->blending_colorspace =
> +			wl_cms_blending_colorspace(image->cms);
> +
> +		strncpy(template, "/tmp/weston-cms-XXXXXX", 24);

sizeof(template) instead of the 24?  Or at least use a constant instead
of the 24.

> +		fd = mkstemp(template);
> +		unlink(template);
> +		profilefile = fdopen(fd, "w");
> +		fwrite(icc_profile, sizeof(icc_profile), 1, profilefile);
> +		rewind(profilefile);
> +		image->profile_colorspace =
> +			wl_cms_colorspace_from_fd(image->cms, fd);
> +		close(fd);

error checking needed in above routines.

> +	}
> +
>  	return image;
>  }
>  
> -- 
> 2.1.2
> 
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel


More information about the wayland-devel mailing list