wined3d: Pass non-NULL rectangles to wined3d_surface_blt() in wined3d_device_copy_res...
[wine.git] / dlls / mscms / transform.c
blobcada3329ace5be5bbfd52cde6768cea34dc51fd9
1 /*
2 * MSCMS - Color Management System for Wine
4 * Copyright 2005, 2006, 2008 Hans Leidekker
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.
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.
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
21 #include "config.h"
22 #include "wine/debug.h"
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "icm.h"
33 #include "mscms_priv.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
37 #ifdef HAVE_LCMS2
39 static DWORD from_bmformat( BMFORMAT format )
41 static BOOL quietfixme = FALSE;
42 DWORD ret;
44 switch (format)
46 case BM_RGBTRIPLETS: ret = TYPE_RGB_8; break;
47 case BM_BGRTRIPLETS: ret = TYPE_BGR_8; break;
48 case BM_GRAY: ret = TYPE_GRAY_8; break;
49 case BM_xRGBQUADS: ret = TYPE_ARGB_8; break;
50 case BM_xBGRQUADS: ret = TYPE_ABGR_8; break;
51 default:
52 if (!quietfixme)
54 FIXME( "unhandled bitmap format %08x\n", format );
55 quietfixme = TRUE;
57 ret = TYPE_RGB_8;
58 break;
60 TRACE( "color space: %08x -> %08x\n", format, ret );
61 return ret;
64 static DWORD from_type( COLORTYPE type )
66 DWORD ret;
68 switch (type)
70 case COLOR_GRAY: ret = TYPE_GRAY_16; break;
71 case COLOR_RGB: ret = TYPE_RGB_16; break;
72 case COLOR_XYZ: ret = TYPE_XYZ_16; break;
73 case COLOR_Yxy: ret = TYPE_Yxy_16; break;
74 case COLOR_Lab: ret = TYPE_Lab_16; break;
75 case COLOR_CMYK: ret = TYPE_CMYK_16; break;
76 default:
77 FIXME( "unhandled color type %08x\n", type );
78 ret = TYPE_RGB_16;
79 break;
82 TRACE( "color type: %08x -> %08x\n", type, ret );
83 return ret;
86 #endif /* HAVE_LCMS2 */
88 /******************************************************************************
89 * CreateColorTransformA [MSCMS.@]
91 * See CreateColorTransformW.
93 HTRANSFORM WINAPI CreateColorTransformA( LPLOGCOLORSPACEA space, HPROFILE dest,
94 HPROFILE target, DWORD flags )
96 LOGCOLORSPACEW spaceW;
97 DWORD len;
99 TRACE( "( %p, %p, %p, 0x%08x )\n", space, dest, target, flags );
101 if (!space || !dest) return FALSE;
103 memcpy( &spaceW, space, FIELD_OFFSET(LOGCOLORSPACEA, lcsFilename) );
104 spaceW.lcsSize = sizeof(LOGCOLORSPACEW);
106 len = MultiByteToWideChar( CP_ACP, 0, space->lcsFilename, -1, NULL, 0 );
107 MultiByteToWideChar( CP_ACP, 0, space->lcsFilename, -1, spaceW.lcsFilename, len );
109 return CreateColorTransformW( &spaceW, dest, target, flags );
112 /******************************************************************************
113 * CreateColorTransformW [MSCMS.@]
115 * Create a color transform.
117 * PARAMS
118 * space [I] Input color space.
119 * dest [I] Color profile of destination device.
120 * target [I] Color profile of target device.
121 * flags [I] Flags.
123 * RETURNS
124 * Success: Handle to a transform.
125 * Failure: NULL
127 HTRANSFORM WINAPI CreateColorTransformW( LPLOGCOLORSPACEW space, HPROFILE dest,
128 HPROFILE target, DWORD flags )
130 HTRANSFORM ret = NULL;
131 #ifdef HAVE_LCMS2
132 struct transform transform;
133 struct profile *dst, *tgt = NULL;
134 cmsHPROFILE cmsinput, cmsoutput, cmstarget = NULL;
135 DWORD proofing = 0;
136 int intent;
138 TRACE( "( %p, %p, %p, 0x%08x )\n", space, dest, target, flags );
140 if (!space || !(dst = grab_profile( dest ))) return FALSE;
142 if (target && !(tgt = grab_profile( target )))
144 release_profile( dst );
145 return FALSE;
147 intent = space->lcsIntent > 3 ? INTENT_PERCEPTUAL : space->lcsIntent;
149 TRACE( "lcsIntent: %x\n", space->lcsIntent );
150 TRACE( "lcsCSType: %s\n", dbgstr_tag( space->lcsCSType ) );
151 TRACE( "lcsFilename: %s\n", debugstr_w( space->lcsFilename ) );
153 cmsinput = cmsCreate_sRGBProfile(); /* FIXME: create from supplied color space */
154 if (target)
156 proofing = cmsFLAGS_SOFTPROOFING;
157 cmstarget = tgt->cmsprofile;
159 cmsoutput = dst->cmsprofile;
160 transform.cmstransform = cmsCreateProofingTransform(cmsinput, 0, cmsoutput, 0, cmstarget,
161 intent, INTENT_ABSOLUTE_COLORIMETRIC,
162 proofing);
163 if (!transform.cmstransform)
165 if (tgt) release_profile( tgt );
166 release_profile( dst );
167 return FALSE;
170 ret = create_transform( &transform );
172 if (tgt) release_profile( tgt );
173 release_profile( dst );
175 #endif /* HAVE_LCMS2 */
176 return ret;
179 /******************************************************************************
180 * CreateMultiProfileTransform [MSCMS.@]
182 * Create a color transform from an array of color profiles.
184 * PARAMS
185 * profiles [I] Array of color profiles.
186 * nprofiles [I] Number of color profiles.
187 * intents [I] Array of rendering intents.
188 * flags [I] Flags.
189 * cmm [I] Profile to take the CMM from.
191 * RETURNS
192 * Success: Handle to a transform.
193 * Failure: NULL
195 HTRANSFORM WINAPI CreateMultiProfileTransform( PHPROFILE profiles, DWORD nprofiles,
196 PDWORD intents, DWORD nintents, DWORD flags, DWORD cmm )
198 HTRANSFORM ret = NULL;
199 #ifdef HAVE_LCMS2
200 cmsHPROFILE *cmsprofiles;
201 struct transform transform;
202 struct profile *profile0, *profile1;
204 TRACE( "( %p, 0x%08x, %p, 0x%08x, 0x%08x, 0x%08x )\n",
205 profiles, nprofiles, intents, nintents, flags, cmm );
207 if (!profiles || !nprofiles || !intents) return NULL;
209 if (nprofiles > 2)
211 FIXME("more than 2 profiles not supported\n");
212 return NULL;
215 profile0 = grab_profile( profiles[0] );
216 if (!profile0) return NULL;
217 profile1 = grab_profile( profiles[1] );
218 if (!profile1)
220 release_profile( profile0 );
221 return NULL;
224 if ((cmsprofiles = HeapAlloc( GetProcessHeap(), 0, (nprofiles + 1) * sizeof(cmsHPROFILE) )))
226 cmsprofiles[0] = profile0->cmsprofile;
227 cmsprofiles[1] = profile1->cmsprofile;
229 transform.cmstransform = cmsCreateMultiprofileTransform( cmsprofiles, nprofiles, 0,
230 0, *intents, 0 );
231 HeapFree( GetProcessHeap(), 0, cmsprofiles );
232 if (!transform.cmstransform)
234 release_profile( profile0 );
235 release_profile( profile1 );
236 return FALSE;
238 ret = create_transform( &transform );
241 release_profile( profile0 );
242 release_profile( profile1 );
244 #endif /* HAVE_LCMS2 */
245 return ret;
248 /******************************************************************************
249 * DeleteColorTransform [MSCMS.@]
251 * Delete a color transform.
253 * PARAMS
254 * transform [I] Handle to a color transform.
256 * RETURNS
257 * Success: TRUE
258 * Failure: FALSE
260 BOOL WINAPI DeleteColorTransform( HTRANSFORM handle )
262 BOOL ret = FALSE;
263 #ifdef HAVE_LCMS2
265 TRACE( "( %p )\n", handle );
267 ret = close_transform( handle );
269 #endif /* HAVE_LCMS2 */
270 return ret;
273 /******************************************************************************
274 * TranslateBitmapBits [MSCMS.@]
276 * Perform color translation.
278 * PARAMS
279 * transform [I] Handle to a color transform.
280 * srcbits [I] Source bitmap.
281 * input [I] Format of the source bitmap.
282 * width [I] Width of the source bitmap.
283 * height [I] Height of the source bitmap.
284 * inputstride [I] Number of bytes in one scanline.
285 * destbits [I] Destination bitmap.
286 * output [I] Format of the destination bitmap.
287 * outputstride [I] Number of bytes in one scanline.
288 * callback [I] Callback function.
289 * data [I] Callback data.
291 * RETURNS
292 * Success: TRUE
293 * Failure: FALSE
295 BOOL WINAPI TranslateBitmapBits( HTRANSFORM handle, PVOID srcbits, BMFORMAT input,
296 DWORD width, DWORD height, DWORD inputstride, PVOID destbits, BMFORMAT output,
297 DWORD outputstride, PBMCALLBACKFN callback, ULONG data )
299 BOOL ret = FALSE;
300 #ifdef HAVE_LCMS2
301 struct transform *transform = grab_transform( handle );
303 TRACE( "( %p, %p, 0x%08x, 0x%08x, 0x%08x, 0x%08x, %p, 0x%08x, 0x%08x, %p, 0x%08x )\n",
304 handle, srcbits, input, width, height, inputstride, destbits, output,
305 outputstride, callback, data );
307 if (!transform) return FALSE;
308 if (!cmsChangeBuffersFormat( transform->cmstransform, from_bmformat(input), from_bmformat(output) ))
309 return FALSE;
311 cmsDoTransform( transform->cmstransform, srcbits, destbits, width * height );
312 release_transform( transform );
313 ret = TRUE;
315 #endif /* HAVE_LCMS2 */
316 return ret;
319 /******************************************************************************
320 * TranslateColors [MSCMS.@]
322 * Perform color translation.
324 * PARAMS
325 * transform [I] Handle to a color transform.
326 * input [I] Array of input colors.
327 * number [I] Number of colors to translate.
328 * input_type [I] Input color format.
329 * output [O] Array of output colors.
330 * output_type [I] Output color format.
332 * RETURNS
333 * Success: TRUE
334 * Failure: FALSE
336 BOOL WINAPI TranslateColors( HTRANSFORM handle, PCOLOR in, DWORD count,
337 COLORTYPE input_type, PCOLOR out, COLORTYPE output_type )
339 #ifdef HAVE_LCMS2
340 BOOL ret = TRUE;
341 struct transform *transform = grab_transform( handle );
342 cmsHTRANSFORM xfrm;
343 unsigned int i;
345 TRACE( "( %p, %p, %d, %d, %p, %d )\n", handle, in, count, input_type, out, output_type );
347 if (!transform) return FALSE;
349 xfrm = transform->cmstransform;
350 if (!cmsChangeBuffersFormat( xfrm, from_type(input_type), from_type(output_type) ))
351 return FALSE;
353 switch (input_type)
355 case COLOR_RGB:
357 switch (output_type)
359 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].rgb, 1 ); goto done;
360 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].Lab, 1 ); goto done;
361 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].gray, 1 ); goto done;
362 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].cmyk, 1 ); goto done;
363 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].XYZ, 1 ); goto done;
364 default:
365 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
366 ret = FALSE;
367 break;
369 break;
371 case COLOR_Lab:
373 switch (output_type)
375 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].rgb, 1 ); goto done;
376 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].Lab, 1 ); goto done;
377 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].gray, 1 ); goto done;
378 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].cmyk, 1 ); goto done;
379 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].XYZ, 1 ); goto done;
380 default:
381 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
382 ret = FALSE;
383 break;
385 break;
387 case COLOR_GRAY:
389 switch (output_type)
391 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].rgb, 1 ); goto done;
392 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].Lab, 1 ); goto done;
393 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].gray, 1 ); goto done;
394 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].cmyk, 1 ); goto done;
395 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].XYZ, 1 ); goto done;
396 default:
397 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
398 ret = FALSE;
399 break;
401 break;
403 case COLOR_CMYK:
405 switch (output_type)
407 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].rgb, 1 ); goto done;
408 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].Lab, 1 ); goto done;
409 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].gray, 1 ); goto done;
410 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].cmyk, 1 ); goto done;
411 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].XYZ, 1 ); goto done;
412 default:
413 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
414 ret = FALSE;
415 break;
417 break;
419 case COLOR_XYZ:
421 switch (output_type)
423 case COLOR_RGB: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].rgb, 1 ); goto done;
424 case COLOR_Lab: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].Lab, 1 ); goto done;
425 case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].gray, 1 ); goto done;
426 case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].cmyk, 1 ); goto done;
427 case COLOR_XYZ: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].XYZ, 1 ); goto done;
428 default:
429 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
430 ret = FALSE;
431 break;
433 break;
435 default:
436 FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
437 ret = FALSE;
438 break;
441 done:
442 release_transform( transform );
443 return ret;
445 #else /* HAVE_LCMS2 */
446 return FALSE;
447 #endif /* HAVE_LCMS2 */