babl: fix some annotation to make the function usable in bindings.
[babl.git] / extensions / double.c
blob21fc581156605f8c5b919fef4d5f0db8b59112c2
1 /* babl - dynamically extendable universal pixel conversion library.
2 * Copyright (C) 2012, Øyvind Kolås
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see
16 * <https://www.gnu.org/licenses/>.
19 #include "config.h"
21 #include <stdint.h>
22 #include <stdlib.h>
24 #include "babl-internal.h"
25 #include "babl-cpuaccel.h"
26 #include "extensions/util.h"
27 #include "base/util.h"
30 static void
31 conv_rgbaD_linear_rgbAD_gamma (const Babl *conversion,
32 unsigned char *src,
33 unsigned char *dst,
34 long samples)
36 const Babl *space = babl_conversion_get_destination_space (conversion);
37 const Babl **trc = (void*)space->space.trc;
39 double *fsrc = (double *) src;
40 double *fdst = (double *) dst;
41 int n = samples;
43 while (n--)
45 double alpha = fsrc[3];
46 double used_alpha = babl_epsilon_for_zero (alpha);
47 *fdst++ = babl_trc_from_linear (trc[0], *fsrc++) * used_alpha;
48 *fdst++ = babl_trc_from_linear (trc[1], *fsrc++) * used_alpha;
49 *fdst++ = babl_trc_from_linear (trc[2], *fsrc++) * used_alpha;
50 *fdst++ = alpha;
51 fsrc++;
55 static void
56 conv_rgbAD_linear_rgbAD_gamma (const Babl *conversion,
57 unsigned char *src,
58 unsigned char *dst,
59 long samples)
61 const Babl *space = babl_conversion_get_destination_space (conversion);
62 const Babl **trc = (void*)space->space.trc;
64 double *fsrc = (double *) src;
65 double *fdst = (double *) dst;
66 int n = samples;
68 while (n--)
70 double alpha = fsrc[3];
71 if (alpha == 0.0)
73 *fdst++ = 0.0;
74 *fdst++ = 0.0;
75 *fdst++ = 0.0;
76 *fdst++ = 0.0;
77 fsrc+=4;
79 else
81 double alpha_recip = 1.0 / alpha;
82 *fdst++ = babl_trc_from_linear (trc[0], *fsrc++ * alpha_recip) * alpha;
83 *fdst++ = babl_trc_from_linear (trc[1], *fsrc++ * alpha_recip) * alpha;
84 *fdst++ = babl_trc_from_linear (trc[2], *fsrc++ * alpha_recip) * alpha;
85 *fdst++ = *fsrc++;
90 static void
91 conv_rgbaD_linear_rgbaD_gamma (const Babl *conversion,
92 unsigned char *src,
93 unsigned char *dst,
94 long samples)
96 const Babl *space = babl_conversion_get_destination_space (conversion);
97 const Babl **trc = (void*)space->space.trc;
99 double *fsrc = (double *) src;
100 double *fdst = (double *) dst;
101 int n = samples;
103 while (n--)
105 *fdst++ = babl_trc_from_linear (trc[0], *fsrc++);
106 *fdst++ = babl_trc_from_linear (trc[1], *fsrc++);
107 *fdst++ = babl_trc_from_linear (trc[2], *fsrc++);
108 *fdst++ = *fsrc++;
112 #define conv_rgbaD_linear_rgbD_linear conv_rgbaD_gamma_rgbD_gamma
114 static void
115 conv_rgbaD_linear_rgbD_linear (const Babl *conversion,
116 unsigned char *src,
117 unsigned char *dst,
118 long samples)
120 double *fsrc = (double *) src;
121 double *fdst = (double *) dst;
122 int n = samples;
124 while (n--)
126 *fdst++ = *fsrc++;
127 *fdst++ = *fsrc++;
128 *fdst++ = *fsrc++;
129 fsrc++;
134 static void
135 conv_rgbD_linear_rgbD_gamma (const Babl *conversion,
136 unsigned char *src,
137 unsigned char *dst,
138 long samples)
140 const Babl *space = babl_conversion_get_destination_space (conversion);
141 const Babl **trc = (void*)space->space.trc;
142 double *fsrc = (double *) src;
143 double *fdst = (double *) dst;
144 int n = samples;
146 while (n--)
148 *fdst++ = babl_trc_from_linear (trc[0], *fsrc++);
149 *fdst++ = babl_trc_from_linear (trc[1], *fsrc++);
150 *fdst++ = babl_trc_from_linear (trc[2], *fsrc++);
155 static void
156 conv_rgbaD_gamma_rgbaD_linear (const Babl *conversion,
157 unsigned char *src,
158 unsigned char *dst,
159 long samples)
161 const Babl *space = babl_conversion_get_destination_space (conversion);
162 const Babl **trc = (void*)space->space.trc;
163 double *fsrc = (double *) src;
164 double *fdst = (double *) dst;
165 int n = samples;
167 while (n--)
169 *fdst++ = babl_trc_to_linear (trc[0], *fsrc++);
170 *fdst++ = babl_trc_to_linear (trc[1], *fsrc++);
171 *fdst++ = babl_trc_to_linear (trc[2], *fsrc++);
172 *fdst++ = *fsrc++;
176 static void
177 conv_rgbD_gamma_rgbD_linear (const Babl *conversion,
178 unsigned char *src,
179 unsigned char *dst,
180 long samples)
182 const Babl *space = babl_conversion_get_destination_space (conversion);
183 const Babl **trc = (void*)space->space.trc;
184 double *fsrc = (double *) src;
185 double *fdst = (double *) dst;
186 int n = samples;
188 while (n--)
190 *fdst++ = babl_trc_to_linear (trc[0], *fsrc++);
191 *fdst++ = babl_trc_to_linear (trc[1], *fsrc++);
192 *fdst++ = babl_trc_to_linear (trc[2], *fsrc++);
197 static void
198 conv_rgbD_linear_rgbaD_linear (const Babl *conversion,
199 unsigned char *src,
200 unsigned char *dst,
201 long samples)
203 const Babl *space = babl_conversion_get_destination_space (conversion);
204 const Babl **trc = (void*)space->space.trc;
205 double *fsrc = (double *) src;
206 double *fdst = (double *) dst;
207 int n = samples;
209 while (n--)
211 *fdst++ = babl_trc_to_linear (trc[0], *fsrc++);
212 *fdst++ = babl_trc_to_linear (trc[1], *fsrc++);
213 *fdst++ = babl_trc_to_linear (trc[2], *fsrc++);
214 *fdst++ = 1.0;
219 #define conv_rgbD_gamma_rgbaD_gamma conv_rgbD_linear_rgbaD_linear
221 #define o(src, dst) \
222 babl_conversion_new (src, dst, "linear", conv_ ## src ## _ ## dst, NULL)
224 int init (void);
225 #include "babl-verify-cpu.inc"
228 init (void)
230 BABL_VERIFY_CPU();
232 const Babl *rgbaD_linear = babl_format_new (
233 babl_model ("RGBA"),
234 babl_type ("double"),
235 babl_component ("R"),
236 babl_component ("G"),
237 babl_component ("B"),
238 babl_component ("A"),
239 NULL);
240 const Babl *rgbAD_linear = babl_format_new (
241 babl_model ("RaGaBaA"),
242 babl_type ("double"),
243 babl_component ("Ra"),
244 babl_component ("Ga"),
245 babl_component ("Ba"),
246 babl_component ("A"),
247 NULL);
248 const Babl *rgbaD_gamma = babl_format_new (
249 babl_model ("R'G'B'A"),
250 babl_type ("double"),
251 babl_component ("R'"),
252 babl_component ("G'"),
253 babl_component ("B'"),
254 babl_component ("A"),
255 NULL);
256 const Babl *rgbAD_gamma = babl_format_new (
257 babl_model ("R'aG'aB'aA"),
258 babl_type ("double"),
259 babl_component ("R'a"),
260 babl_component ("G'a"),
261 babl_component ("B'a"),
262 babl_component ("A"),
263 NULL);
264 const Babl *rgbD_linear = babl_format_new (
265 babl_model ("RGB"),
266 babl_type ("double"),
267 babl_component ("R"),
268 babl_component ("G"),
269 babl_component ("B"),
270 NULL);
271 const Babl *rgbD_gamma = babl_format_new (
272 babl_model ("R'G'B'"),
273 babl_type ("double"),
274 babl_component ("R'"),
275 babl_component ("G'"),
276 babl_component ("B'"),
277 NULL);
279 o (rgbAD_linear, rgbAD_gamma);
280 o (rgbaD_linear, rgbAD_gamma);
281 o (rgbaD_linear, rgbaD_gamma);
282 o (rgbaD_gamma, rgbaD_linear);
283 o (rgbD_linear, rgbD_gamma);
284 o (rgbD_gamma, rgbD_linear);
285 o (rgbaD_linear, rgbD_linear);
286 o (rgbaD_gamma, rgbD_gamma);
289 o (rgbD_linear, rgbaD_linear);
290 o (rgbD_gamma, rgbaD_gamma);
291 o (rgbaD_linear, rgbD_linear);
292 o (rgbaD_gamma, rgbD_gamma);
295 return 0;