profile.c 43.2 KB
Newer Older
1 2 3
/*
 * MSCMS - Color Management System for Wine
 *
4
 * Copyright 2004, 2005, 2006, 2008 Hans Leidekker
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22
 */

#include "config.h"
#include "wine/debug.h"
23
#include "wine/unicode.h"
24 25 26 27 28 29 30

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "wingdi.h"
31
#include "winuser.h"
32
#include "winreg.h"
33
#include "shlwapi.h"
34 35
#include "icm.h"

36
#include "mscms_priv.h"
37

38
static void basename( LPCWSTR path, LPWSTR name )
39 40 41
{
    INT i = lstrlenW( path );

42
    while (i > 0 && path[i - 1] != '\\' && path[i - 1] != '/') i--;
43 44 45
    lstrcpyW( name, &path[i] );
}

46
static inline LPWSTR strdupW( LPCSTR str )
47 48 49 50 51 52 53 54 55 56 57
{
    LPWSTR ret = NULL;
    if (str)
    {
        DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
        if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
            MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
    }
    return ret;
}

58
const char *dbgstr_tag( DWORD tag )
59 60 61 62 63
{
    return wine_dbg_sprintf( "'%c%c%c%c'",
        (char)(tag >> 24), (char)(tag >> 16), (char)(tag >> 8), (char)(tag) );
}

64 65
WINE_DEFAULT_DEBUG_CHANNEL(mscms);

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/******************************************************************************
 * AssociateColorProfileWithDeviceA               [MSCMS.@]
 */
BOOL WINAPI AssociateColorProfileWithDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
{
    int len;
    BOOL ret = FALSE;
    WCHAR *profileW, *deviceW;

    TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );

    if (!profile || !device)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }
    if (machine)
    {
        SetLastError( ERROR_NOT_SUPPORTED );
        return FALSE;
    }

    len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
    if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;

    MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );

    len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
    if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
    {
        MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
        ret = AssociateColorProfileWithDeviceW( NULL, profileW, deviceW );
    }

    HeapFree( GetProcessHeap(), 0, profileW );
    HeapFree( GetProcessHeap(), 0, deviceW );
    return ret;
}

static BOOL set_profile_device_key( PCWSTR file, const BYTE *value, DWORD size )
{
    static const WCHAR fmtW[] = {'%','c','%','c','%','c','%','c',0};
    static const WCHAR icmW[] = {'S','o','f','t','w','a','r','e','\\',
                                 'M','i','c','r','o','s','o','f','t','\\',
                                 'W','i','n','d','o','w','s',' ','N','T','\\',
                                 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
                                 'I','C','M',0};
    PROFILEHEADER header;
    PROFILE profile;
    HPROFILE handle;
    HKEY icm_key, class_key;
    WCHAR basenameW[MAX_PATH], classW[5];

    profile.dwType = PROFILE_FILENAME;
    profile.pProfileData = (PVOID)file;
    profile.cbDataSize = (lstrlenW( file ) + 1) * sizeof(WCHAR);

    /* FIXME is the profile installed? */
    if (!(handle = OpenColorProfileW( &profile, PROFILE_READ, 0, OPEN_EXISTING )))
    {
        SetLastError( ERROR_INVALID_PROFILE );
        return FALSE;
    }
129 130 131 132 133 134
    if (!GetColorProfileHeader( handle, &header ))
    {
        CloseColorProfile( handle );
        SetLastError( ERROR_INVALID_PROFILE );
        return FALSE;
    }
135 136
    RegCreateKeyExW( HKEY_LOCAL_MACHINE, icmW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &icm_key, NULL );

137
    basename( file, basenameW );
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    sprintfW( classW, fmtW, (header.phClass >> 24) & 0xff, (header.phClass >> 16) & 0xff,
                            (header.phClass >> 8) & 0xff,  header.phClass & 0xff );

    RegCreateKeyExW( icm_key, classW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &class_key, NULL );
    if (value) RegSetValueExW( class_key, basenameW, 0, REG_BINARY, value, size );
    else RegDeleteValueW( class_key, basenameW );

    RegCloseKey( class_key );
    RegCloseKey( icm_key );
    CloseColorProfile( handle );
    return TRUE;
}

/******************************************************************************
 * AssociateColorProfileWithDeviceW               [MSCMS.@]
 */
BOOL WINAPI AssociateColorProfileWithDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
{
    static const BYTE dummy_value[12];

    TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );

    if (!profile || !device)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }
    if (machine)
    {
        SetLastError( ERROR_NOT_SUPPORTED );
        return FALSE;
    }

    return set_profile_device_key( profile, dummy_value, sizeof(dummy_value) );
}

/******************************************************************************
 * DisassociateColorProfileFromDeviceA            [MSCMS.@]
 */
BOOL WINAPI DisassociateColorProfileFromDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
{
    int len;
    BOOL ret = FALSE;
    WCHAR *profileW, *deviceW;

    TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );

    if (!profile || !device)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }
    if (machine)
    {
        SetLastError( ERROR_NOT_SUPPORTED );
        return FALSE;
    }

    len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
    if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;

    MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );

    len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
    if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
    {
        MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
        ret = DisassociateColorProfileFromDeviceW( NULL, profileW, deviceW );
    }

    HeapFree( GetProcessHeap(), 0, profileW );
    HeapFree( GetProcessHeap(), 0, deviceW );
    return ret;
}

/******************************************************************************
 * DisassociateColorProfileFromDeviceW            [MSCMS.@]
 */
BOOL WINAPI DisassociateColorProfileFromDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
{
    TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );

    if (!profile || !device)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }
    if (machine)
    {
        SetLastError( ERROR_NOT_SUPPORTED );
        return FALSE;
    }

    return set_profile_device_key( profile, NULL, 0 );
}

234 235 236 237 238
/******************************************************************************
 * GetColorDirectoryA               [MSCMS.@]
 *
 * See GetColorDirectoryW.
 */
239 240 241 242 243
BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
{
    INT len;
    LPWSTR bufferW;
    BOOL ret = FALSE;
244 245 246 247 248
    DWORD sizeW;

    TRACE( "( %p, %p )\n", buffer, size );

    if (machine || !size) return FALSE;
249

250 251 252 253
    if (!buffer)
    {
        ret = GetColorDirectoryW( NULL, NULL, &sizeW );
        *size = sizeW / sizeof(WCHAR);
254
        return ret;
255
    }
256

257
    sizeW = *size * sizeof(WCHAR);
258 259 260 261

    bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
    if (bufferW)
    {
262
        if ((ret = GetColorDirectoryW( NULL, bufferW, &sizeW )))
263
        {
264 265
            *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
            len = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, *size, NULL, NULL );
266 267
            if (!len) ret = FALSE;
        }
268
        else *size = sizeW / sizeof(WCHAR);
269 270 271 272 273 274

        HeapFree( GetProcessHeap(), 0, bufferW );
    }
    return ret;
}

275 276 277 278 279 280 281 282
/******************************************************************************
 * GetColorDirectoryW               [MSCMS.@]
 *
 * Get the directory where color profiles are stored.
 *
 * PARAMS
 *  machine  [I]   Name of the machine for which to get the color directory.
 *                 Must be NULL, which indicates the local machine.
283
 *  buffer   [I]   Buffer to receive the path name.
284 285 286
 *  size     [I/O] Size of the buffer in bytes. On return the variable holds
 *                 the number of bytes actually needed.
 */
287 288
BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
{
289
    WCHAR colordir[MAX_PATH];
290 291
    static const WCHAR colorsubdir[] =
        {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r',0};
292 293
    DWORD len;

294
    TRACE( "( %p, %p )\n", buffer, size );
295

296
    if (machine || !size) return FALSE;
297

298
    GetSystemDirectoryW( colordir, sizeof(colordir) / sizeof(WCHAR) );
299 300
    lstrcatW( colordir, colorsubdir );

301 302
    len = lstrlenW( colordir ) * sizeof(WCHAR);

303
    if (buffer && len <= *size)
304
    {
305
        lstrcpyW( buffer, colordir );
306
        *size = len;
307 308 309
        return TRUE;
    }

310
    SetLastError( ERROR_MORE_DATA );
311 312 313 314
    *size = len;
    return FALSE;
}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
/******************************************************************************
 * GetColorProfileElement               [MSCMS.@]
 *
 * Retrieve data for a specified tag type.
 *
 * PARAMS
 *  profile  [I]   Handle to a color profile.
 *  type     [I]   ICC tag type. 
 *  offset   [I]   Offset in bytes to start copying from. 
 *  size     [I/O] Size of the buffer in bytes. On return the variable holds
 *                 the number of bytes actually needed.
 *  buffer   [O]   Buffer to receive the tag data.
 *  ref      [O]   Pointer to a BOOL that specifies whether more than one tag
 *                 references the data.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
334
BOOL WINAPI GetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
335 336 337
                                    PVOID buffer, PBOOL ref )
{
    BOOL ret = FALSE;
338
#ifdef HAVE_LCMS2
339
    struct profile *profile = grab_profile( handle );
340

341
    TRACE( "( %p, 0x%08x, %d, %p, %p, %p )\n", handle, type, offset, size, buffer, ref );
342

343 344 345 346 347 348 349
    if (!profile) return FALSE;

    if (!size || !ref)
    {
        release_profile( profile );
        return FALSE;
    }
350
    if (!get_tag_data( profile, type, offset, buffer, size ))
351
    {
352 353
        release_profile( profile );
        return FALSE;
354
    }
355 356
    ret = get_tag_data( profile, type, offset, buffer, size );
    *ref = cmsTagLinkedTo( profile->cmsprofile, type ) != 0;
357
    release_profile( profile );
358
#endif /* HAVE_LCMS2 */
359 360 361
    return ret;
}

362 363 364
/******************************************************************************
 * GetColorProfileElementTag               [MSCMS.@]
 *
365
 * Get the tag type from a color profile by index. 
366 367 368 369
 *
 * PARAMS
 *  profile  [I]   Handle to a color profile.
 *  index    [I]   Index into the tag table of the color profile.
370
 *  type     [O]   Pointer to a variable that holds the ICC tag type on return.
371 372 373 374 375 376 377 378 379
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
 *  The tag table index starts at 1.
 *  Use GetCountColorProfileElements to retrieve a count of tagged elements.
 */
380
BOOL WINAPI GetColorProfileElementTag( HPROFILE handle, DWORD index, PTAGTYPE type )
381 382
{
    BOOL ret = FALSE;
383
#ifdef HAVE_LCMS2
384
    struct profile *profile = grab_profile( handle );
385 386
    cmsInt32Number num_tags;
    cmsTagSignature sig;
387

388
    TRACE( "( %p, %d, %p )\n", handle, index, type );
389

390
    if (!profile) return FALSE;
391

392 393 394 395 396
    if (!type)
    {
        release_profile( profile );
        return FALSE;
    }
397 398
    num_tags = cmsGetTagCount( profile->cmsprofile );
    if (num_tags < 0 || index > num_tags || index < 1)
399 400 401 402
    {
        release_profile( profile );
        return FALSE;
    }
403 404 405 406 407
    if ((sig = cmsGetTagSignature( profile->cmsprofile, index - 1 )))
    {
        *type = sig;
        ret = TRUE;
    }
408
    release_profile( profile );
409

410
#endif /* HAVE_LCMS2 */
411 412 413
    return ret;
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
/******************************************************************************
 * GetColorProfileFromHandle               [MSCMS.@]
 *
 * Retrieve an ICC color profile by handle.
 *
 * PARAMS
 *  profile  [I]   Handle to a color profile.
 *  buffer   [O]   Buffer to receive the ICC profile.
 *  size     [I/O] Size of the buffer in bytes. On return the variable holds the
 *                 number of bytes actually needed.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
 *  The profile returned will be in big-endian format.
 */
432
BOOL WINAPI GetColorProfileFromHandle( HPROFILE handle, PBYTE buffer, PDWORD size )
433 434
{
    BOOL ret = FALSE;
435
#ifdef HAVE_LCMS2
436
    struct profile *profile = grab_profile( handle );
437 438
    PROFILEHEADER header;

439
    TRACE( "( %p, %p, %p )\n", handle, buffer, size );
440

441 442 443 444 445 446 447
    if (!profile) return FALSE;

    if (!size)
    {
        release_profile( profile );
        return FALSE;
    }
448
    get_profile_header( profile, &header );
449 450

    if (!buffer || header.phSize > *size)
451
    {
452
        *size = header.phSize;
453
        release_profile( profile );
454
        return FALSE;
455 456
    }

457
    /* No endian conversion needed */
458 459
    memcpy( buffer, profile->data, profile->size );
    *size = profile->size;
460 461

    release_profile( profile );
462 463
    ret = TRUE;

464
#endif /* HAVE_LCMS2 */
465 466 467
    return ret;
}

468 469 470 471 472 473 474 475 476 477 478 479 480 481
/******************************************************************************
 * GetColorProfileHeader               [MSCMS.@]
 *
 * Retrieve a color profile header by handle.
 *
 * PARAMS
 *  profile  [I]   Handle to a color profile.
 *  header   [O]   Buffer to receive the ICC profile header.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
482
 *  The profile header returned will be adjusted for endianness.
483
 */
484
BOOL WINAPI GetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
485
{
486
#ifdef HAVE_LCMS2
487 488 489
    struct profile *profile = grab_profile( handle );

    TRACE( "( %p, %p )\n", handle, header );
490

491
    if (!profile) return FALSE;
492

493 494 495 496 497
    if (!header)
    {
        release_profile( profile );
        return FALSE;
    }
498
    get_profile_header( profile, header );
499
    release_profile( profile );
500 501
    return TRUE;

502 503
#else
    return FALSE;
504
#endif /* HAVE_LCMS2 */
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
}

/******************************************************************************
 * GetCountColorProfileElements               [MSCMS.@]
 *
 * Retrieve the number of elements in a color profile.
 *
 * PARAMS
 *  profile  [I] Handle to a color profile.
 *  count    [O] Pointer to a variable which is set to the number of elements
 *               in the color profile.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
521
BOOL WINAPI GetCountColorProfileElements( HPROFILE handle, PDWORD count )
522 523
{
    BOOL ret = FALSE;
524
#ifdef HAVE_LCMS2
525
    struct profile *profile = grab_profile( handle );
526
    cmsInt32Number num_tags;
527

528
    TRACE( "( %p, %p )\n", handle, count );
529

530 531 532 533 534 535 536
    if (!profile) return FALSE;

    if (!count)
    {
        release_profile( profile );
        return FALSE;
    }
537 538 539 540 541
    if ((num_tags = cmsGetTagCount( profile->cmsprofile )) >= 0)
    {
        *count = num_tags;
        ret = TRUE;
    }
542
    release_profile( profile );
543

544
#endif /* HAVE_LCMS2 */
545 546 547
    return ret;
}

548 549 550 551 552
/******************************************************************************
 * GetStandardColorSpaceProfileA               [MSCMS.@]
 *
 * See GetStandardColorSpaceProfileW.
 */
553 554 555 556 557
BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
{
    INT len;
    LPWSTR profileW;
    BOOL ret = FALSE;
558
    DWORD sizeW;
559

560
    TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
561

562 563
    if (machine) 
    {
564
        SetLastError( ERROR_NOT_SUPPORTED );
565 566 567 568 569
        return FALSE;
    }

    if (!size) 
    {
570
        SetLastError( ERROR_INVALID_PARAMETER );
571 572
        return FALSE;
    }
573 574 575 576 577 578 579

    sizeW = *size * sizeof(WCHAR);

    if (!profile)
    {
        ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
        *size = sizeW / sizeof(WCHAR);
580
        return ret;
581
    }
582 583 584 585

    profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
    if (profileW)
    {
586
        if ((ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW )))
587
        {
588 589
            *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
            len = WideCharToMultiByte( CP_ACP, 0, profileW, -1, profile, *size, NULL, NULL );
590 591
            if (!len) ret = FALSE;
        }
592
        else *size = sizeW / sizeof(WCHAR);
593 594 595 596 597 598

        HeapFree( GetProcessHeap(), 0, profileW );
    }
    return ret;
}

599 600 601 602 603 604 605 606 607
/******************************************************************************
 * GetStandardColorSpaceProfileW               [MSCMS.@]
 *
 * Retrieve the profile filename for a given standard color space id.
 *
 * PARAMS
 *  machine  [I]   Name of the machine for which to get the standard color space.
 *                 Must be NULL, which indicates the local machine.
 *  id       [I]   Id of a standard color space.
608
 *  profile  [O]   Buffer to receive the profile filename.
609 610 611 612 613 614
 *  size     [I/O] Size of the filename buffer in bytes.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
615 616
BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
{
617 618 619
    static const WCHAR rgbprofilefile[] =
        { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
          's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
620
    WCHAR rgbprofile[MAX_PATH];
621
    DWORD len = sizeof(rgbprofile);
622

623
    TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
624

625 626
    if (machine) 
    {
627
        SetLastError( ERROR_NOT_SUPPORTED );
628 629 630 631 632
        return FALSE;
    }

    if (!size) 
    {
633
        SetLastError( ERROR_INVALID_PARAMETER );
634 635
        return FALSE;
    }
636 637 638 639 640 641

    if (!profile)
    {
        SetLastError( ERROR_INSUFFICIENT_BUFFER );
        return FALSE;
    }
642

643
    GetColorDirectoryW( machine, rgbprofile, &len );
644 645

    switch (id)
646
    {
647 648
        case LCS_sRGB:
        case LCS_WINDOWS_COLOR_SPACE: /* FIXME */
649
            lstrcatW( rgbprofile, rgbprofilefile );
650
            len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
651

652
            if (*size < len)
653 654
            {
                *size = len;
655 656
                SetLastError( ERROR_MORE_DATA );
                return FALSE;
657 658 659 660 661 662
            }

            lstrcpyW( profile, rgbprofile );
            break;

        default:
663
            SetLastError( ERROR_FILE_NOT_FOUND );
664 665 666
            return FALSE;
    }
    return TRUE;
667 668
}

669
static BOOL header_from_file( LPCWSTR file, PPROFILEHEADER header )
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
{
    BOOL ret;
    PROFILE profile;
    WCHAR path[MAX_PATH], slash[] = {'\\',0};
    DWORD size = sizeof(path);
    HANDLE handle;

    ret = GetColorDirectoryW( NULL, path, &size );
    if (!ret)
    {
        WARN( "Can't retrieve color directory\n" );
        return FALSE;
    }
    if (size + sizeof(slash) + sizeof(WCHAR) * lstrlenW( file ) > sizeof(path))
    {
        WARN( "Filename too long\n" );
        return FALSE;
    }

    lstrcatW( path, slash );
    lstrcatW( path, file );

    profile.dwType = PROFILE_FILENAME;
    profile.pProfileData = path;
    profile.cbDataSize = lstrlenW( path ) + 1;

    handle = OpenColorProfileW( &profile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );
    if (!handle)
    {
        WARN( "Can't open color profile\n" );
        return FALSE;
    }

    ret = GetColorProfileHeader( handle, header );
    if (!ret)
        WARN( "Can't retrieve color profile header\n" );

    CloseColorProfile( handle );
    return ret;
}

711
static BOOL match_profile( PENUMTYPEW rec, PPROFILEHEADER hdr )
712 713 714 715 716 717 718
{
    if (rec->dwFields & ET_DEVICENAME)
    {
        FIXME( "ET_DEVICENAME: %s\n", debugstr_w(rec->pDeviceName) );
    }
    if (rec->dwFields & ET_MEDIATYPE)
    {
719
        FIXME( "ET_MEDIATYPE: 0x%08x\n", rec->dwMediaType );
720 721 722
    }
    if (rec->dwFields & ET_DITHERMODE)
    {
723
        FIXME( "ET_DITHERMODE: 0x%08x\n", rec->dwDitheringMode );
724 725 726
    }
    if (rec->dwFields & ET_RESOLUTION)
    {
727
        FIXME( "ET_RESOLUTION: 0x%08x, 0x%08x\n",
728 729 730 731
               rec->dwResolution[0], rec->dwResolution[1] );
    }
    if (rec->dwFields & ET_DEVICECLASS)
    {
732
        FIXME( "ET_DEVICECLASS: %s\n", dbgstr_tag(rec->dwMediaType) );
733 734 735
    }
    if (rec->dwFields & ET_CMMTYPE)
    {
736
        TRACE( "ET_CMMTYPE: %s\n", dbgstr_tag(rec->dwCMMType) );
737 738 739 740
        if (rec->dwCMMType != hdr->phCMMType) return FALSE;
    }
    if (rec->dwFields & ET_CLASS)
    {
741
        TRACE( "ET_CLASS: %s\n", dbgstr_tag(rec->dwClass) );
742 743 744 745
        if (rec->dwClass != hdr->phClass) return FALSE;
    }
    if (rec->dwFields & ET_DATACOLORSPACE)
    {
746
        TRACE( "ET_DATACOLORSPACE: %s\n", dbgstr_tag(rec->dwDataColorSpace) );
747 748 749 750
        if (rec->dwDataColorSpace != hdr->phDataColorSpace) return FALSE;
    }
    if (rec->dwFields & ET_CONNECTIONSPACE)
    {
751
        TRACE( "ET_CONNECTIONSPACE: %s\n", dbgstr_tag(rec->dwConnectionSpace) );
752 753 754 755
        if (rec->dwConnectionSpace != hdr->phConnectionSpace) return FALSE;
    }
    if (rec->dwFields & ET_SIGNATURE)
    {
756
        TRACE( "ET_SIGNATURE: %s\n", dbgstr_tag(rec->dwSignature) );
757 758 759 760
        if (rec->dwSignature != hdr->phSignature) return FALSE;
    }
    if (rec->dwFields & ET_PLATFORM)
    {
761
        TRACE( "ET_PLATFORM: %s\n", dbgstr_tag(rec->dwPlatform) );
762 763 764 765
        if (rec->dwPlatform != hdr->phPlatform) return FALSE;
    }
    if (rec->dwFields & ET_PROFILEFLAGS)
    {
766
        TRACE( "ET_PROFILEFLAGS: 0x%08x\n", rec->dwProfileFlags );
767 768 769 770
        if (rec->dwProfileFlags != hdr->phProfileFlags) return FALSE;
    }
    if (rec->dwFields & ET_MANUFACTURER)
    {
771
        TRACE( "ET_MANUFACTURER: %s\n", dbgstr_tag(rec->dwManufacturer) );
772 773 774 775
        if (rec->dwManufacturer != hdr->phManufacturer) return FALSE;
    }
    if (rec->dwFields & ET_MODEL)
    {
776
        TRACE( "ET_MODEL: %s\n", dbgstr_tag(rec->dwModel) );
777 778 779 780
        if (rec->dwModel != hdr->phModel) return FALSE;
    }
    if (rec->dwFields & ET_ATTRIBUTES)
    {
781
        TRACE( "ET_ATTRIBUTES: 0x%08x, 0x%08x\n",
782 783 784 785 786 787
               rec->dwAttributes[0], rec->dwAttributes[1] );
        if (rec->dwAttributes[0] != hdr->phAttributes[0] || 
            rec->dwAttributes[1] != hdr->phAttributes[1]) return FALSE;
    }
    if (rec->dwFields & ET_RENDERINGINTENT)
    {
788
        TRACE( "ET_RENDERINGINTENT: 0x%08x\n", rec->dwRenderingIntent );
789 790 791 792
        if (rec->dwRenderingIntent != hdr->phRenderingIntent) return FALSE;
    }
    if (rec->dwFields & ET_CREATOR)
    {
793
        TRACE( "ET_CREATOR: %s\n", dbgstr_tag(rec->dwCreator) );
794 795 796 797 798 799 800 801 802 803 804 805 806 807
        if (rec->dwCreator != hdr->phCreator) return FALSE;
    }
    return TRUE;
}

/******************************************************************************
 * EnumColorProfilesA               [MSCMS.@]
 *
 * See EnumColorProfilesW.
 */
BOOL WINAPI EnumColorProfilesA( PCSTR machine, PENUMTYPEA record, PBYTE buffer,
                                PDWORD size, PDWORD number )
{
    BOOL match, ret = FALSE;
808
    char spec[] = "\\*.icm";
809 810 811 812 813
    char colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
    DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
    PROFILEHEADER header;
    WIN32_FIND_DATAA data;
    ENUMTYPEW recordW;
814
    WCHAR *fileW = NULL, *deviceW = NULL;
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    HANDLE find;

    TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );

    if (machine || !record || !size ||
        record->dwSize != sizeof(ENUMTYPEA) ||
        record->dwVersion != ENUM_TYPE_VERSION) return FALSE;

    ret = GetColorDirectoryA( machine, colordir, &len );
    if (!ret || len + sizeof(spec) > MAX_PATH)
    {
        WARN( "can't retrieve color directory\n" );
        return FALSE;
    }

    lstrcpyA( glob, colordir );
    lstrcatA( glob, spec );

    find = FindFirstFileA( glob, &data );
    if (find == INVALID_HANDLE_VALUE) return FALSE;

    profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(char *) + 1 );
    if (!profiles) goto exit;

    memcpy( &recordW, record, sizeof(ENUMTYPEA) );
    if (record->pDeviceName)
    {
842
        deviceW = strdupW( record->pDeviceName );
843
        if (!(recordW.pDeviceName = deviceW)) goto exit;
844 845
    }

846
    fileW = strdupW( data.cFileName );
847 848
    if (!fileW) goto exit;

849
    ret = header_from_file( fileW, &header );
850 851
    if (ret)
    {
852
        match = match_profile( &recordW, &header );
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
        if (match)
        {
            len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
            profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );

            if (!profiles[count]) goto exit;
            else
            {
                TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
                lstrcpyA( profiles[count], data.cFileName );
                totalsize += len;
                count++;
            }
        }
    }
    HeapFree( GetProcessHeap(), 0, fileW );
    fileW = NULL;

    while (FindNextFileA( find, &data ))
    {
873
        fileW = strdupW( data.cFileName );
874 875
        if (!fileW) goto exit;

876
        ret = header_from_file( fileW, &header );
877 878 879 880 881 882
        if (!ret)
        {
            HeapFree( GetProcessHeap(), 0, fileW );
            continue;
        }

883
        match = match_profile( &recordW, &header );
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
        if (match)
        {
            char **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                                      profiles, sizeof(char *) * (count + 1) );
            if (!tmp) goto exit;
            else profiles = tmp;

            len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
            profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );

            if (!profiles[count]) goto exit;
            else
            {
                TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
                lstrcpyA( profiles[count], data.cFileName );
                totalsize += len;
                count++;
            }
        }
        HeapFree( GetProcessHeap(), 0, fileW );
        fileW = NULL;
    }

    totalsize++;
    if (buffer && *size >= totalsize)
    {
        char *p = (char *)buffer;

        for (i = 0; i < count; i++)
        {
            lstrcpyA( p, profiles[i] );
            p += lstrlenA( profiles[i] ) + 1;
        }
        *p = 0;
        ret = TRUE;
    }
    else ret = FALSE;

    *size = totalsize;
    if (number) *number = count;

exit:
    for (i = 0; i < count; i++)
        HeapFree( GetProcessHeap(), 0, profiles[i] );
    HeapFree( GetProcessHeap(), 0, profiles );
929
    HeapFree( GetProcessHeap(), 0, deviceW );
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
    HeapFree( GetProcessHeap(), 0, fileW );
    FindClose( find );

    return ret;
}

/******************************************************************************
 * EnumColorProfilesW               [MSCMS.@]
 *
 * Enumerate profiles that match given criteria.
 *
 * PARAMS
 *  machine  [I]   Name of the machine for which to enumerate profiles.
 *                 Must be NULL, which indicates the local machine.
 *  record   [I]   Record of criteria that a profile must match.
 *  buffer   [O]   Buffer to receive a string array of profile filenames.
 *  size     [I/O] Size of the filename buffer in bytes.
 *  number   [O]   Number of filenames copied into buffer.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
BOOL WINAPI EnumColorProfilesW( PCWSTR machine, PENUMTYPEW record, PBYTE buffer,
                                PDWORD size, PDWORD number )
{
    BOOL match, ret = FALSE;
957
    WCHAR spec[] = {'\\','*','i','c','m',0};
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    WCHAR colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
    DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
    PROFILEHEADER header;
    WIN32_FIND_DATAW data;
    HANDLE find;

    TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );

    if (machine || !record || !size ||
        record->dwSize != sizeof(ENUMTYPEW) ||
        record->dwVersion != ENUM_TYPE_VERSION) return FALSE;

    ret = GetColorDirectoryW( machine, colordir, &len );
    if (!ret || len + sizeof(spec) > MAX_PATH)
    {
        WARN( "Can't retrieve color directory\n" );
        return FALSE;
    }

    lstrcpyW( glob, colordir );
    lstrcatW( glob, spec );

    find = FindFirstFileW( glob, &data );
    if (find == INVALID_HANDLE_VALUE) return FALSE;

    profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR *) + 1 );
    if (!profiles) goto exit;

986
    ret = header_from_file( data.cFileName, &header );
987 988
    if (ret)
    {
989
        match = match_profile( record, &header );
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
        if (match)
        {
            len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
            profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );

            if (!profiles[count]) goto exit;
            else
            {
                TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
                lstrcpyW( profiles[count], data.cFileName );
                totalsize += len;
                count++;
            }
        }
    }

    while (FindNextFileW( find, &data ))
    {
1008
        ret = header_from_file( data.cFileName, &header );
1009 1010
        if (!ret) continue;

1011
        match = match_profile( record, &header );
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
        if (match)
        {
            WCHAR **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                                       profiles, sizeof(WCHAR *) * (count + 1) );
            if (!tmp) goto exit;
            else profiles = tmp;

            len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
            profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );

            if (!profiles[count]) goto exit;
            else
            {
                TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
                lstrcpyW( profiles[count], data.cFileName );
                totalsize += len;
                count++;
            }
        }
    }

    totalsize++;
    if (buffer && *size >= totalsize)
    {
        WCHAR *p = (WCHAR *)buffer;

        for (i = 0; i < count; i++)
        {
            lstrcpyW( p, profiles[i] );
            p += lstrlenW( profiles[i] ) + 1;
        }
        *p = 0;
        ret = TRUE;
    }
    else ret = FALSE;

    *size = totalsize;
    if (number) *number = count;

exit:
    for (i = 0; i < count; i++)
        HeapFree( GetProcessHeap(), 0, profiles[i] );
    HeapFree( GetProcessHeap(), 0, profiles );
    FindClose( find );

    return ret;
}

1060 1061 1062 1063 1064
/******************************************************************************
 * InstallColorProfileA               [MSCMS.@]
 *
 * See InstallColorProfileW.
 */
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
{
    UINT len;
    LPWSTR profileW;
    BOOL ret = FALSE;

    TRACE( "( %s )\n", debugstr_a(profile) );

    if (machine || !profile) return FALSE;

    len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
    profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );

    if (profileW)
    {
        MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );

        ret = InstallColorProfileW( NULL, profileW );
        HeapFree( GetProcessHeap(), 0, profileW );
    }
    return ret;
}

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
/******************************************************************************
 * InstallColorProfileW               [MSCMS.@]
 *
 * Install a color profile.
 *
 * PARAMS
 *  machine  [I] Name of the machine to install the profile on. Must be NULL,
 *               which indicates the local machine.
 *  profile  [I] Full path name of the profile to install.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
1102 1103
BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
{
1104 1105 1106 1107 1108
    WCHAR dest[MAX_PATH], base[MAX_PATH];
    DWORD size = sizeof(dest);
    static const WCHAR slash[] = { '\\', 0 };

    TRACE( "( %s )\n", debugstr_w(profile) );
1109 1110 1111

    if (machine || !profile) return FALSE;

1112 1113
    if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;

1114
    basename( profile, base );
1115 1116 1117 1118 1119 1120 1121 1122

    lstrcatW( dest, slash );
    lstrcatW( dest, base );

    /* Is source equal to destination? */
    if (!lstrcmpW( profile, dest )) return TRUE;

    return CopyFileW( profile, dest, TRUE );
1123 1124
}

1125 1126 1127
/******************************************************************************
 * IsColorProfileTagPresent               [MSCMS.@]
 *
1128
 * Determine if a given ICC tag type is present in a color profile.
1129 1130 1131
 *
 * PARAMS
 *  profile  [I] Color profile handle.
1132 1133
 *  tag      [I] ICC tag type.
 *  present  [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
1134 1135 1136 1137 1138 1139
 *               FALSE otherwise.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
1140
BOOL WINAPI IsColorProfileTagPresent( HPROFILE handle, TAGTYPE type, PBOOL present )
1141 1142
{
    BOOL ret = FALSE;
1143
#ifdef HAVE_LCMS2
1144
    struct profile *profile = grab_profile( handle );
1145

1146
    TRACE( "( %p, 0x%08x, %p )\n", handle, type, present );
1147

1148
    if (!profile) return FALSE;
1149

1150 1151 1152 1153 1154
    if (!present)
    {
        release_profile( profile );
        return FALSE;
    }
1155
    *present = (cmsIsTag( profile->cmsprofile, type ) != 0);
1156
    release_profile( profile );
1157
    ret = TRUE;
1158

1159
#endif /* HAVE_LCMS2 */
1160
    return ret;
1161 1162
}

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
/******************************************************************************
 * IsColorProfileValid               [MSCMS.@]
 *
 * Determine if a given color profile is valid.
 *
 * PARAMS
 *  profile  [I] Color profile handle.
 *  valid    [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
 *               FALSE otherwise.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE 
 */
1177
BOOL WINAPI IsColorProfileValid( HPROFILE handle, PBOOL valid )
1178
{
1179
    BOOL ret = FALSE;
1180
#ifdef HAVE_LCMS2
1181 1182 1183
    struct profile *profile = grab_profile( handle );

    TRACE( "( %p, %p )\n", handle, valid );
1184

1185
    if (!profile) return FALSE;
1186

1187 1188 1189 1190 1191
    if (!valid)
    {
        release_profile( profile );
        return FALSE;
    }
1192
    if (profile->data) ret = *valid = TRUE;
1193
    release_profile( profile );
1194

1195
#endif /* HAVE_LCMS2 */
1196 1197 1198
    return ret;
}

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
/******************************************************************************
 * SetColorProfileElement               [MSCMS.@]
 *
 * Set data for a specified tag type.
 *
 * PARAMS
 *  profile  [I]   Handle to a color profile.
 *  type     [I]   ICC tag type.
 *  offset   [I]   Offset in bytes to start copying to.
 *  size     [I/O] Size of the buffer in bytes. On return the variable holds the
 *                 number of bytes actually needed.
 *  buffer   [O]   Buffer holding the tag data.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
1216
BOOL WINAPI SetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
1217 1218 1219
                                    PVOID buffer )
{
    BOOL ret = FALSE;
1220
#ifdef HAVE_LCMS2
1221
    struct profile *profile = grab_profile( handle );
1222

1223
    TRACE( "( %p, 0x%08x, %d, %p, %p )\n", handle, type, offset, size, buffer );
1224

1225
    if (!profile) return FALSE;
1226

1227 1228 1229 1230 1231
    if (!size || !buffer || !(profile->access & PROFILE_READWRITE))
    {
        release_profile( profile );
        return FALSE;
    }
1232
    ret = set_tag_data( profile, type, offset, buffer, size );
1233
    release_profile( profile );
1234
#endif /* HAVE_LCMS2 */
1235 1236 1237
    return ret;
}

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
/******************************************************************************
 * SetColorProfileHeader               [MSCMS.@]
 *
 * Set header data for a given profile.
 *
 * PARAMS
 *  profile  [I] Handle to a color profile.
 *  header   [I] Buffer holding the header data.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
1251
BOOL WINAPI SetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
1252
{
1253
#ifdef HAVE_LCMS2
1254
    struct profile *profile = grab_profile( handle );
1255

1256
    TRACE( "( %p, %p )\n", handle, header );
1257

1258
    if (!profile) return FALSE;
1259

1260 1261 1262 1263 1264
    if (!header || !(profile->access & PROFILE_READWRITE))
    {
        release_profile( profile );
        return FALSE;
    }
1265
    set_profile_header( profile, header );
1266
    release_profile( profile );
1267 1268
    return TRUE;

1269 1270
#else
    return FALSE;
1271
#endif /* HAVE_LCMS2 */
1272 1273 1274 1275 1276 1277 1278
}

/******************************************************************************
 * UninstallColorProfileA               [MSCMS.@]
 *
 * See UninstallColorProfileW.
 */
1279 1280
BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
{
1281 1282 1283 1284
    UINT len;
    LPWSTR profileW;
    BOOL ret = FALSE;

1285
    TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
1286

1287 1288
    if (machine || !profile) return FALSE;

1289 1290
    len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
    profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1291

1292 1293 1294 1295 1296 1297 1298 1299 1300
    if (profileW)
    {
        MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );

        ret = UninstallColorProfileW( NULL, profileW , delete );

        HeapFree( GetProcessHeap(), 0, profileW );
    }
    return ret;
1301 1302
}

1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
/******************************************************************************
 * UninstallColorProfileW               [MSCMS.@]
 *
 * Uninstall a color profile.
 *
 * PARAMS
 *  machine  [I] Name of the machine to uninstall the profile on. Must be NULL,
 *               which indicates the local machine.
 *  profile  [I] Full path name of the profile to uninstall.
 *  delete   [I] Bool that specifies whether the profile file should be deleted.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
1318 1319
BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
{
1320 1321
    TRACE( "( %s, %x )\n", debugstr_w(profile), delete );

1322 1323
    if (machine || !profile) return FALSE;

1324
    if (delete) return DeleteFileW( profile );
1325 1326 1327 1328

    return TRUE;
}

1329 1330 1331 1332 1333
/******************************************************************************
 * OpenColorProfileA               [MSCMS.@]
 *
 * See OpenColorProfileW.
 */
1334 1335 1336 1337
HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
{
    HPROFILE handle = NULL;

1338
    TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373

    if (!profile || !profile->pProfileData) return NULL;

    /* No AW conversion needed for memory based profiles */
    if (profile->dwType & PROFILE_MEMBUFFER)
        return OpenColorProfileW( profile, access, sharing, creation );

    if (profile->dwType & PROFILE_FILENAME)
    {
        UINT len;
        PROFILE profileW;

        profileW.dwType = profile->dwType;
 
        len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
        profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );

        if (profileW.pProfileData)
        {
            profileW.cbDataSize = len * sizeof(WCHAR);
            MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );

            handle = OpenColorProfileW( &profileW, access, sharing, creation );
            HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
        }
    }
    return handle;
}

/******************************************************************************
 * OpenColorProfileW               [MSCMS.@]
 *
 * Open a color profile.
 *
 * PARAMS
1374 1375 1376 1377
 *  profile   [I] Pointer to a color profile structure.
 *  access    [I] Desired access.
 *  sharing   [I] Sharing mode.
 *  creation  [I] Creation mode.
1378 1379
 *
 * RETURNS
1380
 *  Success: Handle to the opened profile.
1381 1382 1383
 *  Failure: NULL
 *
 * NOTES
1384 1385
 *  Values for access:   PROFILE_READ or PROFILE_READWRITE.
 *  Values for sharing:  0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
1386 1387
 *  Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
 *                       OPEN_ALWAYS, TRUNCATE_EXISTING.
1388
 *  Sharing and creation flags are ignored for memory based profiles.
1389 1390 1391
 */
HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
{
1392
#ifdef HAVE_LCMS2
1393
    cmsHPROFILE cmsprofile = NULL;
1394
    char *data = NULL;
1395
    HANDLE handle = INVALID_HANDLE_VALUE;
1396
    DWORD size;
1397

1398
    TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1399 1400 1401

    if (!profile || !profile->pProfileData) return NULL;

1402
    if (profile->dwType == PROFILE_MEMBUFFER)
1403
    {
1404
        /* FIXME: access flags not implemented for memory based profiles */
1405

1406 1407
        if (!(data = HeapAlloc( GetProcessHeap(), 0, profile->cbDataSize ))) return NULL;
        memcpy( data, profile->pProfileData, profile->cbDataSize );
1408

1409 1410 1411 1412 1413
        if (!(cmsprofile = cmsOpenProfileFromMem( data, profile->cbDataSize )))
        {
            HeapFree( GetProcessHeap(), 0, data );
            return FALSE;
        }
1414
        size = profile->cbDataSize;
1415
    }
1416
    else if (profile->dwType == PROFILE_FILENAME)
1417
    {
1418
        DWORD read, flags = 0;
1419

1420
        TRACE( "profile file: %s\n", debugstr_w( profile->pProfileData ) );
1421 1422 1423 1424 1425

        if (access & PROFILE_READ) flags = GENERIC_READ;
        if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;

        if (!flags) return NULL;
1426
        if (!sharing) sharing = FILE_SHARE_READ;
1427

1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
        if (!PathIsRelativeW( profile->pProfileData ))
            handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
        else
        {
            WCHAR *path;

            if (!GetColorDirectoryW( NULL, NULL, &size ) && GetLastError() == ERROR_MORE_DATA)
            {
                size += (strlenW( profile->pProfileData ) + 2) * sizeof(WCHAR);
                if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
                GetColorDirectoryW( NULL, path, &size );
                PathAddBackslashW( path );
                strcatW( path, profile->pProfileData );
            }
            else return NULL;
            handle = CreateFileW( path, flags, sharing, NULL, creation, 0, NULL );
            HeapFree( GetProcessHeap(), 0, path );
        }
1446 1447
        if (handle == INVALID_HANDLE_VALUE)
        {
1448
            WARN( "Unable to open color profile %u\n", GetLastError() );
1449 1450 1451 1452 1453 1454 1455 1456
            return NULL;
        }
        if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
        {
            ERR( "Unable to retrieve size of color profile\n" );
            CloseHandle( handle );
            return NULL;
        }
1457
        if (!(data = HeapAlloc( GetProcessHeap(), 0, size )))
1458
        {
1459 1460 1461
            ERR( "Unable to allocate memory for color profile\n" );
            CloseHandle( handle );
            return NULL;
1462
        }
1463
        if (!ReadFile( handle, data, size, &read, NULL ) || read != size)
1464 1465 1466 1467
        {
            ERR( "Unable to read color profile\n" );

            CloseHandle( handle );
1468
            HeapFree( GetProcessHeap(), 0, data );
1469 1470
            return NULL;
        }
1471 1472 1473 1474 1475 1476
        if (!(cmsprofile = cmsOpenProfileFromMem( data, size )))
        {
            CloseHandle( handle );
            HeapFree( GetProcessHeap(), 0, data );
            return NULL;
        }
1477
    }
1478 1479 1480 1481 1482
    else
    {
        ERR( "Invalid profile type %u\n", profile->dwType );
        return NULL;
    }
1483

1484
    if (cmsprofile)
1485 1486
    {
        struct profile profile;
1487
        HPROFILE hprof;
1488

1489 1490 1491 1492
        profile.file       = handle;
        profile.access     = access;
        profile.data       = data;
        profile.size       = size;
1493 1494
        profile.cmsprofile = cmsprofile;

1495
        if ((hprof = create_profile( &profile ))) return hprof;
1496
        HeapFree( GetProcessHeap(), 0, data );
1497
        cmsCloseProfile( cmsprofile );
1498
    }
1499
    CloseHandle( handle );
1500

1501
#endif /* HAVE_LCMS2 */
1502 1503 1504
    return NULL;
}

1505 1506 1507 1508 1509 1510
/******************************************************************************
 * CloseColorProfile               [MSCMS.@]
 *
 * Close a color profile.
 *
 * PARAMS
1511
 *  profile  [I] Handle to the profile.
1512 1513 1514 1515 1516
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
1517 1518
BOOL WINAPI CloseColorProfile( HPROFILE profile )
{
1519
    BOOL ret = FALSE;
1520
#ifdef HAVE_LCMS2
1521 1522

    TRACE( "( %p )\n", profile );
1523
    ret = close_profile( profile );
1524

1525
#endif /* HAVE_LCMS2 */
1526
    return ret;
1527
}