File: pfsinimgmagick.cpp

package info (click to toggle)
pfstools 2.2.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,548 kB
  • sloc: cpp: 26,364; javascript: 3,814; ansic: 999; sh: 180; python: 65; makefile: 47
file content (222 lines) | stat: -rw-r--r-- 6,892 bytes parent folder | download
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
/**
 * @brief Read files using ImageMagics++ library
 * 
 * This file is a part of PFSTOOLS package.
 * ---------------------------------------------------------------------- 
 * Copyright (C) 2003,2004 Rafal Mantiuk and Grzegorz Krawczyk
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * ---------------------------------------------------------------------- 
 * 
 * @author Rafal Mantiuk, <mantiuk@mpi-sb.mpg.de>
 * 
 * $Id: pfsinimgmagick.cpp,v 1.6 2014/09/03 08:57:48 rafm Exp $
 */

#include <config.h>
#include <Magick++.h>

#include <iostream>

#include <getopt.h>
#include <pfs.h>

#define PROG_NAME "pfsinimgmagick"

using namespace Magick;

class QuietException 
{
};

void printHelp()
{
  std::cerr << PROG_NAME " [--linear] [--absolute <max_lum>] [--verbose] [--help]" << std::endl
            << "See man page for more information." << std::endl;
}



void readFrames( int argc, char* argv[] )
{
  pfs::DOMIO pfsio;

  bool verbose = false;
  bool opt_linear=false;
  float absoluteMaxLum = 0;
  
  // Parse command line parameters
  static struct option cmdLineOptions[] = {
    { "help", no_argument, NULL, 'h' },
    { "verbose", no_argument, NULL, 'v' },
    { "linear", no_argument, NULL, 'l' },
    { "absolute", required_argument, NULL, 'a' },
    { NULL, 0, NULL, 0 }
  };
  static const char optstring[] = "lhva:";
    
  pfs::FrameFileIterator it( argc, argv, "rb", NULL, NULL,
    optstring, cmdLineOptions );
    
  int optionIndex = 0;
  while( 1 ) {
    int c = getopt_long (argc, argv, optstring, cmdLineOptions, &optionIndex);
    if( c == -1 ) break;
    switch( c ) {
    case 'h':
      printHelp();
      throw QuietException();
    case 'v':
      verbose = true;
      break;
    case 'l':
      opt_linear = true;
      break;
    case 'a':
      absoluteMaxLum = (float)strtod( optarg, NULL );
      break;
    case '?':
      throw QuietException();
    case ':':
      throw QuietException();
    }
  }

  if( absoluteMaxLum != 0 && opt_linear )
    throw pfs::Exception( "'absolute' and 'linear' are conflicting options" );
  
  if( absoluteMaxLum < 0 )
    throw pfs::Exception( "maximum absolute luminance must be > 0" );

  VERBOSE_STR << "linearize input image: " << ((opt_linear || absoluteMaxLum!=0) ? "yes" : "no") << std::endl;
  if( absoluteMaxLum != 0 )
    VERBOSE_STR << "maximum absolute luminance: " << absoluteMaxLum << std::endl;

  Magick::InitializeMagick("");
  
  while( true ) {
    pfs::FrameFile ff = it.getNextFrameFile();
    if( ff.fh == NULL ) break; // No more frames
    it.closeFrameFile( ff );

    VERBOSE_STR << "reading file '" << ff.fileName << "'" << std::endl;
    Magick::Image imImage( ff.fileName );

    VERBOSE_STR << "input image gamma:  " << imImage.gamma() << std::endl;
#if MagickLibVersion >= 0x700
    bool hasAlpha = imImage.alpha();
#else
    bool hasAlpha = imImage.matte();
#endif
    if( hasAlpha )
      VERBOSE_STR << "alpha channel found" << std::endl;    
    
    pfs::Frame *frame = pfsio.createFrame( imImage.columns(),
      imImage.rows() );
    pfs::Channel *X, *Y, *Z;
    frame->createXYZChannels( X, Y, Z );

    pfs::Channel *alpha = NULL;
    if( hasAlpha )
      alpha = frame->createChannel( "ALPHA" );
    
    // Copy line by line to pfs::Frame
    int pixInd = 0;
    const float maxValue = (float)QuantumRange;
    for( int r = 0; r < imImage.rows(); r++ ) {
#if MagickLibVersion >= 0x700
      const Magick::Quantum *pixels =
#else
      const Magick::PixelPacket *pixels =
#endif
        imImage.getConstPixels( 0, r, imImage.columns(), 1 );

      for( int c = 0; c < imImage.columns(); c++ ) {
#if MagickLibVersion >= 0x700
        (*X)(pixInd) = (float)MagickCore::GetPixelRed(imImage.image(), pixels) / maxValue;
        (*Y)(pixInd) = (float)MagickCore::GetPixelGreen(imImage.image(), pixels) / maxValue;
        (*Z)(pixInd) = (float)MagickCore::GetPixelBlue(imImage.image(), pixels) / maxValue;
        if( alpha != NULL )
          (*alpha)(pixInd) = (float)MagickCore::GetPixelAlpha(imImage.image(), pixels) / maxValue;
	pixels += MagickCore::GetPixelChannels(imImage.image());
#else
        (*X)(pixInd) = (float)pixels[c].red / maxValue;
        (*Y)(pixInd) = (float)pixels[c].green / maxValue;
        (*Z)(pixInd) = (float)pixels[c].blue / maxValue;
        if( alpha != NULL )
          (*alpha)(pixInd) = (float)pixels[c].opacity / maxValue;
#endif
        pixInd++;
      } 
    }    

    // Linearize data is necessary
    if( opt_linear || absoluteMaxLum != 0 )
    {
      pfs::transformColorSpace( pfs::CS_SRGB, X, Y, Z, pfs::CS_XYZ, X, Y, Z );
      if( absoluteMaxLum != 0 ) {
        // Rescale to absolute luminance level
        const int pixCount = X->getWidth()*X->getHeight();
        for( int i = 0; i < pixCount; i++ ) {
          (*X)(i) *= absoluteMaxLum;
          (*Y)(i) *= absoluteMaxLum;
          (*Z)(i) *= absoluteMaxLum;
        }        
        frame->getTags()->setString("LUMINANCE", "ABSOLUTE");
      } else
        frame->getTags()->setString("LUMINANCE", "RELATIVE");
    }
    else
    {
      pfs::transformColorSpace( pfs::CS_RGB, X, Y, Z, pfs::CS_XYZ, X, Y, Z );
      frame->getTags()->setString("LUMINANCE", "DISPLAY");
    }

    // This is the luminance / luma perceived as reference white
    // Some tone-mappers may need this
    frame->getTags()->setString("WHITE_Y", "1");    

    const char *fileNameTag = strcmp( "-", ff.fileName )==0 ? "stdin" : ff.fileName;
    frame->getTags()->setString( "FILE_NAME", fileNameTag );
 
    char strbuf[3];
    snprintf( strbuf, 3, "%d", (int)imImage.depth() );
    frame->getTags()->setString("BITDEPTH", strbuf );    
   
    pfsio.writeFrame( frame, stdout );
    pfsio.freeFrame( frame );
        
  }
}


int main( int argc, char* argv[] )
{
  try {
    readFrames( argc, argv );
  }
  catch( pfs::Exception ex ) {
    std::cerr << PROG_NAME << " error: " << ex.getMessage() << std::endl;
    return EXIT_FAILURE;
  }        
  catch( Magick::Exception &ex ) { //This is comming from ImageMagick
    std::cerr << PROG_NAME << " error: " << ex.what() << std::endl;
    return EXIT_FAILURE;
  }
  catch( QuietException  ex ) {
    return EXIT_FAILURE;
  }        
  return EXIT_SUCCESS;
}