Sync with trunk r63831.
[reactos.git] / dll / win32 / mscms / profile.c
1 /*
2 * MSCMS - Color Management System for Wine
3 *
4 * Copyright 2004, 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 #include <wine/unicode.h>
26
27 //#include <stdarg.h>
28
29 //#include "windef.h"
30 //#include "winbase.h"
31 //#include "winnls.h"
32 #include <wingdi.h>
33 #include <winuser.h>
34 #include <winreg.h>
35 //#include "shlwapi.h"
36 #include <icm.h>
37
38 //#include "mscms_priv.h"
39
40 static void basename( LPCWSTR path, LPWSTR name )
41 {
42 INT i = lstrlenW( path );
43
44 while (i > 0 && path[i - 1] != '\\' && path[i - 1] != '/') i--;
45 lstrcpyW( name, &path[i] );
46 }
47
48 static inline LPWSTR strdupW( LPCSTR str )
49 {
50 LPWSTR ret = NULL;
51 if (str)
52 {
53 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
54 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
55 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
56 }
57 return ret;
58 }
59
60 const char *dbgstr_tag( DWORD tag )
61 {
62 return wine_dbg_sprintf( "'%c%c%c%c'",
63 (char)(tag >> 24), (char)(tag >> 16), (char)(tag >> 8), (char)(tag) );
64 }
65
66 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
67
68 /******************************************************************************
69 * AssociateColorProfileWithDeviceA [MSCMS.@]
70 */
71 BOOL WINAPI AssociateColorProfileWithDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
72 {
73 int len;
74 BOOL ret = FALSE;
75 WCHAR *profileW, *deviceW;
76
77 TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );
78
79 if (!profile || !device)
80 {
81 SetLastError( ERROR_INVALID_PARAMETER );
82 return FALSE;
83 }
84 if (machine)
85 {
86 SetLastError( ERROR_NOT_SUPPORTED );
87 return FALSE;
88 }
89
90 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
91 if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
92
93 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
94
95 len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
96 if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
97 {
98 MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
99 ret = AssociateColorProfileWithDeviceW( NULL, profileW, deviceW );
100 }
101
102 HeapFree( GetProcessHeap(), 0, profileW );
103 HeapFree( GetProcessHeap(), 0, deviceW );
104 return ret;
105 }
106
107 static BOOL set_profile_device_key( PCWSTR file, const BYTE *value, DWORD size )
108 {
109 static const WCHAR fmtW[] = {'%','c','%','c','%','c','%','c',0};
110 static const WCHAR icmW[] = {'S','o','f','t','w','a','r','e','\\',
111 'M','i','c','r','o','s','o','f','t','\\',
112 'W','i','n','d','o','w','s',' ','N','T','\\',
113 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
114 'I','C','M',0};
115 PROFILEHEADER header;
116 PROFILE profile;
117 HPROFILE handle;
118 HKEY icm_key, class_key;
119 WCHAR basenameW[MAX_PATH], classW[5];
120
121 profile.dwType = PROFILE_FILENAME;
122 profile.pProfileData = (PVOID)file;
123 profile.cbDataSize = (lstrlenW( file ) + 1) * sizeof(WCHAR);
124
125 /* FIXME is the profile installed? */
126 if (!(handle = OpenColorProfileW( &profile, PROFILE_READ, 0, OPEN_EXISTING )))
127 {
128 SetLastError( ERROR_INVALID_PROFILE );
129 return FALSE;
130 }
131 if (!GetColorProfileHeader( handle, &header ))
132 {
133 CloseColorProfile( handle );
134 SetLastError( ERROR_INVALID_PROFILE );
135 return FALSE;
136 }
137 RegCreateKeyExW( HKEY_LOCAL_MACHINE, icmW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &icm_key, NULL );
138
139 basename( file, basenameW );
140 sprintfW( classW, fmtW, (header.phClass >> 24) & 0xff, (header.phClass >> 16) & 0xff,
141 (header.phClass >> 8) & 0xff, header.phClass & 0xff );
142
143 RegCreateKeyExW( icm_key, classW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &class_key, NULL );
144 if (value) RegSetValueExW( class_key, basenameW, 0, REG_BINARY, value, size );
145 else RegDeleteValueW( class_key, basenameW );
146
147 RegCloseKey( class_key );
148 RegCloseKey( icm_key );
149 CloseColorProfile( handle );
150 return TRUE;
151 }
152
153 /******************************************************************************
154 * AssociateColorProfileWithDeviceW [MSCMS.@]
155 */
156 BOOL WINAPI AssociateColorProfileWithDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
157 {
158 static const BYTE dummy_value[12];
159
160 TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );
161
162 if (!profile || !device)
163 {
164 SetLastError( ERROR_INVALID_PARAMETER );
165 return FALSE;
166 }
167 if (machine)
168 {
169 SetLastError( ERROR_NOT_SUPPORTED );
170 return FALSE;
171 }
172
173 return set_profile_device_key( profile, dummy_value, sizeof(dummy_value) );
174 }
175
176 /******************************************************************************
177 * DisassociateColorProfileFromDeviceA [MSCMS.@]
178 */
179 BOOL WINAPI DisassociateColorProfileFromDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
180 {
181 int len;
182 BOOL ret = FALSE;
183 WCHAR *profileW, *deviceW;
184
185 TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );
186
187 if (!profile || !device)
188 {
189 SetLastError( ERROR_INVALID_PARAMETER );
190 return FALSE;
191 }
192 if (machine)
193 {
194 SetLastError( ERROR_NOT_SUPPORTED );
195 return FALSE;
196 }
197
198 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
199 if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
200
201 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
202
203 len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
204 if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
205 {
206 MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
207 ret = DisassociateColorProfileFromDeviceW( NULL, profileW, deviceW );
208 }
209
210 HeapFree( GetProcessHeap(), 0, profileW );
211 HeapFree( GetProcessHeap(), 0, deviceW );
212 return ret;
213 }
214
215 /******************************************************************************
216 * DisassociateColorProfileFromDeviceW [MSCMS.@]
217 */
218 BOOL WINAPI DisassociateColorProfileFromDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
219 {
220 TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );
221
222 if (!profile || !device)
223 {
224 SetLastError( ERROR_INVALID_PARAMETER );
225 return FALSE;
226 }
227 if (machine)
228 {
229 SetLastError( ERROR_NOT_SUPPORTED );
230 return FALSE;
231 }
232
233 return set_profile_device_key( profile, NULL, 0 );
234 }
235
236 /******************************************************************************
237 * GetColorDirectoryA [MSCMS.@]
238 *
239 * See GetColorDirectoryW.
240 */
241 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
242 {
243 INT len;
244 LPWSTR bufferW;
245 BOOL ret = FALSE;
246 DWORD sizeW;
247
248 TRACE( "( %p, %p )\n", buffer, size );
249
250 if (machine || !size) return FALSE;
251
252 if (!buffer)
253 {
254 ret = GetColorDirectoryW( NULL, NULL, &sizeW );
255 *size = sizeW / sizeof(WCHAR);
256 return ret;
257 }
258
259 sizeW = *size * sizeof(WCHAR);
260
261 bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
262 if (bufferW)
263 {
264 if ((ret = GetColorDirectoryW( NULL, bufferW, &sizeW )))
265 {
266 *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
267 len = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, *size, NULL, NULL );
268 if (!len) ret = FALSE;
269 }
270 else *size = sizeW / sizeof(WCHAR);
271
272 HeapFree( GetProcessHeap(), 0, bufferW );
273 }
274 return ret;
275 }
276
277 /******************************************************************************
278 * GetColorDirectoryW [MSCMS.@]
279 *
280 * Get the directory where color profiles are stored.
281 *
282 * PARAMS
283 * machine [I] Name of the machine for which to get the color directory.
284 * Must be NULL, which indicates the local machine.
285 * buffer [I] Buffer to receive the path name.
286 * size [I/O] Size of the buffer in bytes. On return the variable holds
287 * the number of bytes actually needed.
288 */
289 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
290 {
291 WCHAR colordir[MAX_PATH];
292 static const WCHAR colorsubdir[] =
293 {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r',0};
294 DWORD len;
295
296 TRACE( "( %p, %p )\n", buffer, size );
297
298 if (machine || !size) return FALSE;
299
300 GetSystemDirectoryW( colordir, sizeof(colordir) / sizeof(WCHAR) );
301 lstrcatW( colordir, colorsubdir );
302
303 len = lstrlenW( colordir ) * sizeof(WCHAR);
304
305 if (buffer && len <= *size)
306 {
307 lstrcpyW( buffer, colordir );
308 *size = len;
309 return TRUE;
310 }
311
312 SetLastError( ERROR_MORE_DATA );
313 *size = len;
314 return FALSE;
315 }
316
317 /******************************************************************************
318 * GetColorProfileElement [MSCMS.@]
319 *
320 * Retrieve data for a specified tag type.
321 *
322 * PARAMS
323 * profile [I] Handle to a color profile.
324 * type [I] ICC tag type.
325 * offset [I] Offset in bytes to start copying from.
326 * size [I/O] Size of the buffer in bytes. On return the variable holds
327 * the number of bytes actually needed.
328 * buffer [O] Buffer to receive the tag data.
329 * ref [O] Pointer to a BOOL that specifies whether more than one tag
330 * references the data.
331 *
332 * RETURNS
333 * Success: TRUE
334 * Failure: FALSE
335 */
336 BOOL WINAPI GetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
337 PVOID buffer, PBOOL ref )
338 {
339 BOOL ret = FALSE;
340 #ifdef HAVE_LCMS2
341 struct profile *profile = grab_profile( handle );
342
343 TRACE( "( %p, 0x%08x, %d, %p, %p, %p )\n", handle, type, offset, size, buffer, ref );
344
345 if (!profile) return FALSE;
346
347 if (!size || !ref)
348 {
349 release_profile( profile );
350 return FALSE;
351 }
352 if (!get_tag_data( profile, type, offset, buffer, size ))
353 {
354 release_profile( profile );
355 return FALSE;
356 }
357 ret = get_tag_data( profile, type, offset, buffer, size );
358 *ref = cmsTagLinkedTo( profile->cmsprofile, type ) != 0;
359 release_profile( profile );
360 return ret;
361
362 #endif /* HAVE_LCMS2 */
363 return ret;
364 }
365
366 /******************************************************************************
367 * GetColorProfileElementTag [MSCMS.@]
368 *
369 * Get the tag type from a color profile by index.
370 *
371 * PARAMS
372 * profile [I] Handle to a color profile.
373 * index [I] Index into the tag table of the color profile.
374 * type [O] Pointer to a variable that holds the ICC tag type on return.
375 *
376 * RETURNS
377 * Success: TRUE
378 * Failure: FALSE
379 *
380 * NOTES
381 * The tag table index starts at 1.
382 * Use GetCountColorProfileElements to retrieve a count of tagged elements.
383 */
384 BOOL WINAPI GetColorProfileElementTag( HPROFILE handle, DWORD index, PTAGTYPE type )
385 {
386 BOOL ret = FALSE;
387 #ifdef HAVE_LCMS2
388 struct profile *profile = grab_profile( handle );
389 cmsInt32Number num_tags;
390 cmsTagSignature sig;
391
392 TRACE( "( %p, %d, %p )\n", handle, index, type );
393
394 if (!profile) return FALSE;
395
396 if (!type)
397 {
398 release_profile( profile );
399 return FALSE;
400 }
401 num_tags = cmsGetTagCount( profile->cmsprofile );
402 if (num_tags < 0 || index > num_tags || index < 1)
403 {
404 release_profile( profile );
405 return FALSE;
406 }
407 if ((sig = cmsGetTagSignature( profile->cmsprofile, index - 1 )))
408 {
409 *type = sig;
410 ret = TRUE;
411 }
412 release_profile( profile );
413
414 #endif /* HAVE_LCMS2 */
415 return ret;
416 }
417
418 /******************************************************************************
419 * GetColorProfileFromHandle [MSCMS.@]
420 *
421 * Retrieve an ICC color profile by handle.
422 *
423 * PARAMS
424 * profile [I] Handle to a color profile.
425 * buffer [O] Buffer to receive the ICC profile.
426 * size [I/O] Size of the buffer in bytes. On return the variable holds the
427 * number of bytes actually needed.
428 *
429 * RETURNS
430 * Success: TRUE
431 * Failure: FALSE
432 *
433 * NOTES
434 * The profile returned will be in big-endian format.
435 */
436 BOOL WINAPI GetColorProfileFromHandle( HPROFILE handle, PBYTE buffer, PDWORD size )
437 {
438 BOOL ret = FALSE;
439 #ifdef HAVE_LCMS2
440 struct profile *profile = grab_profile( handle );
441 PROFILEHEADER header;
442
443 TRACE( "( %p, %p, %p )\n", handle, buffer, size );
444
445 if (!profile) return FALSE;
446
447 if (!size)
448 {
449 release_profile( profile );
450 return FALSE;
451 }
452 get_profile_header( profile, &header );
453
454 if (!buffer || header.phSize > *size)
455 {
456 *size = header.phSize;
457 release_profile( profile );
458 return FALSE;
459 }
460
461 /* No endian conversion needed */
462 memcpy( buffer, profile->data, profile->size );
463 *size = profile->size;
464
465 release_profile( profile );
466 ret = TRUE;
467
468 #endif /* HAVE_LCMS2 */
469 return ret;
470 }
471
472 /******************************************************************************
473 * GetColorProfileHeader [MSCMS.@]
474 *
475 * Retrieve a color profile header by handle.
476 *
477 * PARAMS
478 * profile [I] Handle to a color profile.
479 * header [O] Buffer to receive the ICC profile header.
480 *
481 * RETURNS
482 * Success: TRUE
483 * Failure: FALSE
484 *
485 * NOTES
486 * The profile header returned will be adjusted for endianness.
487 */
488 BOOL WINAPI GetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
489 {
490 #ifdef HAVE_LCMS2
491 struct profile *profile = grab_profile( handle );
492
493 TRACE( "( %p, %p )\n", handle, header );
494
495 if (!profile) return FALSE;
496
497 if (!header)
498 {
499 release_profile( profile );
500 return FALSE;
501 }
502 get_profile_header( profile, header );
503 release_profile( profile );
504 return TRUE;
505
506 #else
507 return FALSE;
508 #endif /* HAVE_LCMS2 */
509 }
510
511 /******************************************************************************
512 * GetCountColorProfileElements [MSCMS.@]
513 *
514 * Retrieve the number of elements in a color profile.
515 *
516 * PARAMS
517 * profile [I] Handle to a color profile.
518 * count [O] Pointer to a variable which is set to the number of elements
519 * in the color profile.
520 *
521 * RETURNS
522 * Success: TRUE
523 * Failure: FALSE
524 */
525 BOOL WINAPI GetCountColorProfileElements( HPROFILE handle, PDWORD count )
526 {
527 BOOL ret = FALSE;
528 #ifdef HAVE_LCMS2
529 struct profile *profile = grab_profile( handle );
530 cmsInt32Number num_tags;
531
532 TRACE( "( %p, %p )\n", handle, count );
533
534 if (!profile) return FALSE;
535
536 if (!count)
537 {
538 release_profile( profile );
539 return FALSE;
540 }
541 if ((num_tags = cmsGetTagCount( profile->cmsprofile )) >= 0)
542 {
543 *count = num_tags;
544 ret = TRUE;
545 }
546 release_profile( profile );
547
548 #endif /* HAVE_LCMS2 */
549 return ret;
550 }
551
552 /******************************************************************************
553 * GetStandardColorSpaceProfileA [MSCMS.@]
554 *
555 * See GetStandardColorSpaceProfileW.
556 */
557 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
558 {
559 INT len;
560 LPWSTR profileW;
561 BOOL ret = FALSE;
562 DWORD sizeW;
563
564 TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
565
566 if (machine)
567 {
568 SetLastError( ERROR_NOT_SUPPORTED );
569 return FALSE;
570 }
571
572 if (!size)
573 {
574 SetLastError( ERROR_INVALID_PARAMETER );
575 return FALSE;
576 }
577
578 sizeW = *size * sizeof(WCHAR);
579
580 if (!profile)
581 {
582 ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
583 *size = sizeW / sizeof(WCHAR);
584 return ret;
585 }
586
587 profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
588 if (profileW)
589 {
590 if ((ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW )))
591 {
592 *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
593 len = WideCharToMultiByte( CP_ACP, 0, profileW, -1, profile, *size, NULL, NULL );
594 if (!len) ret = FALSE;
595 }
596 else *size = sizeW / sizeof(WCHAR);
597
598 HeapFree( GetProcessHeap(), 0, profileW );
599 }
600 return ret;
601 }
602
603 /******************************************************************************
604 * GetStandardColorSpaceProfileW [MSCMS.@]
605 *
606 * Retrieve the profile filename for a given standard color space id.
607 *
608 * PARAMS
609 * machine [I] Name of the machine for which to get the standard color space.
610 * Must be NULL, which indicates the local machine.
611 * id [I] Id of a standard color space.
612 * profile [O] Buffer to receive the profile filename.
613 * size [I/O] Size of the filename buffer in bytes.
614 *
615 * RETURNS
616 * Success: TRUE
617 * Failure: FALSE
618 */
619 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
620 {
621 static const WCHAR rgbprofilefile[] =
622 { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
623 's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
624 WCHAR rgbprofile[MAX_PATH];
625 DWORD len = sizeof(rgbprofile);
626
627 TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
628
629 if (machine)
630 {
631 SetLastError( ERROR_NOT_SUPPORTED );
632 return FALSE;
633 }
634
635 if (!size)
636 {
637 SetLastError( ERROR_INVALID_PARAMETER );
638 return FALSE;
639 }
640
641 if (!profile)
642 {
643 SetLastError( ERROR_INSUFFICIENT_BUFFER );
644 return FALSE;
645 }
646
647 GetColorDirectoryW( machine, rgbprofile, &len );
648
649 switch (id)
650 {
651 case LCS_sRGB:
652 case LCS_WINDOWS_COLOR_SPACE: /* FIXME */
653 lstrcatW( rgbprofile, rgbprofilefile );
654 len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
655
656 if (*size < len || !profile)
657 {
658 *size = len;
659 SetLastError( ERROR_MORE_DATA );
660 return FALSE;
661 }
662
663 lstrcpyW( profile, rgbprofile );
664 break;
665
666 default:
667 SetLastError( ERROR_FILE_NOT_FOUND );
668 return FALSE;
669 }
670 return TRUE;
671 }
672
673 static BOOL header_from_file( LPCWSTR file, PPROFILEHEADER header )
674 {
675 BOOL ret;
676 PROFILE profile;
677 WCHAR path[MAX_PATH], slash[] = {'\\',0};
678 DWORD size = sizeof(path);
679 HANDLE handle;
680
681 ret = GetColorDirectoryW( NULL, path, &size );
682 if (!ret)
683 {
684 WARN( "Can't retrieve color directory\n" );
685 return FALSE;
686 }
687 if (size + sizeof(slash) + sizeof(WCHAR) * lstrlenW( file ) > sizeof(path))
688 {
689 WARN( "Filename too long\n" );
690 return FALSE;
691 }
692
693 lstrcatW( path, slash );
694 lstrcatW( path, file );
695
696 profile.dwType = PROFILE_FILENAME;
697 profile.pProfileData = path;
698 profile.cbDataSize = lstrlenW( path ) + 1;
699
700 handle = OpenColorProfileW( &profile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );
701 if (!handle)
702 {
703 WARN( "Can't open color profile\n" );
704 return FALSE;
705 }
706
707 ret = GetColorProfileHeader( handle, header );
708 if (!ret)
709 WARN( "Can't retrieve color profile header\n" );
710
711 CloseColorProfile( handle );
712 return ret;
713 }
714
715 static BOOL match_profile( PENUMTYPEW rec, PPROFILEHEADER hdr )
716 {
717 if (rec->dwFields & ET_DEVICENAME)
718 {
719 FIXME( "ET_DEVICENAME: %s\n", debugstr_w(rec->pDeviceName) );
720 }
721 if (rec->dwFields & ET_MEDIATYPE)
722 {
723 FIXME( "ET_MEDIATYPE: 0x%08x\n", rec->dwMediaType );
724 }
725 if (rec->dwFields & ET_DITHERMODE)
726 {
727 FIXME( "ET_DITHERMODE: 0x%08x\n", rec->dwDitheringMode );
728 }
729 if (rec->dwFields & ET_RESOLUTION)
730 {
731 FIXME( "ET_RESOLUTION: 0x%08x, 0x%08x\n",
732 rec->dwResolution[0], rec->dwResolution[1] );
733 }
734 if (rec->dwFields & ET_DEVICECLASS)
735 {
736 FIXME( "ET_DEVICECLASS: %s\n", dbgstr_tag(rec->dwMediaType) );
737 }
738 if (rec->dwFields & ET_CMMTYPE)
739 {
740 TRACE( "ET_CMMTYPE: %s\n", dbgstr_tag(rec->dwCMMType) );
741 if (rec->dwCMMType != hdr->phCMMType) return FALSE;
742 }
743 if (rec->dwFields & ET_CLASS)
744 {
745 TRACE( "ET_CLASS: %s\n", dbgstr_tag(rec->dwClass) );
746 if (rec->dwClass != hdr->phClass) return FALSE;
747 }
748 if (rec->dwFields & ET_DATACOLORSPACE)
749 {
750 TRACE( "ET_DATACOLORSPACE: %s\n", dbgstr_tag(rec->dwDataColorSpace) );
751 if (rec->dwDataColorSpace != hdr->phDataColorSpace) return FALSE;
752 }
753 if (rec->dwFields & ET_CONNECTIONSPACE)
754 {
755 TRACE( "ET_CONNECTIONSPACE: %s\n", dbgstr_tag(rec->dwConnectionSpace) );
756 if (rec->dwConnectionSpace != hdr->phConnectionSpace) return FALSE;
757 }
758 if (rec->dwFields & ET_SIGNATURE)
759 {
760 TRACE( "ET_SIGNATURE: %s\n", dbgstr_tag(rec->dwSignature) );
761 if (rec->dwSignature != hdr->phSignature) return FALSE;
762 }
763 if (rec->dwFields & ET_PLATFORM)
764 {
765 TRACE( "ET_PLATFORM: %s\n", dbgstr_tag(rec->dwPlatform) );
766 if (rec->dwPlatform != hdr->phPlatform) return FALSE;
767 }
768 if (rec->dwFields & ET_PROFILEFLAGS)
769 {
770 TRACE( "ET_PROFILEFLAGS: 0x%08x\n", rec->dwProfileFlags );
771 if (rec->dwProfileFlags != hdr->phProfileFlags) return FALSE;
772 }
773 if (rec->dwFields & ET_MANUFACTURER)
774 {
775 TRACE( "ET_MANUFACTURER: %s\n", dbgstr_tag(rec->dwManufacturer) );
776 if (rec->dwManufacturer != hdr->phManufacturer) return FALSE;
777 }
778 if (rec->dwFields & ET_MODEL)
779 {
780 TRACE( "ET_MODEL: %s\n", dbgstr_tag(rec->dwModel) );
781 if (rec->dwModel != hdr->phModel) return FALSE;
782 }
783 if (rec->dwFields & ET_ATTRIBUTES)
784 {
785 TRACE( "ET_ATTRIBUTES: 0x%08x, 0x%08x\n",
786 rec->dwAttributes[0], rec->dwAttributes[1] );
787 if (rec->dwAttributes[0] != hdr->phAttributes[0] ||
788 rec->dwAttributes[1] != hdr->phAttributes[1]) return FALSE;
789 }
790 if (rec->dwFields & ET_RENDERINGINTENT)
791 {
792 TRACE( "ET_RENDERINGINTENT: 0x%08x\n", rec->dwRenderingIntent );
793 if (rec->dwRenderingIntent != hdr->phRenderingIntent) return FALSE;
794 }
795 if (rec->dwFields & ET_CREATOR)
796 {
797 TRACE( "ET_CREATOR: %s\n", dbgstr_tag(rec->dwCreator) );
798 if (rec->dwCreator != hdr->phCreator) return FALSE;
799 }
800 return TRUE;
801 }
802
803 /******************************************************************************
804 * EnumColorProfilesA [MSCMS.@]
805 *
806 * See EnumColorProfilesW.
807 */
808 BOOL WINAPI EnumColorProfilesA( PCSTR machine, PENUMTYPEA record, PBYTE buffer,
809 PDWORD size, PDWORD number )
810 {
811 BOOL match, ret = FALSE;
812 char spec[] = "\\*.icm";
813 char colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
814 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
815 PROFILEHEADER header;
816 WIN32_FIND_DATAA data;
817 ENUMTYPEW recordW;
818 WCHAR *fileW = NULL, *deviceW = NULL;
819 HANDLE find;
820
821 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
822
823 if (machine || !record || !size ||
824 record->dwSize != sizeof(ENUMTYPEA) ||
825 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
826
827 ret = GetColorDirectoryA( machine, colordir, &len );
828 if (!ret || len + sizeof(spec) > MAX_PATH)
829 {
830 WARN( "can't retrieve color directory\n" );
831 return FALSE;
832 }
833
834 lstrcpyA( glob, colordir );
835 lstrcatA( glob, spec );
836
837 find = FindFirstFileA( glob, &data );
838 if (find == INVALID_HANDLE_VALUE) return FALSE;
839
840 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(char *) + 1 );
841 if (!profiles) goto exit;
842
843 memcpy( &recordW, record, sizeof(ENUMTYPEA) );
844 if (record->pDeviceName)
845 {
846 deviceW = strdupW( record->pDeviceName );
847 if (!(recordW.pDeviceName = deviceW)) goto exit;
848 }
849
850 fileW = strdupW( data.cFileName );
851 if (!fileW) goto exit;
852
853 ret = header_from_file( fileW, &header );
854 if (ret)
855 {
856 match = match_profile( &recordW, &header );
857 if (match)
858 {
859 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
860 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
861
862 if (!profiles[count]) goto exit;
863 else
864 {
865 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
866 lstrcpyA( profiles[count], data.cFileName );
867 totalsize += len;
868 count++;
869 }
870 }
871 }
872 HeapFree( GetProcessHeap(), 0, fileW );
873 fileW = NULL;
874
875 while (FindNextFileA( find, &data ))
876 {
877 fileW = strdupW( data.cFileName );
878 if (!fileW) goto exit;
879
880 ret = header_from_file( fileW, &header );
881 if (!ret)
882 {
883 HeapFree( GetProcessHeap(), 0, fileW );
884 continue;
885 }
886
887 match = match_profile( &recordW, &header );
888 if (match)
889 {
890 char **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
891 profiles, sizeof(char *) * (count + 1) );
892 if (!tmp) goto exit;
893 else profiles = tmp;
894
895 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
896 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
897
898 if (!profiles[count]) goto exit;
899 else
900 {
901 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
902 lstrcpyA( profiles[count], data.cFileName );
903 totalsize += len;
904 count++;
905 }
906 }
907 HeapFree( GetProcessHeap(), 0, fileW );
908 fileW = NULL;
909 }
910
911 totalsize++;
912 if (buffer && *size >= totalsize)
913 {
914 char *p = (char *)buffer;
915
916 for (i = 0; i < count; i++)
917 {
918 lstrcpyA( p, profiles[i] );
919 p += lstrlenA( profiles[i] ) + 1;
920 }
921 *p = 0;
922 ret = TRUE;
923 }
924 else ret = FALSE;
925
926 *size = totalsize;
927 if (number) *number = count;
928
929 exit:
930 for (i = 0; i < count; i++)
931 HeapFree( GetProcessHeap(), 0, profiles[i] );
932 HeapFree( GetProcessHeap(), 0, profiles );
933 HeapFree( GetProcessHeap(), 0, deviceW );
934 HeapFree( GetProcessHeap(), 0, fileW );
935 FindClose( find );
936
937 return ret;
938 }
939
940 /******************************************************************************
941 * EnumColorProfilesW [MSCMS.@]
942 *
943 * Enumerate profiles that match given criteria.
944 *
945 * PARAMS
946 * machine [I] Name of the machine for which to enumerate profiles.
947 * Must be NULL, which indicates the local machine.
948 * record [I] Record of criteria that a profile must match.
949 * buffer [O] Buffer to receive a string array of profile filenames.
950 * size [I/O] Size of the filename buffer in bytes.
951 * number [O] Number of filenames copied into buffer.
952 *
953 * RETURNS
954 * Success: TRUE
955 * Failure: FALSE
956 */
957 BOOL WINAPI EnumColorProfilesW( PCWSTR machine, PENUMTYPEW record, PBYTE buffer,
958 PDWORD size, PDWORD number )
959 {
960 BOOL match, ret = FALSE;
961 WCHAR spec[] = {'\\','*','i','c','m',0};
962 WCHAR colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
963 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
964 PROFILEHEADER header;
965 WIN32_FIND_DATAW data;
966 HANDLE find;
967
968 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
969
970 if (machine || !record || !size ||
971 record->dwSize != sizeof(ENUMTYPEW) ||
972 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
973
974 ret = GetColorDirectoryW( machine, colordir, &len );
975 if (!ret || len + sizeof(spec) > MAX_PATH)
976 {
977 WARN( "Can't retrieve color directory\n" );
978 return FALSE;
979 }
980
981 lstrcpyW( glob, colordir );
982 lstrcatW( glob, spec );
983
984 find = FindFirstFileW( glob, &data );
985 if (find == INVALID_HANDLE_VALUE) return FALSE;
986
987 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR *) + 1 );
988 if (!profiles) goto exit;
989
990 ret = header_from_file( data.cFileName, &header );
991 if (ret)
992 {
993 match = match_profile( record, &header );
994 if (match)
995 {
996 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
997 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
998
999 if (!profiles[count]) goto exit;
1000 else
1001 {
1002 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
1003 lstrcpyW( profiles[count], data.cFileName );
1004 totalsize += len;
1005 count++;
1006 }
1007 }
1008 }
1009
1010 while (FindNextFileW( find, &data ))
1011 {
1012 ret = header_from_file( data.cFileName, &header );
1013 if (!ret) continue;
1014
1015 match = match_profile( record, &header );
1016 if (match)
1017 {
1018 WCHAR **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1019 profiles, sizeof(WCHAR *) * (count + 1) );
1020 if (!tmp) goto exit;
1021 else profiles = tmp;
1022
1023 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
1024 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
1025
1026 if (!profiles[count]) goto exit;
1027 else
1028 {
1029 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
1030 lstrcpyW( profiles[count], data.cFileName );
1031 totalsize += len;
1032 count++;
1033 }
1034 }
1035 }
1036
1037 totalsize++;
1038 if (buffer && *size >= totalsize)
1039 {
1040 WCHAR *p = (WCHAR *)buffer;
1041
1042 for (i = 0; i < count; i++)
1043 {
1044 lstrcpyW( p, profiles[i] );
1045 p += lstrlenW( profiles[i] ) + 1;
1046 }
1047 *p = 0;
1048 ret = TRUE;
1049 }
1050 else ret = FALSE;
1051
1052 *size = totalsize;
1053 if (number) *number = count;
1054
1055 exit:
1056 for (i = 0; i < count; i++)
1057 HeapFree( GetProcessHeap(), 0, profiles[i] );
1058 HeapFree( GetProcessHeap(), 0, profiles );
1059 FindClose( find );
1060
1061 return ret;
1062 }
1063
1064 /******************************************************************************
1065 * InstallColorProfileA [MSCMS.@]
1066 *
1067 * See InstallColorProfileW.
1068 */
1069 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
1070 {
1071 UINT len;
1072 LPWSTR profileW;
1073 BOOL ret = FALSE;
1074
1075 TRACE( "( %s )\n", debugstr_a(profile) );
1076
1077 if (machine || !profile) return FALSE;
1078
1079 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1080 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1081
1082 if (profileW)
1083 {
1084 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1085
1086 ret = InstallColorProfileW( NULL, profileW );
1087 HeapFree( GetProcessHeap(), 0, profileW );
1088 }
1089 return ret;
1090 }
1091
1092 /******************************************************************************
1093 * InstallColorProfileW [MSCMS.@]
1094 *
1095 * Install a color profile.
1096 *
1097 * PARAMS
1098 * machine [I] Name of the machine to install the profile on. Must be NULL,
1099 * which indicates the local machine.
1100 * profile [I] Full path name of the profile to install.
1101 *
1102 * RETURNS
1103 * Success: TRUE
1104 * Failure: FALSE
1105 */
1106 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
1107 {
1108 WCHAR dest[MAX_PATH], base[MAX_PATH];
1109 DWORD size = sizeof(dest);
1110 static const WCHAR slash[] = { '\\', 0 };
1111
1112 TRACE( "( %s )\n", debugstr_w(profile) );
1113
1114 if (machine || !profile) return FALSE;
1115
1116 if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
1117
1118 basename( profile, base );
1119
1120 lstrcatW( dest, slash );
1121 lstrcatW( dest, base );
1122
1123 /* Is source equal to destination? */
1124 if (!lstrcmpW( profile, dest )) return TRUE;
1125
1126 return CopyFileW( profile, dest, TRUE );
1127 }
1128
1129 /******************************************************************************
1130 * IsColorProfileTagPresent [MSCMS.@]
1131 *
1132 * Determine if a given ICC tag type is present in a color profile.
1133 *
1134 * PARAMS
1135 * profile [I] Color profile handle.
1136 * tag [I] ICC tag type.
1137 * present [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
1138 * FALSE otherwise.
1139 *
1140 * RETURNS
1141 * Success: TRUE
1142 * Failure: FALSE
1143 */
1144 BOOL WINAPI IsColorProfileTagPresent( HPROFILE handle, TAGTYPE type, PBOOL present )
1145 {
1146 BOOL ret = FALSE;
1147 #ifdef HAVE_LCMS2
1148 struct profile *profile = grab_profile( handle );
1149
1150 TRACE( "( %p, 0x%08x, %p )\n", handle, type, present );
1151
1152 if (!profile) return FALSE;
1153
1154 if (!present)
1155 {
1156 release_profile( profile );
1157 return FALSE;
1158 }
1159 *present = cmsIsTag( profile->cmsprofile, type );
1160 release_profile( profile );
1161 ret = TRUE;
1162
1163 #endif /* HAVE_LCMS2 */
1164 return ret;
1165 }
1166
1167 /******************************************************************************
1168 * IsColorProfileValid [MSCMS.@]
1169 *
1170 * Determine if a given color profile is valid.
1171 *
1172 * PARAMS
1173 * profile [I] Color profile handle.
1174 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
1175 * FALSE otherwise.
1176 *
1177 * RETURNS
1178 * Success: TRUE
1179 * Failure: FALSE
1180 */
1181 BOOL WINAPI IsColorProfileValid( HPROFILE handle, PBOOL valid )
1182 {
1183 BOOL ret = FALSE;
1184 #ifdef HAVE_LCMS2
1185 struct profile *profile = grab_profile( handle );
1186
1187 TRACE( "( %p, %p )\n", handle, valid );
1188
1189 if (!profile) return FALSE;
1190
1191 if (!valid)
1192 {
1193 release_profile( profile );
1194 return FALSE;
1195 }
1196 if (profile->data) ret = *valid = TRUE;
1197 release_profile( profile );
1198
1199 #endif /* HAVE_LCMS2 */
1200 return ret;
1201 }
1202
1203 /******************************************************************************
1204 * SetColorProfileElement [MSCMS.@]
1205 *
1206 * Set data for a specified tag type.
1207 *
1208 * PARAMS
1209 * profile [I] Handle to a color profile.
1210 * type [I] ICC tag type.
1211 * offset [I] Offset in bytes to start copying to.
1212 * size [I/O] Size of the buffer in bytes. On return the variable holds the
1213 * number of bytes actually needed.
1214 * buffer [O] Buffer holding the tag data.
1215 *
1216 * RETURNS
1217 * Success: TRUE
1218 * Failure: FALSE
1219 */
1220 BOOL WINAPI SetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
1221 PVOID buffer )
1222 {
1223 BOOL ret = FALSE;
1224 #ifdef HAVE_LCMS2
1225 struct profile *profile = grab_profile( handle );
1226
1227 TRACE( "( %p, 0x%08x, %d, %p, %p )\n", handle, type, offset, size, buffer );
1228
1229 if (!profile) return FALSE;
1230
1231 if (!size || !buffer || !(profile->access & PROFILE_READWRITE))
1232 {
1233 release_profile( profile );
1234 return FALSE;
1235 }
1236 ret = set_tag_data( profile, type, offset, buffer, size );
1237 release_profile( profile );
1238 return ret;
1239
1240 #endif /* HAVE_LCMS2 */
1241 return ret;
1242 }
1243
1244 /******************************************************************************
1245 * SetColorProfileHeader [MSCMS.@]
1246 *
1247 * Set header data for a given profile.
1248 *
1249 * PARAMS
1250 * profile [I] Handle to a color profile.
1251 * header [I] Buffer holding the header data.
1252 *
1253 * RETURNS
1254 * Success: TRUE
1255 * Failure: FALSE
1256 */
1257 BOOL WINAPI SetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
1258 {
1259 #ifdef HAVE_LCMS2
1260 struct profile *profile = grab_profile( handle );
1261
1262 TRACE( "( %p, %p )\n", handle, header );
1263
1264 if (!profile) return FALSE;
1265
1266 if (!header || !(profile->access & PROFILE_READWRITE))
1267 {
1268 release_profile( profile );
1269 return FALSE;
1270 }
1271 set_profile_header( profile, header );
1272 release_profile( profile );
1273 return TRUE;
1274
1275 #else
1276 return FALSE;
1277 #endif /* HAVE_LCMS2 */
1278 }
1279
1280 /******************************************************************************
1281 * UninstallColorProfileA [MSCMS.@]
1282 *
1283 * See UninstallColorProfileW.
1284 */
1285 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
1286 {
1287 UINT len;
1288 LPWSTR profileW;
1289 BOOL ret = FALSE;
1290
1291 TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
1292
1293 if (machine || !profile) return FALSE;
1294
1295 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1296 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1297
1298 if (profileW)
1299 {
1300 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1301
1302 ret = UninstallColorProfileW( NULL, profileW , delete );
1303
1304 HeapFree( GetProcessHeap(), 0, profileW );
1305 }
1306 return ret;
1307 }
1308
1309 /******************************************************************************
1310 * UninstallColorProfileW [MSCMS.@]
1311 *
1312 * Uninstall a color profile.
1313 *
1314 * PARAMS
1315 * machine [I] Name of the machine to uninstall the profile on. Must be NULL,
1316 * which indicates the local machine.
1317 * profile [I] Full path name of the profile to uninstall.
1318 * delete [I] Bool that specifies whether the profile file should be deleted.
1319 *
1320 * RETURNS
1321 * Success: TRUE
1322 * Failure: FALSE
1323 */
1324 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
1325 {
1326 TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
1327
1328 if (machine || !profile) return FALSE;
1329
1330 if (delete) return DeleteFileW( profile );
1331
1332 return TRUE;
1333 }
1334
1335 /******************************************************************************
1336 * OpenColorProfileA [MSCMS.@]
1337 *
1338 * See OpenColorProfileW.
1339 */
1340 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1341 {
1342 HPROFILE handle = NULL;
1343
1344 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1345
1346 if (!profile || !profile->pProfileData) return NULL;
1347
1348 /* No AW conversion needed for memory based profiles */
1349 if (profile->dwType & PROFILE_MEMBUFFER)
1350 return OpenColorProfileW( profile, access, sharing, creation );
1351
1352 if (profile->dwType & PROFILE_FILENAME)
1353 {
1354 UINT len;
1355 PROFILE profileW;
1356
1357 profileW.dwType = profile->dwType;
1358
1359 len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
1360 profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1361
1362 if (profileW.pProfileData)
1363 {
1364 profileW.cbDataSize = len * sizeof(WCHAR);
1365 MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
1366
1367 handle = OpenColorProfileW( &profileW, access, sharing, creation );
1368 HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
1369 }
1370 }
1371 return handle;
1372 }
1373
1374 /******************************************************************************
1375 * OpenColorProfileW [MSCMS.@]
1376 *
1377 * Open a color profile.
1378 *
1379 * PARAMS
1380 * profile [I] Pointer to a color profile structure.
1381 * access [I] Desired access.
1382 * sharing [I] Sharing mode.
1383 * creation [I] Creation mode.
1384 *
1385 * RETURNS
1386 * Success: Handle to the opened profile.
1387 * Failure: NULL
1388 *
1389 * NOTES
1390 * Values for access: PROFILE_READ or PROFILE_READWRITE.
1391 * Values for sharing: 0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
1392 * Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
1393 * OPEN_ALWAYS, TRUNCATE_EXISTING.
1394 * Sharing and creation flags are ignored for memory based profiles.
1395 */
1396 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1397 {
1398 #ifdef HAVE_LCMS2
1399 cmsHPROFILE cmsprofile = NULL;
1400 char *data = NULL;
1401 HANDLE handle = INVALID_HANDLE_VALUE;
1402 DWORD size;
1403
1404 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1405
1406 if (!profile || !profile->pProfileData) return NULL;
1407
1408 if (profile->dwType == PROFILE_MEMBUFFER)
1409 {
1410 /* FIXME: access flags not implemented for memory based profiles */
1411
1412 if (!(data = HeapAlloc( GetProcessHeap(), 0, profile->cbDataSize ))) return NULL;
1413 memcpy( data, profile->pProfileData, profile->cbDataSize );
1414
1415 cmsprofile = cmsOpenProfileFromMem( data, profile->cbDataSize );
1416 size = profile->cbDataSize;
1417 }
1418 else if (profile->dwType == PROFILE_FILENAME)
1419 {
1420 DWORD read, flags = 0;
1421
1422 TRACE( "profile file: %s\n", debugstr_w( profile->pProfileData ) );
1423
1424 if (access & PROFILE_READ) flags = GENERIC_READ;
1425 if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
1426
1427 if (!flags) return NULL;
1428 if (!sharing) sharing = FILE_SHARE_READ;
1429
1430 if (!PathIsRelativeW( profile->pProfileData ))
1431 handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
1432 else
1433 {
1434 WCHAR *path;
1435
1436 if (!GetColorDirectoryW( NULL, NULL, &size ) && GetLastError() == ERROR_MORE_DATA)
1437 {
1438 size += (strlenW( profile->pProfileData ) + 2) * sizeof(WCHAR);
1439 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
1440 GetColorDirectoryW( NULL, path, &size );
1441 PathAddBackslashW( path );
1442 strcatW( path, profile->pProfileData );
1443 }
1444 else return NULL;
1445 handle = CreateFileW( path, flags, sharing, NULL, creation, 0, NULL );
1446 HeapFree( GetProcessHeap(), 0, path );
1447 }
1448 if (handle == INVALID_HANDLE_VALUE)
1449 {
1450 WARN( "Unable to open color profile %u\n", GetLastError() );
1451 return NULL;
1452 }
1453 if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
1454 {
1455 ERR( "Unable to retrieve size of color profile\n" );
1456 CloseHandle( handle );
1457 return NULL;
1458 }
1459 if (!(data = HeapAlloc( GetProcessHeap(), 0, size )))
1460 {
1461 ERR( "Unable to allocate memory for color profile\n" );
1462 CloseHandle( handle );
1463 return NULL;
1464 }
1465 if (!ReadFile( handle, data, size, &read, NULL ) || read != size)
1466 {
1467 ERR( "Unable to read color profile\n" );
1468
1469 CloseHandle( handle );
1470 HeapFree( GetProcessHeap(), 0, data );
1471 return NULL;
1472 }
1473 cmsprofile = cmsOpenProfileFromMem( data, size );
1474 }
1475 else
1476 {
1477 ERR( "Invalid profile type %u\n", profile->dwType );
1478 return NULL;
1479 }
1480
1481 if (cmsprofile)
1482 {
1483 struct profile profile;
1484 HPROFILE hprof;
1485
1486 profile.file = handle;
1487 profile.access = access;
1488 profile.data = data;
1489 profile.size = size;
1490 profile.cmsprofile = cmsprofile;
1491
1492 if ((hprof = create_profile( &profile ))) return hprof;
1493 HeapFree( GetProcessHeap(), 0, data );
1494 cmsCloseProfile( cmsprofile );
1495 }
1496 CloseHandle( handle );
1497
1498 #endif /* HAVE_LCMS2 */
1499 return NULL;
1500 }
1501
1502 /******************************************************************************
1503 * CloseColorProfile [MSCMS.@]
1504 *
1505 * Close a color profile.
1506 *
1507 * PARAMS
1508 * profile [I] Handle to the profile.
1509 *
1510 * RETURNS
1511 * Success: TRUE
1512 * Failure: FALSE
1513 */
1514 BOOL WINAPI CloseColorProfile( HPROFILE profile )
1515 {
1516 BOOL ret = FALSE;
1517 #ifdef HAVE_LCMS2
1518
1519 TRACE( "( %p )\n", profile );
1520 ret = close_profile( profile );
1521
1522 #endif /* HAVE_LCMS2 */
1523 return ret;
1524 }