2 * MSCMS - Color Management System for Wine
4 * Copyright 2004, 2005, 2006, 2008 Hans Leidekker
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.
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.
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
22 #include "wine/debug.h"
23 #include "wine/unicode.h"
36 #include "mscms_priv.h"
38 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
40 static void MSCMS_basename( LPCWSTR path
, LPWSTR name
)
42 INT i
= lstrlenW( path
);
44 while (i
> 0 && !IS_SEPARATOR(path
[i
- 1])) i
--;
45 lstrcpyW( name
, &path
[i
] );
48 static inline LPWSTR
MSCMS_strdupW( LPCSTR str
)
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
);
60 const char *MSCMS_dbgstr_tag( DWORD tag
)
62 return wine_dbg_sprintf( "'%c%c%c%c'",
63 (char)(tag
>> 24), (char)(tag
>> 16), (char)(tag
>> 8), (char)(tag
) );
66 WINE_DEFAULT_DEBUG_CHANNEL(mscms
);
68 /******************************************************************************
69 * AssociateColorProfileWithDeviceA [MSCMS.@]
71 BOOL WINAPI
AssociateColorProfileWithDeviceA( PCSTR machine
, PCSTR profile
, PCSTR device
)
75 WCHAR
*profileW
, *deviceW
;
77 TRACE( "( %s, %s, %s )\n", debugstr_a(machine
), debugstr_a(profile
), debugstr_a(device
) );
79 if (!profile
|| !device
)
81 SetLastError( ERROR_INVALID_PARAMETER
);
86 SetLastError( ERROR_NOT_SUPPORTED
);
90 len
= MultiByteToWideChar( CP_ACP
, 0, profile
, -1, NULL
, 0 );
91 if (!(profileW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return FALSE
;
93 MultiByteToWideChar( CP_ACP
, 0, profile
, -1, profileW
, len
);
95 len
= MultiByteToWideChar( CP_ACP
, 0, device
, -1, NULL
, 0 );
96 if ((deviceW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
98 MultiByteToWideChar( CP_ACP
, 0, device
, -1, deviceW
, len
);
99 ret
= AssociateColorProfileWithDeviceW( NULL
, profileW
, deviceW
);
102 HeapFree( GetProcessHeap(), 0, profileW
);
103 HeapFree( GetProcessHeap(), 0, deviceW
);
107 static BOOL
set_profile_device_key( PCWSTR file
, const BYTE
*value
, DWORD size
)
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','\\',
115 PROFILEHEADER header
;
118 HKEY icm_key
, class_key
;
119 WCHAR basenameW
[MAX_PATH
], classW
[5];
121 profile
.dwType
= PROFILE_FILENAME
;
122 profile
.pProfileData
= (PVOID
)file
;
123 profile
.cbDataSize
= (lstrlenW( file
) + 1) * sizeof(WCHAR
);
125 /* FIXME is the profile installed? */
126 if (!(handle
= OpenColorProfileW( &profile
, PROFILE_READ
, 0, OPEN_EXISTING
)))
128 SetLastError( ERROR_INVALID_PROFILE
);
131 if (!GetColorProfileHeader( handle
, &header
))
133 CloseColorProfile( handle
);
134 SetLastError( ERROR_INVALID_PROFILE
);
137 RegCreateKeyExW( HKEY_LOCAL_MACHINE
, icmW
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &icm_key
, NULL
);
139 MSCMS_basename( file
, basenameW
);
140 sprintfW( classW
, fmtW
, (header
.phClass
>> 24) & 0xff, (header
.phClass
>> 16) & 0xff,
141 (header
.phClass
>> 8) & 0xff, header
.phClass
& 0xff );
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
);
147 RegCloseKey( class_key
);
148 RegCloseKey( icm_key
);
149 CloseColorProfile( handle
);
153 /******************************************************************************
154 * AssociateColorProfileWithDeviceW [MSCMS.@]
156 BOOL WINAPI
AssociateColorProfileWithDeviceW( PCWSTR machine
, PCWSTR profile
, PCWSTR device
)
158 static const BYTE dummy_value
[12];
160 TRACE( "( %s, %s, %s )\n", debugstr_w(machine
), debugstr_w(profile
), debugstr_w(device
) );
162 if (!profile
|| !device
)
164 SetLastError( ERROR_INVALID_PARAMETER
);
169 SetLastError( ERROR_NOT_SUPPORTED
);
173 return set_profile_device_key( profile
, dummy_value
, sizeof(dummy_value
) );
176 /******************************************************************************
177 * DisassociateColorProfileFromDeviceA [MSCMS.@]
179 BOOL WINAPI
DisassociateColorProfileFromDeviceA( PCSTR machine
, PCSTR profile
, PCSTR device
)
183 WCHAR
*profileW
, *deviceW
;
185 TRACE( "( %s, %s, %s )\n", debugstr_a(machine
), debugstr_a(profile
), debugstr_a(device
) );
187 if (!profile
|| !device
)
189 SetLastError( ERROR_INVALID_PARAMETER
);
194 SetLastError( ERROR_NOT_SUPPORTED
);
198 len
= MultiByteToWideChar( CP_ACP
, 0, profile
, -1, NULL
, 0 );
199 if (!(profileW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return FALSE
;
201 MultiByteToWideChar( CP_ACP
, 0, profile
, -1, profileW
, len
);
203 len
= MultiByteToWideChar( CP_ACP
, 0, device
, -1, NULL
, 0 );
204 if ((deviceW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
206 MultiByteToWideChar( CP_ACP
, 0, device
, -1, deviceW
, len
);
207 ret
= DisassociateColorProfileFromDeviceW( NULL
, profileW
, deviceW
);
210 HeapFree( GetProcessHeap(), 0, profileW
);
211 HeapFree( GetProcessHeap(), 0, deviceW
);
215 /******************************************************************************
216 * DisassociateColorProfileFromDeviceW [MSCMS.@]
218 BOOL WINAPI
DisassociateColorProfileFromDeviceW( PCWSTR machine
, PCWSTR profile
, PCWSTR device
)
220 TRACE( "( %s, %s, %s )\n", debugstr_w(machine
), debugstr_w(profile
), debugstr_w(device
) );
222 if (!profile
|| !device
)
224 SetLastError( ERROR_INVALID_PARAMETER
);
229 SetLastError( ERROR_NOT_SUPPORTED
);
233 return set_profile_device_key( profile
, NULL
, 0 );
236 /******************************************************************************
237 * GetColorDirectoryA [MSCMS.@]
239 * See GetColorDirectoryW.
241 BOOL WINAPI
GetColorDirectoryA( PCSTR machine
, PSTR buffer
, PDWORD size
)
248 TRACE( "( %p, %p )\n", buffer
, size
);
250 if (machine
|| !size
) return FALSE
;
254 ret
= GetColorDirectoryW( NULL
, NULL
, &sizeW
);
255 *size
= sizeW
/ sizeof(WCHAR
);
259 sizeW
= *size
* sizeof(WCHAR
);
261 bufferW
= HeapAlloc( GetProcessHeap(), 0, sizeW
);
265 if ((ret
= GetColorDirectoryW( NULL
, bufferW
, &sizeW
)))
267 *size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
268 len
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, *size
, NULL
, NULL
);
269 if (!len
) ret
= FALSE
;
271 else *size
= sizeW
/ sizeof(WCHAR
);
273 HeapFree( GetProcessHeap(), 0, bufferW
);
278 /******************************************************************************
279 * GetColorDirectoryW [MSCMS.@]
281 * Get the directory where color profiles are stored.
284 * machine [I] Name of the machine for which to get the color directory.
285 * Must be NULL, which indicates the local machine.
286 * buffer [I] Buffer to receive the path name.
287 * size [I/O] Size of the buffer in bytes. On return the variable holds
288 * the number of bytes actually needed.
290 BOOL WINAPI
GetColorDirectoryW( PCWSTR machine
, PWSTR buffer
, PDWORD size
)
292 WCHAR colordir
[MAX_PATH
];
293 static const WCHAR colorsubdir
[] =
294 {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r',0};
297 TRACE( "( %p, %p )\n", buffer
, size
);
299 if (machine
|| !size
) return FALSE
;
301 GetSystemDirectoryW( colordir
, sizeof(colordir
) / sizeof(WCHAR
) );
302 lstrcatW( colordir
, colorsubdir
);
304 len
= lstrlenW( colordir
) * sizeof(WCHAR
);
306 if (buffer
&& len
<= *size
)
308 lstrcpyW( buffer
, colordir
);
313 SetLastError( ERROR_MORE_DATA
);
318 /******************************************************************************
319 * GetColorProfileElement [MSCMS.@]
321 * Retrieve data for a specified tag type.
324 * profile [I] Handle to a color profile.
325 * type [I] ICC tag type.
326 * offset [I] Offset in bytes to start copying from.
327 * size [I/O] Size of the buffer in bytes. On return the variable holds
328 * the number of bytes actually needed.
329 * buffer [O] Buffer to receive the tag data.
330 * ref [O] Pointer to a BOOL that specifies whether more than one tag
331 * references the data.
337 BOOL WINAPI
GetColorProfileElement( HPROFILE handle
, TAGTYPE type
, DWORD offset
, PDWORD size
,
338 PVOID buffer
, PBOOL ref
)
342 struct profile
*profile
= grab_profile( handle
);
346 TRACE( "( %p, 0x%08x, %d, %p, %p, %p )\n", handle
, type
, offset
, size
, buffer
, ref
);
348 if (!profile
) return FALSE
;
352 release_profile( profile
);
355 count
= MSCMS_get_tag_count( profile
->iccprofile
);
357 for (i
= 0; i
< count
; i
++)
359 MSCMS_get_tag_by_index( profile
->iccprofile
, i
, &tag
);
363 if ((tag
.size
- offset
) > *size
|| !buffer
)
365 *size
= (tag
.size
- offset
);
366 release_profile( profile
);
369 MSCMS_get_tag_data( profile
->iccprofile
, &tag
, offset
, buffer
);
371 *ref
= FALSE
; /* FIXME: calculate properly */
372 release_profile( profile
);
376 release_profile( profile
);
378 #endif /* HAVE_LCMS */
382 /******************************************************************************
383 * GetColorProfileElementTag [MSCMS.@]
385 * Get the tag type from a color profile by index.
388 * profile [I] Handle to a color profile.
389 * index [I] Index into the tag table of the color profile.
390 * type [O] Pointer to a variable that holds the ICC tag type on return.
397 * The tag table index starts at 1.
398 * Use GetCountColorProfileElements to retrieve a count of tagged elements.
400 BOOL WINAPI
GetColorProfileElementTag( HPROFILE handle
, DWORD index
, PTAGTYPE type
)
404 struct profile
*profile
= grab_profile( handle
);
408 TRACE( "( %p, %d, %p )\n", handle
, index
, type
);
410 if (!profile
) return FALSE
;
414 release_profile( profile
);
417 count
= MSCMS_get_tag_count( profile
->iccprofile
);
418 if (index
> count
|| index
< 1)
420 release_profile( profile
);
423 MSCMS_get_tag_by_index( profile
->iccprofile
, index
- 1, &tag
);
426 release_profile( profile
);
429 #endif /* HAVE_LCMS */
433 /******************************************************************************
434 * GetColorProfileFromHandle [MSCMS.@]
436 * Retrieve an ICC color profile by handle.
439 * profile [I] Handle to a color profile.
440 * buffer [O] Buffer to receive the ICC profile.
441 * size [I/O] Size of the buffer in bytes. On return the variable holds the
442 * number of bytes actually needed.
449 * The profile returned will be in big-endian format.
451 BOOL WINAPI
GetColorProfileFromHandle( HPROFILE handle
, PBYTE buffer
, PDWORD size
)
455 struct profile
*profile
= grab_profile( handle
);
456 PROFILEHEADER header
;
458 TRACE( "( %p, %p, %p )\n", handle
, buffer
, size
);
460 if (!profile
) return FALSE
;
464 release_profile( profile
);
467 MSCMS_get_profile_header( profile
->iccprofile
, &header
);
469 if (!buffer
|| header
.phSize
> *size
)
471 *size
= header
.phSize
;
472 release_profile( profile
);
476 /* No endian conversion needed */
477 memcpy( buffer
, profile
->iccprofile
, header
.phSize
);
478 *size
= header
.phSize
;
480 release_profile( profile
);
483 #endif /* HAVE_LCMS */
487 /******************************************************************************
488 * GetColorProfileHeader [MSCMS.@]
490 * Retrieve a color profile header by handle.
493 * profile [I] Handle to a color profile.
494 * header [O] Buffer to receive the ICC profile header.
501 * The profile header returned will be adjusted for endianness.
503 BOOL WINAPI
GetColorProfileHeader( HPROFILE handle
, PPROFILEHEADER header
)
506 struct profile
*profile
= grab_profile( handle
);
508 TRACE( "( %p, %p )\n", handle
, header
);
510 if (!profile
) return FALSE
;
514 release_profile( profile
);
517 MSCMS_get_profile_header( profile
->iccprofile
, header
);
519 release_profile( profile
);
524 #endif /* HAVE_LCMS */
527 /******************************************************************************
528 * GetCountColorProfileElements [MSCMS.@]
530 * Retrieve the number of elements in a color profile.
533 * profile [I] Handle to a color profile.
534 * count [O] Pointer to a variable which is set to the number of elements
535 * in the color profile.
541 BOOL WINAPI
GetCountColorProfileElements( HPROFILE handle
, PDWORD count
)
545 struct profile
*profile
= grab_profile( handle
);
547 TRACE( "( %p, %p )\n", handle
, count
);
549 if (!profile
) return FALSE
;
553 release_profile( profile
);
556 *count
= MSCMS_get_tag_count( profile
->iccprofile
);
558 release_profile( profile
);
561 #endif /* HAVE_LCMS */
565 /******************************************************************************
566 * GetStandardColorSpaceProfileA [MSCMS.@]
568 * See GetStandardColorSpaceProfileW.
570 BOOL WINAPI
GetStandardColorSpaceProfileA( PCSTR machine
, DWORD id
, PSTR profile
, PDWORD size
)
577 TRACE( "( 0x%08x, %p, %p )\n", id
, profile
, size
);
581 SetLastError( ERROR_NOT_SUPPORTED
);
587 SetLastError( ERROR_INVALID_PARAMETER
);
591 sizeW
= *size
* sizeof(WCHAR
);
595 ret
= GetStandardColorSpaceProfileW( NULL
, id
, NULL
, &sizeW
);
596 *size
= sizeW
/ sizeof(WCHAR
);
600 profileW
= HeapAlloc( GetProcessHeap(), 0, sizeW
);
604 if ((ret
= GetStandardColorSpaceProfileW( NULL
, id
, profileW
, &sizeW
)))
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
;
610 else *size
= sizeW
/ sizeof(WCHAR
);
612 HeapFree( GetProcessHeap(), 0, profileW
);
617 /******************************************************************************
618 * GetStandardColorSpaceProfileW [MSCMS.@]
620 * Retrieve the profile filename for a given standard color space id.
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.
633 BOOL WINAPI
GetStandardColorSpaceProfileW( PCWSTR machine
, DWORD id
, PWSTR profile
, PDWORD size
)
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
);
641 TRACE( "( 0x%08x, %p, %p )\n", id
, profile
, size
);
645 SetLastError( ERROR_NOT_SUPPORTED
);
651 SetLastError( ERROR_INVALID_PARAMETER
);
657 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
661 GetColorDirectoryW( machine
, rgbprofile
, &len
);
666 case LCS_WINDOWS_COLOR_SPACE
: /* FIXME */
667 lstrcatW( rgbprofile
, rgbprofilefile
);
668 len
= lstrlenW( rgbprofile
) * sizeof(WCHAR
);
670 if (*size
< len
|| !profile
)
673 SetLastError( ERROR_MORE_DATA
);
677 lstrcpyW( profile
, rgbprofile
);
681 SetLastError( ERROR_FILE_NOT_FOUND
);
687 static BOOL
MSCMS_header_from_file( LPCWSTR file
, PPROFILEHEADER header
)
691 WCHAR path
[MAX_PATH
], slash
[] = {'\\',0};
692 DWORD size
= sizeof(path
);
695 ret
= GetColorDirectoryW( NULL
, path
, &size
);
698 WARN( "Can't retrieve color directory\n" );
701 if (size
+ sizeof(slash
) + sizeof(WCHAR
) * lstrlenW( file
) > sizeof(path
))
703 WARN( "Filename too long\n" );
707 lstrcatW( path
, slash
);
708 lstrcatW( path
, file
);
710 profile
.dwType
= PROFILE_FILENAME
;
711 profile
.pProfileData
= path
;
712 profile
.cbDataSize
= lstrlenW( path
) + 1;
714 handle
= OpenColorProfileW( &profile
, PROFILE_READ
, FILE_SHARE_READ
, OPEN_EXISTING
);
717 WARN( "Can't open color profile\n" );
721 ret
= GetColorProfileHeader( handle
, header
);
723 WARN( "Can't retrieve color profile header\n" );
725 CloseColorProfile( handle
);
729 static BOOL
MSCMS_match_profile( PENUMTYPEW rec
, PPROFILEHEADER hdr
)
731 if (rec
->dwFields
& ET_DEVICENAME
)
733 FIXME( "ET_DEVICENAME: %s\n", debugstr_w(rec
->pDeviceName
) );
735 if (rec
->dwFields
& ET_MEDIATYPE
)
737 FIXME( "ET_MEDIATYPE: 0x%08x\n", rec
->dwMediaType
);
739 if (rec
->dwFields
& ET_DITHERMODE
)
741 FIXME( "ET_DITHERMODE: 0x%08x\n", rec
->dwDitheringMode
);
743 if (rec
->dwFields
& ET_RESOLUTION
)
745 FIXME( "ET_RESOLUTION: 0x%08x, 0x%08x\n",
746 rec
->dwResolution
[0], rec
->dwResolution
[1] );
748 if (rec
->dwFields
& ET_DEVICECLASS
)
750 FIXME( "ET_DEVICECLASS: %s\n", MSCMS_dbgstr_tag(rec
->dwMediaType
) );
752 if (rec
->dwFields
& ET_CMMTYPE
)
754 TRACE( "ET_CMMTYPE: %s\n", MSCMS_dbgstr_tag(rec
->dwCMMType
) );
755 if (rec
->dwCMMType
!= hdr
->phCMMType
) return FALSE
;
757 if (rec
->dwFields
& ET_CLASS
)
759 TRACE( "ET_CLASS: %s\n", MSCMS_dbgstr_tag(rec
->dwClass
) );
760 if (rec
->dwClass
!= hdr
->phClass
) return FALSE
;
762 if (rec
->dwFields
& ET_DATACOLORSPACE
)
764 TRACE( "ET_DATACOLORSPACE: %s\n", MSCMS_dbgstr_tag(rec
->dwDataColorSpace
) );
765 if (rec
->dwDataColorSpace
!= hdr
->phDataColorSpace
) return FALSE
;
767 if (rec
->dwFields
& ET_CONNECTIONSPACE
)
769 TRACE( "ET_CONNECTIONSPACE: %s\n", MSCMS_dbgstr_tag(rec
->dwConnectionSpace
) );
770 if (rec
->dwConnectionSpace
!= hdr
->phConnectionSpace
) return FALSE
;
772 if (rec
->dwFields
& ET_SIGNATURE
)
774 TRACE( "ET_SIGNATURE: %s\n", MSCMS_dbgstr_tag(rec
->dwSignature
) );
775 if (rec
->dwSignature
!= hdr
->phSignature
) return FALSE
;
777 if (rec
->dwFields
& ET_PLATFORM
)
779 TRACE( "ET_PLATFORM: %s\n", MSCMS_dbgstr_tag(rec
->dwPlatform
) );
780 if (rec
->dwPlatform
!= hdr
->phPlatform
) return FALSE
;
782 if (rec
->dwFields
& ET_PROFILEFLAGS
)
784 TRACE( "ET_PROFILEFLAGS: 0x%08x\n", rec
->dwProfileFlags
);
785 if (rec
->dwProfileFlags
!= hdr
->phProfileFlags
) return FALSE
;
787 if (rec
->dwFields
& ET_MANUFACTURER
)
789 TRACE( "ET_MANUFACTURER: %s\n", MSCMS_dbgstr_tag(rec
->dwManufacturer
) );
790 if (rec
->dwManufacturer
!= hdr
->phManufacturer
) return FALSE
;
792 if (rec
->dwFields
& ET_MODEL
)
794 TRACE( "ET_MODEL: %s\n", MSCMS_dbgstr_tag(rec
->dwModel
) );
795 if (rec
->dwModel
!= hdr
->phModel
) return FALSE
;
797 if (rec
->dwFields
& ET_ATTRIBUTES
)
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
;
804 if (rec
->dwFields
& ET_RENDERINGINTENT
)
806 TRACE( "ET_RENDERINGINTENT: 0x%08x\n", rec
->dwRenderingIntent
);
807 if (rec
->dwRenderingIntent
!= hdr
->phRenderingIntent
) return FALSE
;
809 if (rec
->dwFields
& ET_CREATOR
)
811 TRACE( "ET_CREATOR: %s\n", MSCMS_dbgstr_tag(rec
->dwCreator
) );
812 if (rec
->dwCreator
!= hdr
->phCreator
) return FALSE
;
817 /******************************************************************************
818 * EnumColorProfilesA [MSCMS.@]
820 * See EnumColorProfilesW.
822 BOOL WINAPI
EnumColorProfilesA( PCSTR machine
, PENUMTYPEA record
, PBYTE buffer
,
823 PDWORD size
, PDWORD number
)
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
;
832 WCHAR
*fileW
= NULL
, *deviceW
= NULL
;
835 TRACE( "( %p, %p, %p, %p, %p )\n", machine
, record
, buffer
, size
, number
);
837 if (machine
|| !record
|| !size
||
838 record
->dwSize
!= sizeof(ENUMTYPEA
) ||
839 record
->dwVersion
!= ENUM_TYPE_VERSION
) return FALSE
;
841 ret
= GetColorDirectoryA( machine
, colordir
, &len
);
842 if (!ret
|| len
+ sizeof(spec
) > MAX_PATH
)
844 WARN( "can't retrieve color directory\n" );
848 lstrcpyA( glob
, colordir
);
849 lstrcatA( glob
, spec
);
851 find
= FindFirstFileA( glob
, &data
);
852 if (find
== INVALID_HANDLE_VALUE
) return FALSE
;
854 profiles
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(char *) + 1 );
855 if (!profiles
) goto exit
;
857 memcpy( &recordW
, record
, sizeof(ENUMTYPEA
) );
858 if (record
->pDeviceName
)
860 deviceW
= MSCMS_strdupW( record
->pDeviceName
);
861 if (!(recordW
.pDeviceName
= deviceW
)) goto exit
;
864 fileW
= MSCMS_strdupW( data
.cFileName
);
865 if (!fileW
) goto exit
;
867 ret
= MSCMS_header_from_file( fileW
, &header
);
870 match
= MSCMS_match_profile( &recordW
, &header
);
873 len
= sizeof(char) * (lstrlenA( data
.cFileName
) + 1);
874 profiles
[count
] = HeapAlloc( GetProcessHeap(), 0, len
);
876 if (!profiles
[count
]) goto exit
;
879 TRACE( "matching profile: %s\n", debugstr_a(data
.cFileName
) );
880 lstrcpyA( profiles
[count
], data
.cFileName
);
886 HeapFree( GetProcessHeap(), 0, fileW
);
889 while (FindNextFileA( find
, &data
))
891 fileW
= MSCMS_strdupW( data
.cFileName
);
892 if (!fileW
) goto exit
;
894 ret
= MSCMS_header_from_file( fileW
, &header
);
897 HeapFree( GetProcessHeap(), 0, fileW
);
901 match
= MSCMS_match_profile( &recordW
, &header
);
904 char **tmp
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
905 profiles
, sizeof(char *) * (count
+ 1) );
909 len
= sizeof(char) * (lstrlenA( data
.cFileName
) + 1);
910 profiles
[count
] = HeapAlloc( GetProcessHeap(), 0, len
);
912 if (!profiles
[count
]) goto exit
;
915 TRACE( "matching profile: %s\n", debugstr_a(data
.cFileName
) );
916 lstrcpyA( profiles
[count
], data
.cFileName
);
921 HeapFree( GetProcessHeap(), 0, fileW
);
926 if (buffer
&& *size
>= totalsize
)
928 char *p
= (char *)buffer
;
930 for (i
= 0; i
< count
; i
++)
932 lstrcpyA( p
, profiles
[i
] );
933 p
+= lstrlenA( profiles
[i
] ) + 1;
941 if (number
) *number
= count
;
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
);
954 /******************************************************************************
955 * EnumColorProfilesW [MSCMS.@]
957 * Enumerate profiles that match given criteria.
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.
971 BOOL WINAPI
EnumColorProfilesW( PCWSTR machine
, PENUMTYPEW record
, PBYTE buffer
,
972 PDWORD size
, PDWORD number
)
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
;
982 TRACE( "( %p, %p, %p, %p, %p )\n", machine
, record
, buffer
, size
, number
);
984 if (machine
|| !record
|| !size
||
985 record
->dwSize
!= sizeof(ENUMTYPEW
) ||
986 record
->dwVersion
!= ENUM_TYPE_VERSION
) return FALSE
;
988 ret
= GetColorDirectoryW( machine
, colordir
, &len
);
989 if (!ret
|| len
+ sizeof(spec
) > MAX_PATH
)
991 WARN( "Can't retrieve color directory\n" );
995 lstrcpyW( glob
, colordir
);
996 lstrcatW( glob
, spec
);
998 find
= FindFirstFileW( glob
, &data
);
999 if (find
== INVALID_HANDLE_VALUE
) return FALSE
;
1001 profiles
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WCHAR
*) + 1 );
1002 if (!profiles
) goto exit
;
1004 ret
= MSCMS_header_from_file( data
.cFileName
, &header
);
1007 match
= MSCMS_match_profile( record
, &header
);
1010 len
= sizeof(WCHAR
) * (lstrlenW( data
.cFileName
) + 1);
1011 profiles
[count
] = HeapAlloc( GetProcessHeap(), 0, len
);
1013 if (!profiles
[count
]) goto exit
;
1016 TRACE( "matching profile: %s\n", debugstr_w(data
.cFileName
) );
1017 lstrcpyW( profiles
[count
], data
.cFileName
);
1024 while (FindNextFileW( find
, &data
))
1026 ret
= MSCMS_header_from_file( data
.cFileName
, &header
);
1029 match
= MSCMS_match_profile( record
, &header
);
1032 WCHAR
**tmp
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
1033 profiles
, sizeof(WCHAR
*) * (count
+ 1) );
1034 if (!tmp
) goto exit
;
1035 else profiles
= tmp
;
1037 len
= sizeof(WCHAR
) * (lstrlenW( data
.cFileName
) + 1);
1038 profiles
[count
] = HeapAlloc( GetProcessHeap(), 0, len
);
1040 if (!profiles
[count
]) goto exit
;
1043 TRACE( "matching profile: %s\n", debugstr_w(data
.cFileName
) );
1044 lstrcpyW( profiles
[count
], data
.cFileName
);
1052 if (buffer
&& *size
>= totalsize
)
1054 WCHAR
*p
= (WCHAR
*)buffer
;
1056 for (i
= 0; i
< count
; i
++)
1058 lstrcpyW( p
, profiles
[i
] );
1059 p
+= lstrlenW( profiles
[i
] ) + 1;
1067 if (number
) *number
= count
;
1070 for (i
= 0; i
< count
; i
++)
1071 HeapFree( GetProcessHeap(), 0, profiles
[i
] );
1072 HeapFree( GetProcessHeap(), 0, profiles
);
1078 /******************************************************************************
1079 * InstallColorProfileA [MSCMS.@]
1081 * See InstallColorProfileW.
1083 BOOL WINAPI
InstallColorProfileA( PCSTR machine
, PCSTR profile
)
1089 TRACE( "( %s )\n", debugstr_a(profile
) );
1091 if (machine
|| !profile
) return FALSE
;
1093 len
= MultiByteToWideChar( CP_ACP
, 0, profile
, -1, NULL
, 0 );
1094 profileW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1098 MultiByteToWideChar( CP_ACP
, 0, profile
, -1, profileW
, len
);
1100 ret
= InstallColorProfileW( NULL
, profileW
);
1101 HeapFree( GetProcessHeap(), 0, profileW
);
1106 /******************************************************************************
1107 * InstallColorProfileW [MSCMS.@]
1109 * Install a color profile.
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.
1120 BOOL WINAPI
InstallColorProfileW( PCWSTR machine
, PCWSTR profile
)
1122 WCHAR dest
[MAX_PATH
], base
[MAX_PATH
];
1123 DWORD size
= sizeof(dest
);
1124 static const WCHAR slash
[] = { '\\', 0 };
1126 TRACE( "( %s )\n", debugstr_w(profile
) );
1128 if (machine
|| !profile
) return FALSE
;
1130 if (!GetColorDirectoryW( machine
, dest
, &size
)) return FALSE
;
1132 MSCMS_basename( profile
, base
);
1134 lstrcatW( dest
, slash
);
1135 lstrcatW( dest
, base
);
1137 /* Is source equal to destination? */
1138 if (!lstrcmpW( profile
, dest
)) return TRUE
;
1140 return CopyFileW( profile
, dest
, TRUE
);
1143 /******************************************************************************
1144 * IsColorProfileTagPresent [MSCMS.@]
1146 * Determine if a given ICC tag type is present in a color profile.
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,
1158 BOOL WINAPI
IsColorProfileTagPresent( HPROFILE handle
, TAGTYPE type
, PBOOL present
)
1162 struct profile
*profile
= grab_profile( handle
);
1166 TRACE( "( %p, 0x%08x, %p )\n", handle
, type
, present
);
1168 if (!profile
) return FALSE
;
1172 release_profile( profile
);
1175 count
= MSCMS_get_tag_count( profile
->iccprofile
);
1177 for (i
= 0; i
< count
; i
++)
1179 MSCMS_get_tag_by_index( profile
->iccprofile
, i
, &tag
);
1181 if (tag
.sig
== type
)
1183 *present
= ret
= TRUE
;
1187 release_profile( profile
);
1189 #endif /* HAVE_LCMS */
1193 /******************************************************************************
1194 * IsColorProfileValid [MSCMS.@]
1196 * Determine if a given color profile is valid.
1199 * profile [I] Color profile handle.
1200 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
1207 BOOL WINAPI
IsColorProfileValid( HPROFILE handle
, PBOOL valid
)
1211 struct profile
*profile
= grab_profile( handle
);
1213 TRACE( "( %p, %p )\n", handle
, valid
);
1215 if (!profile
) return FALSE
;
1219 release_profile( profile
);
1222 if (profile
->iccprofile
) ret
= *valid
= TRUE
;
1223 release_profile( profile
);
1225 #endif /* HAVE_LCMS */
1229 /******************************************************************************
1230 * SetColorProfileElement [MSCMS.@]
1232 * Set data for a specified tag type.
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.
1246 BOOL WINAPI
SetColorProfileElement( HPROFILE handle
, TAGTYPE type
, DWORD offset
, PDWORD size
,
1251 struct profile
*profile
= grab_profile( handle
);
1255 TRACE( "( %p, 0x%08x, %d, %p, %p )\n", handle
, type
, offset
, size
, buffer
);
1257 if (!profile
) return FALSE
;
1259 if (!size
|| !buffer
|| !(profile
->access
& PROFILE_READWRITE
))
1261 release_profile( profile
);
1264 count
= MSCMS_get_tag_count( profile
->iccprofile
);
1266 for (i
= 0; i
< count
; i
++)
1268 MSCMS_get_tag_by_index( profile
->iccprofile
, i
, &tag
);
1270 if (tag
.sig
== type
)
1272 if (offset
> tag
.size
)
1274 release_profile( profile
);
1277 MSCMS_set_tag_data( profile
->iccprofile
, &tag
, offset
, buffer
);
1279 release_profile( profile
);
1283 release_profile( profile
);
1285 #endif /* HAVE_LCMS */
1289 /******************************************************************************
1290 * SetColorProfileHeader [MSCMS.@]
1292 * Set header data for a given profile.
1295 * profile [I] Handle to a color profile.
1296 * header [I] Buffer holding the header data.
1302 BOOL WINAPI
SetColorProfileHeader( HPROFILE handle
, PPROFILEHEADER header
)
1305 struct profile
*profile
= grab_profile( handle
);
1307 TRACE( "( %p, %p )\n", handle
, header
);
1309 if (!profile
) return FALSE
;
1311 if (!header
|| !(profile
->access
& PROFILE_READWRITE
))
1313 release_profile( profile
);
1316 MSCMS_set_profile_header( profile
->iccprofile
, header
);
1318 release_profile( profile
);
1323 #endif /* HAVE_LCMS */
1326 /******************************************************************************
1327 * UninstallColorProfileA [MSCMS.@]
1329 * See UninstallColorProfileW.
1331 BOOL WINAPI
UninstallColorProfileA( PCSTR machine
, PCSTR profile
, BOOL
delete )
1337 TRACE( "( %s, %x )\n", debugstr_a(profile
), delete );
1339 if (machine
|| !profile
) return FALSE
;
1341 len
= MultiByteToWideChar( CP_ACP
, 0, profile
, -1, NULL
, 0 );
1342 profileW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1346 MultiByteToWideChar( CP_ACP
, 0, profile
, -1, profileW
, len
);
1348 ret
= UninstallColorProfileW( NULL
, profileW
, delete );
1350 HeapFree( GetProcessHeap(), 0, profileW
);
1355 /******************************************************************************
1356 * UninstallColorProfileW [MSCMS.@]
1358 * Uninstall a color profile.
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.
1370 BOOL WINAPI
UninstallColorProfileW( PCWSTR machine
, PCWSTR profile
, BOOL
delete )
1372 TRACE( "( %s, %x )\n", debugstr_w(profile
), delete );
1374 if (machine
|| !profile
) return FALSE
;
1376 if (delete) return DeleteFileW( profile
);
1381 /******************************************************************************
1382 * OpenColorProfileA [MSCMS.@]
1384 * See OpenColorProfileW.
1386 HPROFILE WINAPI
OpenColorProfileA( PPROFILE profile
, DWORD access
, DWORD sharing
, DWORD creation
)
1388 HPROFILE handle
= NULL
;
1390 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile
, access
, sharing
, creation
);
1392 if (!profile
|| !profile
->pProfileData
) return NULL
;
1394 /* No AW conversion needed for memory based profiles */
1395 if (profile
->dwType
& PROFILE_MEMBUFFER
)
1396 return OpenColorProfileW( profile
, access
, sharing
, creation
);
1398 if (profile
->dwType
& PROFILE_FILENAME
)
1403 profileW
.dwType
= profile
->dwType
;
1405 len
= MultiByteToWideChar( CP_ACP
, 0, profile
->pProfileData
, -1, NULL
, 0 );
1406 profileW
.pProfileData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1408 if (profileW
.pProfileData
)
1410 profileW
.cbDataSize
= len
* sizeof(WCHAR
);
1411 MultiByteToWideChar( CP_ACP
, 0, profile
->pProfileData
, -1, profileW
.pProfileData
, len
);
1413 handle
= OpenColorProfileW( &profileW
, access
, sharing
, creation
);
1414 HeapFree( GetProcessHeap(), 0, profileW
.pProfileData
);
1420 /******************************************************************************
1421 * OpenColorProfileW [MSCMS.@]
1423 * Open a color profile.
1426 * profile [I] Pointer to a color profile structure.
1427 * access [I] Desired access.
1428 * sharing [I] Sharing mode.
1429 * creation [I] Creation mode.
1432 * Success: Handle to the opened profile.
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.
1442 HPROFILE WINAPI
OpenColorProfileW( PPROFILE profile
, DWORD access
, DWORD sharing
, DWORD creation
)
1445 cmsHPROFILE cmsprofile
= NULL
;
1446 icProfile
*iccprofile
= NULL
;
1447 HANDLE handle
= INVALID_HANDLE_VALUE
;
1449 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile
, access
, sharing
, creation
);
1451 if (!profile
|| !profile
->pProfileData
) return NULL
;
1453 if (profile
->dwType
== PROFILE_MEMBUFFER
)
1455 /* FIXME: access flags not implemented for memory based profiles */
1457 if (!(iccprofile
= HeapAlloc( GetProcessHeap(), 0, profile
->cbDataSize
))) return NULL
;
1458 memcpy( iccprofile
, profile
->pProfileData
, profile
->cbDataSize
);
1460 cmsprofile
= cmsOpenProfileFromMem( iccprofile
, profile
->cbDataSize
);
1462 else if (profile
->dwType
== PROFILE_FILENAME
)
1464 DWORD size
, read
, flags
= 0;
1466 TRACE( "profile file: %s\n", debugstr_w( profile
->pProfileData
) );
1468 if (access
& PROFILE_READ
) flags
= GENERIC_READ
;
1469 if (access
& PROFILE_READWRITE
) flags
= GENERIC_READ
|GENERIC_WRITE
;
1471 if (!flags
) return NULL
;
1472 if (!sharing
) sharing
= FILE_SHARE_READ
;
1474 if (!PathIsRelativeW( profile
->pProfileData
))
1475 handle
= CreateFileW( profile
->pProfileData
, flags
, sharing
, NULL
, creation
, 0, NULL
);
1481 if (!GetColorDirectoryW( NULL
, NULL
, &size
) && GetLastError() == ERROR_MORE_DATA
)
1483 size
+= (strlenW( profile
->pProfileData
) + 2) * sizeof(WCHAR
);
1484 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
1485 GetColorDirectoryW( NULL
, path
, &size
);
1486 PathAddBackslashW( path
);
1487 strcatW( path
, profile
->pProfileData
);
1490 handle
= CreateFileW( path
, flags
, sharing
, NULL
, creation
, 0, NULL
);
1491 HeapFree( GetProcessHeap(), 0, path
);
1493 if (handle
== INVALID_HANDLE_VALUE
)
1495 WARN( "Unable to open color profile %u\n", GetLastError() );
1499 if ((size
= GetFileSize( handle
, NULL
)) == INVALID_FILE_SIZE
)
1501 ERR( "Unable to retrieve size of color profile\n" );
1502 CloseHandle( handle
);
1506 iccprofile
= HeapAlloc( GetProcessHeap(), 0, size
);
1509 ERR( "Unable to allocate memory for color profile\n" );
1510 CloseHandle( handle
);
1514 if (!ReadFile( handle
, iccprofile
, size
, &read
, NULL
) || read
!= size
)
1516 ERR( "Unable to read color profile\n" );
1518 CloseHandle( handle
);
1519 HeapFree( GetProcessHeap(), 0, iccprofile
);
1523 cmsprofile
= cmsOpenProfileFromMem( iccprofile
, size
);
1527 ERR( "Invalid profile type %u\n", profile
->dwType
);
1533 struct profile profile
;
1535 profile
.file
= handle
;
1536 profile
.access
= access
;
1537 profile
.iccprofile
= iccprofile
;
1538 profile
.cmsprofile
= cmsprofile
;
1540 return create_profile( &profile
);
1543 #endif /* HAVE_LCMS */
1547 /******************************************************************************
1548 * CloseColorProfile [MSCMS.@]
1550 * Close a color profile.
1553 * profile [I] Handle to the profile.
1559 BOOL WINAPI
CloseColorProfile( HPROFILE profile
)
1564 TRACE( "( %p )\n", profile
);
1565 ret
= close_profile( profile
);
1567 #endif /* HAVE_LCMS */