Add details (where missing) for histograms and remove a few that are not worth provid...
[chromium-blink-merge.git] / third_party / qcms / google.patch
blobd40b8a93cc25f7af6aaa3cd84070866a21a1dcc7
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)
7 return false;
9 - if (profile->A2B0 || profile->B2A0)
10 + if (qcms_supports_iccv4 && (profile->A2B0 || profile->B2A0))
11 return false;
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)
22 +#endif
23 // Build our target vector (see mozilla bug 460629)
24 target[0] = 0.96420;
25 target[1] = 1.00000;
26 @@ -310,6 +315,10 @@ qcms_bool qcms_profile_is_bogus(qcms_profile *profile)
27 tolerance[1] = 0.02;
28 tolerance[2] = 0.04;
30 +#if defined (_MSC_VER)
31 +/* Restore warnings */
32 +#pragma warning(pop)
33 +#endif
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);
45 uint32_t count;
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;
52 uint32_t clut_size;
53 size_t entry_size;
54 struct lutType *lut;
55 @@ -997,6 +1006,9 @@ qcms_profile* qcms_profile_from_memory(const void *mem, size_t size)
56 source.size = size;
57 source.valid = true;
59 + if (size < 4)
60 + return INVALID_PROFILE;
62 length = read_u32(src, 0);
63 if (length <= size) {
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 ******************************************************************/
73 +/*
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.
77 + */
79 /*
80 * Color Space Signatures
81 * Note that only icSigXYZData and icSigLabData are valid
82 @@ -102,6 +108,12 @@ typedef enum {
83 QCMS_DATA_GRAYA_8
84 } qcms_data_type;
86 +/* Format of the output data for qcms_transform_data_type() */
87 +typedef enum {
88 + QCMS_OUTPUT_RGBX,
89 + QCMS_OUTPUT_BGRX
90 +} qcms_output_type;
92 /* the names for the following two types are sort of ugly */
93 typedef struct
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) ))
109 #endif
111 +typedef struct _qcms_format_type {
112 + int r;
113 + int b;
114 +} 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);
127 struct matrix {
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,
130 unsigned char *src,
131 unsigned char *dest,
132 - size_t length);
133 + size_t length,
134 + qcms_format_type output_format);
135 void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform,
136 unsigned char *src,
137 unsigned char *dest,
138 - size_t length);
139 + size_t length,
140 + qcms_format_type output_format);
141 void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform,
142 unsigned char *src,
143 unsigned char *dest,
144 - size_t length);
145 + size_t length,
146 + qcms_format_type output_format);
147 void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform,
148 unsigned char *src,
149 unsigned char *dest,
150 - size_t length);
151 + size_t length,
152 + qcms_format_type output_format);
154 extern qcms_bool qcms_supports_iccv4;
157 +#ifdef _MSC_VER
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)
167 +#else
169 +#define qcms_atomic_increment(x) __sync_add_and_fetch(&x, 1)
170 +#define qcms_atomic_decrement(x) __sync_sub_and_fetch(&x, 1)
172 +#endif
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;
178 #ifdef _WIN64
179 typedef unsigned __int64 uintptr_t;
180 #else
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)
187 #endif
189 #elif defined (_AIX)
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,
196 unsigned char *src,
197 unsigned char *dest,
198 - size_t length)
199 + size_t length,
200 + qcms_format_type output_format)
202 unsigned int i;
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;
211 /* CYA */
212 if (!length)
213 @@ -116,9 +119,9 @@ void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform,
214 src += 3;
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]];
223 dest += 3;
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]];
237 _mm_empty();
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,
241 unsigned char *src,
242 unsigned char *dest,
243 - size_t length)
244 + size_t length,
245 + qcms_format_type output_format)
247 unsigned int i;
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;
255 unsigned char alpha;
257 /* CYA */
258 @@ -239,9 +245,9 @@ void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform,
259 src += 4;
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]];
268 dest += 4;
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]];
282 _mm_empty();
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,
290 unsigned char *src,
291 unsigned char *dest,
292 - size_t length)
293 + size_t length,
294 + qcms_format_type output_format)
296 unsigned int i;
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;
305 /* CYA */
306 if (!length)
307 @@ -114,9 +117,9 @@ void qcms_transform_data_rgb_out_lut_sse2(qcms_transform *transform,
308 src += 3;
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]];
317 dest += 3;
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,
333 unsigned char *src,
334 unsigned char *dest,
335 - size_t length)
336 + size_t length,
337 + qcms_format_type output_format)
339 unsigned int i;
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;
347 unsigned char alpha;
349 /* CYA */
350 @@ -231,9 +237,9 @@ void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform,
351 src += 4;
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]];
360 dest += 4;
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,
379 static struct matrix
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)
386 +#endif
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)
395 +#endif
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
402 #if 0
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;
409 int i;
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);
422 + dest += 3;
425 #endif
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;
433 unsigned int i;
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);
451 + dest += 3;
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;
465 unsigned int i;
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);
480 - *dest++ = alpha;
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);
484 + dest[3] = alpha;
485 + dest += 4;
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;
496 unsigned int i;
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];
509 + dest += 3;
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;
520 unsigned int i;
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];
530 - *dest++ = alpha;
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];
534 + dest[3] = alpha;
535 + dest += 4;
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;
545 unsigned int i;
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];
558 + dest += 3;
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;
568 unsigned int i;
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];
578 - *dest++ = alpha;
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];
582 + dest[3] = alpha;
583 + dest += 4;
587 // Not used
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;
595 unsigned int i;
596 int xy_len = 1;
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);
605 - }
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);
609 + dest += 3;
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;
621 unsigned int i;
622 int xy_len = 1;
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);
631 - *dest++ = in_a;
632 - }
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);
636 + dest[3] = in_a;
637 + dest += 4;
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;
648 unsigned int i;
649 int xy_len = 1;
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);
658 - }
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);
662 + dest += 3;
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;
672 unsigned int i;
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);
685 + dest += 3;
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;
695 unsigned int i;
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);
705 - *dest++ = alpha;
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);
709 + dest[3] = alpha;
710 + dest += 4;
714 #if 0
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;
721 int i;
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);
734 + dest += 3;
737 #endif
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.
746 + */
747 static struct precache_output *precache_reference(struct precache_output *p)
749 - p->ref_count++;
750 + qcms_atomic_increment(p->ref_count);
751 return p;
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) {
760 free(p);
764 -#ifdef HAS_POSIX_MEMALIGN
765 +#ifdef HAVE_POSIX_MEMALIGN
766 static qcms_transform *transform_alloc(void)
768 qcms_transform *t;
769 @@ -994,13 +1067,15 @@ void qcms_profile_precache_output_transform(qcms_profile *profile)
770 if (profile->color_space != RGB_SIGNATURE)
771 return;
773 - /* don't precache since we will use the B2A LUT */
774 - if (profile->B2A0)
775 - return;
776 + if (qcms_supports_iccv4) {
777 + /* don't precache since we will use the B2A LUT */
778 + if (profile->B2A0)
779 + return;
781 - /* don't precache since we will use the mBA LUT */
782 - if (profile->mBA)
783 - return;
784 + /* don't precache since we will use the mBA LUT */
785 + if (profile->mBA)
786 + return;
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(
792 return NULL;
794 if (precache) {
795 -#ifdef X86
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;
800 else
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 */
807 } else
808 @@ -1256,13 +1331,34 @@ qcms_transform* qcms_transform_create(
809 return transform;
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
820 +#else
821 +#define HAS_FORCE_ALIGN_ARG_POINTER 0
822 +#endif
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__))
827 #endif
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
849 @@ -36,7 +36,7 @@
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)
856 int upper, lower;
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 */
875 static
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)
893 int upper, lower;
894 value = value * (length - 1);
895 @@ -235,6 +235,21 @@ float u8Fixed8Number_to_float(uint16_t x)
896 return x/256.;
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])
904 + int i;
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)
916 float *gamma_table;
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)
936 int i;
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
942 @@ -31,9 +31,9 @@
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)