2 // Copyright (C) 2009 Mozilla Foundation
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the Software
9 // is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
16 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #define _ISOC99_SOURCE /* for INFINITY */
26 #include <string.h> //memcpy
28 #include "transform_util.h"
31 #if !defined(INFINITY)
32 #define INFINITY HUGE_VAL
35 #define PARAMETRIC_CURVE_TYPE 0x70617261 //'para'
37 /* value must be a value between 0 and 1 */
38 //XXX: is the above a good restriction to have?
39 // the output range of this function is 0..1
40 float lut_interp_linear(double input_value
, uint16_t *table
, size_t length
)
44 input_value
= input_value
* (length
- 1); // scale to length of the array
45 upper
= ceil(input_value
);
46 lower
= floor(input_value
);
47 //XXX: can we be more performant here?
48 value
= table
[upper
]*(1. - (upper
- input_value
)) + table
[lower
]*(upper
- input_value
);
50 return value
* (1.f
/65535.f
);
53 /* same as above but takes and returns a uint16_t value representing a range from 0..1 */
54 uint16_t lut_interp_linear16(uint16_t input_value
, uint16_t *table
, size_t length
)
56 /* Start scaling input_value to the length of the array: 65535*(length-1).
57 * We'll divide out the 65535 next */
58 uintptr_t value
= (input_value
* (length
- 1));
59 uint32_t upper
= (value
+ 65534) / 65535; /* equivalent to ceil(value/65535) */
60 uint32_t lower
= value
/ 65535; /* equivalent to floor(value/65535) */
61 /* interp is the distance from upper to value scaled to 0..65535 */
62 uint32_t interp
= value
% 65535;
64 value
= (table
[upper
]*(interp
) + table
[lower
]*(65535 - interp
))/65535; // 0..65535*65535
69 /* same as above but takes an input_value from 0..PRECACHE_OUTPUT_MAX
70 * and returns a uint8_t value representing a range from 0..1 */
72 uint8_t lut_interp_linear_precache_output(uint32_t input_value
, uint16_t *table
, size_t length
)
74 /* Start scaling input_value to the length of the array: PRECACHE_OUTPUT_MAX*(length-1).
75 * We'll divide out the PRECACHE_OUTPUT_MAX next */
76 uintptr_t value
= (input_value
* (length
- 1));
78 /* equivalent to ceil(value/PRECACHE_OUTPUT_MAX) */
79 uint32_t upper
= (value
+ PRECACHE_OUTPUT_MAX
-1) / PRECACHE_OUTPUT_MAX
;
80 /* equivalent to floor(value/PRECACHE_OUTPUT_MAX) */
81 uint32_t lower
= value
/ PRECACHE_OUTPUT_MAX
;
82 /* interp is the distance from upper to value scaled to 0..PRECACHE_OUTPUT_MAX */
83 uint32_t interp
= value
% PRECACHE_OUTPUT_MAX
;
85 /* the table values range from 0..65535 */
86 value
= (table
[upper
]*(interp
) + table
[lower
]*(PRECACHE_OUTPUT_MAX
- interp
)); // 0..(65535*PRECACHE_OUTPUT_MAX)
89 value
+= (PRECACHE_OUTPUT_MAX
*65535/255)/2;
90 value
/= (PRECACHE_OUTPUT_MAX
*65535/255); // scale to 0..255
94 /* value must be a value between 0 and 1 */
95 //XXX: is the above a good restriction to have?
96 float lut_interp_linear_float(float value
, float *table
, size_t length
)
99 value
= value
* (length
- 1);
101 lower
= floor(value
);
102 //XXX: can we be more performant here?
103 value
= table
[upper
]*(1. - (upper
- value
)) + table
[lower
]*(upper
- value
);
104 /* scale the value */
109 /* if we use a different representation i.e. one that goes from 0 to 0x1000 we can be more efficient
110 * because we can avoid the divisions and use a shifting instead */
111 /* same as above but takes and returns a uint16_t value representing a range from 0..1 */
112 uint16_t lut_interp_linear16(uint16_t input_value
, uint16_t *table
, int length
)
114 uint32_t value
= (input_value
* (length
- 1));
115 uint32_t upper
= (value
+ 4095) / 4096; /* equivalent to ceil(value/4096) */
116 uint32_t lower
= value
/ 4096; /* equivalent to floor(value/4096) */
117 uint32_t interp
= value
% 4096;
119 value
= (table
[upper
]*(interp
) + table
[lower
]*(4096 - interp
))/4096; // 0..4096*4096
125 void compute_curve_gamma_table_type1(float gamma_table
[256], uint16_t gamma
)
128 float gamma_float
= u8Fixed8Number_to_float(gamma
);
129 for (i
= 0; i
< 256; i
++) {
130 // 0..1^(0..255 + 255/256) will always be between 0 and 1
131 gamma_table
[i
] = pow(i
/255., gamma_float
);
135 void compute_curve_gamma_table_type2(float gamma_table
[256], uint16_t *table
, size_t length
)
138 for (i
= 0; i
< 256; i
++) {
139 gamma_table
[i
] = lut_interp_linear(i
/255., table
, length
);
143 void compute_curve_gamma_table_type_parametric(float gamma_table
[256], float parameter
[7], int count
)
148 float y
= parameter
[0];
155 interval
= -INFINITY
;
156 } else if(count
== 1) {
162 interval
= -1 * parameter
[2] / parameter
[1];
163 } else if(count
== 2) {
169 interval
= -1 * parameter
[2] / parameter
[1];
170 } else if(count
== 3) {
176 interval
= parameter
[4];
177 } else if(count
== 4) {
181 e
= parameter
[5] - c
;
183 interval
= parameter
[4];
185 assert(0 && "invalid parametric function type.");
191 interval
= -INFINITY
;
193 for (X
= 0; X
< 256; X
++) {
195 // XXX The equations are not exactly as definied in the spec but are
196 // algebraic equivilent.
197 // TODO Should division by 255 be for the whole expression.
198 gamma_table
[X
] = clamp_float(pow(a
* X
/ 255. + b
, y
) + c
+ e
);
200 gamma_table
[X
] = clamp_float(c
* X
/ 255. + f
);
205 void compute_curve_gamma_table_type0(float gamma_table
[256])
208 for (i
= 0; i
< 256; i
++) {
209 gamma_table
[i
] = i
/255.;
213 float clamp_float(float a
)
215 /* One would naturally write this function as the following:
223 However, that version will let NaNs pass through which is undesirable
231 else // a < 0 or a is NaN
235 unsigned char clamp_u8(float v
)
245 float u8Fixed8Number_to_float(uint16_t x
)
249 // 0xffff = 255 + 255/256
253 /* The SSE2 code uses min & max which let NaNs pass through.
254 We want to try to prevent that here by ensuring that
255 gamma table is within expected values. */
256 void validate_gamma_table(float gamma_table
[256])
259 for (i
= 0; i
< 256; i
++) {
260 // Note: we check that the gamma is not in range
261 // instead of out of range so that we catch NaNs
262 if (!(gamma_table
[i
] >= 0.f
&& gamma_table
[i
] <= 1.f
)) {
263 gamma_table
[i
] = 0.f
;
268 float *build_input_gamma_table(struct curveType
*TRC
)
272 if (!TRC
) return NULL
;
273 gamma_table
= malloc(sizeof(float)*256);
275 if (TRC
->type
== PARAMETRIC_CURVE_TYPE
) {
276 compute_curve_gamma_table_type_parametric(gamma_table
, TRC
->parameter
, TRC
->count
);
278 if (TRC
->count
== 0) {
279 compute_curve_gamma_table_type0(gamma_table
);
280 } else if (TRC
->count
== 1) {
281 compute_curve_gamma_table_type1(gamma_table
, TRC
->data
[0]);
283 compute_curve_gamma_table_type2(gamma_table
, TRC
->data
, TRC
->count
);
288 validate_gamma_table(gamma_table
);
293 struct matrix
build_colorant_matrix(qcms_profile
*p
)
295 struct matrix result
;
296 result
.m
[0][0] = s15Fixed16Number_to_float(p
->redColorant
.X
);
297 result
.m
[0][1] = s15Fixed16Number_to_float(p
->greenColorant
.X
);
298 result
.m
[0][2] = s15Fixed16Number_to_float(p
->blueColorant
.X
);
299 result
.m
[1][0] = s15Fixed16Number_to_float(p
->redColorant
.Y
);
300 result
.m
[1][1] = s15Fixed16Number_to_float(p
->greenColorant
.Y
);
301 result
.m
[1][2] = s15Fixed16Number_to_float(p
->blueColorant
.Y
);
302 result
.m
[2][0] = s15Fixed16Number_to_float(p
->redColorant
.Z
);
303 result
.m
[2][1] = s15Fixed16Number_to_float(p
->greenColorant
.Z
);
304 result
.m
[2][2] = s15Fixed16Number_to_float(p
->blueColorant
.Z
);
305 result
.invalid
= false;
309 /* The following code is copied nearly directly from lcms.
310 * I think it could be much better. For example, Argyll seems to have better code in
311 * icmTable_lookup_bwd and icmTable_setup_bwd. However, for now this is a quick way
312 * to a working solution and allows for easy comparing with lcms. */
313 uint16_fract_t
lut_inverse_interp16(uint16_t Value
, uint16_t LutTable
[], int length
)
317 int x
= 0, res
; // 'int' Give spacing for negative values
318 int NumZeroes
, NumPoles
;
321 double y0
, y1
, x0
, x1
;
324 // July/27 2001 - Expanded to handle degenerated curves with an arbitrary
325 // number of elements containing 0 at the begining of the table (Zeroes)
326 // and another arbitrary number of poles (FFFFh) at the end.
327 // First the zero and pole extents are computed, then value is compared.
330 while (LutTable
[NumZeroes
] == 0 && NumZeroes
< length
-1)
333 // There are no zeros at the beginning and we are trying to find a zero, so
334 // return anything. It seems zero would be the less destructive choice
335 /* I'm not sure that this makes sense, but oh well... */
336 if (NumZeroes
== 0 && Value
== 0)
340 while (LutTable
[length
-1- NumPoles
] == 0xFFFF && NumPoles
< length
-1)
343 // Does the curve belong to this case?
344 if (NumZeroes
> 1 || NumPoles
> 1)
348 // Identify if value fall downto 0 or FFFF zone
349 if (Value
== 0) return 0;
350 // if (Value == 0xFFFF) return 0xFFFF;
351 sample
= (length
-1) * ((double) Value
* (1./65535.));
352 if (LutTable
[sample
] == 0xffff)
355 // else restrict to valid zone
357 a
= ((NumZeroes
-1) * 0xFFFF) / (length
-1);
358 b
= ((length
-1 - NumPoles
) * 0xFFFF) / (length
-1);
363 // Ensure a valid binary search range
370 // If the search range is inverted due to degeneracy,
371 // deem LutTable non-invertible in this search range.
372 // Refer to https://bugzil.la/1132467
378 // For input 0, return that to maintain black level. Note the binary search
379 // does not. For example, it inverts the standard sRGB gamma curve to 7 at
380 // the origin, causing a black level error.
382 if (Value
== 0 && NumZeroes
) {
386 // Seems not a degenerated case... apply binary search
392 res
= (int) lut_interp_linear16((uint16_fract_t
) (x
-1), LutTable
, length
);
396 // Found exact match.
398 return (uint16_fract_t
) (x
- 1);
401 if (res
> Value
) r
= x
- 1;
405 // Not found, should we interpolate?
407 // Get surrounding nodes
411 val2
= (length
-1) * ((double) (x
- 1) / 65535.0);
413 cell0
= (int) floor(val2
);
414 cell1
= (int) ceil(val2
);
418 assert(cell0
< length
);
419 assert(cell1
< length
);
421 if (cell0
== cell1
) return (uint16_fract_t
) x
;
423 y0
= LutTable
[cell0
] ;
424 x0
= (65535.0 * cell0
) / (length
-1);
426 y1
= LutTable
[cell1
] ;
427 x1
= (65535.0 * cell1
) / (length
-1);
429 a
= (y1
- y0
) / (x1
- x0
);
432 if (fabs(a
) < 0.01) return (uint16_fract_t
) x
;
434 f
= ((Value
- b
) / a
);
436 if (f
< 0.0) return (uint16_fract_t
) 0;
437 if (f
>= 65535.0) return (uint16_fract_t
) 0xFFFF;
439 return (uint16_fract_t
) floor(f
+ 0.5);
443 The number of entries needed to invert a lookup table should not
444 necessarily be the same as the original number of entries. This is
445 especially true of lookup tables that have a small number of entries.
449 {0, 3104, 14263, 34802, 65535}
450 invert_lut will produce an inverse of:
451 {3, 34459, 47529, 56801, 65535}
452 which has an maximum error of about 9855 (pixel difference of ~38.346)
454 For now, we punt the decision of output size to the caller. */
455 static uint16_t *invert_lut(uint16_t *table
, int length
, size_t out_length
)
458 /* for now we invert the lut by creating a lut of size out_length
459 * and attempting to lookup a value for each entry using lut_inverse_interp16 */
460 uint16_t *output
= malloc(sizeof(uint16_t)*out_length
);
464 for (i
= 0; i
< out_length
; i
++) {
465 double x
= ((double) i
* 65535.) / (double) (out_length
- 1);
466 uint16_fract_t input
= floor(x
+ .5);
467 output
[i
] = lut_inverse_interp16(input
, table
, length
);
472 static void compute_precache_pow(uint8_t *output
, float gamma
)
475 for (v
= 0; v
< PRECACHE_OUTPUT_SIZE
; v
++) {
476 //XXX: don't do integer/float conversion... and round?
477 output
[v
] = 255. * pow(v
/(double)PRECACHE_OUTPUT_MAX
, gamma
);
481 void compute_precache_lut(uint8_t *output
, uint16_t *table
, int length
)
484 for (v
= 0; v
< PRECACHE_OUTPUT_SIZE
; v
++) {
485 output
[v
] = lut_interp_linear_precache_output(v
, table
, length
);
489 void compute_precache_linear(uint8_t *output
)
492 for (v
= 0; v
< PRECACHE_OUTPUT_SIZE
; v
++) {
494 output
[v
] = v
/ (PRECACHE_OUTPUT_SIZE
/256);
498 qcms_bool
compute_precache(struct curveType
*trc
, uint8_t *output
)
501 if (trc
->type
== PARAMETRIC_CURVE_TYPE
) {
502 float gamma_table
[256];
503 uint16_t gamma_table_uint
[256];
506 int inverted_size
= 256;
508 compute_curve_gamma_table_type_parametric(gamma_table
, trc
->parameter
, trc
->count
);
509 for(i
= 0; i
< 256; i
++) {
510 gamma_table_uint
[i
] = (uint16_t)(gamma_table
[i
] * 65535);
513 //XXX: the choice of a minimum of 256 here is not backed by any theory,
514 // measurement or data, howeve r it is what lcms uses.
515 // the maximum number we would need is 65535 because that's the
516 // accuracy used for computing the pre cache table
517 if (inverted_size
< 256)
520 inverted
= invert_lut(gamma_table_uint
, 256, inverted_size
);
523 compute_precache_lut(output
, inverted
, inverted_size
);
526 if (trc
->count
== 0) {
527 compute_precache_linear(output
);
528 } else if (trc
->count
== 1) {
529 compute_precache_pow(output
, 1./u8Fixed8Number_to_float(trc
->data
[0]));
532 int inverted_size
= trc
->count
;
533 //XXX: the choice of a minimum of 256 here is not backed by any theory,
534 // measurement or data, howeve r it is what lcms uses.
535 // the maximum number we would need is 65535 because that's the
536 // accuracy used for computing the pre cache table
537 if (inverted_size
< 256)
540 inverted
= invert_lut(trc
->data
, trc
->count
, inverted_size
);
543 compute_precache_lut(output
, inverted
, inverted_size
);
551 static uint16_t *build_linear_table(int length
)
554 uint16_t *output
= malloc(sizeof(uint16_t)*length
);
558 for (i
= 0; i
< length
; i
++) {
559 double x
= ((double) i
* 65535.) / (double) (length
- 1);
560 uint16_fract_t input
= floor(x
+ .5);
566 static uint16_t *build_pow_table(float gamma
, int length
)
569 uint16_t *output
= malloc(sizeof(uint16_t)*length
);
573 for (i
= 0; i
< length
; i
++) {
574 uint16_fract_t result
;
575 double x
= ((double) i
) / (double) (length
- 1);
576 x
= pow(x
, gamma
); //XXX turn this conversion into a function
577 result
= floor(x
*65535. + .5);
583 void build_output_lut(struct curveType
*trc
,
584 uint16_t **output_gamma_lut
, size_t *output_gamma_lut_length
)
586 if (trc
->type
== PARAMETRIC_CURVE_TYPE
) {
587 float gamma_table
[256];
589 uint16_t *output
= malloc(sizeof(uint16_t)*256);
592 *output_gamma_lut
= NULL
;
596 compute_curve_gamma_table_type_parametric(gamma_table
, trc
->parameter
, trc
->count
);
597 *output_gamma_lut_length
= 256;
598 for(i
= 0; i
< 256; i
++) {
599 output
[i
] = (uint16_t)(gamma_table
[i
] * 65535);
601 *output_gamma_lut
= output
;
603 if (trc
->count
== 0) {
604 *output_gamma_lut
= build_linear_table(4096);
605 *output_gamma_lut_length
= 4096;
606 } else if (trc
->count
== 1) {
607 float gamma
= 1./u8Fixed8Number_to_float(trc
->data
[0]);
608 *output_gamma_lut
= build_pow_table(gamma
, 4096);
609 *output_gamma_lut_length
= 4096;
611 //XXX: the choice of a minimum of 256 here is not backed by any theory,
612 // measurement or data, however it is what lcms uses.
613 *output_gamma_lut_length
= trc
->count
;
614 if (*output_gamma_lut_length
< 256)
615 *output_gamma_lut_length
= 256;
617 *output_gamma_lut
= invert_lut(trc
->data
, trc
->count
, *output_gamma_lut_length
);