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
);
349 if (machine
|| !size
) return FALSE
;
351 sizeW
= *size
* sizeof(WCHAR
);
355 ret
= GetStandardColorSpaceProfileW( NULL
, id
, NULL
, &sizeW
);
356 *size
= sizeW
/ sizeof(WCHAR
);
360 profileW
= HeapAlloc( GetProcessHeap(), 0, sizeW
);
364 ret
= GetStandardColorSpaceProfileW( NULL
, id
, profileW
, &sizeW
);
365 *size
= WideCharToMultiByte( CP_ACP
, 0, profileW
, -1, NULL
, 0, NULL
, NULL
);
369 len
= WideCharToMultiByte( CP_ACP
, 0, profileW
, *size
, profile
, *size
, NULL
, NULL
);
370 if (!len
) ret
= FALSE
;
373 HeapFree( GetProcessHeap(), 0, profileW
);
378 /******************************************************************************
379 * GetStandardColorSpaceProfileW [MSCMS.@]
381 * Retrieve the profile filename for a given standard color space id.
384 * machine [I] Name of the machine for which to get the standard color space.
385 * Must be NULL, which indicates the local machine.
386 * id [I] Id of a standard color space.
387 * profile [O] Buffer to receive the profile filename.
388 * size [I/O] Size of the filename buffer in bytes.
394 BOOL WINAPI
GetStandardColorSpaceProfileW( PCWSTR machine
, DWORD id
, PWSTR profile
, PDWORD size
)
396 static const WCHAR rgbprofilefile
[] =
397 { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
398 's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
399 WCHAR rgbprofile
[MAX_PATH
];
400 DWORD len
= sizeof(rgbprofile
);
402 TRACE( "( 0x%08lx, %p, %p )\n", id
, profile
, size
);
404 if (machine
|| !size
) return FALSE
;
405 GetColorDirectoryW( machine
, rgbprofile
, &len
);
409 case 0x52474220: /* 'RGB ' */
410 lstrcatW( rgbprofile
, rgbprofilefile
);
411 len
= lstrlenW( rgbprofile
) * sizeof(WCHAR
);
413 if (*size
< len
|| !profile
)
419 lstrcpyW( profile
, rgbprofile
);
428 /******************************************************************************
429 * InstallColorProfileA [MSCMS.@]
431 * See InstallColorProfileW.
433 BOOL WINAPI
InstallColorProfileA( PCSTR machine
, PCSTR profile
)
439 TRACE( "( %s )\n", debugstr_a(profile
) );
441 if (machine
|| !profile
) return FALSE
;
443 len
= MultiByteToWideChar( CP_ACP
, 0, profile
, -1, NULL
, 0 );
444 profileW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
448 MultiByteToWideChar( CP_ACP
, 0, profile
, -1, profileW
, len
);
450 ret
= InstallColorProfileW( NULL
, profileW
);
451 HeapFree( GetProcessHeap(), 0, profileW
);
456 /******************************************************************************
457 * InstallColorProfileW [MSCMS.@]
459 * Install a color profile.
462 * machine [I] Name of the machine to install the profile on. Must be NULL,
463 * which indicates the local machine.
464 * profile [I] Full path name of the profile to install.
470 BOOL WINAPI
InstallColorProfileW( PCWSTR machine
, PCWSTR profile
)
472 WCHAR dest
[MAX_PATH
], base
[MAX_PATH
];
473 DWORD size
= sizeof(dest
);
474 static const WCHAR slash
[] = { '\\', 0 };
476 TRACE( "( %s )\n", debugstr_w(profile
) );
478 if (machine
|| !profile
) return FALSE
;
480 if (!GetColorDirectoryW( machine
, dest
, &size
)) return FALSE
;
482 MSCMS_basename( profile
, base
);
484 lstrcatW( dest
, slash
);
485 lstrcatW( dest
, base
);
487 /* Is source equal to destination? */
488 if (!lstrcmpW( profile
, dest
)) return TRUE
;
490 return CopyFileW( profile
, dest
, TRUE
);
493 /******************************************************************************
494 * IsColorProfileTagPresent [MSCMS.@]
496 * Determine if a given ICC tag type is present in a color profile.
499 * profile [I] Color profile handle.
500 * tag [I] ICC tag type.
501 * present [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
508 BOOL WINAPI
IsColorProfileTagPresent( HPROFILE profile
, TAGTYPE type
, PBOOL present
)
512 icProfile
*iccprofile
= MSCMS_hprofile2iccprofile( profile
);
516 TRACE( "( %p, 0x%08lx, %p )\n", profile
, type
, present
);
518 if (!iccprofile
|| !present
) return FALSE
;
520 count
= MSCMS_get_tag_count( iccprofile
);
522 for (i
= 0; i
< count
; i
++)
524 MSCMS_get_tag_by_index( iccprofile
, i
, &tag
);
528 *present
= ret
= TRUE
;
533 #endif /* HAVE_LCMS */
537 /******************************************************************************
538 * IsColorProfileValid [MSCMS.@]
540 * Determine if a given color profile is valid.
543 * profile [I] Color profile handle.
544 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
551 BOOL WINAPI
IsColorProfileValid( HPROFILE profile
, PBOOL valid
)
555 icProfile
*iccprofile
= MSCMS_hprofile2iccprofile( profile
);
557 TRACE( "( %p, %p )\n", profile
, valid
);
559 if (!valid
) return FALSE
;
560 if (iccprofile
) return *valid
= TRUE
;
562 #endif /* HAVE_LCMS */
566 /******************************************************************************
567 * SetColorProfileElement [MSCMS.@]
569 * Set data for a specified tag type.
572 * profile [I] Handle to a color profile.
573 * type [I] ICC tag type.
574 * offset [I] Offset in bytes to start copying to.
575 * size [I/O] Size of the buffer in bytes. On return the variable holds the
576 * number of bytes actually needed.
577 * buffer [O] Buffer holding the tag data.
583 BOOL WINAPI
SetColorProfileElement( HPROFILE profile
, TAGTYPE type
, DWORD offset
, PDWORD size
,
588 icProfile
*iccprofile
= MSCMS_hprofile2iccprofile( profile
);
589 DWORD i
, count
, access
= MSCMS_hprofile2access( profile
);
592 TRACE( "( %p, 0x%08lx, %ld, %p, %p )\n", profile
, type
, offset
, size
, buffer
);
594 if (!iccprofile
|| !size
|| !buffer
) return FALSE
;
595 if (!(access
& PROFILE_READWRITE
)) return FALSE
;
597 count
= MSCMS_get_tag_count( iccprofile
);
599 for (i
= 0; i
< count
; i
++)
601 MSCMS_get_tag_by_index( iccprofile
, i
, &tag
);
605 if (offset
> tag
.size
) return FALSE
;
607 MSCMS_set_tag_data( iccprofile
, &tag
, offset
, buffer
);
612 #endif /* HAVE_LCMS */
616 /******************************************************************************
617 * SetColorProfileHeader [MSCMS.@]
619 * Set header data for a given profile.
622 * profile [I] Handle to a color profile.
623 * header [I] Buffer holding the header data.
629 BOOL WINAPI
SetColorProfileHeader( HPROFILE profile
, PPROFILEHEADER header
)
633 icProfile
*iccprofile
= MSCMS_hprofile2iccprofile( profile
);
634 DWORD access
= MSCMS_hprofile2access( profile
);
636 TRACE( "( %p, %p )\n", profile
, header
);
638 if (!iccprofile
|| !header
) return FALSE
;
639 if (!(access
& PROFILE_READWRITE
)) return FALSE
;
641 MSCMS_set_profile_header( iccprofile
, header
);
644 #endif /* HAVE_LCMS */
648 /******************************************************************************
649 * UninstallColorProfileA [MSCMS.@]
651 * See UninstallColorProfileW.
653 BOOL WINAPI
UninstallColorProfileA( PCSTR machine
, PCSTR profile
, BOOL
delete )
659 TRACE( "( %s, %x )\n", debugstr_a(profile
), delete );
661 if (machine
|| !profile
) return FALSE
;
663 len
= MultiByteToWideChar( CP_ACP
, 0, profile
, -1, NULL
, 0 );
664 profileW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
668 MultiByteToWideChar( CP_ACP
, 0, profile
, -1, profileW
, len
);
670 ret
= UninstallColorProfileW( NULL
, profileW
, delete );
672 HeapFree( GetProcessHeap(), 0, profileW
);
677 /******************************************************************************
678 * UninstallColorProfileW [MSCMS.@]
680 * Uninstall a color profile.
683 * machine [I] Name of the machine to uninstall the profile on. Must be NULL,
684 * which indicates the local machine.
685 * profile [I] Full path name of the profile to uninstall.
686 * delete [I] Bool that specifies whether the profile file should be deleted.
692 BOOL WINAPI
UninstallColorProfileW( PCWSTR machine
, PCWSTR profile
, BOOL
delete )
694 TRACE( "( %s, %x )\n", debugstr_w(profile
), delete );
696 if (machine
|| !profile
) return FALSE
;
698 if (delete) return DeleteFileW( profile
);
703 /******************************************************************************
704 * OpenColorProfileA [MSCMS.@]
706 * See OpenColorProfileW.
708 HPROFILE WINAPI
OpenColorProfileA( PPROFILE profile
, DWORD access
, DWORD sharing
, DWORD creation
)
710 HPROFILE handle
= NULL
;
712 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile
, access
, sharing
, creation
);
714 if (!profile
|| !profile
->pProfileData
) return NULL
;
716 /* No AW conversion needed for memory based profiles */
717 if (profile
->dwType
& PROFILE_MEMBUFFER
)
718 return OpenColorProfileW( profile
, access
, sharing
, creation
);
720 if (profile
->dwType
& PROFILE_FILENAME
)
725 profileW
.dwType
= profile
->dwType
;
727 len
= MultiByteToWideChar( CP_ACP
, 0, profile
->pProfileData
, -1, NULL
, 0 );
728 profileW
.pProfileData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
730 if (profileW
.pProfileData
)
732 profileW
.cbDataSize
= len
* sizeof(WCHAR
);
733 MultiByteToWideChar( CP_ACP
, 0, profile
->pProfileData
, -1, profileW
.pProfileData
, len
);
735 handle
= OpenColorProfileW( &profileW
, access
, sharing
, creation
);
736 HeapFree( GetProcessHeap(), 0, profileW
.pProfileData
);
742 /******************************************************************************
743 * OpenColorProfileW [MSCMS.@]
745 * Open a color profile.
748 * profile [I] Pointer to a color profile structure.
749 * access [I] Desired access.
750 * sharing [I] Sharing mode.
751 * creation [I] Creation mode.
754 * Success: Handle to the opened profile.
758 * Values for access: PROFILE_READ or PROFILE_READWRITE.
759 * Values for sharing: 0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
760 * Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
761 * OPEN_ALWAYS, TRUNCATE_EXISTING.
762 * Sharing and creation flags are ignored for memory based profiles.
764 HPROFILE WINAPI
OpenColorProfileW( PPROFILE profile
, DWORD access
, DWORD sharing
, DWORD creation
)
767 cmsHPROFILE cmsprofile
= NULL
;
768 icProfile
*iccprofile
= NULL
;
769 HANDLE handle
= NULL
;
772 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile
, access
, sharing
, creation
);
774 if (!profile
|| !profile
->pProfileData
) return NULL
;
776 if (profile
->dwType
& PROFILE_MEMBUFFER
)
778 FIXME( "access flags not implemented for memory based profiles\n" );
780 iccprofile
= profile
->pProfileData
;
781 size
= profile
->cbDataSize
;
783 cmsprofile
= cmsOpenProfileFromMem( iccprofile
, size
);
786 if (profile
->dwType
& PROFILE_FILENAME
)
788 DWORD read
, flags
= 0;
790 TRACE( "profile file: %s\n", debugstr_w( (WCHAR
*)profile
->pProfileData
) );
792 if (access
& PROFILE_READ
) flags
= GENERIC_READ
;
793 if (access
& PROFILE_READWRITE
) flags
= GENERIC_READ
|GENERIC_WRITE
;
795 if (!flags
) return NULL
;
797 handle
= CreateFileW( profile
->pProfileData
, flags
, sharing
, NULL
, creation
, 0, NULL
);
798 if (handle
== INVALID_HANDLE_VALUE
)
800 WARN( "Unable to open color profile\n" );
804 if ((size
= GetFileSize( handle
, NULL
)) == INVALID_FILE_SIZE
)
806 ERR( "Unable to retrieve size of color profile\n" );
807 CloseHandle( handle
);
811 iccprofile
= HeapAlloc( GetProcessHeap(), 0, size
);
814 ERR( "Unable to allocate memory for color profile\n" );
815 CloseHandle( handle
);
819 if (!ReadFile( handle
, iccprofile
, size
, &read
, NULL
) || read
!= size
)
821 ERR( "Unable to read color profile\n" );
823 CloseHandle( handle
);
824 HeapFree( GetProcessHeap
, 0, iccprofile
);
828 cmsprofile
= cmsOpenProfileFromMem( iccprofile
, size
);
832 return MSCMS_create_hprofile_handle( handle
, iccprofile
, cmsprofile
, access
);
834 #endif /* HAVE_LCMS */
838 /******************************************************************************
839 * CloseColorProfile [MSCMS.@]
841 * Close a color profile.
844 * profile [I] Handle to the profile.
850 BOOL WINAPI
CloseColorProfile( HPROFILE profile
)
854 icProfile
*iccprofile
= MSCMS_hprofile2iccprofile( profile
);
855 HANDLE file
= MSCMS_hprofile2handle( profile
);
856 DWORD access
= MSCMS_hprofile2access( profile
);
858 TRACE( "( %p )\n", profile
);
860 if (file
&& (access
& PROFILE_READWRITE
))
862 DWORD written
, size
= MSCMS_get_profile_size( iccprofile
);
864 if (SetFilePointer( file
, 0, NULL
, FILE_BEGIN
) ||
865 !WriteFile( file
, iccprofile
, size
, &written
, NULL
) || written
!= size
)
866 ERR( "Unable to write color profile\n" );
869 ret
= cmsCloseProfile( MSCMS_hprofile2cmsprofile( profile
) );
870 HeapFree( GetProcessHeap(), 0, MSCMS_hprofile2iccprofile( profile
) );
872 CloseHandle( MSCMS_hprofile2handle( profile
) );
873 MSCMS_destroy_hprofile_handle( profile
);
875 #endif /* HAVE_LCMS */