blob: c063c773ace4bc36f8a9067937d8a822d7f43dab [file] [log] [blame]
Richard Hughesb24e48e2013-05-09 20:31:09 +01001/*
2 * Copyright © 2013 Richard Hughes
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Richard Hughesb24e48e2013-05-09 20:31:09 +010024
25#include <stdlib.h>
26#include <string.h>
27#include <stdio.h>
28
29#ifdef HAVE_LCMS
30#include <lcms2.h>
31#endif
32
33#include "compositor.h"
34#include "cms-helper.h"
35
36#ifdef HAVE_LCMS
37static void
38weston_cms_gamma_clear(struct weston_output *o)
39{
40 int i;
41 uint16_t *red;
42
43 if (!o->set_gamma)
44 return;
45
46 red = calloc(o->gamma_size, sizeof(uint16_t));
47 for (i = 0; i < o->gamma_size; i++)
48 red[i] = (uint32_t) 0xffff * (uint32_t) i / (uint32_t) (o->gamma_size - 1);
49 o->set_gamma(o, o->gamma_size, red, red, red);
50 free(red);
51}
52#endif
53
54void
55weston_cms_set_color_profile(struct weston_output *o,
56 struct weston_color_profile *p)
57{
58#ifdef HAVE_LCMS
59 cmsFloat32Number in;
60 const cmsToneCurve **vcgt;
61 int i;
62 int size;
63 uint16_t *red = NULL;
64 uint16_t *green = NULL;
65 uint16_t *blue = NULL;
66
67 if (!o->set_gamma)
68 return;
69 if (!p) {
70 weston_cms_gamma_clear(o);
71 return;
72 }
73
74 weston_log("Using ICC profile %s\n", p->filename);
75 vcgt = cmsReadTag (p->lcms_handle, cmsSigVcgtTag);
76 if (vcgt == NULL || vcgt[0] == NULL) {
77 weston_cms_gamma_clear(o);
78 return;
79 }
80
81 size = o->gamma_size;
82 red = calloc(size, sizeof(uint16_t));
83 green = calloc(size, sizeof(uint16_t));
84 blue = calloc(size, sizeof(uint16_t));
85 for (i = 0; i < size; i++) {
86 in = (cmsFloat32Number) i / (cmsFloat32Number) (size - 1);
87 red[i] = cmsEvalToneCurveFloat(vcgt[0], in) * (double) 0xffff;
88 green[i] = cmsEvalToneCurveFloat(vcgt[1], in) * (double) 0xffff;
89 blue[i] = cmsEvalToneCurveFloat(vcgt[2], in) * (double) 0xffff;
90 }
91 o->set_gamma(o, size, red, green, blue);
92 free(red);
93 free(green);
94 free(blue);
95#endif
96}
97
98void
99weston_cms_destroy_profile(struct weston_color_profile *p)
100{
101 if (!p)
102 return;
103#ifdef HAVE_LCMS
104 cmsCloseProfile(p->lcms_handle);
105#endif
106 free(p->filename);
107 free(p);
108}
109
110struct weston_color_profile *
111weston_cms_create_profile(const char *filename,
112 void *lcms_profile)
113{
114 struct weston_color_profile *p;
115 p = calloc(1, sizeof(struct weston_color_profile));
116 p->filename = strdup(filename);
117 p->lcms_handle = lcms_profile;
118 return p;
119}
120
121struct weston_color_profile *
122weston_cms_load_profile(const char *filename)
123{
124 struct weston_color_profile *p = NULL;
125#ifdef HAVE_LCMS
126 cmsHPROFILE lcms_profile;
127 lcms_profile = cmsOpenProfileFromFile(filename, "r");
128 if (lcms_profile)
129 p = weston_cms_create_profile(filename, lcms_profile);
130#endif
131 return p;
132}