ImfAcesFile.cpp 13.2 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 129 130 131 132 133 134 135 136 137 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2007, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// *       Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// *       Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// *       Neither the name of Industrial Light & Magic nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------------
//
//	ACES image file I/O.
//
//-----------------------------------------------------------------------------

#include <ImfAcesFile.h>
#include <ImfRgbaFile.h>
#include <ImfStandardAttributes.h>
#include <Iex.h>
#include <algorithm> // for std::max()

using namespace std;
using namespace Imath;
using namespace Iex;

namespace Imf {


const Chromaticities &
acesChromaticities ()
{
    static const Chromaticities acesChr
        (V2f (0.73470,  0.26530),	// red
         V2f (0.00000,  1.00000),	// green
         V2f (0.00010, -0.07700),	// blue
         V2f (0.32168,  0.33767));	// white

    return acesChr;
}


class AcesOutputFile::Data
{
  public:

     Data();
    ~Data();

    RgbaOutputFile *	rgbaFile;
};


AcesOutputFile::Data::Data ():
    rgbaFile (0)
{
    // empty
}


AcesOutputFile::Data::~Data ()
{
    delete rgbaFile;
}


namespace {

void
checkCompression (Compression compression)
{
    //
    // Not all compression methods are allowed in ACES files.
    //

    switch (compression)
    {
      case NO_COMPRESSION:
      case PIZ_COMPRESSION:
      case B44A_COMPRESSION:
    break;

      default:
    throw ArgExc ("Invalid compression type for ACES file.");
    }
}

} // namespace


AcesOutputFile::AcesOutputFile
    (const std::string &name,
     const Header &header,
     RgbaChannels rgbaChannels,
     int numThreads)
:
    _data (new Data)
{
    checkCompression (header.compression());

    Header newHeader = header;
    addChromaticities (newHeader, acesChromaticities());
    addAdoptedNeutral (newHeader, acesChromaticities().white);

    _data->rgbaFile = new RgbaOutputFile (name.c_str(),
                      newHeader,
                      rgbaChannels,
                      numThreads);

    _data->rgbaFile->setYCRounding (7, 6);
}


AcesOutputFile::AcesOutputFile
    (OStream &os,
     const Header &header,
     RgbaChannels rgbaChannels,
     int numThreads)
:
    _data (new Data)
{
    checkCompression (header.compression());

    Header newHeader = header;
    addChromaticities (newHeader, acesChromaticities());
    addAdoptedNeutral (newHeader, acesChromaticities().white);

    _data->rgbaFile = new RgbaOutputFile (os,
                      header,
                      rgbaChannels,
                      numThreads);

    _data->rgbaFile->setYCRounding (7, 6);
}


AcesOutputFile::AcesOutputFile
    (const std::string &name,
     const Imath::Box2i &displayWindow,
     const Imath::Box2i &dataWindow,
     RgbaChannels rgbaChannels,
     float pixelAspectRatio,
     const Imath::V2f screenWindowCenter,
     float screenWindowWidth,
     LineOrder lineOrder,
     Compression compression,
     int numThreads)
:
    _data (new Data)
{
    checkCompression (compression);

    Header newHeader (displayWindow,
              dataWindow.isEmpty()? displayWindow: dataWindow,
              pixelAspectRatio,
              screenWindowCenter,
              screenWindowWidth,
              lineOrder,
              compression);

    addChromaticities (newHeader, acesChromaticities());
    addAdoptedNeutral (newHeader, acesChromaticities().white);

    _data->rgbaFile = new RgbaOutputFile (name.c_str(),
                      newHeader,
                      rgbaChannels,
                      numThreads);

    _data->rgbaFile->setYCRounding (7, 6);
}


AcesOutputFile::AcesOutputFile
    (const std::string &name,
     int width,
     int height,
     RgbaChannels rgbaChannels,
     float pixelAspectRatio,
     const Imath::V2f screenWindowCenter,
     float screenWindowWidth,
     LineOrder lineOrder,
     Compression compression,
     int numThreads)
:
    _data (new Data)
{
    checkCompression (compression);

    Header newHeader (width,
              height,
              pixelAspectRatio,
              screenWindowCenter,
              screenWindowWidth,
              lineOrder,
              compression);

    addChromaticities (newHeader, acesChromaticities());
    addAdoptedNeutral (newHeader, acesChromaticities().white);

    _data->rgbaFile = new RgbaOutputFile (name.c_str(),
                      newHeader,
                      rgbaChannels,
                      numThreads);

    _data->rgbaFile->setYCRounding (7, 6);
}


AcesOutputFile::~AcesOutputFile ()
{
    delete _data;
}


void
AcesOutputFile::setFrameBuffer
    (const Rgba *base,
     size_t xStride,
     size_t yStride)
{
    _data->rgbaFile->setFrameBuffer (base, xStride, yStride);
}


void
AcesOutputFile::writePixels (int numScanLines)
{
    _data->rgbaFile->writePixels (numScanLines);
}


int
AcesOutputFile::currentScanLine () const
{
    return _data->rgbaFile->currentScanLine();
}


const Header &
AcesOutputFile::header () const
{
    return _data->rgbaFile->header();
}


const Imath::Box2i &
AcesOutputFile::displayWindow () const
{
    return _data->rgbaFile->displayWindow();
}


const Imath::Box2i &
AcesOutputFile::dataWindow () const
{
    return _data->rgbaFile->dataWindow();
}


float
AcesOutputFile::pixelAspectRatio () const
{
    return _data->rgbaFile->pixelAspectRatio();
}


const Imath::V2f
AcesOutputFile::screenWindowCenter () const
{
    return _data->rgbaFile->screenWindowCenter();
}


float
AcesOutputFile::screenWindowWidth () const
{
    return _data->rgbaFile->screenWindowWidth();
}


LineOrder
AcesOutputFile::lineOrder () const
{
    return _data->rgbaFile->lineOrder();
}


Compression
AcesOutputFile::compression () const
{
    return _data->rgbaFile->compression();
}


RgbaChannels
AcesOutputFile::channels () const
{
    return _data->rgbaFile->channels();
}


void
AcesOutputFile::updatePreviewImage (const PreviewRgba pixels[])
{
    _data->rgbaFile->updatePreviewImage (pixels);
}


class AcesInputFile::Data
{
  public:

     Data();
    ~Data();

    void		initColorConversion ();

    RgbaInputFile *	rgbaFile;

    Rgba *		fbBase;
    size_t		fbXStride;
    size_t		fbYStride;
    int			minX;
    int			maxX;

    bool		mustConvertColor;
    M44f		fileToAces;
};


AcesInputFile::Data::Data ():
    rgbaFile (0),
    fbBase (0),
    fbXStride (0),
    fbYStride (0),
    minX (0),
    maxX (0),
    mustConvertColor (false)
{
    // empty
}


AcesInputFile::Data::~Data ()
{
    delete rgbaFile;
}


void
AcesInputFile::Data::initColorConversion ()
{
    const Header &header = rgbaFile->header();

    Chromaticities fileChr;

    if (hasChromaticities (header))
    fileChr = chromaticities (header);

    V2f fileNeutral = fileChr.white;

    if (hasAdoptedNeutral (header))
    fileNeutral = adoptedNeutral (header);

    const Chromaticities acesChr = acesChromaticities();

    V2f acesNeutral = acesChr.white;

    if (fileChr.red == acesChr.red &&
    fileChr.green == acesChr.green &&
    fileChr.blue == acesChr.blue &&
    fileChr.white == acesChr.white &&
    fileNeutral == acesNeutral)
    {
    //
    // The file already contains ACES data,
    // color conversion is not necessary.

    return;
    }

    mustConvertColor = true;
    minX = header.dataWindow().min.x;
    maxX = header.dataWindow().max.x;

    //
    // Create a matrix that transforms colors from the
    // RGB space of the input file into the ACES space
    // using a color adaptation transform to move the
    // white point.
    //

    //
    // We'll need the Bradford cone primary matrix and its inverse
    //

    static const M44f bradfordCPM
        (0.895100, -0.750200,  0.038900,  0.000000,
         0.266400,  1.713500, -0.068500,  0.000000,
        -0.161400,  0.036700,  1.029600,  0.000000,
         0.000000,  0.000000,  0.000000,  1.000000);

    const static M44f inverseBradfordCPM
        (0.986993,  0.432305, -0.008529,  0.000000,
        -0.147054,  0.518360,  0.040043,  0.000000,
         0.159963,  0.049291,  0.968487,  0.000000,
         0.000000,  0.000000,  0.000000,  1.000000);

    //
    // Convert the white points of the two RGB spaces to XYZ
    //

    float fx = fileNeutral.x;
    float fy = fileNeutral.y;
    V3f fileNeutralXYZ (fx / fy, 1, (1 - fx - fy) / fy);

    float ax = acesNeutral.x;
    float ay = acesNeutral.y;
    V3f acesNeutralXYZ (ax / ay, 1, (1 - ax - ay) / ay);

    //
    // Compute the Bradford transformation matrix
    //

    V3f ratio ((acesNeutralXYZ * bradfordCPM) /
           (fileNeutralXYZ * bradfordCPM));

    M44f ratioMat (ratio[0], 0,        0,        0,
           0,        ratio[1], 0,        0,
           0,        0,        ratio[2], 0,
           0,        0,        0,        1);

    M44f bradfordTrans = bradfordCPM *
                         ratioMat *
             inverseBradfordCPM;

    //
    // Build a combined file-RGB-to-ACES-RGB conversion matrix
    //

    fileToAces = RGBtoXYZ (fileChr, 1) * bradfordTrans * XYZtoRGB (acesChr, 1);
}


AcesInputFile::AcesInputFile (const std::string &name, int numThreads):
    _data (new Data)
{
    _data->rgbaFile = new RgbaInputFile (name.c_str(), numThreads);
    _data->initColorConversion();
}


AcesInputFile::AcesInputFile (IStream &is, int numThreads):
    _data (new Data)
{
    _data->rgbaFile = new RgbaInputFile (is, numThreads);
    _data->initColorConversion();
}


AcesInputFile::~AcesInputFile ()
{
    delete _data;
}


void
AcesInputFile::setFrameBuffer (Rgba *base, size_t xStride, size_t yStride)
{
    _data->rgbaFile->setFrameBuffer (base, xStride, yStride);
    _data->fbBase = base;
    _data->fbXStride = xStride;
    _data->fbYStride = yStride;
}


void
AcesInputFile::readPixels (int scanLine1, int scanLine2)
{
    //
    // Copy the pixels from the RgbaInputFile into the frame buffer.
    //

    _data->rgbaFile->readPixels (scanLine1, scanLine2);

    //
    // If the RGB space of the input file is not the same as the ACES
    // RGB space, then the pixels in the frame buffer must be transformed
    // into the ACES RGB space.
    //

    if (!_data->mustConvertColor)
    return;

    int minY = min (scanLine1, scanLine2);
    int maxY = max (scanLine1, scanLine2);

    for (int y = minY; y <= maxY; ++y)
    {
    Rgba *base = _data->fbBase +
             _data->fbXStride * _data->minX +
             _data->fbYStride * y;

    for (int x = _data->minX; x <= _data->maxX; ++x)
    {
        V3f aces = V3f (base->r, base->g, base->b) * _data->fileToAces;

        base->r = aces[0];
        base->g = aces[1];
        base->b = aces[2];

        base += _data->fbXStride;
    }
    }
}


void
AcesInputFile::readPixels (int scanLine)
{
    readPixels (scanLine, scanLine);
}


const Header &
AcesInputFile::header () const
{
    return _data->rgbaFile->header();
}


const Imath::Box2i &
AcesInputFile::displayWindow () const
{
    return _data->rgbaFile->displayWindow();
}


const Imath::Box2i &
AcesInputFile::dataWindow () const
{
    return _data->rgbaFile->dataWindow();
}


float
AcesInputFile::pixelAspectRatio () const
{
    return _data->rgbaFile->pixelAspectRatio();
}


const Imath::V2f
AcesInputFile::screenWindowCenter () const
{
    return _data->rgbaFile->screenWindowCenter();
}


float
AcesInputFile::screenWindowWidth () const
{
    return _data->rgbaFile->screenWindowWidth();
}


LineOrder
AcesInputFile::lineOrder () const
{
    return _data->rgbaFile->lineOrder();
}


Compression
AcesInputFile::compression () const
{
    return _data->rgbaFile->compression();
}


RgbaChannels
AcesInputFile::channels () const
{
    return _data->rgbaFile->channels();
}


const char *
AcesInputFile::fileName () const
{
    return _data->rgbaFile->fileName();
}


bool
AcesInputFile::isComplete () const
{
    return _data->rgbaFile->isComplete();
}


int
AcesInputFile::version () const
{
    return _data->rgbaFile->version();
}

} // namespace Imf