ImfCRgbaFile.cpp 25.2 KB
Newer Older
1 2 3 4
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
5
//
6
// All rights reserved.
7
//
8 9 10 11 12 13 14 15 16 17 18
// 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
19 20
// from this software without specific prior written permission.
//
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
// 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.
//
///////////////////////////////////////////////////////////////////////////


//-----------------------------------------------------------------------------
//
//	C interface to C++ classes Imf::RgbaOutputFile and Imf::RgbaInputFile
//
//-----------------------------------------------------------------------------


#include <ImfCRgbaFile.h>
#include <ImfRgbaFile.h>
#include <ImfTiledRgbaFile.h>
#include <ImfIntAttribute.h>
#include <ImfFloatAttribute.h>
#include <ImfDoubleAttribute.h>
#include <ImfStringAttribute.h>
#include <ImfBoxAttribute.h>
#include <ImfVecAttribute.h>
#include <ImfMatrixAttribute.h>
#include <ImfChannelList.h>
#include <ImfLut.h>
#include "half.h"
#include <string.h>

using Imath::Box2i;
using Imath::Box2f;
using Imath::V2i;
using Imath::V2f;
using Imath::V3i;
using Imath::V3f;
using Imath::M33f;
using Imath::M44f;


namespace {


const int MAX_ERR_LENGTH = 1024;
char errorMessage[MAX_ERR_LENGTH];


void
setErrorMessage (const std::exception &e)
{
    strncpy (errorMessage, e.what(), MAX_ERR_LENGTH - 1);
    errorMessage[MAX_ERR_LENGTH - 1] = 0;
}


inline Imf::Header *
header (ImfHeader *hdr)
{
    return (Imf::Header *)(hdr);
}


inline const Imf::Header *
header (const ImfHeader *hdr)
{
    return (const Imf::Header *)(hdr);
}


inline Imf::RgbaOutputFile *
outfile (ImfOutputFile *out)
{
    return (Imf::RgbaOutputFile *) out;
}


inline const Imf::RgbaOutputFile *
outfile (const ImfOutputFile *out)
{
    return (const Imf::RgbaOutputFile *) out;
}


inline Imf::TiledRgbaOutputFile *
outfile (ImfTiledOutputFile *out)
{
    return (Imf::TiledRgbaOutputFile *) out;
}


inline const Imf::TiledRgbaOutputFile *
outfile (const ImfTiledOutputFile *out)
{
    return (const Imf::TiledRgbaOutputFile *) out;
}


inline Imf::RgbaInputFile *
infile (ImfInputFile *in)
{
    return (Imf::RgbaInputFile *) in;
}


inline const Imf::RgbaInputFile *
infile (const ImfInputFile *in)
{
    return (const Imf::RgbaInputFile *) in;
}


inline Imf::TiledRgbaInputFile *
infile (ImfTiledInputFile *in)
{
    return (Imf::TiledRgbaInputFile *) in;
}


inline const Imf::TiledRgbaInputFile *
infile (const ImfTiledInputFile *in)
{
    return (const Imf::TiledRgbaInputFile *) in;
}


} // namespace


156
void
157 158 159 160 161 162
ImfFloatToHalf (float f, ImfHalf *h)
{
    *h = half(f).bits();
}


163
void
164 165 166
ImfFloatToHalfArray (int n, const float f[/*n*/], ImfHalf h[/*n*/])
{
    for (int i = 0; i < n; ++i)
167
    h[i] = half(f[i]).bits();
168 169 170
}


171
float
172 173 174 175 176 177 178 179 180 181
ImfHalfToFloat (ImfHalf h)
{
    return float (*((half *)&h));
}


void
ImfHalfToFloatArray (int n, const ImfHalf h[/*n*/], float f[/*n*/])
{
    for (int i = 0; i < n; ++i)
182
    f[i] = float (*((half *)(h + i)));
183 184 185 186 187 188 189 190
}


ImfHeader *
ImfNewHeader (void)
{
    try
    {
191
    return (ImfHeader *) new Imf::Header;
192 193 194
    }
    catch (const std::exception &e)
    {
195 196
    setErrorMessage (e);
    return 0;
197 198 199 200
    }
}


201
void
202 203 204 205 206 207 208 209 210 211 212
ImfDeleteHeader (ImfHeader *hdr)
{
    delete header (hdr);
}


ImfHeader *
ImfCopyHeader (const ImfHeader *hdr)
{
    try
    {
213
    return (ImfHeader *) new Imf::Header (*header (hdr));
214 215 216
    }
    catch (const std::exception &e)
    {
217 218
    setErrorMessage (e);
    return 0;
219 220 221 222
    }
}


223
void
224
ImfHeaderSetDisplayWindow (ImfHeader *hdr,
225 226
               int xMin, int yMin,
               int xMax, int yMax)
227 228 229 230 231
{
    header(hdr)->displayWindow() = Box2i (V2i (xMin, yMin), V2i (xMax, yMax));
}


232
void
233
ImfHeaderDisplayWindow (const ImfHeader *hdr,
234 235
            int *xMin, int *yMin,
            int *xMax, int *yMax)
236 237 238 239 240 241 242 243 244 245 246
{
    const Box2i dw = header(hdr)->displayWindow();
    *xMin = dw.min.x;
    *yMin = dw.min.y;
    *xMax = dw.max.x;
    *yMax = dw.max.y;
}


void
ImfHeaderSetDataWindow (ImfHeader *hdr,
247 248
            int xMin, int yMin,
            int xMax, int yMax)
249 250 251 252 253
{
    header(hdr)->dataWindow() = Box2i (V2i (xMin, yMin), V2i (xMax, yMax));
}


254
void
255
ImfHeaderDataWindow (const ImfHeader *hdr,
256 257
             int *xMin, int *yMin,
             int *xMax, int *yMax)
258 259 260 261 262 263 264 265 266
{
    const Box2i dw = header(hdr)->dataWindow();
    *xMin = dw.min.x;
    *yMin = dw.min.y;
    *xMax = dw.max.x;
    *yMax = dw.max.y;
}


267
void
268 269 270 271 272 273
ImfHeaderSetPixelAspectRatio (ImfHeader *hdr, float pixelAspectRatio)
{
    header(hdr)->pixelAspectRatio() = pixelAspectRatio;
}


274
float
275 276 277 278 279 280
ImfHeaderPixelAspectRatio (const ImfHeader *hdr)
{
    return header(hdr)->pixelAspectRatio();
}


281
void
282 283 284 285 286 287
ImfHeaderSetScreenWindowCenter (ImfHeader *hdr, float x, float y)
{
    header(hdr)->screenWindowCenter() = V2f (x, y);
}


288
void
289 290 291 292 293 294 295 296
ImfHeaderScreenWindowCenter (const ImfHeader *hdr, float *x, float *y)
{
    const V2i &swc = header(hdr)->screenWindowCenter();
    *x = swc.x;
    *y = swc.y;
}


297
void
298 299 300 301 302 303
ImfHeaderSetScreenWindowWidth (ImfHeader *hdr, float width)
{
    header(hdr)->screenWindowWidth() = width;
}


304
float
305 306 307 308 309 310
ImfHeaderScreenWindowWidth (const ImfHeader *hdr)
{
    return header(hdr)->screenWindowWidth();
}


311
void
312 313 314 315 316 317
ImfHeaderSetLineOrder (ImfHeader *hdr, int lineOrder)
{
    header(hdr)->lineOrder() = Imf::LineOrder (lineOrder);
}


318
int
319 320 321 322 323
ImfHeaderLineOrder (const ImfHeader *hdr)
{
    return header(hdr)->lineOrder();
}

324 325

void
326 327 328 329 330 331
ImfHeaderSetCompression (ImfHeader *hdr, int compression)
{
    header(hdr)->compression() = Imf::Compression (compression);
}


332
int
333 334 335 336 337 338
ImfHeaderCompression (const ImfHeader *hdr)
{
    return header(hdr)->compression();
}


339
int
340 341 342 343
ImfHeaderSetIntAttribute (ImfHeader *hdr, const char name[], int value)
{
    try
    {
344 345 346 347 348 349 350 351 352
    if (header(hdr)->find(name) == header(hdr)->end())
    {
        header(hdr)->insert (name, Imf::IntAttribute (value));
    }
    else
    {
        header(hdr)->typedAttribute<Imf::IntAttribute>(name).value() =
        value;
    }
353

354
    return 1;
355 356 357
    }
    catch (const std::exception &e)
    {
358 359
    setErrorMessage (e);
    return 0;
360 361 362 363
    }
}


364
int
365 366 367 368
ImfHeaderIntAttribute (const ImfHeader *hdr, const char name[], int *value)
{
    try
    {
369 370
    *value = header(hdr)->typedAttribute<Imf::IntAttribute>(name).value();
    return 1;
371 372 373
    }
    catch (const std::exception &e)
    {
374 375
    setErrorMessage (e);
    return 0;
376 377 378 379
    }
}


380
int
381 382 383 384
ImfHeaderSetFloatAttribute (ImfHeader *hdr, const char name[], float value)
{
    try
    {
385 386 387 388 389 390 391 392 393
    if (header(hdr)->find(name) == header(hdr)->end())
    {
        header(hdr)->insert (name, Imf::FloatAttribute (value));
    }
    else
    {
        header(hdr)->typedAttribute<Imf::FloatAttribute>(name).value() =
        value;
    }
394

395
    return 1;
396 397 398
    }
    catch (const std::exception &e)
    {
399 400
    setErrorMessage (e);
    return 0;
401 402 403 404
    }
}


405
int
406 407 408 409
ImfHeaderSetDoubleAttribute (ImfHeader *hdr, const char name[], double value)
{
    try
    {
410 411 412 413 414 415 416 417 418
    if (header(hdr)->find(name) == header(hdr)->end())
    {
        header(hdr)->insert (name, Imf::DoubleAttribute (value));
    }
    else
    {
        header(hdr)->typedAttribute<Imf::DoubleAttribute>(name).value() =
        value;
    }
419

420
    return 1;
421 422 423
    }
    catch (const std::exception &e)
    {
424 425
    setErrorMessage (e);
    return 0;
426 427 428 429 430 431 432 433 434
    }
}


int
ImfHeaderFloatAttribute (const ImfHeader *hdr, const char name[], float *value)
{
    try
    {
435 436
    *value = header(hdr)->typedAttribute<Imf::FloatAttribute>(name).value();
    return 1;
437 438 439
    }
    catch (const std::exception &e)
    {
440 441
    setErrorMessage (e);
    return 0;
442 443 444 445 446 447
    }
}


int
ImfHeaderDoubleAttribute (const ImfHeader *hdr,
448 449
              const char name[],
              double *value)
450 451 452
{
    try
    {
453 454
    *value = header(hdr)->
        typedAttribute<Imf::DoubleAttribute>(name).value();
455

456
    return 1;
457 458 459
    }
    catch (const std::exception &e)
    {
460 461
    setErrorMessage (e);
    return 0;
462 463 464 465 466 467
    }
}


int
ImfHeaderSetStringAttribute (ImfHeader *hdr,
468 469
                 const char name[],
                 const char value[])
470 471 472
{
    try
    {
473 474 475 476 477 478 479 480 481
    if (header(hdr)->find(name) == header(hdr)->end())
    {
        header(hdr)->insert (name, Imf::StringAttribute (value));
    }
    else
    {
        header(hdr)->typedAttribute<Imf::StringAttribute>(name).value() =
        value;
    }
482

483
    return 1;
484 485 486
    }
    catch (const std::exception &e)
    {
487 488
    setErrorMessage (e);
    return 0;
489 490 491 492 493 494
    }
}


int
ImfHeaderStringAttribute (const ImfHeader *hdr,
495 496
              const char name[],
              const char **value)
497 498 499
{
    try
    {
500 501
    *value = header(hdr)->
        typedAttribute<Imf::StringAttribute>(name).value().c_str();
502

503
    return 1;
504 505 506
    }
    catch (const std::exception &e)
    {
507 508
    setErrorMessage (e);
    return 0;
509 510 511 512 513 514
    }
}


int
ImfHeaderSetBox2iAttribute (ImfHeader *hdr,
515 516 517
                const char name[],
                int xMin, int yMin,
                int xMax, int yMax)
518 519 520
{
    try
    {
521
    Box2i box (V2i (xMin, yMin), V2i (xMax, yMax));
522

523 524 525 526 527 528 529 530 531
    if (header(hdr)->find(name) == header(hdr)->end())
    {
        header(hdr)->insert (name, Imf::Box2iAttribute (box));
    }
    else
    {
        header(hdr)->typedAttribute<Imf::Box2iAttribute>(name).value() =
        box;
    }
532

533
    return 1;
534 535 536
    }
    catch (const std::exception &e)
    {
537 538
    setErrorMessage (e);
    return 0;
539 540 541 542 543 544
    }
}


int
ImfHeaderBox2iAttribute (const ImfHeader *hdr,
545 546 547
             const char name[],
             int *xMin, int *yMin,
             int *xMax, int *yMax)
548 549 550
{
    try
    {
551 552
    const Box2i &box =
        header(hdr)->typedAttribute<Imf::Box2iAttribute>(name).value();
553

554 555 556 557
    *xMin = box.min.x;
    *yMin = box.min.y;
    *xMax = box.max.x;
    *yMax = box.max.y;
558

559
    return 1;
560 561 562
    }
    catch (const std::exception &e)
    {
563 564
    setErrorMessage (e);
    return 0;
565 566 567 568 569 570
    }
}


int
ImfHeaderSetBox2fAttribute (ImfHeader *hdr,
571 572 573
                const char name[],
                float xMin, float yMin,
                float xMax, float yMax)
574 575 576
{
    try
    {
577
    Box2f box (V2f (xMin, yMin), V2f (xMax, yMax));
578

579 580 581 582 583 584 585 586 587
    if (header(hdr)->find(name) == header(hdr)->end())
    {
        header(hdr)->insert (name, Imf::Box2fAttribute (box));
    }
    else
    {
        header(hdr)->typedAttribute<Imf::Box2fAttribute>(name).value() =
        box;
    }
588

589
    return 1;
590 591 592
    }
    catch (const std::exception &e)
    {
593 594
    setErrorMessage (e);
    return 0;
595 596 597 598 599 600
    }
}


int
ImfHeaderBox2fAttribute (const ImfHeader *hdr,
601 602 603
             const char name[],
             float *xMin, float *yMin,
             float *xMax, float *yMax)
604 605 606
{
    try
    {
607 608
    const Box2f &box =
        header(hdr)->typedAttribute<Imf::Box2fAttribute>(name).value();
609

610 611 612 613
    *xMin = box.min.x;
    *yMin = box.min.y;
    *xMax = box.max.x;
    *yMax = box.max.y;
614

615
    return 1;
616 617 618
    }
    catch (const std::exception &e)
    {
619 620
    setErrorMessage (e);
    return 0;
621 622 623 624 625 626
    }
}


int
ImfHeaderSetV2iAttribute (ImfHeader *hdr,
627 628
              const char name[],
              int x, int y)
629 630 631
{
    try
    {
632
    V2i v (x, y);
633

634 635 636 637
    if (header(hdr)->find(name) == header(hdr)->end())
        header(hdr)->insert (name, Imf::V2iAttribute (v));
    else
        header(hdr)->typedAttribute<Imf::V2iAttribute>(name).value() = v;
638

639
    return 1;
640 641 642
    }
    catch (const std::exception &e)
    {
643 644
    setErrorMessage (e);
    return 0;
645 646 647 648 649 650
    }
}


int
ImfHeaderV2iAttribute (const ImfHeader *hdr,
651 652
               const char name[],
               int *x, int *y)
653 654 655
{
    try
    {
656 657
    const V2i &v =
        header(hdr)->typedAttribute<Imf::V2iAttribute>(name).value();
658

659 660
    *x = v.x;
    *y = v.y;
661

662
    return 1;
663 664 665
    }
    catch (const std::exception &e)
    {
666 667
    setErrorMessage (e);
    return 0;
668 669 670 671
    }
}


672
int
673
ImfHeaderSetV2fAttribute (ImfHeader *hdr,
674 675
              const char name[],
              float x, float y)
676 677 678
{
    try
    {
679
    V2f v (x, y);
680

681 682 683 684
    if (header(hdr)->find(name) == header(hdr)->end())
        header(hdr)->insert (name, Imf::V2fAttribute (v));
    else
        header(hdr)->typedAttribute<Imf::V2fAttribute>(name).value() = v;
685

686
    return 1;
687 688 689
    }
    catch (const std::exception &e)
    {
690 691
    setErrorMessage (e);
    return 0;
692 693 694 695 696 697
    }
}


int
ImfHeaderV2fAttribute (const ImfHeader *hdr,
698 699
               const char name[],
               float *x, float *y)
700 701 702
{
    try
    {
703 704
    const V2f &v =
        header(hdr)->typedAttribute<Imf::V2fAttribute>(name).value();
705

706 707
    *x = v.x;
    *y = v.y;
708

709
    return 1;
710 711 712
    }
    catch (const std::exception &e)
    {
713 714
    setErrorMessage (e);
    return 0;
715 716 717 718 719 720
    }
}


int
ImfHeaderSetV3iAttribute (ImfHeader *hdr,
721 722
              const char name[],
              int x, int y, int z)
723 724 725
{
    try
    {
726
    V3i v (x, y, z);
727

728 729 730 731
    if (header(hdr)->find(name) == header(hdr)->end())
        header(hdr)->insert (name, Imf::V3iAttribute (v));
    else
        header(hdr)->typedAttribute<Imf::V3iAttribute>(name).value() = v;
732

733
    return 1;
734 735 736
    }
    catch (const std::exception &e)
    {
737 738
    setErrorMessage (e);
    return 0;
739 740 741 742 743 744
    }
}


int
ImfHeaderV3iAttribute (const ImfHeader *hdr,
745 746
               const char name[],
               int *x, int *y, int *z)
747 748 749
{
    try
    {
750 751
    const V3i &v =
        header(hdr)->typedAttribute<Imf::V3iAttribute>(name).value();
752

753 754 755
    *x = v.x;
    *y = v.y;
    *z = v.z;
756

757
    return 1;
758 759 760
    }
    catch (const std::exception &e)
    {
761 762
    setErrorMessage (e);
    return 0;
763 764 765 766 767 768
    }
}


int
ImfHeaderSetV3fAttribute (ImfHeader *hdr,
769 770
              const char name[],
              float x, float y, float z)
771 772 773
{
    try
    {
774
    V3f v (x, y, z);
775

776 777 778 779
    if (header(hdr)->find(name) == header(hdr)->end())
        header(hdr)->insert (name, Imf::V3fAttribute (v));
    else
        header(hdr)->typedAttribute<Imf::V3fAttribute>(name).value() = v;
780

781
    return 1;
782 783 784
    }
    catch (const std::exception &e)
    {
785 786
    setErrorMessage (e);
    return 0;
787 788 789 790 791 792
    }
}


int
ImfHeaderV3fAttribute (const ImfHeader *hdr,
793 794
               const char name[],
               float *x, float *y, float *z)
795 796 797
{
    try
    {
798 799
    const V3f &v =
        header(hdr)->typedAttribute<Imf::V3fAttribute>(name).value();
800

801 802 803
    *x = v.x;
    *y = v.y;
    *z = v.z;
804

805
    return 1;
806 807 808
    }
    catch (const std::exception &e)
    {
809 810
    setErrorMessage (e);
    return 0;
811 812 813 814 815 816
    }
}


int
ImfHeaderSetM33fAttribute (ImfHeader *hdr,
817 818
               const char name[],
               const float m[3][3])
819 820 821
{
    try
    {
822
    M33f m3 (m);
823

824 825 826 827
    if (header(hdr)->find(name) == header(hdr)->end())
        header(hdr)->insert (name, Imf::M33fAttribute (m3));
    else
        header(hdr)->typedAttribute<Imf::M33fAttribute>(name).value() = m3;
828

829
    return 1;
830 831 832
    }
    catch (const std::exception &e)
    {
833 834
    setErrorMessage (e);
    return 0;
835 836 837 838 839 840
    }
}


int
ImfHeaderM33fAttribute (const ImfHeader *hdr,
841 842
            const char name[],
            float m[3][3])
843 844 845
{
    try
    {
846 847
    const M33f &m3 =
        header(hdr)->typedAttribute<Imf::M33fAttribute>(name).value();
848

849 850 851
    m[0][0] = m3[0][0];
    m[0][1] = m3[0][1];
    m[0][2] = m3[0][2];
852

853 854 855
    m[1][0] = m3[1][0];
    m[1][1] = m3[1][1];
    m[1][2] = m3[1][2];
856

857 858 859
    m[2][0] = m3[2][0];
    m[2][1] = m3[2][1];
    m[2][2] = m3[2][2];
860

861
    return 1;
862 863 864
    }
    catch (const std::exception &e)
    {
865 866
    setErrorMessage (e);
    return 0;
867 868 869 870 871 872
    }
}


int
ImfHeaderSetM44fAttribute (ImfHeader *hdr,
873 874
               const char name[],
               const float m[4][4])
875 876 877
{
    try
    {
878
    M44f m4 (m);
879

880 881 882 883
    if (header(hdr)->find(name) == header(hdr)->end())
        header(hdr)->insert (name, Imf::M44fAttribute (m4));
    else
        header(hdr)->typedAttribute<Imf::M44fAttribute>(name).value() = m4;
884

885
    return 1;
886 887 888
    }
    catch (const std::exception &e)
    {
889 890
    setErrorMessage (e);
    return 0;
891 892 893 894 895 896
    }
}


int
ImfHeaderM44fAttribute (const ImfHeader *hdr,
897 898
            const char name[],
            float m[4][4])
899 900 901
{
    try
    {
902 903
    const M44f &m4 =
        header(hdr)->typedAttribute<Imf::M44fAttribute>(name).value();
904

905 906 907 908
    m[0][0] = m4[0][0];
    m[0][1] = m4[0][1];
    m[0][2] = m4[0][2];
    m[0][3] = m4[0][3];
909

910 911 912 913
    m[1][0] = m4[1][0];
    m[1][1] = m4[1][1];
    m[1][2] = m4[1][2];
    m[1][3] = m4[1][3];
914

915 916 917 918
    m[2][0] = m4[2][0];
    m[2][1] = m4[2][1];
    m[2][2] = m4[2][2];
    m[2][3] = m4[2][3];
919

920 921 922 923
    m[3][0] = m4[3][0];
    m[3][1] = m4[3][1];
    m[3][2] = m4[3][2];
    m[3][3] = m4[3][3];
924

925
    return 1;
926 927 928
    }
    catch (const std::exception &e)
    {
929 930
    setErrorMessage (e);
    return 0;
931 932 933 934
    }
}


935
ImfOutputFile *
936 937 938 939
ImfOpenOutputFile (const char name[], const ImfHeader *hdr, int channels)
{
    try
    {
940 941
    return (ImfOutputFile *) new Imf::RgbaOutputFile
        (name, *header(hdr), Imf::RgbaChannels (channels));
942 943 944
    }
    catch (const std::exception &e)
    {
945 946
    setErrorMessage (e);
    return 0;
947 948 949 950
    }
}


951
int
952 953 954 955
ImfCloseOutputFile (ImfOutputFile *out)
{
    try
    {
956 957
    delete outfile (out);
    return 1;
958 959 960
    }
    catch (const std::exception &e)
    {
961 962
    setErrorMessage (e);
    return 0;
963 964 965 966
    }
}


967
int
968
ImfOutputSetFrameBuffer (ImfOutputFile *out,
969 970 971
             const ImfRgba *base,
             size_t xStride,
             size_t yStride)
972 973 974
{
    try
    {
975 976
    outfile(out)->setFrameBuffer ((Imf::Rgba *)base, xStride, yStride);
    return 1;
977 978 979
    }
    catch (const std::exception &e)
    {
980 981
    setErrorMessage (e);
    return 0;
982 983 984 985
    }
}


986
int
987 988 989 990
ImfOutputWritePixels (ImfOutputFile *out, int numScanLines)
{
    try
    {
991 992
    outfile(out)->writePixels (numScanLines);
    return 1;
993 994 995
    }
    catch (const std::exception &e)
    {
996 997
    setErrorMessage (e);
    return 0;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
    }
}


int
ImfOutputCurrentScanLine (const ImfOutputFile *out)
{
    return outfile(out)->currentScanLine();
}


const ImfHeader *
ImfOutputHeader (const ImfOutputFile *out)
{
    return (const ImfHeader *) &outfile(out)->header();
}


int
ImfOutputChannels (const ImfOutputFile *out)
{
    return outfile(out)->channels();
}


1023
ImfTiledOutputFile *
1024
ImfOpenTiledOutputFile (const char name[],
1025 1026 1027 1028
            const ImfHeader *hdr,
            int channels,
            int xSize, int ySize,
            int mode, int rmode)
1029 1030 1031
{
    try
    {
1032 1033 1034 1035 1036 1037
    return (ImfTiledOutputFile *) new Imf::TiledRgbaOutputFile
            (name, *header(hdr),
             Imf::RgbaChannels (channels),
             xSize, ySize,
             Imf::LevelMode (mode),
             Imf::LevelRoundingMode (rmode));
1038 1039 1040
    }
    catch (const std::exception &e)
    {
1041 1042
    setErrorMessage (e);
    return 0;
1043 1044 1045 1046
    }
}


1047
int
1048 1049 1050 1051
ImfCloseTiledOutputFile (ImfTiledOutputFile *out)
{
    try
    {
1052 1053
    delete outfile (out);
    return 1;
1054 1055 1056
    }
    catch (const std::exception &e)
    {
1057 1058
    setErrorMessage (e);
    return 0;
1059 1060 1061 1062
    }
}


1063
int
1064
ImfTiledOutputSetFrameBuffer (ImfTiledOutputFile *out,
1065 1066 1067
                  const ImfRgba *base,
                  size_t xStride,
                  size_t yStride)
1068 1069 1070
{
    try
    {
1071 1072
    outfile(out)->setFrameBuffer ((Imf::Rgba *)base, xStride, yStride);
    return 1;
1073 1074 1075
    }
    catch (const std::exception &e)
    {
1076 1077
    setErrorMessage (e);
    return 0;
1078 1079 1080 1081
    }
}


1082
int
1083
ImfTiledOutputWriteTile (ImfTiledOutputFile *out,
1084 1085
             int dx, int dy,
             int lx, int ly)
1086 1087 1088
{
    try
    {
1089 1090
    outfile(out)->writeTile (dx, dy, lx, ly);
    return 1;
1091 1092 1093
    }
    catch (const std::exception &e)
    {
1094 1095
    setErrorMessage (e);
    return 0;
1096 1097 1098 1099
    }
}


1100
int
1101
ImfTiledOutputWriteTiles (ImfTiledOutputFile *out,
1102
              int dxMin, int dxMax,
1103
                          int dyMin, int dyMax,
1104
              int lx, int ly)
1105 1106 1107
{
    try
    {
1108 1109
    outfile(out)->writeTiles (dxMin, dxMax, dyMin, dyMax, lx, ly);
    return 1;
1110 1111 1112
    }
    catch (const std::exception &e)
    {
1113 1114
    setErrorMessage (e);
    return 0;
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    }
}


const ImfHeader *
ImfTiledOutputHeader (const ImfTiledOutputFile *out)
{
    return (const ImfHeader *) &outfile(out)->header();
}


int
ImfTiledOutputChannels (const ImfTiledOutputFile *out)
{
    return outfile(out)->channels();
}


int
ImfTiledOutputTileXSize (const ImfTiledOutputFile *out)
{
    return outfile(out)->tileXSize();
}


int
ImfTiledOutputTileYSize (const ImfTiledOutputFile *out)
{
    return outfile(out)->tileYSize();
}


int
ImfTiledOutputLevelMode (const ImfTiledOutputFile *out)
{
    return outfile(out)->levelMode();
}


int
ImfTiledOutputLevelRoundingMode (const ImfTiledOutputFile *out)
{
    return outfile(out)->levelRoundingMode();
}


1161
ImfInputFile *
1162 1163 1164 1165
ImfOpenInputFile (const char name[])
{
    try
    {
1166
    return (ImfInputFile *) new Imf::RgbaInputFile (name);
1167 1168 1169
    }
    catch (const std::exception &e)
    {
1170 1171
    setErrorMessage (e);
    return 0;
1172 1173 1174 1175 1176 1177 1178 1179 1180
    }
}


int
ImfCloseInputFile (ImfInputFile *in)
{
    try
    {
1181 1182
    delete infile (in);
    return 1;
1183 1184 1185
    }
    catch (const std::exception &e)
    {
1186 1187
    setErrorMessage (e);
    return 0;
1188 1189 1190 1191
    }
}


1192
int
1193
ImfInputSetFrameBuffer (ImfInputFile *in,
1194 1195 1196
            ImfRgba *base,
            size_t xStride,
            size_t yStride)
1197 1198 1199
{
    try
    {
1200 1201
    infile(in)->setFrameBuffer ((Imf::Rgba *) base, xStride, yStride);
    return 1;
1202 1203 1204
    }
    catch (const std::exception &e)
    {
1205 1206
    setErrorMessage (e);
    return 0;
1207 1208 1209 1210 1211 1212 1213 1214 1215
    }
}


int
ImfInputReadPixels (ImfInputFile *in, int scanLine1, int scanLine2)
{
    try
    {
1216 1217
    infile(in)->readPixels (scanLine1, scanLine2);
    return 1;
1218 1219 1220
    }
    catch (const std::exception &e)
    {
1221 1222
    setErrorMessage (e);
    return 0;
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
    }
}


const ImfHeader *
ImfInputHeader (const ImfInputFile *in)
{
    return (const ImfHeader *) &infile(in)->header();
}


int
ImfInputChannels (const ImfInputFile *in)
{
    return infile(in)->channels();
}


const char *
ImfInputFileName (const ImfInputFile *in)
{
    return infile(in)->fileName();
}


1248
ImfTiledInputFile *
1249 1250 1251 1252
ImfOpenTiledInputFile (const char name[])
{
    try
    {
1253
    return (ImfTiledInputFile *) new Imf::TiledRgbaInputFile (name);
1254 1255 1256
    }
    catch (const std::exception &e)
    {
1257 1258
    setErrorMessage (e);
    return 0;
1259 1260 1261 1262 1263 1264 1265 1266 1267
    }
}


int
ImfCloseTiledInputFile (ImfTiledInputFile *in)
{
    try
    {
1268 1269
    delete infile (in);
    return 1;
1270 1271 1272
    }
    catch (const std::exception &e)
    {
1273 1274
    setErrorMessage (e);
    return 0;
1275 1276 1277 1278
    }
}


1279
int
1280
ImfTiledInputSetFrameBuffer (ImfTiledInputFile *in,
1281 1282 1283
                 ImfRgba *base,
                 size_t xStride,
                 size_t yStride)
1284 1285 1286
{
    try
    {
1287 1288
    infile(in)->setFrameBuffer ((Imf::Rgba *) base, xStride, yStride);
    return 1;
1289 1290 1291
    }
    catch (const std::exception &e)
    {
1292 1293
    setErrorMessage (e);
    return 0;
1294 1295 1296 1297 1298 1299
    }
}


int
ImfTiledInputReadTile (ImfTiledInputFile *in,
1300 1301
               int dx, int dy,
               int lx, int ly)
1302 1303 1304
{
    try
    {
1305 1306
    infile(in)->readTile (dx, dy, lx, ly);
    return 1;
1307 1308 1309
    }
    catch (const std::exception &e)
    {
1310 1311
    setErrorMessage (e);
    return 0;
1312 1313 1314 1315 1316 1317
    }
}


int
ImfTiledInputReadTiles (ImfTiledInputFile *in,
1318
                int dxMin, int dxMax,
1319
                        int dyMin, int dyMax,
1320
                int lx, int ly)
1321 1322 1323
{
    try
    {
1324 1325
    infile(in)->readTiles (dxMin, dxMax, dyMin, dyMax, lx, ly);
    return 1;
1326 1327 1328
    }
    catch (const std::exception &e)
    {
1329 1330
    setErrorMessage (e);
    return 0;
1331 1332 1333 1334 1335 1336 1337 1338 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 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
    }
}


const ImfHeader *
ImfTiledInputHeader (const ImfTiledInputFile *in)
{
    return (const ImfHeader *) &infile(in)->header();
}


int
ImfTiledInputChannels (const ImfTiledInputFile *in)
{
    return infile(in)->channels();
}


const char *
ImfTiledInputFileName (const ImfTiledInputFile *in)
{
    return infile(in)->fileName();
}


int
ImfTiledInputTileXSize (const ImfTiledInputFile *in)
{
    return infile(in)->tileXSize();
}


int
ImfTiledInputTileYSize (const ImfTiledInputFile *in)
{
    return infile(in)->tileYSize();
}


int
ImfTiledInputLevelMode (const ImfTiledInputFile *in)
{
    return infile(in)->levelMode();
}


int
ImfTiledInputLevelRoundingMode (const ImfTiledInputFile *in)
{
    return infile(in)->levelRoundingMode();
}


ImfLut *
ImfNewRound12logLut (int channels)
{
    try
    {
1389 1390
    return (ImfLut *) new Imf::RgbaLut
        (Imf::round12log, Imf::RgbaChannels (channels));
1391 1392 1393
    }
    catch (const std::exception &e)
    {
1394 1395
    setErrorMessage (e);
    return 0;
1396 1397 1398 1399 1400 1401 1402 1403 1404
    }
}


ImfLut *
ImfNewRoundNBitLut (unsigned int n, int channels)
{
    try
    {
1405 1406
    return (ImfLut *) new Imf::RgbaLut
        (Imf::roundNBit (n), Imf::RgbaChannels (channels));
1407 1408 1409
    }
    catch (const std::exception &e)
    {
1410 1411
    setErrorMessage (e);
    return 0;
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
    }
}


void
ImfDeleteLut (ImfLut *lut)
{
    delete (Imf::RgbaLut *) lut;
}


void
ImfApplyLut (ImfLut *lut, ImfRgba *data, int nData, int stride)
{
    ((Imf::RgbaLut *)lut)->apply ((Imf::Rgba *)data, nData, stride);
}


1430
const char *
1431 1432 1433 1434
ImfErrorMessage ()
{
    return errorMessage;
}