Isis 3.0 Programmer Reference
Back | Home
ProcessImport.cpp
Go to the documentation of this file.
1 
22 #include "ProcessImport.h"
23 
24 #include <float.h>
25 #include <iostream>
26 #include <QString>
27 #include <sstream>
28 
29 #include "Application.h"
30 #include "BoxcarCachingAlgorithm.h"
31 #include "Brick.h"
32 #include "Cube.h"
33 #include "CubeAttribute.h"
34 #include "IException.h"
35 #include "IString.h"
36 #include "JP2Decoder.h"
37 #include "LineManager.h"
38 #include "PixelType.h"
39 #include "Process.h"
40 #include "Pvl.h"
41 #include "PvlTokenizer.h"
42 #include "SpecialPixel.h"
43 
44 #define EXPONENT_MASK ((char) 0x7F)
45 
46 
47 using namespace std;
48 namespace Isis {
49 
51  ProcessImport::ProcessImport() : Isis::Process() {
52 
53  p_progress->SetText("Importing");
54 
55  // Initialize input file information
56  p_inFile = "";
57  p_pixelType = Isis::None;
58  p_suffixPixelType = Isis::None;
59  p_ns = p_nl = p_nb = 0;
60  p_byteOrder = Isis::NoByteOrder;
64  p_dataPreBytes = 0;
65  p_dataPostBytes = 0;
66  p_suffixData = 0;
68 
69  p_base.push_back(0.0);
70  p_mult.push_back(1.0);
71  // Make all special pixels invalid
72  SetNull(DBL_MAX, -DBL_MAX);
73  SetHRS(DBL_MAX, -DBL_MAX);
74  SetLRS(DBL_MAX, -DBL_MAX);
75  SetHIS(DBL_MAX, -DBL_MAX);
76  SetLIS(DBL_MAX, -DBL_MAX);
77 
78  p_saveFileHeader = false;
79  p_saveDataHeader = false;
80  p_saveDataTrailer = false;
81  p_saveDataPre = false;
82  p_saveDataPost = false;
83  p_saveFileTrailer = false;
84  p_vax_convert = false;
85 
86  p_fileHeader = NULL;
87  p_fileTrailer = NULL;
88  }
89 
90 
93  if (p_fileHeader) {
94  delete [] p_fileHeader;
95  p_fileHeader = NULL;
96  }
97 
98  for(unsigned int i = 0; i < p_dataHeader.size(); i++) {
99  delete [] p_dataHeader[i];
100  }
101 
102  for(unsigned int i = 0; i < p_dataTrailer.size(); i++) {
103  delete [] p_dataTrailer[i];
104  }
105 
106  vector<char *> temp;
107  for(unsigned int j = 0; j < p_dataPre.size(); j++) {
108  temp = p_dataPre[j];
109  for(unsigned int i = 0; i < temp.size(); i++) delete [] temp[i];
110  }
111 
112  for(unsigned int j = 0; j < p_dataPost.size(); j++) {
113  temp = p_dataPost[j];
114  for(unsigned int i = 0; i < temp.size(); i++) delete [] temp[i];
115  }
116 
117  if (p_fileTrailer) {
118  delete p_fileTrailer;
119  }
120  }
121 
122 
132  bool ProcessImport::IsVAXSpecial(unsigned int *vax, VAXSpecialPixel pix) {
133 
134  unsigned int VAX_NULL = 0xFFFFFFFF;
135  unsigned int VAX_MIN = 0xFFEFFFFF;
136  unsigned int VAX_LRS = 0xFFFEFFFF;
137  unsigned int VAX_LIS = 0xFFFDFFFF;
138  unsigned int VAX_HIS = 0xFFFCFFFF;
139  unsigned int VAX_HRS = 0xFFFBFFFF;
140  int n;
141 
142  unsigned int x;
143  memcpy(&x, vax, sizeof(unsigned int));
144 
145  switch(pix) {
146  case VAX_NULL4:
147  n = memcmp(&VAX_NULL, &x, sizeof(unsigned int));
148  if (n == 0) return true;
149  break;
150  case VAX_LRS4:
151  n = memcmp(&VAX_LRS, &x, sizeof(unsigned int));
152  if (n == 0) return true;
153  break;
154  case VAX_LIS4:
155  n = memcmp(&VAX_LIS, &x, sizeof(unsigned int));
156  if (n == 0) return true;
157  break;
158  case VAX_HIS4:
159  n = memcmp(&VAX_HIS, &x, sizeof(unsigned int));
160  if (n == 0) return true;
161  break;
162  case VAX_HRS4:
163  n = memcmp(&VAX_HRS, &x, sizeof(unsigned int));
164  if (n == 0) return true;
165  break;
166  case VAX_MIN4:
167  n = memcmp(&VAX_MIN, &x, sizeof(unsigned int));
168  if (n == 0) return true;
169  break;
170  default:
171  return false;
172  }
173 
174  return false;
175 
176  }
177 
178 
185  double ProcessImport::VAXConversion(void *ibuf) {
186 
187  float result;
188  double dresult;
189  bool swap_bytes = false;
190  bool swap_words = true;
191  int exp_adjust = -1;
192  int exp_mask = 0;
193  int exp_word = 1;
194  int exp_byte;
195  Isis::ByteOrder in_order = p_byteOrder;
196  Isis::ByteOrder out_order;
197 
198  unsigned int *oli, *ili; //4-byte buffer io ptrs
199  unsigned short *osi; //2-byte buffer io ptrs
200  char *oci; //1-byte buffer io ptrs
201 
202  if ( Isis::IsLsb() ) {
203  exp_byte = 1;
204  out_order = Isis::Lsb;
205  }
206  else {
207  exp_byte = 0;
208  out_order = Isis::Msb;
209  }
210 
211  if (in_order != out_order) {
212  swap_bytes =true;
213  }
214 
215  oli = (unsigned int * ) ibuf;
216  ili = (unsigned int * ) ibuf;
217 
218  if (IsVAXSpecial(oli, ProcessImport::VAX_NULL4) ) {
219  return Isis::NULL8;
220  }
221 
222  if (IsVAXSpecial(oli, ProcessImport::VAX_LIS4) ) {
223  return Isis::LOW_INSTR_SAT8;
224  }
225 
226  if (IsVAXSpecial(oli, ProcessImport::VAX_LRS4) ) {
227  return Isis::LOW_REPR_SAT8;
228  }
229 
230  if (IsVAXSpecial(oli, ProcessImport::VAX_HIS4) ) {
231  return Isis::HIGH_INSTR_SAT8;
232  }
233 
234  if (IsVAXSpecial(oli, ProcessImport::VAX_HRS4) ) {
235  return Isis::HIGH_REPR_SAT8;
236  }
237 
238  if (IsVAXSpecial(oli, ProcessImport::VAX_MIN4) ) {
239  return Isis::VALID_MIN8;
240  }
241 
242  //test for word swapping
243  if (swap_words) {
244  *oli = (*ili <<16) | (*ili >> 16);
245  }
246 
247  osi = (unsigned short* ) oli;
248 
249  //test for byte swapping
250 
251  if (swap_bytes) {
252  osi[0] = (osi[0] >> 8 ) | (osi[0] << 8);
253  osi[1] = (osi[1] >> 8 ) | (osi[1] << 8);
254  }
255 
256  //Isolate the exponent and do the conversion
257  oci = (char *) &osi[exp_word];
258 
259  if ( (oci[exp_byte] & EXPONENT_MASK) != exp_mask) {
260  oci[exp_byte] += exp_adjust;
261  }
262 
263  result = *(float *)oli;
264  dresult = static_cast<double>(result);
265  return dresult;
266 
267  }
268 
269 
279 
280  if ((type == Isis::Double) || (type == Isis::Real) || (type == Isis::SignedWord) ||
281  (type == Isis::UnsignedWord) || (type == Isis::UnsignedByte) ||
282  (type == Isis::SignedInteger)) {
283  p_pixelType = type;
284  }
285  else {
286  QString msg = "Unsupported pixel type [" +
287  Isis::PixelTypeName(type) + "]";
289  }
290  }
291 
292  void ProcessImport::SetSuffixPixelType(const Isis::PixelType type) {
293 
294  if ((type == Isis::Double) || (type == Isis::Real) || (type == Isis::SignedWord) ||
295  (type == Isis::UnsignedWord) || (type == Isis::UnsignedByte)) {
296  p_suffixPixelType = type;
297  }
298  else {
299  QString msg = "Unsupported pixel type [" +
300  Isis::PixelTypeName(type) + "]";
301  throw IException(IException::Programmer, msg, _FILEINFO_);
302  }
303  }
304 
305 
318  void ProcessImport::SetDimensions(const int ns, const int nl, const int nb) {
319  if (ns > 0 && nl > 0 && nb > 0) {
320  p_ns = ns;
321  p_nl = nl;
322  p_nb = nb;
323  }
324  else {
325  QString msg = "Illegal dimension [" + toString(ns) + ", " +
326  toString(nl) + ", " + toString(nb) + "]";
328  }
329  }
330 
331 
339  p_byteOrder = order;
340  }
341 
342 
363  void ProcessImport::SetFileHeaderBytes(const int bytes) {
364  if (bytes >= 0) {
365  p_fileHeaderBytes = bytes;
366  }
367  else {
368  QString msg = "Illegal file header size [" + toString(bytes) + "]";
370  }
371  }
372 
373 
393  void ProcessImport::SetFileTrailerBytes(const int bytes) {
394  if (bytes >= 0) {
395  p_fileTrailerBytes = bytes;
396  }
397  else {
398  QString msg = "Illegal file trailer size [" + toString(bytes) + "]";
400  }
401  }
402 
403 
422  void ProcessImport::SetDataHeaderBytes(const int bytes) {
423  if (bytes >= 0) {
424  p_dataHeaderBytes = bytes;
425  }
426  else {
427  QString msg = "Illegal data header size [" + toString(bytes) + "]";
429  }
430  }
431 
432 
433  void ProcessImport::SetSuffixOffset(int samples, int lines, int coreBands, int itemBytes) {
434  p_suffixData = samples*lines*coreBands*itemBytes;
435  }
436 
437 
457  void ProcessImport::SetDataTrailerBytes(const int bytes) {
458 
459  if (bytes >= 0) {
460  p_dataTrailerBytes = bytes;
461  }
462  else {
463  QString msg = "Illegal data trailer size [" + toString(bytes) + "]";
465  }
466  }
467 
468 
487  void ProcessImport::SetDataPrefixBytes(const int bytes) {
488 
489  if (bytes >= 0) {
490  p_dataPreBytes = bytes;
491  }
492  else {
493  QString msg = "Illegal data prefix size [" + toString(bytes) + "]";
495  }
496  }
497 
498 
517  void ProcessImport::SetDataSuffixBytes(const int bytes) {
518 
519  if (bytes >= 0) {
520  p_dataPostBytes = bytes;
521  }
522  else {
523  QString msg = "Illegal data suffix size [" + toString(bytes) + "]";
525  }
526  }
527 
528 
547  if (p_fileHeaderBytes == 0) {
548  QString msg = "File header bytes equals 0. There is nothing to save. "
549  "Use SetFileHeaderBytes() first.";
551  }
552  p_saveFileHeader = true;
553  }
554 
555 
574  if (p_fileTrailerBytes == 0) {
575  QString msg = "File trailer bytes equals 0. There is nothing to save. "
576  "Use SetFileTrailerBytes() first.";
578  _FILEINFO_);
579  }
580  p_saveFileTrailer = true;
581  }
582 
583 
603  if (p_dataHeaderBytes == 0) {
604  QString msg = "Data header bytes equals 0. There is nothing to save. "
605  "Use SetDataHeaderBytes() first.";
607  _FILEINFO_);
608  }
609  p_saveDataHeader = true;
610  }
611 
612 
632  if (p_dataTrailerBytes == 0) {
633  QString msg = "Data trailer bytes equals 0. There is nothing to save. "
634  "Use SetDataTrailerBytes() first.";
636  _FILEINFO_);
637  }
638  p_saveDataTrailer = true;
639  }
640 
641 
660  if (p_dataPreBytes == 0) {
661  QString msg = "Data prefix bytes equals 0. There is nothing to save. "
662  "Use SetDataPrefixBytes() first.";
664  _FILEINFO_);
665  }
666  p_saveDataPre = true;
667  }
668 
669 
689  if (p_dataPostBytes == 0) {
690  QString msg = "Data suffix bytes equals 0. There is nothing to save. "
691  "Use SetDataSuffixBytes() first.";
693  _FILEINFO_);
694  }
695  p_saveDataPost = true;
696  }
697 
698 
703 
704  return p_fileHeaderBytes;
705  }
706 
707 
712  return p_fileTrailerBytes;
713  }
714 
715 
720  return p_dataHeaderBytes;
721  }
722 
723 
728  return p_fileTrailerBytes;
729  }
730 
731 
736  return p_dataPreBytes;
737  }
738 
739 
744 
745  return p_dataPostBytes;
746  }
747 
748 
768  if (p_saveFileHeader) {
769  return p_fileHeader;
770  }
771  QString msg = "File header was not saved. Use SaveFileHeader().";
773  }
774 
775 
794  if (p_saveFileTrailer) {
795  return p_fileTrailer;
796  }
797  QString msg = "File trailer was not saved. Use SaveFileTrailer()";
799  }
800 
801 
821  std::vector<char *> ProcessImport::DataHeader() {
822  if (p_saveDataHeader) {
823  return p_dataHeader;
824  }
825  QString msg = "Data header was not saved. Use SaveDataHeader()";
827  }
828 
829 
849  std::vector<char *> ProcessImport::DataTrailer() {
850  if (p_saveDataTrailer) {
851  return p_dataTrailer;
852  }
853  QString msg = "Data trailer was not saved. Use SaveDataTrailer()";
855  }
856 
857 
877  std::vector<std::vector<char *> > ProcessImport::DataPrefix() {
878  if (p_saveDataPre) {
879  return p_dataPre;
880  }
881  QString msg = "Data prefix was not saved. Use SaveDataPrefix()";
883  }
884 
885 
905  std::vector<std::vector<char *> > ProcessImport::DataSuffix() {
906  if (p_saveDataPost) {
907  return p_dataPost;
908  }
909  QString msg = "Data suffix was not saved. Use SaveDataSuffix()";
911  }
912 
913 
922  p_organization = org;
923  }
924 
925 
933  void ProcessImport::SetVAXConvert(const bool vax_convert) {
934  p_vax_convert = vax_convert;
935  }
936 
937 
943  return p_organization;
944  }
945 
946 
952  void ProcessImport::SetBase(const double base) {
953  p_base.clear();
954  p_base.push_back(base);
955  }
956 
957 
963  void ProcessImport::SetBase(const std::vector<double> base) {
964  p_base = base;
965  }
966 
967 
973  void ProcessImport::SetMultiplier(const double mult) {
974  p_mult.clear();
975  p_mult.push_back(mult);
976  }
977 
978 
984  void ProcessImport::SetMultiplier(const std::vector<double> mult) {
985  p_mult = mult;
986  }
987 
988 
1007  void ProcessImport::SetSpecialValues(const double null, const double lrs,
1008  const double lis, const double hrs,
1009  const double his) {
1010  SetNull(null, null);
1011  SetLRS(lrs, lrs);
1012  SetLIS(lis, lis);
1013  SetHRS(hrs, hrs);
1014  SetHIS(his, his);
1015  }
1016 
1017 
1028  void ProcessImport::SetNull(const double null_min, const double null_max) {
1029  CheckPixelRange("Null", null_min, null_max);
1030  p_null_min = null_min;
1031  p_null_max = null_max;
1032  }
1033 
1034 
1045  void ProcessImport::SetLRS(const double lrs_min, const double lrs_max) {
1046  CheckPixelRange("LRS", lrs_min, lrs_max);
1047  p_lrs_min = lrs_min;
1048  p_lrs_max = lrs_max;
1049  }
1050 
1051 
1062  void ProcessImport::SetLIS(const double lis_min, const double lis_max) {
1063  CheckPixelRange("LIS", lis_min, lis_max);
1064  p_lis_min = lis_min;
1065  p_lis_max = lis_max;
1066  }
1067 
1068 
1079  void ProcessImport::SetHRS(const double hrs_min, const double hrs_max) {
1080  CheckPixelRange("HRS", hrs_min, hrs_max);
1081  p_hrs_min = hrs_min;
1082  p_hrs_max = hrs_max;
1083  }
1084 
1085 
1096  void ProcessImport::SetHIS(const double his_min, const double his_max) {
1097  CheckPixelRange("HIS", his_min, his_max);
1098  p_his_min = his_min;
1099  p_his_max = his_max;
1100  }
1101 
1102 
1112  void ProcessImport::CheckPixelRange(QString pixelName, double pixelMin,
1113  double pixelMax) {
1114  if (pixelMin == DBL_MAX || pixelMax == -DBL_MAX) return;
1115 
1116  if (p_null_min != DBL_MAX && p_null_max != -DBL_MAX && ( //-null has been set
1117  (pixelMin > p_null_min && pixelMin < p_null_max) || // --min crossing
1118  (pixelMax > p_null_min && pixelMax < p_null_max) || // --max crossing
1119  (pixelMin < p_null_min && pixelMax > p_null_max))) { // --straddling
1120  // values
1121  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1122  "," + toString(pixelMax) + "] overlaps the NULL range [" +
1123  toString(p_null_min) + "," + toString(p_null_max) + "]";
1124  throw IException(IException::User, msg, _FILEINFO_);
1125  }
1126 
1127  if (p_lrs_min != DBL_MAX && p_lrs_max != -DBL_MAX && (
1128  (pixelMin > p_lrs_min && pixelMin < p_lrs_max) ||
1129  (pixelMax > p_lrs_min && pixelMax < p_lrs_max) ||
1130  (pixelMin < p_lrs_min && pixelMax > p_lrs_max))) {
1131  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1132  "," + toString(pixelMax) + "] overlaps the LRS range [" +
1133  toString(p_lrs_min) + "," + toString(p_lrs_max) + "]";
1134  throw IException(IException::User, msg, _FILEINFO_);
1135  }
1136 
1137  if (p_lis_min != DBL_MAX && p_lis_max != -DBL_MAX && (
1138  (pixelMin > p_lis_min && pixelMin < p_lis_max) ||
1139  (pixelMax > p_lis_min && pixelMax < p_lis_max) ||
1140  (pixelMin < p_lis_min && pixelMax > p_lis_max))) {
1141  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1142  "," + toString(pixelMax) + "] overlaps the LIS range [" +
1143  toString(p_lis_min) + "," + toString(p_lis_max) + "]";
1144  throw IException(IException::User, msg, _FILEINFO_);
1145  }
1146 
1147  if (p_hrs_min != DBL_MAX && p_hrs_max != -DBL_MAX && (
1148  (pixelMin > p_hrs_min && pixelMin < p_hrs_max) ||
1149  (pixelMax > p_hrs_min && pixelMax < p_hrs_max) ||
1150  (pixelMin < p_hrs_min && pixelMax > p_hrs_max))) {
1151  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1152  "," + toString(pixelMax) + "] overlaps the HRS range [" +
1153  toString(p_hrs_min) + "," + toString(p_hrs_max) + "]";
1154  throw IException(IException::User, msg, _FILEINFO_);
1155  }
1156 
1157  if (p_his_min != DBL_MAX && p_his_max != -DBL_MAX && (
1158  (pixelMin > p_his_min && pixelMin < p_his_max) ||
1159  (pixelMax > p_his_min && pixelMax < p_his_max) ||
1160  (pixelMin < p_his_min && pixelMax > p_his_max))) {
1161  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
1162  "," + toString(pixelMax) + "] overlaps the HIS range [" +
1163  toString(p_his_min) + "," + toString(p_his_max) + "]";
1164  throw IException(IException::User, msg, _FILEINFO_);
1165  }
1166 
1167  }
1168 
1169 
1181  double ProcessImport::TestPixel(const double pixel) {
1182  if (pixel <= p_null_max && pixel >= p_null_min) {
1183  return Isis::NULL8;
1184  }
1185  else if (pixel <= p_hrs_max && pixel >= p_hrs_min) {
1186  return Isis::HIGH_REPR_SAT8;
1187  }
1188  else if (pixel <= p_lrs_max && pixel >= p_lrs_min) {
1189  return Isis::LOW_REPR_SAT8;
1190  }
1191  else if (pixel <= p_his_max && pixel >= p_his_min) {
1192  return Isis::HIGH_INSTR_SAT8;
1193  }
1194  else if (pixel <= p_lis_max && pixel >= p_lis_min) {
1195  return Isis::LOW_INSTR_SAT8;
1196  }
1197  else {
1198  return pixel;
1199  }
1200  }
1201 
1202 
1213  Isis::Cube *ProcessImport::SetOutputCube(const QString &parameter) {
1214  CubeAttributeOutput &att =
1216 
1217  if (att.propagateMinimumMaximum()) {
1218  double min, max;
1219  if ((p_pixelType == Isis::Double) ||
1220  (p_pixelType == Isis::Real) ||
1221  (p_base.size() > 1) || (p_mult.size() > 1)) {
1222  min = Isis::VALID_MIN4;
1223  max = Isis::VALID_MAX4;
1224  }
1225  else if (p_pixelType == Isis::SignedInteger) {
1226  min = Isis::IVALID_MIN4;
1227  max = Isis::IVALID_MAX4;
1228  }
1229  else if (p_pixelType == Isis::SignedWord) {
1230  min = Isis::VALID_MIN2 * p_mult[0] + p_base[0];
1231  max = Isis::VALID_MAX2 * p_mult[0] + p_base[0];
1232  }
1233  else if (p_pixelType == Isis::UnsignedWord) {
1234  min = Isis::VALID_MINU2 * p_mult[0] + p_base[0];
1235  max = Isis::VALID_MAXU2 * p_mult[0] + p_base[0];
1236  }
1237  else if (p_pixelType == Isis::UnsignedByte) {
1238  min = Isis::VALID_MIN1 * p_mult[0] + p_base[0];
1239  max = Isis::VALID_MAX1 * p_mult[0] + p_base[0];
1240  }
1241  else {
1242  QString msg = "Unsupported pixel type [" +
1245  }
1246  att.setMinimum(min);
1247  att.setMaximum(max);
1248  }
1249 
1250  if (att.propagatePixelType()) {
1251  if ((p_base.size() > 1) || (p_mult.size() > 1)) {
1252  att.setPixelType(Isis::Real);
1253  }
1254  else if (p_pixelType == Isis::Double || p_pixelType == Isis::SignedInteger) {
1255  att.setPixelType(Isis::Real);
1256  }
1257  else {
1259  }
1260  }
1261 
1262  return Process::SetOutputCube(Application::GetUserInterface().GetFileName(parameter), att, p_ns, p_nl, p_nb);
1263  }
1264 
1265 
1281  return Isis::Process::SetOutputCube(fname, att, p_ns, p_nl, p_nb);
1282  }
1283 
1284 
1288  ProcessJp2();
1289  }
1290  else if (p_organization == ProcessImport::BSQ) {
1291  ProcessBsq();
1292  }
1293  else if (p_organization == ProcessImport::BIL) {
1294  ProcessBil();
1295  }
1296  else if (p_organization == ProcessImport::BIP) {
1297  ProcessBip();
1298  }
1299  else {
1300  QString msg = "File [" + p_inFile
1301  + "] is not in a supported organization.";
1303  }
1304  }
1305 
1306 
1318  ProcessJp2(funct);
1319  }
1320  else if (p_organization == ProcessImport::BSQ) {
1321  ProcessBsq(funct);
1322  }
1323  else if (p_organization == ProcessImport::BIL) {
1324  ProcessBil(funct);
1325  }
1326  else if (p_organization == ProcessImport::BIP) {
1327  ProcessBip(funct);
1328  }
1329  else {
1330  QString msg = "File [" + p_inFile + "] is not in a supported organization.";
1332  }
1333  }
1334 
1335 
1347  void ProcessImport::ProcessBsq(void funct(Isis::Buffer &out)) {
1348  // Figure out the number of bytes to read for a single line
1349  int readBytes = Isis::SizeOf(p_pixelType);
1350  readBytes = readBytes * p_ns;
1351  char *in = new char [readBytes];
1352 
1353  // Set up an Isis::EndianSwapper object
1354  QString tok(Isis::ByteOrderName(p_byteOrder));
1355  tok = tok.toUpper();
1356  Isis::EndianSwapper swapper(tok);
1357 
1358  ifstream fin;
1359  // Open input file
1360  Isis::FileName inFile(p_inFile);
1361  QString inFileName(inFile.expanded());
1362  fin.open(inFileName.toLatin1().data(), ios::in | ios::binary);
1363  if (!fin.is_open()) {
1364  QString msg = "Cannot open input file [" + p_inFile + "]";
1365  throw IException(IException::Io, msg, _FILEINFO_);
1366  }
1367 
1368  // Handle the file header
1369  streampos pos = fin.tellg();
1370  if (p_saveFileHeader) {
1371  p_fileHeader = new char[p_fileHeaderBytes];
1372  fin.read(p_fileHeader, p_fileHeaderBytes);
1373  fin.seekg(p_suffixData+p_fileHeaderBytes, ios_base::beg);
1374  }
1375  else {
1376  fin.seekg(p_fileHeaderBytes+p_suffixData, ios_base::beg);
1377 
1378  }
1379 
1380  // Check the last io
1381  if (!fin.good()) {
1382  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1383  toString((int)pos) + "]. Byte count [" +
1384  toString(p_fileHeaderBytes) + "]" ;
1385  throw IException(IException::Io, msg, _FILEINFO_);
1386  }
1387 
1388  // Construct a line buffer manager
1389  Isis::Buffer *out = NULL;
1390 
1391  if (funct != NULL) {
1392  out = new Isis::Brick(p_ns, 1, 1, p_pixelType);
1393  }
1394  else {
1395  out = new Isis::LineManager(*OutputCubes[0]);
1396  }
1397 
1398  // Loop once for each band in the image
1401 
1402  // Loop for each band
1403  for(int band = 0; band < p_nb; band++) {
1404  // Set the base multiplier
1405  double base, mult;
1406  if (p_base.size() > 1) {
1407  base = p_base[band];
1408  mult = p_mult[band];
1409  }
1410  else {
1411  base = p_base[0];
1412  mult = p_mult[0];
1413  }
1414 
1415  // Handle any data headers (e.g., the data at the beginning of each band)
1416  pos = fin.tellg();
1417  if (p_saveDataHeader) {
1418  p_dataHeader.push_back(new char[p_dataHeaderBytes]);
1419  fin.read(p_dataHeader.back(), p_dataHeaderBytes);
1420  }
1421  else {
1422  fin.seekg(p_dataHeaderBytes, ios_base::cur);
1423  }
1424 
1425  // Check the last io
1426  if (!fin.good()) {
1427  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1428  toString((int)pos) + "]. Byte count [" +
1429  toString(p_dataHeaderBytes) + "]" ;
1430  throw IException(IException::Io, msg, _FILEINFO_);
1431  }
1432 
1433  // Space for storing prefix and suffix data pointers
1434  vector<char *> tempPre, tempPost;
1435 
1436  // Loop for each line in a band
1437  for(int line = 0; line < p_nl; line++) {
1438 
1439  // Handle any line prefix bytes
1440  pos = fin.tellg();
1441  if (p_saveDataPre) {
1442  tempPre.push_back(new char[p_dataPreBytes]);
1443  fin.read(tempPre.back(), p_dataPreBytes);
1444  }
1445  else {
1446  fin.seekg(p_dataPreBytes, ios_base::cur);
1447  }
1448 
1449  // Check the last io
1450  if (!fin.good()) {
1451  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1452  toString((int)pos) + "]. Byte count [" +
1453  toString(p_dataPreBytes) + "]" ;
1454  throw IException(IException::Io, msg, _FILEINFO_);
1455  }
1456 
1457  // Get a line of data from the input file
1458  pos = fin.tellg();
1459  fin.read(in, readBytes);
1460  if (!fin.good()) {
1461  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1462  toString((int)pos) + "]. Byte count [" +
1463  toString(readBytes) + "]" ;
1464  throw IException(IException::Io, msg, _FILEINFO_);
1465  }
1466 
1467  // Swap the bytes if necessary and convert any out of bounds pixels
1468  // to special pixels
1469  for(int samp = 0; samp < p_ns; samp++) {
1470  switch(p_pixelType) {
1471  case Isis::UnsignedByte:
1472  (*out)[samp] = (double)((unsigned char *)in)[samp];
1473  break;
1474  case Isis::UnsignedWord:
1475  (*out)[samp] =
1476  (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1477  break;
1478  case Isis::SignedWord:
1479  (*out)[samp] = (double)swapper.ShortInt((short int *)in+samp);
1480  break;
1481  case Isis::SignedInteger:
1482  (*out)[samp] = (double)swapper.Int((int *)in+samp);
1483  break;
1484  case Isis::Real:
1485  if(p_vax_convert) {
1486  (*out)[samp]= VAXConversion( (float *)in+samp );
1487  }
1488  else {
1489  (*out)[samp] = (double)swapper.Float((float *)in+samp);
1490  }
1491  break;
1492  case Isis::Double:
1493  //cout << "Double" << endl;
1494  (*out)[samp] = (double)swapper.Double((double *)in+samp);
1495  break;
1496  default:
1497  break;
1498  }
1499 
1500  (*out)[samp] = TestPixel((*out)[samp]);
1501 
1502  if (Isis::IsValidPixel((*out)[samp])) {
1503  (*out)[samp] = mult * ((*out)[samp]) + base;
1504  }
1505  } // End sample loop
1506 
1507  if (funct == NULL) {
1508  // Set the buffer position and write the line to the output file
1509  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1510  OutputCubes[0]->write(*out);
1511  }
1512  else {
1513  ((Isis::Brick *)out)->SetBaseSample(1);
1514  ((Isis::Brick *)out)->SetBaseLine(line + 1);
1515  ((Isis::Brick *)out)->SetBaseBand(band + 1);
1516  funct(*out);
1517  }
1518 
1520 
1521  // Handle any line suffix bytes
1522  pos = fin.tellg();
1523  if (p_saveDataPost) {
1524  tempPost.push_back(new char[p_dataPostBytes]);
1525  fin.read(tempPost.back(), p_dataPostBytes);
1526  }
1527  else {
1528  fin.seekg(p_dataPostBytes, ios_base::cur);
1529  }
1530 
1531  // Check the last io
1532  if (!fin.good()) {
1533  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1534  toString((int)pos) + "]. Byte count [" +
1535  toString(p_dataPreBytes) + "]" ;
1536  throw IException(IException::Io, msg, _FILEINFO_);
1537  }
1538  } // End line loop
1539 
1540  // Save off the prefix bytes vector
1541  if (p_saveDataPre) {
1542  p_dataPre.push_back(tempPre);
1543  tempPre.clear();
1544  }
1545 
1546  // Save off the suffix bytes vector
1547  if (p_saveDataPost) {
1548  p_dataPost.push_back(tempPost);
1549  tempPost.clear();
1550  }
1551 
1552  // Handle the band trailer
1553  pos = fin.tellg();
1554  if (p_saveDataTrailer) {
1555  p_dataTrailer.push_back(new char[p_dataTrailerBytes]);
1556  fin.read(p_dataTrailer.back(), p_dataTrailerBytes);
1557  }
1558  else {
1559  fin.seekg(p_dataTrailerBytes, ios_base::cur);
1560  }
1561 
1562  // Check the last io
1563  if (!fin.good()) {
1564  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1565  toString((int)pos) + "]. Byte count [" +
1566  toString(p_fileHeaderBytes) + "]" ;
1567  throw IException(IException::Io, msg, _FILEINFO_);
1568  }
1569 
1570  } // End band loop
1571 
1572  // Handle the file trailer
1573  pos = fin.tellg();
1574  if (p_saveFileTrailer) {
1575  fin.seekg(0, ios_base::end);
1576  streampos e = fin.tellg();
1577  p_fileTrailerBytes = (int)(e - pos + (streampos)1);
1578  p_fileTrailer = new char[p_fileTrailerBytes];
1579  fin.seekg(pos);
1580  fin.read(p_fileTrailer, p_fileTrailerBytes);
1581 
1582  // Check the io
1583  if (!fin.good()) {
1584  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1585  toString((int)pos) + "]. Byte count [" +
1586  toString(p_fileTrailerBytes) + "]" ;
1587  throw IException(IException::Io, msg, _FILEINFO_);
1588  }
1589 
1590  }
1591 
1592  // Close the file and clean up
1593  fin.close();
1594  delete [] in;
1595  }
1596 
1597 
1608  void ProcessImport::ProcessBil(void funct(Isis::Buffer &someBuf)) {
1609 
1610  // Figure out the number of bytes to read for a single line
1611  int readBytes = Isis::SizeOf(p_pixelType);
1612  readBytes = readBytes * p_ns;
1613  char *in = new char [readBytes];
1614 
1615  // Set up an Isis::EndianSwapper object
1616  QString tok(Isis::ByteOrderName(p_byteOrder));
1617  tok = tok.toUpper();
1618  Isis::EndianSwapper swapper(tok);
1619 
1620  ifstream fin;
1621  // Open input file
1622  Isis::FileName inFile(p_inFile);
1623  QString inFileName(inFile.expanded());
1624  fin.open(inFileName.toLatin1().data(), ios::in | ios::binary);
1625  if (!fin.is_open()) {
1626  QString msg = "Cannot open input file [" + p_inFile + "]";
1627  throw IException(IException::Io, msg, _FILEINFO_);
1628  }
1629 
1630  // Handle the file header
1631  streampos pos = fin.tellg();
1632 
1633  if (p_saveFileHeader) {
1634  p_fileHeader = new char[p_fileHeaderBytes];
1635  fin.read(p_fileHeader, p_fileHeaderBytes);
1636  fin.seekg(p_suffixData+p_fileHeaderBytes, ios_base::beg);
1637  }
1638  else {
1639  fin.seekg(p_fileHeaderBytes+p_suffixData, ios_base::beg);
1640  }
1641 
1642  // Check the last io
1643  if (!fin.good()) {
1644  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1645  toString((int)pos) + "]. Byte count [" +
1646  toString(p_fileHeaderBytes) + "]" ;
1647  throw IException(IException::Io, msg, _FILEINFO_);
1648  }
1649 
1650  // Construct a line buffer manager
1651  Isis::Buffer *out = NULL;
1652 
1653  if (funct != NULL) {
1654  out = new Isis::Brick(p_ns, p_nl, p_nb, p_ns, 1, 1, p_pixelType, true);
1655  ((Isis::Brick *)out)->setpos(0);
1656  }
1657  else {
1658  OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
1659  out = new Isis::LineManager(*OutputCubes[0]);
1660  }
1661 
1662  // Loop once for each line in the image
1665 
1666  // Loop for each line
1667  for(int line = 0; line < p_nl; line++) {
1668  // Loop for each band
1669  for(int band = 0; band < p_nb; band++) {
1670  // Set the base multiplier
1671  double base, mult;
1672  if (p_base.size() > 1) {
1673  base = p_base[band];
1674  mult = p_mult[band];
1675  }
1676  else {
1677  base = p_base[0];
1678  mult = p_mult[0];
1679  }
1680 
1681  // Check the last io
1682  if (!fin.good()) {
1683  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1684  toString((int)pos) + "]. Byte count [" +
1685  toString(p_dataHeaderBytes) + "]" ;
1686  throw IException(IException::Io, msg, _FILEINFO_);
1687  }
1688 
1689  // Space for storing prefix and suffix data pointers
1690  vector<char *> tempPre, tempPost;
1691 
1692 
1693  // Handle any line prefix bytes
1694  pos = fin.tellg();
1695  if (p_saveDataPre) {
1696  tempPre.push_back(new char[p_dataPreBytes]);
1697  fin.read(tempPre.back(), p_dataPreBytes);
1698  }
1699  else {
1700  fin.seekg(p_dataPreBytes, ios_base::cur);
1701  }
1702 
1703  // Check the last io
1704  if (!fin.good()) {
1705  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1706  toString((int)pos) + "]. Byte count [" +
1707  toString(p_dataPreBytes) + "]" ;
1708  throw IException(IException::Io, msg, _FILEINFO_);
1709  }
1710 
1711 
1712  // Get a line of data from the input file
1713  pos = fin.tellg();
1714  fin.read(in, readBytes);
1715  if (!fin.good()) {
1716  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1717  toString((int)pos) + "]. Byte count [" +
1718  toString(readBytes) + "]" ;
1719  throw IException(IException::Io, msg, _FILEINFO_);
1720  }
1721 
1722  // Swap the bytes if necessary and convert any out of bounds pixels
1723  // to special pixels
1724  for(int samp = 0; samp < p_ns; samp++) {
1725  switch(p_pixelType) {
1726  case Isis::UnsignedByte:
1727  (*out)[samp] = (double)((unsigned char *)in)[samp];
1728  break;
1729  case Isis::UnsignedWord:
1730  (*out)[samp] =
1731  (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1732  break;
1733  case Isis::SignedWord:
1734  (*out)[samp] = (double)swapper.ShortInt((short int *)in+samp);
1735  break;
1736  case Isis::SignedInteger:
1737  (*out)[samp] = (double)swapper.Int((int *)in+samp);
1738  break;
1739  case Isis::Real:
1740  if(p_vax_convert) {
1741  (*out)[samp]= VAXConversion( (float *)in+samp );
1742  }
1743  else {
1744  (*out)[samp] = (double)swapper.Float((float *)in+samp);
1745  }
1746  break;
1747  case Isis::Double:
1748  (*out)[samp] = (double)swapper.Double((double *)in+samp);
1749  break;
1750  default:
1751  break;
1752  }
1753 
1754  // Sets out to isis special pixel or leaves it if valid
1755  (*out)[samp] = TestPixel((*out)[samp]);
1756 
1757  if (Isis::IsValidPixel((*out)[samp])) {
1758  (*out)[samp] = mult * ((*out)[samp]) + base;
1759  }
1760  } // End sample loop
1761 
1762  if (funct == NULL) {
1763  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1764  OutputCubes[0]->write(*out);
1765  }
1766  else {
1767  funct(*out);
1768  (*((Isis::Brick *)out))++;
1769  }
1770 
1772 
1773  // Handle any line suffix bytes
1774  pos = fin.tellg();
1775  if (p_saveDataPost) {
1776  tempPost.push_back(new char[p_dataPostBytes]);
1777  fin.read(tempPost.back(), p_dataPostBytes);
1778  }
1779  else {
1780  fin.seekg(p_dataPostBytes, ios_base::cur);
1781  }
1782 
1783  // Check the last io
1784  if (!fin.good()) {
1785  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1786  toString((int)pos) + "]. Byte count [" +
1787  toString(p_dataPreBytes) + "]" ;
1788  throw IException(IException::Io, msg, _FILEINFO_);
1789  }
1790 
1791  // Save off the prefix bytes vector
1792  if (p_saveDataPre) {
1793  p_dataPre.push_back(tempPre);
1794  tempPre.clear();
1795  }
1796 
1797  // Save off the suffix bytes vector
1798  if (p_saveDataPost) {
1799  p_dataPost.push_back(tempPost);
1800  tempPost.clear();
1801  }
1802 
1803  // Check the last io
1804  if (!fin.good()) {
1805  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1806  toString((int)pos) + "]. Byte count [" +
1807  toString(p_fileHeaderBytes) + "]" ;
1808  throw IException(IException::Io, msg, _FILEINFO_);
1809  }
1810 
1811  } // End band loop
1812 
1813  } // End line loop
1814 
1815  // Handle the file trailer
1816  pos = fin.tellg();
1817  if (p_saveFileTrailer) {
1818  fin.seekg(0, ios_base::end);
1819  streampos e = fin.tellg();
1820  p_fileTrailerBytes = (int)(e - pos + (streampos)1);
1821  p_fileTrailer = new char[p_fileTrailerBytes];
1822  fin.seekg(pos);
1823  fin.read(p_fileTrailer, p_fileTrailerBytes);
1824 
1825  // Check the io
1826  if (!fin.good()) {
1827  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1828  toString((int)pos) + "]. Byte count [" +
1829  toString(p_fileTrailerBytes) + "]" ;
1830  throw IException(IException::Io, msg, _FILEINFO_);
1831  }
1832 
1833  }
1834 
1835  // Close the file and clean up
1836  fin.close();
1837  delete [] in;
1838  }
1839 
1840 
1851  void ProcessImport::ProcessBip(void funct(Isis::Buffer &out)) {
1852 
1853  // Figure out the number of bytes to read for a single line
1854  int readBytes = Isis::SizeOf(p_pixelType);
1855  readBytes = readBytes * p_ns * p_nb;
1856  char *in = new char [readBytes];
1857 
1858  // Set up an Isis::EndianSwapper object
1859  QString tok(Isis::ByteOrderName(p_byteOrder));
1860  tok = tok.toUpper();
1861  Isis::EndianSwapper swapper(tok);
1862 
1863  ifstream fin;
1864  // Open input file
1865  Isis::FileName inFile(p_inFile);
1866  QString inFileName(inFile.expanded());
1867  fin.open(inFileName.toLatin1().data(), ios::in | ios::binary);
1868  if (!fin.is_open()) {
1869  QString msg = "Cannot open input file [" + p_inFile + "]";
1870  throw IException(IException::Io, msg, _FILEINFO_);
1871  }
1872 
1873  // Handle the file header
1874  streampos pos = fin.tellg();
1875  if (p_saveFileHeader) {
1876  p_fileHeader = new char[p_fileHeaderBytes];
1877  fin.read(p_fileHeader, p_fileHeaderBytes);
1878  fin.seekg(p_suffixData+p_fileHeaderBytes, ios_base::beg);
1879  }
1880  else {
1881  fin.seekg(p_fileHeaderBytes+p_suffixData, ios_base::beg);
1882  }
1883 
1884  // Check the last io
1885  if (!fin.good()) {
1886  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1887  toString((int)pos) + "]. Byte count [" +
1888  toString(p_fileHeaderBytes) + "]" ;
1889  throw IException(IException::Io, msg, _FILEINFO_);
1890  }
1891 
1892  OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
1893 
1894  // Construct a line buffer manager
1895  Isis::Buffer *out = NULL;
1896 
1897  if (funct != NULL) {
1898  out = new Isis::Buffer(p_ns, p_nl, p_nb, p_pixelType);
1899  }
1900  else {
1901  out = new Isis::LineManager(*OutputCubes[0]);
1902  }
1903 
1904  // Loop once for each line in the image
1907 
1908  // Loop for each line
1909  for(int line = 0; line < p_nl; line++) {
1910  // Check the last io
1911  if (!fin.good()) {
1912  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1913  toString((int)pos) + "]. Byte count [" +
1914  toString(p_dataHeaderBytes) + "]" ;
1915  throw IException(IException::Io, msg, _FILEINFO_);
1916  }
1917 
1918  // Space for storing prefix and suffix data pointers
1919  vector<char *> tempPre, tempPost;
1920 
1921  // Handle any line prefix bytes
1922  pos = fin.tellg();
1923  if (p_saveDataPre) {
1924  tempPre.push_back(new char[p_dataPreBytes]);
1925  fin.read(tempPre.back(), p_dataPreBytes);
1926  }
1927  else {
1928  fin.seekg(p_dataPreBytes, ios_base::cur);
1929  }
1930 
1931  // Check the last io
1932  if (!fin.good()) {
1933  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1934  toString((int)pos) + "]. Byte count [" +
1935  toString(p_dataPreBytes) + "]" ;
1936  throw IException(IException::Io, msg, _FILEINFO_);
1937  }
1938 
1939  // Get a line of data from the input file
1940  pos = fin.tellg();
1941  fin.read(in, readBytes);
1942  if (!fin.good()) {
1943  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1944  toString((int)pos) + "]. Byte count [" +
1945  toString(readBytes) + "]" ;
1946  throw IException(IException::Io, msg, _FILEINFO_);
1947  }
1948 
1949  // Loop for each band
1950  for(int band = 0; band < p_nb; band++) {
1951  // Set the base multiplier
1952  double base, mult;
1953  if (p_base.size() > 1) {
1954  base = p_base[band];
1955  mult = p_mult[band];
1956  }
1957  else {
1958  base = p_base[0];
1959  mult = p_mult[0];
1960  }
1961 
1962  // Swap the bytes if necessary and convert any out of bounds pixels
1963  // to special pixels
1964  int osamp = 0;
1965 
1966  for(int samp = band; samp < p_ns * p_nb; samp += p_nb) {
1967  switch(p_pixelType) {
1968  case Isis::UnsignedByte:
1969  (*out)[osamp] = (double)((unsigned char *)in)[samp];
1970  break;
1971  case Isis::UnsignedWord:
1972  (*out)[osamp] =
1973  (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1974  break;
1975  case Isis::SignedWord:
1976  (*out)[osamp] = (double)swapper.ShortInt((short int *)in+samp);
1977  break;
1978  case Isis::SignedInteger:
1979  (*out)[samp] = (double)swapper.Int((int *)in+samp);
1980  break;
1981  case Isis::Real:
1982  if(p_vax_convert) {
1983  (*out)[osamp]= VAXConversion( (float *)in+samp );
1984  }
1985  else {
1986  (*out)[osamp] = (double)swapper.Float((float *)in+samp);
1987  }
1988  break;
1989  case Isis::Double:
1990  (*out)[osamp] = (double)swapper.Double((double *)in+samp);
1991  break;
1992  default:
1993  break;
1994  }
1995 
1996  // Sets out to isis special pixel or leaves it if valid
1997  (*out)[osamp] = TestPixel((*out)[osamp]);
1998 
1999  if (Isis::IsValidPixel((*out)[osamp])) {
2000  (*out)[osamp] = mult * ((*out)[osamp]) + base;
2001  }
2002  osamp++;
2003  } // End sample loop
2004 
2005  if (funct == NULL) {
2006  //Set the buffer position and write the line to the output file
2007  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
2008  OutputCubes[0]->write(*out);
2009  }
2010  else {
2011  funct(*out);
2012  }
2013 
2014  // Handle any line suffix bytes
2015  pos = fin.tellg();
2016  if (p_saveDataPost) {
2017  tempPost.push_back(new char[p_dataPostBytes]);
2018  fin.read(tempPost.back(), p_dataPostBytes);
2019  }
2020  else {
2021  fin.seekg(p_dataPostBytes, ios_base::cur);
2022  }
2023 
2024  // Check the last io
2025  if (!fin.good()) {
2026  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
2027  toString((int)pos) + "]. Byte count [" +
2028  toString(p_dataPreBytes) + "]" ;
2029  throw IException(IException::Io, msg, _FILEINFO_);
2030  }
2031 
2032 
2033  // Save off the prefix bytes vector
2034  if (p_saveDataPre) {
2035  p_dataPre.push_back(tempPre);
2036  tempPre.clear();
2037  }
2038 
2039  // Save off the suffix bytes vector
2040  if (p_saveDataPost) {
2041  p_dataPost.push_back(tempPost);
2042  tempPost.clear();
2043  }
2044 
2045  // Check the last io
2046  if (!fin.good()) {
2047  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
2048  toString((int)pos) + "]. Byte count [" +
2049  toString(p_fileHeaderBytes) + "]" ;
2050  throw IException(IException::Io, msg, _FILEINFO_);
2051  }
2052 
2053  } // End band loop
2054 
2056 
2057  } // End line loop
2058 
2059  // Handle the file trailer
2060  pos = fin.tellg();
2061  if (p_saveFileTrailer) {
2062  fin.seekg(0, ios_base::end);
2063  streampos e = fin.tellg();
2064  p_fileTrailerBytes = (int)(e - pos + (streampos)1);
2065  p_fileTrailer = new char[p_fileTrailerBytes];
2066  fin.seekg(pos);
2067  fin.read(p_fileTrailer, p_fileTrailerBytes);
2068 
2069  // Check the io
2070  if (!fin.good()) {
2071  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
2072  toString((int)pos) + "]. Byte count [" +
2073  toString(p_fileTrailerBytes) + "]" ;
2074  throw IException(IException::Io, msg, _FILEINFO_);
2075  }
2076 
2077  }
2078 
2079  // Close the file and clean up
2080  fin.close();
2081  delete [] in;
2082 
2083  }
2084 
2085 
2101  void ProcessImport::ProcessJp2(void funct(Isis::Buffer &out)) {
2102 
2103  // Set up an Isis::Jp2Decoder object
2104  JP2Decoder *JP2_decoder;
2105  JP2_decoder = new JP2Decoder(p_inFile);
2106 
2107  // Open JP2 file
2108  JP2_decoder->OpenFile();
2109 
2110  // Make sure JP2 file dimensions match PDS labels
2111  p_ns = JP2_decoder->GetSampleDimension();
2112  p_nl = JP2_decoder->GetLineDimension();
2113  p_nb = JP2_decoder->GetBandDimension();
2114 
2115  // Figure out the number of bytes to read for a single line
2116  // from all bands
2117  int sizeofpixel = Isis::SizeOf(p_pixelType);
2118  int startsamp = p_dataPreBytes / sizeofpixel;
2119  int endsamp = startsamp + p_ns;
2120  int readBytes = sizeofpixel * p_ns * p_nb
2122  char **in = new char* [p_nb];
2123  for(int i = 0; i < p_nb; i++) {
2124  in[i] = new char [readBytes];
2125  }
2126 
2127  // Construct a line buffer manager
2128  Isis::Buffer *out = NULL;
2129 
2130  if (funct != NULL) {
2131  out = new Isis::Buffer(p_ns, p_nl, p_nb, p_pixelType);
2132  }
2133  else {
2134  out = new Isis::LineManager(*OutputCubes[0]);
2135  }
2136 
2137  // Loop once for each line in the image
2138  p_progress->SetMaximumSteps(p_nb * p_nl);
2140 
2141  // Loop for each line
2142  for(int line = 0; line < p_nl; line++) {
2143  if (p_pixelType == Isis::UnsignedByte) {
2144  JP2_decoder->Read((unsigned char **)in);
2145  }
2146  else {
2147  JP2_decoder->Read((short int **)in);
2148  }
2149  // Loop for each band
2150  for(int band = 0; band < p_nb; band++) {
2151  // Set the base multiplier
2152  double base, mult;
2153  if (p_base.size() > 1) {
2154  base = p_base[band];
2155  mult = p_mult[band];
2156  }
2157  else {
2158  base = p_base[0];
2159  mult = p_mult[0];
2160  }
2161 
2162  // Space for storing prefix and suffix data pointers
2163  vector<char *> tempPre, tempPost;
2164 
2165 
2166  // Handle any line prefix bytes
2167  if (p_saveDataPre) {
2168  tempPre.push_back(new char[p_dataPreBytes]);
2169  memcpy(&tempPre[0], in[band], p_dataPreBytes);
2170  }
2171 
2172  // Swap the bytes if necessary and convert any out of bounds pixels
2173  // to special pixels
2174  for(int samp = startsamp; samp < endsamp; samp++) {
2175  switch(p_pixelType) {
2176  case Isis::UnsignedByte:
2177  (*out)[samp] = (double)((unsigned char *)in[band])[samp];
2178  break;
2179  case Isis::UnsignedWord:
2180  (*out)[samp] = (double)((unsigned short int *)in[band])[samp];
2181  break;
2182  case Isis::SignedWord:
2183  (*out)[samp] = (double)((short int *)in[band])[samp];
2184  break;
2185  default:
2186  break;
2187  }
2188 
2189  // Sets out to isis special pixel or leaves it if valid
2190  (*out)[samp] = TestPixel((*out)[samp]);
2191 
2192  if (Isis::IsValidPixel((*out)[samp])) {
2193  (*out)[samp] = mult * ((*out)[samp]) + base;
2194  }
2195  } // End sample loop
2196 
2197  if (funct == NULL) {
2198  //Set the buffer position and write the line to the output file
2199  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
2200  OutputCubes[0]->write(*out);
2201  }
2202  else {
2203  funct(*out);
2204  }
2205 
2207 
2208  // Handle any line suffix bytes
2209  if (p_saveDataPost) {
2210  tempPost.push_back(new char[p_dataPostBytes]);
2211  memcpy(&tempPost[0], &in[band][p_dataPreBytes+p_ns*sizeofpixel],
2212  p_dataPostBytes);
2213  }
2214 
2215  // Save off the prefix bytes vector
2216  if (p_saveDataPre) {
2217  p_dataPre.push_back(tempPre);
2218  tempPre.clear();
2219  }
2220 
2221  // Save off the suffix bytes vector
2222  if (p_saveDataPost) {
2223  p_dataPost.push_back(tempPost);
2224  tempPost.clear();
2225  }
2226 
2227  } // End band loop
2228 
2229  } // End line loop
2230 
2231  // Close the file and clean up
2232  delete JP2_decoder;
2233  delete [] in;
2234  }
2235 
2236 
2237 #if 0
2238 
2245  void ProcessImport::AddLabel(Isis::Pvl &label) {
2246 
2247  label.Root();
2248 
2249  Isis::Pvl *output = OutputCubes[0]->Label();
2250  output->Find("IsisCube");
2251  output->Insert(label);
2252  }
2253 
2260  void ProcessImport::AddImportLabel(Isis::Pvl &importLab) {
2261 
2262  importLab.Root();
2263 
2264  Isis::Pvl *output = OutputCubes[0]->Label();
2265  output->Find("IsisCube");
2266  output->addObject("ImportLabel");
2267  output->Insert(importLab);
2268  }
2269 #endif
2270 
2271 
2280  void ProcessImport::SetInputFile(const QString &file) {
2281  p_inFile = file;
2282  if (!Isis::FileName(file).fileExists()) {
2283  QString msg = "File [" + file + "] does not exist";
2284  throw IException(IException::User, msg, _FILEINFO_);
2285  }
2286  }
2287 
2288 
2298  if (p_inFile.size() <= 0) {
2299  QString msg = "No input file has been set";
2301  }
2302  return p_inFile;
2303  }
2304 
2305 }
Buffer for reading and writing cube data.
Definition: Buffer.h:68
JPEG2000 decoder class.
Definition: JP2Decoder.h:92
int p_dataHeaderBytes
Number of bytes of non-image data after the file header and before the image data of each data block...
int FileTrailerBytes() const
This method returns the number of file trailer bytes.
float Float(void *buf)
Swaps a floating point value.
void SaveDataTrailer()
This method marks the data block trailers to be saved.
double TestPixel(const double pixel)
Tests the pixel.
void SetMaximumSteps(const int steps)
This sets the maximum number of steps in the process.
Definition: Progress.cpp:101
bool p_saveFileTrailer
Flag indicating whether to save the file trailer or not.
virtual ~ProcessImport()
Destroys the Import object.
bool propagateMinimumMaximum() const
Return true if the min/max are to be propagated from an input cube.
static UserInterface & GetUserInterface()
Returns the UserInterface object.
bool propagatePixelType() const
Return true if the pixel type is to be propagated from an input cube.
File name manipulation and expansion.
Definition: FileName.h:111
double p_null_max
The pixel value which is the upper bound of NULL data.
bool p_saveFileHeader
Flag indicating whether to save the file header or not.
std::vector< double > p_mult
An array containing the core multiplier for each band.
void SetFileTrailerBytes(const int bytes)
This method sets the number of bytes in the trailer of a file.
std::vector< std::vector< char * > > p_dataPre
The data prefix.
bool p_saveDataPre
Flag indicating whether to save the data prefix or not.
std::vector< Isis::Cube * > OutputCubes
A vector of pointers to allocated Cube objects.
Definition: Process.h:205
int p_ns
Number of samples.
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition: PixelType.h:62
int p_suffixData
The number of bytes past the file header bytes where the suffix data bands are stored.
Buffer for containing a three dimensional section of an image.
Definition: Brick.h:60
QString PixelTypeName(Isis::PixelType pixelType)
Returns string name of PixelType enumeration entered as input parameter.
Definition: PixelType.h:82
int p_dataPreBytes
Number of bytes of non-image data preceding each data record, such as line prefix data in a band sequ...
bool p_saveDataPost
Flag indicating whether to save the data suffix or not.
void SetDataHeaderBytes(const int bytes)
This method sets the number of bytes in the header of each datablock of a file.
ByteOrder
Tests the current architecture for byte order.
Definition: Endian.h:59
void ProcessBip(void funct(Isis::Buffer &out)=NULL)
Function to process files stored as Band Interleaved by Pixel.
Interleave Organization() const
Gets the organization of the input cube.
unsigned short int UnsignedShortInt(void *buf)
Swaps an unsigned short integer value.
std::vector< char * > p_dataHeader
The data header.
std::vector< char * > DataHeader()
This method returns a pointer to the data header.
bool p_saveDataHeader
Flag indicating whether to save the data header or not.
void SetDataPrefixBytes(const int bytes)
This method sets the number of bytes at the beginning of each data record of a file.
int DataPrefixBytes() const
This method returns the number of data prefix bytes.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
double p_his_max
The pixel value which is the upper bound of HIS data.
bool IsValidPixel(const double d)
Returns if the input pixel is valid.
Definition: SpecialPixel.h:225
std::vector< std::vector< char * > > p_dataPost
The data suffix.
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:154
std::vector< double > p_base
An array containing the core base for each band.
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:163
void SetDataSuffixBytes(const int bytes)
This method sets the number of bytes at the end of each data record of a file.
double p_lis_min
The pixel value which is the lower bound of LIS data.
bool IsVAXSpecial(unsigned int *vax, VAXSpecialPixel pix)
Determines if the VAX encoded pixel value is special or not.
QString InputFile()
Sets the name of the input file to be read in the import StartProcess method and verifies its existan...
void SetDimensions(const int ns, const int nl, const int nb)
Sets the physical size of the input cube.
void CheckStatus()
Checks and updates the status.
Definition: Progress.cpp:121
PixelType
Enumerations for Isis Pixel Types.
Definition: PixelType.h:43
std::vector< char * > DataTrailer()
This method returns a pointer to the data trailer.
void ProcessBsq(void funct(Isis::Buffer &out)=NULL)
Process the import data as a band sequential file.
int DataHeaderBytes() const
This method returns the number of data header bytes.
void addObject(const PvlObject &object)
Add a PvlObject.
Definition: PvlObject.h:319
This algorithm is designed for applications that jump around between a couple of spots in the cube wi...
Buffer manager, for moving through a cube in lines.
Definition: LineManager.h:55
std::vector< char * > p_dataTrailer
The data trailer.
Jpeg 2000 Format (always band sequential).
void Read(unsigned char **inbuf)
Read data from JP2 file containing 8-bit data.
Definition: JP2Decoder.cpp:178
char * p_fileHeader
The file header.
int p_nl
Number of lines.
void SetText(const QString &text)
Changes the value of the text string reported just before 0% processed.
Definition: Progress.cpp:77
void CheckPixelRange(QString pixelName, double min, double max)
Checks the special pixel range of the given against all other special pixel value ranges...
void SaveDataSuffix()
This method marks the data suffix to be saved.
void SetHIS(const double his_min, const double his_max)
Sets the range that will be considered Isis::Null.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:38
double p_his_min
The pixel value which is the lower bound of HIS data.
double Double(void *buf)
Swaps a double precision value.
Manipulate and parse attributes of output cube filenames.
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:134
double VAXConversion(void *ibuf)
Conversion routine which translates VAX_REAL to IEEE_REAL.
Isis::PixelType p_pixelType
Pixel type of input data.
std::vector< std::vector< char * > > DataPrefix()
This method returns a pointer to the data prefix.
bool IsLsb()
Return true if this host is an LSB first machine and false if it is not.
Definition: Endian.h:84
void ProcessBil(void funct(Isis::Buffer &out)=NULL)
Function to process files stored as Band Interleaved by Line.
double p_null_min
The pixel value which is the lower bound of NULL data.
void SaveDataPrefix()
This method marks the data prefix to be saved.
Byte swapper.
Definition: EndianSwapper.h:54
int p_fileHeaderBytes
The number of bytes of non-image data at the beginning of a file.
virtual void StartProcess()
Process the input file and write it to the output.
Isis::Cube * SetOutputCube(const QString &parameter)
Create the output file.
double p_hrs_max
The pixel value which is the upper bound of HRS data.
void setPixelType(PixelType type)
Set the pixel type to that given by the parameter.
void SetDataTrailerBytes(const int bytes)
This method sets the number of bytes in the trailer of each datablock of a file.
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.
void setMinimum(double min)
Set the output cube attribute minimum.
void SetPixelType(const Isis::PixelType type)
Sets the pixel type of the input file.
Container for cube-like labels.
Definition: Pvl.h:135
void SaveDataHeader()
This method marks the data block headers to be saved.
virtual Isis::Cube * SetOutputCube(const QString &parameter)
Allocates a user-specified output cube whose size matches the first input cube.
Definition: Process.cpp:266
int p_dataTrailerBytes
Number of bytes of non-image data after the image data of each data block, such as band trailer data ...
char * p_fileTrailer
The file trailer.
double p_hrs_min
The pixel value which is the lower bound of HRS data.
void SetOrganization(const ProcessImport::Interleave org)
Sets the organization of the input cube.
Isis::CubeAttributeOutput & GetOutputAttribute(const QString &paramName)
Gets the attributes for an output cube.
Definition: IsisAml.cpp:1935
Interleave
This enum includes how the document should be read: by BSQ, BIL, BIP, JP2, or InterleaveUndefined.
Band Sequential Format (i.e.
void SaveFileTrailer()
This method marks the file trailer to be saved.
Band Interleaved By Pixel Format (i.e.
int DataSuffixBytes() const
This method returns the number of data duffix bytes.
double p_lrs_min
The pixel value which is the lower bound of LRS data.
void SetLRS(const double lrs_min, const double lrs_max)
Sets the range that will be considered Isis::Null.
QString p_inFile
Input file name.
ProcessImport::Interleave p_organization
The format of the input file.
Isis::Progress * p_progress
Pointer to a Progress object.
Definition: Process.h:159
char * FileHeader()
This method returns a pointer to the file header.
Isis exception class.
Definition: IException.h:99
char * FileTrailer()
This method returns a pointer to the file trailer.
void SetMultiplier(const double mult)
Sets the core multiplier of the input cube.
int FileHeaderBytes() const
This method returns the number of file header bytes.
void ProcessJp2(void funct(Isis::Buffer &out)=NULL)
Function to process files containing compressed JPEG2000 data (which is always BSQ but is processed a...
short int ShortInt(void *buf)
Swaps a short integer value.
void OpenFile()
Open the JPEG2000 file.
Definition: JP2Decoder.cpp:62
void SetVAXConvert(const bool vax_convert)
Sets the VAX flag of the input cube.
int DataTrailerBytes() const
This method returns the number of data trailer bytes.
Isis::ByteOrder p_byteOrder
Byte order of data.
void SetFileHeaderBytes(const int bytes)
This method sets the number of bytes in the header of a file.
int p_fileTrailerBytes
The number of bytes of non-image data at the end of a file.
void SetLIS(const double lis_min, const double lis_max)
Sets the range that will be considered Isis::Null.
void SetBase(const double base)
Sets the core base of the input cube.
Isis::PixelType p_suffixPixelType
The pixel type of the suffix data.
std::vector< std::vector< char * > > DataSuffix()
This method returns a pointer to 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...
double p_lis_max
The pixel value which is the upper bound of LIS data.
double p_lrs_max
The pixel value which is the upper bound of LRS data.
Base class for all cube processing derivatives.
Definition: Process.h:157
int p_nb
Number of bands.
void setMaximum(double max)
Set the output cube attribute maximum.
int p_dataPostBytes
Number of bytes of non-image data following each data record, such as line suffix data in a band sequ...
void SetByteOrder(const Isis::ByteOrder order)
Sets the byte order of the input file.
Band Interleaved By Line Format (i.e.
void SetHRS(const double hrs_min, const double hrs_max)
Sets the range that will be considered Isis::Null.
bool p_saveDataTrailer
Flag indicating whether to save the data trailer or not.
int Int(void *buf)
Swaps a 4 byte integer value.
IO Handler for Isis Cubes.
Definition: Cube.h:158
void SaveFileHeader()
This method marks the file header to be saved.

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:26:24