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 #include "mscms_priv.h"
35 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
37 static void MSCMS_basename( LPCWSTR path, LPWSTR name )
39 INT i = lstrlenW( path );
41 while (i > 0 && !IS_SEPARATOR(path[i - 1])) i--;
42 lstrcpyW( name, &path[i] );
45 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
47 /******************************************************************************
48 * GetColorDirectoryA [MSCMS.@]
50 * See GetColorDirectoryW.
52 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
59 TRACE( "( %p, %p )\n", buffer, size );
61 if (machine || !size) return FALSE;
65 ret = GetColorDirectoryW( NULL, NULL, &sizeW );
66 *size = sizeW / sizeof(WCHAR);
70 sizeW = *size * sizeof(WCHAR);
72 bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
76 ret = GetColorDirectoryW( NULL, bufferW, &sizeW );
77 *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
81 len = WideCharToMultiByte( CP_ACP, 0, bufferW, *size, buffer, *size, NULL, NULL );
82 if (!len) ret = FALSE;
85 HeapFree( GetProcessHeap(), 0, bufferW );
90 /******************************************************************************
91 * GetColorDirectoryW [MSCMS.@]
93 * Get the directory where color profiles are stored.
96 * machine [I] Name of the machine for which to get the color directory.
97 * Must be NULL, which indicates the local machine.
98 * buffer [I] Buffer to receive the path name.
99 * size [I/O] Size of the buffer in bytes. On return the variable holds
100 * the number of bytes actually needed.
102 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
104 WCHAR colordir[MAX_PATH];
105 static const WCHAR colorsubdir[] = { '\\','c','o','l','o','r',0 };
108 TRACE( "( %p, %p )\n", buffer, size );
110 if (machine || !size) return FALSE;
112 GetSystemDirectoryW( colordir, sizeof(colordir) / sizeof(WCHAR) );
113 lstrcatW( colordir, colorsubdir );
115 len = lstrlenW( colordir ) * sizeof(WCHAR);
117 if (len <= *size && buffer)
119 lstrcpyW( buffer, colordir );
128 /******************************************************************************
129 * GetColorProfileElement [MSCMS.@]
131 * Retrieve data for a specified tag type.
134 * profile [I] Handle to a color profile.
135 * type [I] ICC tag type.
136 * offset [I] Offset in bytes to start copying from.
137 * size [I/O] Size of the buffer in bytes. On return the variable holds
138 * the number of bytes actually needed.
139 * buffer [O] Buffer to receive the tag data.
140 * ref [O] Pointer to a BOOL that specifies whether more than one tag
141 * references the data.
147 BOOL WINAPI GetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
148 PVOID buffer, PBOOL ref )
152 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
156 TRACE( "( %p, 0x%08lx, %ld, %p, %p, %p )\n", profile, type, offset, size, buffer, ref );
158 if (!iccprofile || !size || !ref) return FALSE;
159 count = MSCMS_get_tag_count( iccprofile );
161 for (i = 0; i < count; i++)
163 MSCMS_get_tag_by_index( iccprofile, i, &tag );
167 if ((tag.size - offset) > *size || !buffer)
169 *size = (tag.size - offset);
173 MSCMS_get_tag_data( iccprofile, &tag, offset, buffer );
175 *ref = FALSE; /* FIXME: calculate properly */
180 #endif /* HAVE_LCMS */
184 /******************************************************************************
185 * GetColorProfileElementTag [MSCMS.@]
187 * Get the tag type from a color profile by index.
190 * profile [I] Handle to a color profile.
191 * index [I] Index into the tag table of the color profile.
192 * type [O] Pointer to a variable that holds the ICC tag type on return.
199 * The tag table index starts at 1.
200 * Use GetCountColorProfileElements to retrieve a count of tagged elements.
202 BOOL WINAPI GetColorProfileElementTag( HPROFILE profile, DWORD index, PTAGTYPE type )
206 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
210 TRACE( "( %p, %ld, %p )\n", profile, index, type );
212 if (!iccprofile || !type) return FALSE;
214 count = MSCMS_get_tag_count( iccprofile );
215 if (index > count || index < 1) return FALSE;
217 MSCMS_get_tag_by_index( iccprofile, index - 1, &tag );
222 #endif /* HAVE_LCMS */
226 /******************************************************************************
227 * GetColorProfileFromHandle [MSCMS.@]
229 * Retrieve an ICC color profile by handle.
232 * profile [I] Handle to a color profile.
233 * buffer [O] Buffer to receive the ICC profile.
234 * size [I/O] Size of the buffer in bytes. On return the variable holds the
235 * number of bytes actually needed.
242 * The profile returned will be in big-endian format.
244 BOOL WINAPI GetColorProfileFromHandle( HPROFILE profile, PBYTE buffer, PDWORD size )
248 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
249 PROFILEHEADER header;
251 TRACE( "( %p, %p, %p )\n", profile, buffer, size );
253 if (!iccprofile || !size) return FALSE;
254 MSCMS_get_profile_header( iccprofile, &header );
256 if (!buffer || header.phSize > *size)
258 *size = header.phSize;
262 /* No endian conversion needed */
263 memcpy( buffer, iccprofile, header.phSize );
265 *size = header.phSize;
268 #endif /* HAVE_LCMS */
272 /******************************************************************************
273 * GetColorProfileHeader [MSCMS.@]
275 * Retrieve a color profile header by handle.
278 * profile [I] Handle to a color profile.
279 * header [O] Buffer to receive the ICC profile header.
286 * The profile header returned will be adjusted for endianess.
288 BOOL WINAPI GetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
292 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
294 TRACE( "( %p, %p )\n", profile, header );
296 if (!iccprofile || !header) return FALSE;
298 MSCMS_get_profile_header( iccprofile, header );
301 #endif /* HAVE_LCMS */
305 /******************************************************************************
306 * GetCountColorProfileElements [MSCMS.@]
308 * Retrieve the number of elements in a color profile.
311 * profile [I] Handle to a color profile.
312 * count [O] Pointer to a variable which is set to the number of elements
313 * in the color profile.
319 BOOL WINAPI GetCountColorProfileElements( HPROFILE profile, PDWORD count )
323 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
325 TRACE( "( %p, %p )\n", profile, count );
327 if (!iccprofile || !count) return FALSE;
328 *count = MSCMS_get_tag_count( iccprofile );
331 #endif /* HAVE_LCMS */
335 /******************************************************************************
336 * GetStandardColorSpaceProfileA [MSCMS.@]
338 * See GetStandardColorSpaceProfileW.
340 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
347 TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
351 SetLastError(ERROR_NOT_SUPPORTED);
357 SetLastError(ERROR_INSUFFICIENT_BUFFER);
361 sizeW = *size * sizeof(WCHAR);
365 ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
366 *size = sizeW / sizeof(WCHAR);
370 profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
374 ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW );
375 *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
379 len = WideCharToMultiByte( CP_ACP, 0, profileW, *size, profile, *size, NULL, NULL );
380 if (!len) ret = FALSE;
383 HeapFree( GetProcessHeap(), 0, profileW );
388 /******************************************************************************
389 * GetStandardColorSpaceProfileW [MSCMS.@]
391 * Retrieve the profile filename for a given standard color space id.
394 * machine [I] Name of the machine for which to get the standard color space.
395 * Must be NULL, which indicates the local machine.
396 * id [I] Id of a standard color space.
397 * profile [O] Buffer to receive the profile filename.
398 * size [I/O] Size of the filename buffer in bytes.
404 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
406 static const WCHAR rgbprofilefile[] =
407 { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
408 's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
409 WCHAR rgbprofile[MAX_PATH];
410 DWORD len = sizeof(rgbprofile);
412 TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
416 SetLastError(ERROR_NOT_SUPPORTED);
422 SetLastError(ERROR_INSUFFICIENT_BUFFER);
426 GetColorDirectoryW( machine, rgbprofile, &len );
430 case 0x52474220: /* 'RGB ' */
431 lstrcatW( rgbprofile, rgbprofilefile );
432 len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
434 if (*size < len || !profile)
440 lstrcpyW( profile, rgbprofile );
449 /******************************************************************************
450 * InstallColorProfileA [MSCMS.@]
452 * See InstallColorProfileW.
454 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
460 TRACE( "( %s )\n", debugstr_a(profile) );
462 if (machine || !profile) return FALSE;
464 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
465 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
469 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
471 ret = InstallColorProfileW( NULL, profileW );
472 HeapFree( GetProcessHeap(), 0, profileW );
477 /******************************************************************************
478 * InstallColorProfileW [MSCMS.@]
480 * Install a color profile.
483 * machine [I] Name of the machine to install the profile on. Must be NULL,
484 * which indicates the local machine.
485 * profile [I] Full path name of the profile to install.
491 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
493 WCHAR dest[MAX_PATH], base[MAX_PATH];
494 DWORD size = sizeof(dest);
495 static const WCHAR slash[] = { '\\', 0 };
497 TRACE( "( %s )\n", debugstr_w(profile) );
499 if (machine || !profile) return FALSE;
501 if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
503 MSCMS_basename( profile, base );
505 lstrcatW( dest, slash );
506 lstrcatW( dest, base );
508 /* Is source equal to destination? */
509 if (!lstrcmpW( profile, dest )) return TRUE;
511 return CopyFileW( profile, dest, TRUE );
514 /******************************************************************************
515 * IsColorProfileTagPresent [MSCMS.@]
517 * Determine if a given ICC tag type is present in a color profile.
520 * profile [I] Color profile handle.
521 * tag [I] ICC tag type.
522 * present [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
529 BOOL WINAPI IsColorProfileTagPresent( HPROFILE profile, TAGTYPE type, PBOOL present )
533 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
537 TRACE( "( %p, 0x%08lx, %p )\n", profile, type, present );
539 if (!iccprofile || !present) return FALSE;
541 count = MSCMS_get_tag_count( iccprofile );
543 for (i = 0; i < count; i++)
545 MSCMS_get_tag_by_index( iccprofile, i, &tag );
549 *present = ret = TRUE;
554 #endif /* HAVE_LCMS */
558 /******************************************************************************
559 * IsColorProfileValid [MSCMS.@]
561 * Determine if a given color profile is valid.
564 * profile [I] Color profile handle.
565 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
572 BOOL WINAPI IsColorProfileValid( HPROFILE profile, PBOOL valid )
576 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
578 TRACE( "( %p, %p )\n", profile, valid );
580 if (!valid) return FALSE;
581 if (iccprofile) return *valid = TRUE;
583 #endif /* HAVE_LCMS */
587 /******************************************************************************
588 * SetColorProfileElement [MSCMS.@]
590 * Set data for a specified tag type.
593 * profile [I] Handle to a color profile.
594 * type [I] ICC tag type.
595 * offset [I] Offset in bytes to start copying to.
596 * size [I/O] Size of the buffer in bytes. On return the variable holds the
597 * number of bytes actually needed.
598 * buffer [O] Buffer holding the tag data.
604 BOOL WINAPI SetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
609 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
610 DWORD i, count, access = MSCMS_hprofile2access( profile );
613 TRACE( "( %p, 0x%08lx, %ld, %p, %p )\n", profile, type, offset, size, buffer );
615 if (!iccprofile || !size || !buffer) return FALSE;
616 if (!(access & PROFILE_READWRITE)) return FALSE;
618 count = MSCMS_get_tag_count( iccprofile );
620 for (i = 0; i < count; i++)
622 MSCMS_get_tag_by_index( iccprofile, i, &tag );
626 if (offset > tag.size) return FALSE;
628 MSCMS_set_tag_data( iccprofile, &tag, offset, buffer );
633 #endif /* HAVE_LCMS */
637 /******************************************************************************
638 * SetColorProfileHeader [MSCMS.@]
640 * Set header data for a given profile.
643 * profile [I] Handle to a color profile.
644 * header [I] Buffer holding the header data.
650 BOOL WINAPI SetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
654 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
655 DWORD access = MSCMS_hprofile2access( profile );
657 TRACE( "( %p, %p )\n", profile, header );
659 if (!iccprofile || !header) return FALSE;
660 if (!(access & PROFILE_READWRITE)) return FALSE;
662 MSCMS_set_profile_header( iccprofile, header );
665 #endif /* HAVE_LCMS */
669 /******************************************************************************
670 * UninstallColorProfileA [MSCMS.@]
672 * See UninstallColorProfileW.
674 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
680 TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
682 if (machine || !profile) return FALSE;
684 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
685 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
689 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
691 ret = UninstallColorProfileW( NULL, profileW , delete );
693 HeapFree( GetProcessHeap(), 0, profileW );
698 /******************************************************************************
699 * UninstallColorProfileW [MSCMS.@]
701 * Uninstall a color profile.
704 * machine [I] Name of the machine to uninstall the profile on. Must be NULL,
705 * which indicates the local machine.
706 * profile [I] Full path name of the profile to uninstall.
707 * delete [I] Bool that specifies whether the profile file should be deleted.
713 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
715 TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
717 if (machine || !profile) return FALSE;
719 if (delete) return DeleteFileW( profile );
724 /******************************************************************************
725 * OpenColorProfileA [MSCMS.@]
727 * See OpenColorProfileW.
729 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
731 HPROFILE handle = NULL;
733 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
735 if (!profile || !profile->pProfileData) return NULL;
737 /* No AW conversion needed for memory based profiles */
738 if (profile->dwType & PROFILE_MEMBUFFER)
739 return OpenColorProfileW( profile, access, sharing, creation );
741 if (profile->dwType & PROFILE_FILENAME)
746 profileW.dwType = profile->dwType;
748 len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
749 profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
751 if (profileW.pProfileData)
753 profileW.cbDataSize = len * sizeof(WCHAR);
754 MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
756 handle = OpenColorProfileW( &profileW, access, sharing, creation );
757 HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
763 /******************************************************************************
764 * OpenColorProfileW [MSCMS.@]
766 * Open a color profile.
769 * profile [I] Pointer to a color profile structure.
770 * access [I] Desired access.
771 * sharing [I] Sharing mode.
772 * creation [I] Creation mode.
775 * Success: Handle to the opened profile.
779 * Values for access: PROFILE_READ or PROFILE_READWRITE.
780 * Values for sharing: 0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
781 * Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
782 * OPEN_ALWAYS, TRUNCATE_EXISTING.
783 * Sharing and creation flags are ignored for memory based profiles.
785 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
788 cmsHPROFILE cmsprofile = NULL;
789 icProfile *iccprofile = NULL;
790 HANDLE handle = NULL;
793 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
795 if (!profile || !profile->pProfileData) return NULL;
797 if (profile->dwType & PROFILE_MEMBUFFER)
799 /* FIXME: access flags not implemented for memory based profiles */
801 iccprofile = profile->pProfileData;
802 size = profile->cbDataSize;
804 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
807 if (profile->dwType & PROFILE_FILENAME)
809 DWORD read, flags = 0;
811 TRACE( "profile file: %s\n", debugstr_w( (WCHAR *)profile->pProfileData ) );
813 if (access & PROFILE_READ) flags = GENERIC_READ;
814 if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
816 if (!flags) return NULL;
818 handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
819 if (handle == INVALID_HANDLE_VALUE)
821 WARN( "Unable to open color profile\n" );
825 if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
827 ERR( "Unable to retrieve size of color profile\n" );
828 CloseHandle( handle );
832 iccprofile = HeapAlloc( GetProcessHeap(), 0, size );
835 ERR( "Unable to allocate memory for color profile\n" );
836 CloseHandle( handle );
840 if (!ReadFile( handle, iccprofile, size, &read, NULL ) || read != size)
842 ERR( "Unable to read color profile\n" );
844 CloseHandle( handle );
845 HeapFree( GetProcessHeap, 0, iccprofile );
849 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
853 return MSCMS_create_hprofile_handle( handle, iccprofile, cmsprofile, access );
855 #endif /* HAVE_LCMS */
859 /******************************************************************************
860 * CloseColorProfile [MSCMS.@]
862 * Close a color profile.
865 * profile [I] Handle to the profile.
871 BOOL WINAPI CloseColorProfile( HPROFILE profile )
875 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
876 HANDLE file = MSCMS_hprofile2handle( profile );
877 DWORD access = MSCMS_hprofile2access( profile );
879 TRACE( "( %p )\n", profile );
881 if (file && (access & PROFILE_READWRITE))
883 DWORD written, size = MSCMS_get_profile_size( iccprofile );
885 if (SetFilePointer( file, 0, NULL, FILE_BEGIN ) ||
886 !WriteFile( file, iccprofile, size, &written, NULL ) || written != size)
887 ERR( "Unable to write color profile\n" );
890 ret = cmsCloseProfile( MSCMS_hprofile2cmsprofile( profile ) );
891 HeapFree( GetProcessHeap(), 0, MSCMS_hprofile2iccprofile( profile ) );
893 CloseHandle( MSCMS_hprofile2handle( profile ) );
894 MSCMS_destroy_hprofile_handle( profile );
896 #endif /* HAVE_LCMS */