~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/mscms/profile.c

Version: ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  1 /*
  2  * MSCMS - Color Management System for Wine
  3  *
  4  * Copyright 2004, 2005, 2006, 2008 Hans Leidekker
  5  *
  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.
 10  *
 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.
 15  *
 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
 19  */
 20 
 21 #include "config.h"
 22 #include "wine/debug.h"
 23 #include "wine/unicode.h"
 24 
 25 #include <stdarg.h>
 26 
 27 #include "windef.h"
 28 #include "winbase.h"
 29 #include "winnls.h"
 30 #include "wingdi.h"
 31 #include "winuser.h"
 32 #include "winreg.h"
 33 #include "icm.h"
 34 
 35 #include "mscms_priv.h"
 36 
 37 #define IS_SEPARATOR(ch)  ((ch) == '\\' || (ch) == '/')
 38 
 39 static void MSCMS_basename( LPCWSTR path, LPWSTR name )
 40 {
 41     INT i = lstrlenW( path );
 42 
 43     while (i > 0 && !IS_SEPARATOR(path[i - 1])) i--;
 44     lstrcpyW( name, &path[i] );
 45 }
 46 
 47 static inline LPWSTR MSCMS_strdupW( LPCSTR str )
 48 {
 49     LPWSTR ret = NULL;
 50     if (str)
 51     {
 52         DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
 53         if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
 54             MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
 55     }
 56     return ret;
 57 }
 58 
 59 const char *MSCMS_dbgstr_tag( DWORD tag )
 60 {
 61     return wine_dbg_sprintf( "'%c%c%c%c'",
 62         (char)(tag >> 24), (char)(tag >> 16), (char)(tag >> 8), (char)(tag) );
 63 }
 64 
 65 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
 66 
 67 /******************************************************************************
 68  * AssociateColorProfileWithDeviceA               [MSCMS.@]
 69  */
 70 BOOL WINAPI AssociateColorProfileWithDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
 71 {
 72     int len;
 73     BOOL ret = FALSE;
 74     WCHAR *profileW, *deviceW;
 75 
 76     TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );
 77 
 78     if (!profile || !device)
 79     {
 80         SetLastError( ERROR_INVALID_PARAMETER );
 81         return FALSE;
 82     }
 83     if (machine)
 84     {
 85         SetLastError( ERROR_NOT_SUPPORTED );
 86         return FALSE;
 87     }
 88 
 89     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
 90     if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
 91 
 92     MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
 93 
 94     len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
 95     if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
 96     {
 97         MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
 98         ret = AssociateColorProfileWithDeviceW( NULL, profileW, deviceW );
 99     }
100 
101     HeapFree( GetProcessHeap(), 0, profileW );
102     HeapFree( GetProcessHeap(), 0, deviceW );
103     return ret;
104 }
105 
106 static BOOL set_profile_device_key( PCWSTR file, const BYTE *value, DWORD size )
107 {
108     static const WCHAR fmtW[] = {'%','c','%','c','%','c','%','c',0};
109     static const WCHAR icmW[] = {'S','o','f','t','w','a','r','e','\\',
110                                  'M','i','c','r','o','s','o','f','t','\\',
111                                  'W','i','n','d','o','w','s',' ','N','T','\\',
112                                  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
113                                  'I','C','M',0};
114     PROFILEHEADER header;
115     PROFILE profile;
116     HPROFILE handle;
117     HKEY icm_key, class_key;
118     WCHAR basenameW[MAX_PATH], classW[5];
119 
120     profile.dwType = PROFILE_FILENAME;
121     profile.pProfileData = (PVOID)file;
122     profile.cbDataSize = (lstrlenW( file ) + 1) * sizeof(WCHAR);
123 
124     /* FIXME is the profile installed? */
125     if (!(handle = OpenColorProfileW( &profile, PROFILE_READ, 0, OPEN_EXISTING )))
126     {
127         SetLastError( ERROR_INVALID_PROFILE );
128         return FALSE;
129     }
130     if (!GetColorProfileHeader( handle, &header ))
131     {
132         CloseColorProfile( handle );
133         SetLastError( ERROR_INVALID_PROFILE );
134         return FALSE;
135     }
136     RegCreateKeyExW( HKEY_LOCAL_MACHINE, icmW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &icm_key, NULL );
137 
138     MSCMS_basename( file, basenameW );
139     sprintfW( classW, fmtW, (header.phClass >> 24) & 0xff, (header.phClass >> 16) & 0xff,
140                             (header.phClass >> 8) & 0xff,  header.phClass & 0xff );
141 
142     RegCreateKeyExW( icm_key, classW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &class_key, NULL );
143     if (value) RegSetValueExW( class_key, basenameW, 0, REG_BINARY, value, size );
144     else RegDeleteValueW( class_key, basenameW );
145 
146     RegCloseKey( class_key );
147     RegCloseKey( icm_key );
148     CloseColorProfile( handle );
149     return TRUE;
150 }
151 
152 /******************************************************************************
153  * AssociateColorProfileWithDeviceW               [MSCMS.@]
154  */
155 BOOL WINAPI AssociateColorProfileWithDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
156 {
157     static const BYTE dummy_value[12];
158 
159     TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );
160 
161     if (!profile || !device)
162     {
163         SetLastError( ERROR_INVALID_PARAMETER );
164         return FALSE;
165     }
166     if (machine)
167     {
168         SetLastError( ERROR_NOT_SUPPORTED );
169         return FALSE;
170     }
171 
172     return set_profile_device_key( profile, dummy_value, sizeof(dummy_value) );
173 }
174 
175 /******************************************************************************
176  * DisassociateColorProfileFromDeviceA            [MSCMS.@]
177  */
178 BOOL WINAPI DisassociateColorProfileFromDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
179 {
180     int len;
181     BOOL ret = FALSE;
182     WCHAR *profileW, *deviceW;
183 
184     TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );
185 
186     if (!profile || !device)
187     {
188         SetLastError( ERROR_INVALID_PARAMETER );
189         return FALSE;
190     }
191     if (machine)
192     {
193         SetLastError( ERROR_NOT_SUPPORTED );
194         return FALSE;
195     }
196 
197     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
198     if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
199 
200     MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
201 
202     len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
203     if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
204     {
205         MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
206         ret = DisassociateColorProfileFromDeviceW( NULL, profileW, deviceW );
207     }
208 
209     HeapFree( GetProcessHeap(), 0, profileW );
210     HeapFree( GetProcessHeap(), 0, deviceW );
211     return ret;
212 }
213 
214 /******************************************************************************
215  * DisassociateColorProfileFromDeviceW            [MSCMS.@]
216  */
217 BOOL WINAPI DisassociateColorProfileFromDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
218 {
219     TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );
220 
221     if (!profile || !device)
222     {
223         SetLastError( ERROR_INVALID_PARAMETER );
224         return FALSE;
225     }
226     if (machine)
227     {
228         SetLastError( ERROR_NOT_SUPPORTED );
229         return FALSE;
230     }
231 
232     return set_profile_device_key( profile, NULL, 0 );
233 }
234 
235 /******************************************************************************
236  * GetColorDirectoryA               [MSCMS.@]
237  *
238  * See GetColorDirectoryW.
239  */
240 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
241 {
242     INT len;
243     LPWSTR bufferW;
244     BOOL ret = FALSE;
245     DWORD sizeW;
246 
247     TRACE( "( %p, %p )\n", buffer, size );
248 
249     if (machine || !size) return FALSE;
250 
251     if (!buffer)
252     {
253         ret = GetColorDirectoryW( NULL, NULL, &sizeW );
254         *size = sizeW / sizeof(WCHAR);
255         return FALSE;
256     }
257 
258     sizeW = *size * sizeof(WCHAR);
259 
260     bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
261 
262     if (bufferW)
263     {
264         if ((ret = GetColorDirectoryW( NULL, bufferW, &sizeW )))
265         {
266             *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
267             len = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, *size, NULL, NULL );
268             if (!len) ret = FALSE;
269         }
270         else *size = sizeW / sizeof(WCHAR);
271 
272         HeapFree( GetProcessHeap(), 0, bufferW );
273     }
274     return ret;
275 }
276 
277 /******************************************************************************
278  * GetColorDirectoryW               [MSCMS.@]
279  *
280  * Get the directory where color profiles are stored.
281  *
282  * PARAMS
283  *  machine  [I]   Name of the machine for which to get the color directory.
284  *                 Must be NULL, which indicates the local machine.
285  *  buffer   [I]   Buffer to receive the path name.
286  *  size     [I/O] Size of the buffer in bytes. On return the variable holds
287  *                 the number of bytes actually needed.
288  */
289 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
290 {
291     WCHAR colordir[MAX_PATH];
292     static const WCHAR colorsubdir[] =
293         {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r',0};
294     DWORD len;
295 
296     TRACE( "( %p, %p )\n", buffer, size );
297 
298     if (machine || !size) return FALSE;
299 
300     GetSystemDirectoryW( colordir, sizeof(colordir) / sizeof(WCHAR) );
301     lstrcatW( colordir, colorsubdir );
302 
303     len = lstrlenW( colordir ) * sizeof(WCHAR);
304 
305     if (buffer && len <= *size)
306     {
307         lstrcpyW( buffer, colordir );
308         *size = len;
309         return TRUE;
310     }
311 
312     SetLastError( ERROR_MORE_DATA );
313     *size = len;
314     return FALSE;
315 }
316 
317 /******************************************************************************
318  * GetColorProfileElement               [MSCMS.@]
319  *
320  * Retrieve data for a specified tag type.
321  *
322  * PARAMS
323  *  profile  [I]   Handle to a color profile.
324  *  type     [I]   ICC tag type. 
325  *  offset   [I]   Offset in bytes to start copying from. 
326  *  size     [I/O] Size of the buffer in bytes. On return the variable holds
327  *                 the number of bytes actually needed.
328  *  buffer   [O]   Buffer to receive the tag data.
329  *  ref      [O]   Pointer to a BOOL that specifies whether more than one tag
330  *                 references the data.
331  *
332  * RETURNS
333  *  Success: TRUE
334  *  Failure: FALSE
335  */
336 BOOL WINAPI GetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
337                                     PVOID buffer, PBOOL ref )
338 {
339     BOOL ret = FALSE;
340 #ifdef HAVE_LCMS
341     struct profile *profile = grab_profile( handle );
342     DWORD i, count;
343     icTag tag;
344 
345     TRACE( "( %p, 0x%08x, %d, %p, %p, %p )\n", handle, type, offset, size, buffer, ref );
346 
347     if (!profile) return FALSE;
348 
349     if (!size || !ref)
350     {
351         release_profile( profile );
352         return FALSE;
353     }
354     count = MSCMS_get_tag_count( profile->iccprofile );
355 
356     for (i = 0; i < count; i++)
357     {
358         MSCMS_get_tag_by_index( profile->iccprofile, i, &tag );
359 
360         if (tag.sig == type)
361         {
362             if ((tag.size - offset) > *size || !buffer)
363             {
364                 *size = (tag.size - offset);
365                 release_profile( profile );
366                 return FALSE;
367             }
368             MSCMS_get_tag_data( profile->iccprofile, &tag, offset, buffer );
369 
370             *ref = FALSE; /* FIXME: calculate properly */
371             release_profile( profile );
372             return TRUE;
373         }
374     }
375     release_profile( profile );
376 
377 #endif /* HAVE_LCMS */
378     return ret;
379 }
380 
381 /******************************************************************************
382  * GetColorProfileElementTag               [MSCMS.@]
383  *
384  * Get the tag type from a color profile by index. 
385  *
386  * PARAMS
387  *  profile  [I]   Handle to a color profile.
388  *  index    [I]   Index into the tag table of the color profile.
389  *  type     [O]   Pointer to a variable that holds the ICC tag type on return.
390  *
391  * RETURNS
392  *  Success: TRUE
393  *  Failure: FALSE
394  *
395  * NOTES
396  *  The tag table index starts at 1.
397  *  Use GetCountColorProfileElements to retrieve a count of tagged elements.
398  */
399 BOOL WINAPI GetColorProfileElementTag( HPROFILE handle, DWORD index, PTAGTYPE type )
400 {
401     BOOL ret = FALSE;
402 #ifdef HAVE_LCMS
403     struct profile *profile = grab_profile( handle );
404     DWORD count;
405     icTag tag;
406 
407     TRACE( "( %p, %d, %p )\n", handle, index, type );
408 
409     if (!profile) return FALSE;
410 
411     if (!type)
412     {
413         release_profile( profile );
414         return FALSE;
415     }
416     count = MSCMS_get_tag_count( profile->iccprofile );
417     if (index > count || index < 1)
418     {
419         release_profile( profile );
420         return FALSE;
421     }
422     MSCMS_get_tag_by_index( profile->iccprofile, index - 1, &tag );
423     *type = tag.sig;
424 
425     release_profile( profile );
426     ret = TRUE;
427 
428 #endif /* HAVE_LCMS */
429     return ret;
430 }
431 
432 /******************************************************************************
433  * GetColorProfileFromHandle               [MSCMS.@]
434  *
435  * Retrieve an ICC color profile by handle.
436  *
437  * PARAMS
438  *  profile  [I]   Handle to a color profile.
439  *  buffer   [O]   Buffer to receive the ICC profile.
440  *  size     [I/O] Size of the buffer in bytes. On return the variable holds the
441  *                 number of bytes actually needed.
442  *
443  * RETURNS
444  *  Success: TRUE
445  *  Failure: FALSE
446  *
447  * NOTES
448  *  The profile returned will be in big-endian format.
449  */
450 BOOL WINAPI GetColorProfileFromHandle( HPROFILE handle, PBYTE buffer, PDWORD size )
451 {
452     BOOL ret = FALSE;
453 #ifdef HAVE_LCMS
454     struct profile *profile = grab_profile( handle );
455     PROFILEHEADER header;
456 
457     TRACE( "( %p, %p, %p )\n", handle, buffer, size );
458 
459     if (!profile) return FALSE;
460 
461     if (!size)
462     {
463         release_profile( profile );
464         return FALSE;
465     }
466     MSCMS_get_profile_header( profile->iccprofile, &header );
467 
468     if (!buffer || header.phSize > *size)
469     {
470         *size = header.phSize;
471         release_profile( profile );
472         return FALSE;
473     }
474 
475     /* No endian conversion needed */
476     memcpy( buffer, profile->iccprofile, header.phSize );
477     *size = header.phSize;
478 
479     release_profile( profile );
480     ret = TRUE;
481 
482 #endif /* HAVE_LCMS */
483     return ret;
484 }
485 
486 /******************************************************************************
487  * GetColorProfileHeader               [MSCMS.@]
488  *
489  * Retrieve a color profile header by handle.
490  *
491  * PARAMS
492  *  profile  [I]   Handle to a color profile.
493  *  header   [O]   Buffer to receive the ICC profile header.
494  *
495  * RETURNS
496  *  Success: TRUE
497  *  Failure: FALSE
498  *
499  * NOTES
500  *  The profile header returned will be adjusted for endianess.
501  */
502 BOOL WINAPI GetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
503 {
504 #ifdef HAVE_LCMS
505     struct profile *profile = grab_profile( handle );
506 
507     TRACE( "( %p, %p )\n", handle, header );
508 
509     if (!profile) return FALSE;
510 
511     if (!header)
512     {
513         release_profile( profile );
514         return FALSE;
515     }
516     MSCMS_get_profile_header( profile->iccprofile, header );
517 
518     release_profile( profile );
519     return TRUE;
520 
521 #else
522     return FALSE;
523 #endif /* HAVE_LCMS */
524 }
525 
526 /******************************************************************************
527  * GetCountColorProfileElements               [MSCMS.@]
528  *
529  * Retrieve the number of elements in a color profile.
530  *
531  * PARAMS
532  *  profile  [I] Handle to a color profile.
533  *  count    [O] Pointer to a variable which is set to the number of elements
534  *               in the color profile.
535  *
536  * RETURNS
537  *  Success: TRUE
538  *  Failure: FALSE
539  */
540 BOOL WINAPI GetCountColorProfileElements( HPROFILE handle, PDWORD count )
541 {
542     BOOL ret = FALSE;
543 #ifdef HAVE_LCMS
544     struct profile *profile = grab_profile( handle );
545 
546     TRACE( "( %p, %p )\n", handle, count );
547 
548     if (!profile) return FALSE;
549 
550     if (!count)
551     {
552         release_profile( profile );
553         return FALSE;
554     }
555     *count = MSCMS_get_tag_count( profile->iccprofile );
556 
557     release_profile( profile );
558     ret = TRUE;
559 
560 #endif /* HAVE_LCMS */
561     return ret;
562 }
563 
564 /******************************************************************************
565  * GetStandardColorSpaceProfileA               [MSCMS.@]
566  *
567  * See GetStandardColorSpaceProfileW.
568  */
569 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
570 {
571     INT len;
572     LPWSTR profileW;
573     BOOL ret = FALSE;
574     DWORD sizeW;
575 
576     TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
577 
578     if (machine) 
579     {
580         SetLastError( ERROR_NOT_SUPPORTED );
581         return FALSE;
582     }
583 
584     if (!size) 
585     {
586         SetLastError( ERROR_INVALID_PARAMETER );
587         return FALSE;
588     }
589 
590     sizeW = *size * sizeof(WCHAR);
591 
592     if (!profile)
593     {
594         ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
595         *size = sizeW / sizeof(WCHAR);
596         return FALSE;
597     }
598 
599     profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
600 
601     if (profileW)
602     {
603         if ((ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW )))
604         {
605             *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
606             len = WideCharToMultiByte( CP_ACP, 0, profileW, -1, profile, *size, NULL, NULL );
607             if (!len) ret = FALSE;
608         }
609         else *size = sizeW / sizeof(WCHAR);
610 
611         HeapFree( GetProcessHeap(), 0, profileW );
612     }
613     return ret;
614 }
615 
616 /******************************************************************************
617  * GetStandardColorSpaceProfileW               [MSCMS.@]
618  *
619  * Retrieve the profile filename for a given standard color space id.
620  *
621  * PARAMS
622  *  machine  [I]   Name of the machine for which to get the standard color space.
623  *                 Must be NULL, which indicates the local machine.
624  *  id       [I]   Id of a standard color space.
625  *  profile  [O]   Buffer to receive the profile filename.
626  *  size     [I/O] Size of the filename buffer in bytes.
627  *
628  * RETURNS
629  *  Success: TRUE
630  *  Failure: FALSE
631  */
632 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
633 {
634     static const WCHAR rgbprofilefile[] =
635         { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
636           's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
637     WCHAR rgbprofile[MAX_PATH];
638     DWORD len = sizeof(rgbprofile);
639 
640     TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
641 
642     if (machine) 
643     {
644         SetLastError( ERROR_NOT_SUPPORTED );
645         return FALSE;
646     }
647 
648     if (!size) 
649     {
650         SetLastError( ERROR_INVALID_PARAMETER );
651         return FALSE;
652     }
653 
654     if (!profile)
655     {
656         SetLastError( ERROR_INSUFFICIENT_BUFFER );
657         return FALSE;
658     }
659 
660     GetColorDirectoryW( machine, rgbprofile, &len );
661 
662     switch (id)
663     {
664         case SPACE_RGB: /* 'RGB ' */
665             lstrcatW( rgbprofile, rgbprofilefile );
666             len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
667 
668             if (*size < len || !profile)
669             {
670                 *size = len;
671                 SetLastError( ERROR_MORE_DATA );
672                 return FALSE;
673             }
674 
675             lstrcpyW( profile, rgbprofile );
676             break;
677 
678         default:
679             SetLastError( ERROR_FILE_NOT_FOUND );
680             return FALSE;
681     }
682     return TRUE;
683 }
684 
685 static BOOL MSCMS_header_from_file( LPCWSTR file, PPROFILEHEADER header )
686 {
687     BOOL ret;
688     PROFILE profile;
689     WCHAR path[MAX_PATH], slash[] = {'\\',0};
690     DWORD size = sizeof(path);
691     HANDLE handle;
692 
693     ret = GetColorDirectoryW( NULL, path, &size );
694     if (!ret)
695     {
696         WARN( "Can't retrieve color directory\n" );
697         return FALSE;
698     }
699     if (size + sizeof(slash) + sizeof(WCHAR) * lstrlenW( file ) > sizeof(path))
700     {
701         WARN( "Filename too long\n" );
702         return FALSE;
703     }
704 
705     lstrcatW( path, slash );
706     lstrcatW( path, file );
707 
708     profile.dwType = PROFILE_FILENAME;
709     profile.pProfileData = path;
710     profile.cbDataSize = lstrlenW( path ) + 1;
711 
712     handle = OpenColorProfileW( &profile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );
713     if (!handle)
714     {
715         WARN( "Can't open color profile\n" );
716         return FALSE;
717     }
718 
719     ret = GetColorProfileHeader( handle, header );
720     if (!ret)
721         WARN( "Can't retrieve color profile header\n" );
722 
723     CloseColorProfile( handle );
724     return ret;
725 }
726 
727 static BOOL MSCMS_match_profile( PENUMTYPEW rec, PPROFILEHEADER hdr )
728 {
729     if (rec->dwFields & ET_DEVICENAME)
730     {
731         FIXME( "ET_DEVICENAME: %s\n", debugstr_w(rec->pDeviceName) );
732     }
733     if (rec->dwFields & ET_MEDIATYPE)
734     {
735         FIXME( "ET_MEDIATYPE: 0x%08x\n", rec->dwMediaType );
736     }
737     if (rec->dwFields & ET_DITHERMODE)
738     {
739         FIXME( "ET_DITHERMODE: 0x%08x\n", rec->dwDitheringMode );
740     }
741     if (rec->dwFields & ET_RESOLUTION)
742     {
743         FIXME( "ET_RESOLUTION: 0x%08x, 0x%08x\n",
744                rec->dwResolution[0], rec->dwResolution[1] );
745     }
746     if (rec->dwFields & ET_DEVICECLASS)
747     {
748         FIXME( "ET_DEVICECLASS: %s\n", MSCMS_dbgstr_tag(rec->dwMediaType) );
749     }
750     if (rec->dwFields & ET_CMMTYPE)
751     {
752         TRACE( "ET_CMMTYPE: %s\n", MSCMS_dbgstr_tag(rec->dwCMMType) );
753         if (rec->dwCMMType != hdr->phCMMType) return FALSE;
754     }
755     if (rec->dwFields & ET_CLASS)
756     {
757         TRACE( "ET_CLASS: %s\n", MSCMS_dbgstr_tag(rec->dwClass) );
758         if (rec->dwClass != hdr->phClass) return FALSE;
759     }
760     if (rec->dwFields & ET_DATACOLORSPACE)
761     {
762         TRACE( "ET_DATACOLORSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwDataColorSpace) );
763         if (rec->dwDataColorSpace != hdr->phDataColorSpace) return FALSE;
764     }
765     if (rec->dwFields & ET_CONNECTIONSPACE)
766     {
767         TRACE( "ET_CONNECTIONSPACE: %s\n", MSCMS_dbgstr_tag(rec->dwConnectionSpace) );
768         if (rec->dwConnectionSpace != hdr->phConnectionSpace) return FALSE;
769     }
770     if (rec->dwFields & ET_SIGNATURE)
771     {
772         TRACE( "ET_SIGNATURE: %s\n", MSCMS_dbgstr_tag(rec->dwSignature) );
773         if (rec->dwSignature != hdr->phSignature) return FALSE;
774     }
775     if (rec->dwFields & ET_PLATFORM)
776     {
777         TRACE( "ET_PLATFORM: %s\n", MSCMS_dbgstr_tag(rec->dwPlatform) );
778         if (rec->dwPlatform != hdr->phPlatform) return FALSE;
779     }
780     if (rec->dwFields & ET_PROFILEFLAGS)
781     {
782         TRACE( "ET_PROFILEFLAGS: 0x%08x\n", rec->dwProfileFlags );
783         if (rec->dwProfileFlags != hdr->phProfileFlags) return FALSE;
784     }
785     if (rec->dwFields & ET_MANUFACTURER)
786     {
787         TRACE( "ET_MANUFACTURER: %s\n", MSCMS_dbgstr_tag(rec->dwManufacturer) );
788         if (rec->dwManufacturer != hdr->phManufacturer) return FALSE;
789     }
790     if (rec->dwFields & ET_MODEL)
791     {
792         TRACE( "ET_MODEL: %s\n", MSCMS_dbgstr_tag(rec->dwModel) );
793         if (rec->dwModel != hdr->phModel) return FALSE;
794     }
795     if (rec->dwFields & ET_ATTRIBUTES)
796     {
797         TRACE( "ET_ATTRIBUTES: 0x%08x, 0x%08x\n",
798                rec->dwAttributes[0], rec->dwAttributes[1] );
799         if (rec->dwAttributes[0] != hdr->phAttributes[0] || 
800             rec->dwAttributes[1] != hdr->phAttributes[1]) return FALSE;
801     }
802     if (rec->dwFields & ET_RENDERINGINTENT)
803     {
804         TRACE( "ET_RENDERINGINTENT: 0x%08x\n", rec->dwRenderingIntent );
805         if (rec->dwRenderingIntent != hdr->phRenderingIntent) return FALSE;
806     }
807     if (rec->dwFields & ET_CREATOR)
808     {
809         TRACE( "ET_CREATOR: %s\n", MSCMS_dbgstr_tag(rec->dwCreator) );
810         if (rec->dwCreator != hdr->phCreator) return FALSE;
811     }
812     return TRUE;
813 }
814 
815 /******************************************************************************
816  * EnumColorProfilesA               [MSCMS.@]
817  *
818  * See EnumColorProfilesW.
819  */
820 BOOL WINAPI EnumColorProfilesA( PCSTR machine, PENUMTYPEA record, PBYTE buffer,
821                                 PDWORD size, PDWORD number )
822 {
823     BOOL match, ret = FALSE;
824     char spec[] = "\\*.icm";
825     char colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
826     DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
827     PROFILEHEADER header;
828     WIN32_FIND_DATAA data;
829     ENUMTYPEW recordW;
830     WCHAR *fileW = NULL, *deviceW = NULL;
831     HANDLE find;
832 
833     TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
834 
835     if (machine || !record || !size ||
836         record->dwSize != sizeof(ENUMTYPEA) ||
837         record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
838 
839     ret = GetColorDirectoryA( machine, colordir, &len );
840     if (!ret || len + sizeof(spec) > MAX_PATH)
841     {
842         WARN( "can't retrieve color directory\n" );
843         return FALSE;
844     }
845 
846     lstrcpyA( glob, colordir );
847     lstrcatA( glob, spec );
848 
849     find = FindFirstFileA( glob, &data );
850     if (find == INVALID_HANDLE_VALUE) return FALSE;
851 
852     profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(char *) + 1 );
853     if (!profiles) goto exit;
854 
855     memcpy( &recordW, record, sizeof(ENUMTYPEA) );
856     if (record->pDeviceName)
857     {
858         deviceW = MSCMS_strdupW( record->pDeviceName );
859         if (!(recordW.pDeviceName = deviceW)) goto exit;
860     }
861 
862     fileW = MSCMS_strdupW( data.cFileName );
863     if (!fileW) goto exit;
864 
865     ret = MSCMS_header_from_file( fileW, &header );
866     if (ret)
867     {
868         match = MSCMS_match_profile( &recordW, &header );
869         if (match)
870         {
871             len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
872             profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
873 
874             if (!profiles[count]) goto exit;
875             else
876             {
877                 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
878                 lstrcpyA( profiles[count], data.cFileName );
879                 totalsize += len;
880                 count++;
881             }
882         }
883     }
884     HeapFree( GetProcessHeap(), 0, fileW );
885     fileW = NULL;
886 
887     while (FindNextFileA( find, &data ))
888     {
889         fileW = MSCMS_strdupW( data.cFileName );
890         if (!fileW) goto exit;
891 
892         ret = MSCMS_header_from_file( fileW, &header );
893         if (!ret)
894         {
895             HeapFree( GetProcessHeap(), 0, fileW );
896             continue;
897         }
898 
899         match = MSCMS_match_profile( &recordW, &header );
900         if (match)
901         {
902             char **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
903                                       profiles, sizeof(char *) * (count + 1) );
904             if (!tmp) goto exit;
905             else profiles = tmp;
906 
907             len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
908             profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
909 
910             if (!profiles[count]) goto exit;
911             else
912             {
913                 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
914                 lstrcpyA( profiles[count], data.cFileName );
915                 totalsize +=