예제 #1
0
void EXRReader::read(Frame & frame, const Params &/*params*/)
{
    if ( !isOpen() ) open();

    // helpers...
    InputFile& file = m_data->file_;
    Box2i& dtw = m_data->dtw_;

    pfs::Frame tempFrame( width(), height() );
    pfs::Channel *X, *Y, *Z;
    tempFrame.createXYZChannels( X, Y, Z );

    FrameBuffer frameBuffer;
    frameBuffer.insert( "R",                                    // name
                        Slice( FLOAT,                           // type
                               (char*)(X->data() - dtw.min.x - dtw.min.y * width()),
                               sizeof(float),                   // xStride
                               sizeof(float) * width(),         // yStride
                               1, 1,                            // x/y sampling
                               0.0));                           // fillValue

    frameBuffer.insert( "G",                                    // name
                        Slice( FLOAT,                           // type
                               (char*)(Y->data() - dtw.min.x - dtw.min.y * width()),
                               sizeof(float),                   // xStride
                               sizeof(float) * width(),         // yStride
                               1, 1,                            // x/y sampling
                               0.0));                           // fillValue

    frameBuffer.insert( "B",                                    // name
                        Slice( FLOAT,                           // type
                               (char*)(Z->data() - dtw.min.x - dtw.min.y * width()),
                               sizeof(float),                   // xStride
                               sizeof(float) * width(),         // yStride
                               1, 1,                            // x/y sampling
                               0.0));                           // fillValue



    // I know I have the channels I need because I have checked that I have the
    // RGB channels. Hence, I don't load any further that that...
    /*
    const ChannelList &channels = file.header().channels();
    for ( ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i )
    {
        if ( !strcmp( i.name(), "R" ) ||
             !strcmp( i.name(), "G" ) || !strcmp( i.name(), "B" ) ) continue;

        std::string channelName( i.name() );
        if ( channelName == "Z" ) {
            channelName = "DEPTH";
        }
        pfs::Channel *pfsCh = tempFrame.createChannel( channelName );
        frameBuffer.insert( i.name(),                               // name
                            Slice( FLOAT,                           // type
                                   (char *)(pfsCh->data() - dtw.min.x - dtw.min.y * width()),
                                   sizeof(float),                   // xStride
                                   sizeof(float) * width(),         // yStride
                                   1, 1,                            // x/y sampling
                                   0.0));                           // fillValue
    }
    */

    // Copy attributes to tags
    for ( Header::ConstIterator it = file.header().begin(), itEnd = file.header().end();
            it != itEnd; ++it )
    {
        const char *attribName = it.name();
        const StringAttribute *attrib =
            file.header().findTypedAttribute<StringAttribute>(attribName);

        if ( attrib == NULL ) continue; // Skip if type is not String

        // fprintf( stderr, "Tag: %s = %s\n", attribName, attrib->value().c_str() );

        const char *colon = strstr( attribName, ":" );
        if ( colon == NULL )    // frame tag
        {
            tempFrame.getTags().setTag( attribName,
                                        escapeString(attrib->value()) );
        }
        else  // channel tag
        {
            std::string channelName = string( attribName, colon-attribName );
            pfs::Channel *ch = tempFrame.getChannel( channelName );
            if ( ch == NULL ) {
                std::cerr << " Warning! Can not set tag for "
                          << channelName
                          << " channel because it does not exist\n";
            } else {
                ch->getTags().setTag(colon + 1,
                                     escapeString( attrib->value() ));
            }
        }
    }

    file.setFrameBuffer( frameBuffer );
    file.readPixels( dtw.min.y, dtw.max.y );

    // Rescale values if WhiteLuminance is present
    if ( hasWhiteLuminance( file.header() ) )
    {
        float scaleFactor = whiteLuminance( file.header() );
        int pixelCount = tempFrame.getHeight()*tempFrame.getWidth();

        for ( int i = 0; i < pixelCount; i++ )
        {
            (*X)(i) *= scaleFactor;
            (*Y)(i) *= scaleFactor;
            (*Z)(i) *= scaleFactor;
        }

        // const StringAttribute *relativeLum =
        // file.header().findTypedAttribute<StringAttribute>("RELATIVE_LUMINANCE");

        std::string luminanceTag = tempFrame.getTags().getTag("LUMINANCE");
        if ( luminanceTag.empty() )
        {
            tempFrame.getTags().setTag("LUMINANCE", "ABSOLUTE");
        }
    }

    tempFrame.getTags().setTag( "FILE_NAME", filename() );

    frame.swap( tempFrame );
}
예제 #2
0
파일: main.cpp 프로젝트: GYingchao/openexr
void
printInfo (const char fileName[])
{
    MultiPartInputFile in (fileName);
    int parts = in.parts();

    //
    // check to see if any parts are incomplete
    //

    bool is_complete=true;

    for (size_t i = 0; i < parts && is_complete; ++i)
    {
        is_complete &= in.partComplete(i);
    }

    cout << "\n" << fileName <<
            (is_complete ? "": " (incomplete file)") <<
            ":\n\n";

    cout << "file format version: " <<
            getVersion (in.version()) << ", "
            "flags 0x" <<
            setbase (16) << getFlags (in.version()) << setbase (10) << "\n";

    for (size_t p = 0; p < parts ; ++p)
    {
        const Header & h = in.header(p);
        if( parts != 1 )
        {
            cout  << "\n\n part " << p <<
            ( in.partComplete(p) ? "": " (incomplete)") <<
            ":\n";

        }
	 
        for (Header::ConstIterator i = h.begin(); i != h.end(); ++i)
        {
            const Attribute *a = &i.attribute();
            cout << i.name() << " (type " << a->typeName() << ")";

            if (const Box2iAttribute *ta =
                            dynamic_cast <const Box2iAttribute *> (a))
            {
                cout << ": " << ta->value().min << " - " << ta->value().max;
            }

            else if (const Box2fAttribute *ta =
                            dynamic_cast <const Box2fAttribute *> (a))
            {
                cout << ": " << ta->value().min << " - " << ta->value().max;
            }
            else if (const ChannelListAttribute *ta =
                            dynamic_cast <const ChannelListAttribute *> (a))
            {
                cout << ":";
                printChannelList (ta->value());
            }
            else if (const ChromaticitiesAttribute *ta =
                            dynamic_cast <const ChromaticitiesAttribute *> (a))
            {
                cout << ":\n"
                "    red   " << ta->value().red << "\n"
                "    green " << ta->value().green << "\n"
                "    blue  " << ta->value().blue << "\n"
                "    white " << ta->value().white;
            }
            else if (const CompressionAttribute *ta =
                            dynamic_cast <const CompressionAttribute *> (a))
            {
                cout << ": ";
                printCompression (ta->value());
            }
            else if (const DoubleAttribute *ta =
                            dynamic_cast <const DoubleAttribute *> (a))
            {
                cout << ": " << ta->value();
            }
            else if (const EnvmapAttribute *ta =
                            dynamic_cast <const EnvmapAttribute *> (a))
            {
                cout << ": ";
                printEnvmap (ta->value());
            }
            else if (const FloatAttribute *ta =
                            dynamic_cast <const FloatAttribute *> (a))
            {
                cout << ": " << ta->value();
            }
            else if (const IntAttribute *ta =
                            dynamic_cast <const IntAttribute *> (a))
            {
                cout << ": " << ta->value();
            }
            else if (const KeyCodeAttribute *ta =
                            dynamic_cast <const KeyCodeAttribute *> (a))
            {
                cout << ":\n"
                "    film manufacturer code " <<
                ta->value().filmMfcCode() << "\n"
                "    film type code " <<
                ta->value().filmType() << "\n"
                "    prefix " <<
                ta->value().prefix() << "\n"
                "    count " <<
                ta->value().count() << "\n"
                "    perf offset " <<
                ta->value().perfOffset() << "\n"
                "    perfs per frame " <<
                ta->value().perfsPerFrame() << "\n"
                "    perfs per count " <<
                ta->value().perfsPerCount();
            }
            else if (const LineOrderAttribute *ta =
                            dynamic_cast <const LineOrderAttribute *> (a))
            {
                cout << ": ";
                printLineOrder (ta->value());
            }
            else if (const M33fAttribute *ta =
                            dynamic_cast <const M33fAttribute *> (a))
            {
                cout << ":\n"
                "   (" <<
                ta->value()[0][0] << " " <<
                ta->value()[0][1] << " " <<
                ta->value()[0][2] << "\n    " <<
                ta->value()[1][0] << " " <<
                ta->value()[1][1] << " " <<
                ta->value()[1][2] << "\n    " <<
                ta->value()[2][0] << " " <<
                ta->value()[2][1] << " " <<
                ta->value()[2][2] << ")";
            }
            else if (const M44fAttribute *ta =
                            dynamic_cast <const M44fAttribute *> (a))
            {
                cout << ":\n"
                "   (" <<
                ta->value()[0][0] << " " <<
                ta->value()[0][1] << " " <<
                ta->value()[0][2] << " " <<
                ta->value()[0][3] << "\n    " <<
                ta->value()[1][0] << " " <<
                ta->value()[1][1] << " " <<
                ta->value()[1][2] << " " <<
                ta->value()[1][3] << "\n    " <<
                ta->value()[2][0] << " " <<
                ta->value()[2][1] << " " <<
                ta->value()[2][2] << " " <<
                ta->value()[2][3] << "\n    " <<
                ta->value()[3][0] << " " <<
                ta->value()[3][1] << " " <<
                ta->value()[3][2] << " " <<
                ta->value()[3][3] << ")";
            }
            else if (const PreviewImageAttribute *ta =
                            dynamic_cast <const PreviewImageAttribute *> (a))
            {
                cout << ": " <<
                ta->value().width()  << " by " <<
                ta->value().height() << " pixels";
            }
            else if (const StringAttribute *ta =
                            dynamic_cast <const StringAttribute *> (a))
            {
                cout << ": \"" << ta->value() << "\"";
            }
            else if (const StringVectorAttribute * ta =
                            dynamic_cast<const StringVectorAttribute *>(a))
            {
                cout << ":";

                for (StringVector::const_iterator i = ta->value().begin();
                                i != ta->value().end();
                                ++i)
                {
                    cout << "\n    \"" << *i << "\"";
                }
            }
            else if (const RationalAttribute *ta =
                            dynamic_cast <const RationalAttribute *> (a))
            {
                cout << ": " << ta->value().n << "/" << ta->value().d <<
                " (" << double (ta->value()) << ")";
            }
            else if (const TileDescriptionAttribute *ta =
                            dynamic_cast <const TileDescriptionAttribute *> (a))
            {
                cout << ":\n    ";

                printLevelMode (ta->value().mode);

                cout << "\n    tile size " <<
                ta->value().xSize << " by " <<
                ta->value().ySize << " pixels";

                if (ta->value().mode != ONE_LEVEL)
                {
                    cout << "\n    level sizes rounded ";
                    printLevelRoundingMode (ta->value().roundingMode);
                }
            }
            else if (const TimeCodeAttribute *ta =
                            dynamic_cast <const TimeCodeAttribute *> (a))
            {
                cout << ":\n";
                printTimeCode (ta->value());
            }
            else if (const V2iAttribute *ta =
                            dynamic_cast <const V2iAttribute *> (a))
            {
                cout << ": " << ta->value();
            }
            else if (const V2fAttribute *ta =
                            dynamic_cast <const V2fAttribute *> (a))
            {
                cout << ": " << ta->value();
            }
            else if (const V3iAttribute *ta =
                            dynamic_cast <const V3iAttribute *> (a))
            {
                cout << ": " << ta->value();
            }
            else if (const V3fAttribute *ta =
                            dynamic_cast <const V3fAttribute *> (a))
            {
                cout << ": " << ta->value();
            }

            cout << '\n';
        }
    }

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

  bool verbose = false;
  bool keepRGB = false;

  // Parse command line parameters
  static struct option cmdLineOptions[] = {
    { "help", no_argument, NULL, 'h' },
    { "verbose", no_argument, NULL, 'v' },
    { "keep-rgb", no_argument, NULL, 'k' },
    { "linear", no_argument, NULL, 'l' },
    { NULL, 0, NULL, 0 }
  };
  static const char optstring[] = "lkhv";
    
  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 'k':
      keepRGB = true;
      break;
    case 'l':
      std::cerr << PROG_NAME << " warning: linearize option ignored for an HDR input!"
                << std::endl;
      break;
    case '?':
      throw QuietException();
    case ':':
      throw QuietException();
    }
  }

  VERBOSE_STR << "keep RGB channels untouch: " << (keepRGB ? "yes" : "no") << std::endl;
  
  while( true )
  {
    pfs::FrameFile ff = it.getNextFrameFile();
    if( ff.fh == NULL ) break; // No more frames
    it.closeFrameFile( ff );

    InputFile file( ff.fileName );

    FrameBuffer frameBuffer;
    
    Box2i dw = file.header().displayWindow();
    Box2i dtw = file.header().dataWindow();
    int width  = dw.max.x - dw.min.x + 1;
    int height = dw.max.y - dw.min.y + 1;

    if( dtw.min.x < dw.min.x && dtw.max.x > dw.max.x ||
      dtw.min.y < dw.min.y && dtw.max.y > dw.max.y )
      throw pfs::Exception( "No support for OpenEXR files DataWidnow greater than DisplayWindow" );
    

    pfs::Frame *frame = pfsio.createFrame( width, height );

    const ChannelList &channels = file.header().channels();

    bool processColorChannels = false;
    pfs::Channel *X, *Y, *Z;
    if( !keepRGB ) {            // Keep RGB channels as they are
      const Channel *rChannel = channels.findChannel( "R" );
      const Channel *gChannel = channels.findChannel( "G" );
      const Channel *bChannel = channels.findChannel( "B" );
      if( rChannel!=NULL && gChannel!=NULL && bChannel!=NULL ) {
        frame->createXYZChannels( X, Y, Z );

        frameBuffer.insert( "R",				  // name
          Slice( FLOAT,			  // type
//            (char*)(X->getRawData()),
            (char*)(X->getRawData()),
            sizeof(float),	  // xStride
            sizeof(float) * width,// yStride
            1, 1,			  // x/y sampling
            0.0));			  // fillValue

        frameBuffer.insert( "G",				  // name
          Slice( FLOAT,			  // type
            (char*)(Y->getRawData()),
            sizeof(float),	  // xStride
            sizeof(float) * width,// yStride
            1, 1,			  // x/y sampling
            0.0));			  // fillValue

        frameBuffer.insert( "B",				  // name
          Slice( FLOAT,			  // type
            (char*)(Z->getRawData()),
            sizeof(float),	  // xStride
            sizeof(float) * width,// yStride
            1, 1,			  // x/y sampling
            0.0)); // fillValue

        processColorChannels = true;
      }
    }
    
    for( ChannelList::ConstIterator i = channels.begin();
         i != channels.end(); ++i )
    {
      const Channel &channel = i.channel();
        
      if( processColorChannels ) { // Skip color channels
        if( !strcmp( i.name(), "R" ) || !strcmp( i.name(), "G" ) ||
          !strcmp( i.name(), "B" ) ) continue;
      }

      const char *channelName = i.name();
      if( !strcmp( channelName, "Z" ) ) {
        channelName = "DEPTH";
      }
        
      pfs::Channel *pfsCh = frame->createChannel( channelName );
      frameBuffer.insert( i.name(),				  // name
        Slice( FLOAT,			  // type
          (char *)pfsCh->getRawData(),
          sizeof(float),	  // xStride
          sizeof(float) * width,// yStride
          1, 1,			  // x/y sampling
          0.0));			  // fillValue
    }     

    // Copy attributes to tags
    {
      for( Header::ConstIterator it = file.header().begin();
           it != file.header().end(); it++ ) {
        const char *attribName = it.name();
        const char *colon = strstr( attribName, ":" );
        const StringAttribute *attrib =
          file.header().findTypedAttribute<StringAttribute>(attribName);

        if( attrib == NULL ) continue; // Skip if type is not String
        
//         fprintf( stderr, "Tag: %s = %s\n", attribName, attrib->value().c_str() );
        
        if( colon == NULL ) {   // frame tag
          frame->getTags()->setString( attribName, escapeString(attrib->value()).c_str() );
        } else {                // channel tag
          string channelName = string( attribName, colon-attribName );
          pfs::Channel *ch = frame->getChannel( channelName.c_str() );
          if( ch == NULL ) {
            fprintf( stderr, PROG_NAME ": Warning! Can not set tag for '%s' channel because it does not exist\n", channelName.c_str() );
            continue;
          }
          ch->getTags()->setString(  colon+1, escapeString( attrib->value() ).c_str() );
        }
        
      }
    }
    
    file.setFrameBuffer( frameBuffer );
    file.readPixels( dw.min.y, dw.max.y );

    VERBOSE_STR << "reading file (linear) '" << ff.fileName << "'" << std::endl;
    
    if( processColorChannels ) {      
      // Rescale values if WhiteLuminance is present
      if( hasWhiteLuminance( file.header() ) ) {
        float scaleFactor = whiteLuminance( file.header() );
        int pixelCount = frame->getHeight()*frame->getWidth();
        for( int i = 0; i < pixelCount; i++ ) {
          (*X)(i) *= scaleFactor;
          (*Y)(i) *= scaleFactor;
          (*Z)(i) *= scaleFactor;
        }
        const StringAttribute *relativeLum =
          file.header().findTypedAttribute<StringAttribute>("RELATIVE_LUMINANCE");

        const char *luminanceTag = frame->getTags()->getString( "LUMINANCE" );
        if( luminanceTag == NULL )
          frame->getTags()->setString( "LUMINANCE", "ABSOLUTE" );
      }  
      pfs::transformColorSpace( pfs::CS_RGB, X, Y, Z, pfs::CS_XYZ, X, Y, Z );
    }    
    frame->getTags()->setString( "FILE_NAME", ff.fileName );
    
    pfsio.writeFrame( frame, stdout );
    pfsio.freeFrame( frame );
  }
}