aboutsummaryrefslogtreecommitdiffstats
path: root/src/gamma-helper.c
blob: 78a38fc72a25cdaddf5deb2b9eca55dc8cbefb42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
 * libgamma — Display server abstraction layer for gamma ramp adjustments
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "gamma-helper.h"

#include "libgamma-method.h"
#include "libgamma-error.h"

#include <errno.h>
#include <stdlib.h>
#include <stdint.h>


#define ANY  bits64

/**
 * Get current the gamma ramps for a CRTC, re-encoding version
 * 
 * @param   this          The CRTC state
 * @param   ramps         The gamma ramps to fill with the current values
 * @param   depth_user    The depth of the gamma ramps that are provided by the user,
 *                        `-1` for `float`, `-2` for `double`
 * @param   depth_system  The depth of the gamma ramps as required by the adjustment method,
 *                        `-1` for `float`, `-2` for `double`
 * @param   fun           Function that is to be used read the ramps, its parameters have
 *                        the same function as those of this function with the same names,
 *                        and the return value too is identical
 * @return                Zero on success, otherwise (negative) the value of an
 *                        error identifier provided by this library
 */
int libgamma_translated_ramp_get_(libgamma_crtc_state_t* restrict this,
				  libgamma_gamma_ramps_any_t* restrict ramps,
				  signed depth_user, signed depth_system,
				  libgamma_get_ramps_any_fun* fun)
{
  size_t n = ramps->ANY.red_size + ramps->ANY.green_size + ramps->ANY.blue_size;
  size_t d, i;
  int r, e;
  libgamma_gamma_ramps_any_t ramps_sys;
  uint64_t* ramps_full;
  
  switch (depth_system)
    {
    case 16:  d = sizeof(uint16_t);  break;
    case 32:  d = sizeof(uint32_t);  break;
    case 64:  d = sizeof(uint64_t);  break;
    case -1:  d = sizeof(float);     break;
    case -2:  d = sizeof(double);    break;
    default:
      return errno = EINVAL, LIBGAMMA_ERRNO_SET;
    }
  
  ramps_sys.ANY.  red_size = ramps->ANY.  red_size;
  ramps_sys.ANY.green_size = ramps->ANY.green_size;
  ramps_sys.ANY. blue_size = ramps->ANY. blue_size;
  
  ramps_sys.ANY.red   = malloc(n * d);
  ramps_sys.ANY.green = (void*)(((char*)(ramps_sys.ANY.  red)) + ramps->ANY.  red_size * d / sizeof(char));
  ramps_sys.ANY.blue  = (void*)(((char*)(ramps_sys.ANY.green)) + ramps->ANY.green_size * d / sizeof(char));
  if (ramps_sys.ANY.red == NULL)
    return LIBGAMMA_ERRNO_SET;
  
  if ((r = fun(this, &ramps_sys)))
    {
      e = errno;
      free(ramps_sys.ANY.red);
      return errno = e, r;
    }
  
  ramps_full = malloc(n * sizeof(uint64_t));
  if (ramps_full)
    {
      e = errno;
      free(ramps_sys.ANY.red);
      return errno = e, r;
    }
  
  switch (depth_system)
    {
    case 16:
      for (i = 0; i < n; i++)
	ramps_full[i] = (uint64_t)(ramps_sys.bits16.red[i]) * 0x0001000100010001ULL;
      break;
      
    case 32:
      for (i = 0; i < n; i++)
	ramps_full[i] = (uint64_t)(ramps_sys.bits32.red[i]) * 0x0000000100000001ULL;
      break;
      
    case 64:
      for (i = 0; i < n; i++)
	ramps_full[i] = ramps_sys.bits64.red[i];
      break;
      
    case -1:
      for (i = 0; i < n; i++)
	ramps_full[i] = (uint64_t)(ramps_sys.float_single.red[i] * (float)UINT64_MAX);
      break;
      
    case -2:
      for (i = 0; i < n; i++)
	ramps_full[i] = (uint64_t)(ramps_sys.float_double.red[i] * (double)UINT64_MAX);
      break;
      
    default: /* This is not possible. */
      abort();
      break;
    }
  
  free(ramps_sys.ANY.red);
  
  if ((0 < depth_user) && (depth_user < 64))
    for (i = 0; i < n; i++)
      {
	ramps_full[i] >>= (64 - depth_user - 1);
	ramps_full[i] = (ramps_full[i] & 1) | (ramps_full[i] >> 1);
      }
  
  switch (depth_user)
    {
    case 16:
      for (i = 0; i < n; i++)
	ramps->bits16.red[i] = (uint16_t)(ramps_full[i]);
      break;
      
    case 32:
      for (i = 0; i < n; i++)
	ramps->bits32.red[i] = (uint32_t)(ramps_full[i]);
      break;
      
    case 64:
      for (i = 0; i < n; i++)
	ramps->bits64.red[i] = ramps_full[i];
      break;
      
    case -1:
      for (i = 0; i < n; i++)
	ramps->float_single.red[i] = (float)(ramps_full[i]) / (float)UINT64_MAX;
      break;
      
    case -2:
      for (i = 0; i < n; i++)
	ramps->float_double.red[i] = (double)(ramps_full[i]) / (double)UINT64_MAX;
      break;
      
    default: /* This is not possible. */
      abort();
      break;
    }
  
  free(ramps_full);
  return 0;
}


/**
 * Set the gamma ramps for a CRTC, re-encoding version
 * 
 * @param   this          The CRTC state
 * @param   ramps         The gamma ramps to apply
 * @param   depth_user    The depth of the gamma ramps that are provided by the user,
 *                        `-1` for `float`, `-2` for `double`
 * @param   depth_system  The depth of the gamma ramps as required by the adjustment method,
 *                        `-1` for `float`, `-2` for `double`
 * @param   fun           Function that is to be used write the ramps, its parameters have
 *                        the same function as those of this function with the same names,
 *                        and the return value too is identical
 * @return                Zero on success, otherwise (negative) the value of an
 *                        error identifier provided by this library
 */
int libgamma_translated_ramp_set_(libgamma_crtc_state_t* restrict this,
				  libgamma_gamma_ramps_any_t ramps,
				  signed depth_user, signed depth_system,
				  libgamma_set_ramps_any_fun* fun)
{
}

#undef ANY