Handle wParam in WM_PAINT properly: if non-null, it is the hdc we are
[wine] / dlls / mscms / profile.c
1 /*
2  * MSCMS - Color Management System for Wine
3  *
4  * Copyright 2004, 2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/debug.h"
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "icm.h"
32
33 #define LCMS_API_FUNCTION(f) extern typeof(f) * p##f;
34 #include "lcms_api.h"
35 #undef LCMS_API_FUNCTION
36
37 #define IS_SEPARATOR(ch)  ((ch) == '\\' || (ch) == '/')
38
39 static void MSCMS_basename( LPCWSTR path, LPWSTR name )
40 {
41     INT i = lstrlenW( path );
42
43     while (i > 0 && !IS_SEPARATOR(path[i - 1])) i--;
44     lstrcpyW( name, &path[i] );
45 }
46
47 static const WCHAR rgbprofile[] =
48 { 'c',':','\\','w','i','n','d','o','w','s','\\', 's','y','s','t','e','m','3','2',
49   '\\','s','p','o','o','l','\\','d','r','i','v','e','r','s',
50   '\\','c','o','l','o','r','\\','s','r','g','b',' ','c','o','l','o','r',' ',
51   's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
52
53 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
54
55 /******************************************************************************
56  * GetColorDirectoryA               [MSCMS.@]
57  *
58  * See GetColorDirectoryW.
59  */
60 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
61 {
62     INT len;
63     LPWSTR bufferW;
64     BOOL ret = FALSE;
65     DWORD sizeW;
66
67     TRACE( "( %p, %p )\n", buffer, size );
68
69     if (machine || !size) return FALSE;
70
71     if (!buffer)
72     {
73         ret = GetColorDirectoryW( NULL, NULL, &sizeW );
74         *size = sizeW / sizeof(WCHAR);
75         return FALSE;
76     }
77
78     sizeW = *size * sizeof(WCHAR);
79
80     bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
81
82     if (bufferW)
83     {
84         ret = GetColorDirectoryW( NULL, bufferW, &sizeW );
85         *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
86
87         if (ret)
88         {
89             len = WideCharToMultiByte( CP_ACP, 0, bufferW, *size, buffer, *size, NULL, NULL );
90             if (!len) ret = FALSE;
91         }
92
93         HeapFree( GetProcessHeap(), 0, bufferW );
94     }
95     return ret;
96 }
97
98 /******************************************************************************
99  * GetColorDirectoryW               [MSCMS.@]
100  *
101  * Get the directory where color profiles are stored.
102  *
103  * PARAMS
104  *  machine  [I]   Name of the machine for which to get the color directory.
105  *                 Must be NULL, which indicates the local machine.
106  *  buffer   [I]   Buffer to receive the path name.
107  *  size     [I/O] Size of the buffer in bytes. On return the variable holds
108  *                 the number of bytes actually needed.
109  */
110 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
111 {
112     /* FIXME: Get this directory from the registry? */
113     static const WCHAR colordir[] =
114     { 'c',':','\\','w','i','n','d','o','w','s','\\', 's','y','s','t','e','m','3','2',
115       '\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r',0 };
116
117     DWORD len;
118
119     TRACE( "( %p, %p )\n", buffer, size );
120
121     if (machine || !size) return FALSE;
122
123     len = lstrlenW( colordir ) * sizeof(WCHAR);
124
125     if (len <= *size && buffer)
126     {
127         lstrcpyW( buffer, colordir );
128         *size = len;
129         return TRUE;
130     }
131
132     *size = len;
133     return FALSE;
134 }
135
136 /******************************************************************************
137  * GetColorProfileElement               [MSCMS.@]
138  *
139  * Retrieve data for a specified tag type.
140  *
141  * PARAMS
142  *  profile  [I]   Handle to a color profile.
143  *  type     [I]   ICC tag type. 
144  *  offset   [I]   Offset in bytes to start copying from. 
145  *  size     [I/O] Size of the buffer in bytes. On return the variable holds
146  *                 the number of bytes actually needed.
147  *  buffer   [O]   Buffer to receive the tag data.
148  *  ref      [O]   Pointer to a BOOL that specifies whether more than one tag
149  *                 references the data.
150  *
151  * RETURNS
152  *  Success: TRUE
153  *  Failure: FALSE
154  */
155 BOOL WINAPI GetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
156                                     PVOID buffer, PBOOL ref )
157 {
158     BOOL ret = FALSE;
159 #ifdef HAVE_LCMS_H
160     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
161     DWORD i, count;
162     icTag tag;
163
164     TRACE( "( %p, 0x%08lx, %ld, %p, %p, %p )\n", profile, type, offset, size, buffer, ref );
165
166     if (!iccprofile || !size || !ref) return FALSE;
167     count = MSCMS_get_tag_count( iccprofile );
168
169     for (i = 0; i < count; i++)
170     {
171         MSCMS_get_tag_by_index( iccprofile, i, &tag );
172
173         if (tag.sig == type)
174         {
175             if ((tag.size - offset) > *size || !buffer)
176             {
177                 *size = (tag.size - offset);
178                 return FALSE;
179             }
180
181             MSCMS_get_tag_data( iccprofile, &tag, offset, buffer );
182
183             *ref = FALSE; /* FIXME: calculate properly */
184             return TRUE;
185         }
186     }
187
188 #endif /* HAVE_LCMS_H */
189     return ret;
190 }
191
192 /******************************************************************************
193  * GetColorProfileElementTag               [MSCMS.@]
194  *
195  * Get the tag type from a color profile by index. 
196  *
197  * PARAMS
198  *  profile  [I]   Handle to a color profile.
199  *  index    [I]   Index into the tag table of the color profile.
200  *  type     [O]   Pointer to a variable that holds the ICC tag type on return.
201  *
202  * RETURNS
203  *  Success: TRUE
204  *  Failure: FALSE
205  *
206  * NOTES
207  *  The tag table index starts at 1.
208  *  Use GetCountColorProfileElements to retrieve a count of tagged elements.
209  */
210 BOOL WINAPI GetColorProfileElementTag( HPROFILE profile, DWORD index, PTAGTYPE type )
211 {
212     BOOL ret = FALSE;
213 #ifdef HAVE_LCMS_H
214     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
215     DWORD count;
216     icTag tag;
217
218     TRACE( "( %p, %ld, %p )\n", profile, index, type );
219
220     if (!iccprofile || !type) return FALSE;
221
222     count = MSCMS_get_tag_count( iccprofile );
223     if (index > count || index < 1) return FALSE;
224
225     MSCMS_get_tag_by_index( iccprofile, index - 1, &tag );
226     *type = tag.sig;
227
228     ret = TRUE;
229
230 #endif /* HAVE_LCMS_H */
231     return ret;
232 }
233
234 /******************************************************************************
235  * GetColorProfileFromHandle               [MSCMS.@]
236  *
237  * Retrieve an ICC color profile by handle.
238  *
239  * PARAMS
240  *  profile  [I]   Handle to a color profile.
241  *  buffer   [O]   Buffer to receive the ICC profile.
242  *  size     [I/O] Size of the buffer in bytes. On return the variable holds the
243  *                 number of bytes actually needed.
244  *
245  * RETURNS
246  *  Success: TRUE
247  *  Failure: FALSE
248  *
249  * NOTES
250  *  The profile returned will be in big-endian format.
251  */
252 BOOL WINAPI GetColorProfileFromHandle( HPROFILE profile, PBYTE buffer, PDWORD size )
253 {
254     BOOL ret = FALSE;
255 #ifdef HAVE_LCMS_H
256     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
257     PROFILEHEADER header;
258
259     TRACE( "( %p, %p, %p )\n", profile, buffer, size );
260
261     if (!iccprofile || !size) return FALSE;
262     MSCMS_get_profile_header( iccprofile, &header );
263
264     if (!buffer || header.phSize > *size)
265     {
266         *size = header.phSize;
267         return FALSE;
268     }
269
270     /* No endian conversion needed */
271     memcpy( buffer, iccprofile, header.phSize );
272
273     *size = header.phSize;
274     ret = TRUE;
275
276 #endif /* HAVE_LCMS_H */
277     return ret;
278 }
279
280 /******************************************************************************
281  * GetColorProfileHeader               [MSCMS.@]
282  *
283  * Retrieve a color profile header by handle.
284  *
285  * PARAMS
286  *  profile  [I]   Handle to a color profile.
287  *  header   [O]   Buffer to receive the ICC profile header.
288  *
289  * RETURNS
290  *  Success: TRUE
291  *  Failure: FALSE
292  *
293  * NOTES
294  *  The profile header returned will be adjusted for endianess.
295  */
296 BOOL WINAPI GetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
297 {
298     BOOL ret = FALSE;
299 #ifdef HAVE_LCMS_H
300     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
301
302     TRACE( "( %p, %p )\n", profile, header );
303
304     if (!iccprofile || !header) return FALSE;
305
306     MSCMS_get_profile_header( iccprofile, header );
307     return TRUE;
308
309 #endif /* HAVE_LCMS_H */
310     return ret;
311 }
312
313 /******************************************************************************
314  * GetCountColorProfileElements               [MSCMS.@]
315  *
316  * Retrieve the number of elements in a color profile.
317  *
318  * PARAMS
319  *  profile  [I] Handle to a color profile.
320  *  count    [O] Pointer to a variable which is set to the number of elements
321  *               in the color profile.
322  *
323  * RETURNS
324  *  Success: TRUE
325  *  Failure: FALSE
326  */
327 BOOL WINAPI GetCountColorProfileElements( HPROFILE profile, PDWORD count )
328 {
329     BOOL ret = FALSE;
330 #ifdef HAVE_LCMS_H
331     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
332
333     TRACE( "( %p, %p )\n", profile, count );
334
335     if (!iccprofile || !count) return FALSE;
336     *count = MSCMS_get_tag_count( iccprofile );
337     ret = TRUE;
338
339 #endif /* HAVE_LCMS_H */
340     return ret;
341 }
342
343 /******************************************************************************
344  * GetStandardColorSpaceProfileA               [MSCMS.@]
345  *
346  * See GetStandardColorSpaceProfileW.
347  */
348 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
349 {
350     INT len;
351     LPWSTR profileW;
352     BOOL ret = FALSE;
353     DWORD sizeW;
354
355     TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
356
357     if (machine || !size) return FALSE;
358
359     sizeW = *size * sizeof(WCHAR);
360
361     if (!profile)
362     {
363         ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
364         *size = sizeW / sizeof(WCHAR);
365         return FALSE;
366     }
367
368     profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
369
370     if (profileW)
371     {
372         ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW );
373         *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
374
375         if (ret)
376         {
377             len = WideCharToMultiByte( CP_ACP, 0, profileW, *size, profile, *size, NULL, NULL );
378             if (!len) ret = FALSE;
379         }
380
381         HeapFree( GetProcessHeap(), 0, profileW );
382     }
383     return ret;
384 }
385
386 /******************************************************************************
387  * GetStandardColorSpaceProfileW               [MSCMS.@]
388  *
389  * Retrieve the profile filename for a given standard color space id.
390  *
391  * PARAMS
392  *  machine  [I]   Name of the machine for which to get the standard color space.
393  *                 Must be NULL, which indicates the local machine.
394  *  id       [I]   Id of a standard color space.
395  *  profile  [O]   Buffer to receive the profile filename.
396  *  size     [I/O] Size of the filename buffer in bytes.
397  *
398  * RETURNS
399  *  Success: TRUE
400  *  Failure: FALSE
401  */
402 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
403 {
404     DWORD len;
405
406     TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
407
408     if (machine || !size) return FALSE;
409
410     switch (id)
411     {
412         case 0x52474220: /* 'RGB ' */
413             len = sizeof( rgbprofile );
414
415             if (*size < len || !profile)
416             {
417                 *size = len;
418                 return TRUE;
419             }
420
421             lstrcpyW( profile, rgbprofile );
422             break;
423
424         default:
425             return FALSE;
426     }
427
428     return TRUE;
429 }
430
431 /******************************************************************************
432  * InstallColorProfileA               [MSCMS.@]
433  *
434  * See InstallColorProfileW.
435  */
436 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
437 {
438     UINT len;
439     LPWSTR profileW;
440     BOOL ret = FALSE;
441
442     TRACE( "( %s )\n", debugstr_a(profile) );
443
444     if (machine || !profile) return FALSE;
445
446     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
447     profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
448
449     if (profileW)
450     {
451         MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
452
453         ret = InstallColorProfileW( NULL, profileW );
454         HeapFree( GetProcessHeap(), 0, profileW );
455     }
456     return ret;
457 }
458
459 /******************************************************************************
460  * InstallColorProfileW               [MSCMS.@]
461  *
462  * Install a color profile.
463  *
464  * PARAMS
465  *  machine  [I] Name of the machine to install the profile on. Must be NULL,
466  *               which indicates the local machine.
467  *  profile  [I] Full path name of the profile to install.
468  *
469  * RETURNS
470  *  Success: TRUE
471  *  Failure: FALSE
472  */
473 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
474 {
475     WCHAR dest[MAX_PATH], base[MAX_PATH];
476     DWORD size = sizeof(dest);
477     static const WCHAR slash[] = { '\\', 0 };
478
479     TRACE( "( %s )\n", debugstr_w(profile) );
480
481     if (machine || !profile) return FALSE;
482
483     if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
484
485     MSCMS_basename( profile, base );
486
487     lstrcatW( dest, slash );
488     lstrcatW( dest, base );
489
490     /* Is source equal to destination? */
491     if (!lstrcmpW( profile, dest )) return TRUE;
492
493     return CopyFileW( profile, dest, TRUE );
494 }
495
496 /******************************************************************************
497  * IsColorProfileTagPresent               [MSCMS.@]
498  *
499  * Determine if a given ICC tag type is present in a color profile.
500  *
501  * PARAMS
502  *  profile  [I] Color profile handle.
503  *  tag      [I] ICC tag type.
504  *  present  [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
505  *               FALSE otherwise.
506  *
507  * RETURNS
508  *  Success: TRUE
509  *  Failure: FALSE
510  */
511 BOOL WINAPI IsColorProfileTagPresent( HPROFILE profile, TAGTYPE type, PBOOL present )
512 {
513     BOOL ret = FALSE;
514 #ifdef HAVE_LCMS_H
515     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
516     DWORD i, count;
517     icTag tag;
518
519     TRACE( "( %p, 0x%08lx, %p )\n", profile, type, present );
520
521     if (!iccprofile || !present) return FALSE;
522
523     count = MSCMS_get_tag_count( iccprofile );
524
525     for (i = 0; i < count; i++)
526     {
527         MSCMS_get_tag_by_index( iccprofile, i, &tag );
528
529         if (tag.sig == type)
530         {
531             *present = ret = TRUE;
532             break;
533         }
534     }
535
536 #endif /* HAVE_LCMS_H */
537     return ret;
538 }
539
540 /******************************************************************************
541  * IsColorProfileValid               [MSCMS.@]
542  *
543  * Determine if a given color profile is valid.
544  *
545  * PARAMS
546  *  profile  [I] Color profile handle.
547  *  valid    [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
548  *               FALSE otherwise.
549  *
550  * RETURNS
551  *  Success: TRUE
552  *  Failure: FALSE 
553  */
554 BOOL WINAPI IsColorProfileValid( HPROFILE profile, PBOOL valid )
555 {
556     BOOL ret = FALSE;
557 #ifdef HAVE_LCMS_H
558     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
559
560     TRACE( "( %p, %p )\n", profile, valid );
561
562     if (!valid) return FALSE;
563     if (iccprofile) return *valid = TRUE;
564
565 #endif /* HAVE_LCMS_H */
566     return ret;
567 }
568
569 /******************************************************************************
570  * SetColorProfileElement               [MSCMS.@]
571  *
572  * Set data for a specified tag type.
573  *
574  * PARAMS
575  *  profile  [I]   Handle to a color profile.
576  *  type     [I]   ICC tag type.
577  *  offset   [I]   Offset in bytes to start copying to.
578  *  size     [I/O] Size of the buffer in bytes. On return the variable holds the
579  *                 number of bytes actually needed.
580  *  buffer   [O]   Buffer holding the tag data.
581  *
582  * RETURNS
583  *  Success: TRUE
584  *  Failure: FALSE
585  */
586 BOOL WINAPI SetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
587                                     PVOID buffer )
588 {
589     BOOL ret = FALSE;
590 #ifdef HAVE_LCMS_H
591     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
592     DWORD i, count, access = MSCMS_hprofile2access( profile );
593     icTag tag;
594
595     TRACE( "( %p, 0x%08lx, %ld, %p, %p )\n", profile, type, offset, size, buffer );
596
597     if (!iccprofile || !size || !buffer) return FALSE;
598     if (!(access & PROFILE_READWRITE)) return FALSE;
599
600     count = MSCMS_get_tag_count( iccprofile );
601
602     for (i = 0; i < count; i++)
603     {
604         MSCMS_get_tag_by_index( iccprofile, i, &tag );
605
606         if (tag.sig == type)
607         {
608             if (offset > tag.size) return FALSE;
609
610             MSCMS_set_tag_data( iccprofile, &tag, offset, buffer );
611             return TRUE;
612         }
613     }
614
615 #endif /* HAVE_LCMS_H */
616     return ret;
617 }
618
619 /******************************************************************************
620  * SetColorProfileHeader               [MSCMS.@]
621  *
622  * Set header data for a given profile.
623  *
624  * PARAMS
625  *  profile  [I] Handle to a color profile.
626  *  header   [I] Buffer holding the header data.
627  *
628  * RETURNS
629  *  Success: TRUE
630  *  Failure: FALSE
631  */
632 BOOL WINAPI SetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
633 {
634     BOOL ret = FALSE;
635 #ifdef HAVE_LCMS_H
636     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
637     DWORD access = MSCMS_hprofile2access( profile );
638
639     TRACE( "( %p, %p )\n", profile, header );
640
641     if (!iccprofile || !header) return FALSE;
642     if (!(access & PROFILE_READWRITE)) return FALSE;
643
644     MSCMS_set_profile_header( iccprofile, header );
645     return TRUE;
646
647 #endif /* HAVE_LCMS_H */
648     return ret;
649 }
650
651 /******************************************************************************
652  * UninstallColorProfileA               [MSCMS.@]
653  *
654  * See UninstallColorProfileW.
655  */
656 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
657 {
658     UINT len;
659     LPWSTR profileW;
660     BOOL ret = FALSE;
661
662     TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
663
664     if (machine || !profile) return FALSE;
665
666     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
667     profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
668
669     if (profileW)
670     {
671         MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
672
673         ret = UninstallColorProfileW( NULL, profileW , delete );
674
675         HeapFree( GetProcessHeap(), 0, profileW );
676     }
677     return ret;
678 }
679
680 /******************************************************************************
681  * UninstallColorProfileW               [MSCMS.@]
682  *
683  * Uninstall a color profile.
684  *
685  * PARAMS
686  *  machine  [I] Name of the machine to uninstall the profile on. Must be NULL,
687  *               which indicates the local machine.
688  *  profile  [I] Full path name of the profile to uninstall.
689  *  delete   [I] Bool that specifies whether the profile file should be deleted.
690  *
691  * RETURNS
692  *  Success: TRUE
693  *  Failure: FALSE
694  */
695 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
696 {
697     TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
698
699     if (machine || !profile) return FALSE;
700
701     if (delete) return DeleteFileW( profile );
702
703     return TRUE;
704 }
705
706 /******************************************************************************
707  * OpenColorProfileA               [MSCMS.@]
708  *
709  * See OpenColorProfileW.
710  */
711 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
712 {
713     HPROFILE handle = NULL;
714
715     TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
716
717     if (!profile || !profile->pProfileData) return NULL;
718
719     /* No AW conversion needed for memory based profiles */
720     if (profile->dwType & PROFILE_MEMBUFFER)
721         return OpenColorProfileW( profile, access, sharing, creation );
722
723     if (profile->dwType & PROFILE_FILENAME)
724     {
725         UINT len;
726         PROFILE profileW;
727
728         profileW.dwType = profile->dwType;
729  
730         len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
731         profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
732
733         if (profileW.pProfileData)
734         {
735             profileW.cbDataSize = len * sizeof(WCHAR);
736             MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
737
738             handle = OpenColorProfileW( &profileW, access, sharing, creation );
739             HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
740         }
741     }
742     return handle;
743 }
744
745 /******************************************************************************
746  * OpenColorProfileW               [MSCMS.@]
747  *
748  * Open a color profile.
749  *
750  * PARAMS
751  *  profile   [I] Pointer to a color profile structure.
752  *  access    [I] Desired access.
753  *  sharing   [I] Sharing mode.
754  *  creation  [I] Creation mode.
755  *
756  * RETURNS
757  *  Success: Handle to the opened profile.
758  *  Failure: NULL
759  *
760  * NOTES
761  *  Values for access:   PROFILE_READ or PROFILE_READWRITE.
762  *  Values for sharing:  0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
763  *  Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
764  *                       OPEN_ALWAYS, TRUNCATE_EXISTING.
765  *  Sharing and creation flags are ignored for memory based profiles.
766  */
767 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
768 {
769 #ifdef HAVE_LCMS_H
770     cmsHPROFILE cmsprofile = NULL;
771     icProfile *iccprofile = NULL;
772     HANDLE handle = NULL;
773     DWORD size;
774
775     TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
776
777     if (!profile || !profile->pProfileData) return NULL;
778
779     if (profile->dwType & PROFILE_MEMBUFFER)
780     {
781         FIXME( "access flags not implemented for memory based profiles\n" );
782
783         iccprofile = profile->pProfileData;
784         size = profile->cbDataSize;
785     
786         cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
787     }
788
789     if (profile->dwType & PROFILE_FILENAME)
790     {
791         DWORD read, flags = 0;
792
793         TRACE( "profile file: %s\n", debugstr_w( (WCHAR *)profile->pProfileData ) );
794
795         if (access & PROFILE_READ) flags = GENERIC_READ;
796         if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
797
798         if (!flags) return NULL;
799
800         handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
801         if (handle == INVALID_HANDLE_VALUE)
802         {
803             WARN( "Unable to open color profile\n" );
804             return NULL;
805         }
806
807         if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
808         {
809             ERR( "Unable to retrieve size of color profile\n" );
810             CloseHandle( handle );
811             return NULL;
812         }
813
814         iccprofile = HeapAlloc( GetProcessHeap(), 0, size );
815         if (!iccprofile)
816         {
817             ERR( "Unable to allocate memory for color profile\n" );
818             CloseHandle( handle );
819             return NULL;
820         }
821
822         if (!ReadFile( handle, iccprofile, size, &read, NULL ) || read != size)
823         {
824             ERR( "Unable to read color profile\n" );
825
826             CloseHandle( handle );
827             HeapFree( GetProcessHeap, 0, iccprofile );
828             return NULL;
829         }
830
831         cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
832     }
833
834     if (cmsprofile)
835         return MSCMS_create_hprofile_handle( handle, iccprofile, cmsprofile, access );
836
837 #endif /* HAVE_LCMS_H */
838     return NULL;
839 }
840
841 /******************************************************************************
842  * CloseColorProfile               [MSCMS.@]
843  *
844  * Close a color profile.
845  *
846  * PARAMS
847  *  profile  [I] Handle to the profile.
848  *
849  * RETURNS
850  *  Success: TRUE
851  *  Failure: FALSE
852  */
853 BOOL WINAPI CloseColorProfile( HPROFILE profile )
854 {
855     BOOL ret = FALSE;
856 #ifdef HAVE_LCMS_H
857     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
858     HANDLE file = MSCMS_hprofile2handle( profile );
859     DWORD access = MSCMS_hprofile2access( profile );
860
861     TRACE( "( %p )\n", profile );
862
863     if (file && (access & PROFILE_READWRITE))
864     {
865         DWORD written, size = MSCMS_get_profile_size( iccprofile );
866
867         if (SetFilePointer( file, 0, NULL, FILE_BEGIN ) ||
868             !WriteFile( file, iccprofile, size, &written, NULL ) || written != size)
869             ERR( "Unable to write color profile\n" );
870     }
871
872     ret = cmsCloseProfile( MSCMS_hprofile2cmsprofile( profile ) );
873     HeapFree( GetProcessHeap(), 0, MSCMS_hprofile2iccprofile( profile ) );
874
875     CloseHandle( MSCMS_hprofile2handle( profile ) );
876     MSCMS_destroy_hprofile_handle( profile );
877
878 #endif /* HAVE_LCMS_H */
879     return ret;
880 }