blob: 7b7b8aad9158212309cc20362dca956018f681df [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>
30#include <stdio.h>
31
32#ifdef HAVE_LCMS
33#include <lcms2.h>
34#endif
35
36#include "compositor.h"
37#include "cms-helper.h"
38
39#ifdef HAVE_LCMS
40static void
41weston_cms_gamma_clear(struct weston_output *o)
42{
43 int i;
44 uint16_t *red;
45
46 if (!o->set_gamma)
47 return;
48
49 red = calloc(o->gamma_size, sizeof(uint16_t));
50 for (i = 0; i < o->gamma_size; i++)
51 red[i] = (uint32_t) 0xffff * (uint32_t) i / (uint32_t) (o->gamma_size - 1);
52 o->set_gamma(o, o->gamma_size, red, red, red);
53 free(red);
54}
55#endif
56
57void
58weston_cms_set_color_profile(struct weston_output *o,
59 struct weston_color_profile *p)
60{
61#ifdef HAVE_LCMS
62 cmsFloat32Number in;
63 const cmsToneCurve **vcgt;
64 int i;
65 int size;
66 uint16_t *red = NULL;
67 uint16_t *green = NULL;
68 uint16_t *blue = NULL;
69
70 if (!o->set_gamma)
71 return;
72 if (!p) {
73 weston_cms_gamma_clear(o);
74 return;
75 }
76
77 weston_log("Using ICC profile %s\n", p->filename);
78 vcgt = cmsReadTag (p->lcms_handle, cmsSigVcgtTag);
79 if (vcgt == NULL || vcgt[0] == NULL) {
80 weston_cms_gamma_clear(o);
81 return;
82 }
83
84 size = o->gamma_size;
85 red = calloc(size, sizeof(uint16_t));
86 green = calloc(size, sizeof(uint16_t));
87 blue = calloc(size, sizeof(uint16_t));
88 for (i = 0; i < size; i++) {
89 in = (cmsFloat32Number) i / (cmsFloat32Number) (size - 1);
90 red[i] = cmsEvalToneCurveFloat(vcgt[0], in) * (double) 0xffff;
91 green[i] = cmsEvalToneCurveFloat(vcgt[1], in) * (double) 0xffff;
92 blue[i] = cmsEvalToneCurveFloat(vcgt[2], in) * (double) 0xffff;
93 }
94 o->set_gamma(o, size, red, green, blue);
95 free(red);
96 free(green);
97 free(blue);
98#endif
99}
100
101void
102weston_cms_destroy_profile(struct weston_color_profile *p)
103{
104 if (!p)
105 return;
106#ifdef HAVE_LCMS
107 cmsCloseProfile(p->lcms_handle);
108#endif
109 free(p->filename);
110 free(p);
111}
112
113struct weston_color_profile *
114weston_cms_create_profile(const char *filename,
115 void *lcms_profile)
116{
117 struct weston_color_profile *p;
Bryce Harringtonde16d892014-11-20 22:21:57 -0800118 p = zalloc(sizeof(struct weston_color_profile));
Richard Hughesb24e48e2013-05-09 20:31:09 +0100119 p->filename = strdup(filename);
120 p->lcms_handle = lcms_profile;
121 return p;
122}
123
124struct weston_color_profile *
125weston_cms_load_profile(const char *filename)
126{
127 struct weston_color_profile *p = NULL;
128#ifdef HAVE_LCMS
129 cmsHPROFILE lcms_profile;
130 lcms_profile = cmsOpenProfileFromFile(filename, "r");
131 if (lcms_profile)
132 p = weston_cms_create_profile(filename, lcms_profile);
133#endif
134 return p;
135}