2 * MSCMS - Color Management System for Wine
4 * Copyright 2004, 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/debug.h"
33 #define LCMS_API_FUNCTION(f) extern typeof(f) * p##f;
35 #undef LCMS_API_FUNCTION
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 const WCHAR rgbprofile[] =
48 { 'c',':','\\','w','i','n','d','o','w','s','\\', 's','y','s','t','e','m','3','2',
49 '\\','s','p','o','o','l','\\','d','r','i','v','e','r','s',
50 '\\','c','o','l','o','r','\\','s','r','g','b',' ','c','o','l','o','r',' ',
51 's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
53 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
55 /******************************************************************************
56 * GetColorDirectoryA [MSCMS.@]
58 * See GetColorDirectoryW.
60 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
67 TRACE( "( %p, %p )\n", buffer, size );
69 if (machine || !size) return FALSE;
73 ret = GetColorDirectoryW( NULL, NULL, &sizeW );
74 *size = sizeW / sizeof(WCHAR);
78 sizeW = *size * sizeof(WCHAR);
80 bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
84 ret = GetColorDirectoryW( NULL, bufferW, &sizeW );
85 *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
89 len = WideCharToMultiByte( CP_ACP, 0, bufferW, *size, buffer, *size, NULL, NULL );
90 if (!len) ret = FALSE;
93 HeapFree( GetProcessHeap(), 0, bufferW );
98 /******************************************************************************
99 * GetColorDirectoryW [MSCMS.@]
101 * Get the directory where color profiles are stored.
104 * machine [I] Name of the machine for which to get the color directory.
105 * Must be NULL, which indicates the local machine.
106 * buffer [I] Buffer to receive the path name.
107 * size [I/O] Size of the buffer in bytes. On return the variable holds
108 * the number of bytes actually needed.
110 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
112 /* FIXME: Get this directory from the registry? */
113 static const WCHAR colordir[] =
114 { 'c',':','\\','w','i','n','d','o','w','s','\\', 's','y','s','t','e','m','3','2',
115 '\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r',0 };
119 TRACE( "( %p, %p )\n", buffer, size );
121 if (machine || !size) return FALSE;
123 len = lstrlenW( colordir ) * sizeof(WCHAR);
125 if (len <= *size && buffer)
127 lstrcpyW( buffer, colordir );
136 /******************************************************************************
137 * GetColorProfileElement [MSCMS.@]
139 * Retrieve data for a specified tag type.
142 * profile [I] Handle to a color profile.
143 * type [I] ICC tag type.
144 * offset [I] Offset in bytes to start copying from.
145 * size [I/O] Size of the buffer in bytes. On return the variable holds
146 * the number of bytes actually needed.
147 * buffer [O] Buffer to receive the tag data.
148 * ref [O] Pointer to a BOOL that specifies whether more than one tag
149 * references the data.
155 BOOL WINAPI GetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
156 PVOID buffer, PBOOL ref )
160 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
164 TRACE( "( %p, 0x%08lx, %ld, %p, %p, %p )\n", profile, type, offset, size, buffer, ref );
166 if (!iccprofile || !size || !ref) return FALSE;
167 count = MSCMS_get_tag_count( iccprofile );
169 for (i = 0; i < count; i++)
171 MSCMS_get_tag_by_index( iccprofile, i, &tag );
175 if ((tag.size - offset) > *size || !buffer)
177 *size = (tag.size - offset);
181 MSCMS_get_tag_data( iccprofile, &tag, offset, buffer );
183 *ref = FALSE; /* FIXME: calculate properly */
188 #endif /* HAVE_LCMS_H */
192 /******************************************************************************
193 * GetColorProfileElementTag [MSCMS.@]
195 * Get the tag type from a color profile by index.
198 * profile [I] Handle to a color profile.
199 * index [I] Index into the tag table of the color profile.
200 * type [O] Pointer to a variable that holds the ICC tag type on return.
207 * The tag table index starts at 1.
208 * Use GetCountColorProfileElements to retrieve a count of tagged elements.
210 BOOL WINAPI GetColorProfileElementTag( HPROFILE profile, DWORD index, PTAGTYPE type )
214 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
218 TRACE( "( %p, %ld, %p )\n", profile, index, type );
220 if (!iccprofile || !type) return FALSE;
222 count = MSCMS_get_tag_count( iccprofile );
223 if (index > count || index < 1) return FALSE;
225 MSCMS_get_tag_by_index( iccprofile, index - 1, &tag );
230 #endif /* HAVE_LCMS_H */
234 /******************************************************************************
235 * GetColorProfileFromHandle [MSCMS.@]
237 * Retrieve an ICC color profile by handle.
240 * profile [I] Handle to a color profile.
241 * buffer [O] Buffer to receive the ICC profile.
242 * size [I/O] Size of the buffer in bytes. On return the variable holds the
243 * number of bytes actually needed.
250 * The profile returned will be in big-endian format.
252 BOOL WINAPI GetColorProfileFromHandle( HPROFILE profile, PBYTE buffer, PDWORD size )
256 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
257 PROFILEHEADER header;
259 TRACE( "( %p, %p, %p )\n", profile, buffer, size );
261 if (!iccprofile || !size) return FALSE;
262 MSCMS_get_profile_header( iccprofile, &header );
264 if (!buffer || header.phSize > *size)
266 *size = header.phSize;
270 /* No endian conversion needed */
271 memcpy( buffer, iccprofile, header.phSize );
273 *size = header.phSize;
276 #endif /* HAVE_LCMS_H */
280 /******************************************************************************
281 * GetColorProfileHeader [MSCMS.@]
283 * Retrieve a color profile header by handle.
286 * profile [I] Handle to a color profile.
287 * header [O] Buffer to receive the ICC profile header.
294 * The profile header returned will be adjusted for endianess.
296 BOOL WINAPI GetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
300 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
302 TRACE( "( %p, %p )\n", profile, header );
304 if (!iccprofile || !header) return FALSE;
306 MSCMS_get_profile_header( iccprofile, header );
309 #endif /* HAVE_LCMS_H */
313 /******************************************************************************
314 * GetCountColorProfileElements [MSCMS.@]
316 * Retrieve the number of elements in a color profile.
319 * profile [I] Handle to a color profile.
320 * count [O] Pointer to a variable which is set to the number of elements
321 * in the color profile.
327 BOOL WINAPI GetCountColorProfileElements( HPROFILE profile, PDWORD count )
331 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
333 TRACE( "( %p, %p )\n", profile, count );
335 if (!iccprofile || !count) return FALSE;
336 *count = MSCMS_get_tag_count( iccprofile );
339 #endif /* HAVE_LCMS_H */
343 /******************************************************************************
344 * GetStandardColorSpaceProfileA [MSCMS.@]
346 * See GetStandardColorSpaceProfileW.
348 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
355 TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
357 if (machine || !size) return FALSE;
359 sizeW = *size * sizeof(WCHAR);
363 ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
364 *size = sizeW / sizeof(WCHAR);
368 profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
372 ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW );
373 *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
377 len = WideCharToMultiByte( CP_ACP, 0, profileW, *size, profile, *size, NULL, NULL );
378 if (!len) ret = FALSE;
381 HeapFree( GetProcessHeap(), 0, profileW );
386 /******************************************************************************
387 * GetStandardColorSpaceProfileW [MSCMS.@]
389 * Retrieve the profile filename for a given standard color space id.
392 * machine [I] Name of the machine for which to get the standard color space.
393 * Must be NULL, which indicates the local machine.
394 * id [I] Id of a standard color space.
395 * profile [O] Buffer to receive the profile filename.
396 * size [I/O] Size of the filename buffer in bytes.
402 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
406 TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
408 if (machine || !size) return FALSE;
412 case 0x52474220: /* 'RGB ' */
413 len = sizeof( rgbprofile );
415 if (*size < len || !profile)
421 lstrcpyW( profile, rgbprofile );
431 /******************************************************************************
432 * InstallColorProfileA [MSCMS.@]
434 * See InstallColorProfileW.
436 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
442 TRACE( "( %s )\n", debugstr_a(profile) );
444 if (machine || !profile) return FALSE;
446 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
447 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
451 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
453 ret = InstallColorProfileW( NULL, profileW );
454 HeapFree( GetProcessHeap(), 0, profileW );
459 /******************************************************************************
460 * InstallColorProfileW [MSCMS.@]
462 * Install a color profile.
465 * machine [I] Name of the machine to install the profile on. Must be NULL,
466 * which indicates the local machine.
467 * profile [I] Full path name of the profile to install.
473 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
475 WCHAR dest[MAX_PATH], base[MAX_PATH];
476 DWORD size = sizeof(dest);
477 static const WCHAR slash[] = { '\\', 0 };
479 TRACE( "( %s )\n", debugstr_w(profile) );
481 if (machine || !profile) return FALSE;
483 if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
485 MSCMS_basename( profile, base );
487 lstrcatW( dest, slash );
488 lstrcatW( dest, base );
490 /* Is source equal to destination? */
491 if (!lstrcmpW( profile, dest )) return TRUE;
493 return CopyFileW( profile, dest, TRUE );
496 /******************************************************************************
497 * IsColorProfileTagPresent [MSCMS.@]
499 * Determine if a given ICC tag type is present in a color profile.
502 * profile [I] Color profile handle.
503 * tag [I] ICC tag type.
504 * present [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
511 BOOL WINAPI IsColorProfileTagPresent( HPROFILE profile, TAGTYPE type, PBOOL present )
515 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
519 TRACE( "( %p, 0x%08lx, %p )\n", profile, type, present );
521 if (!iccprofile || !present) return FALSE;
523 count = MSCMS_get_tag_count( iccprofile );
525 for (i = 0; i < count; i++)
527 MSCMS_get_tag_by_index( iccprofile, i, &tag );
531 *present = ret = TRUE;
536 #endif /* HAVE_LCMS_H */
540 /******************************************************************************
541 * IsColorProfileValid [MSCMS.@]
543 * Determine if a given color profile is valid.
546 * profile [I] Color profile handle.
547 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
554 BOOL WINAPI IsColorProfileValid( HPROFILE profile, PBOOL valid )
558 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
560 TRACE( "( %p, %p )\n", profile, valid );
562 if (!valid) return FALSE;
563 if (iccprofile) return *valid = TRUE;
565 #endif /* HAVE_LCMS_H */
569 /******************************************************************************
570 * SetColorProfileElement [MSCMS.@]
572 * Set data for a specified tag type.
575 * profile [I] Handle to a color profile.
576 * type [I] ICC tag type.
577 * offset [I] Offset in bytes to start copying to.
578 * size [I/O] Size of the buffer in bytes. On return the variable holds the
579 * number of bytes actually needed.
580 * buffer [O] Buffer holding the tag data.
586 BOOL WINAPI SetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
591 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
592 DWORD i, count, access = MSCMS_hprofile2access( profile );
595 TRACE( "( %p, 0x%08lx, %ld, %p, %p )\n", profile, type, offset, size, buffer );
597 if (!iccprofile || !size || !buffer) return FALSE;
598 if (!(access & PROFILE_READWRITE)) return FALSE;
600 count = MSCMS_get_tag_count( iccprofile );
602 for (i = 0; i < count; i++)
604 MSCMS_get_tag_by_index( iccprofile, i, &tag );
608 if (offset > tag.size) return FALSE;
610 MSCMS_set_tag_data( iccprofile, &tag, offset, buffer );
615 #endif /* HAVE_LCMS_H */
619 /******************************************************************************
620 * SetColorProfileHeader [MSCMS.@]
622 * Set header data for a given profile.
625 * profile [I] Handle to a color profile.
626 * header [I] Buffer holding the header data.
632 BOOL WINAPI SetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
636 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
637 DWORD access = MSCMS_hprofile2access( profile );
639 TRACE( "( %p, %p )\n", profile, header );
641 if (!iccprofile || !header) return FALSE;
642 if (!(access & PROFILE_READWRITE)) return FALSE;
644 MSCMS_set_profile_header( iccprofile, header );
647 #endif /* HAVE_LCMS_H */
651 /******************************************************************************
652 * UninstallColorProfileA [MSCMS.@]
654 * See UninstallColorProfileW.
656 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
662 TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
664 if (machine || !profile) return FALSE;
666 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
667 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
671 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
673 ret = UninstallColorProfileW( NULL, profileW , delete );
675 HeapFree( GetProcessHeap(), 0, profileW );
680 /******************************************************************************
681 * UninstallColorProfileW [MSCMS.@]
683 * Uninstall a color profile.
686 * machine [I] Name of the machine to uninstall the profile on. Must be NULL,
687 * which indicates the local machine.
688 * profile [I] Full path name of the profile to uninstall.
689 * delete [I] Bool that specifies whether the profile file should be deleted.
695 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
697 TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
699 if (machine || !profile) return FALSE;
701 if (delete) return DeleteFileW( profile );
706 /******************************************************************************
707 * OpenColorProfileA [MSCMS.@]
709 * See OpenColorProfileW.
711 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
713 HPROFILE handle = NULL;
715 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
717 if (!profile || !profile->pProfileData) return NULL;
719 /* No AW conversion needed for memory based profiles */
720 if (profile->dwType & PROFILE_MEMBUFFER)
721 return OpenColorProfileW( profile, access, sharing, creation );
723 if (profile->dwType & PROFILE_FILENAME)
728 profileW.dwType = profile->dwType;
730 len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
731 profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
733 if (profileW.pProfileData)
735 profileW.cbDataSize = len * sizeof(WCHAR);
736 MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
738 handle = OpenColorProfileW( &profileW, access, sharing, creation );
739 HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
745 /******************************************************************************
746 * OpenColorProfileW [MSCMS.@]
748 * Open a color profile.
751 * profile [I] Pointer to a color profile structure.
752 * access [I] Desired access.
753 * sharing [I] Sharing mode.
754 * creation [I] Creation mode.
757 * Success: Handle to the opened profile.
761 * Values for access: PROFILE_READ or PROFILE_READWRITE.
762 * Values for sharing: 0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
763 * Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
764 * OPEN_ALWAYS, TRUNCATE_EXISTING.
765 * Sharing and creation flags are ignored for memory based profiles.
767 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
770 cmsHPROFILE cmsprofile = NULL;
771 icProfile *iccprofile = NULL;
772 HANDLE handle = NULL;
775 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
777 if (!profile || !profile->pProfileData) return NULL;
779 if (profile->dwType & PROFILE_MEMBUFFER)
781 FIXME( "access flags not implemented for memory based profiles\n" );
783 iccprofile = profile->pProfileData;
784 size = profile->cbDataSize;
786 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
789 if (profile->dwType & PROFILE_FILENAME)
791 DWORD read, flags = 0;
793 TRACE( "profile file: %s\n", debugstr_w( (WCHAR *)profile->pProfileData ) );
795 if (access & PROFILE_READ) flags = GENERIC_READ;
796 if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
798 if (!flags) return NULL;
800 handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
801 if (handle == INVALID_HANDLE_VALUE)
803 WARN( "Unable to open color profile\n" );
807 if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
809 ERR( "Unable to retrieve size of color profile\n" );
810 CloseHandle( handle );
814 iccprofile = HeapAlloc( GetProcessHeap(), 0, size );
817 ERR( "Unable to allocate memory for color profile\n" );
818 CloseHandle( handle );
822 if (!ReadFile( handle, iccprofile, size, &read, NULL ) || read != size)
824 ERR( "Unable to read color profile\n" );
826 CloseHandle( handle );
827 HeapFree( GetProcessHeap, 0, iccprofile );
831 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
835 return MSCMS_create_hprofile_handle( handle, iccprofile, cmsprofile, access );
837 #endif /* HAVE_LCMS_H */
841 /******************************************************************************
842 * CloseColorProfile [MSCMS.@]
844 * Close a color profile.
847 * profile [I] Handle to the profile.
853 BOOL WINAPI CloseColorProfile( HPROFILE profile )
857 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
858 HANDLE file = MSCMS_hprofile2handle( profile );
859 DWORD access = MSCMS_hprofile2access( profile );
861 TRACE( "( %p )\n", profile );
863 if (file && (access & PROFILE_READWRITE))
865 DWORD written, size = MSCMS_get_profile_size( iccprofile );
867 if (SetFilePointer( file, 0, NULL, FILE_BEGIN ) ||
868 !WriteFile( file, iccprofile, size, &written, NULL ) || written != size)
869 ERR( "Unable to write color profile\n" );
872 ret = cmsCloseProfile( MSCMS_hprofile2cmsprofile( profile ) );
873 HeapFree( GetProcessHeap(), 0, MSCMS_hprofile2iccprofile( profile ) );
875 CloseHandle( MSCMS_hprofile2handle( profile ) );
876 MSCMS_destroy_hprofile_handle( profile );
878 #endif /* HAVE_LCMS_H */