Implements OleLoadPicturePath.
[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 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
344 {
345     INT len;
346     LPWSTR profileW;
347     BOOL ret = FALSE;
348     DWORD sizeW;
349
350     TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
351
352     if (machine || !size) return FALSE;
353
354     sizeW = *size * sizeof(WCHAR);
355
356     if (!profile)
357     {
358         ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
359         *size = sizeW / sizeof(WCHAR);
360         return FALSE;
361     }
362
363     profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
364
365     if (profileW)
366     {
367         ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW );
368         *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
369
370         if (ret)
371         {
372             len = WideCharToMultiByte( CP_ACP, 0, profileW, *size, profile, *size, NULL, NULL );
373             if (!len) ret = FALSE;
374         }
375
376         HeapFree( GetProcessHeap(), 0, profileW );
377     }
378     return ret;
379 }
380
381 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
382 {
383     DWORD len;
384
385     TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
386
387     if (machine || !size) return FALSE;
388
389     switch (id)
390     {
391         case 0x52474220: /* 'RGB ' */
392             len = sizeof( rgbprofile );
393
394             if (*size < len || !profile)
395             {
396                 *size = len;
397                 return TRUE;
398             }
399
400             lstrcpyW( profile, rgbprofile );
401             break;
402
403         default:
404             return FALSE;
405     }
406
407     return TRUE;
408 }
409
410 /******************************************************************************
411  * InstallColorProfileA               [MSCMS.@]
412  *
413  * See InstallColorProfileW.
414  */
415 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
416 {
417     UINT len;
418     LPWSTR profileW;
419     BOOL ret = FALSE;
420
421     TRACE( "( %s )\n", debugstr_a(profile) );
422
423     if (machine || !profile) return FALSE;
424
425     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
426     profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
427
428     if (profileW)
429     {
430         MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
431
432         ret = InstallColorProfileW( NULL, profileW );
433         HeapFree( GetProcessHeap(), 0, profileW );
434     }
435     return ret;
436 }
437
438 /******************************************************************************
439  * InstallColorProfileW               [MSCMS.@]
440  *
441  * Install a color profile.
442  *
443  * PARAMS
444  *  machine  [I] Name of the machine to install the profile on. Must be NULL,
445  *               which indicates the local machine.
446  *  profile  [I] Full path name of the profile to install.
447  *
448  * RETURNS
449  *  Success: TRUE
450  *  Failure: FALSE
451  */
452 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
453 {
454     WCHAR dest[MAX_PATH], base[MAX_PATH];
455     DWORD size = sizeof(dest);
456     static const WCHAR slash[] = { '\\', 0 };
457
458     TRACE( "( %s )\n", debugstr_w(profile) );
459
460     if (machine || !profile) return FALSE;
461
462     if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
463
464     MSCMS_basename( profile, base );
465
466     lstrcatW( dest, slash );
467     lstrcatW( dest, base );
468
469     /* Is source equal to destination? */
470     if (!lstrcmpW( profile, dest )) return TRUE;
471
472     return CopyFileW( profile, dest, TRUE );
473 }
474
475 /******************************************************************************
476  * IsColorProfileTagPresent               [MSCMS.@]
477  *
478  * Determine if a given ICC tag type is present in a color profile.
479  *
480  * PARAMS
481  *  profile  [I] Color profile handle.
482  *  tag      [I] ICC tag type.
483  *  present  [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
484  *               FALSE otherwise.
485  *
486  * RETURNS
487  *  Success: TRUE
488  *  Failure: FALSE
489  */
490 BOOL WINAPI IsColorProfileTagPresent( HPROFILE profile, TAGTYPE type, PBOOL present )
491 {
492     BOOL ret = FALSE;
493 #ifdef HAVE_LCMS_H
494     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
495     DWORD i, count;
496     icTag tag;
497
498     TRACE( "( %p, 0x%08lx, %p )\n", profile, type, present );
499
500     if (!iccprofile || !present) return FALSE;
501
502     count = MSCMS_get_tag_count( iccprofile );
503
504     for (i = 0; i < count; i++)
505     {
506         MSCMS_get_tag_by_index( iccprofile, i, &tag );
507
508         if (tag.sig == type)
509         {
510             *present = ret = TRUE;
511             break;
512         }
513     }
514
515 #endif /* HAVE_LCMS_H */
516     return ret;
517 }
518
519 /******************************************************************************
520  * IsColorProfileValid               [MSCMS.@]
521  *
522  * Determine if a given color profile is valid.
523  *
524  * PARAMS
525  *  profile  [I] Color profile handle.
526  *  valid    [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
527  *               FALSE otherwise.
528  *
529  * RETURNS
530  *  Success: TRUE
531  *  Failure: FALSE 
532  */
533 BOOL WINAPI IsColorProfileValid( HPROFILE profile, PBOOL valid )
534 {
535     BOOL ret = FALSE;
536 #ifdef HAVE_LCMS_H
537     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
538
539     TRACE( "( %p, %p )\n", profile, valid );
540
541     if (!valid) return FALSE;
542     if (iccprofile) return *valid = TRUE;
543
544 #endif /* HAVE_LCMS_H */
545     return ret;
546 }
547
548 /******************************************************************************
549  * SetColorProfileElement               [MSCMS.@]
550  *
551  * Set data for a specified tag type.
552  *
553  * PARAMS
554  *  profile  [I]   Handle to a color profile.
555  *  type     [I]   ICC tag type.
556  *  offset   [I]   Offset in bytes to start copying to.
557  *  size     [I/O] Size of the buffer in bytes. On return the variable holds the
558  *                 number of bytes actually needed.
559  *  buffer   [O]   Buffer holding the tag data.
560  *
561  * RETURNS
562  *  Success: TRUE
563  *  Failure: FALSE
564  */
565 BOOL WINAPI SetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
566                                     PVOID buffer )
567 {
568     BOOL ret = FALSE;
569 #ifdef HAVE_LCMS_H
570     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
571     DWORD i, count, access = MSCMS_hprofile2access( profile );
572     icTag tag;
573
574     TRACE( "( %p, 0x%08lx, %ld, %p, %p )\n", profile, type, offset, size, buffer );
575
576     if (!iccprofile || !size || !buffer) return FALSE;
577     if (!(access & PROFILE_READWRITE)) return FALSE;
578
579     count = MSCMS_get_tag_count( iccprofile );
580
581     for (i = 0; i < count; i++)
582     {
583         MSCMS_get_tag_by_index( iccprofile, i, &tag );
584
585         if (tag.sig == type)
586         {
587             if (offset > tag.size) return FALSE;
588
589             MSCMS_set_tag_data( iccprofile, &tag, offset, buffer );
590             return TRUE;
591         }
592     }
593
594 #endif /* HAVE_LCMS_H */
595     return ret;
596 }
597
598 BOOL WINAPI SetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
599 {
600     BOOL ret = FALSE;
601 #ifdef HAVE_LCMS_H
602     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
603     DWORD access = MSCMS_hprofile2access( profile );
604
605     TRACE( "( %p, %p )\n", profile, header );
606
607     if (!iccprofile || !header) return FALSE;
608     if (!(access & PROFILE_READWRITE)) return FALSE;
609
610     MSCMS_set_profile_header( iccprofile, header );
611     return TRUE;
612
613 #endif /* HAVE_LCMS_H */
614     return ret;
615 }
616
617 BOOL WINAPI SetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile )
618 {
619     FIXME( "( 0x%08lx, %p ) stub\n", id, profile );
620
621     return TRUE;
622 }
623
624 BOOL WINAPI SetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile )
625 {
626     FIXME( "( 0x%08lx, %p ) stub\n", id, profile );
627
628     return TRUE;
629 }
630
631 /******************************************************************************
632  * UninstallColorProfileA               [MSCMS.@]
633  *
634  * See UninstallColorProfileW.
635  */
636 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
637 {
638     UINT len;
639     LPWSTR profileW;
640     BOOL ret = FALSE;
641
642     TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
643
644     if (machine || !profile) return FALSE;
645
646     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
647     profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
648
649     if (profileW)
650     {
651         MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
652
653         ret = UninstallColorProfileW( NULL, profileW , delete );
654
655         HeapFree( GetProcessHeap(), 0, profileW );
656     }
657     return ret;
658 }
659
660 /******************************************************************************
661  * UninstallColorProfileW               [MSCMS.@]
662  *
663  * Uninstall a color profile.
664  *
665  * PARAMS
666  *  machine  [I] Name of the machine to uninstall the profile on. Must be NULL,
667  *               which indicates the local machine.
668  *  profile  [I] Full path name of the profile to uninstall.
669  *  delete   [I] Bool that specifies whether the profile file should be deleted.
670  *
671  * RETURNS
672  *  Success: TRUE
673  *  Failure: FALSE
674  */
675 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
676 {
677     TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
678
679     if (machine || !profile) return FALSE;
680
681     if (delete) return DeleteFileW( profile );
682
683     return TRUE;
684 }
685
686 /******************************************************************************
687  * OpenColorProfileA               [MSCMS.@]
688  *
689  * See OpenColorProfileW.
690  */
691 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
692 {
693     HPROFILE handle = NULL;
694
695     TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
696
697     if (!profile || !profile->pProfileData) return NULL;
698
699     /* No AW conversion needed for memory based profiles */
700     if (profile->dwType & PROFILE_MEMBUFFER)
701         return OpenColorProfileW( profile, access, sharing, creation );
702
703     if (profile->dwType & PROFILE_FILENAME)
704     {
705         UINT len;
706         PROFILE profileW;
707
708         profileW.dwType = profile->dwType;
709  
710         len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
711         profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
712
713         if (profileW.pProfileData)
714         {
715             profileW.cbDataSize = len * sizeof(WCHAR);
716             MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
717
718             handle = OpenColorProfileW( &profileW, access, sharing, creation );
719             HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
720         }
721     }
722     return handle;
723 }
724
725 /******************************************************************************
726  * OpenColorProfileW               [MSCMS.@]
727  *
728  * Open a color profile.
729  *
730  * PARAMS
731  *  profile   [I] Pointer to a color profile structure.
732  *  access    [I] Desired access.
733  *  sharing   [I] Sharing mode.
734  *  creation  [I] Creation mode.
735  *
736  * RETURNS
737  *  Success: Handle to the opened profile.
738  *  Failure: NULL
739  *
740  * NOTES
741  *  Values for access:   PROFILE_READ or PROFILE_READWRITE.
742  *  Values for sharing:  0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
743  *  Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
744  *                       OPEN_ALWAYS, TRUNCATE_EXISTING.
745  *  Sharing and creation flags are ignored for memory based profiles.
746  */
747 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
748 {
749 #ifdef HAVE_LCMS_H
750     cmsHPROFILE cmsprofile = NULL;
751     icProfile *iccprofile = NULL;
752     HANDLE handle = NULL;
753     DWORD size;
754
755     TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
756
757     if (!profile || !profile->pProfileData) return NULL;
758
759     if (profile->dwType & PROFILE_MEMBUFFER)
760     {
761         FIXME( "access flags not implemented for memory based profiles\n" );
762
763         iccprofile = profile->pProfileData;
764         size = profile->cbDataSize;
765     
766         cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
767     }
768
769     if (profile->dwType & PROFILE_FILENAME)
770     {
771         DWORD read, flags = 0;
772
773         TRACE( "profile file: %s\n", debugstr_w( (WCHAR *)profile->pProfileData ) );
774
775         if (access & PROFILE_READ) flags = GENERIC_READ;
776         if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
777
778         if (!flags) return NULL;
779
780         handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
781         if (handle == INVALID_HANDLE_VALUE)
782         {
783             WARN( "Unable to open color profile\n" );
784             return NULL;
785         }
786
787         if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
788         {
789             ERR( "Unable to retrieve size of color profile\n" );
790             CloseHandle( handle );
791             return NULL;
792         }
793
794         iccprofile = (icProfile *)HeapAlloc( GetProcessHeap(), 0, size );
795         if (!iccprofile)
796         {
797             ERR( "Unable to allocate memory for color profile\n" );
798             CloseHandle( handle );
799             return NULL;
800         }
801
802         if (!ReadFile( handle, iccprofile, size, &read, NULL ) || read != size)
803         {
804             ERR( "Unable to read color profile\n" );
805
806             CloseHandle( handle );
807             HeapFree( GetProcessHeap, 0, iccprofile );
808             return NULL;
809         }
810
811         cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
812     }
813
814     if (cmsprofile)
815         return MSCMS_create_hprofile_handle( handle, iccprofile, cmsprofile, access );
816
817 #endif /* HAVE_LCMS_H */
818     return NULL;
819 }
820
821 /******************************************************************************
822  * CloseColorProfile               [MSCMS.@]
823  *
824  * Close a color profile.
825  *
826  * PARAMS
827  *  profile  [I] Handle to the profile.
828  *
829  * RETURNS
830  *  Success: TRUE
831  *  Failure: FALSE
832  */
833 BOOL WINAPI CloseColorProfile( HPROFILE profile )
834 {
835     BOOL ret = FALSE;
836 #ifdef HAVE_LCMS_H
837     icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
838     HANDLE file = MSCMS_hprofile2handle( profile );
839     DWORD access = MSCMS_hprofile2access( profile );
840
841     TRACE( "( %p )\n", profile );
842
843     if (file && (access & PROFILE_READWRITE))
844     {
845         DWORD written, size = MSCMS_get_profile_size( iccprofile );
846
847         if (SetFilePointer( file, 0, NULL, FILE_BEGIN ) ||
848             !WriteFile( file, iccprofile, size, &written, NULL ) || written != size)
849             ERR( "Unable to write color profile\n" );
850     }
851
852     ret = cmsCloseProfile( MSCMS_hprofile2cmsprofile( profile ) );
853     HeapFree( GetProcessHeap(), 0, MSCMS_hprofile2iccprofile( profile ) );
854
855     CloseHandle( MSCMS_hprofile2handle( profile ) );
856     MSCMS_destroy_hprofile_handle( profile );
857
858 #endif /* HAVE_LCMS_H */
859     return ret;
860 }