Modify VarDiv() and VarSub() to use the existing VarDecSub() and
[wine/wine64.git] / dlls / mscms / profile.c
blobfba677da95c098e8d65ee96e244e254dcf67dee9
1 /*
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
21 #include "config.h"
22 #include "wine/debug.h"
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "icm.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 )
54 INT len;
55 LPWSTR bufferW;
56 BOOL ret = FALSE;
57 DWORD sizeW;
59 TRACE( "( %p, %p )\n", buffer, size );
61 if (machine || !size) return FALSE;
63 if (!buffer)
65 ret = GetColorDirectoryW( NULL, NULL, &sizeW );
66 *size = sizeW / sizeof(WCHAR);
67 return FALSE;
70 sizeW = *size * sizeof(WCHAR);
72 bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
74 if (bufferW)
76 ret = GetColorDirectoryW( NULL, bufferW, &sizeW );
77 *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
79 if (ret)
81 len = WideCharToMultiByte( CP_ACP, 0, bufferW, *size, buffer, *size, NULL, NULL );
82 if (!len) ret = FALSE;
85 HeapFree( GetProcessHeap(), 0, bufferW );
87 return ret;
90 /******************************************************************************
91 * GetColorDirectoryW [MSCMS.@]
93 * Get the directory where color profiles are stored.
95 * PARAMS
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 };
106 DWORD len;
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 );
120 *size = len;
121 return TRUE;
124 *size = len;
125 return FALSE;
128 /******************************************************************************
129 * GetColorProfileElement [MSCMS.@]
131 * Retrieve data for a specified tag type.
133 * PARAMS
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.
143 * RETURNS
144 * Success: TRUE
145 * Failure: FALSE
147 BOOL WINAPI GetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
148 PVOID buffer, PBOOL ref )
150 BOOL ret = FALSE;
151 #ifdef HAVE_LCMS
152 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
153 DWORD i, count;
154 icTag tag;
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 );
165 if (tag.sig == type)
167 if ((tag.size - offset) > *size || !buffer)
169 *size = (tag.size - offset);
170 return FALSE;
173 MSCMS_get_tag_data( iccprofile, &tag, offset, buffer );
175 *ref = FALSE; /* FIXME: calculate properly */
176 return TRUE;
180 #endif /* HAVE_LCMS */
181 return ret;
184 /******************************************************************************
185 * GetColorProfileElementTag [MSCMS.@]
187 * Get the tag type from a color profile by index.
189 * PARAMS
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.
194 * RETURNS
195 * Success: TRUE
196 * Failure: FALSE
198 * NOTES
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 )
204 BOOL ret = FALSE;
205 #ifdef HAVE_LCMS
206 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
207 DWORD count;
208 icTag tag;
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 );
218 *type = tag.sig;
220 ret = TRUE;
222 #endif /* HAVE_LCMS */
223 return ret;
226 /******************************************************************************
227 * GetColorProfileFromHandle [MSCMS.@]
229 * Retrieve an ICC color profile by handle.
231 * PARAMS
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.
237 * RETURNS
238 * Success: TRUE
239 * Failure: FALSE
241 * NOTES
242 * The profile returned will be in big-endian format.
244 BOOL WINAPI GetColorProfileFromHandle( HPROFILE profile, PBYTE buffer, PDWORD size )
246 BOOL ret = FALSE;
247 #ifdef HAVE_LCMS
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;
259 return FALSE;
262 /* No endian conversion needed */
263 memcpy( buffer, iccprofile, header.phSize );
265 *size = header.phSize;
266 ret = TRUE;
268 #endif /* HAVE_LCMS */
269 return ret;
272 /******************************************************************************
273 * GetColorProfileHeader [MSCMS.@]
275 * Retrieve a color profile header by handle.
277 * PARAMS
278 * profile [I] Handle to a color profile.
279 * header [O] Buffer to receive the ICC profile header.
281 * RETURNS
282 * Success: TRUE
283 * Failure: FALSE
285 * NOTES
286 * The profile header returned will be adjusted for endianess.
288 BOOL WINAPI GetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
290 BOOL ret = FALSE;
291 #ifdef HAVE_LCMS
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 );
299 return TRUE;
301 #endif /* HAVE_LCMS */
302 return ret;
305 /******************************************************************************
306 * GetCountColorProfileElements [MSCMS.@]
308 * Retrieve the number of elements in a color profile.
310 * PARAMS
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.
315 * RETURNS
316 * Success: TRUE
317 * Failure: FALSE
319 BOOL WINAPI GetCountColorProfileElements( HPROFILE profile, PDWORD count )
321 BOOL ret = FALSE;
322 #ifdef HAVE_LCMS
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 );
329 ret = TRUE;
331 #endif /* HAVE_LCMS */
332 return ret;
335 /******************************************************************************
336 * GetStandardColorSpaceProfileA [MSCMS.@]
338 * See GetStandardColorSpaceProfileW.
340 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
342 INT len;
343 LPWSTR profileW;
344 BOOL ret = FALSE;
345 DWORD sizeW;
347 TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
349 if (machine || !size) return FALSE;
351 sizeW = *size * sizeof(WCHAR);
353 if (!profile)
355 ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
356 *size = sizeW / sizeof(WCHAR);
357 return FALSE;
360 profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
362 if (profileW)
364 ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW );
365 *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
367 if (ret)
369 len = WideCharToMultiByte( CP_ACP, 0, profileW, *size, profile, *size, NULL, NULL );
370 if (!len) ret = FALSE;
373 HeapFree( GetProcessHeap(), 0, profileW );
375 return ret;
378 /******************************************************************************
379 * GetStandardColorSpaceProfileW [MSCMS.@]
381 * Retrieve the profile filename for a given standard color space id.
383 * PARAMS
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.
390 * RETURNS
391 * Success: TRUE
392 * Failure: FALSE
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 );
407 switch (id)
409 case 0x52474220: /* 'RGB ' */
410 lstrcatW( rgbprofile, rgbprofilefile );
411 len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
413 if (*size < len || !profile)
415 *size = len;
416 return TRUE;
419 lstrcpyW( profile, rgbprofile );
420 break;
422 default:
423 return FALSE;
425 return TRUE;
428 /******************************************************************************
429 * InstallColorProfileA [MSCMS.@]
431 * See InstallColorProfileW.
433 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
435 UINT len;
436 LPWSTR profileW;
437 BOOL ret = FALSE;
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) );
446 if (profileW)
448 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
450 ret = InstallColorProfileW( NULL, profileW );
451 HeapFree( GetProcessHeap(), 0, profileW );
453 return ret;
456 /******************************************************************************
457 * InstallColorProfileW [MSCMS.@]
459 * Install a color profile.
461 * PARAMS
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.
466 * RETURNS
467 * Success: TRUE
468 * Failure: FALSE
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.
498 * PARAMS
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,
502 * FALSE otherwise.
504 * RETURNS
505 * Success: TRUE
506 * Failure: FALSE
508 BOOL WINAPI IsColorProfileTagPresent( HPROFILE profile, TAGTYPE type, PBOOL present )
510 BOOL ret = FALSE;
511 #ifdef HAVE_LCMS
512 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
513 DWORD i, count;
514 icTag tag;
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 );
526 if (tag.sig == type)
528 *present = ret = TRUE;
529 break;
533 #endif /* HAVE_LCMS */
534 return ret;
537 /******************************************************************************
538 * IsColorProfileValid [MSCMS.@]
540 * Determine if a given color profile is valid.
542 * PARAMS
543 * profile [I] Color profile handle.
544 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
545 * FALSE otherwise.
547 * RETURNS
548 * Success: TRUE
549 * Failure: FALSE
551 BOOL WINAPI IsColorProfileValid( HPROFILE profile, PBOOL valid )
553 BOOL ret = FALSE;
554 #ifdef HAVE_LCMS
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 */
563 return ret;
566 /******************************************************************************
567 * SetColorProfileElement [MSCMS.@]
569 * Set data for a specified tag type.
571 * PARAMS
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.
579 * RETURNS
580 * Success: TRUE
581 * Failure: FALSE
583 BOOL WINAPI SetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
584 PVOID buffer )
586 BOOL ret = FALSE;
587 #ifdef HAVE_LCMS
588 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
589 DWORD i, count, access = MSCMS_hprofile2access( profile );
590 icTag tag;
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 );
603 if (tag.sig == type)
605 if (offset > tag.size) return FALSE;
607 MSCMS_set_tag_data( iccprofile, &tag, offset, buffer );
608 return TRUE;
612 #endif /* HAVE_LCMS */
613 return ret;
616 /******************************************************************************
617 * SetColorProfileHeader [MSCMS.@]
619 * Set header data for a given profile.
621 * PARAMS
622 * profile [I] Handle to a color profile.
623 * header [I] Buffer holding the header data.
625 * RETURNS
626 * Success: TRUE
627 * Failure: FALSE
629 BOOL WINAPI SetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
631 BOOL ret = FALSE;
632 #ifdef HAVE_LCMS
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 );
642 return TRUE;
644 #endif /* HAVE_LCMS */
645 return ret;
648 /******************************************************************************
649 * UninstallColorProfileA [MSCMS.@]
651 * See UninstallColorProfileW.
653 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
655 UINT len;
656 LPWSTR profileW;
657 BOOL ret = FALSE;
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) );
666 if (profileW)
668 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
670 ret = UninstallColorProfileW( NULL, profileW , delete );
672 HeapFree( GetProcessHeap(), 0, profileW );
674 return ret;
677 /******************************************************************************
678 * UninstallColorProfileW [MSCMS.@]
680 * Uninstall a color profile.
682 * PARAMS
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.
688 * RETURNS
689 * Success: TRUE
690 * Failure: FALSE
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 );
700 return TRUE;
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)
722 UINT len;
723 PROFILE profileW;
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 );
739 return handle;
742 /******************************************************************************
743 * OpenColorProfileW [MSCMS.@]
745 * Open a color profile.
747 * PARAMS
748 * profile [I] Pointer to a color profile structure.
749 * access [I] Desired access.
750 * sharing [I] Sharing mode.
751 * creation [I] Creation mode.
753 * RETURNS
754 * Success: Handle to the opened profile.
755 * Failure: NULL
757 * NOTES
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 )
766 #ifdef HAVE_LCMS
767 cmsHPROFILE cmsprofile = NULL;
768 icProfile *iccprofile = NULL;
769 HANDLE handle = NULL;
770 DWORD size;
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" );
801 return NULL;
804 if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
806 ERR( "Unable to retrieve size of color profile\n" );
807 CloseHandle( handle );
808 return NULL;
811 iccprofile = HeapAlloc( GetProcessHeap(), 0, size );
812 if (!iccprofile)
814 ERR( "Unable to allocate memory for color profile\n" );
815 CloseHandle( handle );
816 return NULL;
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 );
825 return NULL;
828 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
831 if (cmsprofile)
832 return MSCMS_create_hprofile_handle( handle, iccprofile, cmsprofile, access );
834 #endif /* HAVE_LCMS */
835 return NULL;
838 /******************************************************************************
839 * CloseColorProfile [MSCMS.@]
841 * Close a color profile.
843 * PARAMS
844 * profile [I] Handle to the profile.
846 * RETURNS
847 * Success: TRUE
848 * Failure: FALSE
850 BOOL WINAPI CloseColorProfile( HPROFILE profile )
852 BOOL ret = FALSE;
853 #ifdef HAVE_LCMS
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 */
876 return ret;