Isis 3 Programmer Reference
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) || type==Isis::UnsignedInteger) {
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_dataTrailerBytes;
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 
1230  else if (p_pixelType == Isis::UnsignedInteger) {
1231  min = Isis::VALID_MINUI4;
1232  max = Isis::VALID_MAXUI4;
1233  }
1234  else if (p_pixelType == Isis::SignedWord) {
1235  min = Isis::VALID_MIN2 * p_mult[0] + p_base[0];
1236  max = Isis::VALID_MAX2 * p_mult[0] + p_base[0];
1237  }
1238  else if (p_pixelType == Isis::UnsignedWord) {
1239  min = Isis::VALID_MINU2 * p_mult[0] + p_base[0];
1240  max = Isis::VALID_MAXU2 * p_mult[0] + p_base[0];
1241  }
1242  else if (p_pixelType == Isis::UnsignedByte) {
1243  min = Isis::VALID_MIN1 * p_mult[0] + p_base[0];
1244  max = Isis::VALID_MAX1 * p_mult[0] + p_base[0];
1245  }
1246  else {
1247  QString msg = "Unsupported pixel type [" +
1250  }
1251  att.setMinimum(min);
1252  att.setMaximum(max);
1253  }
1254 
1255  if (att.propagatePixelType()) {
1256  if ((p_base.size() > 1) || (p_mult.size() > 1)) {
1257  att.setPixelType(Isis::Real);
1258  }
1259  else if (p_pixelType == Isis::Double || p_pixelType == Isis::SignedInteger) {
1260  att.setPixelType(Isis::Real);
1261  }
1262  else {
1264  }
1265  }
1266 
1267  return Process::SetOutputCube(Application::GetUserInterface().GetFileName(parameter), att, p_ns, p_nl, p_nb);
1268  }
1269 
1270 
1286  return Isis::Process::SetOutputCube(fname, att, p_ns, p_nl, p_nb);
1287  }
1288 
1289 
1293  ProcessJp2();
1294  }
1295  else if (p_organization == ProcessImport::BSQ) {
1296  ProcessBsq();
1297  }
1298  else if (p_organization == ProcessImport::BIL) {
1299  ProcessBil();
1300  }
1301  else if (p_organization == ProcessImport::BIP) {
1302  ProcessBip();
1303  }
1304  else {
1305  QString msg = "File [" + p_inFile
1306  + "] is not in a supported organization.";
1308  }
1309  }
1310 
1311 
1323  ProcessJp2(funct);
1324  }
1325  else if (p_organization == ProcessImport::BSQ) {
1326  ProcessBsq(funct);
1327  }
1328  else if (p_organization == ProcessImport::BIL) {
1329  ProcessBil(funct);
1330  }
1331  else if (p_organization == ProcessImport::BIP) {
1332  ProcessBip(funct);
1333  }
1334  else {
1335  QString msg = "File [" + p_inFile + "] is not in a supported organization.";
1337  }
1338  }
1339 
1340 
1352  void ProcessImport::ProcessBsq(void funct(Isis::Buffer &out)) {
1353  // Figure out the number of bytes to read for a single line
1354  int readBytes = Isis::SizeOf(p_pixelType);
1355  readBytes = readBytes * p_ns;
1356  char *in = new char [readBytes];
1357 
1358  // Set up an Isis::EndianSwapper object
1359  QString tok(Isis::ByteOrderName(p_byteOrder));
1360  tok = tok.toUpper();
1361  Isis::EndianSwapper swapper(tok);
1362 
1363  ifstream fin;
1364  // Open input file
1365  Isis::FileName inFile(p_inFile);
1366  QString inFileName(inFile.expanded());
1367  fin.open(inFileName.toLatin1().data(), ios::in | ios::binary);
1368  if (!fin.is_open()) {
1369  QString msg = "Cannot open input file [" + p_inFile + "]";
1370  throw IException(IException::Io, msg, _FILEINFO_);
1371  }
1372 
1373  // Handle the file header
1374  streampos pos = fin.tellg();
1375  if (p_saveFileHeader) {
1376  p_fileHeader = new char[p_fileHeaderBytes];
1377  fin.read(p_fileHeader, p_fileHeaderBytes);
1378  fin.seekg(p_suffixData+p_fileHeaderBytes, ios_base::beg);
1379  }
1380  else {
1381  fin.seekg(p_fileHeaderBytes+p_suffixData, ios_base::beg);
1382  }
1383 
1384  // Check the last io
1385  if (!fin.good()) {
1386  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1387  toString((int)pos) + "]. Byte count [" +
1388  toString(p_fileHeaderBytes) + "]" ;
1389  throw IException(IException::Io, msg, _FILEINFO_);
1390  }
1391 
1392  // Construct a line buffer manager
1393  Isis::Buffer *out = NULL;
1394 
1395  if (funct != NULL) {
1396  out = new Isis::Brick(p_ns, 1, 1, p_pixelType);
1397  }
1398  else {
1399  out = new Isis::LineManager(*OutputCubes[0]);
1400  }
1401 
1402  // Loop once for each band in the image
1405 
1406  // Loop for each band
1407  for(int band = 0; band < p_nb; band++) {
1408  // Set the base multiplier
1409  double base, mult;
1410  if (p_base.size() > 1) {
1411  base = p_base[band];
1412  mult = p_mult[band];
1413  }
1414  else {
1415  base = p_base[0];
1416  mult = p_mult[0];
1417  }
1418 
1419  // Handle any data headers (e.g., the data at the beginning of each band)
1420  pos = fin.tellg();
1421  if (p_saveDataHeader) {
1422  p_dataHeader.push_back(new char[p_dataHeaderBytes]);
1423  fin.read(p_dataHeader.back(), p_dataHeaderBytes);
1424  }
1425  else {
1426  fin.seekg(p_dataHeaderBytes, ios_base::cur);
1427  }
1428 
1429  // Check the last io
1430  if (!fin.good()) {
1431  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1432  toString((int)pos) + "]. Byte count [" +
1433  toString(p_dataHeaderBytes) + "]" ;
1434  throw IException(IException::Io, msg, _FILEINFO_);
1435  }
1436 
1437  // Space for storing prefix and suffix data pointers
1438  vector<char *> tempPre, tempPost;
1439 
1440  // Loop for each line in a band
1441  for(int line = 0; line < p_nl; line++) {
1442 
1443  // Handle any line prefix bytes
1444  pos = fin.tellg();
1445  if (p_saveDataPre) {
1446  tempPre.push_back(new char[p_dataPreBytes]);
1447  fin.read(tempPre.back(), p_dataPreBytes);
1448  }
1449  else {
1450  fin.seekg(p_dataPreBytes, ios_base::cur);
1451  }
1452 
1453  // Check the last io
1454  if (!fin.good()) {
1455  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1456  toString((int)pos) + "]. Byte count [" +
1457  toString(p_dataPreBytes) + "]" ;
1458  throw IException(IException::Io, msg, _FILEINFO_);
1459  }
1460 
1461  // Get a line of data from the input file
1462  pos = fin.tellg();
1463  fin.read(in, readBytes);
1464  if (!fin.good()) {
1465  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1466  toString((int)pos) + "]. Byte count [" +
1467  toString(readBytes) + "]" ;
1468  throw IException(IException::Io, msg, _FILEINFO_);
1469  }
1470 
1471  // Swap the bytes if necessary and convert any out of bounds pixels
1472  // to special pixels
1473  for(int samp = 0; samp < p_ns; samp++) {
1474  switch(p_pixelType) {
1475  case Isis::UnsignedByte:
1476  (*out)[samp] = (double)((unsigned char *)in)[samp];
1477  break;
1478  case Isis::UnsignedWord:
1479  (*out)[samp] =
1480  (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1481  break;
1482  case Isis::SignedWord:
1483  (*out)[samp] = (double)swapper.ShortInt((short int *)in+samp);
1484  break;
1485  case Isis::SignedInteger:
1486  (*out)[samp] = (double)swapper.Int((int *)in+samp);
1487  break;
1488 
1489  case Isis::UnsignedInteger:
1490  (*out)[samp] = (double)swapper.Uint32_t((unsigned int *)in+samp);
1491  break;
1492  case Isis::Real:
1493  if(p_vax_convert) {
1494  (*out)[samp]= VAXConversion( (float *)in+samp );
1495  }
1496  else {
1497  (*out)[samp] = (double)swapper.Float((float *)in+samp);
1498  }
1499  break;
1500  case Isis::Double:
1501  (*out)[samp] = (double)swapper.Double((double *)in+samp);
1502  break;
1503  default:
1504  break;
1505  }
1506 
1507  (*out)[samp] = TestPixel((*out)[samp]);
1508 
1509  if (Isis::IsValidPixel((*out)[samp])) {
1510  (*out)[samp] = mult * ((*out)[samp]) + base;
1511  }
1512  } // End sample loop
1513 
1514  if (funct == NULL) {
1515  // Set the buffer position and write the line to the output file
1516  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1517  OutputCubes[0]->write(*out);
1518  }
1519  else {
1520  ((Isis::Brick *)out)->SetBaseSample(1);
1521  ((Isis::Brick *)out)->SetBaseLine(line + 1);
1522  ((Isis::Brick *)out)->SetBaseBand(band + 1);
1523  funct(*out);
1524  }
1525 
1527 
1528  // Handle any line suffix bytes
1529  pos = fin.tellg();
1530  if (p_saveDataPost) {
1531  tempPost.push_back(new char[p_dataPostBytes]);
1532  fin.read(tempPost.back(), p_dataPostBytes);
1533  }
1534  else {
1535  fin.seekg(p_dataPostBytes, ios_base::cur);
1536  }
1537 
1538  // Check the last io
1539  if (!fin.good()) {
1540  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1541  toString((int)pos) + "]. Byte count [" +
1542  toString(p_dataPreBytes) + "]" ;
1543  throw IException(IException::Io, msg, _FILEINFO_);
1544  }
1545  } // End line loop
1546 
1547  // Save off the prefix bytes vector
1548  if (p_saveDataPre) {
1549  p_dataPre.push_back(tempPre);
1550  tempPre.clear();
1551  }
1552 
1553  // Save off the suffix bytes vector
1554  if (p_saveDataPost) {
1555  p_dataPost.push_back(tempPost);
1556  tempPost.clear();
1557  }
1558 
1559  // Handle the band trailer
1560  pos = fin.tellg();
1561  if (p_saveDataTrailer) {
1562  p_dataTrailer.push_back(new char[p_dataTrailerBytes]);
1563  fin.read(p_dataTrailer.back(), p_dataTrailerBytes);
1564  }
1565  else {
1566  fin.seekg(p_dataTrailerBytes, ios_base::cur);
1567  }
1568 
1569  // Check the last io
1570  if (!fin.good()) {
1571  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1572  toString((int)pos) + "]. Byte count [" +
1573  toString(p_fileHeaderBytes) + "]" ;
1574  throw IException(IException::Io, msg, _FILEINFO_);
1575  }
1576 
1577  } // End band loop
1578 
1579  // Handle the file trailer
1580  pos = fin.tellg();
1581  if (p_saveFileTrailer) {
1582  fin.seekg(0, ios_base::end);
1583  streampos e = fin.tellg();
1584  p_fileTrailerBytes = (int)(e - pos + (streampos)1);
1585  p_fileTrailer = new char[p_fileTrailerBytes];
1586  fin.seekg(pos);
1587  fin.read(p_fileTrailer, p_fileTrailerBytes);
1588 
1589  // Check the io
1590  if (!fin.good()) {
1591  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1592  toString((int)pos) + "]. Byte count [" +
1593  toString(p_fileTrailerBytes) + "]" ;
1594  throw IException(IException::Io, msg, _FILEINFO_);
1595  }
1596 
1597  }
1598 
1599  // Close the file and clean up
1600  fin.close();
1601  delete [] in;
1602  }
1603 
1604 
1615  void ProcessImport::ProcessBil(void funct(Isis::Buffer &someBuf)) {
1616 
1617  // Figure out the number of bytes to read for a single line
1618  int readBytes = Isis::SizeOf(p_pixelType);
1619  readBytes = readBytes * p_ns;
1620  char *in = new char [readBytes];
1621 
1622  // Set up an Isis::EndianSwapper object
1623  QString tok(Isis::ByteOrderName(p_byteOrder));
1624  tok = tok.toUpper();
1625  Isis::EndianSwapper swapper(tok);
1626 
1627  ifstream fin;
1628  // Open input file
1629  Isis::FileName inFile(p_inFile);
1630  QString inFileName(inFile.expanded());
1631  fin.open(inFileName.toLatin1().data(), ios::in | ios::binary);
1632  if (!fin.is_open()) {
1633  QString msg = "Cannot open input file [" + p_inFile + "]";
1634  throw IException(IException::Io, msg, _FILEINFO_);
1635  }
1636 
1637  // Handle the file header
1638  streampos pos = fin.tellg();
1639 
1640  if (p_saveFileHeader) {
1641  p_fileHeader = new char[p_fileHeaderBytes];
1642  fin.read(p_fileHeader, p_fileHeaderBytes);
1643  fin.seekg(p_suffixData+p_fileHeaderBytes, ios_base::beg);
1644  }
1645  else {
1646  fin.seekg(p_fileHeaderBytes+p_suffixData, ios_base::beg);
1647  }
1648 
1649  // Check the last io
1650  if (!fin.good()) {
1651  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1652  toString((int)pos) + "]. Byte count [" +
1653  toString(p_fileHeaderBytes) + "]" ;
1654  throw IException(IException::Io, msg, _FILEINFO_);
1655  }
1656 
1657  // Construct a line buffer manager
1658  Isis::Buffer *out = NULL;
1659 
1660  if (funct != NULL) {
1661  out = new Isis::Brick(p_ns, p_nl, p_nb, p_ns, 1, 1, p_pixelType, true);
1662  ((Isis::Brick *)out)->setpos(0);
1663  }
1664  else {
1665  OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
1666  out = new Isis::LineManager(*OutputCubes[0]);
1667  }
1668 
1669  // Loop once for each line in the image
1672 
1673  // Loop for each line
1674  for(int line = 0; line < p_nl; line++) {
1675  // Loop for each band
1676  for(int band = 0; band < p_nb; band++) {
1677  // Set the base multiplier
1678  double base, mult;
1679  if (p_base.size() > 1) {
1680  base = p_base[band];
1681  mult = p_mult[band];
1682  }
1683  else {
1684  base = p_base[0];
1685  mult = p_mult[0];
1686  }
1687 
1688  // Check the last io
1689  if (!fin.good()) {
1690  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1691  toString((int)pos) + "]. Byte count [" +
1692  toString(p_dataHeaderBytes) + "]" ;
1693  throw IException(IException::Io, msg, _FILEINFO_);
1694  }
1695 
1696  // Space for storing prefix and suffix data pointers
1697  vector<char *> tempPre, tempPost;
1698 
1699  // Handle any line prefix bytes
1700  pos = fin.tellg();
1701  if (p_saveDataPre) {
1702  tempPre.push_back(new char[p_dataPreBytes]);
1703  fin.read(tempPre.back(), p_dataPreBytes);
1704  }
1705  else {
1706  fin.seekg(p_dataPreBytes, ios_base::cur);
1707  }
1708 
1709  // Check the last io
1710  if (!fin.good()) {
1711  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1712  toString((int)pos) + "]. Byte count [" +
1713  toString(p_dataPreBytes) + "]" ;
1714  throw IException(IException::Io, msg, _FILEINFO_);
1715  }
1716 
1717 
1718  // Get a line of data from the input file
1719  pos = fin.tellg();
1720  fin.read(in, readBytes);
1721  if (!fin.good()) {
1722  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1723  toString((int)pos) + "]. Byte count [" +
1724  toString(readBytes) + "]" ;
1725  throw IException(IException::Io, msg, _FILEINFO_);
1726  }
1727 
1728  // Swap the bytes if necessary and convert any out of bounds pixels
1729  // to special pixels
1730  for(int samp = 0; samp < p_ns; samp++) {
1731  switch(p_pixelType) {
1732  case Isis::UnsignedByte:
1733  (*out)[samp] = (double)((unsigned char *)in)[samp];
1734  break;
1735  case Isis::UnsignedWord:
1736  (*out)[samp] =
1737  (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1738  break;
1739  case Isis::SignedWord:
1740  (*out)[samp] = (double)swapper.ShortInt((short int *)in+samp);
1741  break;
1742  case Isis::SignedInteger:
1743  (*out)[samp] = (double)swapper.Int((int *)in+samp);
1744  break;
1745  case Isis::UnsignedInteger:
1746  (*out)[samp] = (double)swapper.Uint32_t((unsigned int *)in+samp);
1747  break;
1748  case Isis::Real:
1749  if(p_vax_convert) {
1750  (*out)[samp]= VAXConversion( (float *)in+samp );
1751  }
1752  else {
1753  (*out)[samp] = (double)swapper.Float((float *)in+samp);
1754  }
1755  break;
1756  case Isis::Double:
1757  (*out)[samp] = (double)swapper.Double((double *)in+samp);
1758  break;
1759  default:
1760  break;
1761  }
1762 
1763  // Sets out to isis special pixel or leaves it if valid
1764  (*out)[samp] = TestPixel((*out)[samp]);
1765 
1766  if (Isis::IsValidPixel((*out)[samp])) {
1767  (*out)[samp] = mult * ((*out)[samp]) + base;
1768  }
1769  } // End sample loop
1770 
1771  if (funct == NULL) {
1772  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1773  OutputCubes[0]->write(*out);
1774  }
1775  else {
1776  funct(*out);
1777  (*((Isis::Brick *)out))++;
1778  }
1779 
1781 
1782  // Handle any line suffix bytes
1783  pos = fin.tellg();
1784  if (p_saveDataPost) {
1785  tempPost.push_back(new char[p_dataPostBytes]);
1786  fin.read(tempPost.back(), p_dataPostBytes);
1787  }
1788  else {
1789  fin.seekg(p_dataPostBytes, ios_base::cur);
1790  }
1791 
1792  // Check the last io
1793  if (!fin.good()) {
1794  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1795  toString((int)pos) + "]. Byte count [" +
1796  toString(p_dataPreBytes) + "]" ;
1797  throw IException(IException::Io, msg, _FILEINFO_);
1798  }
1799 
1800  // Save off the prefix bytes vector
1801  if (p_saveDataPre) {
1802  p_dataPre.push_back(tempPre);
1803  tempPre.clear();
1804  }
1805 
1806  // Save off the suffix bytes vector
1807  if (p_saveDataPost) {
1808  p_dataPost.push_back(tempPost);
1809  tempPost.clear();
1810  }
1811 
1812  // Check the last io
1813  if (!fin.good()) {
1814  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1815  toString((int)pos) + "]. Byte count [" +
1816  toString(p_fileHeaderBytes) + "]" ;
1817  throw IException(IException::Io, msg, _FILEINFO_);
1818  }
1819 
1820  } // End band loop
1821 
1822  } // End line loop
1823 
1824  // Handle the file trailer
1825  pos = fin.tellg();
1826  if (p_saveFileTrailer) {
1827  fin.seekg(0, ios_base::end);
1828  streampos e = fin.tellg();
1829  p_fileTrailerBytes = (int)(e - pos + (streampos)1);
1830  p_fileTrailer = new char[p_fileTrailerBytes];
1831  fin.seekg(pos);
1832  fin.read(p_fileTrailer, p_fileTrailerBytes);
1833 
1834  // Check the io
1835  if (!fin.good()) {
1836  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1837  toString((int)pos) + "]. Byte count [" +
1838  toString(p_fileTrailerBytes) + "]" ;
1839  throw IException(IException::Io, msg, _FILEINFO_);
1840  }
1841 
1842  }
1843 
1844  // Close the file and clean up
1845  fin.close();
1846  delete [] in;
1847  }
1848 
1849 
1860  void ProcessImport::ProcessBip(void funct(Isis::Buffer &out)) {
1861 
1862  // Set up an Isis::EndianSwapper object
1863  QString tok(Isis::ByteOrderName(p_byteOrder));
1864  tok = tok.toUpper();
1865  Isis::EndianSwapper swapper(tok);
1866 
1867  ifstream fin;
1868  // Open input file
1869  Isis::FileName inFile(p_inFile);
1870  QString inFileName(inFile.expanded());
1871  fin.open(inFileName.toLatin1().data(), ios::in | ios::binary);
1872  if (!fin.is_open()) {
1873  QString msg = "Cannot open input file [" + p_inFile + "]";
1874  throw IException(IException::Io, msg, _FILEINFO_);
1875  }
1876 
1877  // Handle the file header
1878  streampos pos = fin.tellg();
1879  if (p_saveFileHeader) {
1880  p_fileHeader = new char[p_fileHeaderBytes];
1881  fin.read(p_fileHeader, p_fileHeaderBytes);
1882  fin.seekg(p_suffixData+p_fileHeaderBytes, ios_base::beg);
1883  }
1884  else {
1885  fin.seekg(p_fileHeaderBytes+p_suffixData, ios_base::beg);
1886  }
1887 
1888  // Check the last io
1889  if (!fin.good()) {
1890  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1891  toString((int)pos) + "]. Byte count [" +
1892  toString(p_fileHeaderBytes) + "]" ;
1893  throw IException(IException::Io, msg, _FILEINFO_);
1894  }
1895 
1896  OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
1897 
1898  // Construct a line buffer manager
1899  Isis::Buffer *out = NULL;
1900 
1901  if (funct != NULL) {
1902  out = new Isis::Buffer(p_ns, p_nl, p_nb, p_pixelType);
1903  }
1904  else {
1905  out = new Isis::LineManager(*OutputCubes[0]);
1906  }
1907 
1908  // Loop once for each line in the image
1911 
1912  // Figure out the number of bytes to read for a single line
1913  int sampleBytes = Isis::SizeOf(p_pixelType) * p_nb + p_dataPreBytes + p_dataPostBytes;
1914  int readBytes = p_ns * sampleBytes;
1915  char *in = new char [readBytes];
1916 
1917  // Loop for each line
1918  for(int line = 0; line < p_nl; line++) {
1919  // Check the last io
1920  if (!fin.good()) {
1921  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1922  toString((int)pos) + "]. Byte count [" +
1923  toString(p_dataHeaderBytes) + "]" ;
1924  throw IException(IException::Io, msg, _FILEINFO_);
1925  }
1926 
1927  // Space for storing prefix and suffix data pointers
1928  vector<char *> tempPre, tempPost;
1929 
1930  // Check the last io
1931  if (!fin.good()) {
1932  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1933  toString((int)pos) + "]. Byte count [" +
1934  toString(p_dataPreBytes) + "]" ;
1935  throw IException(IException::Io, msg, _FILEINFO_);
1936  }
1937 
1938  // Get a line of data from the input file
1939  pos = fin.tellg();
1940  fin.read(in, readBytes);
1941  if (!fin.good()) {
1942  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1943  toString((int)pos) + "]. Byte count [" +
1944  toString(readBytes) + "]" ;
1945  throw IException(IException::Io, msg, _FILEINFO_);
1946  }
1947 
1948  // Loop for each band
1949  for(int band = 0; band < p_nb; band++) {
1950  // Set the base multiplier
1951  double base, mult;
1952  if (p_base.size() > 1) {
1953  base = p_base[band];
1954  mult = p_mult[band];
1955  }
1956  else {
1957  base = p_base[0];
1958  mult = p_mult[0];
1959  }
1960 
1961  // Swap the bytes if necessary and convert any out of bounds pixels
1962  // to special pixels
1963  int osamp = 0;
1964 
1965  for(int samp = 0; samp < p_ns; samp++) {
1966  int bufferIndex = p_dataPreBytes + Isis::SizeOf(p_pixelType)*band + samp*sampleBytes;
1967  switch(p_pixelType) {
1968  case Isis::UnsignedByte:
1969  (*out)[osamp] = (double)((unsigned char *)in)[bufferIndex];
1970  break;
1971  case Isis::UnsignedWord:
1972  (*out)[osamp] =
1973  (double)swapper.UnsignedShortInt(&in[bufferIndex]);
1974  break;
1975  case Isis::SignedWord:
1976  (*out)[osamp] = (double)swapper.ShortInt(&in[bufferIndex]);
1977  break;
1978  case Isis::SignedInteger:
1979  (*out)[samp] = (double)swapper.Int(&in[bufferIndex]);
1980  break;
1981  case Isis::UnsignedInteger:
1982  (*out)[samp] = (double)swapper.Uint32_t(&in[bufferIndex]);
1983  break;
1984  case Isis::Real:
1985  if(p_vax_convert) {
1986  (*out)[osamp]= VAXConversion(&in[bufferIndex]);
1987  }
1988  else {
1989  (*out)[osamp] = (double)swapper.Float(&in[bufferIndex]);
1990  }
1991  break;
1992  case Isis::Double:
1993  (*out)[osamp] = (double)swapper.Double(&in[bufferIndex]);
1994  break;
1995  default:
1996  break;
1997  }
1998 
1999  // Sets out to isis special pixel or leaves it if valid
2000  (*out)[osamp] = TestPixel((*out)[osamp]);
2001 
2002  if (Isis::IsValidPixel((*out)[osamp])) {
2003  (*out)[osamp] = mult * ((*out)[osamp]) + base;
2004  }
2005  osamp++;
2006  } // End sample loop
2007 
2008  if (funct == NULL) {
2009  //Set the buffer position and write the line to the output file
2010  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
2011  OutputCubes[0]->write(*out);
2012  }
2013  else {
2014  funct(*out);
2015  }
2016 
2017  } // End band loop
2018 
2019  // Handle record prefix and suffix
2020  if (p_saveDataPre) {
2021  for(int samp = 0; samp < p_ns; samp++) {
2022  char *samplePrefix = new char[p_dataPreBytes];
2023  memcpy(samplePrefix, &in[samp*sampleBytes], p_dataPreBytes);
2024  tempPre.push_back(samplePrefix);
2025  }
2026  p_dataPre.push_back(tempPre);
2027  tempPre.clear();
2028  }
2029  if (p_saveDataPost) {
2030  for(int samp = 0; samp < p_ns; samp++) {
2031  char *sampleSuffix = new char[p_dataPostBytes];
2032  int suffixIndex = p_dataPreBytes + Isis::SizeOf(p_pixelType)*p_nb + samp*sampleBytes;
2033  memcpy(sampleSuffix, &in[suffixIndex], p_dataPostBytes);
2034  tempPost.push_back(sampleSuffix);
2035  }
2036  p_dataPost.push_back(tempPost);
2037  tempPost.clear();
2038  }
2039 
2040  // Check the last io
2041  if (!fin.good()) {
2042  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
2043  toString((int)pos) + "]. Byte count [" +
2044  toString(p_dataPreBytes) + "]" ;
2045  throw IException(IException::Io, msg, _FILEINFO_);
2046  }
2047 
2048  // Handle the data trailer
2049  pos = fin.tellg();
2050  if (p_saveDataTrailer) {
2051  p_dataTrailer.push_back(new char[p_dataTrailerBytes]);
2052  fin.read(p_dataTrailer.back(), p_dataTrailerBytes);
2053  }
2054  else {
2055  fin.seekg(p_dataTrailerBytes, ios_base::cur);
2056  }
2057 
2058  // Check the last io
2059  if (!fin.good()) {
2060  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
2061  toString((int)pos) + "]. Byte count [" +
2062  toString(p_fileHeaderBytes) + "]" ;
2063  throw IException(IException::Io, msg, _FILEINFO_);
2064  }
2065 
2067 
2068  } // End line loop
2069 
2070  // Handle the file trailer
2071  pos = fin.tellg();
2072  if (p_saveFileTrailer) {
2073  fin.seekg(0, ios_base::end);
2074  streampos e = fin.tellg();
2075  p_fileTrailerBytes = (int)(e - pos + (streampos)1);
2076  p_fileTrailer = new char[p_fileTrailerBytes];
2077  fin.seekg(pos);
2078  fin.read(p_fileTrailer, p_fileTrailerBytes);
2079 
2080  // Check the io
2081  if (!fin.good()) {
2082  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
2083  toString((int)pos) + "]. Byte count [" +
2084  toString(p_fileTrailerBytes) + "]" ;
2085  throw IException(IException::Io, msg, _FILEINFO_);
2086  }
2087 
2088  }
2089 
2090  // Close the file and clean up
2091  fin.close();
2092  delete [] in;
2093 
2094  }
2095 
2096 
2112  void ProcessImport::ProcessJp2(void funct(Isis::Buffer &out)) {
2113 
2114  // Set up an Isis::Jp2Decoder object
2115  JP2Decoder *JP2_decoder;
2116  JP2_decoder = new JP2Decoder(p_inFile);
2117 
2118  // Open JP2 file
2119  JP2_decoder->OpenFile();
2120 
2121  // Make sure JP2 file dimensions match PDS labels
2122  p_ns = JP2_decoder->GetSampleDimension();
2123  p_nl = JP2_decoder->GetLineDimension();
2124  p_nb = JP2_decoder->GetBandDimension();
2125 
2126  // Figure out the number of bytes to read for a single line
2127  // from all bands
2128  int sizeofpixel = Isis::SizeOf(p_pixelType);
2129  int startsamp = p_dataPreBytes / sizeofpixel;
2130  int endsamp = startsamp + p_ns;
2131  int readBytes = sizeofpixel * p_ns * p_nb
2133  char **in = new char* [p_nb];
2134  for(int i = 0; i < p_nb; i++) {
2135  in[i] = new char [readBytes];
2136  }
2137 
2138  // Construct a line buffer manager
2139  Isis::Buffer *out = NULL;
2140 
2141  if (funct != NULL) {
2142  out = new Isis::Buffer(p_ns, p_nl, p_nb, p_pixelType);
2143  }
2144  else {
2145  out = new Isis::LineManager(*OutputCubes[0]);
2146  }
2147 
2148  // Loop once for each line in the image
2151 
2152  // Loop for each line
2153  for(int line = 0; line < p_nl; line++) {
2154  if (p_pixelType == Isis::UnsignedByte) {
2155  JP2_decoder->Read((unsigned char **)in);
2156  }
2157  else {
2158  JP2_decoder->Read((short int **)in);
2159  }
2160  // Loop for each band
2161  for(int band = 0; band < p_nb; band++) {
2162  // Set the base multiplier
2163  double base, mult;
2164  if (p_base.size() > 1) {
2165  base = p_base[band];
2166  mult = p_mult[band];
2167  }
2168  else {
2169  base = p_base[0];
2170  mult = p_mult[0];
2171  }
2172 
2173  // Space for storing prefix and suffix data pointers
2174  vector<char *> tempPre, tempPost;
2175 
2176 
2177  // Handle any line prefix bytes
2178  if (p_saveDataPre) {
2179  tempPre.push_back(new char[p_dataPreBytes]);
2180  memcpy(&tempPre[0], in[band], p_dataPreBytes);
2181  }
2182 
2183  // Swap the bytes if necessary and convert any out of bounds pixels
2184  // to special pixels
2185  for(int samp = startsamp; samp < endsamp; samp++) {
2186  switch(p_pixelType) {
2187  case Isis::UnsignedByte:
2188  (*out)[samp] = (double)((unsigned char *)in[band])[samp];
2189  break;
2190  case Isis::UnsignedWord:
2191  (*out)[samp] = (double)((unsigned short int *)in[band])[samp];
2192  break;
2193  case Isis::SignedWord:
2194  (*out)[samp] = (double)((short int *)in[band])[samp];
2195  break;
2196  default:
2197  break;
2198  }
2199 
2200  // Sets out to isis special pixel or leaves it if valid
2201  (*out)[samp] = TestPixel((*out)[samp]);
2202 
2203  if (Isis::IsValidPixel((*out)[samp])) {
2204  (*out)[samp] = mult * ((*out)[samp]) + base;
2205  }
2206  } // End sample loop
2207 
2208  if (funct == NULL) {
2209  //Set the buffer position and write the line to the output file
2210  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
2211  OutputCubes[0]->write(*out);
2212  }
2213  else {
2214  funct(*out);
2215  }
2216 
2218 
2219  // Handle any line suffix bytes
2220  if (p_saveDataPost) {
2221  tempPost.push_back(new char[p_dataPostBytes]);
2222  memcpy(&tempPost[0], &in[band][p_dataPreBytes+p_ns*sizeofpixel],
2223  p_dataPostBytes);
2224  }
2225 
2226  // Save off the prefix bytes vector
2227  if (p_saveDataPre) {
2228  p_dataPre.push_back(tempPre);
2229  tempPre.clear();
2230  }
2231 
2232  // Save off the suffix bytes vector
2233  if (p_saveDataPost) {
2234  p_dataPost.push_back(tempPost);
2235  tempPost.clear();
2236  }
2237 
2238  } // End band loop
2239 
2240  } // End line loop
2241 
2242  // Close the file and clean up
2243  delete JP2_decoder;
2244  delete [] in;
2245  }
2246 
2247 
2248 #if 0
2249 
2256  void ProcessImport::AddLabel(Isis::Pvl &label) {
2257 
2258  label.Root();
2259 
2260  Isis::Pvl *output = OutputCubes[0]->Label();
2261  output->Find("IsisCube");
2262  output->Insert(label);
2263  }
2264 
2271  void ProcessImport::AddImportLabel(Isis::Pvl &importLab) {
2272 
2273  importLab.Root();
2274 
2275  Isis::Pvl *output = OutputCubes[0]->Label();
2276  output->Find("IsisCube");
2277  output->addObject("ImportLabel");
2278  output->Insert(importLab);
2279  }
2280 #endif
2281 
2282 
2291  void ProcessImport::SetInputFile(const QString &file) {
2292  p_inFile = file;
2293  if (!Isis::FileName(file).fileExists()) {
2294  QString msg = "File [" + file + "] does not exist";
2295  throw IException(IException::User, msg, _FILEINFO_);
2296  }
2297  }
2298 
2299 
2309  if (p_inFile.size() <= 0) {
2310  QString msg = "No input file has been set";
2312  }
2313  return p_inFile;
2314  }
2315 
2316 }
Buffer for reading and writing cube data.
Definition: Buffer.h:69
JPEG2000 decoder class.
Definition: JP2Decoder.h:98
int p_dataHeaderBytes
Number of bytes of non-image data after the file header and before the image data of each data block...
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.
static UserInterface & GetUserInterface()
Returns the UserInterface object.
File name manipulation and expansion.
Definition: FileName.h:116
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:206
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 DataSuffixBytes() const
This method returns the number of data duffix bytes.
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:61
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.
Namespace for the standard library.
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.
bool propagatePixelType() const
Return true if the pixel type is to be propagated from an 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 propagateMinimumMaximum() const
Return true if the min/max are to be propagated from an input cube.
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.
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:238
std::vector< std::vector< char * > > p_dataPost
The data suffix.
int DataTrailerBytes() const
This method returns the number of data trailer bytes.
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
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:171
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.
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:184
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...
Interleave Organization() const
Gets the organization of the input cube.
void SaveDataSuffix()
This method marks the data suffix to be saved.
int DataHeaderBytes() const
This method returns the number of data header bytes.
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:40
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:142
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:55
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.
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
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 FileHeaderBytes() const
This method returns the number of file header 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.
int DataPrefixBytes() const
This method returns the number of data prefix bytes.
ProcessImport::Interleave p_organization
The format of the input file.
Isis::Progress * p_progress
Pointer to a Progress object.
Definition: Process.h:160
char * FileHeader()
This method returns a pointer to the file header.
Isis exception class.
Definition: IException.h:107
char * FileTrailer()
This method returns a pointer to the file trailer.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void SetMultiplier(const double mult)
Sets the core multiplier of the input cube.
int FileTrailerBytes() const
This method returns the number of file trailer 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:68
void SetVAXConvert(const bool vax_convert)
Sets the VAX flag of the input cube.
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.
uint32_t Uint32_t(void *buf)
Swaps a 32bit unsigned integer.
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:158
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:170
void SaveFileHeader()
This method marks the file header to be saved.