user: ChangeDisplaySettings virtual desktop mode handling fix.
[wine/dibdrv.git] / dlls / mscms / profile.c
blobb5761bf55fa9318c660b0e11cba0dee5dbafd2f5
1 /*
2 * MSCMS - Color Management System for Wine
4 * Copyright 2004, 2005, 2006 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 static inline LPWSTR MSCMS_strdupW( LPCSTR str )
47 LPWSTR ret = NULL;
48 if (str)
50 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
51 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
52 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
54 return ret;
57 static const char *MSCMS_dbgstr_tag( DWORD tag )
59 return wine_dbg_sprintf( "'%c%c%c%c'",
60 (char)(tag >> 24), (char)(tag >> 16), (char)(tag >> 8), (char)(tag) );
63 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
65 /******************************************************************************
66 * GetColorDirectoryA [MSCMS.@]
68 * See GetColorDirectoryW.
70 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
72 INT len;
73 LPWSTR bufferW;
74 BOOL ret = FALSE;
75 DWORD sizeW;
77 TRACE( "( %p, %p )\n", buffer, size );
79 if (machine || !size) return FALSE;
81 if (!buffer)
83 ret = GetColorDirectoryW( NULL, NULL, &sizeW );
84 *size = sizeW / sizeof(WCHAR);
85 return FALSE;
88 sizeW = *size * sizeof(WCHAR);
90 bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
92 if (bufferW)
94 ret = GetColorDirectoryW( NULL, bufferW, &sizeW );
95 *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
97 if (ret)
99 len = WideCharToMultiByte( CP_ACP, 0, bufferW, *size, buffer, *size, NULL, NULL );
100 if (!len) ret = FALSE;
103 HeapFree( GetProcessHeap(), 0, bufferW );
105 return ret;
108 /******************************************************************************
109 * GetColorDirectoryW [MSCMS.@]
111 * Get the directory where color profiles are stored.
113 * PARAMS
114 * machine [I] Name of the machine for which to get the color directory.
115 * Must be NULL, which indicates the local machine.
116 * buffer [I] Buffer to receive the path name.
117 * size [I/O] Size of the buffer in bytes. On return the variable holds
118 * the number of bytes actually needed.
120 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
122 WCHAR colordir[MAX_PATH];
123 static const WCHAR colorsubdir[] = { '\\','c','o','l','o','r',0 };
124 DWORD len;
126 TRACE( "( %p, %p )\n", buffer, size );
128 if (machine || !size) return FALSE;
130 GetSystemDirectoryW( colordir, sizeof(colordir) / sizeof(WCHAR) );
131 lstrcatW( colordir, colorsubdir );
133 len = lstrlenW( colordir ) * sizeof(WCHAR);
135 if (len <= *size && buffer)
137 lstrcpyW( buffer, colordir );
138 *size = len;
139 return TRUE;
142 *size = len;
143 return FALSE;
146 /******************************************************************************
147 * GetColorProfileElement [MSCMS.@]
149 * Retrieve data for a specified tag type.
151 * PARAMS
152 * profile [I] Handle to a color profile.
153 * type [I] ICC tag type.
154 * offset [I] Offset in bytes to start copying from.
155 * size [I/O] Size of the buffer in bytes. On return the variable holds
156 * the number of bytes actually needed.
157 * buffer [O] Buffer to receive the tag data.
158 * ref [O] Pointer to a BOOL that specifies whether more than one tag
159 * references the data.
161 * RETURNS
162 * Success: TRUE
163 * Failure: FALSE
165 BOOL WINAPI GetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
166 PVOID buffer, PBOOL ref )
168 BOOL ret = FALSE;
169 #ifdef HAVE_LCMS
170 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
171 DWORD i, count;
172 icTag tag;
174 TRACE( "( %p, 0x%08lx, %ld, %p, %p, %p )\n", profile, type, offset, size, buffer, ref );
176 if (!iccprofile || !size || !ref) return FALSE;
177 count = MSCMS_get_tag_count( iccprofile );
179 for (i = 0; i < count; i++)
181 MSCMS_get_tag_by_index( iccprofile, i, &tag );
183 if (tag.sig == type)
185 if ((tag.size - offset) > *size || !buffer)
187 *size = (tag.size - offset);
188 return FALSE;
191 MSCMS_get_tag_data( iccprofile, &tag, offset, buffer );
193 *ref = FALSE; /* FIXME: calculate properly */
194 return TRUE;
198 #endif /* HAVE_LCMS */
199 return ret;
202 /******************************************************************************
203 * GetColorProfileElementTag [MSCMS.@]
205 * Get the tag type from a color profile by index.
207 * PARAMS
208 * profile [I] Handle to a color profile.
209 * index [I] Index into the tag table of the color profile.
210 * type [O] Pointer to a variable that holds the ICC tag type on return.
212 * RETURNS
213 * Success: TRUE
214 * Failure: FALSE
216 * NOTES
217 * The tag table index starts at 1.
218 * Use GetCountColorProfileElements to retrieve a count of tagged elements.
220 BOOL WINAPI GetColorProfileElementTag( HPROFILE profile, DWORD index, PTAGTYPE type )
222 BOOL ret = FALSE;
223 #ifdef HAVE_LCMS
224 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
225 DWORD count;
226 icTag tag;
228 TRACE( "( %p, %ld, %p )\n", profile, index, type );
230 if (!iccprofile || !type) return FALSE;
232 count = MSCMS_get_tag_count( iccprofile );
233 if (index > count || index < 1) return FALSE;
235 MSCMS_get_tag_by_index( iccprofile, index - 1, &tag );
236 *type = tag.sig;
238 ret = TRUE;
240 #endif /* HAVE_LCMS */
241 return ret;
244 /******************************************************************************
245 * GetColorProfileFromHandle [MSCMS.@]
247 * Retrieve an ICC color profile by handle.
249 * PARAMS
250 * profile [I] Handle to a color profile.
251 * buffer [O] Buffer to receive the ICC profile.
252 * size [I/O] Size of the buffer in bytes. On return the variable holds the
253 * number of bytes actually needed.
255 * RETURNS
256 * Success: TRUE
257 * Failure: FALSE
259 * NOTES
260 * The profile returned will be in big-endian format.
262 BOOL WINAPI GetColorProfileFromHandle( HPROFILE profile, PBYTE buffer, PDWORD size )
264 BOOL ret = FALSE;
265 #ifdef HAVE_LCMS
266 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
267 PROFILEHEADER header;
269 TRACE( "( %p, %p, %p )\n", profile, buffer, size );
271 if (!iccprofile || !size) return FALSE;
272 MSCMS_get_profile_header( iccprofile, &header );
274 if (!buffer || header.phSize > *size)
276 *size = header.phSize;
277 return FALSE;
280 /* No endian conversion needed */
281 memcpy( buffer, iccprofile, header.phSize );
283 *size = header.phSize;
284 ret = TRUE;
286 #endif /* HAVE_LCMS */
287 return ret;
290 /******************************************************************************
291 * GetColorProfileHeader [MSCMS.@]
293 * Retrieve a color profile header by handle.
295 * PARAMS
296 * profile [I] Handle to a color profile.
297 * header [O] Buffer to receive the ICC profile header.
299 * RETURNS
300 * Success: TRUE
301 * Failure: FALSE
303 * NOTES
304 * The profile header returned will be adjusted for endianess.
306 BOOL WINAPI GetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
308 BOOL ret = FALSE;
309 #ifdef HAVE_LCMS
310 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
312 TRACE( "( %p, %p )\n", profile, header );
314 if (!iccprofile || !header) return FALSE;
316 MSCMS_get_profile_header( iccprofile, header );
317 return TRUE;
319 #endif /* HAVE_LCMS */
320 return ret;
323 /******************************************************************************
324 * GetCountColorProfileElements [MSCMS.@]
326 * Retrieve the number of elements in a color profile.
328 * PARAMS
329 * profile [I] Handle to a color profile.
330 * count [O] Pointer to a variable which is set to the number of elements
331 * in the color profile.
333 * RETURNS
334 * Success: TRUE
335 * Failure: FALSE
337 BOOL WINAPI GetCountColorProfileElements( HPROFILE profile, PDWORD count )
339 BOOL ret = FALSE;
340 #ifdef HAVE_LCMS
341 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
343 TRACE( "( %p, %p )\n", profile, count );
345 if (!iccprofile || !count) return FALSE;
346 *count = MSCMS_get_tag_count( iccprofile );
347 ret = TRUE;
349 #endif /* HAVE_LCMS */
350 return ret;
353 /******************************************************************************
354 * GetStandardColorSpaceProfileA [MSCMS.@]
356 * See GetStandardColorSpaceProfileW.
358 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
360 INT len;
361 LPWSTR profileW;
362 BOOL ret = FALSE;
363 DWORD sizeW;
365 TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
367 if (machine)
369 SetLastError( ERROR_NOT_SUPPORTED );
370 return FALSE;
373 if (!size)
375 SetLastError( ERROR_INVALID_PARAMETER );
376 return FALSE;
379 sizeW = *size * sizeof(WCHAR);
381 if (!profile)
383 ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
384 *size = sizeW / sizeof(WCHAR);
385 return FALSE;
388 profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
390 if (profileW)
392 ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW );
393 *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
395 if (ret)
397 len = WideCharToMultiByte( CP_ACP, 0, profileW, *size, profile, *size, NULL, NULL );
398 if (!len) ret = FALSE;
401 HeapFree( GetProcessHeap(), 0, profileW );
403 return ret;
406 /******************************************************************************
407 * GetStandardColorSpaceProfileW [MSCMS.@]
409 * Retrieve the profile filename for a given standard color space id.
411 * PARAMS
412 * machine [I] Name of the machine for which to get the standard color space.
413 * Must be NULL, which indicates the local machine.
414 * id [I] Id of a standard color space.
415 * profile [O] Buffer to receive the profile filename.
416 * size [I/O] Size of the filename buffer in bytes.
418 * RETURNS
419 * Success: TRUE
420 * Failure: FALSE
422 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
424 static const WCHAR rgbprofilefile[] =
425 { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
426 's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
427 WCHAR rgbprofile[MAX_PATH];
428 DWORD len = sizeof(rgbprofile);
430 TRACE( "( 0x%08lx, %p, %p )\n", id, profile, size );
432 if (machine)
434 SetLastError( ERROR_NOT_SUPPORTED );
435 return FALSE;
438 if (!size)
440 SetLastError( ERROR_INVALID_PARAMETER );
441 return FALSE;
444 if (!profile)
446 SetLastError( ERROR_INSUFFICIENT_BUFFER );
447 return FALSE;
450 GetColorDirectoryW( machine, rgbprofile, &len );
452 switch (id)
454 case 0x52474220: /* 'RGB ' */
455 lstrcatW( rgbprofile, rgbprofilefile );
456 len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
458 if (*size < len || !profile)
460 *size = len;
461 return TRUE;
464 lstrcpyW( profile, rgbprofile );
465 break;
467 default:
468 return FALSE;
470 return TRUE;
473 static BOOL MSCMS_header_from_file( LPWSTR file, PPROFILEHEADER header )
475 BOOL ret;
476 PROFILE profile;
477 WCHAR path[MAX_PATH], slash[] = {'\\',0};
478 DWORD size = sizeof(path);
479 HANDLE handle;
481 ret = GetColorDirectoryW( NULL, path, &size );
482 if (!ret)
484 WARN( "Can't retrieve color directory\n" );
485 return FALSE;
487 if (size + sizeof(slash) + sizeof(WCHAR) * lstrlenW( file ) > sizeof(path))
489 WARN( "Filename too long\n" );
490 return FALSE;
493 lstrcatW( path, slash );
494 lstrcatW( path, file );
496 profile.dwType = PROFILE_FILENAME;
497 profile.pProfileData = path;
498 profile.cbDataSize = lstrlenW( path ) + 1;
500 handle = OpenColorProfileW( &profile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );
501 if (!handle)
503 WARN( "Can't open color profile\n" );
504 return FALSE;
507 ret = GetColorProfileHeader( handle, header );
508 if (!ret)
509 WARN( "Can't retrieve color profile header\n" );
511 CloseColorProfile( handle );
512 return ret;
515 static BOOL MSCMS_match_profile( PENUMTYPEW rec, PPROFILEHEADER hdr )
517 if (rec->dwFields & ET_DEVICENAME)
519 FIXME( "ET_DEVICENAME: %s\n", debugstr_w(rec->pDeviceName) );
521 if (rec->dwFields & ET_MEDIATYPE)
523 FIXME( "ET_MEDIATYPE: 0x%08lx\n", rec->dwMediaType );
525 if (rec->dwFields & ET_DITHERMODE)
527 FIXME( "ET_DITHERMODE: 0x%08lx\n", rec->dwDitheringMode );
529 if (rec->dwFields & ET_RESOLUTION)
531 FIXME( "ET_RESOLUTION: 0x%08lx, 0x%08lx\n",
532 rec->dwResolution[0], rec->dwResolution[1] );
534 if (rec->dwFields & ET_DEVICECLASS)
536 FIXME( "ET_DEVICECLASS: %s\n", MSCMS_dbgstr_tag(rec->dwMediaType) );
538 if (rec->dwFields & ET_CMMTYPE)
540 TRACE( "ET_CMMTYPE: %s\n", MSCMS_dbgstr_tag(rec->dwCMMType) );
541 if (rec->dwCMMType != hdr->phCMMType) return FALSE;
543 if (rec->dwFields & ET_CLASS)
545 TRACE( "ET_CLASS: %s\n", MSCMS_dbgstr_tag(rec->dwClass) );
546 if (rec->dwClass != hdr->phClass) return FALSE;
548 if (rec->dwFields & ET_DATACOLORSPACE)
550 TRACE( "ET_DATACOLORSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwDataColorSpace) );
551 if (rec->dwDataColorSpace != hdr->phDataColorSpace) return FALSE;
553 if (rec->dwFields & ET_CONNECTIONSPACE)
555 TRACE( "ET_CONNECTIONSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwConnectionSpace) );
556 if (rec->dwConnectionSpace != hdr->phConnectionSpace) return FALSE;
558 if (rec->dwFields & ET_SIGNATURE)
560 TRACE( "ET_SIGNATURE: %s\n", MSCMS_dbgstr_tag(rec->dwSignature) );
561 if (rec->dwSignature != hdr->phSignature) return FALSE;
563 if (rec->dwFields & ET_PLATFORM)
565 TRACE( "ET_PLATFORM: %s\n", MSCMS_dbgstr_tag(rec->dwPlatform) );
566 if (rec->dwPlatform != hdr->phPlatform) return FALSE;
568 if (rec->dwFields & ET_PROFILEFLAGS)
570 TRACE( "ET_PROFILEFLAGS: 0x%08lx\n", rec->dwProfileFlags );
571 if (rec->dwProfileFlags != hdr->phProfileFlags) return FALSE;
573 if (rec->dwFields & ET_MANUFACTURER)
575 TRACE( "ET_MANUFACTURER: %s\n", MSCMS_dbgstr_tag(rec->dwManufacturer) );
576 if (rec->dwManufacturer != hdr->phManufacturer) return FALSE;
578 if (rec->dwFields & ET_MODEL)
580 TRACE( "ET_MODEL: %s\n", MSCMS_dbgstr_tag(rec->dwModel) );
581 if (rec->dwModel != hdr->phModel) return FALSE;
583 if (rec->dwFields & ET_ATTRIBUTES)
585 TRACE( "ET_ATTRIBUTES: 0x%08lx, 0x%08lx\n",
586 rec->dwAttributes[0], rec->dwAttributes[1] );
587 if (rec->dwAttributes[0] != hdr->phAttributes[0] ||
588 rec->dwAttributes[1] != hdr->phAttributes[1]) return FALSE;
590 if (rec->dwFields & ET_RENDERINGINTENT)
592 TRACE( "ET_RENDERINGINTENT: 0x%08lx\n", rec->dwRenderingIntent );
593 if (rec->dwRenderingIntent != hdr->phRenderingIntent) return FALSE;
595 if (rec->dwFields & ET_CREATOR)
597 TRACE( "ET_CREATOR: %s\n", MSCMS_dbgstr_tag(rec->dwCreator) );
598 if (rec->dwCreator != hdr->phCreator) return FALSE;
600 return TRUE;
603 /******************************************************************************
604 * EnumColorProfilesA [MSCMS.@]
606 * See EnumColorProfilesW.
608 BOOL WINAPI EnumColorProfilesA( PCSTR machine, PENUMTYPEA record, PBYTE buffer,
609 PDWORD size, PDWORD number )
611 BOOL match, ret = FALSE;
612 char spec[] = "\\*";
613 char colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
614 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
615 PROFILEHEADER header;
616 WIN32_FIND_DATAA data;
617 ENUMTYPEW recordW;
618 WCHAR *fileW = NULL;
619 HANDLE find;
621 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
623 if (machine || !record || !size ||
624 record->dwSize != sizeof(ENUMTYPEA) ||
625 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
627 ret = GetColorDirectoryA( machine, colordir, &len );
628 if (!ret || len + sizeof(spec) > MAX_PATH)
630 WARN( "can't retrieve color directory\n" );
631 return FALSE;
634 lstrcpyA( glob, colordir );
635 lstrcatA( glob, spec );
637 find = FindFirstFileA( glob, &data );
638 if (find == INVALID_HANDLE_VALUE) return FALSE;
640 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(char *) + 1 );
641 if (!profiles) goto exit;
643 memcpy( &recordW, record, sizeof(ENUMTYPEA) );
644 if (record->pDeviceName)
646 recordW.pDeviceName = MSCMS_strdupW( record->pDeviceName );
647 if (!recordW.pDeviceName) goto exit;
650 fileW = MSCMS_strdupW( data.cFileName );
651 if (!fileW) goto exit;
653 ret = MSCMS_header_from_file( fileW, &header );
654 if (ret)
656 match = MSCMS_match_profile( &recordW, &header );
657 if (match)
659 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
660 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
662 if (!profiles[count]) goto exit;
663 else
665 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
666 lstrcpyA( profiles[count], data.cFileName );
667 totalsize += len;
668 count++;
672 HeapFree( GetProcessHeap(), 0, fileW );
673 fileW = NULL;
675 while (FindNextFileA( find, &data ))
677 fileW = MSCMS_strdupW( data.cFileName );
678 if (!fileW) goto exit;
680 ret = MSCMS_header_from_file( fileW, &header );
681 if (!ret)
683 HeapFree( GetProcessHeap(), 0, fileW );
684 continue;
687 match = MSCMS_match_profile( &recordW, &header );
688 if (match)
690 char **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
691 profiles, sizeof(char *) * (count + 1) );
692 if (!tmp) goto exit;
693 else profiles = tmp;
695 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
696 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
698 if (!profiles[count]) goto exit;
699 else
701 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
702 lstrcpyA( profiles[count], data.cFileName );
703 totalsize += len;
704 count++;
707 HeapFree( GetProcessHeap(), 0, fileW );
708 fileW = NULL;
711 totalsize++;
712 if (buffer && *size >= totalsize)
714 char *p = (char *)buffer;
716 for (i = 0; i < count; i++)
718 lstrcpyA( p, profiles[i] );
719 p += lstrlenA( profiles[i] ) + 1;
721 *p = 0;
722 ret = TRUE;
724 else ret = FALSE;
726 *size = totalsize;
727 if (number) *number = count;
729 exit:
730 for (i = 0; i < count; i++)
731 HeapFree( GetProcessHeap(), 0, profiles[i] );
732 HeapFree( GetProcessHeap(), 0, profiles );
733 HeapFree( GetProcessHeap(), 0, (WCHAR *)recordW.pDeviceName );
734 HeapFree( GetProcessHeap(), 0, fileW );
735 FindClose( find );
737 return ret;
740 /******************************************************************************
741 * EnumColorProfilesW [MSCMS.@]
743 * Enumerate profiles that match given criteria.
745 * PARAMS
746 * machine [I] Name of the machine for which to enumerate profiles.
747 * Must be NULL, which indicates the local machine.
748 * record [I] Record of criteria that a profile must match.
749 * buffer [O] Buffer to receive a string array of profile filenames.
750 * size [I/O] Size of the filename buffer in bytes.
751 * number [O] Number of filenames copied into buffer.
753 * RETURNS
754 * Success: TRUE
755 * Failure: FALSE
757 BOOL WINAPI EnumColorProfilesW( PCWSTR machine, PENUMTYPEW record, PBYTE buffer,
758 PDWORD size, PDWORD number )
760 BOOL match, ret = FALSE;
761 WCHAR spec[] = {'\\','*',0};
762 WCHAR colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
763 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
764 PROFILEHEADER header;
765 WIN32_FIND_DATAW data;
766 HANDLE find;
768 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
770 if (machine || !record || !size ||
771 record->dwSize != sizeof(ENUMTYPEW) ||
772 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
774 ret = GetColorDirectoryW( machine, colordir, &len );
775 if (!ret || len + sizeof(spec) > MAX_PATH)
777 WARN( "Can't retrieve color directory\n" );
778 return FALSE;
781 lstrcpyW( glob, colordir );
782 lstrcatW( glob, spec );
784 find = FindFirstFileW( glob, &data );
785 if (find == INVALID_HANDLE_VALUE) return FALSE;
787 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR *) + 1 );
788 if (!profiles) goto exit;
790 ret = MSCMS_header_from_file( data.cFileName, &header );
791 if (ret)
793 match = MSCMS_match_profile( record, &header );
794 if (match)
796 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
797 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
799 if (!profiles[count]) goto exit;
800 else
802 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
803 lstrcpyW( profiles[count], data.cFileName );
804 totalsize += len;
805 count++;
810 while (FindNextFileW( find, &data ))
812 ret = MSCMS_header_from_file( data.cFileName, &header );
813 if (!ret) continue;
815 match = MSCMS_match_profile( record, &header );
816 if (match)
818 WCHAR **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
819 profiles, sizeof(WCHAR *) * (count + 1) );
820 if (!tmp) goto exit;
821 else profiles = tmp;
823 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
824 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
826 if (!profiles[count]) goto exit;
827 else
829 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
830 lstrcpyW( profiles[count], data.cFileName );
831 totalsize += len;
832 count++;
837 totalsize++;
838 if (buffer && *size >= totalsize)
840 WCHAR *p = (WCHAR *)buffer;
842 for (i = 0; i < count; i++)
844 lstrcpyW( p, profiles[i] );
845 p += lstrlenW( profiles[i] ) + 1;
847 *p = 0;
848 ret = TRUE;
850 else ret = FALSE;
852 *size = totalsize;
853 if (number) *number = count;
855 exit:
856 for (i = 0; i < count; i++)
857 HeapFree( GetProcessHeap(), 0, profiles[i] );
858 HeapFree( GetProcessHeap(), 0, profiles );
859 FindClose( find );
861 return ret;
864 /******************************************************************************
865 * InstallColorProfileA [MSCMS.@]
867 * See InstallColorProfileW.
869 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
871 UINT len;
872 LPWSTR profileW;
873 BOOL ret = FALSE;
875 TRACE( "( %s )\n", debugstr_a(profile) );
877 if (machine || !profile) return FALSE;
879 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
880 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
882 if (profileW)
884 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
886 ret = InstallColorProfileW( NULL, profileW );
887 HeapFree( GetProcessHeap(), 0, profileW );
889 return ret;
892 /******************************************************************************
893 * InstallColorProfileW [MSCMS.@]
895 * Install a color profile.
897 * PARAMS
898 * machine [I] Name of the machine to install the profile on. Must be NULL,
899 * which indicates the local machine.
900 * profile [I] Full path name of the profile to install.
902 * RETURNS
903 * Success: TRUE
904 * Failure: FALSE
906 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
908 WCHAR dest[MAX_PATH], base[MAX_PATH];
909 DWORD size = sizeof(dest);
910 static const WCHAR slash[] = { '\\', 0 };
912 TRACE( "( %s )\n", debugstr_w(profile) );
914 if (machine || !profile) return FALSE;
916 if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
918 MSCMS_basename( profile, base );
920 lstrcatW( dest, slash );
921 lstrcatW( dest, base );
923 /* Is source equal to destination? */
924 if (!lstrcmpW( profile, dest )) return TRUE;
926 return CopyFileW( profile, dest, TRUE );
929 /******************************************************************************
930 * IsColorProfileTagPresent [MSCMS.@]
932 * Determine if a given ICC tag type is present in a color profile.
934 * PARAMS
935 * profile [I] Color profile handle.
936 * tag [I] ICC tag type.
937 * present [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
938 * FALSE otherwise.
940 * RETURNS
941 * Success: TRUE
942 * Failure: FALSE
944 BOOL WINAPI IsColorProfileTagPresent( HPROFILE profile, TAGTYPE type, PBOOL present )
946 BOOL ret = FALSE;
947 #ifdef HAVE_LCMS
948 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
949 DWORD i, count;
950 icTag tag;
952 TRACE( "( %p, 0x%08lx, %p )\n", profile, type, present );
954 if (!iccprofile || !present) return FALSE;
956 count = MSCMS_get_tag_count( iccprofile );
958 for (i = 0; i < count; i++)
960 MSCMS_get_tag_by_index( iccprofile, i, &tag );
962 if (tag.sig == type)
964 *present = ret = TRUE;
965 break;
969 #endif /* HAVE_LCMS */
970 return ret;
973 /******************************************************************************
974 * IsColorProfileValid [MSCMS.@]
976 * Determine if a given color profile is valid.
978 * PARAMS
979 * profile [I] Color profile handle.
980 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
981 * FALSE otherwise.
983 * RETURNS
984 * Success: TRUE
985 * Failure: FALSE
987 BOOL WINAPI IsColorProfileValid( HPROFILE profile, PBOOL valid )
989 BOOL ret = FALSE;
990 #ifdef HAVE_LCMS
991 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
993 TRACE( "( %p, %p )\n", profile, valid );
995 if (!valid) return FALSE;
996 if (iccprofile) return *valid = TRUE;
998 #endif /* HAVE_LCMS */
999 return ret;
1002 /******************************************************************************
1003 * SetColorProfileElement [MSCMS.@]
1005 * Set data for a specified tag type.
1007 * PARAMS
1008 * profile [I] Handle to a color profile.
1009 * type [I] ICC tag type.
1010 * offset [I] Offset in bytes to start copying to.
1011 * size [I/O] Size of the buffer in bytes. On return the variable holds the
1012 * number of bytes actually needed.
1013 * buffer [O] Buffer holding the tag data.
1015 * RETURNS
1016 * Success: TRUE
1017 * Failure: FALSE
1019 BOOL WINAPI SetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
1020 PVOID buffer )
1022 BOOL ret = FALSE;
1023 #ifdef HAVE_LCMS
1024 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
1025 DWORD i, count, access = MSCMS_hprofile2access( profile );
1026 icTag tag;
1028 TRACE( "( %p, 0x%08lx, %ld, %p, %p )\n", profile, type, offset, size, buffer );
1030 if (!iccprofile || !size || !buffer) return FALSE;
1031 if (!(access & PROFILE_READWRITE)) return FALSE;
1033 count = MSCMS_get_tag_count( iccprofile );
1035 for (i = 0; i < count; i++)
1037 MSCMS_get_tag_by_index( iccprofile, i, &tag );
1039 if (tag.sig == type)
1041 if (offset > tag.size) return FALSE;
1043 MSCMS_set_tag_data( iccprofile, &tag, offset, buffer );
1044 return TRUE;
1048 #endif /* HAVE_LCMS */
1049 return ret;
1052 /******************************************************************************
1053 * SetColorProfileHeader [MSCMS.@]
1055 * Set header data for a given profile.
1057 * PARAMS
1058 * profile [I] Handle to a color profile.
1059 * header [I] Buffer holding the header data.
1061 * RETURNS
1062 * Success: TRUE
1063 * Failure: FALSE
1065 BOOL WINAPI SetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
1067 BOOL ret = FALSE;
1068 #ifdef HAVE_LCMS
1069 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
1070 DWORD access = MSCMS_hprofile2access( profile );
1072 TRACE( "( %p, %p )\n", profile, header );
1074 if (!iccprofile || !header) return FALSE;
1075 if (!(access & PROFILE_READWRITE)) return FALSE;
1077 MSCMS_set_profile_header( iccprofile, header );
1078 return TRUE;
1080 #endif /* HAVE_LCMS */
1081 return ret;
1084 /******************************************************************************
1085 * UninstallColorProfileA [MSCMS.@]
1087 * See UninstallColorProfileW.
1089 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
1091 UINT len;
1092 LPWSTR profileW;
1093 BOOL ret = FALSE;
1095 TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
1097 if (machine || !profile) return FALSE;
1099 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1100 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1102 if (profileW)
1104 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1106 ret = UninstallColorProfileW( NULL, profileW , delete );
1108 HeapFree( GetProcessHeap(), 0, profileW );
1110 return ret;
1113 /******************************************************************************
1114 * UninstallColorProfileW [MSCMS.@]
1116 * Uninstall a color profile.
1118 * PARAMS
1119 * machine [I] Name of the machine to uninstall the profile on. Must be NULL,
1120 * which indicates the local machine.
1121 * profile [I] Full path name of the profile to uninstall.
1122 * delete [I] Bool that specifies whether the profile file should be deleted.
1124 * RETURNS
1125 * Success: TRUE
1126 * Failure: FALSE
1128 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
1130 TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
1132 if (machine || !profile) return FALSE;
1134 if (delete) return DeleteFileW( profile );
1136 return TRUE;
1139 /******************************************************************************
1140 * OpenColorProfileA [MSCMS.@]
1142 * See OpenColorProfileW.
1144 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1146 HPROFILE handle = NULL;
1148 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
1150 if (!profile || !profile->pProfileData) return NULL;
1152 /* No AW conversion needed for memory based profiles */
1153 if (profile->dwType & PROFILE_MEMBUFFER)
1154 return OpenColorProfileW( profile, access, sharing, creation );
1156 if (profile->dwType & PROFILE_FILENAME)
1158 UINT len;
1159 PROFILE profileW;
1161 profileW.dwType = profile->dwType;
1163 len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
1164 profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1166 if (profileW.pProfileData)
1168 profileW.cbDataSize = len * sizeof(WCHAR);
1169 MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
1171 handle = OpenColorProfileW( &profileW, access, sharing, creation );
1172 HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
1175 return handle;
1178 /******************************************************************************
1179 * OpenColorProfileW [MSCMS.@]
1181 * Open a color profile.
1183 * PARAMS
1184 * profile [I] Pointer to a color profile structure.
1185 * access [I] Desired access.
1186 * sharing [I] Sharing mode.
1187 * creation [I] Creation mode.
1189 * RETURNS
1190 * Success: Handle to the opened profile.
1191 * Failure: NULL
1193 * NOTES
1194 * Values for access: PROFILE_READ or PROFILE_READWRITE.
1195 * Values for sharing: 0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
1196 * Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
1197 * OPEN_ALWAYS, TRUNCATE_EXISTING.
1198 * Sharing and creation flags are ignored for memory based profiles.
1200 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1202 #ifdef HAVE_LCMS
1203 cmsHPROFILE cmsprofile = NULL;
1204 icProfile *iccprofile = NULL;
1205 HANDLE handle = NULL;
1206 DWORD size;
1208 TRACE( "( %p, 0x%08lx, 0x%08lx, 0x%08lx )\n", profile, access, sharing, creation );
1210 if (!profile || !profile->pProfileData) return NULL;
1212 if (profile->dwType & PROFILE_MEMBUFFER)
1214 /* FIXME: access flags not implemented for memory based profiles */
1216 iccprofile = profile->pProfileData;
1217 size = profile->cbDataSize;
1219 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
1222 if (profile->dwType & PROFILE_FILENAME)
1224 DWORD read, flags = 0;
1226 TRACE( "profile file: %s\n", debugstr_w( (WCHAR *)profile->pProfileData ) );
1228 if (access & PROFILE_READ) flags = GENERIC_READ;
1229 if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
1231 if (!flags) return NULL;
1233 handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
1234 if (handle == INVALID_HANDLE_VALUE)
1236 WARN( "Unable to open color profile\n" );
1237 return NULL;
1240 if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
1242 ERR( "Unable to retrieve size of color profile\n" );
1243 CloseHandle( handle );
1244 return NULL;
1247 iccprofile = HeapAlloc( GetProcessHeap(), 0, size );
1248 if (!iccprofile)
1250 ERR( "Unable to allocate memory for color profile\n" );
1251 CloseHandle( handle );
1252 return NULL;
1255 if (!ReadFile( handle, iccprofile, size, &read, NULL ) || read != size)
1257 ERR( "Unable to read color profile\n" );
1259 CloseHandle( handle );
1260 HeapFree( GetProcessHeap(), 0, iccprofile );
1261 return NULL;
1264 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
1267 if (cmsprofile)
1268 return MSCMS_create_hprofile_handle( handle, iccprofile, cmsprofile, access );
1270 #endif /* HAVE_LCMS */
1271 return NULL;
1274 /******************************************************************************
1275 * CloseColorProfile [MSCMS.@]
1277 * Close a color profile.
1279 * PARAMS
1280 * profile [I] Handle to the profile.
1282 * RETURNS
1283 * Success: TRUE
1284 * Failure: FALSE
1286 BOOL WINAPI CloseColorProfile( HPROFILE profile )
1288 BOOL ret = FALSE;
1289 #ifdef HAVE_LCMS
1290 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
1291 HANDLE file = MSCMS_hprofile2handle( profile );
1292 DWORD access = MSCMS_hprofile2access( profile );
1294 TRACE( "( %p )\n", profile );
1296 if (file && (access & PROFILE_READWRITE))
1298 DWORD written, size = MSCMS_get_profile_size( iccprofile );
1300 if (SetFilePointer( file, 0, NULL, FILE_BEGIN ) ||
1301 !WriteFile( file, iccprofile, size, &written, NULL ) || written != size)
1302 ERR( "Unable to write color profile\n" );
1305 ret = cmsCloseProfile( MSCMS_hprofile2cmsprofile( profile ) );
1306 HeapFree( GetProcessHeap(), 0, MSCMS_hprofile2iccprofile( profile ) );
1308 CloseHandle( MSCMS_hprofile2handle( profile ) );
1309 MSCMS_destroy_hprofile_handle( profile );
1311 #endif /* HAVE_LCMS */
1312 return ret;