Move weston source to compositor/
[platform/upstream/weston.git] / compositor / cms-helper.c
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 <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
40 static void
41 weston_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
57 void
58 weston_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
101 void
102 weston_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
113 struct weston_color_profile *
114 weston_cms_create_profile(const char *filename,
115                           void *lcms_profile)
116 {
117         struct weston_color_profile *p;
118         p = zalloc(sizeof(struct weston_color_profile));
119         p->filename = strdup(filename);
120         p->lcms_handle = lcms_profile;
121         return p;
122 }
123
124 struct weston_color_profile *
125 weston_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 }