1 /* vim: set ts=8 sw=8 noexpandtab: */
2 //  qcms
3 //  Copyright (C) 2009 Mozilla Foundation
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the Software
10 // is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
17 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 
23 #include "qcms.h"
24 #include "qcmstypes.h"
25 
26 /* used as a lookup table for the output transformation.
27  * we refcount them so we only need to have one around per output
28  * profile, instead of duplicating them per transform */
29 struct precache_output
30 {
31 	int ref_count;
32 	/* We previously used a count of 65536 here but that seems like more
33 	 * precision than we actually need.  By reducing the size we can
34 	 * improve startup performance and reduce memory usage. ColorSync on
35 	 * 10.5 uses 4097 which is perhaps because they use a fixed point
36 	 * representation where 1. is represented by 0x1000. */
37 #define PRECACHE_OUTPUT_SIZE 8192
38 #define PRECACHE_OUTPUT_MAX (PRECACHE_OUTPUT_SIZE-1)
39 	uint8_t data[PRECACHE_OUTPUT_SIZE];
40 };
41 
42 #ifdef _MSC_VER
43 #define ALIGN __declspec(align(16))
44 #else
45 #define ALIGN __attribute__(( aligned (16) ))
46 #endif
47 
48 typedef struct _qcms_format_type {
49 	int r;
50 	int b;
51 } qcms_format_type;
52 
53 struct _qcms_transform {
54 	float ALIGN matrix[3][4];
55 	float *input_gamma_table_r;
56 	float *input_gamma_table_g;
57 	float *input_gamma_table_b;
58 
59 	float *input_clut_table_r;
60 	float *input_clut_table_g;
61 	float *input_clut_table_b;
62 	uint16_t input_clut_table_length;
63 	float *r_clut;
64 	float *g_clut;
65 	float *b_clut;
66 	uint16_t grid_size;
67 	float *output_clut_table_r;
68 	float *output_clut_table_g;
69 	float *output_clut_table_b;
70 	uint16_t output_clut_table_length;
71 
72 	float *input_gamma_table_gray;
73 
74 	float out_gamma_r;
75 	float out_gamma_g;
76 	float out_gamma_b;
77 
78 	float out_gamma_gray;
79 
80 	uint16_t *output_gamma_lut_r;
81 	uint16_t *output_gamma_lut_g;
82 	uint16_t *output_gamma_lut_b;
83 
84 	uint16_t *output_gamma_lut_gray;
85 
86 	size_t output_gamma_lut_r_length;
87 	size_t output_gamma_lut_g_length;
88 	size_t output_gamma_lut_b_length;
89 
90 	size_t output_gamma_lut_gray_length;
91 
92 	struct precache_output *output_table_r;
93 	struct precache_output *output_table_g;
94 	struct precache_output *output_table_b;
95 
96 	void (*transform_fn)(struct _qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length, struct _qcms_format_type output_format);
97 
98 	unsigned char floor_cache[256];
99 	unsigned char ceil_cache[256];
100 	float r_cache[256];
101 
102 #define TRANSFORM_FLAG_MATRIX		0x0001
103 #define TRANSFORM_FLAG_CLUT_CACHE	0x0002
104 
105 	uint16_t transform_flags;
106 };
107 
108 struct matrix {
109 	float m[3][3];
110 	bool invalid;
111 };
112 
113 struct qcms_modular_transform;
114 
115 typedef void (*transform_module_fn_t)(struct qcms_modular_transform *transform, float *src, float *dest, size_t length);
116 
117 struct qcms_modular_transform {
118 	struct matrix matrix;
119 	float tx, ty, tz;
120 
121 	float *input_clut_table_r;
122 	float *input_clut_table_g;
123 	float *input_clut_table_b;
124 	uint16_t input_clut_table_length;
125 	float *r_clut;
126 	float *g_clut;
127 	float *b_clut;
128 	uint16_t grid_size;
129 	float *output_clut_table_r;
130 	float *output_clut_table_g;
131 	float *output_clut_table_b;
132 	uint16_t output_clut_table_length;
133 
134 	uint16_t *output_gamma_lut_r;
135 	uint16_t *output_gamma_lut_g;
136 	uint16_t *output_gamma_lut_b;
137 
138 	size_t output_gamma_lut_r_length;
139 	size_t output_gamma_lut_g_length;
140 	size_t output_gamma_lut_b_length;
141 
142 	transform_module_fn_t transform_module_fn;
143 	struct qcms_modular_transform *next_transform;
144 };
145 
146 typedef int32_t s15Fixed16Number;
147 typedef uint16_t uInt16Number;
148 typedef uint8_t uInt8Number;
149 
150 struct XYZNumber {
151 	s15Fixed16Number X;
152 	s15Fixed16Number Y;
153 	s15Fixed16Number Z;
154 };
155 
156 struct curveType {
157 	uint32_t type;
158 	uint32_t count;
159 	float parameter[7];
160 	uInt16Number data[];
161 };
162 
163 struct lutmABType {
164 	uint8_t num_in_channels;
165 	uint8_t num_out_channels;
166 	// 16 is the upperbound, actual is 0..num_in_channels.
167 	uint8_t num_grid_points[16];
168 
169 	s15Fixed16Number e00;
170 	s15Fixed16Number e01;
171 	s15Fixed16Number e02;
172 	s15Fixed16Number e03;
173 	s15Fixed16Number e10;
174 	s15Fixed16Number e11;
175 	s15Fixed16Number e12;
176 	s15Fixed16Number e13;
177 	s15Fixed16Number e20;
178 	s15Fixed16Number e21;
179 	s15Fixed16Number e22;
180 	s15Fixed16Number e23;
181 
182 	// reversed elements (for mBA)
183 	bool reversed;
184 
185 	float *clut_table;
186 	struct curveType *a_curves[10];
187 	struct curveType *b_curves[10];
188 	struct curveType *m_curves[10];
189 	float clut_table_data[];
190 };
191 
192 /* should lut8Type and lut16Type be different types? */
193 struct lutType { // used by lut8Type/lut16Type (mft2) only
194 	uint8_t num_input_channels;
195 	uint8_t num_output_channels;
196 	uint8_t num_clut_grid_points;
197 
198 	s15Fixed16Number e00;
199 	s15Fixed16Number e01;
200 	s15Fixed16Number e02;
201 	s15Fixed16Number e10;
202 	s15Fixed16Number e11;
203 	s15Fixed16Number e12;
204 	s15Fixed16Number e20;
205 	s15Fixed16Number e21;
206 	s15Fixed16Number e22;
207 
208 	uint16_t num_input_table_entries;
209 	uint16_t num_output_table_entries;
210 
211 	float *input_table;
212 	float *clut_table;
213 	float *output_table;
214 
215 	float table_data[];
216 };
217 
218 struct vcgtType {
219 	/* data contains three gamma channels: R[length], then G[length], then
220 	 * B[length]. */
221 	uint16_t *data;
222 	size_t length;
223 };
224 
225 #if 0
226 /* this is from an intial idea of having the struct correspond to the data in
227  * the file. I decided that it wasn't a good idea.
228  */
229 struct tag_value {
230 	uint32_t type;
231 	union {
232 		struct {
233 			uint32_t reserved;
234 			struct {
235 				s15Fixed16Number X;
236 				s15Fixed16Number Y;
237 				s15Fixed16Number Z;
238 			} XYZNumber;
239 		} XYZType;
240 	};
241 }; // I guess we need to pack this?
242 #endif
243 
244 #define RGB_SIGNATURE  0x52474220
245 #define GRAY_SIGNATURE 0x47524159
246 #define XYZ_SIGNATURE  0x58595A20
247 #define LAB_SIGNATURE  0x4C616220
248 
249 struct _qcms_profile {
250 	uint32_t icc_version;
251 	char description[64];
252 	uint32_t class;
253 	uint32_t color_space;
254 	uint32_t pcs;
255 	qcms_intent rendering_intent;
256 	struct XYZNumber mediaWhitePoint;
257 	struct XYZNumber redColorant;
258 	struct XYZNumber blueColorant;
259 	struct XYZNumber greenColorant;
260 	struct curveType *redTRC;
261 	struct curveType *blueTRC;
262 	struct curveType *greenTRC;
263 	struct curveType *grayTRC;
264 	struct lutType *A2B0;
265 	struct lutType *B2A0;
266 	struct lutmABType *mAB;
267 	struct lutmABType *mBA;
268 	struct matrix chromaticAdaption;
269 	struct vcgtType vcgt;
270 
271 	struct precache_output *output_table_r;
272 	struct precache_output *output_table_g;
273 	struct precache_output *output_table_b;
274 };
275 
276 #ifdef _MSC_VER
277 #define inline _inline
278 #endif
279 
280 /* produces the nearest float to 'a' with a maximum error
281  * of 1/1024 which happens for large values like 0x40000040 */
s15Fixed16Number_to_float(s15Fixed16Number a)282 static inline float s15Fixed16Number_to_float(s15Fixed16Number a)
283 {
284 	return ((int32_t)a)/65536.f;
285 }
286 
double_to_s15Fixed16Number(double v)287 static inline s15Fixed16Number double_to_s15Fixed16Number(double v)
288 {
289 	return (int32_t)(v*65536);
290 }
291 
uInt8Number_to_float(uInt8Number a)292 static inline float uInt8Number_to_float(uInt8Number a)
293 {
294 	return ((int32_t)a)/255.f;
295 }
296 
uInt16Number_to_float(uInt16Number a)297 static inline float uInt16Number_to_float(uInt16Number a)
298 {
299 	return ((int32_t)a)/65535.f;
300 }
301 
302 
303 void precache_release(struct precache_output *p);
304 qcms_bool set_rgb_colorants(qcms_profile *profile, qcms_CIE_xyY white_point, qcms_CIE_xyYTRIPLE primaries);
305 
306 void qcms_transform_data_rgb_out_lut_sse2(qcms_transform *transform,
307                                           unsigned char *src,
308                                           unsigned char *dest,
309                                           size_t length,
310                                           qcms_format_type output_format);
311 void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform,
312                                           unsigned char *src,
313                                           unsigned char *dest,
314                                           size_t length,
315                                           qcms_format_type output_format);
316 void qcms_transform_data_tetra_clut_rgba_sse2(qcms_transform* transform,
317                                               unsigned char* src,
318                                               unsigned char* dest,
319                                               size_t length,
320                                               qcms_format_type output_format);
321 
322 void qcms_transform_build_clut_cache(qcms_transform* transform);
323 
324 extern qcms_bool qcms_supports_iccv4;
325 
326 
327 #ifdef _MSC_VER
328 
329 long __cdecl _InterlockedIncrement(long volatile *);
330 long __cdecl _InterlockedDecrement(long volatile *);
331 #pragma intrinsic(_InterlockedIncrement)
332 #pragma intrinsic(_InterlockedDecrement)
333 
334 #define qcms_atomic_increment(x) _InterlockedIncrement((long volatile *)&x)
335 #define qcms_atomic_decrement(x) _InterlockedDecrement((long volatile*)&x)
336 
337 #else
338 
339 #define qcms_atomic_increment(x) __sync_add_and_fetch(&x, 1)
340 #define qcms_atomic_decrement(x) __sync_sub_and_fetch(&x, 1)
341 
342 #endif
343