[MSCMS] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / 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_bmformat( BMFORMAT format )
42 {
43 static BOOL quietfixme = FALSE;
44 DWORD ret;
45
46 switch (format)
47 {
48 case BM_RGBTRIPLETS: ret = TYPE_RGB_8; break;
49 case BM_BGRTRIPLETS: ret = TYPE_BGR_8; break;
50 case BM_GRAY: ret = TYPE_GRAY_8; break;
51 case BM_xRGBQUADS: ret = TYPE_ARGB_8; break;
52 case BM_xBGRQUADS: ret = TYPE_ABGR_8; break;
53 default:
54 if (!quietfixme)
55 {
56 FIXME( "unhandled bitmap format %08x\n", format );
57 quietfixme = TRUE;
58 }
59 ret = TYPE_RGB_8;
60 break;
61 }
62 TRACE( "color space: %08x -> %08x\n", format, ret );
63 return ret;
64 }
65
66 static DWORD from_type( COLORTYPE type )
67 {
68 DWORD ret;
69
70 switch (type)
71 {
72 case COLOR_GRAY: ret = TYPE_GRAY_16; break;
73 case COLOR_RGB: ret = TYPE_RGB_16; break;
74 case COLOR_XYZ: ret = TYPE_XYZ_16; break;
75 case COLOR_Yxy: ret = TYPE_Yxy_16; break;
76 case COLOR_Lab: ret = TYPE_Lab_16; break;
77 case COLOR_CMYK: ret = TYPE_CMYK_16; break;
78 default:
79 FIXME( "unhandled color type %08x\n", type );
80 ret = TYPE_RGB_16;
81 break;
82 }
83
84 TRACE( "color type: %08x -> %08x\n", type, ret );
85 return ret;
86 }
87
88 #endif /* HAVE_LCMS2 */
89
90 /******************************************************************************
91 * CreateColorTransformA [MSCMS.@]
92 *
93 * See CreateColorTransformW.
94 */
95 HTRANSFORM WINAPI CreateColorTransformA( LPLOGCOLORSPACEA space, HPROFILE dest,
96 HPROFILE target, DWORD flags )
97 {
98 LOGCOLORSPACEW spaceW;
99 DWORD len;
100
101 TRACE( "( %p, %p, %p, 0x%08x )\n", space, dest, target, flags );
102
103 if (!space || !dest) return FALSE;
104
105 memcpy( &spaceW, space, FIELD_OFFSET(LOGCOLORSPACEA, lcsFilename) );
106 spaceW.lcsSize = sizeof(LOGCOLORSPACEW);
107
108 len = MultiByteToWideChar( CP_ACP, 0, space->lcsFilename, -1, NULL, 0 );
109 MultiByteToWideChar( CP_ACP, 0, space->lcsFilename, -1, spaceW.lcsFilename, len );
110
111 return CreateColorTransformW( &spaceW, dest, target, flags );
112 }
113
114 /******************************************************************************
115 * CreateColorTransformW [MSCMS.@]
116 *
117 * Create a color transform.
118 *
119 * PARAMS
120 * space [I] Input color space.
121 * dest [I] Color profile of destination device.
122 * target [I] Color profile of target device.
123 * flags [I] Flags.
124 *
125 * RETURNS
126 * Success: Handle to a transform.
127 * Failure: NULL
128 */
129 HTRANSFORM WINAPI CreateColorTransformW( LPLOGCOLORSPACEW space, HPROFILE dest,
130 HPROFILE target, DWORD flags )
131 {
132 HTRANSFORM ret = NULL;
133 #ifdef HAVE_LCMS2
134 struct transform transform;
135 struct profile *dst, *tgt = NULL;
136 cmsHPROFILE cmsinput, cmsoutput, cmstarget = NULL;
137 DWORD proofing = 0;
138 int intent;
139
140 TRACE( "( %p, %p, %p, 0x%08x )\n", space, dest, target, flags );
141
142 if (!space || !(dst = grab_profile( dest ))) return FALSE;
143
144 if (target && !(tgt = grab_profile( target )))
145 {
146 release_profile( dst );
147 return FALSE;
148 }
149 intent = space->lcsIntent > 3 ? INTENT_PERCEPTUAL : space->lcsIntent;
150
151 TRACE( "lcsIntent: %x\n", space->lcsIntent );
152 TRACE( "lcsCSType: %s\n", dbgstr_tag( space->lcsCSType ) );
153 TRACE( "lcsFilename: %s\n", debugstr_w( space->lcsFilename ) );
154
155 cmsinput = cmsCreate_sRGBProfile(); /* FIXME: create from supplied color space */
156 if (target)
157 {
158 proofing = cmsFLAGS_SOFTPROOFING;
159 cmstarget = tgt->cmsprofile;
160 }
161 cmsoutput = dst->cmsprofile;
162 transform.cmstransform = cmsCreateProofingTransform(cmsinput, 0, cmsoutput, 0, cmstarget,
163 intent, INTENT_ABSOLUTE_COLORIMETRIC,
164 proofing);
165 if (!transform.cmstransform)
166 {
167 if (tgt) release_profile( tgt );
168 release_profile( dst );
169 return FALSE;
170 }
171
172 ret = create_transform( &transform );
173
174 if (tgt) release_profile( tgt );
175 release_profile( dst );
176
177 #endif /* HAVE_LCMS2 */
178 return ret;
179 }
180
181 /******************************************************************************
182 * CreateMultiProfileTransform [MSCMS.@]
183 *
184 * Create a color transform from an array of color profiles.
185 *
186 * PARAMS
187 * profiles [I] Array of color profiles.
188 * nprofiles [I] Number of color profiles.
189 * intents [I] Array of rendering intents.
190 * flags [I] Flags.
191 * cmm [I] Profile to take the CMM from.
192 *
193 * RETURNS
194 * Success: Handle to a transform.
195 * Failure: NULL
196 */
197 HTRANSFORM WINAPI CreateMultiProfileTransform( PHPROFILE profiles, DWORD nprofiles,
198 PDWORD intents, DWORD nintents, DWORD flags, DWORD cmm )
199 {
200 HTRANSFORM ret = NULL;
201 #ifdef HAVE_LCMS2
202 cmsHPROFILE *cmsprofiles;
203 struct transform transform;
204 struct profile *profile0, *profile1;
205
206 TRACE( "( %p, 0x%08x, %p, 0x%08x, 0x%08x, 0x%08x )\n",
207 profiles, nprofiles, intents, nintents, flags, cmm );
208
209 if (!profiles || !nprofiles || !intents) return NULL;
210
211 if (nprofiles > 2)
212 {
213 FIXME("more than 2 profiles not supported\n");
214 return NULL;
215 }
216
217 profile0 = grab_profile( profiles[0] );
218 if (!profile0) return NULL;
219 profile1 = grab_profile( profiles[1] );
220 if (!profile1)
221 {
222 release_profile( profile0 );
223 return NULL;
224 }
225
226 if ((cmsprofiles = HeapAlloc( GetProcessHeap(), 0, (nprofiles + 1) * sizeof(cmsHPROFILE) )))
227 {
228 cmsprofiles[0] = profile0->cmsprofile;
229 cmsprofiles[1] = profile1->cmsprofile;
230
231 transform.cmstransform = cmsCreateMultiprofileTransform( cmsprofiles, nprofiles, 0,
232 0, *intents, 0 );
233 HeapFree( GetProcessHeap(), 0, cmsprofiles );
234 if (!transform.cmstransform)
235 {
236 release_profile( profile0 );
237 release_profile( profile1 );
238 return FALSE;
239 }
240 ret = create_transform( &transform );
241 }
242
243 release_profile( profile0 );
244 release_profile( profile1 );
245
246 #endif /* HAVE_LCMS2 */
247 return ret;
248 }
249
250 /******************************************************************************
251 * DeleteColorTransform [MSCMS.@]
252 *
253 * Delete a color transform.
254 *
255 * PARAMS
256 * transform [I] Handle to a color transform.
257 *
258 * RETURNS
259 * Success: TRUE
260 * Failure: FALSE
261 */
262 BOOL WINAPI DeleteColorTransform( HTRANSFORM handle )
263 {
264 BOOL ret = FALSE;
265 #ifdef HAVE_LCMS2
266
267 TRACE( "( %p )\n", handle );
268
269 ret = close_transform( handle );
270
271 #endif /* HAVE_LCMS2 */
272 return ret;
273 }
274
275 /******************************************************************************
276 * TranslateBitmapBits [MSCMS.@]
277 *
278 * Perform color translation.
279 *
280 * PARAMS
281 * transform [I] Handle to a color transform.
282 * srcbits [I] Source bitmap.
283 * input [I] Format of the source bitmap.
284 * width [I] Width of the source bitmap.
285 * height [I] Height of the source bitmap.
286 * inputstride [I] Number of bytes in one scanline.
287 * destbits [I] Destination bitmap.
288 * output [I] Format of the destination bitmap.
289 * outputstride [I] Number of bytes in one scanline.
290 * callback [I] Callback function.
291 * data [I] Callback data.
292 *
293 * RETURNS
294 * Success: TRUE
295 * Failure: FALSE
296 */
297 BOOL WINAPI TranslateBitmapBits( HTRANSFORM handle, PVOID srcbits, BMFORMAT input,
298 DWORD width, DWORD height, DWORD inputstride, PVOID destbits, BMFORMAT output,
299 DWORD outputstride, PBMCALLBACKFN callback, ULONG data )
300 {
301 BOOL ret = FALSE;
302 #ifdef HAVE_LCMS2
303 struct transform *transform = grab_transform( handle );
304
305 TRACE( "( %p, %p, 0x%08x, 0x%08x, 0x%08x, 0x%08x, %p, 0x%08x, 0x%08x, %p, 0x%08x )\n",
306 handle, srcbits, input, width, height, inputstride, destbits, output,
307 outputstride, callback, data );
308
309 if (!transform) return FALSE;
310 if (!cmsChangeBuffersFormat( transform->cmstransform, from_bmformat(input), from_bmformat(output) ))
311 return FALSE;
312
313 cmsDoTransform( transform->cmstransform, srcbits, destbits, width * height );
314 release_transform( transform );
315 ret = TRUE;
316
317 #endif /* HAVE_LCMS2 */
318 return ret;
319 }
320
321 /******************************************************************************
322 * TranslateColors [MSCMS.@]
323 *
324 * Perform color translation.
325 *
326 * PARAMS
327 * transform [I] Handle to a color transform.
328 * input [I] Array of input colors.
329 * number [I] Number of colors to translate.
330 * input_type [I] Input color format.
331 * output [O] Array of output colors.
332 * output_type [I] Output color format.
333 *
334 * RETURNS
335 * Success: TRUE
336 * Failure: FALSE
337 */
338 BOOL WINAPI TranslateColors( HTRANSFORM handle, PCOLOR in, DWORD count,
339 COLORTYPE input_type, PCOLOR out, COLORTYPE output_type )
340 {
341 #ifdef HAVE_LCMS2
342 BOOL ret = TRUE;
343 struct transform *transform = grab_transform( handle );
344 cmsHTRANSFORM xfrm;
345 unsigned int i;
346
347 TRACE( "( %p, %p, %d, %d, %p, %d )\n", handle, in, count, input_type, out, output_type );
348
349 if (!transform) return FALSE;
350
351 xfrm = transform->cmstransform;
352 if (!cmsChangeBuffersFormat( xfrm, from_type(input_type), from_type(output_type) ))
353 return FALSE;
354
355 switch (input_type)
356 {
357 case COLOR_RGB:
358 {
359 switch (output_type)
360 {
361 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].rgb, 1 ); goto done;
362 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].Lab, 1 ); goto done;
363 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].gray, 1 ); goto done;
364 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].cmyk, 1 ); goto done;
365 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].XYZ, 1 ); goto done;
366 default:
367 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
368 ret = FALSE;
369 break;
370 }
371 break;
372 }
373 case COLOR_Lab:
374 {
375 switch (output_type)
376 {
377 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].rgb, 1 ); goto done;
378 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].Lab, 1 ); goto done;
379 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].gray, 1 ); goto done;
380 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].cmyk, 1 ); goto done;
381 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].XYZ, 1 ); goto done;
382 default:
383 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
384 ret = FALSE;
385 break;
386 }
387 break;
388 }
389 case COLOR_GRAY:
390 {
391 switch (output_type)
392 {
393 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].rgb, 1 ); goto done;
394 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].Lab, 1 ); goto done;
395 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].gray, 1 ); goto done;
396 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].cmyk, 1 ); goto done;
397 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].XYZ, 1 ); goto done;
398 default:
399 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
400 ret = FALSE;
401 break;
402 }
403 break;
404 }
405 case COLOR_CMYK:
406 {
407 switch (output_type)
408 {
409 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].rgb, 1 ); goto done;
410 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].Lab, 1 ); goto done;
411 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].gray, 1 ); goto done;
412 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].cmyk, 1 ); goto done;
413 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].XYZ, 1 ); goto done;
414 default:
415 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
416 ret = FALSE;
417 break;
418 }
419 break;
420 }
421 case COLOR_XYZ:
422 {
423 switch (output_type)
424 {
425 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].rgb, 1 ); goto done;
426 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].Lab, 1 ); goto done;
427 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].gray, 1 ); goto done;
428 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].cmyk, 1 ); goto done;
429 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].XYZ, 1 ); goto done;
430 default:
431 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
432 ret = FALSE;
433 break;
434 }
435 break;
436 }
437 default:
438 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
439 ret = FALSE;
440 break;
441 }
442
443 done:
444 release_transform( transform );
445 return ret;
446
447 #else /* HAVE_LCMS2 */
448 return FALSE;
449 #endif /* HAVE_LCMS2 */
450 }