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 633 634 635 636 637 638 639 640 641 642 643 644
|
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004, 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.
//
///////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
//
// exrstdattr -- a program that can set the values of most
// standard attributes in an OpenEXR image file's header.
//
//-----------------------------------------------------------------------------
#include <ImfStandardAttributes.h>
#include <ImfVecAttribute.h>
#include <ImfInputFile.h>
#include <ImfOutputFile.h>
#include <ImfTiledOutputFile.h>
#include <map>
#include <string>
#include <iostream>
#include <exception>
#include <stdlib.h>
using namespace std;
using namespace Imf;
using namespace Imath;
void
usageMessage (const char argv0[], bool verbose = false)
{
cerr << "usage: " << argv0 << " [options] infile outfile" << endl;
if (verbose)
{
cerr << "\n"
"Reads an OpenEXR image from infile, sets the values of\n"
"one or more standard attributes in the image's header,\n"
"and saves the result in outfile. Infile and outfile must\n"
"not refer to the same file (the program cannot edit an\n"
"image file \"in place\").\n"
"\n"
"Options for setting attribute values:\n"
"\n"
" -chromaticities f f f f f f f f\n"
" CIE xy chromaticities for the red, green\n"
" and blue primaries, and for the white point\n"
" (8 floats)\n"
"\n"
" -whiteLuminance f\n"
" white luminance, in candelas per square meter\n"
" (float, >= 0.0)\n"
"\n"
" -xDensity f\n"
" horizontal output density, in pixels per inch\n"
" (float, >= 0.0)\n"
"\n"
" -owner s\n"
" name of the owner of the image (string)\n"
"\n"
" -comments s\n"
" additional information about the image (string)\n"
"\n"
" -capDate s\n"
" date when the image was created or\n"
" captured, in local time (string,\n"
" formatted as YYYY:MM:DD hh:mm:ss)\n"
"\n"
" -utcOffset f\n"
" offset of local time at capDate from UTC, in\n"
" seconds (float, UTC == local time + x)\n"
"\n"
" -longitude f\n"
" -latitude f\n"
" -altitude f\n"
" location where the image was recorded, in\n"
" degrees east of Greenwich and north of the\n"
" equator, and in meters above sea level\n"
" (float)\n"
"\n"
" -focus f\n"
" the camera's focus distance, in meters\n"
" (float, > 0, or \"infinity\")\n"
"\n"
" -expTime f\n"
" exposure time, in seconds (float, >= 0)\n"
"\n"
" -aperture f\n"
" lens apterture, in f-stops (float, >= 0)\n"
"\n"
" -isoSpeed f\n"
" effective speed of the film or image\n"
" sensor that was used to record the image\n"
" (float, >= 0)\n"
"\n"
" -envmap s\n"
" indicates that the image is an environment map\n"
" (string, LATLONG or CUBE)\n"
"\n"
" -keyCode i i i i i i i\n"
" key code that uniquely identifies a motion\n"
" picture film frame using 7 integers:\n"
" * film manufacturer code (0 - 99)\n"
" * film type code (0 - 99)\n"
" * prefix to identify film roll (0 - 999999)\n"
" * count, increments once every perfsPerCount\n"
" perforations (0 - 9999)\n"
" * offset of frame, in perforations from\n"
" zero-frame reference mark (0 - 119)\n"
" * number of perforations per frame (1 - 15)\n"
" * number of perforations per count (20 - 120)\n"
"\n"
" -timeCode i i\n"
" SMPTE time and control code, specified as a pair\n"
" of 8-digit base-16 integers. The first number\n"
" contains the time address and flags (drop frame,\n"
" color frame, field/phase, bgf0, bgf1, bgf2).\n"
" The second number contains the user data and\n"
" control codes.\n"
"\n"
" -wrapmodes s\n"
" if the image is used as a texture map, specifies\n"
" how the image should be extrapolated outside the\n"
" zero-to-one texture coordinate range\n"
" (string, e.g. \"clamp\" or \"periodic,clamp\")\n"
"\n"
" -pixelAspectRatio f\n"
" width divided by height of a pixel\n"
" (float, >= 0)\n"
"\n"
" -screenWindowWidth f\n"
" width of the screen window (float, >= 0)\n"
"\n"
" -screenWindowCenter f f\n"
" center of the screen window (2 floats)\n"
"\n"
"Other Options:\n"
"\n"
" -h prints this message\n";
cerr << endl;
}
exit (1);
}
typedef map <string, Attribute *> AttrMap;
void
isNonNegative (const char attrName[], float f)
{
if (f < 0)
{
cerr << "The value for the " << attrName << " attribute "
"must not be less than zero." << endl;
exit (1);
}
}
void
isPositive (const char attrName[], float f)
{
if (f <= 0)
{
cerr << "The value for the " << attrName << " attribute "
"must be greater than zero." << endl;
exit (1);
}
}
void
notValidDate (const char attrName[])
{
cerr << "The value for the " << attrName << " attribute "
"is not a valid date of the form \"YYYY:MM:DD yy:mm:ss\"." << endl;
exit (1);
}
int
strToInt (const char str[], int length)
{
int x = 0;
for (int i = 0; i < length; ++i)
{
if (str[i] < '0' || str[i] > '9')
return -1;
x = x * 10 + (str[i] - '0');
}
return x;
}
void
isDate (const char attrName[], const char str[])
{
//
// Check that str represents a valid
// date of the form YYYY:MM:DD hh:mm:ss.
//
if (strlen (str) != 19 ||
str[4] != ':' || str[7] != ':' ||
str[10] != ' ' ||
str[13] != ':' || str[16] != ':')
{
notValidDate (attrName);
}
int Y = strToInt (str + 0, 4); // year
int M = strToInt (str + 5, 2); // month
int D = strToInt (str + 8, 2); // day
int h = strToInt (str + 11, 2); // hour
int m = strToInt (str + 14, 2); // minute
int s = strToInt (str + 17, 2); // second
if (Y < 0 ||
M < 1 || M > 12 ||
D < 1 ||
h < 0 || h > 23 ||
m < 0 || m > 59 ||
s < 0 || s > 59)
{
notValidDate (attrName);
}
if (M == 2)
{
bool leapYear = (Y % 4 == 0) && (Y % 100 != 0 || Y % 400 == 0);
if (D > (leapYear? 29: 28))
notValidDate (attrName);
}
else if (M == 4 || M == 6 || M == 9 || M == 11)
{
if (D > 30)
notValidDate (attrName);
}
else
{
if (D > 31)
notValidDate (attrName);
}
}
void
getFloat (const char attrName[],
int argc,
char **argv,
int &i,
AttrMap &attrs,
void (*check) (const char attrName[], float f) = 0)
{
if (i > argc - 2)
usageMessage (argv[0]);
float f = strtod (argv[i + 1], 0);
if (check)
check (attrName, f);
attrs[attrName] = new FloatAttribute (f);
i += 2;
}
void
getPosFloatOrInf (const char attrName[],
int argc,
char **argv,
int &i,
AttrMap &attrs)
{
if (i > argc - 2)
usageMessage (argv[0]);
float f;
if (!strcmp (argv[i + 1], "inf") || !strcmp (argv[i + 1], "infinity"))
{
f = float (half::posInf());
}
else
{
f = strtod (argv[i + 1], 0);
if (f <= 0)
{
cerr << "The value for the " << attrName << " attribute "
"must be greater than zero, or \"infinity\"." << endl;
exit (1);
}
}
attrs[attrName] = new FloatAttribute (f);
i += 2;
}
void
getV2f (const char attrName[],
int argc,
char **argv,
int &i,
AttrMap &attrs,
void (*check) (const char attrName[], const V2f &v) = 0)
{
if (i > argc - 3)
usageMessage (argv[0]);
V2f v (strtod (argv[i + 1], 0), strtod (argv[i + 2], 0));
if (check)
check (attrName, v);
attrs[attrName] = new V2fAttribute (v);
i += 3;
}
void
getString (const char attrName[],
int argc,
char **argv,
int &i,
AttrMap &attrs,
void (*check) (const char attrName[], const char str[]) = 0)
{
if (i > argc - 2)
usageMessage (argv[0]);
const char *str = argv[i + 1];
if (check)
check (attrName, str);
attrs[attrName] = new StringAttribute (str);
i += 2;
}
void
getChromaticities (const char attrName[],
int argc,
char **argv,
int &i,
AttrMap &attrs)
{
if (i > argc - 9)
usageMessage (argv[0]);
ChromaticitiesAttribute *a = new ChromaticitiesAttribute;
attrs[attrName] = a;
a->value().red.x = strtod (argv[i + 1], 0);
a->value().red.y = strtod (argv[i + 2], 0);
a->value().green.x = strtod (argv[i + 3], 0);
a->value().green.y = strtod (argv[i + 4], 0);
a->value().blue.x = strtod (argv[i + 5], 0);
a->value().blue.y = strtod (argv[i + 6], 0);
a->value().white.x = strtod (argv[i + 7], 0);
a->value().white.y = strtod (argv[i + 8], 0);
i += 9;
}
void
getEnvmap (const char attrName[],
int argc,
char **argv,
int &i,
AttrMap &attrs)
{
if (i > argc - 2)
usageMessage (argv[0]);
char *str = argv[i + 1];
Envmap type;
if (!strcmp (str, "latlong") || !strcmp (str, "LATLONG"))
{
type = ENVMAP_LATLONG;
}
else if (!strcmp (str, "cube") || !strcmp (str, "CUBE"))
{
type = ENVMAP_CUBE;
}
else
{
cerr << "The value for the " << attrName << " attribute "
"must be either LATLONG or CUBE." << endl;
exit (1);
}
attrs[attrName] = new EnvmapAttribute (type);
i += 2;
}
void
getKeyCode (const char attrName[],
int argc,
char **argv,
int &i,
AttrMap &attrs)
{
if (i > argc - 8)
usageMessage (argv[0]);
KeyCodeAttribute *a = new KeyCodeAttribute;
attrs[attrName] = a;
a->value().setFilmMfcCode (strtol (argv[i + 1], 0, 0));
a->value().setFilmType (strtol (argv[i + 2], 0, 0));
a->value().setPrefix (strtol (argv[i + 3], 0, 0));
a->value().setCount (strtol (argv[i + 4], 0, 0));
a->value().setPerfOffset (strtol (argv[i + 5], 0, 0));
a->value().setPerfsPerFrame (strtol (argv[i + 6], 0, 0));
a->value().setPerfsPerCount (strtol (argv[i + 7], 0, 0));
i += 8;
}
void
getTimeCode (const char attrName[],
int argc,
char **argv,
int &i,
AttrMap &attrs)
{
if (i > argc - 3)
usageMessage (argv[0]);
TimeCodeAttribute *a = new TimeCodeAttribute;
attrs[attrName] = a;
a->value().setTimeAndFlags (strtoul (argv[i + 1], 0, 16));
a->value().setUserData (strtoul (argv[i + 2], 0, 16));
i += 3;
}
int
main(int argc, char **argv)
{
//
// Parse the command line.
//
if (argc < 2)
usageMessage (argv[0], true);
int exitStatus = 0;
try
{
const char *inFileName = 0;
const char *outFileName = 0;
AttrMap attrs;
int i = 1;
while (i < argc)
{
const char *attrName = argv[i] + 1;
if (!strcmp (argv[i], "-chromaticities"))
{
getChromaticities (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-whiteLuminance"))
{
getFloat (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-xDensity"))
{
getFloat (attrName, argc, argv, i, attrs, isPositive);
}
else if (!strcmp (argv[i], "-owner"))
{
getString (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-comments"))
{
getString (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-capDate"))
{
getString (attrName, argc, argv, i, attrs, isDate);
}
else if (!strcmp (argv[i], "-utcOffset"))
{
getFloat (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-longitude"))
{
getFloat (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-latitude"))
{
getFloat (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-altitude"))
{
getFloat (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-focus"))
{
getPosFloatOrInf (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-expTime"))
{
getFloat (attrName, argc, argv, i, attrs, isPositive);
}
else if (!strcmp (argv[i], "-aperture"))
{
getFloat (attrName, argc, argv, i, attrs, isPositive);
}
else if (!strcmp (argv[i], "-isoSpeed"))
{
getFloat (attrName, argc, argv, i, attrs, isPositive);
}
else if (!strcmp (argv[i], "-envmap"))
{
getEnvmap (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-keyCode"))
{
getKeyCode (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-timeCode"))
{
getTimeCode (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-wrapmodes"))
{
getString (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-pixelAspectRatio"))
{
getFloat (attrName, argc, argv, i, attrs, isPositive);
}
else if (!strcmp (argv[i], "-screenWindowWidth"))
{
getFloat (attrName, argc, argv, i, attrs, isNonNegative);
}
else if (!strcmp (argv[i], "-screenWindowCenter"))
{
getV2f (attrName, argc, argv, i, attrs);
}
else if (!strcmp (argv[i], "-h"))
{
usageMessage (argv[0], true);
}
else
{
if (inFileName == 0)
inFileName = argv[i];
else
outFileName = argv[i];
i += 1;
}
}
if (inFileName == 0 || outFileName == 0)
usageMessage (argv[0]);
if (!strcmp (inFileName, outFileName))
{
cerr << "Input and output cannot be the same file." << endl;
return 1;
}
//
// Load the input file, add the new attributes,
// and save the result in the output file.
//
InputFile in (inFileName);
Header header = in.header();
for (AttrMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i)
header.insert (i->first.c_str(), *i->second);
if (header.hasTileDescription())
{
TiledOutputFile out (outFileName, header);
out.copyPixels (in);
}
else
{
OutputFile out (outFileName, header);
out.copyPixels (in);
}
}
catch (const exception &e)
{
cerr << e.what() << endl;
exitStatus = 1;
}
return exitStatus;
}
|