1 /**
2  * @brief Read files in Radiance's RGBE format
3  *
4  * This file is a part of PFSTOOLS package.
5  * ----------------------------------------------------------------------
6  * Copyright (C) 2003,2004 Rafal Mantiuk and Grzegorz Krawczyk
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  * ----------------------------------------------------------------------
22  *
23  * @author Grzegorz Krawczyk, <krawczyk@mpi-sb.mpg.de>
24  * @author Rafal Mantiuk, <mantiuk@mpi-sb.mpg.de>
25  *
26  * $Id: pfsinrgbe.cpp,v 1.5 2014/06/17 21:57:08 rafm Exp $
27  */
28 
29 #include <config.h>
30 
31 #include <cstdlib>
32 
33 #include <iostream>
34 
35 #include <getopt.h>
36 #include <pfs.h>
37 
38 #include "rgbeio.h"
39 
40 #define PROG_NAME "pfsinrgbe"
41 
42 class QuietException
43 {
44 };
45 
printHelp()46 void printHelp()
47 {
48   std::cerr << PROG_NAME " [--linear] [--radiance] [--verbose] [--help]" << std::endl
49             << "See man page for more information." << std::endl;
50 }
51 
52 
readFrames(int argc,char * argv[])53 void readFrames( int argc, char* argv[] )
54 {
55   pfs::DOMIO pfsio;
56 
57   bool verbose = false;
58   bool opt_linear=false;
59   bool radiance_compatibility = false;
60   bool quiet = false;
61 
62   // Parse command line parameters
63   static struct option cmdLineOptions[] = {
64     { "help", no_argument, NULL, 'h' },
65     { "verbose", no_argument, NULL, 'v' },
66     { "linear", no_argument, NULL, 'l' },
67 	{ "radiance", no_argument, NULL, 'r' },
68 	{ "quiet", no_argument, NULL, 'q' },
69 	{ NULL, 0, NULL, 0 }
70   };
71   static const char optstring[] = "lhvrq";
72 
73   pfs::FrameFileIterator it( argc, argv, "rb", NULL, stdin,
74     optstring, cmdLineOptions );
75 
76   int optionIndex = 0;
77   while( 1 ) {
78     int c = getopt_long (argc, argv, optstring, cmdLineOptions, &optionIndex);
79     if( c == -1 ) break;
80     switch( c ) {
81     case 'h':
82       printHelp();
83       throw QuietException();
84     case 'v':
85       verbose = true;
86       break;
87 	case 'r':
88 		radiance_compatibility = true;
89 		break;
90 	case 'q':
91 		quiet = true;
92 		break;
93 	case 'l':
94       std::cerr << PROG_NAME << " warning: linearize option ignored for an HDR input!"
95                 << std::endl;
96       break;
97     case '?':
98       throw QuietException();
99     case ':':
100       throw QuietException();
101     }
102   }
103 
104 /*  if (!quiet && !radiance_compatibility)
105 	  std::cerr << PROG_NAME << " warning: starting from pfstools 1.9.0, .hdr files are read without correcting for WHITE_EFFICIENCY,"
106 	  " which makes the absolute values incompatible with Radiance and previos versions of pfstools but compatible with most software."
107 	  " Use --radiance option to retain compatibility with Radiance and earlier versions of pfstools.  Use --quiet to suppress this message."
108 	  " Check the manual pages for pfsinrgbe for details." << std::endl;*/
109 
110   while( true ) {
111     pfs::FrameFile ff = it.getNextFrameFile();
112     if( ff.fh == NULL ) break; // No more frames
113 
114     RGBEReader reader( ff.fh, radiance_compatibility );
115 
116     VERBOSE_STR << "reading file (linear) '" << ff.fileName << "'" << std::endl;
117 
118     pfs::Frame *frame = pfsio.createFrame( reader.getWidth(),
119 					 reader.getHeight() );
120     pfs::Channel *X, *Y, *Z;
121     frame->createXYZChannels( X, Y, Z );
122 
123     //Store RGB data temporarily in XYZ channels
124     reader.readImage( X, Y, Z );
125     pfs::transformColorSpace( pfs::CS_RGB, X, Y, Z, pfs::CS_XYZ, X, Y, Z );
126 
127     frame->getTags()->setString("LUMINANCE", "RELATIVE");
128     const char *fileNameTag = strcmp( "-", ff.fileName )==0 ? "stdin" : ff.fileName;
129     frame->getTags()->setString( "FILE_NAME", fileNameTag );
130 
131     pfsio.writeFrame( frame, stdout );
132     pfsio.freeFrame( frame );
133 
134     it.closeFrameFile( ff );
135   }
136 }
137 
138 
main(int argc,char * argv[])139 int main( int argc, char* argv[] )
140 {
141   try {
142     readFrames( argc, argv );
143   }
144   catch( pfs::Exception ex ) {
145     fprintf( stderr, PROG_NAME " error: %s\n", ex.getMessage() );
146     return EXIT_FAILURE;
147   }
148   catch( QuietException ) {
149     return EXIT_FAILURE;
150   }
151   return EXIT_SUCCESS;
152 }
153