localui: Add German resources.
[wine/wine64.git] / dlls / mscms / profile.c
blob0ada3e8ab7a4f890311113b7f86ab994b13a9e54
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 if ((ret = GetColorDirectoryW( NULL, bufferW, &sizeW )))
96 *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
97 len = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, *size, NULL, NULL );
98 if (!len) ret = FALSE;
100 else *size = sizeW / sizeof(WCHAR);
102 HeapFree( GetProcessHeap(), 0, bufferW );
104 return ret;
107 /******************************************************************************
108 * GetColorDirectoryW [MSCMS.@]
110 * Get the directory where color profiles are stored.
112 * PARAMS
113 * machine [I] Name of the machine for which to get the color directory.
114 * Must be NULL, which indicates the local machine.
115 * buffer [I] Buffer to receive the path name.
116 * size [I/O] Size of the buffer in bytes. On return the variable holds
117 * the number of bytes actually needed.
119 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
121 WCHAR colordir[MAX_PATH];
122 static const WCHAR colorsubdir[] = { '\\','c','o','l','o','r',0 };
123 DWORD len;
125 TRACE( "( %p, %p )\n", buffer, size );
127 if (machine || !size) return FALSE;
129 GetSystemDirectoryW( colordir, sizeof(colordir) / sizeof(WCHAR) );
130 lstrcatW( colordir, colorsubdir );
132 len = lstrlenW( colordir ) * sizeof(WCHAR);
134 if (buffer && len <= *size)
136 lstrcpyW( buffer, colordir );
137 *size = len;
138 return TRUE;
141 SetLastError( ERROR_MORE_DATA );
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%08x, %d, %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, %d, %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%08x, %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 if ((ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW )))
394 *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
395 len = WideCharToMultiByte( CP_ACP, 0, profileW, -1, profile, *size, NULL, NULL );
396 if (!len) ret = FALSE;
398 else *size = sizeW / sizeof(WCHAR);
400 HeapFree( GetProcessHeap(), 0, profileW );
402 return ret;
405 /******************************************************************************
406 * GetStandardColorSpaceProfileW [MSCMS.@]
408 * Retrieve the profile filename for a given standard color space id.
410 * PARAMS
411 * machine [I] Name of the machine for which to get the standard color space.
412 * Must be NULL, which indicates the local machine.
413 * id [I] Id of a standard color space.
414 * profile [O] Buffer to receive the profile filename.
415 * size [I/O] Size of the filename buffer in bytes.
417 * RETURNS
418 * Success: TRUE
419 * Failure: FALSE
421 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
423 static const WCHAR rgbprofilefile[] =
424 { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
425 's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
426 WCHAR rgbprofile[MAX_PATH];
427 DWORD len = sizeof(rgbprofile);
429 TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
431 if (machine)
433 SetLastError( ERROR_NOT_SUPPORTED );
434 return FALSE;
437 if (!size)
439 SetLastError( ERROR_INVALID_PARAMETER );
440 return FALSE;
443 if (!profile)
445 SetLastError( ERROR_INSUFFICIENT_BUFFER );
446 return FALSE;
449 GetColorDirectoryW( machine, rgbprofile, &len );
451 switch (id)
453 case SPACE_RGB: /* 'RGB ' */
454 lstrcatW( rgbprofile, rgbprofilefile );
455 len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
457 if (*size < len || !profile)
459 *size = len;
460 SetLastError( ERROR_MORE_DATA );
461 return FALSE;
464 lstrcpyW( profile, rgbprofile );
465 break;
467 default:
468 SetLastError( ERROR_FILE_NOT_FOUND );
469 return FALSE;
471 return TRUE;
474 static BOOL MSCMS_header_from_file( LPWSTR file, PPROFILEHEADER header )
476 BOOL ret;
477 PROFILE profile;
478 WCHAR path[MAX_PATH], slash[] = {'\\',0};
479 DWORD size = sizeof(path);
480 HANDLE handle;
482 ret = GetColorDirectoryW( NULL, path, &size );
483 if (!ret)
485 WARN( "Can't retrieve color directory\n" );
486 return FALSE;
488 if (size + sizeof(slash) + sizeof(WCHAR) * lstrlenW( file ) > sizeof(path))
490 WARN( "Filename too long\n" );
491 return FALSE;
494 lstrcatW( path, slash );
495 lstrcatW( path, file );
497 profile.dwType = PROFILE_FILENAME;
498 profile.pProfileData = path;
499 profile.cbDataSize = lstrlenW( path ) + 1;
501 handle = OpenColorProfileW( &profile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );
502 if (!handle)
504 WARN( "Can't open color profile\n" );
505 return FALSE;
508 ret = GetColorProfileHeader( handle, header );
509 if (!ret)
510 WARN( "Can't retrieve color profile header\n" );
512 CloseColorProfile( handle );
513 return ret;
516 static BOOL MSCMS_match_profile( PENUMTYPEW rec, PPROFILEHEADER hdr )
518 if (rec->dwFields & ET_DEVICENAME)
520 FIXME( "ET_DEVICENAME: %s\n", debugstr_w(rec->pDeviceName) );
522 if (rec->dwFields & ET_MEDIATYPE)
524 FIXME( "ET_MEDIATYPE: 0x%08x\n", rec->dwMediaType );
526 if (rec->dwFields & ET_DITHERMODE)
528 FIXME( "ET_DITHERMODE: 0x%08x\n", rec->dwDitheringMode );
530 if (rec->dwFields & ET_RESOLUTION)
532 FIXME( "ET_RESOLUTION: 0x%08x, 0x%08x\n",
533 rec->dwResolution[0], rec->dwResolution[1] );
535 if (rec->dwFields & ET_DEVICECLASS)
537 FIXME( "ET_DEVICECLASS: %s\n", MSCMS_dbgstr_tag(rec->dwMediaType) );
539 if (rec->dwFields & ET_CMMTYPE)
541 TRACE( "ET_CMMTYPE: %s\n", MSCMS_dbgstr_tag(rec->dwCMMType) );
542 if (rec->dwCMMType != hdr->phCMMType) return FALSE;
544 if (rec->dwFields & ET_CLASS)
546 TRACE( "ET_CLASS: %s\n", MSCMS_dbgstr_tag(rec->dwClass) );
547 if (rec->dwClass != hdr->phClass) return FALSE;
549 if (rec->dwFields & ET_DATACOLORSPACE)
551 TRACE( "ET_DATACOLORSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwDataColorSpace) );
552 if (rec->dwDataColorSpace != hdr->phDataColorSpace) return FALSE;
554 if (rec->dwFields & ET_CONNECTIONSPACE)
556 TRACE( "ET_CONNECTIONSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwConnectionSpace) );
557 if (rec->dwConnectionSpace != hdr->phConnectionSpace) return FALSE;
559 if (rec->dwFields & ET_SIGNATURE)
561 TRACE( "ET_SIGNATURE: %s\n", MSCMS_dbgstr_tag(rec->dwSignature) );
562 if (rec->dwSignature != hdr->phSignature) return FALSE;
564 if (rec->dwFields & ET_PLATFORM)
566 TRACE( "ET_PLATFORM: %s\n", MSCMS_dbgstr_tag(rec->dwPlatform) );
567 if (rec->dwPlatform != hdr->phPlatform) return FALSE;
569 if (rec->dwFields & ET_PROFILEFLAGS)
571 TRACE( "ET_PROFILEFLAGS: 0x%08x\n", rec->dwProfileFlags );
572 if (rec->dwProfileFlags != hdr->phProfileFlags) return FALSE;
574 if (rec->dwFields & ET_MANUFACTURER)
576 TRACE( "ET_MANUFACTURER: %s\n", MSCMS_dbgstr_tag(rec->dwManufacturer) );
577 if (rec->dwManufacturer != hdr->phManufacturer) return FALSE;
579 if (rec->dwFields & ET_MODEL)
581 TRACE( "ET_MODEL: %s\n", MSCMS_dbgstr_tag(rec->dwModel) );
582 if (rec->dwModel != hdr->phModel) return FALSE;
584 if (rec->dwFields & ET_ATTRIBUTES)
586 TRACE( "ET_ATTRIBUTES: 0x%08x, 0x%08x\n",
587 rec->dwAttributes[0], rec->dwAttributes[1] );
588 if (rec->dwAttributes[0] != hdr->phAttributes[0] ||
589 rec->dwAttributes[1] != hdr->phAttributes[1]) return FALSE;
591 if (rec->dwFields & ET_RENDERINGINTENT)
593 TRACE( "ET_RENDERINGINTENT: 0x%08x\n", rec->dwRenderingIntent );
594 if (rec->dwRenderingIntent != hdr->phRenderingIntent) return FALSE;
596 if (rec->dwFields & ET_CREATOR)
598 TRACE( "ET_CREATOR: %s\n", MSCMS_dbgstr_tag(rec->dwCreator) );
599 if (rec->dwCreator != hdr->phCreator) return FALSE;
601 return TRUE;
604 /******************************************************************************
605 * EnumColorProfilesA [MSCMS.@]
607 * See EnumColorProfilesW.
609 BOOL WINAPI EnumColorProfilesA( PCSTR machine, PENUMTYPEA record, PBYTE buffer,
610 PDWORD size, PDWORD number )
612 BOOL match, ret = FALSE;
613 char spec[] = "\\*";
614 char colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
615 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
616 PROFILEHEADER header;
617 WIN32_FIND_DATAA data;
618 ENUMTYPEW recordW;
619 WCHAR *fileW = NULL, *deviceW = NULL;
620 HANDLE find;
622 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
624 if (machine || !record || !size ||
625 record->dwSize != sizeof(ENUMTYPEA) ||
626 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
628 ret = GetColorDirectoryA( machine, colordir, &len );
629 if (!ret || len + sizeof(spec) > MAX_PATH)
631 WARN( "can't retrieve color directory\n" );
632 return FALSE;
635 lstrcpyA( glob, colordir );
636 lstrcatA( glob, spec );
638 find = FindFirstFileA( glob, &data );
639 if (find == INVALID_HANDLE_VALUE) return FALSE;
641 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(char *) + 1 );
642 if (!profiles) goto exit;
644 memcpy( &recordW, record, sizeof(ENUMTYPEA) );
645 if (record->pDeviceName)
647 deviceW = MSCMS_strdupW( record->pDeviceName );
648 if (!(recordW.pDeviceName = deviceW)) goto exit;
651 fileW = MSCMS_strdupW( data.cFileName );
652 if (!fileW) goto exit;
654 ret = MSCMS_header_from_file( fileW, &header );
655 if (ret)
657 match = MSCMS_match_profile( &recordW, &header );
658 if (match)
660 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
661 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
663 if (!profiles[count]) goto exit;
664 else
666 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
667 lstrcpyA( profiles[count], data.cFileName );
668 totalsize += len;
669 count++;
673 HeapFree( GetProcessHeap(), 0, fileW );
674 fileW = NULL;
676 while (FindNextFileA( find, &data ))
678 fileW = MSCMS_strdupW( data.cFileName );
679 if (!fileW) goto exit;
681 ret = MSCMS_header_from_file( fileW, &header );
682 if (!ret)
684 HeapFree( GetProcessHeap(), 0, fileW );
685 continue;
688 match = MSCMS_match_profile( &recordW, &header );
689 if (match)
691 char **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
692 profiles, sizeof(char *) * (count + 1) );
693 if (!tmp) goto exit;
694 else profiles = tmp;
696 len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
697 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
699 if (!profiles[count]) goto exit;
700 else
702 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
703 lstrcpyA( profiles[count], data.cFileName );
704 totalsize += len;
705 count++;
708 HeapFree( GetProcessHeap(), 0, fileW );
709 fileW = NULL;
712 totalsize++;
713 if (buffer && *size >= totalsize)
715 char *p = (char *)buffer;
717 for (i = 0; i < count; i++)
719 lstrcpyA( p, profiles[i] );
720 p += lstrlenA( profiles[i] ) + 1;
722 *p = 0;
723 ret = TRUE;
725 else ret = FALSE;
727 *size = totalsize;
728 if (number) *number = count;
730 exit:
731 for (i = 0; i < count; i++)
732 HeapFree( GetProcessHeap(), 0, profiles[i] );
733 HeapFree( GetProcessHeap(), 0, profiles );
734 HeapFree( GetProcessHeap(), 0, deviceW );
735 HeapFree( GetProcessHeap(), 0, fileW );
736 FindClose( find );
738 return ret;
741 /******************************************************************************
742 * EnumColorProfilesW [MSCMS.@]
744 * Enumerate profiles that match given criteria.
746 * PARAMS
747 * machine [I] Name of the machine for which to enumerate profiles.
748 * Must be NULL, which indicates the local machine.
749 * record [I] Record of criteria that a profile must match.
750 * buffer [O] Buffer to receive a string array of profile filenames.
751 * size [I/O] Size of the filename buffer in bytes.
752 * number [O] Number of filenames copied into buffer.
754 * RETURNS
755 * Success: TRUE
756 * Failure: FALSE
758 BOOL WINAPI EnumColorProfilesW( PCWSTR machine, PENUMTYPEW record, PBYTE buffer,
759 PDWORD size, PDWORD number )
761 BOOL match, ret = FALSE;
762 WCHAR spec[] = {'\\','*',0};
763 WCHAR colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
764 DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
765 PROFILEHEADER header;
766 WIN32_FIND_DATAW data;
767 HANDLE find;
769 TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
771 if (machine || !record || !size ||
772 record->dwSize != sizeof(ENUMTYPEW) ||
773 record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
775 ret = GetColorDirectoryW( machine, colordir, &len );
776 if (!ret || len + sizeof(spec) > MAX_PATH)
778 WARN( "Can't retrieve color directory\n" );
779 return FALSE;
782 lstrcpyW( glob, colordir );
783 lstrcatW( glob, spec );
785 find = FindFirstFileW( glob, &data );
786 if (find == INVALID_HANDLE_VALUE) return FALSE;
788 profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR *) + 1 );
789 if (!profiles) goto exit;
791 ret = MSCMS_header_from_file( data.cFileName, &header );
792 if (ret)
794 match = MSCMS_match_profile( record, &header );
795 if (match)
797 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
798 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
800 if (!profiles[count]) goto exit;
801 else
803 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
804 lstrcpyW( profiles[count], data.cFileName );
805 totalsize += len;
806 count++;
811 while (FindNextFileW( find, &data ))
813 ret = MSCMS_header_from_file( data.cFileName, &header );
814 if (!ret) continue;
816 match = MSCMS_match_profile( record, &header );
817 if (match)
819 WCHAR **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
820 profiles, sizeof(WCHAR *) * (count + 1) );
821 if (!tmp) goto exit;
822 else profiles = tmp;
824 len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
825 profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
827 if (!profiles[count]) goto exit;
828 else
830 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
831 lstrcpyW( profiles[count], data.cFileName );
832 totalsize += len;
833 count++;
838 totalsize++;
839 if (buffer && *size >= totalsize)
841 WCHAR *p = (WCHAR *)buffer;
843 for (i = 0; i < count; i++)
845 lstrcpyW( p, profiles[i] );
846 p += lstrlenW( profiles[i] ) + 1;
848 *p = 0;
849 ret = TRUE;
851 else ret = FALSE;
853 *size = totalsize;
854 if (number) *number = count;
856 exit:
857 for (i = 0; i < count; i++)
858 HeapFree( GetProcessHeap(), 0, profiles[i] );
859 HeapFree( GetProcessHeap(), 0, profiles );
860 FindClose( find );
862 return ret;
865 /******************************************************************************
866 * InstallColorProfileA [MSCMS.@]
868 * See InstallColorProfileW.
870 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
872 UINT len;
873 LPWSTR profileW;
874 BOOL ret = FALSE;
876 TRACE( "( %s )\n", debugstr_a(profile) );
878 if (machine || !profile) return FALSE;
880 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
881 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
883 if (profileW)
885 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
887 ret = InstallColorProfileW( NULL, profileW );
888 HeapFree( GetProcessHeap(), 0, profileW );
890 return ret;
893 /******************************************************************************
894 * InstallColorProfileW [MSCMS.@]
896 * Install a color profile.
898 * PARAMS
899 * machine [I] Name of the machine to install the profile on. Must be NULL,
900 * which indicates the local machine.
901 * profile [I] Full path name of the profile to install.
903 * RETURNS
904 * Success: TRUE
905 * Failure: FALSE
907 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
909 WCHAR dest[MAX_PATH], base[MAX_PATH];
910 DWORD size = sizeof(dest);
911 static const WCHAR slash[] = { '\\', 0 };
913 TRACE( "( %s )\n", debugstr_w(profile) );
915 if (machine || !profile) return FALSE;
917 if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
919 MSCMS_basename( profile, base );
921 lstrcatW( dest, slash );
922 lstrcatW( dest, base );
924 /* Is source equal to destination? */
925 if (!lstrcmpW( profile, dest )) return TRUE;
927 return CopyFileW( profile, dest, TRUE );
930 /******************************************************************************
931 * IsColorProfileTagPresent [MSCMS.@]
933 * Determine if a given ICC tag type is present in a color profile.
935 * PARAMS
936 * profile [I] Color profile handle.
937 * tag [I] ICC tag type.
938 * present [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
939 * FALSE otherwise.
941 * RETURNS
942 * Success: TRUE
943 * Failure: FALSE
945 BOOL WINAPI IsColorProfileTagPresent( HPROFILE profile, TAGTYPE type, PBOOL present )
947 BOOL ret = FALSE;
948 #ifdef HAVE_LCMS
949 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
950 DWORD i, count;
951 icTag tag;
953 TRACE( "( %p, 0x%08x, %p )\n", profile, type, present );
955 if (!iccprofile || !present) return FALSE;
957 count = MSCMS_get_tag_count( iccprofile );
959 for (i = 0; i < count; i++)
961 MSCMS_get_tag_by_index( iccprofile, i, &tag );
963 if (tag.sig == type)
965 *present = ret = TRUE;
966 break;
970 #endif /* HAVE_LCMS */
971 return ret;
974 /******************************************************************************
975 * IsColorProfileValid [MSCMS.@]
977 * Determine if a given color profile is valid.
979 * PARAMS
980 * profile [I] Color profile handle.
981 * valid [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
982 * FALSE otherwise.
984 * RETURNS
985 * Success: TRUE
986 * Failure: FALSE
988 BOOL WINAPI IsColorProfileValid( HPROFILE profile, PBOOL valid )
990 BOOL ret = FALSE;
991 #ifdef HAVE_LCMS
992 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
994 TRACE( "( %p, %p )\n", profile, valid );
996 if (!valid) return FALSE;
997 if (iccprofile) return *valid = TRUE;
999 #endif /* HAVE_LCMS */
1000 return ret;
1003 /******************************************************************************
1004 * SetColorProfileElement [MSCMS.@]
1006 * Set data for a specified tag type.
1008 * PARAMS
1009 * profile [I] Handle to a color profile.
1010 * type [I] ICC tag type.
1011 * offset [I] Offset in bytes to start copying to.
1012 * size [I/O] Size of the buffer in bytes. On return the variable holds the
1013 * number of bytes actually needed.
1014 * buffer [O] Buffer holding the tag data.
1016 * RETURNS
1017 * Success: TRUE
1018 * Failure: FALSE
1020 BOOL WINAPI SetColorProfileElement( HPROFILE profile, TAGTYPE type, DWORD offset, PDWORD size,
1021 PVOID buffer )
1023 BOOL ret = FALSE;
1024 #ifdef HAVE_LCMS
1025 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
1026 DWORD i, count, access = MSCMS_hprofile2access( profile );
1027 icTag tag;
1029 TRACE( "( %p, 0x%08x, %d, %p, %p )\n", profile, type, offset, size, buffer );
1031 if (!iccprofile || !size || !buffer) return FALSE;
1032 if (!(access & PROFILE_READWRITE)) return FALSE;
1034 count = MSCMS_get_tag_count( iccprofile );
1036 for (i = 0; i < count; i++)
1038 MSCMS_get_tag_by_index( iccprofile, i, &tag );
1040 if (tag.sig == type)
1042 if (offset > tag.size) return FALSE;
1044 MSCMS_set_tag_data( iccprofile, &tag, offset, buffer );
1045 return TRUE;
1049 #endif /* HAVE_LCMS */
1050 return ret;
1053 /******************************************************************************
1054 * SetColorProfileHeader [MSCMS.@]
1056 * Set header data for a given profile.
1058 * PARAMS
1059 * profile [I] Handle to a color profile.
1060 * header [I] Buffer holding the header data.
1062 * RETURNS
1063 * Success: TRUE
1064 * Failure: FALSE
1066 BOOL WINAPI SetColorProfileHeader( HPROFILE profile, PPROFILEHEADER header )
1068 BOOL ret = FALSE;
1069 #ifdef HAVE_LCMS
1070 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
1071 DWORD access = MSCMS_hprofile2access( profile );
1073 TRACE( "( %p, %p )\n", profile, header );
1075 if (!iccprofile || !header) return FALSE;
1076 if (!(access & PROFILE_READWRITE)) return FALSE;
1078 MSCMS_set_profile_header( iccprofile, header );
1079 return TRUE;
1081 #endif /* HAVE_LCMS */
1082 return ret;
1085 /******************************************************************************
1086 * UninstallColorProfileA [MSCMS.@]
1088 * See UninstallColorProfileW.
1090 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
1092 UINT len;
1093 LPWSTR profileW;
1094 BOOL ret = FALSE;
1096 TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
1098 if (machine || !profile) return FALSE;
1100 len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1101 profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1103 if (profileW)
1105 MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1107 ret = UninstallColorProfileW( NULL, profileW , delete );
1109 HeapFree( GetProcessHeap(), 0, profileW );
1111 return ret;
1114 /******************************************************************************
1115 * UninstallColorProfileW [MSCMS.@]
1117 * Uninstall a color profile.
1119 * PARAMS
1120 * machine [I] Name of the machine to uninstall the profile on. Must be NULL,
1121 * which indicates the local machine.
1122 * profile [I] Full path name of the profile to uninstall.
1123 * delete [I] Bool that specifies whether the profile file should be deleted.
1125 * RETURNS
1126 * Success: TRUE
1127 * Failure: FALSE
1129 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
1131 TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
1133 if (machine || !profile) return FALSE;
1135 if (delete) return DeleteFileW( profile );
1137 return TRUE;
1140 /******************************************************************************
1141 * OpenColorProfileA [MSCMS.@]
1143 * See OpenColorProfileW.
1145 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1147 HPROFILE handle = NULL;
1149 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1151 if (!profile || !profile->pProfileData) return NULL;
1153 /* No AW conversion needed for memory based profiles */
1154 if (profile->dwType & PROFILE_MEMBUFFER)
1155 return OpenColorProfileW( profile, access, sharing, creation );
1157 if (profile->dwType & PROFILE_FILENAME)
1159 UINT len;
1160 PROFILE profileW;
1162 profileW.dwType = profile->dwType;
1164 len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
1165 profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1167 if (profileW.pProfileData)
1169 profileW.cbDataSize = len * sizeof(WCHAR);
1170 MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
1172 handle = OpenColorProfileW( &profileW, access, sharing, creation );
1173 HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
1176 return handle;
1179 /******************************************************************************
1180 * OpenColorProfileW [MSCMS.@]
1182 * Open a color profile.
1184 * PARAMS
1185 * profile [I] Pointer to a color profile structure.
1186 * access [I] Desired access.
1187 * sharing [I] Sharing mode.
1188 * creation [I] Creation mode.
1190 * RETURNS
1191 * Success: Handle to the opened profile.
1192 * Failure: NULL
1194 * NOTES
1195 * Values for access: PROFILE_READ or PROFILE_READWRITE.
1196 * Values for sharing: 0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
1197 * Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
1198 * OPEN_ALWAYS, TRUNCATE_EXISTING.
1199 * Sharing and creation flags are ignored for memory based profiles.
1201 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1203 #ifdef HAVE_LCMS
1204 cmsHPROFILE cmsprofile = NULL;
1205 icProfile *iccprofile = NULL;
1206 HANDLE handle = NULL;
1207 DWORD size;
1209 TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1211 if (!profile || !profile->pProfileData) return NULL;
1213 if (profile->dwType & PROFILE_MEMBUFFER)
1215 /* FIXME: access flags not implemented for memory based profiles */
1217 iccprofile = profile->pProfileData;
1218 size = profile->cbDataSize;
1220 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
1223 if (profile->dwType & PROFILE_FILENAME)
1225 DWORD read, flags = 0;
1227 TRACE( "profile file: %s\n", debugstr_w( (WCHAR *)profile->pProfileData ) );
1229 if (access & PROFILE_READ) flags = GENERIC_READ;
1230 if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
1232 if (!flags) return NULL;
1234 handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
1235 if (handle == INVALID_HANDLE_VALUE)
1237 WARN( "Unable to open color profile\n" );
1238 return NULL;
1241 if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
1243 ERR( "Unable to retrieve size of color profile\n" );
1244 CloseHandle( handle );
1245 return NULL;
1248 iccprofile = HeapAlloc( GetProcessHeap(), 0, size );
1249 if (!iccprofile)
1251 ERR( "Unable to allocate memory for color profile\n" );
1252 CloseHandle( handle );
1253 return NULL;
1256 if (!ReadFile( handle, iccprofile, size, &read, NULL ) || read != size)
1258 ERR( "Unable to read color profile\n" );
1260 CloseHandle( handle );
1261 HeapFree( GetProcessHeap(), 0, iccprofile );
1262 return NULL;
1265 cmsprofile = cmsOpenProfileFromMem( iccprofile, size );
1268 if (cmsprofile)
1269 return MSCMS_create_hprofile_handle( handle, iccprofile, cmsprofile, access );
1271 #endif /* HAVE_LCMS */
1272 return NULL;
1275 /******************************************************************************
1276 * CloseColorProfile [MSCMS.@]
1278 * Close a color profile.
1280 * PARAMS
1281 * profile [I] Handle to the profile.
1283 * RETURNS
1284 * Success: TRUE
1285 * Failure: FALSE
1287 BOOL WINAPI CloseColorProfile( HPROFILE profile )
1289 BOOL ret = FALSE;
1290 #ifdef HAVE_LCMS
1291 icProfile *iccprofile = MSCMS_hprofile2iccprofile( profile );
1292 HANDLE file = MSCMS_hprofile2handle( profile );
1293 DWORD access = MSCMS_hprofile2access( profile );
1295 TRACE( "( %p )\n", profile );
1297 if (file && (access & PROFILE_READWRITE))
1299 DWORD written, size = MSCMS_get_profile_size( iccprofile );
1301 if (SetFilePointer( file, 0, NULL, FILE_BEGIN ) ||
1302 !WriteFile( file, iccprofile, size, &written, NULL ) || written != size)
1303 ERR( "Unable to write color profile\n" );
1306 ret = cmsCloseProfile( MSCMS_hprofile2cmsprofile( profile ) );
1307 HeapFree( GetProcessHeap(), 0, MSCMS_hprofile2iccprofile( profile ) );
1309 CloseHandle( MSCMS_hprofile2handle( profile ) );
1310 MSCMS_destroy_hprofile_handle( profile );
1312 #endif /* HAVE_LCMS */
1313 return ret;