transform.c 14.5 KB
Newer Older
1 2 3
/*
 * MSCMS - Color Management System for Wine
 *
4
 * Copyright 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 23 24 25 26 27 28 29 30 31 32
 */

#include "config.h"
#include "wine/debug.h"

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "wingdi.h"
#include "winuser.h"
#include "icm.h"

33
#include "mscms_priv.h"
34 35 36

WINE_DEFAULT_DEBUG_CHANNEL(mscms);

37
#ifdef HAVE_LCMS2
38 39 40

static DWORD from_bmformat( BMFORMAT format )
{
41
    static BOOL quietfixme = FALSE;
42
    DWORD ret;
43 44 45

    switch (format)
    {
46 47 48 49 50
    case BM_RGBTRIPLETS: ret = TYPE_RGB_8; break;
    case BM_BGRTRIPLETS: ret = TYPE_BGR_8; break;
    case BM_GRAY:        ret = TYPE_GRAY_8; break;
    case BM_xRGBQUADS:   ret = TYPE_ARGB_8; break;
    case BM_xBGRQUADS:   ret = TYPE_ABGR_8; break;
51
    case BM_KYMCQUADS:   ret = TYPE_KYMC_8; break;
52
    default:
53
        if (!quietfixme)
54
        {
55
            FIXME( "unhandled bitmap format %08x\n", format );
56
            quietfixme = TRUE;
57
        }
58 59
        ret = TYPE_RGB_8;
        break;
60
    }
61 62
    TRACE( "color space: %08x -> %08x\n", format, ret );
    return ret;
63 64 65 66
}

static DWORD from_type( COLORTYPE type )
{
67
    DWORD ret;
68 69 70

    switch (type)
    {
71 72 73 74 75 76
    case COLOR_GRAY: ret = TYPE_GRAY_16; break;
    case COLOR_RGB:  ret = TYPE_RGB_16; break;
    case COLOR_XYZ:  ret = TYPE_XYZ_16; break;
    case COLOR_Yxy:  ret = TYPE_Yxy_16; break;
    case COLOR_Lab:  ret = TYPE_Lab_16; break;
    case COLOR_CMYK: ret = TYPE_CMYK_16; break;
77
    default:
78 79 80
        FIXME( "unhandled color type %08x\n", type );
        ret = TYPE_RGB_16;
        break;
81
    }
82 83 84

    TRACE( "color type: %08x -> %08x\n", type, ret );
    return ret;
85 86
}

87
#endif /* HAVE_LCMS2 */
88

89 90 91 92 93
/******************************************************************************
 * CreateColorTransformA            [MSCMS.@]
 *
 * See CreateColorTransformW.
 */
94 95 96 97 98 99
HTRANSFORM WINAPI CreateColorTransformA( LPLOGCOLORSPACEA space, HPROFILE dest,
    HPROFILE target, DWORD flags )
{
    LOGCOLORSPACEW spaceW;
    DWORD len;

100
    TRACE( "( %p, %p, %p, 0x%08x )\n", space, dest, target, flags );
101 102 103 104 105 106 107 108 109 110 111 112

    if (!space || !dest) return FALSE;

    memcpy( &spaceW, space, FIELD_OFFSET(LOGCOLORSPACEA, lcsFilename) );
    spaceW.lcsSize = sizeof(LOGCOLORSPACEW);

    len = MultiByteToWideChar( CP_ACP, 0, space->lcsFilename, -1, NULL, 0 );
    MultiByteToWideChar( CP_ACP, 0, space->lcsFilename, -1, spaceW.lcsFilename, len );

    return CreateColorTransformW( &spaceW, dest, target, flags );
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/******************************************************************************
 * CreateColorTransformW            [MSCMS.@]
 *
 * Create a color transform.
 *
 * PARAMS
 *  space  [I] Input color space.
 *  dest   [I] Color profile of destination device.
 *  target [I] Color profile of target device.
 *  flags  [I] Flags.
 *
 * RETURNS
 *  Success: Handle to a transform.
 *  Failure: NULL
 */
128 129 130 131
HTRANSFORM WINAPI CreateColorTransformW( LPLOGCOLORSPACEW space, HPROFILE dest,
    HPROFILE target, DWORD flags )
{
    HTRANSFORM ret = NULL;
132
#ifdef HAVE_LCMS2
133 134
    struct transform transform;
    struct profile *dst, *tgt = NULL;
135
    cmsHPROFILE cmsinput, cmsoutput, cmstarget = NULL;
136
    DWORD proofing = 0;
137
    int intent;
138

139
    TRACE( "( %p, %p, %p, 0x%08x )\n", space, dest, target, flags );
140

141
    if (!space || !(dst = grab_profile( dest ))) return FALSE;
142

143 144 145 146 147
    if (target && !(tgt = grab_profile( target )))
    {
        release_profile( dst );
        return FALSE;
    }
148 149
    intent = space->lcsIntent > 3 ? INTENT_PERCEPTUAL : space->lcsIntent;

150
    TRACE( "lcsIntent:   %x\n", space->lcsIntent );
151
    TRACE( "lcsCSType:   %s\n", dbgstr_tag( space->lcsCSType ) );
152
    TRACE( "lcsFilename: %s\n", debugstr_w( space->lcsFilename ) );
153

154
    cmsinput = cmsCreate_sRGBProfile(); /* FIXME: create from supplied color space */
155 156
    if (target)
    {
157
        proofing = cmsFLAGS_SOFTPROOFING;
158
        cmstarget = tgt->cmsprofile;
159
    }
160
    cmsoutput = dst->cmsprofile;
161 162 163
    transform.cmstransform = cmsCreateProofingTransform(cmsinput, 0, cmsoutput, 0, cmstarget,
                                                        intent, INTENT_ABSOLUTE_COLORIMETRIC,
                                                        proofing);
164 165 166 167 168 169
    if (!transform.cmstransform)
    {
        if (tgt) release_profile( tgt );
        release_profile( dst );
        return FALSE;
    }
170

171 172 173 174
    ret = create_transform( &transform );

    if (tgt) release_profile( tgt );
    release_profile( dst );
175

176
#endif /* HAVE_LCMS2 */
177 178 179
    return ret;
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
/******************************************************************************
 * CreateMultiProfileTransform      [MSCMS.@]
 *
 * Create a color transform from an array of color profiles.
 *
 * PARAMS
 *  profiles  [I] Array of color profiles.
 *  nprofiles [I] Number of color profiles.
 *  intents   [I] Array of rendering intents.
 *  flags     [I] Flags.
 *  cmm       [I] Profile to take the CMM from.
 *
 * RETURNS
 *  Success: Handle to a transform.
 *  Failure: NULL
 */ 
196 197 198 199
HTRANSFORM WINAPI CreateMultiProfileTransform( PHPROFILE profiles, DWORD nprofiles,
    PDWORD intents, DWORD nintents, DWORD flags, DWORD cmm )
{
    HTRANSFORM ret = NULL;
200
#ifdef HAVE_LCMS2
201
    cmsHPROFILE *cmsprofiles;
202 203
    struct transform transform;
    struct profile *profile0, *profile1;
204

205
    TRACE( "( %p, 0x%08x, %p, 0x%08x, 0x%08x, 0x%08x )\n",
206 207
           profiles, nprofiles, intents, nintents, flags, cmm );

208
    if (!profiles || !nprofiles || !intents) return NULL;
209

210 211 212 213 214
    if (nprofiles > 2)
    {
        FIXME("more than 2 profiles not supported\n");
        return NULL;
    }
215

216 217 218 219 220 221 222 223
    profile0 = grab_profile( profiles[0] );
    if (!profile0) return NULL;
    profile1 = grab_profile( profiles[1] );
    if (!profile1)
    {
        release_profile( profile0 );
        return NULL;
    }
224

225
    if ((cmsprofiles = HeapAlloc( GetProcessHeap(), 0, (nprofiles + 1) * sizeof(cmsHPROFILE) )))
226
    {
227
        cmsprofiles[0] = profile0->cmsprofile;
228 229 230 231
        cmsprofiles[1] = profile1->cmsprofile;

        transform.cmstransform = cmsCreateMultiprofileTransform( cmsprofiles, nprofiles, 0,
                                                                 0, *intents, 0 );
232
        HeapFree( GetProcessHeap(), 0, cmsprofiles );
233 234 235 236 237 238
        if (!transform.cmstransform)
        {
            release_profile( profile0 );
            release_profile( profile1 );
            return FALSE;
        }
239
        ret = create_transform( &transform );
240
    }
241

242 243 244
    release_profile( profile0 );
    release_profile( profile1 );

245
#endif /* HAVE_LCMS2 */
246 247 248
    return ret;
}

249 250 251 252 253 254 255 256 257 258 259 260
/******************************************************************************
 * DeleteColorTransform             [MSCMS.@]
 *
 * Delete a color transform.
 *
 * PARAMS
 *  transform [I] Handle to a color transform.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */ 
261
BOOL WINAPI DeleteColorTransform( HTRANSFORM handle )
262 263
{
    BOOL ret = FALSE;
264
#ifdef HAVE_LCMS2
265

266
    TRACE( "( %p )\n", handle );
267

268
    ret = close_transform( handle );
269

270
#endif /* HAVE_LCMS2 */
271 272
    return ret;
}
273

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/******************************************************************************
 * TranslateBitmapBits              [MSCMS.@]
 *
 * Perform color translation.
 *
 * PARAMS
 *  transform    [I] Handle to a color transform.
 *  srcbits      [I] Source bitmap.
 *  input        [I] Format of the source bitmap.
 *  width        [I] Width of the source bitmap.
 *  height       [I] Height of the source bitmap.
 *  inputstride  [I] Number of bytes in one scanline.
 *  destbits     [I] Destination bitmap.
 *  output       [I] Format of the destination bitmap.
 *  outputstride [I] Number of bytes in one scanline. 
 *  callback     [I] Callback function.
 *  data         [I] Callback data. 
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
296
BOOL WINAPI TranslateBitmapBits( HTRANSFORM handle, PVOID srcbits, BMFORMAT input,
297 298 299 300
    DWORD width, DWORD height, DWORD inputstride, PVOID destbits, BMFORMAT output,
    DWORD outputstride, PBMCALLBACKFN callback, ULONG data )
{
    BOOL ret = FALSE;
301
#ifdef HAVE_LCMS2
302
    struct transform *transform = grab_transform( handle );
303

304
    TRACE( "( %p, %p, 0x%08x, 0x%08x, 0x%08x, 0x%08x, %p, 0x%08x, 0x%08x, %p, 0x%08x )\n",
305
           handle, srcbits, input, width, height, inputstride, destbits, output,
306 307
           outputstride, callback, data );

308
    if (!transform) return FALSE;
309 310
    if (!cmsChangeBuffersFormat( transform->cmstransform, from_bmformat(input), from_bmformat(output) ))
        return FALSE;
311

312 313
    cmsDoTransform( transform->cmstransform, srcbits, destbits, width * height );
    release_transform( transform );
314 315
    ret = TRUE;

316
#endif /* HAVE_LCMS2 */
317 318
    return ret;
}
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

/******************************************************************************
 * TranslateColors              [MSCMS.@]
 *
 * Perform color translation.
 *
 * PARAMS
 *  transform    [I] Handle to a color transform.
 *  input        [I] Array of input colors.
 *  number       [I] Number of colors to translate.
 *  input_type   [I] Input color format.
 *  output       [O] Array of output colors.
 *  output_type  [I] Output color format.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
337
BOOL WINAPI TranslateColors( HTRANSFORM handle, PCOLOR in, DWORD count,
338 339
                             COLORTYPE input_type, PCOLOR out, COLORTYPE output_type )
{
340
#ifdef HAVE_LCMS2
341
    BOOL ret = TRUE;
342 343
    struct transform *transform = grab_transform( handle );
    cmsHTRANSFORM xfrm;
344 345
    unsigned int i;

346 347 348
    TRACE( "( %p, %p, %d, %d, %p, %d )\n", handle, in, count, input_type, out, output_type );

    if (!transform) return FALSE;
349

350
    xfrm = transform->cmstransform;
351 352
    if (!cmsChangeBuffersFormat( xfrm, from_type(input_type), from_type(output_type) ))
        return FALSE;
353 354 355 356 357 358 359

    switch (input_type)
    {
    case COLOR_RGB:
    {
        switch (output_type)
        {
360 361 362 363 364
        case COLOR_RGB:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].rgb, 1 ); goto done;
        case COLOR_Lab:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].Lab, 1 ); goto done;
        case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].gray, 1 ); goto done;
        case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].cmyk, 1 ); goto done;
        case COLOR_XYZ:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].rgb, &out[i].XYZ, 1 ); goto done;
365 366
        default:
            FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
367 368
            ret = FALSE;
            break;
369
        }
370
        break;
371 372 373 374 375
    }
    case COLOR_Lab:
    {
        switch (output_type)
        {
376 377 378 379 380
        case COLOR_RGB:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].rgb, 1 ); goto done;
        case COLOR_Lab:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].Lab, 1 ); goto done;
        case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].gray, 1 ); goto done;
        case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].cmyk, 1 ); goto done;
        case COLOR_XYZ:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].Lab, &out[i].XYZ, 1 ); goto done;
381 382
        default:
            FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
383 384
            ret = FALSE;
            break;
385
        }
386
        break;
387 388 389 390 391
    }
    case COLOR_GRAY:
    {
        switch (output_type)
        {
392 393 394 395 396
        case COLOR_RGB:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].rgb, 1 ); goto done;
        case COLOR_Lab:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].Lab, 1 ); goto done;
        case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].gray, 1 ); goto done;
        case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].cmyk, 1 ); goto done;
        case COLOR_XYZ:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].gray, &out[i].XYZ, 1 ); goto done;
397 398
        default:
            FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
399 400
            ret = FALSE;
            break;
401
        }
402
        break;
403 404 405 406 407
    }
    case COLOR_CMYK:
    {
        switch (output_type)
        {
408 409 410 411 412
        case COLOR_RGB:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].rgb, 1 ); goto done;
        case COLOR_Lab:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].Lab, 1 ); goto done;
        case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].gray, 1 ); goto done;
        case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].cmyk, 1 ); goto done;
        case COLOR_XYZ:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].cmyk, &out[i].XYZ, 1 ); goto done;
413 414
        default:
            FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
415 416
            ret = FALSE;
            break;
417
        }
418
        break;
419 420 421 422 423
    }
    case COLOR_XYZ:
    {
        switch (output_type)
        {
424 425 426 427 428
        case COLOR_RGB:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].rgb, 1 ); goto done;
        case COLOR_Lab:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].Lab, 1 ); goto done;
        case COLOR_GRAY: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].gray, 1 ); goto done;
        case COLOR_CMYK: for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].cmyk, 1 ); goto done;
        case COLOR_XYZ:  for (i = 0; i < count; i++) cmsDoTransform( xfrm, &in[i].XYZ, &out[i].XYZ, 1 ); goto done;
429 430
        default:
            FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
431 432
            ret = FALSE;
            break;
433
        }
434
        break;
435 436 437
    }
    default:
        FIXME("unhandled input/output pair: %d/%d\n", input_type, output_type);
438
        ret = FALSE;
439 440
        break;
    }
441 442

done:
443
    release_transform( transform );
444
    return ret;
445

446
#else  /* HAVE_LCMS2 */
447
    return FALSE;
448
#endif /* HAVE_LCMS2 */
449
}