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"
35 #include "mscms_priv.h"
37 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
39 static void MSCMS_basename( LPCWSTR path, LPWSTR name )
41 INT i = lstrlenW( path );
43 while (i > 0 && !IS_SEPARATOR(path[i - 1])) i--;
44 lstrcpyW( name, &path[i] );
47 static inline LPWSTR MSCMS_strdupW( LPCSTR str )
52 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
53 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
54 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
59 const char *MSCMS_dbgstr_tag( DWORD tag )
61 return wine_dbg_sprintf( "'%c%c%c%c'",
62 (char)(tag >> 24), (char)(tag >> 16), (char)(tag >> 8), (char)(tag) );
65 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
67 /******************************************************************************
68 * AssociateColorProfileWithDeviceA [MSCMS.@]
70 BOOL WINAPI AssociateColorProfileWithDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
74 WCHAR *profileW, *deviceW;
76 TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );
78 if (!profile || !device)
80 SetLastError( ERROR_INVALID_PARAMETER );
85 SetLastError( ERROR_NOT_SUPPORTED );
89 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
90 if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
92 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
94 len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
95 if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
97 MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
98 ret = AssociateColorProfileWithDeviceW( NULL, profileW, deviceW );
101 HeapFree( GetProcessHeap(), 0, profileW );
102 HeapFree( GetProcessHeap(), 0, deviceW );
106 static BOOL set_profile_device_key( PCWSTR file, const BYTE *value, DWORD size )
108 static const WCHAR fmtW[] = {'%','c','%','c','%','c','%','c',0};
109 static const WCHAR icmW[] = {'S','o','f','t','w','a','r','e','\\',
110 'M','i','c','r','o','s','o','f','t','\\',
111 'W','i','n','d','o','w','s',' ','N','T','\\',
112 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
114 PROFILEHEADER header;
117 HKEY icm_key, class_key;
118 WCHAR basenameW[MAX_PATH], classW[5];
120 profile.dwType = PROFILE_FILENAME;
121 profile.pProfileData = (PVOID)file;
122 profile.cbDataSize = (lstrlenW( file ) + 1) * sizeof(WCHAR);
124 /* FIXME is the profile installed? */
125 if (!(handle = OpenColorProfileW( &profile, PROFILE_READ, 0, OPEN_EXISTING )))
127 SetLastError( ERROR_INVALID_PROFILE );
130 if (!GetColorProfileHeader( handle, &header ))
132 CloseColorProfile( handle );
133 SetLastError( ERROR_INVALID_PROFILE );
136 RegCreateKeyExW( HKEY_LOCAL_MACHINE, icmW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &icm_key, NULL );
138 MSCMS_basename( file, basenameW );
139 sprintfW( classW, fmtW, (header.phClass >> 24) & 0xff, (header.phClass >> 16) & 0xff,
140 (header.phClass >> 8) & 0xff, header.phClass & 0xff );
142 RegCreateKeyExW( icm_key, classW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &class_key, NULL );
143 if (value) RegSetValueExW( class_key, basenameW, 0, REG_BINARY, value, size );
144 else RegDeleteValueW( class_key, basenameW );
146 RegCloseKey( class_key );
147 RegCloseKey( icm_key );
148 CloseColorProfile( handle );
152 /******************************************************************************
153 * AssociateColorProfileWithDeviceW [MSCMS.@]
155 BOOL WINAPI AssociateColorProfileWithDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
157 static const BYTE dummy_value[12];
159 TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );
161 if (!profile || !device)
163 SetLastError( ERROR_INVALID_PARAMETER );
168 SetLastError( ERROR_NOT_SUPPORTED );
172 return set_profile_device_key( profile, dummy_value, sizeof(dummy_value) );
175 /******************************************************************************
176 * DisassociateColorProfileFromDeviceA [MSCMS.@]
178 BOOL WINAPI DisassociateColorProfileFromDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
182 WCHAR *profileW, *deviceW;
184 TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );
186 if (!profile || !device)
188 SetLastError( ERROR_INVALID_PARAMETER );
193 SetLastError( ERROR_NOT_SUPPORTED );
197 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
198 if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
200 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
202 len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
203 if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
205 MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
206 ret = DisassociateColorProfileFromDeviceW( NULL, profileW, deviceW );
209 HeapFree( GetProcessHeap(), 0, profileW );
210 HeapFree( GetProcessHeap(), 0, deviceW );
214 /******************************************************************************
215 * DisassociateColorProfileFromDeviceW [MSCMS.@]
217 BOOL WINAPI DisassociateColorProfileFromDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
219 TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );
221 if (!profile || !device)
223 SetLastError( ERROR_INVALID_PARAMETER );
228 SetLastError( ERROR_NOT_SUPPORTED );
232 return set_profile_device_key( profile, NULL, 0 );
235 /******************************************************************************
236 * GetColorDirectoryA [MSCMS.@]
238 * See GetColorDirectoryW.
240 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
247 TRACE( "( %p, %p )\n", buffer, size );
249 if (machine || !size) return FALSE;
253 ret = GetColorDirectoryW( NULL, NULL, &sizeW );
254 *size = sizeW / sizeof(WCHAR);
258 sizeW = *size * sizeof(WCHAR);
260 bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
264 if ((ret = GetColorDirectoryW( NULL, bufferW, &sizeW )))
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;
270 else *size = sizeW / sizeof(WCHAR);
272 HeapFree( GetProcessHeap(), 0, bufferW );
277 /******************************************************************************
278 * GetColorDirectoryW [MSCMS.@]
280 * Get the directory where color profiles are stored.
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.
289 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
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};
296 TRACE( "( %p, %p )\n", buffer, size );
298 if (machine || !size) return FALSE;
300 GetSystemDirectoryW( colordir, sizeof(colordir) / sizeof(WCHAR) );
301 lstrcatW( colordir, colorsubdir );
303 len = lstrlenW( colordir ) * sizeof(WCHAR);
305 if (buffer && len <= *size)
307 lstrcpyW( buffer, colordir );
312 SetLastError( ERROR_MORE_DATA );
317 /******************************************************************************
318 * GetColorProfileElement [MSCMS.@]
320 * Retrieve data for a specified tag type.
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.
336 BOOL WINAPI GetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
337 PVOID buffer, PBOOL ref )
341 struct profile *profile = grab_profile( handle );
345 TRACE( "( %p, 0x%08x, %d, %p, %p, %p )\n", handle, type, offset, size, buffer, ref );
347 if (!profile) return FALSE;
351 release_profile( profile );
354 count = MSCMS_get_tag_count( profile->iccprofile );
356 for (i = 0; i < count; i++)
358 MSCMS_get_tag_by_index( profile->iccprofile, i, &tag );
362 if ((tag.size - offset) > *size || !buffer)
364 *size = (tag.size - offset);
365 release_profile( profile );
368 MSCMS_get_tag_data( profile->iccprofile, &tag, offset, buffer );
370 *ref = FALSE; /* FIXME: calculate properly */
371 release_profile( profile );
375 release_profile( profile );
377 #endif /* HAVE_LCMS */
381 /******************************************************************************
382 * GetColorProfileElementTag [MSCMS.@]
384 * Get the tag type from a color profile by index.
387 * profile [I] Handle to a color profile.
388 * index [I] Index into the tag table of the color profile.
389 * type [O] Pointer to a variable that holds the ICC tag type on return.
396 * The tag table index starts at 1.
397 * Use GetCountColorProfileElements to retrieve a count of tagged elements.
399 BOOL WINAPI GetColorProfileElementTag( HPROFILE handle, DWORD index, PTAGTYPE type )
403 struct profile *profile = grab_profile( handle );
407 TRACE( "( %p, %d, %p )\n", handle, index, type );
409 if (!profile) return FALSE;
413 release_profile( profile );
416 count = MSCMS_get_tag_count( profile->iccprofile );
417 if (index > count || index < 1)
419 release_profile( profile );
422 MSCMS_get_tag_by_index( profile->iccprofile, index - 1, &tag );
425 release_profile( profile );
428 #endif /* HAVE_LCMS */
432 /******************************************************************************
433 * GetColorProfileFromHandle [MSCMS.@]
435 * Retrieve an ICC color profile by handle.
438 * profile [I] Handle to a color profile.
439 * buffer [O] Buffer to receive the ICC profile.
440 * size [I/O] Size of the buffer in bytes. On return the variable holds the
441 * number of bytes actually needed.
448 * The profile returned will be in big-endian format.
450 BOOL WINAPI GetColorProfileFromHandle( HPROFILE handle, PBYTE buffer, PDWORD size )
454 struct profile *profile = grab_profile( handle );
455 PROFILEHEADER header;
457 TRACE( "( %p, %p, %p )\n", handle, buffer, size );
459 if (!profile) return FALSE;
463 release_profile( profile );
466 MSCMS_get_profile_header( profile->iccprofile, &header );
468 if (!buffer || header.phSize > *size)
470 *size = header.phSize;
471 release_profile( profile );
475 /* No endian conversion needed */
476 memcpy( buffer, profile->iccprofile, header.phSize );
477 *size = header.phSize;
479 release_profile( profile );
482 #endif /* HAVE_LCMS */
486 /******************************************************************************
487 * GetColorProfileHeader [MSCMS.@]
489 * Retrieve a color profile header by handle.
492 * profile [I] Handle to a color profile.
493 * header [O] Buffer to receive the ICC profile header.
500 * The profile header returned will be adjusted for endianess.
502 BOOL WINAPI GetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
505 struct profile *profile = grab_profile( handle );
507 TRACE( "( %p, %p )\n", handle, header );
509 if (!profile) return FALSE;
513 release_profile( profile );
516 MSCMS_get_profile_header( profile->iccprofile, header );
518 release_profile( profile );
523 #endif /* HAVE_LCMS */
526 /******************************************************************************
527 * GetCountColorProfileElements [MSCMS.@]
529 * Retrieve the number of elements in a color profile.
532 * profile [I] Handle to a color profile.
533 * count [O] Pointer to a variable which is set to the number of elements
534 * in the color profile.
540 BOOL WINAPI GetCountColorProfileElements( HPROFILE handle, PDWORD count )
544 struct profile *profile = grab_profile( handle );
546 TRACE( "( %p, %p )\n", handle, count );
548 if (!profile) return FALSE;
552 release_profile( profile );
555 *count = MSCMS_get_tag_count( profile->iccprofile );
557 release_profile( profile );
560 #endif /* HAVE_LCMS */
564 /******************************************************************************
565 * GetStandardColorSpaceProfileA [MSCMS.@]
567 * See GetStandardColorSpaceProfileW.
569 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
576 TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
580 SetLastError( ERROR_NOT_SUPPORTED );
586 SetLastError( ERROR_INVALID_PARAMETER );
590 sizeW = *size * sizeof(WCHAR);
594 ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
595 *size = sizeW / sizeof(WCHAR);
599 profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
603 if ((ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW )))
605 *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
606 len = WideCharToMultiByte( CP_ACP, 0, profileW, -1, profile, *size, NULL, NULL );
607 if (!len) ret = FALSE;
609 else *size = sizeW / sizeof(WCHAR);
611 HeapFree( GetProcessHeap(), 0, profileW );
616 /******************************************************************************
617 * GetStandardColorSpaceProfileW [MSCMS.@]
619 * Retrieve the profile filename for a given standard color space id.
622 * machine [I] Name of the machine for which to get the standard color space.
623 * Must be NULL, which indicates the local machine.
624 * id [I] Id of a standard color space.
625 * profile [O] Buffer to receive the profile filename.
626 * size [I/O] Size of the filename buffer in bytes.
632 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
634 static const WCHAR rgbprofilefile[] =
635 { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
636 's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
637 WCHAR rgbprofile[MAX_PATH];
638 DWORD len = sizeof(rgbprofile);
640 TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
644 SetLastError( ERROR_NOT_SUPPORTED );
650 SetLastError( ERROR_INVALID_PARAMETER );
656 SetLastError( ERROR_INSUFFICIENT_BUFFER );
660 GetColorDirectoryW( machine, rgbprofile, &len );
664 case LCS_WINDOWS_COLOR_SPACE: /* FIXME */
666 lstrcatW( rgbprofile, rgbprofilefile );
667 len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
669 if (*size < len || !profile)
672 SetLastError( ERROR_MORE_DATA );
676 lstrcpyW( profile, rgbprofile );
680 SetLastError( ERROR_FILE_NOT_FOUND );
686 static BOOL MSCMS_header_from_file( LPCWSTR file, PPROFILEHEADER header )
690 WCHAR path[MAX_PATH], slash[] = {'\\',0};
691 DWORD size = sizeof(path);
694 ret = GetColorDirectoryW( NULL, path, &size );
697 WARN( "Can't retrieve color directory\n" );
700 if (size + sizeof(slash) + sizeof(WCHAR) * lstrlenW( file ) > sizeof(path))
702 WARN( "Filename too long\n" );
706 lstrcatW( path, slash );
707 lstrcatW( path, file );
709 profile.dwType = PROFILE_FILENAME;
710 profile.pProfileData = path;
711 profile.cbDataSize = lstrlenW( path ) + 1;
713 handle = OpenColorProfileW( &profile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );
716 WARN( "Can't open color profile\n" );
720 ret = GetColorProfileHeader( handle, header );
722 WARN( "Can't retrieve color profile header\n" );
724 CloseColorProfile( handle );
728 static BOOL MSCMS_match_profile( PENUMTYPEW rec, PPROFILEHEADER hdr )
730 if (rec->dwFields & ET_DEVICENAME)
732 FIXME( "ET_DEVICENAME: %s\n", debugstr_w(rec->pDeviceName) );
734 if (rec->dwFields & ET_MEDIATYPE)
736 FIXME( "ET_MEDIATYPE: 0x%08x\n", rec->dwMediaType );
738 if (rec->dwFields & ET_DITHERMODE)
740 FIXME( "ET_DITHERMODE: 0x%08x\n", rec->dwDitheringMode );
742 if (rec->dwFields & ET_RESOLUTION)
744 FIXME( "ET_RESOLUTION: 0x%08x, 0x%08x\n",
745 rec->dwResolution[0], rec->dwResolution[1] );
747 if (rec->dwFields & ET_DEVICECLASS)
749 FIXME( "ET_DEVICECLASS: %s\n", MSCMS_dbgstr_tag(rec->dwMediaType) );
751 if (rec->dwFields & ET_CMMTYPE)
753 TRACE( "ET_CMMTYPE: %s\n", MSCMS_dbgstr_tag(rec->dwCMMType) );
754 if (rec->dwCMMType != hdr->phCMMType) return FALSE;
756 if (rec->dwFields & ET_CLASS)
758 TRACE( "ET_CLASS: %s\n", MSCMS_dbgstr_tag(rec->dwClass) );
759 if (rec->dwClass != hdr->phClass) return FALSE;
761 if (rec->dwFields & ET_DATACOLORSPACE)
763 TRACE( "ET_DATACOLORSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwDataColorSpace) );
764 if (rec->dwDataColorSpace != hdr->phDataColorSpace) return FALSE;
766 if (rec->dwFields & ET_CONNECTIONSPACE)
768 TRACE( "ET_CONNECTIONSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwConnectionSpace) );
769 if (rec->dwConnectionSpace != hdr->phConnectionSpace) return FALSE;
771 if (rec->dwFields & ET_SIGNATURE)
773 TRACE( "ET_SIGNATURE: %s\n", MSCMS_dbgstr_tag(rec->dwSignature) );
774 if (rec->dwSignature != hdr->phSignature) return FALSE;
776 if (rec->dwFields & ET_PLATFORM)
778 TRACE( "ET_PLATFORM: %s\n", MSCMS_dbgstr_tag(rec->dwPlatform) );
779 if (rec->dwPlatform != hdr->phPlatform) return FALSE;
781 if (rec->dwFields & ET_PROFILEFLAGS)
783 TRACE( "ET_PROFILEFLAGS: 0x%08x\n", rec->dwProfileFlags );
784 if (rec->dwProfileFlags != hdr->phProfileFlags) return FALSE;
786 if (rec->dwFields & ET_MANUFACTURER)
788 TRACE( "ET_MANUFACTURER: %s\n", MSCMS_dbgstr_tag(rec->dwManufacturer) );
789 if (rec->dwManufacturer != hdr->phManufacturer) return FALSE;
791 if (rec->dwFields & ET_MODEL)
793 TRACE( "ET_MODEL: %s\n", MSCMS_dbgstr_tag(rec->dwModel) );
794 if (rec->dwModel != hdr->phModel) return FALSE;
796 if (rec->dwFields & ET_ATTRIBUTES)
798 TRACE( "ET_ATTRIBUTES: 0x%08x, 0x%08x\n",
799 rec->dwAttributes[0], rec->dwAttributes[1] );
800 if (rec->dwAttributes[0] != hdr->phAttributes[0] ||
801 rec->dwAttributes[1] != hdr->phAttributes[1]) return FALSE;
803 if (rec->dwFields & ET_RENDERINGINTENT)
805 TRACE( "ET_RENDERINGINTENT: 0x%08x\n", rec->dwRenderingIntent );
806 if (rec->dwRenderingIntent != hdr->phRenderingIntent) return FALSE;
808 if (rec->dwFields & ET_CREATOR)
810 TRACE( "ET_CREATOR: %s\n", MSCMS_dbgstr_tag(rec->dwCreator) );
811 if (rec->dwCreator != hdr->phCreator) return FALSE;
816 /******************************************************************************
817 * EnumColorProfilesA [MSCMS.@]
819 * See EnumColorProfilesW.
821 BOOL WINAPI EnumColorProfilesA( PCSTR machine, PENUMTYPEA record, PBYTE buffer,
822 PDWORD size, PDWORD number )
824 BOOL match, ret = FALSE;
825 char spec[] = "\\*.icm";
826 char colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
827 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
828 PROFILEHEADER header;
829 WIN32_FIND_DATAA data;
831 WCHAR *fileW = NULL, *deviceW = NULL;
834 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
836 if (machine || !record || !size ||
837 record->dwSize != sizeof(ENUMTYPEA) ||
838 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
840 ret = GetColorDirectoryA( machine, colordir, &len );
841 if (!ret || len + sizeof(spec) > MAX_PATH)
843 WARN( "can't retrieve color directory\n" );
847 lstrcpyA( glob, colordir );
848 lstrcatA( glob, spec );
850 find = FindFirstFileA( glob, &data );
851 if (find == INVALID_HANDLE_VALUE) return FALSE;
853 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(char *) + 1 );
854 if (!profiles) goto exit;
856 memcpy( &recordW, record, sizeof(ENUMTYPEA) );
857 if (record->pDeviceName)
859 deviceW = MSCMS_strdupW( record->pDeviceName );
860 if (!(recordW.pDeviceName = deviceW)) goto exit;
863 fileW = MSCMS_strdupW( data.cFileName );
864 if (!fileW) goto exit;
866 ret = MSCMS_header_from_file( fileW, &header );
869 match = MSCMS_match_profile( &recordW, &header );
872 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
873 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
875 if (!profiles[count]) goto exit;
878 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
879 lstrcpyA( profiles[count], data.cFileName );
885 HeapFree( GetProcessHeap(), 0, fileW );
888 while (FindNextFileA( find, &data ))
890 fileW = MSCMS_strdupW( data.cFileName );
891 if (!fileW) goto exit;
893 ret = MSCMS_header_from_file( fileW, &header );
896 HeapFree( GetProcessHeap(), 0, fileW );
900 match = MSCMS_match_profile( &recordW, &header );
903 char **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
904 profiles, sizeof(char *) * (count + 1) );
908 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
909 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
911 if (!profiles[count]) goto exit;
914 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
915 lstrcpyA( profiles[count], data.cFileName );
920 HeapFree( GetProcessHeap(), 0, fileW );
925 if (buffer && *size >= totalsize)
927 char *p = (char *)buffer;
929 for (i = 0; i < count; i++)
931 lstrcpyA( p, profiles[i] );
932 p += lstrlenA( profiles[i] ) + 1;
940 if (number) *number = count;
943 for (i = 0; i < count; i++)
944 HeapFree( GetProcessHeap(), 0, profiles[i] );
945 HeapFree( GetProcessHeap(), 0, profiles );
946 HeapFree( GetProcessHeap(), 0, deviceW );
947 HeapFree( GetProcessHeap(), 0, fileW );
953 /******************************************************************************
954 * EnumColorProfilesW [MSCMS.@]
956 * Enumerate profiles that match given criteria.
959 * machine [I] Name of the machine for which to enumerate profiles.
960 * Must be NULL, which indicates the local machine.
961 * record [I] Record of criteria that a profile must match.
962 * buffer [O] Buffer to receive a string array of profile filenames.
963 * size [I/O] Size of the filename buffer in bytes.
964 * number [O] Number of filenames copied into buffer.
970 BOOL WINAPI EnumColorProfilesW( PCWSTR machine, PENUMTYPEW record, PBYTE buffer,
971 PDWORD size, PDWORD number )
973 BOOL match, ret = FALSE;
974 WCHAR spec[] = {'\\','*','i','c','m',0};
975 WCHAR colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
976 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
977 PROFILEHEADER header;
978 WIN32_FIND_DATAW data;
981 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
983 if (machine || !record || !size ||
984 record->dwSize != sizeof(ENUMTYPEW) ||
985 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
987 ret = GetColorDirectoryW( machine, colordir, &len );
988 if (!ret || len + sizeof(spec) > MAX_PATH)
990 WARN( "Can't retrieve color directory\n" );
994 lstrcpyW( glob, colordir );
995 lstrcatW( glob, spec );
997 find = FindFirstFileW( glob, &data );
998 if (find == INVALID_HANDLE_VALUE) return FALSE;
1000 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR *) + 1 );
1001 if (!profiles) goto exit;
1003 ret = MSCMS_header_from_file( data.cFileName, &header );
1006 match = MSCMS_match_profile( record, &header );
1009 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
1010 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
1012 if (!profiles[count]) goto exit;
1015 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
1016 lstrcpyW( profiles[count], data.cFileName );
1023 while (FindNextFileW( find, &data ))
1025 ret = MSCMS_header_from_file( data.cFileName, &header );
1028 match = MSCMS_match_profile( record, &header );
1031 WCHAR **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1032 profiles, sizeof(WCHAR *) * (count + 1) );
1033 if (!tmp) goto exit;
1034 else profiles = tmp;
1036 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
1037 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
1039 if (!profiles[count]) goto exit;
1042 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
1043 lstrcpyW( profiles[count], data.cFileName );
1051 if (buffer && *size >= totalsize)
1053 WCHAR *p = (WCHAR *)buffer;
1055 for (i = 0; i < count; i++)
1057 lstrcpyW( p, profiles[i] );
1058 p += lstrlenW( profiles[i] ) + 1;
1066 if (number) *number = count;
1069 for (i = 0; i < count; i++)
1070 HeapFree( GetProcessHeap(), 0, profiles[i] );
1071 HeapFree( GetProcessHeap(), 0, profiles );
1077 /******************************************************************************
1078 * InstallColorProfileA [MSCMS.@]
1080 * See InstallColorProfileW.
1082 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
1088 TRACE( "( %s )\n", debugstr_a(profile) );
1090 if (machine || !profile) return FALSE;
1092 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1093 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1097 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1099 ret = InstallColorProfileW( NULL, profileW );
1100 HeapFree( GetProcessHeap(), 0, profileW );
1105 /******************************************************************************
1106 * InstallColorProfileW [MSCMS.@]
1108 * Install a color profile.
1111 * machine [I] Name of the machine to install the profile on. Must be NULL,
1112 * which indicates the local machine.
1113 * profile [I] Full path name of the profile to install.
1119 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
1121 WCHAR dest[MAX_PATH], base[MAX_PATH];
1122 DWORD size = sizeof(dest);
1123 static const WCHAR slash[] = { '\\', 0 };
1125 TRACE( "( %s )\n", debugstr_w(profile) );
1127 if (machine || !profile) return FALSE;
1129 if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
1131 MSCMS_basename( profile, base );
1133 lstrcatW( dest, slash );
1134 lstrcatW( dest, base );
1136 /* Is source equal to destination? */
1137 if (!lstrcmpW( profile, dest )) return TRUE;
1139 return CopyFileW( profile, dest, TRUE );
1142 /******************************************************************************
1143 * IsColorProfileTagPresent [MSCMS.@]
1145 * Determine if a given ICC tag type is present in a color profile.
1148 * profile [I] Color profile handle.
1149 * tag [I] ICC tag type.
1150 * present [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
1157 BOOL WINAPI IsColorProfileTagPresent( HPROFILE handle, TAGTYPE type, PBOOL present )
1161 struct profile *profile = grab_profile( handle );
1165 TRACE( "( %p, 0x%08x, %p )\n", handle, type, present );
1167 if (!profile) return FALSE;
1171 release_profile( profile );
1174 count = MSCMS_get_tag_count( profile->iccprofile );
1176 for (i = 0; i < count; i++)
1178 MSCMS_get_tag_by_index( profile->iccprofile, i, &tag );
1180 if (tag.sig == type)
1182 *present = ret = TRUE;
1186 release_profile( profile );
1188 #endif /* HAVE_LCMS */
1192 /******************************************************************************
1193 * IsColorProfileValid [MSCMS.@]
1195 * Determine if a given color profile is valid.
1198 * profile [I] Color profile handle.
1199 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
1206 BOOL WINAPI IsColorProfileValid( HPROFILE handle, PBOOL valid )
1210 struct profile *profile = grab_profile( handle );
1212 TRACE( "( %p, %p )\n", handle, valid );
1214 if (!profile) return FALSE;
1218 release_profile( profile );
1221 if (profile->iccprofile) ret = *valid = TRUE;
1222 release_profile( profile );
1224 #endif /* HAVE_LCMS */
1228 /******************************************************************************
1229 * SetColorProfileElement [MSCMS.@]
1231 * Set data for a specified tag type.
1234 * profile [I] Handle to a color profile.
1235 * type [I] ICC tag type.
1236 * offset [I] Offset in bytes to start copying to.
1237 * size [I/O] Size of the buffer in bytes. On return the variable holds the
1238 * number of bytes actually needed.
1239 * buffer [O] Buffer holding the tag data.
1245 BOOL WINAPI SetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
1250 struct profile *profile = grab_profile( handle );
1254 TRACE( "( %p, 0x%08x, %d, %p, %p )\n", handle, type, offset, size, buffer );
1256 if (!profile) return FALSE;
1258 if (!size || !buffer || !(profile->access & PROFILE_READWRITE))
1260 release_profile( profile );
1263 count = MSCMS_get_tag_count( profile->iccprofile );
1265 for (i = 0; i < count; i++)
1267 MSCMS_get_tag_by_index( profile->iccprofile, i, &tag );
1269 if (tag.sig == type)
1271 if (offset > tag.size)
1273 release_profile( profile );
1276 MSCMS_set_tag_data( profile->iccprofile, &tag, offset, buffer );
1278 release_profile( profile );
1282 release_profile( profile );
1284 #endif /* HAVE_LCMS */
1288 /******************************************************************************
1289 * SetColorProfileHeader [MSCMS.@]
1291 * Set header data for a given profile.
1294 * profile [I] Handle to a color profile.
1295 * header [I] Buffer holding the header data.
1301 BOOL WINAPI SetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
1304 struct profile *profile = grab_profile( handle );
1306 TRACE( "( %p, %p )\n", handle, header );
1308 if (!profile) return FALSE;
1310 if (!header || !(profile->access & PROFILE_READWRITE))
1312 release_profile( profile );
1315 MSCMS_set_profile_header( profile->iccprofile, header );
1317 release_profile( profile );
1322 #endif /* HAVE_LCMS */
1325 /******************************************************************************
1326 * UninstallColorProfileA [MSCMS.@]
1328 * See UninstallColorProfileW.
1330 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
1336 TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
1338 if (machine || !profile) return FALSE;
1340 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1341 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1345 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1347 ret = UninstallColorProfileW( NULL, profileW , delete );
1349 HeapFree( GetProcessHeap(), 0, profileW );
1354 /******************************************************************************
1355 * UninstallColorProfileW [MSCMS.@]
1357 * Uninstall a color profile.
1360 * machine [I] Name of the machine to uninstall the profile on. Must be NULL,
1361 * which indicates the local machine.
1362 * profile [I] Full path name of the profile to uninstall.
1363 * delete [I] Bool that specifies whether the profile file should be deleted.
1369 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
1371 TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
1373 if (machine || !profile) return FALSE;
1375 if (delete) return DeleteFileW( profile );
1380 /******************************************************************************
1381 * OpenColorProfileA [MSCMS.@]
1383 * See OpenColorProfileW.
1385 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1387 HPROFILE handle = NULL;
1389 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1391 if (!profile || !profile->pProfileData) return NULL;
1393 /* No AW conversion needed for memory based profiles */
1394 if (profile->dwType & PROFILE_MEMBUFFER)
1395 return OpenColorProfileW( profile, access, sharing, creation );
1397 if (profile->dwType & PROFILE_FILENAME)
1402 profileW.dwType = profile->dwType;
1404 len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
1405 profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1407 if (profileW.pProfileData)
1409 profileW.cbDataSize = len * sizeof(WCHAR);
1410 MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
1412 handle = OpenColorProfileW( &profileW, access, sharing, creation );
1413 HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
1419 /******************************************************************************
1420 * OpenColorProfileW [MSCMS.@]
1422 * Open a color profile.
1425 * profile [I] Pointer to a color profile structure.
1426 * access [I] Desired access.
1427 * sharing [I] Sharing mode.
1428 * creation [I] Creation mode.
1431 * Success: Handle to the opened profile.
1435 * Values for access: PROFILE_READ or PROFILE_READWRITE.
1436 * Values for sharing: 0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
1437 * Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
1438 * OPEN_ALWAYS, TRUNCATE_EXISTING.
1439 * Sharing and creation flags are ignored for memory based profiles.
1441 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1444 cmsHPROFILE cmsprofile = NULL;
1445 icProfile *iccprofile = NULL;
1446 HANDLE handle = INVALID_HANDLE_VALUE;
1448 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1450 if (!profile || !profile->pProfileData) return NULL;
1452 if (profile->dwType == PROFILE_MEMBUFFER)
1454 /* FIXME: access flags not implemented for memory based profiles */
1456 if (!(iccprofile = HeapAlloc( GetProcessHeap(), 0, profile->cbDataSize ))) return NULL;
1457 memcpy( iccprofile, profile->pProfileData, profile->cbDataSize );
1459 cmsprofile = cmsOpenProfileFromMem( iccprofile, profile->cbDataSize );
1461 else if (profile->dwType == PROFILE_FILENAME)
1463 DWORD size, read, flags = 0;
1465 TRACE( "profile file: %s\n", debugstr_w( profile->pProfileData ) );
1467 if (access & PROFILE_READ) flags = GENERIC_READ;
1468 if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
1470 if (!flags) return NULL;
1471 if (!sharing) sharing = FILE_SHARE_READ;
1473 handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
1474 if (handle == INVALID_HANDLE_VALUE)
1476 WARN( "Unable to open color profile %u\n", GetLastError() );
1480 if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
1482 ERR( "Unable to retrieve size of color profile\n" );
1483 CloseHandle( handle );
1487 iccprofile = HeapAlloc( GetProcessHeap(), 0, size );
1490 ERR( "Unable to allocate memory for color profile\n" );
1491 CloseHandle( handle );
1495 if (!ReadFile( handle, iccprofile, size, &read, NULL ) || read != size)
1497 ERR( "Unable to read color profile\n" );
1499 CloseHandle( handle );
1500 HeapFree( GetProcessHeap(), 0, iccprofile );
1504 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
1508 ERR( "Invalid profile type %u\n", profile->dwType );
1514 struct profile profile;
1516 profile.file = handle;
1517 profile.access = access;
1518 profile.iccprofile = iccprofile;
1519 profile.cmsprofile = cmsprofile;
1521 return create_profile( &profile );
1524 #endif /* HAVE_LCMS */
1528 /******************************************************************************
1529 * CloseColorProfile [MSCMS.@]
1531 * Close a color profile.
1534 * profile [I] Handle to the profile.
1540 BOOL WINAPI CloseColorProfile( HPROFILE profile )
1545 TRACE( "( %p )\n", profile );
1546 ret = close_profile( profile );
1548 #endif /* HAVE_LCMS */