blob: 1784c46d36049ff1878045c13dd8b5a80fa8bde3 [file] [log] [blame]
Richard Hughesb24e48e2013-05-09 20:31:09 +01001/*
2 * Copyright © 2013 Richard Hughes
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Richard Hughesb24e48e2013-05-09 20:31:09 +010011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Richard Hughesb24e48e2013-05-09 20:31:09 +010024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Richard Hughesb24e48e2013-05-09 20:31:09 +010027
28#include <stdlib.h>
29#include <string.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Richard Hughesb24e48e2013-05-09 20:31:09 +010031#include <stdio.h>
32
33#ifdef HAVE_LCMS
34#include <lcms2.h>
35#endif
36
37#include "compositor.h"
38#include "cms-helper.h"
39
40#ifdef HAVE_LCMS
41static void
42weston_cms_gamma_clear(struct weston_output *o)
43{
44 int i;
45 uint16_t *red;
46
47 if (!o->set_gamma)
48 return;
49
50 red = calloc(o->gamma_size, sizeof(uint16_t));
51 for (i = 0; i < o->gamma_size; i++)
52 red[i] = (uint32_t) 0xffff * (uint32_t) i / (uint32_t) (o->gamma_size - 1);
53 o->set_gamma(o, o->gamma_size, red, red, red);
54 free(red);
55}
56#endif
57
58void
59weston_cms_set_color_profile(struct weston_output *o,
60 struct weston_color_profile *p)
61{
62#ifdef HAVE_LCMS
63 cmsFloat32Number in;
64 const cmsToneCurve **vcgt;
65 int i;
66 int size;
67 uint16_t *red = NULL;
68 uint16_t *green = NULL;
69 uint16_t *blue = NULL;
70
71 if (!o->set_gamma)
72 return;
73 if (!p) {
74 weston_cms_gamma_clear(o);
75 return;
76 }
77
78 weston_log("Using ICC profile %s\n", p->filename);
79 vcgt = cmsReadTag (p->lcms_handle, cmsSigVcgtTag);
80 if (vcgt == NULL || vcgt[0] == NULL) {
81 weston_cms_gamma_clear(o);
82 return;
83 }
84
85 size = o->gamma_size;
86 red = calloc(size, sizeof(uint16_t));
87 green = calloc(size, sizeof(uint16_t));
88 blue = calloc(size, sizeof(uint16_t));
89 for (i = 0; i < size; i++) {
90 in = (cmsFloat32Number) i / (cmsFloat32Number) (size - 1);
91 red[i] = cmsEvalToneCurveFloat(vcgt[0], in) * (double) 0xffff;
92 green[i] = cmsEvalToneCurveFloat(vcgt[1], in) * (double) 0xffff;
93 blue[i] = cmsEvalToneCurveFloat(vcgt[2], in) * (double) 0xffff;
94 }
95 o->set_gamma(o, size, red, green, blue);
96 free(red);
97 free(green);
98 free(blue);
99#endif
100}
101
102void
103weston_cms_destroy_profile(struct weston_color_profile *p)
104{
105 if (!p)
106 return;
107#ifdef HAVE_LCMS
108 cmsCloseProfile(p->lcms_handle);
109#endif
110 free(p->filename);
111 free(p);
112}
113
114struct weston_color_profile *
115weston_cms_create_profile(const char *filename,
116 void *lcms_profile)
117{
118 struct weston_color_profile *p;
Bryce Harringtonde16d892014-11-20 22:21:57 -0800119 p = zalloc(sizeof(struct weston_color_profile));
Richard Hughesb24e48e2013-05-09 20:31:09 +0100120 p->filename = strdup(filename);
121 p->lcms_handle = lcms_profile;
122 return p;
123}
124
125struct weston_color_profile *
126weston_cms_load_profile(const char *filename)
127{
128 struct weston_color_profile *p = NULL;
129#ifdef HAVE_LCMS
130 cmsHPROFILE lcms_profile;
131 lcms_profile = cmsOpenProfileFromFile(filename, "r");
132 if (lcms_profile)
133 p = weston_cms_create_profile(filename, lcms_profile);
134#endif
135 return p;
136}