Official websites use .gov
A .gov website belongs to an official government organization in the United States.

Secure .gov websites use HTTPS
A lock ( ) or https:// means you’ve safely connected to the .gov website. Share sensitive information only on official, secure websites.

Isis 3 Programmer Reference
ProcessImport.cpp
1
5
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "ProcessImport.h"
8
9#include <float.h>
10#include <iostream>
11#include <QString>
12#include <sstream>
13
14#include "Application.h"
15#include "BoxcarCachingAlgorithm.h"
16#include "Brick.h"
17#include "Cube.h"
18#include "CubeAttribute.h"
19#include "IException.h"
20#include "IString.h"
21#include "JP2Decoder.h"
22#include "LineManager.h"
23#include "PixelType.h"
24#include "Process.h"
25#include "Pvl.h"
26#include "SpecialPixel.h"
27#include "UserInterface.h"
28
29#define EXPONENT_MASK ((char) 0x7F)
30
31
32using namespace std;
33namespace Isis {
34
37
38 p_progress->SetText("Importing");
39
40 // Initialize input file information
41 p_inFile = "";
42 p_pixelType = Isis::None;
43 p_suffixPixelType = Isis::None;
44 p_ns = p_nl = p_nb = 0;
45 p_byteOrder = Isis::NoByteOrder;
51 p_suffixData = 0;
53
54 p_base.push_back(0.0);
55 p_mult.push_back(1.0);
56 // Make all special pixels invalid
57 SetNull(DBL_MAX, -DBL_MAX);
58 SetHRS(DBL_MAX, -DBL_MAX);
59 SetLRS(DBL_MAX, -DBL_MAX);
60 SetHIS(DBL_MAX, -DBL_MAX);
61 SetLIS(DBL_MAX, -DBL_MAX);
62
63 p_saveFileHeader = false;
64 p_saveDataHeader = false;
65 p_saveDataTrailer = false;
66 p_saveDataPre = false;
67 p_saveDataPost = false;
68 p_saveFileTrailer = false;
69 p_vax_convert = false;
70
71 p_fileHeader = NULL;
72 p_fileTrailer = NULL;
73 }
74
75
78 if (p_fileHeader) {
79 delete [] p_fileHeader;
80 p_fileHeader = NULL;
81 }
82
83 for(unsigned int i = 0; i < p_dataHeader.size(); i++) {
84 delete [] p_dataHeader[i];
85 }
86
87 for(unsigned int i = 0; i < p_dataTrailer.size(); i++) {
88 delete [] p_dataTrailer[i];
89 }
90
91 vector<char *> temp;
92 for(unsigned int j = 0; j < p_dataPre.size(); j++) {
93 temp = p_dataPre[j];
94 for(unsigned int i = 0; i < temp.size(); i++) delete [] temp[i];
95 }
96
97 for(unsigned int j = 0; j < p_dataPost.size(); j++) {
98 temp = p_dataPost[j];
99 for(unsigned int i = 0; i < temp.size(); i++) delete [] temp[i];
100 }
101
102 if (p_fileTrailer) {
103 delete p_fileTrailer;
104 }
105 }
106
107
117 bool ProcessImport::IsVAXSpecial(unsigned int *vax, VAXSpecialPixel pix) {
118
119 unsigned int VAX_NULL = 0xFFFFFFFF;
120 unsigned int VAX_MIN = 0xFFEFFFFF;
121 unsigned int VAX_LRS = 0xFFFEFFFF;
122 unsigned int VAX_LIS = 0xFFFDFFFF;
123 unsigned int VAX_HIS = 0xFFFCFFFF;
124 unsigned int VAX_HRS = 0xFFFBFFFF;
125 int n;
126
127 unsigned int x;
128 memcpy(&x, vax, sizeof(unsigned int));
129
130 switch(pix) {
131 case VAX_NULL4:
132 n = memcmp(&VAX_NULL, &x, sizeof(unsigned int));
133 if (n == 0) return true;
134 break;
135 case VAX_LRS4:
136 n = memcmp(&VAX_LRS, &x, sizeof(unsigned int));
137 if (n == 0) return true;
138 break;
139 case VAX_LIS4:
140 n = memcmp(&VAX_LIS, &x, sizeof(unsigned int));
141 if (n == 0) return true;
142 break;
143 case VAX_HIS4:
144 n = memcmp(&VAX_HIS, &x, sizeof(unsigned int));
145 if (n == 0) return true;
146 break;
147 case VAX_HRS4:
148 n = memcmp(&VAX_HRS, &x, sizeof(unsigned int));
149 if (n == 0) return true;
150 break;
151 case VAX_MIN4:
152 n = memcmp(&VAX_MIN, &x, sizeof(unsigned int));
153 if (n == 0) return true;
154 break;
155 default:
156 return false;
157 }
158
159 return false;
160
161 }
162
163
170 double ProcessImport::VAXConversion(void *ibuf) {
171
172 float result;
173 double dresult;
174 bool swap_bytes = false;
175 bool swap_words = true;
176 int exp_adjust = -1;
177 int exp_mask = 0;
178 int exp_word = 1;
179 int exp_byte;
180 Isis::ByteOrder in_order = p_byteOrder;
181 Isis::ByteOrder out_order;
182
183 unsigned int *oli, *ili; //4-byte buffer io ptrs
184 unsigned short *osi; //2-byte buffer io ptrs
185 char *oci; //1-byte buffer io ptrs
186
187 if ( Isis::IsLsb() ) {
188 exp_byte = 1;
189 out_order = Isis::Lsb;
190 }
191 else {
192 exp_byte = 0;
193 out_order = Isis::Msb;
194 }
195
196 if (in_order != out_order) {
197 swap_bytes =true;
198 }
199
200 oli = (unsigned int * ) ibuf;
201 ili = (unsigned int * ) ibuf;
202
203 if (IsVAXSpecial(oli, ProcessImport::VAX_NULL4) ) {
204 return Isis::NULL8;
205 }
206
207 if (IsVAXSpecial(oli, ProcessImport::VAX_LIS4) ) {
208 return Isis::LOW_INSTR_SAT8;
209 }
210
211 if (IsVAXSpecial(oli, ProcessImport::VAX_LRS4) ) {
212 return Isis::LOW_REPR_SAT8;
213 }
214
215 if (IsVAXSpecial(oli, ProcessImport::VAX_HIS4) ) {
216 return Isis::HIGH_INSTR_SAT8;
217 }
218
219 if (IsVAXSpecial(oli, ProcessImport::VAX_HRS4) ) {
220 return Isis::HIGH_REPR_SAT8;
221 }
222
223 if (IsVAXSpecial(oli, ProcessImport::VAX_MIN4) ) {
224 return Isis::VALID_MIN8;
225 }
226
227 //test for word swapping
228 if (swap_words) {
229 *oli = (*ili <<16) | (*ili >> 16);
230 }
231
232 osi = (unsigned short* ) oli;
233
234 //test for byte swapping
235
236 if (swap_bytes) {
237 osi[0] = (osi[0] >> 8 ) | (osi[0] << 8);
238 osi[1] = (osi[1] >> 8 ) | (osi[1] << 8);
239 }
240
241 //Isolate the exponent and do the conversion
242 oci = (char *) &osi[exp_word];
243
244 if ( (oci[exp_byte] & EXPONENT_MASK) != exp_mask) {
245 oci[exp_byte] += exp_adjust;
246 }
247
248 result = *(float *)oli;
249 dresult = static_cast<double>(result);
250 return dresult;
251
252 }
253
254
263 void ProcessImport::SetPixelType(const Isis::PixelType type) {
264
265 if ((type == Isis::Double) || (type == Isis::Real) || (type == Isis::SignedWord) ||
266 (type == Isis::UnsignedWord) || (type == Isis::UnsignedByte) ||
267 (type == Isis::SignedInteger) || type==Isis::UnsignedInteger) {
268 p_pixelType = type;
269 }
270 else {
271 QString msg = "Unsupported pixel type [" +
272 Isis::PixelTypeName(type) + "]";
273 throw IException(IException::Programmer, msg, _FILEINFO_);
274 }
275 }
276
277 void ProcessImport::SetSuffixPixelType(const Isis::PixelType type) {
278
279 if ((type == Isis::Double) || (type == Isis::Real) || (type == Isis::SignedWord) ||
280 (type == Isis::UnsignedWord) || (type == Isis::UnsignedByte)) {
281 p_suffixPixelType = type;
282 }
283 else {
284 QString msg = "Unsupported pixel type [" +
285 Isis::PixelTypeName(type) + "]";
286 throw IException(IException::Programmer, msg, _FILEINFO_);
287 }
288 }
289
290
303 void ProcessImport::SetDimensions(const int ns, const int nl, const int nb) {
304 if (ns > 0 && nl > 0 && nb > 0) {
305 p_ns = ns;
306 p_nl = nl;
307 p_nb = nb;
308 }
309 else {
310 QString msg = "Illegal dimension [" + toString(ns) + ", " +
311 toString(nl) + ", " + toString(nb) + "]";
312 throw IException(IException::Programmer, msg, _FILEINFO_);
313 }
314 }
315
316
323 void ProcessImport::SetByteOrder(const Isis::ByteOrder order) {
324 p_byteOrder = order;
325 }
326
327
348 void ProcessImport::SetFileHeaderBytes(const int bytes) {
349 if (bytes >= 0) {
350 p_fileHeaderBytes = bytes;
351 }
352 else {
353 QString msg = "Illegal file header size [" + toString(bytes) + "]";
354 throw IException(IException::Programmer, msg, _FILEINFO_);
355 }
356 }
357
358
379 if (bytes >= 0) {
380 p_fileTrailerBytes = bytes;
381 }
382 else {
383 QString msg = "Illegal file trailer size [" + toString(bytes) + "]";
384 throw IException(IException::Programmer, msg, _FILEINFO_);
385 }
386 }
387
388
407 void ProcessImport::SetDataHeaderBytes(const int bytes) {
408 if (bytes >= 0) {
409 p_dataHeaderBytes = bytes;
410 }
411 else {
412 QString msg = "Illegal data header size [" + toString(bytes) + "]";
413 throw IException(IException::Programmer, msg, _FILEINFO_);
414 }
415 }
416
417
418 void ProcessImport::SetSuffixOffset(int samples, int lines, int coreBands, int itemBytes) {
419 p_suffixData = samples*lines*coreBands*itemBytes;
420 }
421
422
443
444 if (bytes >= 0) {
445 p_dataTrailerBytes = bytes;
446 }
447 else {
448 QString msg = "Illegal data trailer size [" + toString(bytes) + "]";
449 throw IException(IException::Programmer, msg, _FILEINFO_);
450 }
451 }
452
453
472 void ProcessImport::SetDataPrefixBytes(const int bytes) {
473
474 if (bytes >= 0) {
475 p_dataPreBytes = bytes;
476 }
477 else {
478 QString msg = "Illegal data prefix size [" + toString(bytes) + "]";
479 throw IException(IException::Programmer, msg, _FILEINFO_);
480 }
481 }
482
483
502 void ProcessImport::SetDataSuffixBytes(const int bytes) {
503
504 if (bytes >= 0) {
505 p_dataPostBytes = bytes;
506 }
507 else {
508 QString msg = "Illegal data suffix size [" + toString(bytes) + "]";
509 throw IException(IException::Programmer, msg, _FILEINFO_);
510 }
511 }
512
513
532 if (p_fileHeaderBytes == 0) {
533 QString msg = "File header bytes equals 0. There is nothing to save. "
534 "Use SetFileHeaderBytes() first.";
535 throw IException(IException::Programmer, msg, _FILEINFO_);
536 }
537 p_saveFileHeader = true;
538 }
539
540
559 if (p_fileTrailerBytes == 0) {
560 QString msg = "File trailer bytes equals 0. There is nothing to save. "
561 "Use SetFileTrailerBytes() first.";
562 throw IException(IException::Programmer, msg,
563 _FILEINFO_);
564 }
565 p_saveFileTrailer = true;
566 }
567
568
588 if (p_dataHeaderBytes == 0) {
589 QString msg = "Data header bytes equals 0. There is nothing to save. "
590 "Use SetDataHeaderBytes() first.";
591 throw IException(IException::Programmer, msg,
592 _FILEINFO_);
593 }
594 p_saveDataHeader = true;
595 }
596
597
617 if (p_dataTrailerBytes == 0) {
618 QString msg = "Data trailer bytes equals 0. There is nothing to save. "
619 "Use SetDataTrailerBytes() first.";
620 throw IException(IException::Programmer, msg,
621 _FILEINFO_);
622 }
623 p_saveDataTrailer = true;
624 }
625
626
645 if (p_dataPreBytes == 0) {
646 QString msg = "Data prefix bytes equals 0. There is nothing to save. "
647 "Use SetDataPrefixBytes() first.";
648 throw IException(IException::Programmer, msg,
649 _FILEINFO_);
650 }
651 p_saveDataPre = true;
652 }
653
654
674 if (p_dataPostBytes == 0) {
675 QString msg = "Data suffix bytes equals 0. There is nothing to save. "
676 "Use SetDataSuffixBytes() first.";
677 throw IException(IException::Programmer, msg,
678 _FILEINFO_);
679 }
680 p_saveDataPost = true;
681 }
682
683
688
689 return p_fileHeaderBytes;
690 }
691
692
699
700
705 return p_dataHeaderBytes;
706 }
707
708
715
716
721 return p_dataPreBytes;
722 }
723
724
729
730 return p_dataPostBytes;
731 }
732
733
753 if (p_saveFileHeader) {
754 return p_fileHeader;
755 }
756 QString msg = "File header was not saved. Use SaveFileHeader().";
757 throw IException(IException::Programmer, msg, _FILEINFO_);
758 }
759
760
779 if (p_saveFileTrailer) {
780 return p_fileTrailer;
781 }
782 QString msg = "File trailer was not saved. Use SaveFileTrailer()";
783 throw IException(IException::Programmer, msg, _FILEINFO_);
784 }
785
786
806 std::vector<char *> ProcessImport::DataHeader() {
807 if (p_saveDataHeader) {
808 return p_dataHeader;
809 }
810 QString msg = "Data header was not saved. Use SaveDataHeader()";
811 throw IException(IException::Programmer, msg, _FILEINFO_);
812 }
813
814
834 std::vector<char *> ProcessImport::DataTrailer() {
835 if (p_saveDataTrailer) {
836 return p_dataTrailer;
837 }
838 QString msg = "Data trailer was not saved. Use SaveDataTrailer()";
839 throw IException(IException::Programmer, msg, _FILEINFO_);
840 }
841
842
862 std::vector<std::vector<char *> > ProcessImport::DataPrefix() {
863 if (p_saveDataPre) {
864 return p_dataPre;
865 }
866 QString msg = "Data prefix was not saved. Use SaveDataPrefix()";
867 throw IException(IException::Programmer, msg, _FILEINFO_);
868 }
869
870
890 std::vector<std::vector<char *> > ProcessImport::DataSuffix() {
891 if (p_saveDataPost) {
892 return p_dataPost;
893 }
894 QString msg = "Data suffix was not saved. Use SaveDataSuffix()";
895 throw IException(IException::Programmer, msg, _FILEINFO_);
896 }
897
898
909
910
918 void ProcessImport::SetVAXConvert(const bool vax_convert) {
919 p_vax_convert = vax_convert;
920 }
921
922
930
931
937 void ProcessImport::SetBase(const double base) {
938 p_base.clear();
939 p_base.push_back(base);
940 }
941
942
948 void ProcessImport::SetBase(const std::vector<double> base) {
949 p_base = base;
950 }
951
952
958 void ProcessImport::SetMultiplier(const double mult) {
959 p_mult.clear();
960 p_mult.push_back(mult);
961 }
962
963
969 void ProcessImport::SetMultiplier(const std::vector<double> mult) {
970 p_mult = mult;
971 }
972
973
992 void ProcessImport::SetSpecialValues(const double null, const double lrs,
993 const double lis, const double hrs,
994 const double his) {
995 SetNull(null, null);
996 SetLRS(lrs, lrs);
997 SetLIS(lis, lis);
998 SetHRS(hrs, hrs);
999 SetHIS(his, his);
1000 }
1001
1002
1013 void ProcessImport::SetNull(const double null_min, const double null_max) {
1014 CheckPixelRange("Null", null_min, null_max);
1015 p_null_min = null_min;
1016 p_null_max = null_max;
1017 }
1018
1019
1030 void ProcessImport::SetLRS(const double lrs_min, const double lrs_max) {
1031 CheckPixelRange("LRS", lrs_min, lrs_max);
1032 p_lrs_min = lrs_min;
1033 p_lrs_max = lrs_max;
1034 }
1035
1036
1047 void ProcessImport::SetLIS(const double lis_min, const double lis_max) {
1048 CheckPixelRange("LIS", lis_min, lis_max);
1049 p_lis_min = lis_min;
1050 p_lis_max = lis_max;
1051 }
1052
1053
1064 void ProcessImport::SetHRS(const double hrs_min, const double hrs_max) {
1065 CheckPixelRange("HRS", hrs_min, hrs_max);
1066 p_hrs_min = hrs_min;
1067 p_hrs_max = hrs_max;
1068 }
1069
1070
1081 void ProcessImport::SetHIS(const double his_min, const double his_max) {
1082 CheckPixelRange("HIS", his_min, his_max);
1083 p_his_min = his_min;
1084 p_his_max = his_max;
1085 }
1086
1087
1097 void ProcessImport::CheckPixelRange(QString pixelName, double pixelMin,
1098 double pixelMax) {
1099 if (pixelMin == DBL_MAX || pixelMax == -DBL_MAX) return;
1100
1101 if (p_null_min != DBL_MAX && p_null_max != -DBL_MAX && ( //-null has been set
1102 (pixelMin > p_null_min && pixelMin < p_null_max) || // --min crossing
1103 (pixelMax > p_null_min && pixelMax < p_null_max) || // --max crossing
1104 (pixelMin < p_null_min && pixelMax > p_null_max))) { // --straddling
1105 // values
1106 QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1107 "," + toString(pixelMax) + "] overlaps the NULL range [" +
1108 toString(p_null_min) + "," + toString(p_null_max) + "]";
1109 throw IException(IException::User, msg, _FILEINFO_);
1110 }
1111
1112 if (p_lrs_min != DBL_MAX && p_lrs_max != -DBL_MAX && (
1113 (pixelMin > p_lrs_min && pixelMin < p_lrs_max) ||
1114 (pixelMax > p_lrs_min && pixelMax < p_lrs_max) ||
1115 (pixelMin < p_lrs_min && pixelMax > p_lrs_max))) {
1116 QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1117 "," + toString(pixelMax) + "] overlaps the LRS range [" +
1118 toString(p_lrs_min) + "," + toString(p_lrs_max) + "]";
1119 throw IException(IException::User, msg, _FILEINFO_);
1120 }
1121
1122 if (p_lis_min != DBL_MAX && p_lis_max != -DBL_MAX && (
1123 (pixelMin > p_lis_min && pixelMin < p_lis_max) ||
1124 (pixelMax > p_lis_min && pixelMax < p_lis_max) ||
1125 (pixelMin < p_lis_min && pixelMax > p_lis_max))) {
1126 QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1127 "," + toString(pixelMax) + "] overlaps the LIS range [" +
1128 toString(p_lis_min) + "," + toString(p_lis_max) + "]";
1129 throw IException(IException::User, msg, _FILEINFO_);
1130 }
1131
1132 if (p_hrs_min != DBL_MAX && p_hrs_max != -DBL_MAX && (
1133 (pixelMin > p_hrs_min && pixelMin < p_hrs_max) ||
1134 (pixelMax > p_hrs_min && pixelMax < p_hrs_max) ||
1135 (pixelMin < p_hrs_min && pixelMax > p_hrs_max))) {
1136 QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1137 "," + toString(pixelMax) + "] overlaps the HRS range [" +
1138 toString(p_hrs_min) + "," + toString(p_hrs_max) + "]";
1139 throw IException(IException::User, msg, _FILEINFO_);
1140 }
1141
1142 if (p_his_min != DBL_MAX && p_his_max != -DBL_MAX && (
1143 (pixelMin > p_his_min && pixelMin < p_his_max) ||
1144 (pixelMax > p_his_min && pixelMax < p_his_max) ||
1145 (pixelMin < p_his_min && pixelMax > p_his_max))) {
1146 QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1147 "," + toString(pixelMax) + "] overlaps the HIS range [" +
1148 toString(p_his_min) + "," + toString(p_his_max) + "]";
1149 throw IException(IException::User, msg, _FILEINFO_);
1150 }
1151
1152 }
1153
1154
1166 double ProcessImport::TestPixel(const double pixel) {
1167 if (pixel <= p_null_max && pixel >= p_null_min) {
1168 return Isis::NULL8;
1169 }
1170 else if (pixel <= p_hrs_max && pixel >= p_hrs_min) {
1171 return Isis::HIGH_REPR_SAT8;
1172 }
1173 else if (pixel <= p_lrs_max && pixel >= p_lrs_min) {
1174 return Isis::LOW_REPR_SAT8;
1175 }
1176 else if (pixel <= p_his_max && pixel >= p_his_min) {
1177 return Isis::HIGH_INSTR_SAT8;
1178 }
1179 else if (pixel <= p_lis_max && pixel >= p_lis_min) {
1180 return Isis::LOW_INSTR_SAT8;
1181 }
1182 else {
1183 return pixel;
1184 }
1185 }
1186
1187
1198 if (att.propagateMinimumMaximum()) {
1199 double min, max;
1200 if ((p_pixelType == Isis::Double) ||
1201 (p_pixelType == Isis::Real) ||
1202 (p_base.size() > 1) || (p_mult.size() > 1)) {
1203 min = Isis::VALID_MIN4;
1204 max = Isis::VALID_MAX4;
1205 }
1206 else if (p_pixelType == Isis::SignedInteger) {
1207 min = Isis::IVALID_MIN4;
1208 max = Isis::IVALID_MAX4;
1209 }
1210
1211 else if (p_pixelType == Isis::UnsignedInteger) {
1212 min = Isis::VALID_MINUI4;
1213 max = Isis::VALID_MAXUI4;
1214 }
1215 else if (p_pixelType == Isis::SignedWord) {
1216 min = Isis::VALID_MIN2 * p_mult[0] + p_base[0];
1217 max = Isis::VALID_MAX2 * p_mult[0] + p_base[0];
1218 }
1219 else if (p_pixelType == Isis::UnsignedWord) {
1220 min = Isis::VALID_MINU2 * p_mult[0] + p_base[0];
1221 max = Isis::VALID_MAXU2 * p_mult[0] + p_base[0];
1222 }
1223 else if (p_pixelType == Isis::UnsignedByte) {
1224 min = Isis::VALID_MIN1 * p_mult[0] + p_base[0];
1225 max = Isis::VALID_MAX1 * p_mult[0] + p_base[0];
1226 }
1227 else {
1228 QString msg = "Unsupported pixel type [" +
1229 Isis::PixelTypeName(p_pixelType) + "]";
1230 throw IException(IException::Programmer, msg, _FILEINFO_);
1231 }
1232 att.setMinimum(min);
1233 att.setMaximum(max);
1234 }
1235
1236 if (att.propagatePixelType()) {
1237 if ((p_base.size() > 1) || (p_mult.size() > 1)) {
1238 att.setPixelType(Isis::Real);
1239 }
1240 else if (p_pixelType == Isis::Double || p_pixelType == Isis::SignedInteger) {
1241 att.setPixelType(Isis::Real);
1242 }
1243 else {
1245 }
1246 }
1247 }
1248
1249
1260 Isis::Cube *ProcessImport::SetOutputCube(const QString &parameter) {
1261 CubeAttributeOutput &att =
1263
1264 SetAttributes(att);
1265
1266 return Process::SetOutputCube(Application::GetUserInterface().GetCubeName(parameter), att, p_ns, p_nl, p_nb);
1267 }
1268
1269
1275 CubeAttributeOutput &att = ui.GetOutputAttribute(parameter);
1276 SetAttributes(att);
1277 return Isis::Process::SetOutputCube(ui.GetCubeName(parameter), att, p_ns, p_nl, p_nb);
1278 }
1279
1295 SetAttributes(att);
1296 return Isis::Process::SetOutputCube(fname, att, p_ns, p_nl, p_nb);
1297 }
1298
1299
1303 ProcessJp2();
1304 }
1305 else if (p_organization == ProcessImport::BSQ) {
1306 ProcessBsq();
1307 }
1308 else if (p_organization == ProcessImport::BIL) {
1309 ProcessBil();
1310 }
1311 else if (p_organization == ProcessImport::BIP) {
1312 ProcessBip();
1313 }
1314 else {
1315 QString msg = "File [" + p_inFile
1316 + "] is not in a supported organization.";
1317 throw IException(IException::Programmer, msg, _FILEINFO_);
1318 }
1319 }
1320
1321
1333 ProcessJp2(funct);
1334 }
1335 else if (p_organization == ProcessImport::BSQ) {
1336 ProcessBsq(funct);
1337 }
1338 else if (p_organization == ProcessImport::BIL) {
1339 ProcessBil(funct);
1340 }
1341 else if (p_organization == ProcessImport::BIP) {
1342 ProcessBip(funct);
1343 }
1344 else {
1345 QString msg = "File [" + p_inFile + "] is not in a supported organization.";
1346 throw IException(IException::Programmer, msg, _FILEINFO_);
1347 }
1348 }
1349
1350
1363 // Figure out the number of bytes to read for a single line
1364 int readBytes = Isis::SizeOf(p_pixelType);
1365 readBytes = readBytes * p_ns;
1366 char *in = new char [readBytes];
1367
1368 // Set up an Isis::EndianSwapper object
1369 QString tok(Isis::ByteOrderName(p_byteOrder));
1370 tok = tok.toUpper();
1371 Isis::EndianSwapper swapper(tok);
1372
1373 ifstream fin;
1374 // Open input file
1375 Isis::FileName inFile(p_inFile);
1376 QString inFileName(inFile.expanded());
1377 fin.open(inFileName.toLatin1().data(), ios::in | ios::binary);
1378 if (!fin.is_open()) {
1379 QString msg = "Cannot open input file [" + p_inFile + "]";
1380 throw IException(IException::Io, msg, _FILEINFO_);
1381 }
1382
1383 // Handle the file header
1384 streampos pos = fin.tellg();
1385 if (p_saveFileHeader) {
1386 p_fileHeader = new char[p_fileHeaderBytes];
1388 fin.seekg(p_suffixData+p_fileHeaderBytes, ios_base::beg);
1389 }
1390 else {
1391 fin.seekg(p_fileHeaderBytes+p_suffixData, ios_base::beg);
1392 }
1393
1394 // Check the last io
1395 if (!fin.good()) {
1396 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1397 toString((int)pos) + "]. Byte count [" +
1399 throw IException(IException::Io, msg, _FILEINFO_);
1400 }
1401
1402 // Construct a line buffer manager
1403 Isis::Buffer *out = NULL;
1404
1405 if (funct != NULL) {
1406 out = new Isis::Brick(p_ns, 1, 1, p_pixelType);
1407 }
1408 else {
1409 out = new Isis::LineManager(*OutputCubes[0]);
1410 }
1411
1412 // Loop once for each band in the image
1413 p_progress->SetMaximumSteps(p_nl * p_nb);
1414 p_progress->CheckStatus();
1415
1416 // Loop for each band
1417 for(int band = 0; band < p_nb; band++) {
1418 // Set the base multiplier
1419 double base, mult;
1420 if (p_base.size() > 1) {
1421 base = p_base[band];
1422 mult = p_mult[band];
1423 }
1424 else {
1425 base = p_base[0];
1426 mult = p_mult[0];
1427 }
1428
1429 // Handle any data headers (e.g., the data at the beginning of each band)
1430 pos = fin.tellg();
1431 if (p_saveDataHeader) {
1432 p_dataHeader.push_back(new char[p_dataHeaderBytes]);
1433 fin.read(p_dataHeader.back(), p_dataHeaderBytes);
1434 }
1435 else {
1436 fin.seekg(p_dataHeaderBytes, ios_base::cur);
1437 }
1438
1439 // Check the last io
1440 if (!fin.good()) {
1441 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1442 toString((int)pos) + "]. Byte count [" +
1444 throw IException(IException::Io, msg, _FILEINFO_);
1445 }
1446
1447 // Space for storing prefix and suffix data pointers
1448 vector<char *> tempPre, tempPost;
1449
1450 // Loop for each line in a band
1451 for(int line = 0; line < p_nl; line++) {
1452
1453 // Handle any line prefix bytes
1454 pos = fin.tellg();
1455 if (p_saveDataPre) {
1456 tempPre.push_back(new char[p_dataPreBytes]);
1457 fin.read(tempPre.back(), p_dataPreBytes);
1458 }
1459 else {
1460 fin.seekg(p_dataPreBytes, ios_base::cur);
1461 }
1462
1463 // Check the last io
1464 if (!fin.good()) {
1465 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1466 toString((int)pos) + "]. Byte count [" +
1467 toString(p_dataPreBytes) + "]" ;
1468 throw IException(IException::Io, msg, _FILEINFO_);
1469 }
1470
1471 // Get a line of data from the input file
1472 pos = fin.tellg();
1473 fin.read(in, readBytes);
1474 if (!fin.good()) {
1475 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1476 toString((int)pos) + "]. Byte count [" +
1477 toString(readBytes) + "]" ;
1478 throw IException(IException::Io, msg, _FILEINFO_);
1479 }
1480
1481 // Swap the bytes if necessary and convert any out of bounds pixels
1482 // to special pixels
1483 for(int samp = 0; samp < p_ns; samp++) {
1484 switch(p_pixelType) {
1485 case Isis::UnsignedByte:
1486 (*out)[samp] = (double)((unsigned char *)in)[samp];
1487 break;
1488 case Isis::UnsignedWord:
1489 (*out)[samp] =
1490 (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1491 break;
1492 case Isis::SignedWord:
1493 (*out)[samp] = (double)swapper.ShortInt((short int *)in+samp);
1494 break;
1495 case Isis::SignedInteger:
1496 (*out)[samp] = (double)swapper.Int((int *)in+samp);
1497 break;
1498
1499 case Isis::UnsignedInteger:
1500 (*out)[samp] = (double)swapper.Uint32_t((unsigned int *)in+samp);
1501 break;
1502 case Isis::Real:
1503 if(p_vax_convert) {
1504 (*out)[samp]= VAXConversion( (float *)in+samp );
1505 }
1506 else {
1507 (*out)[samp] = (double)swapper.Float((float *)in+samp);
1508 }
1509 break;
1510 case Isis::Double:
1511 (*out)[samp] = (double)swapper.Double((double *)in+samp);
1512 break;
1513 default:
1514 break;
1515 }
1516
1517 (*out)[samp] = TestPixel((*out)[samp]);
1518
1519 if (Isis::IsValidPixel((*out)[samp])) {
1520 (*out)[samp] = mult * ((*out)[samp]) + base;
1521 }
1522 } // End sample loop
1523
1524 if (funct == NULL) {
1525 // Set the buffer position and write the line to the output file
1526 ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1527 OutputCubes[0]->write(*out);
1528 }
1529 else {
1530 ((Isis::Brick *)out)->SetBaseSample(1);
1531 ((Isis::Brick *)out)->SetBaseLine(line + 1);
1532 ((Isis::Brick *)out)->SetBaseBand(band + 1);
1533 funct(*out);
1534 }
1535
1536 p_progress->CheckStatus();
1537
1538 // Handle any line suffix bytes
1539 pos = fin.tellg();
1540 if (p_saveDataPost) {
1541 tempPost.push_back(new char[p_dataPostBytes]);
1542 fin.read(tempPost.back(), p_dataPostBytes);
1543 }
1544 else {
1545 fin.seekg(p_dataPostBytes, ios_base::cur);
1546 }
1547
1548 // Check the last io
1549 if (!fin.good()) {
1550 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1551 toString((int)pos) + "]. Byte count [" +
1552 toString(p_dataPreBytes) + "]" ;
1553 throw IException(IException::Io, msg, _FILEINFO_);
1554 }
1555 } // End line loop
1556
1557 // Save off the prefix bytes vector
1558 if (p_saveDataPre) {
1559 p_dataPre.push_back(tempPre);
1560 tempPre.clear();
1561 }
1562
1563 // Save off the suffix bytes vector
1564 if (p_saveDataPost) {
1565 p_dataPost.push_back(tempPost);
1566 tempPost.clear();
1567 }
1568
1569 // Handle the band trailer
1570 pos = fin.tellg();
1571 if (p_saveDataTrailer) {
1572 p_dataTrailer.push_back(new char[p_dataTrailerBytes]);
1573 fin.read(p_dataTrailer.back(), p_dataTrailerBytes);
1574 }
1575 else {
1576 fin.seekg(p_dataTrailerBytes, ios_base::cur);
1577 }
1578
1579 // Check the last io
1580 if (!fin.good()) {
1581 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1582 toString((int)pos) + "]. Byte count [" +
1584 throw IException(IException::Io, msg, _FILEINFO_);
1585 }
1586
1587 } // End band loop
1588
1589 // Handle the file trailer
1590 pos = fin.tellg();
1591 if (p_saveFileTrailer) {
1592 fin.seekg(0, ios_base::end);
1593 streampos e = fin.tellg();
1594 p_fileTrailerBytes = (int)(e - pos + (streampos)1);
1596 fin.seekg(pos);
1598
1599 // Check the io
1600 if (!fin.good()) {
1601 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1602 toString((int)pos) + "]. Byte count [" +
1604 throw IException(IException::Io, msg, _FILEINFO_);
1605 }
1606
1607 }
1608
1609 // Close the file and clean up
1610 fin.close();
1611 delete [] in;
1612 }
1613
1614
1625 void ProcessImport::ProcessBil(void funct(Isis::Buffer &someBuf)) {
1626
1627 // Figure out the number of bytes to read for a single line
1628 int readBytes = Isis::SizeOf(p_pixelType);
1629 readBytes = readBytes * p_ns;
1630 char *in = new char [readBytes];
1631
1632 // Set up an Isis::EndianSwapper object
1633 QString tok(Isis::ByteOrderName(p_byteOrder));
1634 tok = tok.toUpper();
1635 Isis::EndianSwapper swapper(tok);
1636
1637 ifstream fin;
1638 // Open input file
1639 Isis::FileName inFile(p_inFile);
1640 QString inFileName(inFile.expanded());
1641 fin.open(inFileName.toLatin1().data(), ios::in | ios::binary);
1642 if (!fin.is_open()) {
1643 QString msg = "Cannot open input file [" + p_inFile + "]";
1644 throw IException(IException::Io, msg, _FILEINFO_);
1645 }
1646
1647 // Handle the file header
1648 streampos pos = fin.tellg();
1649
1650 if (p_saveFileHeader) {
1651 p_fileHeader = new char[p_fileHeaderBytes];
1653 fin.seekg(p_suffixData+p_fileHeaderBytes, ios_base::beg);
1654 }
1655 else {
1656 fin.seekg(p_fileHeaderBytes+p_suffixData, ios_base::beg);
1657 }
1658
1659 // Check the last io
1660 if (!fin.good()) {
1661 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1662 toString((int)pos) + "]. Byte count [" +
1664 throw IException(IException::Io, msg, _FILEINFO_);
1665 }
1666
1667 // Construct a line buffer manager
1668 Isis::Buffer *out = NULL;
1669
1670 if (funct != NULL) {
1671 out = new Isis::Brick(p_ns, p_nl, p_nb, p_ns, 1, 1, p_pixelType, true);
1672 ((Isis::Brick *)out)->setpos(0);
1673 }
1674 else {
1675 OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
1676 out = new Isis::LineManager(*OutputCubes[0]);
1677 }
1678
1679 // Loop once for each line in the image
1680 p_progress->SetMaximumSteps(p_nb * p_nl);
1681 p_progress->CheckStatus();
1682
1683 // Loop for each line
1684 for(int line = 0; line < p_nl; line++) {
1685 // Loop for each band
1686 for(int band = 0; band < p_nb; band++) {
1687 // Set the base multiplier
1688 double base, mult;
1689 if (p_base.size() > 1) {
1690 base = p_base[band];
1691 mult = p_mult[band];
1692 }
1693 else {
1694 base = p_base[0];
1695 mult = p_mult[0];
1696 }
1697
1698 // Check the last io
1699 if (!fin.good()) {
1700 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1701 toString((int)pos) + "]. Byte count [" +
1703 throw IException(IException::Io, msg, _FILEINFO_);
1704 }
1705
1706 // Space for storing prefix and suffix data pointers
1707 vector<char *> tempPre, tempPost;
1708
1709 // Handle any line prefix bytes
1710 pos = fin.tellg();
1711 if (p_saveDataPre) {
1712 tempPre.push_back(new char[p_dataPreBytes]);
1713 fin.read(tempPre.back(), p_dataPreBytes);
1714 }
1715 else {
1716 fin.seekg(p_dataPreBytes, ios_base::cur);
1717 }
1718
1719 // Check the last io
1720 if (!fin.good()) {
1721 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1722 toString((int)pos) + "]. Byte count [" +
1723 toString(p_dataPreBytes) + "]" ;
1724 throw IException(IException::Io, msg, _FILEINFO_);
1725 }
1726
1727
1728 // Get a line of data from the input file
1729 pos = fin.tellg();
1730 fin.read(in, readBytes);
1731 if (!fin.good()) {
1732 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1733 toString((int)pos) + "]. Byte count [" +
1734 toString(readBytes) + "]" ;
1735 throw IException(IException::Io, msg, _FILEINFO_);
1736 }
1737
1738 // Swap the bytes if necessary and convert any out of bounds pixels
1739 // to special pixels
1740 for(int samp = 0; samp < p_ns; samp++) {
1741 switch(p_pixelType) {
1742 case Isis::UnsignedByte:
1743 (*out)[samp] = (double)((unsigned char *)in)[samp];
1744 break;
1745 case Isis::UnsignedWord:
1746 (*out)[samp] =
1747 (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1748 break;
1749 case Isis::SignedWord:
1750 (*out)[samp] = (double)swapper.ShortInt((short int *)in+samp);
1751 break;
1752 case Isis::SignedInteger:
1753 (*out)[samp] = (double)swapper.Int((int *)in+samp);
1754 break;
1755 case Isis::UnsignedInteger:
1756 (*out)[samp] = (double)swapper.Uint32_t((unsigned int *)in+samp);
1757 break;
1758 case Isis::Real:
1759 if(p_vax_convert) {
1760 (*out)[samp]= VAXConversion( (float *)in+samp );
1761 }
1762 else {
1763 (*out)[samp] = (double)swapper.Float((float *)in+samp);
1764 }
1765 break;
1766 case Isis::Double:
1767 (*out)[samp] = (double)swapper.Double((double *)in+samp);
1768 break;
1769 default:
1770 break;
1771 }
1772
1773 // Sets out to isis special pixel or leaves it if valid
1774 (*out)[samp] = TestPixel((*out)[samp]);
1775
1776 if (Isis::IsValidPixel((*out)[samp])) {
1777 (*out)[samp] = mult * ((*out)[samp]) + base;
1778 }
1779 } // End sample loop
1780
1781 if (funct == NULL) {
1782 ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1783 OutputCubes[0]->write(*out);
1784 }
1785 else {
1786 funct(*out);
1787 (*((Isis::Brick *)out))++;
1788 }
1789
1790 p_progress->CheckStatus();
1791
1792 // Handle any line suffix bytes
1793 pos = fin.tellg();
1794 if (p_saveDataPost) {
1795 tempPost.push_back(new char[p_dataPostBytes]);
1796 fin.read(tempPost.back(), p_dataPostBytes);
1797 }
1798 else {
1799 fin.seekg(p_dataPostBytes, ios_base::cur);
1800 }
1801
1802 // Check the last io
1803 if (!fin.good()) {
1804 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1805 toString((int)pos) + "]. Byte count [" +
1806 toString(p_dataPreBytes) + "]" ;
1807 throw IException(IException::Io, msg, _FILEINFO_);
1808 }
1809
1810 // Save off the prefix bytes vector
1811 if (p_saveDataPre) {
1812 p_dataPre.push_back(tempPre);
1813 tempPre.clear();
1814 }
1815
1816 // Save off the suffix bytes vector
1817 if (p_saveDataPost) {
1818 p_dataPost.push_back(tempPost);
1819 tempPost.clear();
1820 }
1821
1822 // Check the last io
1823 if (!fin.good()) {
1824 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1825 toString((int)pos) + "]. Byte count [" +
1827 throw IException(IException::Io, msg, _FILEINFO_);
1828 }
1829
1830 } // End band loop
1831
1832 } // End line loop
1833
1834 // Handle the file trailer
1835 pos = fin.tellg();
1836 if (p_saveFileTrailer) {
1837 fin.seekg(0, ios_base::end);
1838 streampos e = fin.tellg();
1839 p_fileTrailerBytes = (int)(e - pos + (streampos)1);
1841 fin.seekg(pos);
1843
1844 // Check the io
1845 if (!fin.good()) {
1846 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1847 toString((int)pos) + "]. Byte count [" +
1849 throw IException(IException::Io, msg, _FILEINFO_);
1850 }
1851
1852 }
1853
1854 // Close the file and clean up
1855 fin.close();
1856 delete [] in;
1857 }
1858
1859
1871
1872 // Set up an Isis::EndianSwapper object
1873 QString tok(Isis::ByteOrderName(p_byteOrder));
1874 tok = tok.toUpper();
1875 Isis::EndianSwapper swapper(tok);
1876
1877 ifstream fin;
1878 // Open input file
1879 Isis::FileName inFile(p_inFile);
1880 QString inFileName(inFile.expanded());
1881 fin.open(inFileName.toLatin1().data(), ios::in | ios::binary);
1882 if (!fin.is_open()) {
1883 QString msg = "Cannot open input file [" + p_inFile + "]";
1884 throw IException(IException::Io, msg, _FILEINFO_);
1885 }
1886
1887 // Handle the file header
1888 streampos pos = fin.tellg();
1889 if (p_saveFileHeader) {
1890 p_fileHeader = new char[p_fileHeaderBytes];
1892 fin.seekg(p_suffixData+p_fileHeaderBytes, ios_base::beg);
1893 }
1894 else {
1895 fin.seekg(p_fileHeaderBytes+p_suffixData, ios_base::beg);
1896 }
1897
1898 // Check the last io
1899 if (!fin.good()) {
1900 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1901 toString((int)pos) + "]. Byte count [" +
1903 throw IException(IException::Io, msg, _FILEINFO_);
1904 }
1905
1906 OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
1907
1908 // Construct a line buffer manager
1909 Isis::Buffer *out = NULL;
1910
1911 if (funct != NULL) {
1912 out = new Isis::Buffer(p_ns, p_nl, p_nb, p_pixelType);
1913 }
1914 else {
1915 out = new Isis::LineManager(*OutputCubes[0]);
1916 }
1917
1918 // Loop once for each line in the image
1919 p_progress->SetMaximumSteps(p_nl);
1920 p_progress->CheckStatus();
1921
1922 // Figure out the number of bytes to read for a single line
1923 int sampleBytes = Isis::SizeOf(p_pixelType) * p_nb + p_dataPreBytes + p_dataPostBytes;
1924 int readBytes = p_ns * sampleBytes;
1925 char *in = new char [readBytes];
1926
1927 // Loop for each line
1928 for(int line = 0; line < p_nl; line++) {
1929 // Check the last io
1930 if (!fin.good()) {
1931 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1932 toString((int)pos) + "]. Byte count [" +
1934 throw IException(IException::Io, msg, _FILEINFO_);
1935 }
1936
1937 // Space for storing prefix and suffix data pointers
1938 vector<char *> tempPre, tempPost;
1939
1940 // Check the last io
1941 if (!fin.good()) {
1942 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1943 toString((int)pos) + "]. Byte count [" +
1944 toString(p_dataPreBytes) + "]" ;
1945 throw IException(IException::Io, msg, _FILEINFO_);
1946 }
1947
1948 // Get a line of data from the input file
1949 pos = fin.tellg();
1950 fin.read(in, readBytes);
1951 if (!fin.good()) {
1952 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1953 toString((int)pos) + "]. Byte count [" +
1954 toString(readBytes) + "]" ;
1955 throw IException(IException::Io, msg, _FILEINFO_);
1956 }
1957
1958 // Loop for each band
1959 for(int band = 0; band < p_nb; band++) {
1960 // Set the base multiplier
1961 double base, mult;
1962 if (p_base.size() > 1) {
1963 base = p_base[band];
1964 mult = p_mult[band];
1965 }
1966 else {
1967 base = p_base[0];
1968 mult = p_mult[0];
1969 }
1970
1971 // Swap the bytes if necessary and convert any out of bounds pixels
1972 // to special pixels
1973 int osamp = 0;
1974
1975 for(int samp = 0; samp < p_ns; samp++) {
1976 int bufferIndex = p_dataPreBytes + Isis::SizeOf(p_pixelType)*band + samp*sampleBytes;
1977 switch(p_pixelType) {
1978 case Isis::UnsignedByte:
1979 (*out)[osamp] = (double)((unsigned char *)in)[bufferIndex];
1980 break;
1981 case Isis::UnsignedWord:
1982 (*out)[osamp] =
1983 (double)swapper.UnsignedShortInt(&in[bufferIndex]);
1984 break;
1985 case Isis::SignedWord:
1986 (*out)[osamp] = (double)swapper.ShortInt(&in[bufferIndex]);
1987 break;
1988 case Isis::SignedInteger:
1989 (*out)[samp] = (double)swapper.Int(&in[bufferIndex]);
1990 break;
1991 case Isis::UnsignedInteger:
1992 (*out)[samp] = (double)swapper.Uint32_t(&in[bufferIndex]);
1993 break;
1994 case Isis::Real:
1995 if(p_vax_convert) {
1996 (*out)[osamp]= VAXConversion(&in[bufferIndex]);
1997 }
1998 else {
1999 (*out)[osamp] = (double)swapper.Float(&in[bufferIndex]);
2000 }
2001 break;
2002 case Isis::Double:
2003 (*out)[osamp] = (double)swapper.Double(&in[bufferIndex]);
2004 break;
2005 default:
2006 break;
2007 }
2008
2009 // Sets out to isis special pixel or leaves it if valid
2010 (*out)[osamp] = TestPixel((*out)[osamp]);
2011
2012 if (Isis::IsValidPixel((*out)[osamp])) {
2013 (*out)[osamp] = mult * ((*out)[osamp]) + base;
2014 }
2015 osamp++;
2016 } // End sample loop
2017
2018 if (funct == NULL) {
2019 //Set the buffer position and write the line to the output file
2020 ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
2021 OutputCubes[0]->write(*out);
2022 }
2023 else {
2024 funct(*out);
2025 }
2026
2027 } // End band loop
2028
2029 // Handle record prefix and suffix
2030 if (p_saveDataPre) {
2031 for(int samp = 0; samp < p_ns; samp++) {
2032 char *samplePrefix = new char[p_dataPreBytes];
2033 memcpy(samplePrefix, &in[samp*sampleBytes], p_dataPreBytes);
2034 tempPre.push_back(samplePrefix);
2035 }
2036 p_dataPre.push_back(tempPre);
2037 tempPre.clear();
2038 }
2039 if (p_saveDataPost) {
2040 for(int samp = 0; samp < p_ns; samp++) {
2041 char *sampleSuffix = new char[p_dataPostBytes];
2042 int suffixIndex = p_dataPreBytes + Isis::SizeOf(p_pixelType)*p_nb + samp*sampleBytes;
2043 memcpy(sampleSuffix, &in[suffixIndex], p_dataPostBytes);
2044 tempPost.push_back(sampleSuffix);
2045 }
2046 p_dataPost.push_back(tempPost);
2047 tempPost.clear();
2048 }
2049
2050 // Check the last io
2051 if (!fin.good()) {
2052 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
2053 toString((int)pos) + "]. Byte count [" +
2054 toString(p_dataPreBytes) + "]" ;
2055 throw IException(IException::Io, msg, _FILEINFO_);
2056 }
2057
2058 // Handle the data trailer
2059 pos = fin.tellg();
2060 if (p_saveDataTrailer) {
2061 p_dataTrailer.push_back(new char[p_dataTrailerBytes]);
2062 fin.read(p_dataTrailer.back(), p_dataTrailerBytes);
2063 }
2064 else {
2065 fin.seekg(p_dataTrailerBytes, ios_base::cur);
2066 }
2067
2068 // Check the last io
2069 if (!fin.good()) {
2070 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
2071 toString((int)pos) + "]. Byte count [" +
2073 throw IException(IException::Io, msg, _FILEINFO_);
2074 }
2075
2076 p_progress->CheckStatus();
2077
2078 } // End line loop
2079
2080 // Handle the file trailer
2081 pos = fin.tellg();
2082 if (p_saveFileTrailer) {
2083 fin.seekg(0, ios_base::end);
2084 streampos e = fin.tellg();
2085 p_fileTrailerBytes = (int)(e - pos + (streampos)1);
2087 fin.seekg(pos);
2089
2090 // Check the io
2091 if (!fin.good()) {
2092 QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
2093 toString((int)pos) + "]. Byte count [" +
2095 throw IException(IException::Io, msg, _FILEINFO_);
2096 }
2097
2098 }
2099
2100 // Close the file and clean up
2101 fin.close();
2102 delete [] in;
2103
2104 }
2105
2106
2123
2124 // Set up an Isis::Jp2Decoder object
2125 JP2Decoder *JP2_decoder;
2126 JP2_decoder = new JP2Decoder(p_inFile);
2127
2128 // Open JP2 file
2129 JP2_decoder->OpenFile();
2130
2131 // Make sure JP2 file dimensions match PDS labels
2132 p_ns = JP2_decoder->GetSampleDimension();
2133 p_nl = JP2_decoder->GetLineDimension();
2134 p_nb = JP2_decoder->GetBandDimension();
2135
2136 // Figure out the number of bytes to read for a single line
2137 // from all bands
2138 int sizeofpixel = Isis::SizeOf(p_pixelType);
2139 int startsamp = p_dataPreBytes / sizeofpixel;
2140 int endsamp = startsamp + p_ns;
2141 int readBytes = sizeofpixel * p_ns * p_nb
2143 char **in = new char* [p_nb];
2144 for(int i = 0; i < p_nb; i++) {
2145 in[i] = new char [readBytes];
2146 }
2147
2148 // Construct a line buffer manager
2149 Isis::Buffer *out = NULL;
2150
2151 if (funct != NULL) {
2152 out = new Isis::Buffer(p_ns, p_nl, p_nb, p_pixelType);
2153 }
2154 else {
2155 out = new Isis::LineManager(*OutputCubes[0]);
2156 }
2157
2158 // Loop once for each line in the image
2159 p_progress->SetMaximumSteps(p_nb * p_nl);
2160 p_progress->CheckStatus();
2161
2162 // Loop for each line
2163 for(int line = 0; line < p_nl; line++) {
2164 if (p_pixelType == Isis::UnsignedByte) {
2165 JP2_decoder->Read((unsigned char **)in);
2166 }
2167 else {
2168 JP2_decoder->Read((short int **)in);
2169 }
2170 // Loop for each band
2171 for(int band = 0; band < p_nb; band++) {
2172 // Set the base multiplier
2173 double base, mult;
2174 if (p_base.size() > 1) {
2175 base = p_base[band];
2176 mult = p_mult[band];
2177 }
2178 else {
2179 base = p_base[0];
2180 mult = p_mult[0];
2181 }
2182
2183 // Space for storing prefix and suffix data pointers
2184 vector<char *> tempPre, tempPost;
2185
2186
2187 // Handle any line prefix bytes
2188 if (p_saveDataPre) {
2189 tempPre.push_back(new char[p_dataPreBytes]);
2190 memcpy(&tempPre[0], in[band], p_dataPreBytes);
2191 }
2192
2193 // Swap the bytes if necessary and convert any out of bounds pixels
2194 // to special pixels
2195 for(int samp = startsamp; samp < endsamp; samp++) {
2196 switch(p_pixelType) {
2197 case Isis::UnsignedByte:
2198 (*out)[samp] = (double)((unsigned char *)in[band])[samp];
2199 break;
2200 case Isis::UnsignedWord:
2201 (*out)[samp] = (double)((unsigned short int *)in[band])[samp];
2202 break;
2203 case Isis::SignedWord:
2204 (*out)[samp] = (double)((short int *)in[band])[samp];
2205 break;
2206 default:
2207 break;
2208 }
2209
2210 // Sets out to isis special pixel or leaves it if valid
2211 (*out)[samp] = TestPixel((*out)[samp]);
2212
2213 if (Isis::IsValidPixel((*out)[samp])) {
2214 (*out)[samp] = mult * ((*out)[samp]) + base;
2215 }
2216 } // End sample loop
2217
2218 if (funct == NULL) {
2219 //Set the buffer position and write the line to the output file
2220 ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
2221 OutputCubes[0]->write(*out);
2222 }
2223 else {
2224 funct(*out);
2225 }
2226
2227 p_progress->CheckStatus();
2228
2229 // Handle any line suffix bytes
2230 if (p_saveDataPost) {
2231 tempPost.push_back(new char[p_dataPostBytes]);
2232 memcpy(&tempPost[0], &in[band][p_dataPreBytes+p_ns*sizeofpixel],
2234 }
2235
2236 // Save off the prefix bytes vector
2237 if (p_saveDataPre) {
2238 p_dataPre.push_back(tempPre);
2239 tempPre.clear();
2240 }
2241
2242 // Save off the suffix bytes vector
2243 if (p_saveDataPost) {
2244 p_dataPost.push_back(tempPost);
2245 tempPost.clear();
2246 }
2247
2248 } // End band loop
2249
2250 } // End line loop
2251
2252 // Close the file and clean up
2253 delete JP2_decoder;
2254 delete [] in;
2255 }
2256
2257
2258#if 0
2259
2266 void ProcessImport::AddLabel(Isis::Pvl &label) {
2267
2268 label.Root();
2269
2270 Isis::Pvl *output = OutputCubes[0]->Label();
2271 output->Find("IsisCube");
2272 output->Insert(label);
2273 }
2274
2281 void ProcessImport::AddImportLabel(Isis::Pvl &importLab) {
2282
2283 importLab.Root();
2284
2285 Isis::Pvl *output = OutputCubes[0]->Label();
2286 output->Find("IsisCube");
2287 output->addObject("ImportLabel");
2288 output->Insert(importLab);
2289 }
2290#endif
2291
2292
2301 void ProcessImport::SetInputFile(const QString &file) {
2302 p_inFile = file;
2303 if (!Isis::FileName(file).fileExists()) {
2304 QString msg = "File [" + file + "] does not exist";
2305 throw IException(IException::User, msg, _FILEINFO_);
2306 }
2307 }
2308
2309
2319 if (p_inFile.size() <= 0) {
2320 QString msg = "No input file has been set";
2321 throw IException(IException::Programmer, msg, _FILEINFO_);
2322 }
2323 return p_inFile;
2324 }
2325
2326}
static UserInterface & GetUserInterface()
Returns the UserInterface object.
This algorithm is designed for applications that jump around between a couple of spots in the cube wi...
Buffer for containing a three dimensional section of an image.
Definition Brick.h:45
Buffer for reading and writing cube data.
Definition Buffer.h:53
Manipulate and parse attributes of output cube filenames.
bool propagateMinimumMaximum() const
Return true if the min/max are to be propagated from an input cube.
bool propagatePixelType() const
Return true if the pixel type is to be propagated from an input cube.
void setMinimum(double min)
Set the output cube attribute minimum.
void setMaximum(double max)
Set the output cube attribute maximum.
void setPixelType(PixelType type)
Set the pixel type to that given by the parameter.
IO Handler for Isis Cubes.
Definition Cube.h:168
float Float(void *buf)
Swaps a floating point value.
short int ShortInt(void *buf)
Swaps a short integer value.
int Int(void *buf)
Swaps a 4 byte integer value.
double Double(void *buf)
Swaps a double precision value.
uint32_t Uint32_t(void *buf)
Swaps a 32bit unsigned integer.
unsigned short int UnsignedShortInt(void *buf)
Swaps an unsigned short integer value.
JPEG2000 decoder class.
Definition JP2Decoder.h:83
void OpenFile()
Open the JPEG2000 file.
void Read(unsigned char **inbuf)
Read data from JP2 file containing 8-bit data.
Buffer manager, for moving through a cube in lines.
Definition LineManager.h:39
Process()
Constructs a Process Object.
Definition Process.cpp:29
Isis::Progress * p_progress
Pointer to a Progress object.
Definition Process.h:145
virtual Isis::Cube * SetOutputCube(const QString &parameter)
Allocates a user-specified output cube whose size matches the first input cube.
Definition Process.cpp:163
std::vector< Isis::Cube * > OutputCubes
A vector of pointers to allocated Cube objects.
Definition Process.h:191
void SetVAXConvert(const bool vax_convert)
Sets the VAX flag of the input cube.
virtual void StartProcess()
Process the input file and write it to the output.
char * p_fileHeader
The file header.
ProcessImport::Interleave p_organization
The format of the input file.
double p_lis_min
The pixel value which is the lower bound of LIS data.
bool p_saveDataPost
Flag indicating whether to save the data suffix or not.
bool p_saveDataTrailer
Flag indicating whether to save the data trailer or not.
Isis::PixelType p_pixelType
Pixel type of input data.
void SetPixelType(const Isis::PixelType type)
Sets the pixel type of the input file.
void SetFileHeaderBytes(const int bytes)
This method sets the number of bytes in the header of a file.
bool p_saveDataPre
Flag indicating whether to save the data prefix or not.
std::vector< char * > DataTrailer()
This method returns a pointer to the data trailer.
double p_null_max
The pixel value which is the upper bound of NULL data.
ProcessImport()
Constructs an Import object.
void SaveDataPrefix()
This method marks the data prefix to be saved.
void SaveDataTrailer()
This method marks the data block trailers to be saved.
int FileHeaderBytes() const
This method returns the number of file header bytes.
void SetOrganization(const ProcessImport::Interleave org)
Sets the organization of the input cube.
char * FileTrailer()
This method returns a pointer to the file trailer.
void SetDataPrefixBytes(const int bytes)
This method sets the number of bytes at the beginning of each data record of a file.
int DataTrailerBytes() const
This method returns the number of data trailer bytes.
std::vector< double > p_base
An array containing the core base for each band.
QString p_inFile
Input file name.
void SetLIS(const double lis_min, const double lis_max)
Sets the range that will be considered Isis::Null.
int p_ns
Number of samples.
void SetHRS(const double hrs_min, const double hrs_max)
Sets the range that will be considered Isis::Null.
int p_suffixData
The number of bytes past the file header bytes where the suffix data bands are stored.
void SetSpecialValues(const double null, const double lrs, const double lis, const double hrs, const double his)
Sets a mapping of input pixel values to output special pixel values.
std::vector< std::vector< char * > > p_dataPost
The data suffix.
void SetNull(const double null_min, const double null_max)
Sets the range that will be considered Isis::Null.
void SetInputFile(const QString &file)
Sets the name of the input file to be read in the import StartProcess method and verifies its existan...
void SetMultiplier(const double mult)
Sets the core multiplier of the input cube.
void SetDataHeaderBytes(const int bytes)
This method sets the number of bytes in the header of each datablock of a file.
std::vector< double > p_mult
An array containing the core multiplier for each band.
std::vector< char * > p_dataTrailer
The data trailer.
void ProcessBip(void funct(Isis::Buffer &out)=NULL)
Function to process files stored as Band Interleaved by Pixel.
double p_his_min
The pixel value which is the lower bound of HIS data.
double p_lis_max
The pixel value which is the upper bound of LIS data.
Isis::Cube * SetOutputCube(const QString &parameter)
Create the output file.
void SaveFileTrailer()
This method marks the file trailer to be saved.
bool p_saveDataHeader
Flag indicating whether to save the data header or not.
bool p_saveFileHeader
Flag indicating whether to save the file header or not.
int FileTrailerBytes() const
This method returns the number of file trailer bytes.
double p_lrs_max
The pixel value which is the upper bound of LRS data.
void CheckPixelRange(QString pixelName, double min, double max)
Checks the special pixel range of the given against all other special pixel value ranges,...
double p_hrs_max
The pixel value which is the upper bound of HRS data.
void ProcessJp2(void funct(Isis::Buffer &out)=NULL)
Function to process files containing compressed JPEG2000 data (which is always BSQ but is processed a...
int p_dataTrailerBytes
Number of bytes of non-image data after the image data of each data block, such as band trailer data ...
void SetLRS(const double lrs_min, const double lrs_max)
Sets the range that will be considered Isis::Null.
std::vector< std::vector< char * > > p_dataPre
The data prefix.
double p_lrs_min
The pixel value which is the lower bound of LRS data.
int DataPrefixBytes() const
This method returns the number of data prefix bytes.
double p_null_min
The pixel value which is the lower bound of NULL data.
double p_hrs_min
The pixel value which is the lower bound of HRS data.
int p_fileTrailerBytes
The number of bytes of non-image data at the end of a file.
Isis::PixelType p_suffixPixelType
The pixel type of the suffix data.
bool p_saveFileTrailer
Flag indicating whether to save the file trailer or not.
Interleave Organization() const
Gets the organization of the input cube.
double VAXConversion(void *ibuf)
Conversion routine which translates VAX_REAL to IEEE_REAL.
int p_nl
Number of lines.
void SetFileTrailerBytes(const int bytes)
This method sets the number of bytes in the trailer of a file.
void SetDataTrailerBytes(const int bytes)
This method sets the number of bytes in the trailer of each datablock of a file.
std::vector< std::vector< char * > > DataSuffix()
This method returns a pointer to the data suffix.
QString InputFile()
Sets the name of the input file to be read in the import StartProcess method and verifies its existan...
std::vector< char * > p_dataHeader
The data header.
int p_nb
Number of bands.
int DataSuffixBytes() const
This method returns the number of data duffix bytes.
void SaveDataHeader()
This method marks the data block headers to be saved.
void SetByteOrder(const Isis::ByteOrder order)
Sets the byte order of the input file.
void ProcessBil(void funct(Isis::Buffer &out)=NULL)
Function to process files stored as Band Interleaved by Line.
double p_his_max
The pixel value which is the upper bound of HIS data.
virtual ~ProcessImport()
Destroys the Import object.
std::vector< std::vector< char * > > DataPrefix()
This method returns a pointer to the data prefix.
void ProcessBsq(void funct(Isis::Buffer &out)=NULL)
Process the import data as a band sequential file.
void SetHIS(const double his_min, const double his_max)
Sets the range that will be considered Isis::Null.
Interleave
This enum includes how the document should be read: by BSQ, BIL, BIP, JP2, or InterleaveUndefined.
@ BIL
Band Interleaved By Line Format (i.e.
@ BIP
Band Interleaved By Pixel Format (i.e.
@ JP2
Jpeg 2000 Format (always band sequential).
@ BSQ
Band Sequential Format (i.e.
std::vector< char * > DataHeader()
This method returns a pointer to the data header.
void SetDataSuffixBytes(const int bytes)
This method sets the number of bytes at the end of each data record of a file.
char * FileHeader()
This method returns a pointer to the file header.
void SetBase(const double base)
Sets the core base of the input cube.
double TestPixel(const double pixel)
Tests the pixel.
Isis::ByteOrder p_byteOrder
Byte order of data.
void SetAttributes(CubeAttributeOutput &att)
Given a CubeAttributeOutput object, set min/max to propagate if propagating min/max attributes was re...
int p_dataPreBytes
Number of bytes of non-image data preceding each data record, such as line prefix data in a band sequ...
int p_dataPostBytes
Number of bytes of non-image data following each data record, such as line suffix data in a band sequ...
char * p_fileTrailer
The file trailer.
void SaveFileHeader()
This method marks the file header to be saved.
void SaveDataSuffix()
This method marks the data suffix to be saved.
int p_dataHeaderBytes
Number of bytes of non-image data after the file header and before the image data of each data block,...
void SetDimensions(const int ns, const int nl, const int nb)
Sets the physical size of the input cube.
int DataHeaderBytes() const
This method returns the number of data header bytes.
int p_fileHeaderBytes
The number of bytes of non-image data at the beginning of a file.
bool IsVAXSpecial(unsigned int *vax, VAXSpecialPixel pix)
Determines if the VAX encoded pixel value is special or not.
Command Line and Xml loader, validation, and access.
QString GetCubeName(const QString &paramName, QString extension="") const
Retrieves of a value for a parameter of type "cubename".
Definition IsisAml.cpp:722
Isis::CubeAttributeOutput & GetOutputAttribute(const QString &paramName)
Gets the attributes for an output cube.
Definition IsisAml.cpp:2102
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(const LinearAlgebra::Vector &vector, int precision)
A global function to format LinearAlgebra::Vector as a QString with the given precision.
Namespace for the standard library.