1 diff --git a/third_party/qcms/src/iccread.c b/third_party/qcms/src/iccread.c
2 index 36b7011..9ee6b94 100644
3 --- a/third_party/qcms/src/iccread.c
4 +++ b/third_party/qcms/src/iccread.c
5 @@ -266,7 +266,7 @@ qcms_bool qcms_profile_is_bogus(qcms_profile *profile)
6 if (profile->color_space != RGB_SIGNATURE)
9 - if (profile->A2B0 || profile->B2A0)
10 + if (qcms_supports_iccv4 && (profile->A2B0 || profile->B2A0))
13 rX = s15Fixed16Number_to_float(profile->redColorant.X);
14 @@ -297,6 +297,11 @@ qcms_bool qcms_profile_is_bogus(qcms_profile *profile)
15 sum[1] = rY + gY + bY;
16 sum[2] = rZ + gZ + bZ;
18 +#if defined (_MSC_VER)
19 +#pragma warning(push)
20 +/* Disable double to float truncation warning 4305 */
21 +#pragma warning(disable:4305)
23 // Build our target vector (see mozilla bug 460629)
26 @@ -310,6 +315,10 @@ qcms_bool qcms_profile_is_bogus(qcms_profile *profile)
30 +#if defined (_MSC_VER)
31 +/* Restore warnings */
34 // Compare with our tolerance
35 for (i = 0; i < 3; ++i) {
36 if (!(((sum[i] - tolerance[i]) <= target[i]) &&
37 @@ -402,7 +411,7 @@ static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_inde
38 // present that are not part of the tag_index.
39 static struct curveType *read_curveType(struct mem_source *src, uint32_t offset, uint32_t *len)
41 - static const size_t COUNT_TO_LENGTH[5] = {1, 3, 4, 5, 7};
42 + static const uint32_t COUNT_TO_LENGTH[5] = {1, 3, 4, 5, 7};
43 struct curveType *curve = NULL;
44 uint32_t type = read_u32(src, offset);
46 @@ -657,7 +666,7 @@ static struct lutType *read_tag_lutType(struct mem_source *src, struct tag_index
47 uint16_t num_input_table_entries;
48 uint16_t num_output_table_entries;
49 uint8_t in_chan, grid_points, out_chan;
50 - uint32_t clut_offset, output_offset;
51 + size_t clut_offset, output_offset;
55 @@ -997,6 +1006,9 @@ qcms_profile* qcms_profile_from_memory(const void *mem, size_t size)
60 + return INVALID_PROFILE;
62 length = read_u32(src, 0);
64 // shrink the area that we can read if appropriate
65 diff --git a/third_party/qcms/src/qcms.h b/third_party/qcms/src/qcms.h
66 index 7d83623..11fe222 100644
67 --- a/third_party/qcms/src/qcms.h
68 +++ b/third_party/qcms/src/qcms.h
69 @@ -40,6 +40,12 @@ sale, use or other dealings in this Software without written
70 authorization from SunSoft Inc.
71 ******************************************************************/
74 + * QCMS, in general, is not threadsafe. However, it should be safe to create
75 + * profile and transformation objects on different threads, so long as you
76 + * don't use the same objects on different threads at the same time.
80 * Color Space Signatures
81 * Note that only icSigXYZData and icSigLabData are valid
82 @@ -102,6 +108,12 @@ typedef enum {
86 +/* Format of the output data for qcms_transform_data_type() */
92 /* the names for the following two types are sort of ugly */
95 @@ -146,6 +158,7 @@ qcms_transform* qcms_transform_create(
96 void qcms_transform_release(qcms_transform *);
98 void qcms_transform_data(qcms_transform *transform, void *src, void *dest, size_t length);
99 +void qcms_transform_data_type(qcms_transform *transform, void *src, void *dest, size_t length, qcms_output_type type);
101 void qcms_enable_iccv4();
103 diff --git a/third_party/qcms/src/qcmsint.h b/third_party/qcms/src/qcmsint.h
104 index 53a3420..fb53e96 100644
105 --- a/third_party/qcms/src/qcmsint.h
106 +++ b/third_party/qcms/src/qcmsint.h
107 @@ -45,6 +45,11 @@ struct precache_output
108 #define ALIGN __attribute__(( aligned (16) ))
111 +typedef struct _qcms_format_type {
116 struct _qcms_transform {
117 float ALIGN matrix[3][4];
118 float *input_gamma_table_r;
119 @@ -88,7 +93,7 @@ struct _qcms_transform {
120 struct precache_output *output_table_g;
121 struct precache_output *output_table_b;
123 - void (*transform_fn)(struct _qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length);
124 + void (*transform_fn)(struct _qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, struct _qcms_format_type output_format);
128 @@ -280,18 +285,40 @@ qcms_bool set_rgb_colorants(qcms_profile *profile, qcms_CIE_xyY white_point, qcm
129 void qcms_transform_data_rgb_out_lut_sse2(qcms_transform *transform,
134 + qcms_format_type output_format);
135 void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform,
140 + qcms_format_type output_format);
141 void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform,
146 + qcms_format_type output_format);
147 void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform,
152 + qcms_format_type output_format);
154 extern qcms_bool qcms_supports_iccv4;
159 +long __cdecl _InterlockedIncrement(long volatile *);
160 +long __cdecl _InterlockedDecrement(long volatile *);
161 +#pragma intrinsic(_InterlockedIncrement)
162 +#pragma intrinsic(_InterlockedDecrement)
164 +#define qcms_atomic_increment(x) _InterlockedIncrement((long volatile *)&x)
165 +#define qcms_atomic_decrement(x) _InterlockedDecrement((long volatile*)&x)
169 +#define qcms_atomic_increment(x) __sync_add_and_fetch(&x, 1)
170 +#define qcms_atomic_decrement(x) __sync_sub_and_fetch(&x, 1)
173 diff --git a/third_party/qcms/src/qcmstypes.h b/third_party/qcms/src/qcmstypes.h
174 index 56d8de3..9a9b197 100644
175 --- a/third_party/qcms/src/qcmstypes.h
176 +++ b/third_party/qcms/src/qcmstypes.h
177 @@ -87,7 +87,12 @@ typedef unsigned __int64 uint64_t;
179 typedef unsigned __int64 uintptr_t;
181 +#pragma warning(push)
182 +/* Disable benign redefinition of type warning 4142 */
183 +#pragma warning(disable:4142)
184 typedef unsigned long uintptr_t;
185 +/* Restore warnings */
186 +#pragma warning(pop)
190 diff --git a/third_party/qcms/src/transform-sse1.c b/third_party/qcms/src/transform-sse1.c
191 index 2f34db5..aaee1bf 100644
192 --- a/third_party/qcms/src/transform-sse1.c
193 +++ b/third_party/qcms/src/transform-sse1.c
194 @@ -34,7 +34,8 @@ static const ALIGN float clampMaxValueX4[4] =
195 void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform,
200 + qcms_format_type output_format)
203 float (*mat)[4] = transform->matrix;
204 @@ -70,6 +71,8 @@ void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform,
206 /* working variables */
207 __m128 vec_r, vec_g, vec_b, result;
208 + const int r_out = output_format.r;
209 + const int b_out = output_format.b;
213 @@ -116,9 +119,9 @@ void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform,
216 /* use calc'd indices to output RGB values */
217 - dest[0] = otdata_r[output[0]];
218 - dest[1] = otdata_g[output[1]];
219 - dest[2] = otdata_b[output[2]];
220 + dest[r_out] = otdata_r[output[0]];
221 + dest[1] = otdata_g[output[1]];
222 + dest[b_out] = otdata_b[output[2]];
226 @@ -141,9 +144,9 @@ void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform,
227 result = _mm_movehl_ps(result, result);
228 *((__m64 *)&output[2]) = _mm_cvtps_pi32(result);
230 - dest[0] = otdata_r[output[0]];
231 - dest[1] = otdata_g[output[1]];
232 - dest[2] = otdata_b[output[2]];
233 + dest[r_out] = otdata_r[output[0]];
234 + dest[1] = otdata_g[output[1]];
235 + dest[b_out] = otdata_b[output[2]];
239 @@ -151,7 +154,8 @@ void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform,
240 void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform,
245 + qcms_format_type output_format)
248 float (*mat)[4] = transform->matrix;
249 @@ -187,6 +191,8 @@ void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform,
251 /* working variables */
252 __m128 vec_r, vec_g, vec_b, result;
253 + const int r_out = output_format.r;
254 + const int b_out = output_format.b;
258 @@ -239,9 +245,9 @@ void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform,
261 /* use calc'd indices to output RGB values */
262 - dest[0] = otdata_r[output[0]];
263 - dest[1] = otdata_g[output[1]];
264 - dest[2] = otdata_b[output[2]];
265 + dest[r_out] = otdata_r[output[0]];
266 + dest[1] = otdata_g[output[1]];
267 + dest[b_out] = otdata_b[output[2]];
271 @@ -266,9 +272,9 @@ void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform,
272 result = _mm_movehl_ps(result, result);
273 *((__m64 *)&output[2]) = _mm_cvtps_pi32(result);
275 - dest[0] = otdata_r[output[0]];
276 - dest[1] = otdata_g[output[1]];
277 - dest[2] = otdata_b[output[2]];
278 + dest[r_out] = otdata_r[output[0]];
279 + dest[1] = otdata_g[output[1]];
280 + dest[b_out] = otdata_b[output[2]];
284 diff --git a/third_party/qcms/src/transform-sse2.c b/third_party/qcms/src/transform-sse2.c
285 index 6a5faf9..fa7f2d1 100644
286 --- a/third_party/qcms/src/transform-sse2.c
287 +++ b/third_party/qcms/src/transform-sse2.c
288 @@ -34,7 +34,8 @@ static const ALIGN float clampMaxValueX4[4] =
289 void qcms_transform_data_rgb_out_lut_sse2(qcms_transform *transform,
294 + qcms_format_type output_format)
297 float (*mat)[4] = transform->matrix;
298 @@ -70,6 +71,8 @@ void qcms_transform_data_rgb_out_lut_sse2(qcms_transform *transform,
300 /* working variables */
301 __m128 vec_r, vec_g, vec_b, result;
302 + const int r_out = output_format.r;
303 + const int b_out = output_format.b;
307 @@ -114,9 +117,9 @@ void qcms_transform_data_rgb_out_lut_sse2(qcms_transform *transform,
310 /* use calc'd indices to output RGB values */
311 - dest[0] = otdata_r[output[0]];
312 - dest[1] = otdata_g[output[1]];
313 - dest[2] = otdata_b[output[2]];
314 + dest[r_out] = otdata_r[output[0]];
315 + dest[1] = otdata_g[output[1]];
316 + dest[b_out] = otdata_b[output[2]];
320 @@ -137,15 +140,16 @@ void qcms_transform_data_rgb_out_lut_sse2(qcms_transform *transform,
322 _mm_store_si128((__m128i*)output, _mm_cvtps_epi32(result));
324 - dest[0] = otdata_r[output[0]];
325 - dest[1] = otdata_g[output[1]];
326 - dest[2] = otdata_b[output[2]];
327 + dest[r_out] = otdata_r[output[0]];
328 + dest[1] = otdata_g[output[1]];
329 + dest[b_out] = otdata_b[output[2]];
332 void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform,
337 + qcms_format_type output_format)
340 float (*mat)[4] = transform->matrix;
341 @@ -181,6 +185,8 @@ void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform,
343 /* working variables */
344 __m128 vec_r, vec_g, vec_b, result;
345 + const int r_out = output_format.r;
346 + const int b_out = output_format.b;
350 @@ -231,9 +237,9 @@ void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform,
353 /* use calc'd indices to output RGB values */
354 - dest[0] = otdata_r[output[0]];
355 - dest[1] = otdata_g[output[1]];
356 - dest[2] = otdata_b[output[2]];
357 + dest[r_out] = otdata_r[output[0]];
358 + dest[1] = otdata_g[output[1]];
359 + dest[b_out] = otdata_b[output[2]];
363 @@ -256,7 +262,7 @@ void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform,
365 _mm_store_si128((__m128i*)output, _mm_cvtps_epi32(result));
367 - dest[0] = otdata_r[output[0]];
368 - dest[1] = otdata_g[output[1]];
369 - dest[2] = otdata_b[output[2]];
370 + dest[r_out] = otdata_r[output[0]];
371 + dest[1] = otdata_g[output[1]];
372 + dest[b_out] = otdata_b[output[2]];
374 diff --git a/third_party/qcms/src/transform.c b/third_party/qcms/src/transform.c
375 index 9a6562b..7e0ba2c 100644
376 --- a/third_party/qcms/src/transform.c
377 +++ b/third_party/qcms/src/transform.c
378 @@ -181,11 +181,20 @@ compute_chromatic_adaption(struct CIE_XYZ source_white_point,
380 adaption_matrix(struct CIE_XYZ source_illumination, struct CIE_XYZ target_illumination)
382 +#if defined (_MSC_VER)
383 +#pragma warning(push)
384 +/* Disable double to float truncation warning 4305 */
385 +#pragma warning(disable:4305)
387 struct matrix lam_rigg = {{ // Bradford matrix
388 { 0.8951, 0.2664, -0.1614 },
389 { -0.7502, 1.7135, 0.0367 },
390 { 0.0389, -0.0685, 1.0296 }
392 +#if defined (_MSC_VER)
393 +/* Restore warnings */
394 +#pragma warning(pop)
396 return compute_chromatic_adaption(source_illumination, target_illumination, lam_rigg);
399 @@ -230,8 +239,11 @@ qcms_bool set_rgb_colorants(qcms_profile *profile, qcms_CIE_xyY white_point, qcm
403 -static void qcms_transform_data_rgb_out_pow(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
404 +static void qcms_transform_data_rgb_out_pow(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
406 + const int r_out = output_format.r;
407 + const int b_out = output_format.b;
410 float (*mat)[4] = transform->matrix;
411 for (i=0; i<length; i++) {
412 @@ -251,15 +263,19 @@ static void qcms_transform_data_rgb_out_pow(qcms_transform *transform, unsigned
413 float out_device_g = pow(out_linear_g, transform->out_gamma_g);
414 float out_device_b = pow(out_linear_b, transform->out_gamma_b);
416 - *dest++ = clamp_u8(255*out_device_r);
417 - *dest++ = clamp_u8(255*out_device_g);
418 - *dest++ = clamp_u8(255*out_device_b);
419 + dest[r_out] = clamp_u8(out_device_r*255);
420 + dest[1] = clamp_u8(out_device_g*255);
421 + dest[b_out] = clamp_u8(out_device_b*255);
427 -static void qcms_transform_data_gray_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
428 +static void qcms_transform_data_gray_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
430 + const int r_out = output_format.r;
431 + const int b_out = output_format.b;
434 for (i = 0; i < length; i++) {
435 float out_device_r, out_device_g, out_device_b;
436 @@ -267,13 +283,14 @@ static void qcms_transform_data_gray_out_lut(qcms_transform *transform, unsigned
438 float linear = transform->input_gamma_table_gray[device];
440 - out_device_r = lut_interp_linear(linear, transform->output_gamma_lut_r, transform->output_gamma_lut_r_length);
441 + out_device_r = lut_interp_linear(linear, transform->output_gamma_lut_r, transform->output_gamma_lut_r_length);
442 out_device_g = lut_interp_linear(linear, transform->output_gamma_lut_g, transform->output_gamma_lut_g_length);
443 out_device_b = lut_interp_linear(linear, transform->output_gamma_lut_b, transform->output_gamma_lut_b_length);
445 - *dest++ = clamp_u8(out_device_r*255);
446 - *dest++ = clamp_u8(out_device_g*255);
447 - *dest++ = clamp_u8(out_device_b*255);
448 + dest[r_out] = clamp_u8(out_device_r*255);
449 + dest[1] = clamp_u8(out_device_g*255);
450 + dest[b_out] = clamp_u8(out_device_b*255);
455 @@ -283,8 +300,11 @@ static void qcms_transform_data_gray_out_lut(qcms_transform *transform, unsigned
456 See: ftp://ftp.alvyray.com/Acrobat/17_Nonln.pdf
459 -static void qcms_transform_data_graya_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
460 +static void qcms_transform_data_graya_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
462 + const int r_out = output_format.r;
463 + const int b_out = output_format.b;
466 for (i = 0; i < length; i++) {
467 float out_device_r, out_device_g, out_device_b;
468 @@ -293,20 +313,24 @@ static void qcms_transform_data_graya_out_lut(qcms_transform *transform, unsigne
470 float linear = transform->input_gamma_table_gray[device];
472 - out_device_r = lut_interp_linear(linear, transform->output_gamma_lut_r, transform->output_gamma_lut_r_length);
473 + out_device_r = lut_interp_linear(linear, transform->output_gamma_lut_r, transform->output_gamma_lut_r_length);
474 out_device_g = lut_interp_linear(linear, transform->output_gamma_lut_g, transform->output_gamma_lut_g_length);
475 out_device_b = lut_interp_linear(linear, transform->output_gamma_lut_b, transform->output_gamma_lut_b_length);
477 - *dest++ = clamp_u8(out_device_r*255);
478 - *dest++ = clamp_u8(out_device_g*255);
479 - *dest++ = clamp_u8(out_device_b*255);
481 + dest[r_out] = clamp_u8(out_device_r*255);
482 + dest[1] = clamp_u8(out_device_g*255);
483 + dest[b_out] = clamp_u8(out_device_b*255);
490 -static void qcms_transform_data_gray_out_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
491 +static void qcms_transform_data_gray_out_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
493 + const int r_out = output_format.r;
494 + const int b_out = output_format.b;
497 for (i = 0; i < length; i++) {
498 unsigned char device = *src++;
499 @@ -317,14 +341,19 @@ static void qcms_transform_data_gray_out_precache(qcms_transform *transform, uns
500 /* we could round here... */
501 gray = linear * PRECACHE_OUTPUT_MAX;
503 - *dest++ = transform->output_table_r->data[gray];
504 - *dest++ = transform->output_table_g->data[gray];
505 - *dest++ = transform->output_table_b->data[gray];
506 + dest[r_out] = transform->output_table_r->data[gray];
507 + dest[1] = transform->output_table_g->data[gray];
508 + dest[b_out] = transform->output_table_b->data[gray];
513 -static void qcms_transform_data_graya_out_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
515 +static void qcms_transform_data_graya_out_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
517 + const int r_out = output_format.r;
518 + const int b_out = output_format.b;
521 for (i = 0; i < length; i++) {
522 unsigned char device = *src++;
523 @@ -336,15 +365,19 @@ static void qcms_transform_data_graya_out_precache(qcms_transform *transform, un
524 /* we could round here... */
525 gray = linear * PRECACHE_OUTPUT_MAX;
527 - *dest++ = transform->output_table_r->data[gray];
528 - *dest++ = transform->output_table_g->data[gray];
529 - *dest++ = transform->output_table_b->data[gray];
531 + dest[r_out] = transform->output_table_r->data[gray];
532 + dest[1] = transform->output_table_g->data[gray];
533 + dest[b_out] = transform->output_table_b->data[gray];
539 -static void qcms_transform_data_rgb_out_lut_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
540 +static void qcms_transform_data_rgb_out_lut_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
542 + const int r_out = output_format.r;
543 + const int b_out = output_format.b;
546 float (*mat)[4] = transform->matrix;
547 for (i = 0; i < length; i++) {
548 @@ -370,14 +403,18 @@ static void qcms_transform_data_rgb_out_lut_precache(qcms_transform *transform,
549 g = out_linear_g * PRECACHE_OUTPUT_MAX;
550 b = out_linear_b * PRECACHE_OUTPUT_MAX;
552 - *dest++ = transform->output_table_r->data[r];
553 - *dest++ = transform->output_table_g->data[g];
554 - *dest++ = transform->output_table_b->data[b];
555 + dest[r_out] = transform->output_table_r->data[r];
556 + dest[1] = transform->output_table_g->data[g];
557 + dest[b_out] = transform->output_table_b->data[b];
562 -static void qcms_transform_data_rgba_out_lut_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
563 +static void qcms_transform_data_rgba_out_lut_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
565 + const int r_out = output_format.r;
566 + const int b_out = output_format.b;
569 float (*mat)[4] = transform->matrix;
570 for (i = 0; i < length; i++) {
571 @@ -404,16 +441,21 @@ static void qcms_transform_data_rgba_out_lut_precache(qcms_transform *transform,
572 g = out_linear_g * PRECACHE_OUTPUT_MAX;
573 b = out_linear_b * PRECACHE_OUTPUT_MAX;
575 - *dest++ = transform->output_table_r->data[r];
576 - *dest++ = transform->output_table_g->data[g];
577 - *dest++ = transform->output_table_b->data[b];
579 + dest[r_out] = transform->output_table_r->data[r];
580 + dest[1] = transform->output_table_g->data[g];
581 + dest[b_out] = transform->output_table_b->data[b];
589 -static void qcms_transform_data_clut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) {
590 +static void qcms_transform_data_clut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
592 + const int r_out = output_format.r;
593 + const int b_out = output_format.b;
597 int x_len = transform->grid_size;
598 @@ -462,15 +504,20 @@ static void qcms_transform_data_clut(qcms_transform *transform, unsigned char *s
599 float b_y2 = lerp(b_x3, b_x4, y_d);
600 float clut_b = lerp(b_y1, b_y2, z_d);
602 - *dest++ = clamp_u8(clut_r*255.0f);
603 - *dest++ = clamp_u8(clut_g*255.0f);
604 - *dest++ = clamp_u8(clut_b*255.0f);
606 + dest[r_out] = clamp_u8(clut_r*255.0f);
607 + dest[1] = clamp_u8(clut_g*255.0f);
608 + dest[b_out] = clamp_u8(clut_b*255.0f);
614 // Using lcms' tetra interpolation algorithm.
615 -static void qcms_transform_data_tetra_clut_rgba(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) {
616 +static void qcms_transform_data_tetra_clut_rgba(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
618 + const int r_out = output_format.r;
619 + const int b_out = output_format.b;
623 int x_len = transform->grid_size;
624 @@ -577,15 +624,20 @@ static void qcms_transform_data_tetra_clut_rgba(qcms_transform *transform, unsig
625 clut_g = c0_g + c1_g*rx + c2_g*ry + c3_g*rz;
626 clut_b = c0_b + c1_b*rx + c2_b*ry + c3_b*rz;
628 - *dest++ = clamp_u8(clut_r*255.0f);
629 - *dest++ = clamp_u8(clut_g*255.0f);
630 - *dest++ = clamp_u8(clut_b*255.0f);
633 + dest[r_out] = clamp_u8(clut_r*255.0f);
634 + dest[1] = clamp_u8(clut_g*255.0f);
635 + dest[b_out] = clamp_u8(clut_b*255.0f);
641 // Using lcms' tetra interpolation code.
642 -static void qcms_transform_data_tetra_clut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) {
643 +static void qcms_transform_data_tetra_clut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
645 + const int r_out = output_format.r;
646 + const int b_out = output_format.b;
650 int x_len = transform->grid_size;
651 @@ -691,14 +743,18 @@ static void qcms_transform_data_tetra_clut(qcms_transform *transform, unsigned c
652 clut_g = c0_g + c1_g*rx + c2_g*ry + c3_g*rz;
653 clut_b = c0_b + c1_b*rx + c2_b*ry + c3_b*rz;
655 - *dest++ = clamp_u8(clut_r*255.0f);
656 - *dest++ = clamp_u8(clut_g*255.0f);
657 - *dest++ = clamp_u8(clut_b*255.0f);
659 + dest[r_out] = clamp_u8(clut_r*255.0f);
660 + dest[1] = clamp_u8(clut_g*255.0f);
661 + dest[b_out] = clamp_u8(clut_b*255.0f);
666 -static void qcms_transform_data_rgb_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
667 +static void qcms_transform_data_rgb_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
669 + const int r_out = output_format.r;
670 + const int b_out = output_format.b;
673 float (*mat)[4] = transform->matrix;
674 for (i = 0; i < length; i++) {
675 @@ -726,14 +782,18 @@ static void qcms_transform_data_rgb_out_lut(qcms_transform *transform, unsigned
676 out_device_b = lut_interp_linear(out_linear_b,
677 transform->output_gamma_lut_b, transform->output_gamma_lut_b_length);
679 - *dest++ = clamp_u8(out_device_r*255);
680 - *dest++ = clamp_u8(out_device_g*255);
681 - *dest++ = clamp_u8(out_device_b*255);
682 + dest[r_out] = clamp_u8(out_device_r*255);
683 + dest[1] = clamp_u8(out_device_g*255);
684 + dest[b_out] = clamp_u8(out_device_b*255);
689 -static void qcms_transform_data_rgba_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
690 +static void qcms_transform_data_rgba_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
692 + const int r_out = output_format.r;
693 + const int b_out = output_format.b;
696 float (*mat)[4] = transform->matrix;
697 for (i = 0; i < length; i++) {
698 @@ -762,16 +822,20 @@ static void qcms_transform_data_rgba_out_lut(qcms_transform *transform, unsigned
699 out_device_b = lut_interp_linear(out_linear_b,
700 transform->output_gamma_lut_b, transform->output_gamma_lut_b_length);
702 - *dest++ = clamp_u8(out_device_r*255);
703 - *dest++ = clamp_u8(out_device_g*255);
704 - *dest++ = clamp_u8(out_device_b*255);
706 + dest[r_out] = clamp_u8(out_device_r*255);
707 + dest[1] = clamp_u8(out_device_g*255);
708 + dest[b_out] = clamp_u8(out_device_b*255);
715 -static void qcms_transform_data_rgb_out_linear(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length)
716 +static void qcms_transform_data_rgb_out_linear(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, qcms_format_type output_format)
718 + const int r_out = output_format.r;
719 + const int b_out = output_format.b;
722 float (*mat)[4] = transform->matrix;
723 for (i = 0; i < length; i++) {
724 @@ -787,16 +851,25 @@ static void qcms_transform_data_rgb_out_linear(qcms_transform *transform, unsign
725 float out_linear_g = mat[0][1]*linear_r + mat[1][1]*linear_g + mat[2][1]*linear_b;
726 float out_linear_b = mat[0][2]*linear_r + mat[1][2]*linear_g + mat[2][2]*linear_b;
728 - *dest++ = clamp_u8(out_linear_r*255);
729 - *dest++ = clamp_u8(out_linear_g*255);
730 - *dest++ = clamp_u8(out_linear_b*255);
731 + dest[r_out] = clamp_u8(out_linear_r*255);
732 + dest[1] = clamp_u8(out_linear_g*255);
733 + dest[b_out] = clamp_u8(out_linear_b*255);
740 + * If users create and destroy objects on different threads, even if the same
741 + * objects aren't used on different threads at the same time, we can still run
742 + * in to trouble with refcounts if they aren't atomic.
744 + * This can lead to us prematurely deleting the precache if threads get unlucky
745 + * and write the wrong value to the ref count.
747 static struct precache_output *precache_reference(struct precache_output *p)
750 + qcms_atomic_increment(p->ref_count);
754 @@ -810,12 +883,12 @@ static struct precache_output *precache_create()
756 void precache_release(struct precache_output *p)
758 - if (--p->ref_count == 0) {
759 + if (qcms_atomic_decrement(p->ref_count) == 0) {
764 -#ifdef HAS_POSIX_MEMALIGN
765 +#ifdef HAVE_POSIX_MEMALIGN
766 static qcms_transform *transform_alloc(void)
769 @@ -994,13 +1067,15 @@ void qcms_profile_precache_output_transform(qcms_profile *profile)
770 if (profile->color_space != RGB_SIGNATURE)
773 - /* don't precache since we will use the B2A LUT */
776 + if (qcms_supports_iccv4) {
777 + /* don't precache since we will use the B2A LUT */
781 - /* don't precache since we will use the mBA LUT */
784 + /* don't precache since we will use the mBA LUT */
789 /* don't precache if we do not have the TRC curves */
790 if (!profile->redTRC || !profile->greenTRC || !profile->blueTRC)
791 @@ -1157,14 +1232,14 @@ qcms_transform* qcms_transform_create(
796 +#if defined(SSE2_ENABLE) && defined(X86)
797 if (sse_version_available() >= 2) {
798 if (in_type == QCMS_DATA_RGB_8)
799 transform->transform_fn = qcms_transform_data_rgb_out_lut_sse2;
801 transform->transform_fn = qcms_transform_data_rgba_out_lut_sse2;
803 -#if !(defined(_MSC_VER) && defined(_M_AMD64))
804 +#if defined(SSE2_ENABLE) && !(defined(_MSC_VER) && defined(_M_AMD64))
805 /* Microsoft Compiler for x64 doesn't support MMX.
806 * SSE code uses MMX so that we disable on x64 */
808 @@ -1256,13 +1331,34 @@ qcms_transform* qcms_transform_create(
812 -#if defined(__GNUC__) && !defined(__x86_64__) && !defined(__amd64__)
813 +/* __force_align_arg_pointer__ is an x86-only attribute, and gcc/clang warns on unused
814 + * attributes. Don't use this on ARM or AMD64. __has_attribute can detect the presence
815 + * of the attribute but is currently only supported by clang */
816 +#if defined(__has_attribute)
817 +#define HAS_FORCE_ALIGN_ARG_POINTER __has_attribute(__force_align_arg_pointer__)
818 +#elif defined(__GNUC__) && !defined(__x86_64__) && !defined(__amd64__) && !defined(__arm__) && !defined(__mips__)
819 +#define HAS_FORCE_ALIGN_ARG_POINTER 1
821 +#define HAS_FORCE_ALIGN_ARG_POINTER 0
824 +#if HAS_FORCE_ALIGN_ARG_POINTER
825 /* we need this to avoid crashes when gcc assumes the stack is 128bit aligned */
826 __attribute__((__force_align_arg_pointer__))
828 void qcms_transform_data(qcms_transform *transform, void *src, void *dest, size_t length)
830 - transform->transform_fn(transform, src, dest, length);
831 + static const struct _qcms_format_type output_rgbx = { 0, 2 };
833 + transform->transform_fn(transform, src, dest, length, output_rgbx);
836 +void qcms_transform_data_type(qcms_transform *transform, void *src, void *dest, size_t length, qcms_output_type type)
838 + static const struct _qcms_format_type output_rgbx = { 0, 2 };
839 + static const struct _qcms_format_type output_bgrx = { 2, 0 };
841 + transform->transform_fn(transform, src, dest, length, type == QCMS_OUTPUT_BGRX ? output_bgrx : output_rgbx);
844 qcms_bool qcms_supports_iccv4;
845 diff --git a/third_party/qcms/src/transform_util.c b/third_party/qcms/src/transform_util.c
846 index e8447e5..f4338b2 100644
847 --- a/third_party/qcms/src/transform_util.c
848 +++ b/third_party/qcms/src/transform_util.c
851 /* value must be a value between 0 and 1 */
852 //XXX: is the above a good restriction to have?
853 -float lut_interp_linear(double value, uint16_t *table, int length)
854 +float lut_interp_linear(double value, uint16_t *table, size_t length)
857 value = value * (length - 1); // scale to length of the array
858 @@ -49,11 +49,11 @@ float lut_interp_linear(double value, uint16_t *table, int length)
861 /* same as above but takes and returns a uint16_t value representing a range from 0..1 */
862 -uint16_t lut_interp_linear16(uint16_t input_value, uint16_t *table, int length)
863 +uint16_t lut_interp_linear16(uint16_t input_value, uint16_t *table, size_t length)
865 /* Start scaling input_value to the length of the array: 65535*(length-1).
866 * We'll divide out the 65535 next */
867 - uint32_t value = (input_value * (length - 1));
868 + uintptr_t value = (input_value * (length - 1));
869 uint32_t upper = (value + 65534) / 65535; /* equivalent to ceil(value/65535) */
870 uint32_t lower = value / 65535; /* equivalent to floor(value/65535) */
871 /* interp is the distance from upper to value scaled to 0..65535 */
872 @@ -67,11 +67,11 @@ uint16_t lut_interp_linear16(uint16_t input_value, uint16_t *table, int length)
873 /* same as above but takes an input_value from 0..PRECACHE_OUTPUT_MAX
874 * and returns a uint8_t value representing a range from 0..1 */
876 -uint8_t lut_interp_linear_precache_output(uint32_t input_value, uint16_t *table, int length)
877 +uint8_t lut_interp_linear_precache_output(uint32_t input_value, uint16_t *table, size_t length)
879 /* Start scaling input_value to the length of the array: PRECACHE_OUTPUT_MAX*(length-1).
880 * We'll divide out the PRECACHE_OUTPUT_MAX next */
881 - uint32_t value = (input_value * (length - 1));
882 + uintptr_t value = (input_value * (length - 1));
884 /* equivalent to ceil(value/PRECACHE_OUTPUT_MAX) */
885 uint32_t upper = (value + PRECACHE_OUTPUT_MAX-1) / PRECACHE_OUTPUT_MAX;
886 @@ -91,7 +91,7 @@ uint8_t lut_interp_linear_precache_output(uint32_t input_value, uint16_t *table,
888 /* value must be a value between 0 and 1 */
889 //XXX: is the above a good restriction to have?
890 -float lut_interp_linear_float(float value, float *table, int length)
891 +float lut_interp_linear_float(float value, float *table, size_t length)
894 value = value * (length - 1);
895 @@ -235,6 +235,21 @@ float u8Fixed8Number_to_float(uint16_t x)
899 +/* The SSE2 code uses min & max which let NaNs pass through.
900 + We want to try to prevent that here by ensuring that
901 + gamma table is within expected values. */
902 +void validate_gamma_table(float gamma_table[256])
905 + for (i = 0; i < 256; i++) {
906 + // Note: we check that the gamma is not in range
907 + // instead of out of range so that we catch NaNs
908 + if (!(gamma_table[i] >= 0.f && gamma_table[i] <= 1.f)) {
909 + gamma_table[i] = 0.f;
914 float *build_input_gamma_table(struct curveType *TRC)
917 @@ -254,7 +269,10 @@ float *build_input_gamma_table(struct curveType *TRC)
921 - return gamma_table;
923 + validate_gamma_table(gamma_table);
925 + return gamma_table;
928 struct matrix build_colorant_matrix(qcms_profile *p)
929 @@ -390,7 +408,7 @@ uint16_fract_t lut_inverse_interp16(uint16_t Value, uint16_t LutTable[], int len
930 which has an maximum error of about 9855 (pixel difference of ~38.346)
932 For now, we punt the decision of output size to the caller. */
933 -static uint16_t *invert_lut(uint16_t *table, int length, int out_length)
934 +static uint16_t *invert_lut(uint16_t *table, int length, size_t out_length)
937 /* for now we invert the lut by creating a lut of size out_length
938 diff --git a/third_party/qcms/src/transform_util.h b/third_party/qcms/src/transform_util.h
939 index 8f358a8..de465f4 100644
940 --- a/third_party/qcms/src/transform_util.h
941 +++ b/third_party/qcms/src/transform_util.h
943 //XXX: could use a bettername
944 typedef uint16_t uint16_fract_t;
946 -float lut_interp_linear(double value, uint16_t *table, int length);
947 -float lut_interp_linear_float(float value, float *table, int length);
948 -uint16_t lut_interp_linear16(uint16_t input_value, uint16_t *table, int length);
949 +float lut_interp_linear(double value, uint16_t *table, size_t length);
950 +float lut_interp_linear_float(float value, float *table, size_t length);
951 +uint16_t lut_interp_linear16(uint16_t input_value, uint16_t *table, size_t length);
954 static inline float lerp(float a, float b, float t)