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