Blame exrmakepreview/main.cpp

Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
//
Packit 0d464f
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
Packit 0d464f
// Digital Ltd. LLC
Packit 0d464f
// 
Packit 0d464f
// All rights reserved.
Packit 0d464f
// 
Packit 0d464f
// Redistribution and use in source and binary forms, with or without
Packit 0d464f
// modification, are permitted provided that the following conditions are
Packit 0d464f
// met:
Packit 0d464f
// *       Redistributions of source code must retain the above copyright
Packit 0d464f
// notice, this list of conditions and the following disclaimer.
Packit 0d464f
// *       Redistributions in binary form must reproduce the above
Packit 0d464f
// copyright notice, this list of conditions and the following disclaimer
Packit 0d464f
// in the documentation and/or other materials provided with the
Packit 0d464f
// distribution.
Packit 0d464f
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 0d464f
// its contributors may be used to endorse or promote products derived
Packit 0d464f
// from this software without specific prior written permission. 
Packit 0d464f
// 
Packit 0d464f
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 0d464f
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 0d464f
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 0d464f
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 0d464f
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 0d464f
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 0d464f
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 0d464f
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 0d464f
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 0d464f
//
Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
Packit 0d464f
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
//
Packit 0d464f
//	exrmakepreview -- a program that inserts a
Packit 0d464f
//	preview image into an OpenEXR file's header.
Packit 0d464f
//
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
Packit 0d464f
#include "makePreview.h"
Packit 0d464f
Packit 0d464f
#include <iostream>
Packit 0d464f
#include <exception>
Packit 0d464f
#include <stdlib.h>
Packit 0d464f
#include <string.h>
Packit 0d464f
Packit 0d464f
using namespace std;
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
usageMessage (const char argv0[], bool verbose = false)
Packit 0d464f
{
Packit 0d464f
    cerr << "usage: " << argv0 << " [options] infile outfile" << endl;
Packit 0d464f
Packit 0d464f
    if (verbose)
Packit 0d464f
    {
Packit 0d464f
	cerr << "\n"
Packit 0d464f
		"Reads an OpenEXR image from infile, generates a preview\n"
Packit 0d464f
		"image, adds it to the image's header, and saves the result\n"
Packit 0d464f
		"in outfile.  Infile and outfile must not refer to the same\n"
Packit 0d464f
		"file (the program cannot edit an image file \"in place\").\n"
Packit 0d464f
		"\n"
Packit 0d464f
		"Options:\n"
Packit 0d464f
		"\n"
Packit 0d464f
		"-w x      sets the width of the preview image to x pixels\n"
Packit 0d464f
		"          (default is 100)\n"
Packit 0d464f
		"\n"
Packit 0d464f
		"-e s      adjusts the preview image's exposure by s f-stops\n"
Packit 0d464f
		"          (default is 0).  Positive values make the image\n"
Packit 0d464f
		"          brighter, negative values make it darker.\n"
Packit 0d464f
		"\n"
Packit 0d464f
		"-v        verbose mode\n"
Packit 0d464f
		"\n"
Packit 0d464f
		"-h        prints this message\n";
Packit 0d464f
Packit 0d464f
	 cerr << endl;
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    exit (1);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
int
Packit 0d464f
main(int argc, char **argv)
Packit 0d464f
{
Packit 0d464f
    const char *inFile = 0;
Packit 0d464f
    const char *outFile = 0;
Packit 0d464f
    int previewWidth = 100;
Packit 0d464f
    float exposure = 0;
Packit 0d464f
    bool verbose = false;
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // Parse the command line.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    if (argc < 2)
Packit 0d464f
	usageMessage (argv[0], true);
Packit 0d464f
Packit 0d464f
    int i = 1;
Packit 0d464f
Packit 0d464f
    while (i < argc)
Packit 0d464f
    {
Packit 0d464f
	if (!strcmp (argv[i], "-w"))
Packit 0d464f
	{
Packit 0d464f
	    //
Packit 0d464f
	    // Set preview image width
Packit 0d464f
	    //
Packit 0d464f
Packit 0d464f
	    if (i > argc - 2)
Packit 0d464f
		usageMessage (argv[0]);
Packit 0d464f
Packit 0d464f
	    previewWidth = strtol (argv[i + 1], 0, 0);
Packit 0d464f
	    i += 2;
Packit 0d464f
	}
Packit 0d464f
	else if (!strcmp (argv[i], "-e"))
Packit 0d464f
	{
Packit 0d464f
	    //
Packit 0d464f
	    // Set preview image width
Packit 0d464f
	    //
Packit 0d464f
Packit 0d464f
	    if (i > argc - 2)
Packit 0d464f
		usageMessage (argv[0]);
Packit 0d464f
Packit 0d464f
	    exposure = strtod (argv[i + 1], 0);
Packit 0d464f
	    i += 2;
Packit 0d464f
	}
Packit 0d464f
	else if (!strcmp (argv[i], "-v"))
Packit 0d464f
	{
Packit 0d464f
	    //
Packit 0d464f
	    // Verbose mode
Packit 0d464f
	    //
Packit 0d464f
Packit 0d464f
	    verbose = true;
Packit 0d464f
	    i += 1;
Packit 0d464f
	}
Packit 0d464f
	else if (!strcmp (argv[i], "-h"))
Packit 0d464f
	{
Packit 0d464f
	    //
Packit 0d464f
	    // Print help message
Packit 0d464f
	    //
Packit 0d464f
Packit 0d464f
	    usageMessage (argv[0], true);
Packit 0d464f
	}
Packit 0d464f
	else
Packit 0d464f
	{
Packit 0d464f
	    //
Packit 0d464f
	    // Image file name
Packit 0d464f
	    //
Packit 0d464f
Packit 0d464f
	    if (inFile == 0)
Packit 0d464f
		inFile = argv[i];
Packit 0d464f
	    else
Packit 0d464f
		outFile = argv[i];
Packit 0d464f
Packit 0d464f
	    i += 1;
Packit 0d464f
	}
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    if (inFile == 0 || outFile == 0)
Packit 0d464f
	usageMessage (argv[0]);
Packit 0d464f
Packit 0d464f
    if (!strcmp (inFile, outFile))
Packit 0d464f
    {
Packit 0d464f
	cerr << "Input and output cannot be the same file." << endl;
Packit 0d464f
	return 1;
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    if (previewWidth <= 0)
Packit 0d464f
    {
Packit 0d464f
	cerr << "Preview image width must be greather than zero." << endl;
Packit 0d464f
	return 1;
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // Load inFile, add a preview image, and save the result in outFile.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    int exitStatus = 0;
Packit 0d464f
Packit 0d464f
    try
Packit 0d464f
    {
Packit 0d464f
	makePreview (inFile, outFile, previewWidth, exposure, verbose);
Packit 0d464f
    }
Packit 0d464f
    catch (const exception &e)
Packit 0d464f
    {
Packit 0d464f
	cerr << e.what() << endl;
Packit 0d464f
	exitStatus = 1;
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    return exitStatus;
Packit 0d464f
}