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 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
49 /******************************************************************************
50 * GetColorDirectoryA [MSCMS.@]
52 * See GetColorDirectoryW.
54 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
61 TRACE( "( %p, %p )\n", buffer, size );
63 if (machine || !size) return FALSE;
67 ret = GetColorDirectoryW( NULL, NULL, &sizeW );
68 *size = sizeW / sizeof(WCHAR);
72 sizeW = *size * sizeof(WCHAR);
74 bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
78 ret = GetColorDirectoryW( NULL, bufferW, &sizeW );
79 *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
83 len = WideCharToMultiByte( CP_ACP, 0, bufferW, *size, buffer, *size, NULL, NULL );
84 if (!len) ret = FALSE;
87 HeapFree( GetProcessHeap(), 0, bufferW );
92 /******************************************************************************
93 * GetColorDirectoryW [MSCMS.@]
95 * Get the directory where color profiles are stored.
98 * machine [I] Name of the machine for which to get the color directory.
99 * Must be NULL, which indicates the local machine.
100 * buffer [I] Buffer to receive the path name.
101 * size [I/O] Size of the buffer in bytes. On return the variable holds
102 * the number of bytes actually needed.
104 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
106 WCHAR colordir[MAX_PATH];
107 static const WCHAR colorsubdir[] = { '\\','c','o','l','o','r',0 };
110 TRACE( "( %p, %p )\n", buffer, size );
112 if (machine || !size) return FALSE;
114 GetSystemDirectoryW( colordir, sizeof(colordir) / sizeof(WCHAR) );
115 lstrcatW( colordir, colorsubdir );
117 len = lstrlenW( colordir ) * sizeof(WCHAR);
119 if (len <= *size && buffer)
121 lstrcpyW( buffer, colordir );
130 /******************************************************************************
131 * GetColorProfileElement [MSCMS.@]
133 * Retrieve data for a specified tag type.
136 * profile [I] Handle to a color profile.
137 * type [I] ICC tag type.
138 * offset [I] Offset in bytes to start copying from.
139 * size [I/O] Size of the buffer in bytes. On return the variable holds
140 * the number of bytes actually needed.
141 * buffer [O] Buffer to receive the tag data.
142 * ref [O] Pointer to a BOOL that specifies whether more than one tag
143 * references the data.
149 BOOL WINAPI GetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
150 PVOID buffer, PBOOL ref )
154 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
158 TRACE( "( %p, 0x%08lx, %ld, %p, %p, %p )\n", profile, type, offset, size, buffer, ref );
160 if (!iccprofile || !size || !ref) return FALSE;
161 count = MSCMS_get_tag_count( iccprofile );
163 for (i = 0; i < count; i++)
165 MSCMS_get_tag_by_index( iccprofile, i, &tag );
169 if ((tag.size - offset) > *size || !buffer)
171 *size = (tag.size - offset);
175 MSCMS_get_tag_data( iccprofile, &tag, offset, buffer );
177 *ref = FALSE; /* FIXME: calculate properly */
182 #endif /* HAVE_LCMS_H */
186 /******************************************************************************
187 * GetColorProfileElementTag [MSCMS.@]
189 * Get the tag type from a color profile by index.
192 * profile [I] Handle to a color profile.
193 * index [I] Index into the tag table of the color profile.
194 * type [O] Pointer to a variable that holds the ICC tag type on return.
201 * The tag table index starts at 1.
202 * Use GetCountColorProfileElements to retrieve a count of tagged elements.
204 BOOL WINAPI GetColorProfileElementTag( HPROFILE profile, DWORD index, PTAGTYPE type )
208 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
212 TRACE( "( %p, %ld, %p )\n", profile, index, type );
214 if (!iccprofile || !type) return FALSE;
216 count = MSCMS_get_tag_count( iccprofile );
217 if (index > count || index < 1) return FALSE;
219 MSCMS_get_tag_by_index( iccprofile, index - 1, &tag );
224 #endif /* HAVE_LCMS_H */
228 /******************************************************************************
229 * GetColorProfileFromHandle [MSCMS.@]
231 * Retrieve an ICC color profile by handle.
234 * profile [I] Handle to a color profile.
235 * buffer [O] Buffer to receive the ICC profile.
236 * size [I/O] Size of the buffer in bytes. On return the variable holds the
237 * number of bytes actually needed.
244 * The profile returned will be in big-endian format.
246 BOOL WINAPI GetColorProfileFromHandle( HPROFILE profile, PBYTE buffer, PDWORD size )
250 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
251 PROFILEHEADER header;
253 TRACE( "( %p, %p, %p )\n", profile, buffer, size );
255 if (!iccprofile || !size) return FALSE;
256 MSCMS_get_profile_header( iccprofile, &header );
258 if (!buffer || header.phSize > *size)
260 *size = header.phSize;
264 /* No endian conversion needed */
265 memcpy( buffer, iccprofile, header.phSize );
267 *size = header.phSize;
270 #endif /* HAVE_LCMS_H */
274 /******************************************************************************
275 * GetColorProfileHeader [MSCMS.@]
277 * Retrieve a color profile header by handle.
280 * profile [I] Handle to a color profile.
281 * header [O] Buffer to receive the ICC profile header.
288 * The profile header returned will be adjusted for endianess.
290 BOOL WINAPI GetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
294 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
296 TRACE( "( %p, %p )\n", profile, header );
298 if (!iccprofile || !header) return FALSE;
300 MSCMS_get_profile_header( iccprofile, header );
303 #endif /* HAVE_LCMS_H */
307 /******************************************************************************
308 * GetCountColorProfileElements [MSCMS.@]
310 * Retrieve the number of elements in a color profile.
313 * profile [I] Handle to a color profile.
314 * count [O] Pointer to a variable which is set to the number of elements
315 * in the color profile.
321 BOOL WINAPI GetCountColorProfileElements( HPROFILE profile, PDWORD count )
325 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
327 TRACE( "( %p, %p )\n", profile, count );
329 if (!iccprofile || !count) return FALSE;
330 *count = MSCMS_get_tag_count( iccprofile );
333 #endif /* HAVE_LCMS_H */
337 /******************************************************************************
338 * GetStandardColorSpaceProfileA [MSCMS.@]
340 * See GetStandardColorSpaceProfileW.
342 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
349 TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
351 if (machine || !size) return FALSE;
353 sizeW = *size * sizeof(WCHAR);
357 ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
358 *size = sizeW / sizeof(WCHAR);
362 profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
366 ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW );
367 *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
371 len = WideCharToMultiByte( CP_ACP, 0, profileW, *size, profile, *size, NULL, NULL );
372 if (!len) ret = FALSE;
375 HeapFree( GetProcessHeap(), 0, profileW );
380 /******************************************************************************
381 * GetStandardColorSpaceProfileW [MSCMS.@]
383 * Retrieve the profile filename for a given standard color space id.
386 * machine [I] Name of the machine for which to get the standard color space.
387 * Must be NULL, which indicates the local machine.
388 * id [I] Id of a standard color space.
389 * profile [O] Buffer to receive the profile filename.
390 * size [I/O] Size of the filename buffer in bytes.
396 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
398 static const WCHAR rgbprofilefile[] =
399 { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
400 's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
401 WCHAR rgbprofile[MAX_PATH];
402 DWORD len = sizeof(rgbprofile);
404 TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
406 if (machine || !size) return FALSE;
407 GetColorDirectoryW( machine, rgbprofile, &len );
411 case 0x52474220: /* 'RGB ' */
412 lstrcatW( rgbprofile, rgbprofilefile );
413 len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
415 if (*size < len || !profile)
421 lstrcpyW( profile, rgbprofile );
430 /******************************************************************************
431 * InstallColorProfileA [MSCMS.@]
433 * See InstallColorProfileW.
435 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
441 TRACE( "( %s )\n", debugstr_a(profile) );
443 if (machine || !profile) return FALSE;
445 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
446 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
450 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
452 ret = InstallColorProfileW( NULL, profileW );
453 HeapFree( GetProcessHeap(), 0, profileW );
458 /******************************************************************************
459 * InstallColorProfileW [MSCMS.@]
461 * Install a color profile.
464 * machine [I] Name of the machine to install the profile on. Must be NULL,
465 * which indicates the local machine.
466 * profile [I] Full path name of the profile to install.
472 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
474 WCHAR dest[MAX_PATH], base[MAX_PATH];
475 DWORD size = sizeof(dest);
476 static const WCHAR slash[] = { '\\', 0 };
478 TRACE( "( %s )\n", debugstr_w(profile) );
480 if (machine || !profile) return FALSE;
482 if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
484 MSCMS_basename( profile, base );
486 lstrcatW( dest, slash );
487 lstrcatW( dest, base );
489 /* Is source equal to destination? */
490 if (!lstrcmpW( profile, dest )) return TRUE;
492 return CopyFileW( profile, dest, TRUE );
495 /******************************************************************************
496 * IsColorProfileTagPresent [MSCMS.@]
498 * Determine if a given ICC tag type is present in a color profile.
501 * profile [I] Color profile handle.
502 * tag [I] ICC tag type.
503 * present [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
510 BOOL WINAPI IsColorProfileTagPresent( HPROFILE profile, TAGTYPE type, PBOOL present )
514 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
518 TRACE( "( %p, 0x%08lx, %p )\n", profile, type, present );
520 if (!iccprofile || !present) return FALSE;
522 count = MSCMS_get_tag_count( iccprofile );
524 for (i = 0; i < count; i++)
526 MSCMS_get_tag_by_index( iccprofile, i, &tag );
530 *present = ret = TRUE;
535 #endif /* HAVE_LCMS_H */
539 /******************************************************************************
540 * IsColorProfileValid [MSCMS.@]
542 * Determine if a given color profile is valid.
545 * profile [I] Color profile handle.
546 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
553 BOOL WINAPI IsColorProfileValid( HPROFILE profile, PBOOL valid )
557 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
559 TRACE( "( %p, %p )\n", profile, valid );
561 if (!valid) return FALSE;
562 if (iccprofile) return *valid = TRUE;
564 #endif /* HAVE_LCMS_H */
568 /******************************************************************************
569 * SetColorProfileElement [MSCMS.@]
571 * Set data for a specified tag type.
574 * profile [I] Handle to a color profile.
575 * type [I] ICC tag type.
576 * offset [I] Offset in bytes to start copying to.
577 * size [I/O] Size of the buffer in bytes. On return the variable holds the
578 * number of bytes actually needed.
579 * buffer [O] Buffer holding the tag data.
585 BOOL WINAPI SetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
590 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
591 DWORD i, count, access = MSCMS_hprofile2access( profile );
594 TRACE( "( %p, 0x%08lx, %ld, %p, %p )\n", profile, type, offset, size, buffer );
596 if (!iccprofile || !size || !buffer) return FALSE;
597 if (!(access & PROFILE_READWRITE)) return FALSE;
599 count = MSCMS_get_tag_count( iccprofile );
601 for (i = 0; i < count; i++)
603 MSCMS_get_tag_by_index( iccprofile, i, &tag );
607 if (offset > tag.size) return FALSE;
609 MSCMS_set_tag_data( iccprofile, &tag, offset, buffer );
614 #endif /* HAVE_LCMS_H */
618 /******************************************************************************
619 * SetColorProfileHeader [MSCMS.@]
621 * Set header data for a given profile.
624 * profile [I] Handle to a color profile.
625 * header [I] Buffer holding the header data.
631 BOOL WINAPI SetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
635 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
636 DWORD access = MSCMS_hprofile2access( profile );
638 TRACE( "( %p, %p )\n", profile, header );
640 if (!iccprofile || !header) return FALSE;
641 if (!(access & PROFILE_READWRITE)) return FALSE;
643 MSCMS_set_profile_header( iccprofile, header );
646 #endif /* HAVE_LCMS_H */
650 /******************************************************************************
651 * UninstallColorProfileA [MSCMS.@]
653 * See UninstallColorProfileW.
655 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
661 TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
663 if (machine || !profile) return FALSE;
665 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
666 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
670 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
672 ret = UninstallColorProfileW( NULL, profileW , delete );
674 HeapFree( GetProcessHeap(), 0, profileW );
679 /******************************************************************************
680 * UninstallColorProfileW [MSCMS.@]
682 * Uninstall a color profile.
685 * machine [I] Name of the machine to uninstall the profile on. Must be NULL,
686 * which indicates the local machine.
687 * profile [I] Full path name of the profile to uninstall.
688 * delete [I] Bool that specifies whether the profile file should be deleted.
694 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
696 TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
698 if (machine || !profile) return FALSE;
700 if (delete) return DeleteFileW( profile );
705 /******************************************************************************
706 * OpenColorProfileA [MSCMS.@]
708 * See OpenColorProfileW.
710 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
712 HPROFILE handle = NULL;
714 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
716 if (!profile || !profile->pProfileData) return NULL;
718 /* No AW conversion needed for memory based profiles */
719 if (profile->dwType & PROFILE_MEMBUFFER)
720 return OpenColorProfileW( profile, access, sharing, creation );
722 if (profile->dwType & PROFILE_FILENAME)
727 profileW.dwType = profile->dwType;
729 len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
730 profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
732 if (profileW.pProfileData)
734 profileW.cbDataSize = len * sizeof(WCHAR);
735 MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
737 handle = OpenColorProfileW( &profileW, access, sharing, creation );
738 HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
744 /******************************************************************************
745 * OpenColorProfileW [MSCMS.@]
747 * Open a color profile.
750 * profile [I] Pointer to a color profile structure.
751 * access [I] Desired access.
752 * sharing [I] Sharing mode.
753 * creation [I] Creation mode.
756 * Success: Handle to the opened profile.
760 * Values for access: PROFILE_READ or PROFILE_READWRITE.
761 * Values for sharing: 0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
762 * Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
763 * OPEN_ALWAYS, TRUNCATE_EXISTING.
764 * Sharing and creation flags are ignored for memory based profiles.
766 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
769 cmsHPROFILE cmsprofile = NULL;
770 icProfile *iccprofile = NULL;
771 HANDLE handle = NULL;
774 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
776 if (!profile || !profile->pProfileData) return NULL;
778 if (profile->dwType & PROFILE_MEMBUFFER)
780 FIXME( "access flags not implemented for memory based profiles\n" );
782 iccprofile = profile->pProfileData;
783 size = profile->cbDataSize;
785 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
788 if (profile->dwType & PROFILE_FILENAME)
790 DWORD read, flags = 0;
792 TRACE( "profile file: %s\n", debugstr_w( (WCHAR *)profile->pProfileData ) );
794 if (access & PROFILE_READ) flags = GENERIC_READ;
795 if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
797 if (!flags) return NULL;
799 handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
800 if (handle == INVALID_HANDLE_VALUE)
802 WARN( "Unable to open color profile\n" );
806 if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
808 ERR( "Unable to retrieve size of color profile\n" );
809 CloseHandle( handle );
813 iccprofile = HeapAlloc( GetProcessHeap(), 0, size );
816 ERR( "Unable to allocate memory for color profile\n" );
817 CloseHandle( handle );
821 if (!ReadFile( handle, iccprofile, size, &read, NULL ) || read != size)
823 ERR( "Unable to read color profile\n" );
825 CloseHandle( handle );
826 HeapFree( GetProcessHeap, 0, iccprofile );
830 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
834 return MSCMS_create_hprofile_handle( handle, iccprofile, cmsprofile, access );
836 #endif /* HAVE_LCMS_H */
840 /******************************************************************************
841 * CloseColorProfile [MSCMS.@]
843 * Close a color profile.
846 * profile [I] Handle to the profile.
852 BOOL WINAPI CloseColorProfile( HPROFILE profile )
856 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
857 HANDLE file = MSCMS_hprofile2handle( profile );
858 DWORD access = MSCMS_hprofile2access( profile );
860 TRACE( "( %p )\n", profile );
862 if (file && (access & PROFILE_READWRITE))
864 DWORD written, size = MSCMS_get_profile_size( iccprofile );
866 if (SetFilePointer( file, 0, NULL, FILE_BEGIN ) ||
867 !WriteFile( file, iccprofile, size, &written, NULL ) || written != size)
868 ERR( "Unable to write color profile\n" );
871 ret = cmsCloseProfile( MSCMS_hprofile2cmsprofile( profile ) );
872 HeapFree( GetProcessHeap(), 0, MSCMS_hprofile2iccprofile( profile ) );
874 CloseHandle( MSCMS_hprofile2handle( profile ) );
875 MSCMS_destroy_hprofile_handle( profile );
877 #endif /* HAVE_LCMS_H */