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