Synchronize with trunk's revision r57629.
[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 #include "config.h"
22 #include "wine/debug.h"
23 #include "wine/unicode.h"
24
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winreg.h"
33 #include "shlwapi.h"
34 #include "icm.h"
35
36 #include "mscms_priv.h"
37
38 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
39
40 static void MSCMS_basename( LPCWSTR path, LPWSTR name )
41 {
42 INT i = lstrlenW( path );
43
44 while (i > 0 && !IS_SEPARATOR(path[i - 1])) i--;
45 lstrcpyW( name, &path[i] );
46 }
47
48 static inline LPWSTR MSCMS_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 *MSCMS_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 MSCMS_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_LCMS
341 struct profile *profile = grab_profile( handle );
342 DWORD i, count;
343 icTag tag;
344
345 TRACE( "( %p, 0x%08x, %d, %p, %p, %p )\n", handle, type, offset, size, buffer, ref );
346
347 if (!profile) return FALSE;
348
349 if (!size || !ref)
350 {
351 release_profile( profile );
352 return FALSE;
353 }
354 count = MSCMS_get_tag_count( profile->iccprofile );
355
356 for (i = 0; i < count; i++)
357 {
358 MSCMS_get_tag_by_index( profile->iccprofile, i, &tag );
359
360 if (tag.sig == type)
361 {
362 if ((tag.size - offset) > *size || !buffer)
363 {
364 *size = (tag.size - offset);
365 release_profile( profile );
366 return FALSE;
367 }
368 MSCMS_get_tag_data( profile->iccprofile, &tag, offset, buffer );
369
370 *ref = FALSE; /* FIXME: calculate properly */
371 release_profile( profile );
372 return TRUE;
373 }
374 }
375 release_profile( profile );
376
377 #endif /* HAVE_LCMS */
378 return ret;
379 }
380
381 /******************************************************************************
382 * GetColorProfileElementTag [MSCMS.@]
383 *
384 * Get the tag type from a color profile by index.
385 *
386 * PARAMS
387 * profile [I] Handle to a color profile.
388 * index [I] Index into the tag table of the color profile.
389 * type [O] Pointer to a variable that holds the ICC tag type on return.
390 *
391 * RETURNS
392 * Success: TRUE
393 * Failure: FALSE
394 *
395 * NOTES
396 * The tag table index starts at 1.
397 * Use GetCountColorProfileElements to retrieve a count of tagged elements.
398 */
399 BOOL WINAPI GetColorProfileElementTag( HPROFILE handle, DWORD index, PTAGTYPE type )
400 {
401 BOOL ret = FALSE;
402 #ifdef HAVE_LCMS
403 struct profile *profile = grab_profile( handle );
404 DWORD count;
405 icTag tag;
406
407 TRACE( "( %p, %d, %p )\n", handle, index, type );
408
409 if (!profile) return FALSE;
410
411 if (!type)
412 {
413 release_profile( profile );
414 return FALSE;
415 }
416 count = MSCMS_get_tag_count( profile->iccprofile );
417 if (index > count || index < 1)
418 {
419 release_profile( profile );
420 return FALSE;
421 }
422 MSCMS_get_tag_by_index( profile->iccprofile, index - 1, &tag );
423 *type = tag.sig;
424
425 release_profile( profile );
426 ret = TRUE;
427
428 #endif /* HAVE_LCMS */
429 return ret;
430 }
431
432 /******************************************************************************
433 * GetColorProfileFromHandle [MSCMS.@]
434 *
435 * Retrieve an ICC color profile by handle.
436 *
437 * PARAMS
438 * profile [I] Handle to a color profile.
439 * buffer [O] Buffer to receive the ICC profile.
440 * size [I/O] Size of the buffer in bytes. On return the variable holds the
441 * number of bytes actually needed.
442 *
443 * RETURNS
444 * Success: TRUE
445 * Failure: FALSE
446 *
447 * NOTES
448 * The profile returned will be in big-endian format.
449 */
450 BOOL WINAPI GetColorProfileFromHandle( HPROFILE handle, PBYTE buffer, PDWORD size )
451 {
452 BOOL ret = FALSE;
453 #ifdef HAVE_LCMS
454 struct profile *profile = grab_profile( handle );
455 PROFILEHEADER header;
456
457 TRACE( "( %p, %p, %p )\n", handle, buffer, size );
458
459 if (!profile) return FALSE;
460
461 if (!size)
462 {
463 release_profile( profile );
464 return FALSE;
465 }
466 MSCMS_get_profile_header( profile->iccprofile, &header );
467
468 if (!buffer || header.phSize > *size)
469 {
470 *size = header.phSize;
471 release_profile( profile );
472 return FALSE;
473 }
474
475 /* No endian conversion needed */
476 memcpy( buffer, profile->iccprofile, header.phSize );
477 *size = header.phSize;
478
479 release_profile( profile );
480 ret = TRUE;
481
482 #endif /* HAVE_LCMS */
483 return ret;
484 }
485
486 /******************************************************************************
487 * GetColorProfileHeader [MSCMS.@]
488 *
489 * Retrieve a color profile header by handle.
490 *
491 * PARAMS
492 * profile [I] Handle to a color profile.
493 * header [O] Buffer to receive the ICC profile header.
494 *
495 * RETURNS
496 * Success: TRUE
497 * Failure: FALSE
498 *
499 * NOTES
500 * The profile header returned will be adjusted for endianness.
501 */
502 BOOL WINAPI GetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
503 {
504 #ifdef HAVE_LCMS
505 struct profile *profile = grab_profile( handle );
506
507 TRACE( "( %p, %p )\n", handle, header );
508
509 if (!profile) return FALSE;
510
511 if (!header)
512 {
513 release_profile( profile );
514 return FALSE;
515 }
516 MSCMS_get_profile_header( profile->iccprofile, header );
517
518 release_profile( profile );
519 return TRUE;
520
521 #else
522 return FALSE;
523 #endif /* HAVE_LCMS */
524 }
525
526 /******************************************************************************
527 * GetCountColorProfileElements [MSCMS.@]
528 *
529 * Retrieve the number of elements in a color profile.
530 *
531 * PARAMS
532 * profile [I] Handle to a color profile.
533 * count [O] Pointer to a variable which is set to the number of elements
534 * in the color profile.
535 *
536 * RETURNS
537 * Success: TRUE
538 * Failure: FALSE
539 */
540 BOOL WINAPI GetCountColorProfileElements( HPROFILE handle, PDWORD count )
541 {
542 BOOL ret = FALSE;
543 #ifdef HAVE_LCMS
544 struct profile *profile = grab_profile( handle );
545
546 TRACE( "( %p, %p )\n", handle, count );
547
548 if (!profile) return FALSE;
549
550 if (!count)
551 {
552 release_profile( profile );
553 return FALSE;
554 }
555 *count = MSCMS_get_tag_count( profile->iccprofile );
556
557 release_profile( profile );
558 ret = TRUE;
559
560 #endif /* HAVE_LCMS */
561 return ret;
562 }
563
564 /******************************************************************************
565 * GetStandardColorSpaceProfileA [MSCMS.@]
566 *
567 * See GetStandardColorSpaceProfileW.
568 */
569 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
570 {
571 INT len;
572 LPWSTR profileW;
573 BOOL ret = FALSE;
574 DWORD sizeW;
575
576 TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
577
578 if (machine)
579 {
580 SetLastError( ERROR_NOT_SUPPORTED );
581 return FALSE;
582 }
583
584 if (!size)
585 {
586 SetLastError( ERROR_INVALID_PARAMETER );
587 return FALSE;
588 }
589
590 sizeW = *size * sizeof(WCHAR);
591
592 if (!profile)
593 {
594 ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
595 *size = sizeW / sizeof(WCHAR);
596 return ret;
597 }
598
599 profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
600 if (profileW)
601 {
602 if ((ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW )))
603 {
604 *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
605 len = WideCharToMultiByte( CP_ACP, 0, profileW, -1, profile, *size, NULL, NULL );
606 if (!len) ret = FALSE;
607 }
608 else *size = sizeW / sizeof(WCHAR);
609
610 HeapFree( GetProcessHeap(), 0, profileW );
611 }
612 return ret;
613 }
614
615 /******************************************************************************
616 * GetStandardColorSpaceProfileW [MSCMS.@]
617 *
618 * Retrieve the profile filename for a given standard color space id.
619 *
620 * PARAMS
621 * machine [I] Name of the machine for which to get the standard color space.
622 * Must be NULL, which indicates the local machine.
623 * id [I] Id of a standard color space.
624 * profile [O] Buffer to receive the profile filename.
625 * size [I/O] Size of the filename buffer in bytes.
626 *
627 * RETURNS
628 * Success: TRUE
629 * Failure: FALSE
630 */
631 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
632 {
633 static const WCHAR rgbprofilefile[] =
634 { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
635 's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
636 WCHAR rgbprofile[MAX_PATH];
637 DWORD len = sizeof(rgbprofile);
638
639 TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
640
641 if (machine)
642 {
643 SetLastError( ERROR_NOT_SUPPORTED );
644 return FALSE;
645 }
646
647 if (!size)
648 {
649 SetLastError( ERROR_INVALID_PARAMETER );
650 return FALSE;
651 }
652
653 if (!profile)
654 {
655 SetLastError( ERROR_INSUFFICIENT_BUFFER );
656 return FALSE;
657 }
658
659 GetColorDirectoryW( machine, rgbprofile, &len );
660
661 switch (id)
662 {
663 case LCS_sRGB:
664 case LCS_WINDOWS_COLOR_SPACE: /* FIXME */
665 lstrcatW( rgbprofile, rgbprofilefile );
666 len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
667
668 if (*size < len || !profile)
669 {
670 *size = len;
671 SetLastError( ERROR_MORE_DATA );
672 return FALSE;
673 }
674
675 lstrcpyW( profile, rgbprofile );
676 break;
677
678 default:
679 SetLastError( ERROR_FILE_NOT_FOUND );
680 return FALSE;
681 }
682 return TRUE;
683 }
684
685 static BOOL MSCMS_header_from_file( LPCWSTR file, PPROFILEHEADER header )
686 {
687 BOOL ret;
688 PROFILE profile;
689 WCHAR path[MAX_PATH], slash[] = {'\\',0};
690 DWORD size = sizeof(path);
691 HANDLE handle;
692
693 ret = GetColorDirectoryW( NULL, path, &size );
694 if (!ret)
695 {
696 WARN( "Can't retrieve color directory\n" );
697 return FALSE;
698 }
699 if (size + sizeof(slash) + sizeof(WCHAR) * lstrlenW( file ) > sizeof(path))
700 {
701 WARN( "Filename too long\n" );
702 return FALSE;
703 }
704
705 lstrcatW( path, slash );
706 lstrcatW( path, file );
707
708 profile.dwType = PROFILE_FILENAME;
709 profile.pProfileData = path;
710 profile.cbDataSize = lstrlenW( path ) + 1;
711
712 handle = OpenColorProfileW( &profile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );
713 if (!handle)
714 {
715 WARN( "Can't open color profile\n" );
716 return FALSE;
717 }
718
719 ret = GetColorProfileHeader( handle, header );
720 if (!ret)
721 WARN( "Can't retrieve color profile header\n" );
722
723 CloseColorProfile( handle );
724 return ret;
725 }
726
727 static BOOL MSCMS_match_profile( PENUMTYPEW rec, PPROFILEHEADER hdr )
728 {
729 if (rec->dwFields & ET_DEVICENAME)
730 {
731 FIXME( "ET_DEVICENAME: %s\n", debugstr_w(rec->pDeviceName) );
732 }
733 if (rec->dwFields & ET_MEDIATYPE)
734 {
735 FIXME( "ET_MEDIATYPE: 0x%08x\n", rec->dwMediaType );
736 }
737 if (rec->dwFields & ET_DITHERMODE)
738 {
739 FIXME( "ET_DITHERMODE: 0x%08x\n", rec->dwDitheringMode );
740 }
741 if (rec->dwFields & ET_RESOLUTION)
742 {
743 FIXME( "ET_RESOLUTION: 0x%08x, 0x%08x\n",
744 rec->dwResolution[0], rec->dwResolution[1] );
745 }
746 if (rec->dwFields & ET_DEVICECLASS)
747 {
748 FIXME( "ET_DEVICECLASS: %s\n", MSCMS_dbgstr_tag(rec->dwMediaType) );
749 }
750 if (rec->dwFields & ET_CMMTYPE)
751 {
752 TRACE( "ET_CMMTYPE: %s\n", MSCMS_dbgstr_tag(rec->dwCMMType) );
753 if (rec->dwCMMType != hdr->phCMMType) return FALSE;
754 }
755 if (rec->dwFields & ET_CLASS)
756 {
757 TRACE( "ET_CLASS: %s\n", MSCMS_dbgstr_tag(rec->dwClass) );
758 if (rec->dwClass != hdr->phClass) return FALSE;
759 }
760 if (rec->dwFields & ET_DATACOLORSPACE)
761 {
762 TRACE( "ET_DATACOLORSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwDataColorSpace) );
763 if (rec->dwDataColorSpace != hdr->phDataColorSpace) return FALSE;
764 }
765 if (rec->dwFields & ET_CONNECTIONSPACE)
766 {
767 TRACE( "ET_CONNECTIONSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwConnectionSpace) );
768 if (rec->dwConnectionSpace != hdr->phConnectionSpace) return FALSE;
769 }
770 if (rec->dwFields & ET_SIGNATURE)
771 {
772 TRACE( "ET_SIGNATURE: %s\n", MSCMS_dbgstr_tag(rec->dwSignature) );
773 if (rec->dwSignature != hdr->phSignature) return FALSE;
774 }
775 if (rec->dwFields & ET_PLATFORM)
776 {
777 TRACE( "ET_PLATFORM: %s\n", MSCMS_dbgstr_tag(rec->dwPlatform) );
778 if (rec->dwPlatform != hdr->phPlatform) return FALSE;
779 }
780 if (rec->dwFields & ET_PROFILEFLAGS)
781 {
782 TRACE( "ET_PROFILEFLAGS: 0x%08x\n", rec->dwProfileFlags );
783 if (rec->dwProfileFlags != hdr->phProfileFlags) return FALSE;
784 }
785 if (rec->dwFields & ET_MANUFACTURER)
786 {
787 TRACE( "ET_MANUFACTURER: %s\n", MSCMS_dbgstr_tag(rec->dwManufacturer) );
788 if (rec->dwManufacturer != hdr->phManufacturer) return FALSE;
789 }
790 if (rec->dwFields & ET_MODEL)
791 {
792 TRACE( "ET_MODEL: %s\n", MSCMS_dbgstr_tag(rec->dwModel) );
793 if (rec->dwModel != hdr->phModel) return FALSE;
794 }
795 if (rec->dwFields & ET_ATTRIBUTES)
796 {
797 TRACE( "ET_ATTRIBUTES: 0x%08x, 0x%08x\n",
798 rec->dwAttributes[0], rec->dwAttributes[1] );
799 if (rec->dwAttributes[0] != hdr->phAttributes[0] ||
800 rec->dwAttributes[1] != hdr->phAttributes[1]) return FALSE;
801 }
802 if (rec->dwFields & ET_RENDERINGINTENT)
803 {
804 TRACE( "ET_RENDERINGINTENT: 0x%08x\n", rec->dwRenderingIntent );
805 if (rec->dwRenderingIntent != hdr->phRenderingIntent) return FALSE;
806 }
807 if (rec->dwFields & ET_CREATOR)
808 {
809 TRACE( "ET_CREATOR: %s\n", MSCMS_dbgstr_tag(rec->dwCreator) );
810 if (rec->dwCreator != hdr->phCreator) return FALSE;
811 }
812 return TRUE;
813 }
814
815 /******************************************************************************
816 * EnumColorProfilesA [MSCMS.@]
817 *
818 * See EnumColorProfilesW.
819 */
820 BOOL WINAPI EnumColorProfilesA( PCSTR machine, PENUMTYPEA record, PBYTE buffer,
821 PDWORD size, PDWORD number )
822 {
823 BOOL match, ret = FALSE;
824 char spec[] = "\\*.icm";
825 char colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
826 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
827 PROFILEHEADER header;
828 WIN32_FIND_DATAA data;
829 ENUMTYPEW recordW;
830 WCHAR *fileW = NULL, *deviceW = NULL;
831 HANDLE find;
832
833 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
834
835 if (machine || !record || !size ||
836 record->dwSize != sizeof(ENUMTYPEA) ||
837 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
838
839 ret = GetColorDirectoryA( machine, colordir, &len );
840 if (!ret || len + sizeof(spec) > MAX_PATH)
841 {
842 WARN( "can't retrieve color directory\n" );
843 return FALSE;
844 }
845
846 lstrcpyA( glob, colordir );
847 lstrcatA( glob, spec );
848
849 find = FindFirstFileA( glob, &data );
850 if (find == INVALID_HANDLE_VALUE) return FALSE;
851
852 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(char *) + 1 );
853 if (!profiles) goto exit;
854
855 memcpy( &recordW, record, sizeof(ENUMTYPEA) );
856 if (record->pDeviceName)
857 {
858 deviceW = MSCMS_strdupW( record->pDeviceName );
859 if (!(recordW.pDeviceName = deviceW)) goto exit;
860 }
861
862 fileW = MSCMS_strdupW( data.cFileName );
863 if (!fileW) goto exit;
864
865 ret = MSCMS_header_from_file( fileW, &header );
866 if (ret)
867 {
868 match = MSCMS_match_profile( &recordW, &header );
869 if (match)
870 {
871 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
872 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
873
874 if (!profiles[count]) goto exit;
875 else
876 {
877 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
878 lstrcpyA( profiles[count], data.cFileName );
879 totalsize += len;
880 count++;
881 }
882 }
883 }
884 HeapFree( GetProcessHeap(), 0, fileW );
885 fileW = NULL;
886
887 while (FindNextFileA( find, &data ))
888 {
889 fileW = MSCMS_strdupW( data.cFileName );
890 if (!fileW) goto exit;
891
892 ret = MSCMS_header_from_file( fileW, &header );
893 if (!ret)
894 {
895 HeapFree( GetProcessHeap(), 0, fileW );
896 continue;
897 }
898
899 match = MSCMS_match_profile( &recordW, &header );
900 if (match)
901 {
902 char **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
903 profiles, sizeof(char *) * (count + 1) );
904 if (!tmp) goto exit;
905 else profiles = tmp;
906
907 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
908 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
909
910 if (!profiles[count]) goto exit;
911 else
912 {
913 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
914 lstrcpyA( profiles[count], data.cFileName );
915 totalsize += len;
916 count++;
917 }
918 }
919 HeapFree( GetProcessHeap(), 0, fileW );
920 fileW = NULL;
921 }
922
923 totalsize++;
924 if (buffer && *size >= totalsize)
925 {
926 char *p = (char *)buffer;
927
928 for (i = 0; i < count; i++)
929 {
930 lstrcpyA( p, profiles[i] );
931 p += lstrlenA( profiles[i] ) + 1;
932 }
933 *p = 0;
934 ret = TRUE;
935 }
936 else ret = FALSE;
937
938 *size = totalsize;
939 if (number) *number = count;
940
941 exit:
942 for (i = 0; i < count; i++)
943 HeapFree( GetProcessHeap(), 0, profiles[i] );
944 HeapFree( GetProcessHeap(), 0, profiles );
945 HeapFree( GetProcessHeap(), 0, deviceW );
946 HeapFree( GetProcessHeap(), 0, fileW );
947 FindClose( find );
948
949 return ret;
950 }
951
952 /******************************************************************************
953 * EnumColorProfilesW [MSCMS.@]
954 *
955 * Enumerate profiles that match given criteria.
956 *
957 * PARAMS
958 * machine [I] Name of the machine for which to enumerate profiles.
959 * Must be NULL, which indicates the local machine.
960 * record [I] Record of criteria that a profile must match.
961 * buffer [O] Buffer to receive a string array of profile filenames.
962 * size [I/O] Size of the filename buffer in bytes.
963 * number [O] Number of filenames copied into buffer.
964 *
965 * RETURNS
966 * Success: TRUE
967 * Failure: FALSE
968 */
969 BOOL WINAPI EnumColorProfilesW( PCWSTR machine, PENUMTYPEW record, PBYTE buffer,
970 PDWORD size, PDWORD number )
971 {
972 BOOL match, ret = FALSE;
973 WCHAR spec[] = {'\\','*','i','c','m',0};
974 WCHAR colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
975 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
976 PROFILEHEADER header;
977 WIN32_FIND_DATAW data;
978 HANDLE find;
979
980 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
981
982 if (machine || !record || !size ||
983 record->dwSize != sizeof(ENUMTYPEW) ||
984 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
985
986 ret = GetColorDirectoryW( machine, colordir, &len );
987 if (!ret || len + sizeof(spec) > MAX_PATH)
988 {
989 WARN( "Can't retrieve color directory\n" );
990 return FALSE;
991 }
992
993 lstrcpyW( glob, colordir );
994 lstrcatW( glob, spec );
995
996 find = FindFirstFileW( glob, &data );
997 if (find == INVALID_HANDLE_VALUE) return FALSE;
998
999 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR *) + 1 );
1000 if (!profiles) goto exit;
1001
1002 ret = MSCMS_header_from_file( data.cFileName, &header );
1003 if (ret)
1004 {
1005 match = MSCMS_match_profile( record, &header );
1006 if (match)
1007 {
1008 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
1009 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
1010
1011 if (!profiles[count]) goto exit;
1012 else
1013 {
1014 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
1015 lstrcpyW( profiles[count], data.cFileName );
1016 totalsize += len;
1017 count++;
1018 }
1019 }
1020 }
1021
1022 while (FindNextFileW( find, &data ))
1023 {
1024 ret = MSCMS_header_from_file( data.cFileName, &header );
1025 if (!ret) continue;
1026
1027 match = MSCMS_match_profile( record, &header );
1028 if (match)
1029 {
1030 WCHAR **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1031 profiles, sizeof(WCHAR *) * (count + 1) );
1032 if (!tmp) goto exit;
1033 else profiles = tmp;
1034
1035 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
1036 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
1037
1038 if (!profiles[count]) goto exit;
1039 else
1040 {
1041 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
1042 lstrcpyW( profiles[count], data.cFileName );
1043 totalsize += len;
1044 count++;
1045 }
1046 }
1047 }
1048
1049 totalsize++;
1050 if (buffer && *size >= totalsize)
1051 {
1052 WCHAR *p = (WCHAR *)buffer;
1053
1054 for (i = 0; i < count; i++)
1055 {
1056 lstrcpyW( p, profiles[i] );
1057 p += lstrlenW( profiles[i] ) + 1;
1058 }
1059 *p = 0;
1060 ret = TRUE;
1061 }
1062 else ret = FALSE;
1063
1064 *size = totalsize;
1065 if (number) *number = count;
1066
1067 exit:
1068 for (i = 0; i < count; i++)
1069 HeapFree( GetProcessHeap(), 0, profiles[i] );
1070 HeapFree( GetProcessHeap(), 0, profiles );
1071 FindClose( find );
1072
1073 return ret;
1074 }
1075
1076 /******************************************************************************
1077 * InstallColorProfileA [MSCMS.@]
1078 *
1079 * See InstallColorProfileW.
1080 */
1081 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
1082 {
1083 UINT len;
1084 LPWSTR profileW;
1085 BOOL ret = FALSE;
1086
1087 TRACE( "( %s )\n", debugstr_a(profile) );
1088
1089 if (machine || !profile) return FALSE;
1090
1091 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1092 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1093
1094 if (profileW)
1095 {
1096 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1097
1098 ret = InstallColorProfileW( NULL, profileW );
1099 HeapFree( GetProcessHeap(), 0, profileW );
1100 }
1101 return ret;
1102 }
1103
1104 /******************************************************************************
1105 * InstallColorProfileW [MSCMS.@]
1106 *
1107 * Install a color profile.
1108 *
1109 * PARAMS
1110 * machine [I] Name of the machine to install the profile on. Must be NULL,
1111 * which indicates the local machine.
1112 * profile [I] Full path name of the profile to install.
1113 *
1114 * RETURNS
1115 * Success: TRUE
1116 * Failure: FALSE
1117 */
1118 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
1119 {
1120 WCHAR dest[MAX_PATH], base[MAX_PATH];
1121 DWORD size = sizeof(dest);
1122 static const WCHAR slash[] = { '\\', 0 };
1123
1124 TRACE( "( %s )\n", debugstr_w(profile) );
1125
1126 if (machine || !profile) return FALSE;
1127
1128 if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
1129
1130 MSCMS_basename( profile, base );
1131
1132 lstrcatW( dest, slash );
1133 lstrcatW( dest, base );
1134
1135 /* Is source equal to destination? */
1136 if (!lstrcmpW( profile, dest )) return TRUE;
1137
1138 return CopyFileW( profile, dest, TRUE );
1139 }
1140
1141 /******************************************************************************
1142 * IsColorProfileTagPresent [MSCMS.@]
1143 *
1144 * Determine if a given ICC tag type is present in a color profile.
1145 *
1146 * PARAMS
1147 * profile [I] Color profile handle.
1148 * tag [I] ICC tag type.
1149 * present [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
1150 * FALSE otherwise.
1151 *
1152 * RETURNS
1153 * Success: TRUE
1154 * Failure: FALSE
1155 */
1156 BOOL WINAPI IsColorProfileTagPresent( HPROFILE handle, TAGTYPE type, PBOOL present )
1157 {
1158 BOOL ret = FALSE;
1159 #ifdef HAVE_LCMS
1160 struct profile *profile = grab_profile( handle );
1161 DWORD i, count;
1162 icTag tag;
1163
1164 TRACE( "( %p, 0x%08x, %p )\n", handle, type, present );
1165
1166 if (!profile) return FALSE;
1167
1168 if (!present)
1169 {
1170 release_profile( profile );
1171 return FALSE;
1172 }
1173 count = MSCMS_get_tag_count( profile->iccprofile );
1174
1175 for (i = 0; i < count; i++)
1176 {
1177 MSCMS_get_tag_by_index( profile->iccprofile, i, &tag );
1178
1179 if (tag.sig == type)
1180 {
1181 *present = ret = TRUE;
1182 break;
1183 }
1184 }
1185 release_profile( profile );
1186
1187 #endif /* HAVE_LCMS */
1188 return ret;
1189 }
1190
1191 /******************************************************************************
1192 * IsColorProfileValid [MSCMS.@]
1193 *
1194 * Determine if a given color profile is valid.
1195 *
1196 * PARAMS
1197 * profile [I] Color profile handle.
1198 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
1199 * FALSE otherwise.
1200 *
1201 * RETURNS
1202 * Success: TRUE
1203 * Failure: FALSE
1204 */
1205 BOOL WINAPI IsColorProfileValid( HPROFILE handle, PBOOL valid )
1206 {
1207 BOOL ret = FALSE;
1208 #ifdef HAVE_LCMS
1209 struct profile *profile = grab_profile( handle );
1210
1211 TRACE( "( %p, %p )\n", handle, valid );
1212
1213 if (!profile) return FALSE;
1214
1215 if (!valid)
1216 {
1217 release_profile( profile );
1218 return FALSE;
1219 }
1220 if (profile->iccprofile) ret = *valid = TRUE;
1221 release_profile( profile );
1222
1223 #endif /* HAVE_LCMS */
1224 return ret;
1225 }
1226
1227 /******************************************************************************
1228 * SetColorProfileElement [MSCMS.@]
1229 *
1230 * Set data for a specified tag type.
1231 *
1232 * PARAMS
1233 * profile [I] Handle to a color profile.
1234 * type [I] ICC tag type.
1235 * offset [I] Offset in bytes to start copying to.
1236 * size [I/O] Size of the buffer in bytes. On return the variable holds the
1237 * number of bytes actually needed.
1238 * buffer [O] Buffer holding the tag data.
1239 *
1240 * RETURNS
1241 * Success: TRUE
1242 * Failure: FALSE
1243 */
1244 BOOL WINAPI SetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
1245 PVOID buffer )
1246 {
1247 BOOL ret = FALSE;
1248 #ifdef HAVE_LCMS
1249 struct profile *profile = grab_profile( handle );
1250 DWORD i, count;
1251 icTag tag;
1252
1253 TRACE( "( %p, 0x%08x, %d, %p, %p )\n", handle, type, offset, size, buffer );
1254
1255 if (!profile) return FALSE;
1256
1257 if (!size || !buffer || !(profile->access & PROFILE_READWRITE))
1258 {
1259 release_profile( profile );
1260 return FALSE;
1261 }
1262 count = MSCMS_get_tag_count( profile->iccprofile );
1263
1264 for (i = 0; i < count; i++)
1265 {
1266 MSCMS_get_tag_by_index( profile->iccprofile, i, &tag );
1267
1268 if (tag.sig == type)
1269 {
1270 if (offset > tag.size)
1271 {
1272 release_profile( profile );
1273 return FALSE;
1274 }
1275 MSCMS_set_tag_data( profile->iccprofile, &tag, offset, buffer );
1276
1277 release_profile( profile );
1278 return TRUE;
1279 }
1280 }
1281 release_profile( profile );
1282
1283 #endif /* HAVE_LCMS */
1284 return ret;
1285 }
1286
1287 /******************************************************************************
1288 * SetColorProfileHeader [MSCMS.@]
1289 *
1290 * Set header data for a given profile.
1291 *
1292 * PARAMS
1293 * profile [I] Handle to a color profile.
1294 * header [I] Buffer holding the header data.
1295 *
1296 * RETURNS
1297 * Success: TRUE
1298 * Failure: FALSE
1299 */
1300 BOOL WINAPI SetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
1301 {
1302 #ifdef HAVE_LCMS
1303 struct profile *profile = grab_profile( handle );
1304
1305 TRACE( "( %p, %p )\n", handle, header );
1306
1307 if (!profile) return FALSE;
1308
1309 if (!header || !(profile->access & PROFILE_READWRITE))
1310 {
1311 release_profile( profile );
1312 return FALSE;
1313 }
1314 MSCMS_set_profile_header( profile->iccprofile, header );
1315
1316 release_profile( profile );
1317 return TRUE;
1318
1319 #else
1320 return FALSE;
1321 #endif /* HAVE_LCMS */
1322 }
1323
1324 /******************************************************************************
1325 * UninstallColorProfileA [MSCMS.@]
1326 *
1327 * See UninstallColorProfileW.
1328 */
1329 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
1330 {
1331 UINT len;
1332 LPWSTR profileW;
1333 BOOL ret = FALSE;
1334
1335 TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
1336
1337 if (machine || !profile) return FALSE;
1338
1339 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1340 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1341
1342 if (profileW)
1343 {
1344 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1345
1346 ret = UninstallColorProfileW( NULL, profileW , delete );
1347
1348 HeapFree( GetProcessHeap(), 0, profileW );
1349 }
1350 return ret;
1351 }
1352
1353 /******************************************************************************
1354 * UninstallColorProfileW [MSCMS.@]
1355 *
1356 * Uninstall a color profile.
1357 *
1358 * PARAMS
1359 * machine [I] Name of the machine to uninstall the profile on. Must be NULL,
1360 * which indicates the local machine.
1361 * profile [I] Full path name of the profile to uninstall.
1362 * delete [I] Bool that specifies whether the profile file should be deleted.
1363 *
1364 * RETURNS
1365 * Success: TRUE
1366 * Failure: FALSE
1367 */
1368 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
1369 {
1370 TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
1371
1372 if (machine || !profile) return FALSE;
1373
1374 if (delete) return DeleteFileW( profile );
1375
1376 return TRUE;
1377 }
1378
1379 /******************************************************************************
1380 * OpenColorProfileA [MSCMS.@]
1381 *
1382 * See OpenColorProfileW.
1383 */
1384 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1385 {
1386 HPROFILE handle = NULL;
1387
1388 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1389
1390 if (!profile || !profile->pProfileData) return NULL;
1391
1392 /* No AW conversion needed for memory based profiles */
1393 if (profile->dwType & PROFILE_MEMBUFFER)
1394 return OpenColorProfileW( profile, access, sharing, creation );
1395
1396 if (profile->dwType & PROFILE_FILENAME)
1397 {
1398 UINT len;
1399 PROFILE profileW;
1400
1401 profileW.dwType = profile->dwType;
1402
1403 len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
1404 profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1405
1406 if (profileW.pProfileData)
1407 {
1408 profileW.cbDataSize = len * sizeof(WCHAR);
1409 MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
1410
1411 handle = OpenColorProfileW( &profileW, access, sharing, creation );
1412 HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
1413 }
1414 }
1415 return handle;
1416 }
1417
1418 /******************************************************************************
1419 * OpenColorProfileW [MSCMS.@]
1420 *
1421 * Open a color profile.
1422 *
1423 * PARAMS
1424 * profile [I] Pointer to a color profile structure.
1425 * access [I] Desired access.
1426 * sharing [I] Sharing mode.
1427 * creation [I] Creation mode.
1428 *
1429 * RETURNS
1430 * Success: Handle to the opened profile.
1431 * Failure: NULL
1432 *
1433 * NOTES
1434 * Values for access: PROFILE_READ or PROFILE_READWRITE.
1435 * Values for sharing: 0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
1436 * Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
1437 * OPEN_ALWAYS, TRUNCATE_EXISTING.
1438 * Sharing and creation flags are ignored for memory based profiles.
1439 */
1440 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1441 {
1442 #ifdef HAVE_LCMS
1443 cmsHPROFILE cmsprofile = NULL;
1444 icProfile *iccprofile = NULL;
1445 HANDLE handle = INVALID_HANDLE_VALUE;
1446
1447 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1448
1449 if (!profile || !profile->pProfileData) return NULL;
1450
1451 if (profile->dwType == PROFILE_MEMBUFFER)
1452 {
1453 /* FIXME: access flags not implemented for memory based profiles */
1454
1455 if (!(iccprofile = HeapAlloc( GetProcessHeap(), 0, profile->cbDataSize ))) return NULL;
1456 memcpy( iccprofile, profile->pProfileData, profile->cbDataSize );
1457
1458 cmsprofile = cmsOpenProfileFromMem( iccprofile, profile->cbDataSize );
1459 }
1460 else if (profile->dwType == PROFILE_FILENAME)
1461 {
1462 DWORD size, read, flags = 0;
1463
1464 TRACE( "profile file: %s\n", debugstr_w( profile->pProfileData ) );
1465
1466 if (access & PROFILE_READ) flags = GENERIC_READ;
1467 if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
1468
1469 if (!flags) return NULL;
1470 if (!sharing) sharing = FILE_SHARE_READ;
1471
1472 if (!PathIsRelativeW( profile->pProfileData ))
1473 handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
1474 else
1475 {
1476 WCHAR *path;
1477
1478 if (!GetColorDirectoryW( NULL, NULL, &size ) && GetLastError() == ERROR_MORE_DATA)
1479 {
1480 size += (strlenW( profile->pProfileData ) + 2) * sizeof(WCHAR);
1481 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
1482 GetColorDirectoryW( NULL, path, &size );
1483 PathAddBackslashW( path );
1484 strcatW( path, profile->pProfileData );
1485 }
1486 else return NULL;
1487 handle = CreateFileW( path, flags, sharing, NULL, creation, 0, NULL );
1488 HeapFree( GetProcessHeap(), 0, path );
1489 }
1490 if (handle == INVALID_HANDLE_VALUE)
1491 {
1492 WARN( "Unable to open color profile %u\n", GetLastError() );
1493 return NULL;
1494 }
1495
1496 if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
1497 {
1498 ERR( "Unable to retrieve size of color profile\n" );
1499 CloseHandle( handle );
1500 return NULL;
1501 }
1502
1503 iccprofile = HeapAlloc( GetProcessHeap(), 0, size );
1504 if (!iccprofile)
1505 {
1506 ERR( "Unable to allocate memory for color profile\n" );
1507 CloseHandle( handle );
1508 return NULL;
1509 }
1510
1511 if (!ReadFile( handle, iccprofile, size, &read, NULL ) || read != size)
1512 {
1513 ERR( "Unable to read color profile\n" );
1514
1515 CloseHandle( handle );
1516 HeapFree( GetProcessHeap(), 0, iccprofile );
1517 return NULL;
1518 }
1519
1520 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
1521 }
1522 else
1523 {
1524 ERR( "Invalid profile type %u\n", profile->dwType );
1525 return NULL;
1526 }
1527
1528 if (cmsprofile)
1529 {
1530 struct profile profile;
1531
1532 profile.file = handle;
1533 profile.access = access;
1534 profile.iccprofile = iccprofile;
1535 profile.cmsprofile = cmsprofile;
1536
1537 return create_profile( &profile );
1538 }
1539
1540 #endif /* HAVE_LCMS */
1541 return NULL;
1542 }
1543
1544 /******************************************************************************
1545 * CloseColorProfile [MSCMS.@]
1546 *
1547 * Close a color profile.
1548 *
1549 * PARAMS
1550 * profile [I] Handle to the profile.
1551 *
1552 * RETURNS
1553 * Success: TRUE
1554 * Failure: FALSE
1555 */
1556 BOOL WINAPI CloseColorProfile( HPROFILE profile )
1557 {
1558 BOOL ret = FALSE;
1559 #ifdef HAVE_LCMS
1560
1561 TRACE( "( %p )\n", profile );
1562 ret = close_profile( profile );
1563
1564 #endif /* HAVE_LCMS */
1565 return ret;
1566 }