* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / win32 / mscms / transform.c
1 /*
2 * MSCMS - Color Management System for Wine
3 *
4 * Copyright 2005, 2006, 2008 Hans Leidekker
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22
23 #include <config.h>
24 #include <wine/debug.h>
25
26 //#include <stdarg.h>
27
28 #include <windef.h>
29 #include <winbase.h>
30 #include <winnls.h>
31 #include <wingdi.h>
32 #include <winuser.h>
33 #include <icm.h>
34
35 //#include "mscms_priv.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
38
39 #ifdef HAVE_LCMS2
40
41 static DWORD from_profile( HPROFILE profile )
42 {
43 PROFILEHEADER header;
44
45 GetColorProfileHeader( profile, &header );
46 TRACE( "color space: 0x%08x %s\n", header.phDataColorSpace, dbgstr_tag( header.phDataColorSpace ) );
47
48 switch (header.phDataColorSpace)
49 {
50 case 0x434d594b: return TYPE_CMYK_16; /* 'CMYK' */
51 case 0x47524159: return TYPE_GRAY_16; /* 'GRAY' */
52 case 0x4c616220: return TYPE_Lab_16; /* 'Lab ' */
53 case 0x52474220: return TYPE_RGB_16; /* 'RGB ' */
54 case 0x58595a20: return TYPE_XYZ_16; /* 'XYZ ' */
55 default:
56 WARN("unhandled format\n");
57 return TYPE_RGB_16;
58 }
59 }
60
61 static DWORD from_bmformat( BMFORMAT format )
62 {
63 static BOOL quietfixme = FALSE;
64 TRACE( "bitmap format: 0x%08x\n", format );
65
66 switch (format)
67 {
68 case BM_RGBTRIPLETS: return TYPE_RGB_8;
69 case BM_BGRTRIPLETS: return TYPE_BGR_8;
70 case BM_GRAY: return TYPE_GRAY_8;
71 case BM_xRGBQUADS: return TYPE_ARGB_8;
72 case BM_xBGRQUADS: return TYPE_ABGR_8;
73 default:
74 if (!quietfixme)
75 {
76 FIXME("unhandled bitmap format 0x%x\n", format);
77 quietfixme = TRUE;
78 }
79 return TYPE_RGB_8;
80 }
81 }
82
83 static DWORD from_type( COLORTYPE type )
84 {
85 TRACE( "color type: 0x%08x\n", type );
86
87 switch (type)
88 {
89 case COLOR_GRAY: return TYPE_GRAY_16;
90 case COLOR_RGB: return TYPE_RGB_16;
91 case COLOR_XYZ: return TYPE_XYZ_16;
92 case COLOR_Yxy: return TYPE_Yxy_16;
93 case COLOR_Lab: return TYPE_Lab_16;
94 case COLOR_CMYK: return TYPE_CMYK_16;
95 default:
96 FIXME("unhandled color type\n");
97 return TYPE_RGB_16;
98 }
99 }
100
101 #endif /* HAVE_LCMS2 */
102
103 /******************************************************************************
104 * CreateColorTransformA [MSCMS.@]
105 *
106 * See CreateColorTransformW.
107 */
108 HTRANSFORM WINAPI CreateColorTransformA( LPLOGCOLORSPACEA space, HPROFILE dest,
109 HPROFILE target, DWORD flags )
110 {
111 LOGCOLORSPACEW spaceW;
112 DWORD len;
113
114 TRACE( "( %p, %p, %p, 0x%08x )\n", space, dest, target, flags );
115
116 if (!space || !dest) return FALSE;
117
118 memcpy( &spaceW, space, FIELD_OFFSET(LOGCOLORSPACEA, lcsFilename) );
119 spaceW.lcsSize = sizeof(LOGCOLORSPACEW);
120
121 len = MultiByteToWideChar( CP_ACP, 0, space->lcsFilename, -1, NULL, 0 );
122 MultiByteToWideChar( CP_ACP, 0, space->lcsFilename, -1, spaceW.lcsFilename, len );
123
124 return CreateColorTransformW( &spaceW, dest, target, flags );
125 }
126
127 /******************************************************************************
128 * CreateColorTransformW [MSCMS.@]
129 *
130 * Create a color transform.
131 *
132 * PARAMS
133 * space [I] Input color space.
134 * dest [I] Color profile of destination device.
135 * target [I] Color profile of target device.
136 * flags [I] Flags.
137 *
138 * RETURNS
139 * Success: Handle to a transform.
140 * Failure: NULL
141 */
142 HTRANSFORM WINAPI CreateColorTransformW( LPLOGCOLORSPACEW space, HPROFILE dest,
143 HPROFILE target, DWORD flags )
144 {
145 HTRANSFORM ret = NULL;
146 #ifdef HAVE_LCMS2
147 struct transform transform;
148 struct profile *dst, *tgt = NULL;
149 cmsHPROFILE cmsinput, cmsoutput, cmstarget = NULL;
150 DWORD in_format, out_format, proofing = 0;
151 int intent;
152
153 TRACE( "( %p, %p, %p, 0x%08x )\n", space, dest, target, flags );
154
155 if (!space || !(dst = grab_profile( dest ))) return FALSE;
156
157 if (target && !(tgt = grab_profile( target )))
158 {
159 release_profile( dst );
160 return FALSE;
161 }
162 intent = space->lcsIntent > 3 ? INTENT_PERCEPTUAL : space->lcsIntent;
163
164 TRACE( "lcsIntent: %x\n", space->lcsIntent );
165 TRACE( "lcsCSType: %s\n", dbgstr_tag( space->lcsCSType ) );
166 TRACE( "lcsFilename: %s\n", debugstr_w( space->lcsFilename ) );
167
168 in_format = TYPE_RGB_16;
169 out_format = from_profile( dest );
170
171 cmsinput = cmsCreate_sRGBProfile(); /* FIXME: create from supplied color space */
172 if (target)
173 {
174 proofing = cmsFLAGS_SOFTPROOFING;
175 cmstarget = tgt->cmsprofile;
176 }
177 cmsoutput = dst->cmsprofile;
178 transform.cmstransform = cmsCreateProofingTransform(cmsinput, in_format, cmsoutput, out_format, cmstarget,
179 intent, INTENT_ABSOLUTE_COLORIMETRIC, proofing);
180
181 ret = create_transform( &transform );
182
183 if (tgt) release_profile( tgt );
184 release_profile( dst );
185
186 #endif /* HAVE_LCMS2 */
187 return ret;
188 }
189
190 /******************************************************************************
191 * CreateMultiProfileTransform [MSCMS.@]
192 *
193 * Create a color transform from an array of color profiles.
194 *
195 * PARAMS
196 * profiles [I] Array of color profiles.
197 * nprofiles [I] Number of color profiles.
198 * intents [I] Array of rendering intents.
199 * flags [I] Flags.
200 * cmm [I] Profile to take the CMM from.
201 *
202 * RETURNS
203 * Success: Handle to a transform.
204 * Failure: NULL
205 */
206 HTRANSFORM WINAPI CreateMultiProfileTransform( PHPROFILE profiles, DWORD nprofiles,
207 PDWORD intents, DWORD nintents, DWORD flags, DWORD cmm )
208 {
209 HTRANSFORM ret = NULL;
210 #ifdef HAVE_LCMS2
211 cmsHPROFILE *cmsprofiles, cmsconvert = NULL;
212 struct transform transform;
213 struct profile *profile0, *profile1;
214 DWORD in_format, out_format;
215
216 TRACE( "( %p, 0x%08x, %p, 0x%08x, 0x%08x, 0x%08x )\n",
217 profiles, nprofiles, intents, nintents, flags, cmm );
218
219 if (!profiles || !nprofiles || !intents) return NULL;
220
221 if (nprofiles > 2)
222 {
223 FIXME("more than 2 profiles not supported\n");
224 return NULL;
225 }
226
227 profile0 = grab_profile( profiles[0] );
228 if (!profile0) return NULL;
229 profile1 = grab_profile( profiles[1] );
230 if (!profile1)
231 {
232 release_profile( profile0 );
233 return NULL;
234 }
235 in_format = from_profile( profiles[0] );
236 out_format = from_profile( profiles[nprofiles - 1] );
237
238 if (in_format != out_format)
239 {
240 /* insert a conversion profile for pairings that lcms doesn't handle */
241 if (out_format == TYPE_RGB_16) cmsconvert = cmsCreate_sRGBProfile();
242 if (out_format == TYPE_Lab_16) cmsconvert = cmsCreateLab2Profile( NULL );
243 }
244
245 cmsprofiles = HeapAlloc( GetProcessHeap(), 0, (nprofiles + 1) * sizeof(cmsHPROFILE) );
246 if (cmsprofiles)
247 {
248 cmsprofiles[0] = profile0->cmsprofile;
249 if (cmsconvert)
250 {
251 cmsprofiles[1] = cmsconvert;
252 cmsprofiles[2] = profile1->cmsprofile;
253 nprofiles++;
254 }
255 else
256 {
257 cmsprofiles[1] = profile1->cmsprofile;
258 }
259 transform.cmstransform = cmsCreateMultiprofileTransform( cmsprofiles, nprofiles, in_format, out_format, *intents, 0 );
260
261 HeapFree( GetProcessHeap(), 0, cmsprofiles );
262 ret = create_transform( &transform );
263 }
264
265 release_profile( profile0 );
266 release_profile( profile1 );
267
268 #endif /* HAVE_LCMS2 */
269 return ret;
270 }
271
272 /******************************************************************************
273 * DeleteColorTransform [MSCMS.@]
274 *
275 * Delete a color transform.
276 *
277 * PARAMS
278 * transform [I] Handle to a color transform.
279 *
280 * RETURNS
281 * Success: TRUE
282 * Failure: FALSE
283 */
284 BOOL WINAPI DeleteColorTransform( HTRANSFORM handle )
285 {
286 BOOL ret = FALSE;
287 #ifdef HAVE_LCMS2
288
289 TRACE( "( %p )\n", handle );
290
291 ret = close_transform( handle );
292
293 #endif /* HAVE_LCMS2 */
294 return ret;
295 }
296
297 /******************************************************************************
298 * TranslateBitmapBits [MSCMS.@]
299 *
300 * Perform color translation.
301 *
302 * PARAMS
303 * transform [I] Handle to a color transform.
304 * srcbits [I] Source bitmap.
305 * input [I] Format of the source bitmap.
306 * width [I] Width of the source bitmap.
307 * height [I] Height of the source bitmap.
308 * inputstride [I] Number of bytes in one scanline.
309 * destbits [I] Destination bitmap.
310 * output [I] Format of the destination bitmap.
311 * outputstride [I] Number of bytes in one scanline.
312 * callback [I] Callback function.
313 * data [I] Callback data.
314 *
315 * RETURNS
316 * Success: TRUE
317 * Failure: FALSE
318 */
319 BOOL WINAPI TranslateBitmapBits( HTRANSFORM handle, PVOID srcbits, BMFORMAT input,
320 DWORD width, DWORD height, DWORD inputstride, PVOID destbits, BMFORMAT output,
321 DWORD outputstride, PBMCALLBACKFN callback, ULONG data )
322 {
323 BOOL ret = FALSE;
324 #ifdef HAVE_LCMS2
325 struct transform *transform = grab_transform( handle );
326
327 TRACE( "( %p, %p, 0x%08x, 0x%08x, 0x%08x, 0x%08x, %p, 0x%08x, 0x%08x, %p, 0x%08x )\n",
328 handle, srcbits, input, width, height, inputstride, destbits, output,
329 outputstride, callback, data );
330
331 if (!transform) return FALSE;
332 cmsChangeBuffersFormat( transform->cmstransform, from_bmformat(input), from_bmformat(output) );
333
334 cmsDoTransform( transform->cmstransform, srcbits, destbits, width * height );
335 release_transform( transform );
336 ret = TRUE;
337
338 #endif /* HAVE_LCMS2 */
339 return ret;
340 }
341
342 /******************************************************************************
343 * TranslateColors [MSCMS.@]
344 *
345 * Perform color translation.
346 *
347 * PARAMS
348 * transform [I] Handle to a color transform.
349 * input [I] Array of input colors.
350 * number [I] Number of colors to translate.
351 * input_type [I] Input color format.
352 * output [O] Array of output colors.
353 * output_type [I] Output color format.
354 *
355 * RETURNS
356 * Success: TRUE
357 * Failure: FALSE
358 */
359 BOOL WINAPI TranslateColors( HTRANSFORM handle, PCOLOR in, DWORD count,
360 COLORTYPE input_type, PCOLOR out, COLORTYPE output_type )
361 {
362 #ifdef HAVE_LCMS2
363 BOOL ret = TRUE;
364 struct transform *transform = grab_transform( handle );
365 cmsHTRANSFORM xfrm;
366 unsigned int i;
367
368 TRACE( "( %p, %p, %d, %d, %p, %d )\n", handle, in, count, input_type, out, output_type );
369
370 if (!transform) return FALSE;
371
372 xfrm = transform->cmstransform;
373 cmsChangeBuffersFormat( xfrm, from_type(input_type), from_type(output_type) );
374
375 switch (input_type)
376 {
377 case COLOR_RGB:
378 {
379 switch (output_type)
380 {
381 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].rgb, 1 ); goto done;
382 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].Lab, 1 ); goto done;
383 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].gray, 1 ); goto done;
384 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].cmyk, 1 ); goto done;
385 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].XYZ, 1 ); goto done;
386 default:
387 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
388 ret = FALSE;
389 break;
390 }
391 break;
392 }
393 case COLOR_Lab:
394 {
395 switch (output_type)
396 {
397 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].rgb, 1 ); goto done;
398 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].Lab, 1 ); goto done;
399 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].gray, 1 ); goto done;
400 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].cmyk, 1 ); goto done;
401 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].XYZ, 1 ); goto done;
402 default:
403 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
404 ret = FALSE;
405 break;
406 }
407 break;
408 }
409 case COLOR_GRAY:
410 {
411 switch (output_type)
412 {
413 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].rgb, 1 ); goto done;
414 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].Lab, 1 ); goto done;
415 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].gray, 1 ); goto done;
416 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].cmyk, 1 ); goto done;
417 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].XYZ, 1 ); goto done;
418 default:
419 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
420 ret = FALSE;
421 break;
422 }
423 break;
424 }
425 case COLOR_CMYK:
426 {
427 switch (output_type)
428 {
429 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].rgb, 1 ); goto done;
430 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].Lab, 1 ); goto done;
431 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].gray, 1 ); goto done;
432 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].cmyk, 1 ); goto done;
433 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].XYZ, 1 ); goto done;
434 default:
435 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
436 ret = FALSE;
437 break;
438 }
439 break;
440 }
441 case COLOR_XYZ:
442 {
443 switch (output_type)
444 {
445 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].rgb, 1 ); goto done;
446 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].Lab, 1 ); goto done;
447 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].gray, 1 ); goto done;
448 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].cmyk, 1 ); goto done;
449 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].XYZ, 1 ); goto done;
450 default:
451 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
452 ret = FALSE;
453 break;
454 }
455 break;
456 }
457 default:
458 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
459 ret = FALSE;
460 break;
461 }
462
463 done:
464 release_transform( transform );
465 return ret;
466
467 #else /* HAVE_LCMS2 */
468 return FALSE;
469 #endif /* HAVE_LCMS2 */
470 }