1 /*
2 * Copyright © 2013 Richard Hughes
3 *
4 * 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:
11 *
12 * 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.
24 */
25
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <stdio.h>
32
33 #ifdef HAVE_LCMS
34 #include <lcms2.h>
35 #endif
36
37 #include <libweston/libweston.h>
38 #include "cms-helper.h"
39
40 #ifdef HAVE_LCMS
41 static void
weston_cms_gamma_clear(struct weston_output * o)42 weston_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
58 void
weston_cms_set_color_profile(struct weston_output * o,struct weston_color_profile * p)59 weston_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
102 void
weston_cms_destroy_profile(struct weston_color_profile * p)103 weston_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
114 struct weston_color_profile *
weston_cms_create_profile(const char * filename,void * lcms_profile)115 weston_cms_create_profile(const char *filename,
116 void *lcms_profile)
117 {
118 struct weston_color_profile *p;
119 p = zalloc(sizeof(struct weston_color_profile));
120 p->filename = strdup(filename);
121 p->lcms_handle = lcms_profile;
122 return p;
123 }
124
125 struct weston_color_profile *
weston_cms_load_profile(const char * filename)126 weston_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 }
137