source: trunk/src/mscms/profile.c@ 21916

Last change on this file since 21916 was 21916, checked in by dmik, 14 years ago

Merge branch gcc-kmk to trunk.

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