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