/* * This file is part of rasdaman community. * * Rasdaman community 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 3 of the License, or * (at your option) any later version. * * Rasdaman community 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 rasdaman community. If not, see . * * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann / rasdaman GmbH. * * For more information please see * or contact Peter Baumann via . */ #ifdef EARLY_TEMPLATE #define __EXECUTABLE__ #ifdef __GNUG__ #include "raslib/template_inst.hh" #endif #endif #include #include #include /* #include #include "raslib/rminit.hh" #include "rasodmg/marray.hh" #include "rasodmg/ref.hh" #include "rasodmg/set.hh" #include "rasodmg/database.hh" #include "rasodmg/partinsert.hh" #include "raslib/type.hh" #include "raslib/odmgtypes.hh" */ #include #include #include #include #include #include #include #include "image.h" #include "utilities.h" using namespace std; using namespace RasNITF; typedef struct { unsigned char red, green, blue; } RGBPixel; image_band::image_band(){ lutdnnm = NULL; } image_band::~image_band(){ if (lutdnnm != NULL){ delete lutdnnm; lutdnnm = NULL; } } image::image(){ m_icom = NULL; m_bands = NULL; m_udid = NULL; m_ixshd = NULL; m_data = NULL; } image::~image(){ if (m_icom != NULL) { delete m_icom; m_icom = NULL; } if (m_bands != NULL){ delete[] m_bands; m_bands = NULL; } if (m_udid != NULL) { delete m_udid; m_udid = NULL; } if (m_ixshd != NULL) { delete m_ixshd; m_ixshd = NULL; } if (m_data != NULL) { delete m_data; m_data = NULL; } } /********************************************************************** * NAME: image::read_file * PURPOSE: read an image from an ntf file into an image object *********************************************************************/ int image::read_file(istream &hNITF, long lish, long li, bool read_image_data){ int charsread=0; // read IM through ICORDS charsread += read_verify2(hNITF, m_im, (2+10+14+17+80+1+2+11+2+20+2+8+4+1+8+43+1+40+1+8+15+1+42+8+8+3+8+8+2+1+1)); // read IGEOLO field if used if (m_icords[0] != ' ') { charsread += read_verify2(hNITF, m_igeolo, 60); } else { memset(m_igeolo, ' ', 60); charsread += 60; } // read NICOM and ICOM charsread += read_verify2(hNITF, m_nicom, 1); n_nicom = charptrtoint(m_nicom, 1); if (n_nicom > 0) { m_icom = new char[n_nicom * 80]; charsread += read_verify2(hNITF, m_icom, n_nicom * 80); } else { m_icom = NULL; } // read IC charsread += read_verify2(hNITF, m_ic, 2); // read COMRAT if necessary if ((strncmp(m_ic, "NC", 2) != 0) && (strncmp(m_ic, "NM", 2) != 0)) { charsread += read_verify2(hNITF, m_comrat, 4); } charsread += read_verify2(hNITF, m_nbands, 1); n_nbands = charptrtoint(m_nbands, 1); if (n_nbands == 0) { charsread += read_verify2(hNITF, m_xbands, 5); n_xbands = charptrtoint(m_xbands, 5); numbands = n_xbands; } else { n_xbands = 0; numbands = n_nbands; } if (numbands > 0){ m_bands = new image_band[numbands]; for (int i = 0; i < numbands; i++) { // read IREPBANDnn..NLUTSnn charsread += read_verify2(hNITF, m_bands[i].irepbandnn, (2+6+1+3+1)); // get # of LUTS for this band m_bands[i].numluts = charptrtoint( m_bands[i].nlutsnn, 1); // read LUTs entries per LUT if (m_bands[i].numluts > 0) { // read # of LUT entries charsread += read_verify2(hNITF, m_bands[i].nelutnn, 5); m_bands[i].numlutentries = charptrtoint(m_bands[i].nelutnn, 5); // allocate space for the LUT entries m_bands[i].lutdnnm = new char[m_bands[i].numluts * m_bands[i].numlutentries]; // read LUT entries charsread += read_verify2(hNITF, m_bands[i].lutdnnm, m_bands[i].numluts * m_bands[i].numlutentries); } else { m_bands[i].lutdnnm = NULL; } } /* end for (band_num = 0... */ } else { m_bands = NULL; } // read m_isync..udidl charsread += read_verify2(hNITF, m_isync, (1+1+4+4+4+4+2+3+3+10+4+5)); n_udidl = charptrtolong(m_udidl, 5); if (n_udidl > 0) { charsread += read_verify2(hNITF, m_udofl, 3); m_udid = new char[n_udidl]; charsread += read_verify2(hNITF, m_udid, n_udidl - 3); } // read ixshdl charsread += read_verify2(hNITF, m_ixshdl, 5); n_ixshdl = charptrtolong(m_ixshdl, 5); if (n_ixshdl > 0) { charsread += read_verify2(hNITF, m_ixsofl, 3); m_ixshd = new char[n_ixshdl]; charsread += read_verify2(hNITF, m_ixshd, n_ixshdl - 3); } /*********************************************************************** * MASKED IMAGES ARE IGNORED FOR NOW !!! *********************************************************************** strncpy(temp_buffer, m_ic, 2); temp_buffer[2] = '\0'; if (strcmp(temp_buffer, "NM") == 0 || strcmp(temp_buffer, "M1") == 0 || strcmp(temp_buffer, "M3") == 0 || strcmp(temp_buffer, "M4") == 0 || strcmp(temp_buffer, "M5") == 0) { // ERROR not implemented yet masked images } ************************************************************************/ // copy image data header_length = lish; data_length = li; if(read_image_data == true) { m_data = new char[li]; charsread += read_verify2(hNITF, m_data, li); } else { charsread += li; } n_nbpr = 0; // number of blocks per row n_nbpc = 0; // number of blocks per column n_nppbh = 0; // number of of pixels per block horizontally n_nppbv = 0; // number of of pixels per block vertically n_nppbh = charptrtoint( m_nppbh, 4); n_nppbv = charptrtoint( m_nppbv, 4); n_nbpr = charptrtoint( m_nbpr, 4); n_nbpc = charptrtoint( m_nbpc, 4); cout<<"IC: "; cout.write(m_ic, 2); cout<<"\nNBANDS: "; cout.write(m_nbands, 1); cout<<"\nnumber of blocks per row: "; cout.write(m_nbpr, 4); cout<<"\nNumber of blocks per column: "; cout.write(m_nbpc, 4); cout<<"\nImode: "; cout.write(m_imode, 1); return charsread; } /********************************************************************** * NAME: image::write_file * PURPOSE: write an image object into an ntf file on hard drive *********************************************************************/ int image::write_file(ofstream &fs){ int charswritten=0; fs.write(m_im, 2); fs.write(m_iid1, 10); fs.write(m_idatim, 14); fs.write(m_tgtid, 17); fs.write(m_iid2, 80); fs.write(m_isclas, 1); fs.write(m_isclsy, 2); fs.write(m_iscode, 11); fs.write(m_isctlh, 2); fs.write(m_isrel, 20); fs.write(m_isdctp, 2); fs.write(m_isdcdt, 8); fs.write(m_isdcxm, 4); fs.write(m_isdg, 1); fs.write(m_isdgdt, 8); fs.write(m_iscltx, 43); fs.write(m_iscatp, 1); fs.write(m_iscaut, 40); fs.write(m_iscrsn, 1); fs.write(m_issrdt, 8); fs.write(m_isctln, 15); fs.write(m_encryp, 1); fs.write(m_isorce, 42); fs.write(m_nrows, 8); fs.write(m_ncols, 8); fs.write(m_pvtype, 3); fs.write(m_irep, 8); fs.write(m_icat, 8); fs.write(m_abpp, 2); fs.write(m_pjust, 1); fs.write(m_icords, 1); if (m_icords[0] != ' '){ fs.write(m_igeolo, 60); charswritten += 60; } fs.write(m_nicom, 1); charswritten += 2 + 10 + 14 + 17 + 80 + 1 + 2 + 11 + 2 + 20 + 2 + 8 + 4 +1 + 8 + 43 + 1 + 40 + 1 + 8 + 15 + 1 + 42 + 8 + 8 + 3 + 8 + 8 + 2 + 1 + 1 + 1; if (m_icom!=NULL) { fs.write(m_icom, n_nicom * 80); charswritten += n_nicom * 80; } fs.write(m_ic, 2); charswritten += 2; if ((strncmp(m_ic, "NC", 2) != 0) && (strncmp(m_ic, "NM", 2) != 0)) { fs.write(m_comrat, 4); charswritten += 4; } fs.write(m_nbands, 1); charswritten += 1; if (n_nbands == 0) { fs.write(m_xbands, 5); charswritten += 5; } if (m_bands != NULL){ for (int i=0; i 0){ fs.write(m_bands[i].nelutnn, 5); fs.write(m_bands[i].lutdnnm, m_bands[i].numluts * m_bands[i].numlutentries); charswritten += 5 + m_bands[i].numluts * m_bands[i].numlutentries; } } } fs.write(m_isync, 1); fs.write(m_imode, 1); fs.write(m_nbpr, 4); fs.write(m_nbpc, 4); fs.write(m_nppbh, 4); fs.write(m_nppbv, 4); fs.write(m_nbpp, 2); fs.write(m_idlvl, 3); fs.write(m_ialvl, 3); fs.write(m_iloc, 10); fs.write(m_imag, 4); fs.write(m_udidl, 5); charswritten += 1 + 1 + 4 + 4 + 4 +4 + 2 + 3 + 3 + 10 + 4 + 5; if (n_udidl > 0) { fs.write(m_udofl, 3); fs.write(m_udid, n_udidl - 3); charswritten += n_udidl; } fs.write(m_ixshdl, 5); charswritten += 5; if (n_ixshdl > 0) { fs.write(m_ixsofl, 3); fs.write(m_ixshd, n_ixshdl - 3); charswritten += n_ixshdl; } cout<<" wrote image header length: " << charswritten <