summaryrefslogtreecommitdiffstats
path: root/conversion/ntf.cc
blob: 7db1bc7a189a6e4e66a48f8def0e6c06656bdba1 (plain)
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
/*
* 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 <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE: ntf.cc
 *
 * MODULE:  conversion
 *
 * CLASSES: r_Conv_NTF
 *
 * COMMENTS:
 *
 * Provides functions to convert data to NTF and back.
 *
*/

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <new>

#include "raslib/rminit.hh"
#include "raslib/rmdebug.hh"
#include "raslib/parseparams.hh"
#include "conversion/ntf.hh"
#include "conversion/memfs.hh"

// nitf reader library 

#include "nitf.h"
#include "image.h"

r_Conv_NTF::r_Conv_NTF( const char *src, const r_Minterval &interv, const r_Type *tp ) 
    throw(r_Error):r_Convert_Memory(src, interv, tp)
{
}

r_Conv_NTF::r_Conv_NTF( const char *src, const r_Minterval &interv, int tp ) 
    throw(r_Error):r_Convert_Memory(src, interv, tp)
{
}

r_Conv_NTF::~r_Conv_NTF( void ){
}

/***********************************************************
 * NAME: r_Conv_NTF::convertFrom 
 * ---------------------------------------------------------
 * PURPOSE: convert from NITF image file data to the 
 * corresponding rasdaman type pixel sequential data
 ***********************************************************/

r_convDesc& r_Conv_NTF::convertFrom( const char *options ) throw(r_Error){
    
    char *nitf_file_data = NULL;
    char *nitf_img = NULL; 
    long fileSize = 0;
    RasNITF::nitf nitf_file;
    
    nitf_file_data = (char*) desc.src; 
    
    //READ NITF HEADERS
    
    nitf_file.read_headers_from(nitf_file_data, int(desc.srcInterv[0].high() - desc.srcInterv[0].low() + 1));
    
    // MOVE POINTER TO THE IMAGE DATA

    nitf_img = (char*)(desc.src + nitf_file.get_image_offset());
    fileSize = nitf_file.get_image_size();

    // CONVERT ALL DATA TO PIXEL SEQUENTIAL

    nitf_file.image_to_pixel_sequential(0);  // the address of the image is also known internaly so no need to pass the pointer
    
    // ALLOCATE STORAGE FOR DESC.DEST WITH? THE SIZE OF THE IMAGE

    if ((desc.dest = (char*)mystore.storage_alloc(fileSize)) == NULL)
    {
	RMInit::logOut << "r_Conv_NTF::convertTo(): out of memory!" << endl;
	throw r_Error(MEMMORYALLOCATIONERROR);
    }
    
    // COPY THE PIXEL SEQUENTIAL DATA TO THE DESC.DEST
    
    memcpy(desc.dest, nitf_img, fileSize);	
    
    // SET THE INTERVALS

    desc.destInterv = r_Minterval(2);
    desc.destInterv << r_Sinterval(( r_Range)0, (r_Range) ( nitf_file.get_image_width() - 1)) 
		    << r_Sinterval(( r_Range)0, (r_Range) ( nitf_file.get_image_height() -1));       
    
    // SETR BASE TYPE

    int image_index = 0;

    if(nitf_file.isRGB(image_index)) {
	desc.baseType = ctype_rgb;
    } else if(nitf_file.isGRAY(image_index)) {
	desc.baseType = ctype_char;
    } else if(nitf_file.isBOOL(image_index)) {
	desc.baseType = ctype_bool;
    }
    else {	
	RMInit::logOut << "r_Conv_NTF::convertFrom:" << "unsupported NITF file pixel type" << endl;
	throw r_Error(r_Error::r_Error_General);
    }
	
   

/*    switch (nitf_file.get_image_pixel_type()){
	case NITF_BI:
	    desc.baseType = ctype_bool;
	    break;
	case NITF_GRAY:
	    desc.baseType = ctype_char;
	    break;
	case NITF_RGB:
	    desc.baseType = ctype_rgb;
	    break;
	default:
	    RMInit::logOut << "r_Conv_NTF::convertFrom:" << "unsupported NITF file pixel type" << endl;
    }
*/  
    desc.destType = get_external_type(desc.baseType);
    
    return desc;
    
}

/***********************************************************
 * NAME: r_Conv_NTF::convertTo 
 * --------------------------------------------------------- 
 * PURPOSE: convert from rasdaman data to the 
 * corresponding nitf storage type 
 ***********************************************************/

r_convDesc& r_Conv_NTF::convertTo( const char *options ) throw(r_Error){
    
    int width=0, height=0;
    int fileSize = 0;
    
    // DETERMINE WIDTH AND HEIGHT

    width  = (int)(desc.srcInterv[0].high() - desc.srcInterv[0].low() + 1);
    height = (int)(desc.srcInterv[1].high() - desc.srcInterv[1].low() + 1);
    
    // CALCULATE THE SIZE OF THE IMAGE FILE

    switch (desc.baseType)
    {
    	case ctype_bool:
	    fileSize = width * height * 1; 
	    break;
	case ctype_char:
	    fileSize = width * height * 1; 
	    break;
	case ctype_rgb:
	    fileSize = width * height * 3; 
        default:
	    RMInit::logOut << "r_Conv_BMP::convertTo(): Unknown base type!" << endl;
	    throw r_Error(r_Error::r_Error_General);
    }

    // ALLOCATE STORAGE FOR THE IMAGE DATA
    
    if ((desc.dest = (char*)mystore.storage_alloc(fileSize)) == NULL)
    {
		RMInit::logOut << "r_Conv_NTF::convertTo(): out of memory!" << endl;
		throw r_Error(MEMMORYALLOCATIONERROR);
    }
    
    memcpy(desc.dest, desc.src, fileSize);

    // SET THE SIZE OF THE DATA

    desc.destInterv = r_Minterval(1);
    desc.destInterv << r_Sinterval((r_Range)0, (r_Range)fileSize-1);
    
    // SET THE TYPE OF TEH DATA

    desc.destType = r_Type::get_any_type("char");
    
    return desc;
}


r_Convertor* r_Conv_NTF::clone( void ) const{
    return new r_Conv_NTF(desc.src, desc.srcInterv, desc.baseType);
}

const char * r_Conv_NTF::get_name( void ) const{
    return format_name_ntf;
}

r_Data_Format r_Conv_NTF::get_data_format( void ) const{
    return r_NTF;
}