core/vil/file_formats/vil_nitf2_des.cxx
Go to the documentation of this file.
00001 // vil_nitf2: Written by Rob Radtke (rob@) and Harry Voorhees (hlv@) of
00002 // Stellar Science Ltd. Co. (stellarscience.com) for
00003 // Air Force Research Laboratory, 2005.
00004 
00005 #include "vil_nitf2_des.h"
00006 #include "vil_nitf2_field_definition.h"
00007 #include "vil_nitf2_typed_field_formatter.h"
00008 #include <vcl_sstream.h>
00009 
00010 vil_nitf2_des::field_definition_map&
00011   vil_nitf2_des::all_definitions()
00012 {
00013   class field_definition_map_t: public vil_nitf2_des::field_definition_map
00014   {
00015    public:
00016     ~field_definition_map_t()
00017     {
00018       for (iterator it = begin(), last = end();
00019            it != last; it++ )
00020       {
00021         delete it->second;
00022       }
00023     }
00024   };
00025 
00026   static field_definition_map_t field_definitions;
00027   return field_definitions;
00028 }
00029 
00030 
00031 vil_nitf2_field_definitions&
00032 vil_nitf2_des::define(vcl_string desId )
00033 {
00034   if (all_definitions().find(desId) != all_definitions().end()) {
00035     throw("des with that name already defined.");
00036   }
00037   vil_nitf2_field_definitions* definition = new vil_nitf2_field_definitions();
00038   all_definitions().insert( vcl_make_pair(desId, definition) );
00039   return *definition;
00040 }
00041 
00042 vil_nitf2_des::vil_nitf2_des( vil_nitf2_classification::file_version version, int data_width )
00043   : m_field_sequence1( 0 ),
00044     m_field_sequence2( 0 )
00045 {
00046   m_field_sequence1 = new vil_nitf2_field_sequence( *create_field_definitions( version, data_width ) );
00047 }
00048 
00049 bool vil_nitf2_des::read(vil_stream* stream)
00050 {
00051   if ( m_field_sequence1->read(*stream) ) {
00052     vcl_string desId;
00053     m_field_sequence1->get_value( "DESID", desId );
00054     if ( desId == "TRE_OVERFLOW" ){
00055       return true;
00056     }
00057     else {
00058       field_definition_map::iterator it = all_definitions().find( desId );
00059       if ( it != all_definitions().end() ) {
00060         if ( m_field_sequence2 ) delete m_field_sequence2;
00061         m_field_sequence2 = new vil_nitf2_field_sequence( *((*it).second) );
00062         return m_field_sequence2->read(*stream);
00063       }
00064     }
00065   }
00066   return true;
00067 }
00068 
00069 vil_nitf2_field_definitions* vil_nitf2_des::create_field_definitions( vil_nitf2_classification::file_version ver, int data_width )
00070 {
00071   vil_nitf2_field_definitions* field_definitions = new vil_nitf2_field_definitions();
00072   add_shared_field_defs_1(field_definitions);
00073   vil_nitf2_classification::add_field_defs(field_definitions, ver, "I", "Image");
00074   add_shared_field_defs_2(field_definitions, data_width);
00075   return field_definitions;
00076 }
00077 
00078 void vil_nitf2_des::add_shared_field_defs_1( vil_nitf2_field_definitions* defs )
00079 {
00080   (*defs)
00081     .field( "DE", "Data Extension Subheader", NITF_ENUM( 2, vil_nitf2_enum_values().value( "DE" ) ), false, 0, 0 )
00082     .field( "DESID", "Unique DES Type Identifier", NITF_STR( 25 ),
00083             false, 0, 0 )
00084     .field( "DESVER", "Version of the Data Definition", NITF_INT( 2, false ), false, 0, 0 );
00085 }
00086 
00087 void vil_nitf2_des::add_shared_field_defs_2( vil_nitf2_field_definitions* defs, int data_width )
00088 {
00089   vil_nitf2_enum_values overflow_enum;
00090   overflow_enum.value( "UDHD", "User Defined Header Data" )
00091                .value( "UDID" "User Defined Image Data" )
00092                .value( "XHD", "Extended Header Data" )
00093                .value( "IXSHD", "Image Extended Subheader Data" )
00094                .value( "SXSHD", "Graphic Extended Subheader Data" )
00095                .value( "TXSHD", "Text Extended Subheader Data" );
00096   (*defs)
00097     .field( "DESOFLW", "Overflowed Header Type", NITF_ENUM( 6, overflow_enum ),
00098             false, 0, new vil_nitf2_field_value_one_of<vcl_string>( "DESID", "TRE_OVERFLOW" ) )
00099     .field( "DESITEM", "Data Item Overflowed", NITF_INT( 3, false ),
00100             false, 0, new vil_nitf2_field_value_one_of<vcl_string>( "DESID", "TRE_OVERFLOW" ))
00101     .field( "DESSHL", "Length of DES-Defined Subheader Fields", NITF_INT( 4, false ), false, 0, 0 )
00102     .field( "DESDATA", "DES-Defined Data Field", NITF_TRES(), false,
00103             new vil_nitf2_max_field_value_plus_offset_and_threshold( "DESSHL", data_width, 0, -1 ),
00104             new vil_nitf2_field_value_one_of<vcl_string>( "DESID", "TRE_OVERFLOW" ) )
00105     .end();
00106 }
00107 
00108 vil_nitf2_field::field_tree* vil_nitf2_des::get_tree( int i ) const
00109 {
00110   vil_nitf2_field::field_tree* t = new vil_nitf2_field::field_tree;
00111   vcl_stringstream name_stream;
00112   name_stream << "Data Extension Segment";
00113   if ( i > 0 ) name_stream << " #" << i;
00114   t->columns.push_back( name_stream.str() );
00115   vcl_string desId;
00116   if ( m_field_sequence1->get_value( "DESID", desId ) ) {
00117     t->columns.push_back( desId );
00118   }
00119   m_field_sequence1->get_tree( t );
00120   if ( m_field_sequence2 ) m_field_sequence2->get_tree( t );
00121   return t;
00122 }
00123 
00124 vil_nitf2_des::~vil_nitf2_des()
00125 {
00126   if ( m_field_sequence1 ) delete m_field_sequence1;
00127   if ( m_field_sequence2 ) delete m_field_sequence2;
00128 }