USGS

Isis 3.0 Object Programmers' Reference

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 using namespace std;
45 namespace Isis {
46 
48  ProcessImport::ProcessImport() : Isis::Process() {
49 
50  p_progress->SetText("Importing");
51 
52  // Initialize input file information
53  p_inFile = "";
54  p_pixelType = Isis::None;
55  p_ns = p_nl = p_nb = 0;
56  p_byteOrder = Isis::NoByteOrder;
60  p_dataPreBytes = 0;
61  p_dataPostBytes = 0;
63  p_base.push_back(0.0);
64  p_mult.push_back(1.0);
65  // Make all special pixels invalid
66  SetNull(DBL_MAX, -DBL_MAX);
67  SetHRS(DBL_MAX, -DBL_MAX);
68  SetLRS(DBL_MAX, -DBL_MAX);
69  SetHIS(DBL_MAX, -DBL_MAX);
70  SetLIS(DBL_MAX, -DBL_MAX);
71 
72  p_saveFileHeader = false;
73  p_saveDataHeader = false;
74  p_saveDataTrailer = false;
75  p_saveDataPre = false;
76  p_saveDataPost = false;
77  p_saveFileTrailer = false;
78 
79  p_fileHeader = NULL;
80  p_fileTrailer = NULL;
81  }
82 
83 
86  if (p_fileHeader) {
87  delete [] p_fileHeader;
88  p_fileHeader = NULL;
89  }
90 
91  for(unsigned int i = 0; i < p_dataHeader.size(); i++) {
92  delete [] p_dataHeader[i];
93  }
94 
95  for(unsigned int i = 0; i < p_dataTrailer.size(); i++) {
96  delete [] p_dataTrailer[i];
97  }
98 
99  vector<char *> temp;
100  for(unsigned int j = 0; j < p_dataPre.size(); j++) {
101  temp = p_dataPre[j];
102  for(unsigned int i = 0; i < temp.size(); i++) delete [] temp[i];
103  }
104 
105  for(unsigned int j = 0; j < p_dataPost.size(); j++) {
106  temp = p_dataPost[j];
107  for(unsigned int i = 0; i < temp.size(); i++) delete [] temp[i];
108  }
109 
110  if (p_fileTrailer) {
111  delete p_fileTrailer;
112  }
113  }
114 
115 
125 
126  if ((type == Isis::Double) || (type == Isis::Real) || (type == Isis::SignedWord) ||
127  (type == Isis::UnsignedWord) || (type == Isis::UnsignedByte)) {
128  p_pixelType = type;
129  }
130  else {
131  QString msg = "Unsupported pixel type [" +
132  Isis::PixelTypeName(type) + "]";
134  }
135  };
136 
137 
150  void ProcessImport::SetDimensions(const int ns, const int nl, const int nb) {
151  if (ns > 0 && nl > 0 && nb > 0) {
152  p_ns = ns;
153  p_nl = nl;
154  p_nb = nb;
155  }
156  else {
157  QString msg = "Illegal dimension [" + toString(ns) + ", " +
158  toString(nl) + ", " + toString(nb) + "]";
160  }
161  };
162 
163 
171  p_byteOrder = order;
172  };
173 
174 
195  void ProcessImport::SetFileHeaderBytes(const int bytes) {
196  if (bytes >= 0) {
197  p_fileHeaderBytes = bytes;
198  }
199  else {
200  QString msg = "Illegal file header size [" + toString(bytes) + "]";
202  }
203  };
204 
205 
225  void ProcessImport::SetFileTrailerBytes(const int bytes) {
226  if (bytes >= 0) {
227  p_fileTrailerBytes = bytes;
228  }
229  else {
230  QString msg = "Illegal file trailer size [" + toString(bytes) + "]";
232  }
233  };
234 
235 
254  void ProcessImport::SetDataHeaderBytes(const int bytes) {
255  if (bytes >= 0) {
256  p_dataHeaderBytes = bytes;
257  }
258  else {
259  QString msg = "Illegal data header size [" + toString(bytes) + "]";
261  }
262  };
263 
264 
284  void ProcessImport::SetDataTrailerBytes(const int bytes) {
285 
286  if (bytes >= 0) {
287  p_dataTrailerBytes = bytes;
288  }
289  else {
290  QString msg = "Illegal data trailer size [" + toString(bytes) + "]";
292  }
293  };
294 
295 
314  void ProcessImport::SetDataPrefixBytes(const int bytes) {
315 
316  if (bytes >= 0) {
317  p_dataPreBytes = bytes;
318  }
319  else {
320  QString msg = "Illegal data prefix size [" + toString(bytes) + "]";
322  }
323  };
324 
325 
344  void ProcessImport::SetDataSuffixBytes(const int bytes) {
345 
346  if (bytes >= 0) {
347  p_dataPostBytes = bytes;
348  }
349  else {
350  QString msg = "Illegal data suffix size [" + toString(bytes) + "]";
352  }
353  };
354 
355 
374  if (p_fileHeaderBytes == 0) {
375  QString msg = "File header bytes equals 0. There is nothing to save. "
376  "Use SetFileHeaderBytes() first.";
378  }
379  p_saveFileHeader = true;
380  };
381 
382 
401  if (p_fileTrailerBytes == 0) {
402  QString msg = "File trailer bytes equals 0. There is nothing to save. "
403  "Use SetFileTrailerBytes() first.";
405  _FILEINFO_);
406  }
407  p_saveFileTrailer = true;
408  };
409 
410 
430  if (p_dataHeaderBytes == 0) {
431  QString msg = "Data header bytes equals 0. There is nothing to save. "
432  "Use SetDataHeaderBytes() first.";
434  _FILEINFO_);
435  }
436  p_saveDataHeader = true;
437  };
438 
439 
459  if (p_dataTrailerBytes == 0) {
460  QString msg = "Data trailer bytes equals 0. There is nothing to save. "
461  "Use SetDataTrailerBytes() first.";
463  _FILEINFO_);
464  }
465  p_saveDataTrailer = true;
466  };
467 
468 
487  if (p_dataPreBytes == 0) {
488  QString msg = "Data prefix bytes equals 0. There is nothing to save. "
489  "Use SetDataPrefixBytes() first.";
491  _FILEINFO_);
492  }
493  p_saveDataPre = true;
494  };
495 
496 
516  if (p_dataPostBytes == 0) {
517  QString msg = "Data suffix bytes equals 0. There is nothing to save. "
518  "Use SetDataSuffixBytes() first.";
520  _FILEINFO_);
521  }
522  p_saveDataPost = true;
523  };
524 
525 
530  return p_fileHeaderBytes;
531  }
532 
533 
538  return p_fileTrailerBytes;
539  }
540 
541 
546  return p_dataHeaderBytes;
547  }
548 
549 
554  return p_fileTrailerBytes;
555  }
556 
557 
562  return p_dataPreBytes;
563  }
564 
565 
570  return p_dataPostBytes;
571  }
572 
573 
593  if (p_saveFileHeader) {
594  return p_fileHeader;
595  }
596  QString msg = "File header was not saved. Use SaveFileHeader().";
598  };
599 
600 
619  if (p_saveFileTrailer) {
620  return p_fileTrailer;
621  }
622  QString msg = "File trailer was not saved. Use SaveFileTrailer()";
624  };
625 
626 
646  std::vector<char *> ProcessImport::DataHeader() {
647  if (p_saveDataHeader) {
648  return p_dataHeader;
649  }
650  QString msg = "Data header was not saved. Use SaveDataHeader()";
652  };
653 
654 
674  std::vector<char *> ProcessImport::DataTrailer() {
675  if (p_saveDataTrailer) {
676  return p_dataTrailer;
677  }
678  QString msg = "Data trailer was not saved. Use SaveDataTrailer()";
680  };
681 
682 
702  std::vector<std::vector<char *> > ProcessImport::DataPrefix() {
703  if (p_saveDataPre) {
704  return p_dataPre;
705  }
706  QString msg = "Data prefix was not saved. Use SaveDataPrefix()";
708  };
709 
710 
730  std::vector<std::vector<char *> > ProcessImport::DataSuffix() {
731  if (p_saveDataPost) {
732  return p_dataPost;
733  }
734  QString msg = "Data suffix was not saved. Use SaveDataSuffix()";
736  };
737 
738 
747  p_organization = org;
748  };
749 
750 
756  return p_organization;
757  };
758 
759 
765  void ProcessImport::SetBase(const double base) {
766  p_base.clear();
767  p_base.push_back(base);
768  };
769 
770 
776  void ProcessImport::SetBase(const std::vector<double> base) {
777  p_base = base;
778  };
779 
780 
786  void ProcessImport::SetMultiplier(const double mult) {
787  p_mult.clear();
788  p_mult.push_back(mult);
789  };
790 
791 
797  void ProcessImport::SetMultiplier(const std::vector<double> mult) {
798  p_mult = mult;
799  };
800 
801 
820  void ProcessImport::SetSpecialValues(const double null, const double lrs,
821  const double lis, const double hrs,
822  const double his) {
823  SetNull(null, null);
824  SetLRS(lrs, lrs);
825  SetLIS(lis, lis);
826  SetHRS(hrs, hrs);
827  SetHIS(his, his);
828  };
829 
830 
841  void ProcessImport::SetNull(const double null_min, const double null_max) {
842  CheckPixelRange("Null", null_min, null_max);
843  p_null_min = null_min;
844  p_null_max = null_max;
845  }
846 
847 
858  void ProcessImport::SetLRS(const double lrs_min, const double lrs_max) {
859  CheckPixelRange("LRS", lrs_min, lrs_max);
860  p_lrs_min = lrs_min;
861  p_lrs_max = lrs_max;
862  }
863 
864 
875  void ProcessImport::SetLIS(const double lis_min, const double lis_max) {
876  CheckPixelRange("LIS", lis_min, lis_max);
877  p_lis_min = lis_min;
878  p_lis_max = lis_max;
879  }
880 
881 
892  void ProcessImport::SetHRS(const double hrs_min, const double hrs_max) {
893  CheckPixelRange("HRS", hrs_min, hrs_max);
894  p_hrs_min = hrs_min;
895  p_hrs_max = hrs_max;
896  }
897 
898 
909  void ProcessImport::SetHIS(const double his_min, const double his_max) {
910  CheckPixelRange("HIS", his_min, his_max);
911  p_his_min = his_min;
912  p_his_max = his_max;
913  }
914 
915 
925  void ProcessImport::CheckPixelRange(QString pixelName, double pixelMin,
926  double pixelMax) {
927  if (pixelMin == DBL_MAX || pixelMax == -DBL_MAX) return;
928 
929  if (p_null_min != DBL_MAX && p_null_max != -DBL_MAX && ( //-null has been set
930  (pixelMin > p_null_min && pixelMin < p_null_max) || // --min crossing
931  (pixelMax > p_null_min && pixelMax < p_null_max) || // --max crossing
932  (pixelMin < p_null_min && pixelMax > p_null_max))) { // --straddling
933  // values
934  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
935  "," + toString(pixelMax) + "] overlaps the NULL range [" +
936  toString(p_null_min) + "," + toString(p_null_max) + "]";
938  }
939 
940  if (p_lrs_min != DBL_MAX && p_lrs_max != -DBL_MAX && (
941  (pixelMin > p_lrs_min && pixelMin < p_lrs_max) ||
942  (pixelMax > p_lrs_min && pixelMax < p_lrs_max) ||
943  (pixelMin < p_lrs_min && pixelMax > p_lrs_max))) {
944  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
945  "," + toString(pixelMax) + "] overlaps the LRS range [" +
946  toString(p_lrs_min) + "," + toString(p_lrs_max) + "]";
948  }
949 
950  if (p_lis_min != DBL_MAX && p_lis_max != -DBL_MAX && (
951  (pixelMin > p_lis_min && pixelMin < p_lis_max) ||
952  (pixelMax > p_lis_min && pixelMax < p_lis_max) ||
953  (pixelMin < p_lis_min && pixelMax > p_lis_max))) {
954  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
955  "," + toString(pixelMax) + "] overlaps the LIS range [" +
956  toString(p_lis_min) + "," + toString(p_lis_max) + "]";
958  }
959 
960  if (p_hrs_min != DBL_MAX && p_hrs_max != -DBL_MAX && (
961  (pixelMin > p_hrs_min && pixelMin < p_hrs_max) ||
962  (pixelMax > p_hrs_min && pixelMax < p_hrs_max) ||
963  (pixelMin < p_hrs_min && pixelMax > p_hrs_max))) {
964  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
965  "," + toString(pixelMax) + "] overlaps the HRS range [" +
966  toString(p_hrs_min) + "," + toString(p_hrs_max) + "]";
968  }
969 
970  if (p_his_min != DBL_MAX && p_his_max != -DBL_MAX && (
971  (pixelMin > p_his_min && pixelMin < p_his_max) ||
972  (pixelMax > p_his_min && pixelMax < p_his_max) ||
973  (pixelMin < p_his_min && pixelMax > p_his_max))) {
974  QString msg = "The " + pixelName + " range [" + toString(pixelMin) +
975  "," + toString(pixelMax) + "] overlaps the HIS range [" +
976  toString(p_his_min) + "," + toString(p_his_max) + "]";
978  }
979 
980  }
981 
982 
994  double ProcessImport::TestPixel(const double pixel) {
995  if (pixel <= p_null_max && pixel >= p_null_min) {
996  return Isis::NULL8;
997  }
998  else if (pixel <= p_hrs_max && pixel >= p_hrs_min) {
999  return Isis::HIGH_REPR_SAT8;
1000  }
1001  else if (pixel <= p_lrs_max && pixel >= p_lrs_min) {
1002  return Isis::LOW_REPR_SAT8;
1003  }
1004  else if (pixel <= p_his_max && pixel >= p_his_min) {
1005  return Isis::HIGH_INSTR_SAT8;
1006  }
1007  else if (pixel <= p_lis_max && pixel >= p_lis_min) {
1008  return Isis::LOW_INSTR_SAT8;
1009  }
1010  else {
1011  return pixel;
1012  }
1013  }
1014 
1015 
1026  Isis::Cube *ProcessImport::SetOutputCube(const QString &parameter) {
1027  CubeAttributeOutput &att =
1029 
1030  if (att.propagateMinimumMaximum()) {
1031  double min, max;
1032  if ((p_pixelType == Isis::Double) ||
1033  (p_pixelType == Isis::Real) ||
1034  (p_base.size() > 1) || (p_mult.size() > 1)) {
1035  min = Isis::VALID_MIN4;
1036  max = Isis::VALID_MAX4;
1037  }
1038  else if (p_pixelType == Isis::SignedWord) {
1039  min = Isis::VALID_MIN2 * p_mult[0] + p_base[0];
1040  max = Isis::VALID_MAX2 * p_mult[0] + p_base[0];
1041  }
1042  else if (p_pixelType == Isis::UnsignedWord) {
1043  min = Isis::VALID_MINU2 * p_mult[0] + p_base[0];
1044  max = Isis::VALID_MAXU2 * p_mult[0] + p_base[0];
1045  }
1046  else if (p_pixelType == Isis::UnsignedByte) {
1047  min = Isis::VALID_MIN1 * p_mult[0] + p_base[0];
1048  max = Isis::VALID_MAX1 * p_mult[0] + p_base[0];
1049  }
1050  else {
1051  QString msg = "Unsupported pixel type [" +
1054  }
1055  att.setMinimum(min);
1056  att.setMaximum(max);
1057  }
1058 
1059  if (att.propagatePixelType()) {
1060  if ((p_base.size() > 1) || (p_mult.size() > 1)) {
1061  att.setPixelType(Isis::Real);
1062  }
1063  else if (p_pixelType == Isis::UnsignedWord || p_pixelType == Isis::Double) {
1064  att.setPixelType(Isis::Real);
1065  }
1066  else {
1068  }
1069  }
1070 
1071  return Process::SetOutputCube(Application::GetUserInterface().GetFileName(parameter), att, p_ns, p_nl, p_nb);
1072  }
1073 
1074 
1090  return Isis::Process::SetOutputCube(fname, att, p_ns, p_nl, p_nb);
1091  }
1092 
1093 
1097  ProcessJp2();
1098  }
1099  else if (p_organization == ProcessImport::BSQ) {
1100  ProcessBsq();
1101  }
1102  else if (p_organization == ProcessImport::BIL) {
1103  ProcessBil();
1104  }
1105  else if (p_organization == ProcessImport::BIP) {
1106  ProcessBip();
1107  }
1108  else {
1109  QString msg = "File [" + p_inFile
1110  + "] is not in a supported organization.";
1112  }
1113  }
1114 
1115 
1127  ProcessJp2(funct);
1128  }
1129  else if (p_organization == ProcessImport::BSQ) {
1130  ProcessBsq(funct);
1131  }
1132  else if (p_organization == ProcessImport::BIL) {
1133  ProcessBil(funct);
1134  }
1135  else if (p_organization == ProcessImport::BIP) {
1136  ProcessBip(funct);
1137  }
1138  else {
1139  QString msg = "File [" + p_inFile + "] is not in a supported organization.";
1141  }
1142  }
1143 
1144 
1156  void ProcessImport::ProcessBsq(void funct(Isis::Buffer &out)) {
1157  // Figure out the number of bytes to read for a single line
1158  int readBytes = Isis::SizeOf(p_pixelType);
1159  readBytes = readBytes * p_ns;
1160  char *in = new char [readBytes];
1161 
1162  // Set up an Isis::EndianSwapper object
1163  QString tok(Isis::ByteOrderName(p_byteOrder));
1164  tok = tok.toUpper();
1165  Isis::EndianSwapper swapper(tok);
1166 
1167  ifstream fin;
1168  // Open input file
1169  Isis::FileName inFile(p_inFile);
1170  QString inFileName(inFile.expanded());
1171  fin.open(inFileName.toAscii().data(), ios::in | ios::binary);
1172  if (!fin.is_open()) {
1173  QString msg = "Cannot open input file [" + p_inFile + "]";
1174  throw IException(IException::Io, msg, _FILEINFO_);
1175  }
1176 
1177  // Handle the file header
1178  streampos pos = fin.tellg();
1179  if (p_saveFileHeader) {
1180  p_fileHeader = new char[p_fileHeaderBytes];
1181  fin.read(p_fileHeader, p_fileHeaderBytes);
1182  }
1183  else {
1184  fin.seekg(p_fileHeaderBytes, ios_base::beg);
1185  }
1186 
1187  // Check the last io
1188  if (!fin.good()) {
1189  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1190  toString((int)pos) + "]. Byte count [" +
1191  toString(p_fileHeaderBytes) + "]" ;
1192  throw IException(IException::Io, msg, _FILEINFO_);
1193  }
1194 
1195  // Construct a line buffer manager
1196  Isis::Buffer *out = NULL;
1197 
1198  if (funct != NULL) {
1199  out = new Isis::Brick(p_ns, 1, 1, p_pixelType);
1200  }
1201  else {
1202  out = new Isis::LineManager(*OutputCubes[0]);
1203  }
1204 
1205  // Loop once for each band in the image
1208 
1209  // Loop for each band
1210  for(int band = 0; band < p_nb; band++) {
1211  // Set the base multiplier
1212  double base, mult;
1213  if (p_base.size() > 1) {
1214  base = p_base[band];
1215  mult = p_mult[band];
1216  }
1217  else {
1218  base = p_base[0];
1219  mult = p_mult[0];
1220  }
1221 
1222  // Handle any data headers (e.g., the data at the beginning of each band)
1223  pos = fin.tellg();
1224  if (p_saveDataHeader) {
1225  p_dataHeader.push_back(new char[p_dataHeaderBytes]);
1226  fin.read(p_dataHeader.back(), p_dataHeaderBytes);
1227  }
1228  else {
1229  fin.seekg(p_dataHeaderBytes, ios_base::cur);
1230  }
1231 
1232  // Check the last io
1233  if (!fin.good()) {
1234  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1235  toString((int)pos) + "]. Byte count [" +
1236  toString(p_dataHeaderBytes) + "]" ;
1237  throw IException(IException::Io, msg, _FILEINFO_);
1238  }
1239 
1240  // Space for storing prefix and suffix data pointers
1241  vector<char *> tempPre, tempPost;
1242 
1243  // Loop for each line in a band
1244  for(int line = 0; line < p_nl; line++) {
1245 
1246  // Handle any line prefix bytes
1247  pos = fin.tellg();
1248  if (p_saveDataPre) {
1249  tempPre.push_back(new char[p_dataPreBytes]);
1250  fin.read(tempPre.back(), p_dataPreBytes);
1251  }
1252  else {
1253  fin.seekg(p_dataPreBytes, ios_base::cur);
1254  }
1255 
1256  // Check the last io
1257  if (!fin.good()) {
1258  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1259  toString((int)pos) + "]. Byte count [" +
1260  toString(p_dataPreBytes) + "]" ;
1261  throw IException(IException::Io, msg, _FILEINFO_);
1262  }
1263 
1264 
1265  // Get a line of data from the input file
1266  pos = fin.tellg();
1267  fin.read(in, readBytes);
1268  if (!fin.good()) {
1269  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1270  toString((int)pos) + "]. Byte count [" +
1271  toString(readBytes) + "]" ;
1272  throw IException(IException::Io, msg, _FILEINFO_);
1273  }
1274 
1275  // Swap the bytes if necessary and convert any out of bounds pixels
1276  // to special pixels
1277  for(int samp = 0; samp < p_ns; samp++) {
1278  switch(p_pixelType) {
1279  case Isis::UnsignedByte:
1280  (*out)[samp] = (double)((unsigned char *)in)[samp];
1281  break;
1282  case Isis::UnsignedWord:
1283  (*out)[samp] =
1284  (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1285  break;
1286  case Isis::SignedWord:
1287  (*out)[samp] = (double)swapper.ShortInt((short int *)in+samp);
1288  break;
1289  case Isis::Real:
1290  (*out)[samp] = (double)swapper.Float((float *)in+samp);
1291  break;
1292  case Isis::Double:
1293  (*out)[samp] = (double)swapper.Double((double *)in+samp);
1294  break;
1295  default:
1296  break;
1297  }
1298 
1299  // Sets out to isis special pixel or leaves it if valid
1300  (*out)[samp] = TestPixel((*out)[samp]);
1301 
1302  if (Isis::IsValidPixel((*out)[samp])) {
1303  (*out)[samp] = mult * ((*out)[samp]) + base;
1304  }
1305  } // End sample loop
1306 
1307  if (funct == NULL) {
1308  // Set the buffer position and write the line to the output file
1309  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1310  OutputCubes[0]->write(*out);
1311  }
1312  else {
1313  ((Isis::Brick *)out)->SetBaseSample(1);
1314  ((Isis::Brick *)out)->SetBaseLine(line + 1);
1315  ((Isis::Brick *)out)->SetBaseBand(band + 1);
1316  funct(*out);
1317  }
1318 
1320 
1321  // Handle any line suffix bytes
1322  pos = fin.tellg();
1323  if (p_saveDataPost) {
1324  tempPost.push_back(new char[p_dataPostBytes]);
1325  fin.read(tempPost.back(), p_dataPostBytes);
1326  }
1327  else {
1328  fin.seekg(p_dataPostBytes, ios_base::cur);
1329  }
1330 
1331  // Check the last io
1332  if (!fin.good()) {
1333  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1334  toString((int)pos) + "]. Byte count [" +
1335  toString(p_dataPreBytes) + "]" ;
1336  throw IException(IException::Io, msg, _FILEINFO_);
1337  }
1338  } // End line loop
1339 
1340  // Save off the prefix bytes vector
1341  if (p_saveDataPre) {
1342  p_dataPre.push_back(tempPre);
1343  tempPre.clear();
1344  }
1345 
1346  // Save off the suffix bytes vector
1347  if (p_saveDataPost) {
1348  p_dataPost.push_back(tempPost);
1349  tempPost.clear();
1350  }
1351 
1352  // Handle the band trailer
1353  pos = fin.tellg();
1354  if (p_saveDataTrailer) {
1355  p_dataTrailer.push_back(new char[p_dataTrailerBytes]);
1356  fin.read(p_dataTrailer.back(), p_dataTrailerBytes);
1357  }
1358  else {
1359  fin.seekg(p_dataTrailerBytes, ios_base::cur);
1360  }
1361 
1362  // Check the last io
1363  if (!fin.good()) {
1364  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1365  toString((int)pos) + "]. Byte count [" +
1366  toString(p_fileHeaderBytes) + "]" ;
1367  throw IException(IException::Io, msg, _FILEINFO_);
1368  }
1369 
1370  } // End band loop
1371 
1372  // Handle the file trailer
1373  pos = fin.tellg();
1374  if (p_saveFileTrailer) {
1375  fin.seekg(0, ios_base::end);
1376  streampos e = fin.tellg();
1377  p_fileTrailerBytes = (int)(e - pos + (streampos)1);
1378  p_fileTrailer = new char[p_fileTrailerBytes];
1379  fin.seekg(pos);
1380  fin.read(p_fileTrailer, p_fileTrailerBytes);
1381 
1382  // Check the io
1383  if (!fin.good()) {
1384  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1385  toString((int)pos) + "]. Byte count [" +
1386  toString(p_fileTrailerBytes) + "]" ;
1387  throw IException(IException::Io, msg, _FILEINFO_);
1388  }
1389 
1390  }
1391 
1392  // Close the file and clean up
1393  fin.close();
1394  delete [] in;
1395  }
1396 
1397 
1408  void ProcessImport::ProcessBil(void funct(Isis::Buffer &someBuf)) {
1409 
1410  // Figure out the number of bytes to read for a single line
1411  int readBytes = Isis::SizeOf(p_pixelType);
1412  readBytes = readBytes * p_ns;
1413  char *in = new char [readBytes];
1414 
1415  // Set up an Isis::EndianSwapper object
1416  QString tok(Isis::ByteOrderName(p_byteOrder));
1417  tok = tok.toUpper();
1418  Isis::EndianSwapper swapper(tok);
1419 
1420  ifstream fin;
1421  // Open input file
1422  Isis::FileName inFile(p_inFile);
1423  QString inFileName(inFile.expanded());
1424  fin.open(inFileName.toAscii().data(), ios::in | ios::binary);
1425  if (!fin.is_open()) {
1426  QString msg = "Cannot open input file [" + p_inFile + "]";
1427  throw IException(IException::Io, msg, _FILEINFO_);
1428  }
1429 
1430  // Handle the file header
1431  streampos pos = fin.tellg();
1432  if (p_saveFileHeader) {
1433  p_fileHeader = new char[p_fileHeaderBytes];
1434  fin.read(p_fileHeader, p_fileHeaderBytes);
1435  }
1436  else {
1437  fin.seekg(p_fileHeaderBytes, ios_base::beg);
1438  }
1439 
1440  // Check the last io
1441  if (!fin.good()) {
1442  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1443  toString((int)pos) + "]. Byte count [" +
1444  toString(p_fileHeaderBytes) + "]" ;
1445  throw IException(IException::Io, msg, _FILEINFO_);
1446  }
1447 
1448  // Construct a line buffer manager
1449  Isis::Buffer *out = NULL;
1450 
1451  if (funct != NULL) {
1452  out = new Isis::Brick(p_ns, p_nl, p_nb, p_ns, 1, 1, p_pixelType, true);
1453  ((Isis::Brick *)out)->setpos(0);
1454  }
1455  else {
1456  OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
1457  out = new Isis::LineManager(*OutputCubes[0]);
1458  }
1459 
1460  // Loop once for each line in the image
1463 
1464  // Loop for each line
1465  for(int line = 0; line < p_nl; line++) {
1466  // Loop for each band
1467  for(int band = 0; band < p_nb; band++) {
1468  // Set the base multiplier
1469  double base, mult;
1470  if (p_base.size() > 1) {
1471  base = p_base[band];
1472  mult = p_mult[band];
1473  }
1474  else {
1475  base = p_base[0];
1476  mult = p_mult[0];
1477  }
1478 
1479  // Check the last io
1480  if (!fin.good()) {
1481  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1482  toString((int)pos) + "]. Byte count [" +
1483  toString(p_dataHeaderBytes) + "]" ;
1484  throw IException(IException::Io, msg, _FILEINFO_);
1485  }
1486 
1487  // Space for storing prefix and suffix data pointers
1488  vector<char *> tempPre, tempPost;
1489 
1490 
1491  // Handle any line prefix bytes
1492  pos = fin.tellg();
1493  if (p_saveDataPre) {
1494  tempPre.push_back(new char[p_dataPreBytes]);
1495  fin.read(tempPre.back(), p_dataPreBytes);
1496  }
1497  else {
1498  fin.seekg(p_dataPreBytes, ios_base::cur);
1499  }
1500 
1501  // Check the last io
1502  if (!fin.good()) {
1503  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1504  toString((int)pos) + "]. Byte count [" +
1505  toString(p_dataPreBytes) + "]" ;
1506  throw IException(IException::Io, msg, _FILEINFO_);
1507  }
1508 
1509 
1510  // Get a line of data from the input file
1511  pos = fin.tellg();
1512  fin.read(in, readBytes);
1513  if (!fin.good()) {
1514  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1515  toString((int)pos) + "]. Byte count [" +
1516  toString(readBytes) + "]" ;
1517  throw IException(IException::Io, msg, _FILEINFO_);
1518  }
1519 
1520  // Swap the bytes if necessary and convert any out of bounds pixels
1521  // to special pixels
1522  for(int samp = 0; samp < p_ns; samp++) {
1523  switch(p_pixelType) {
1524  case Isis::UnsignedByte:
1525  (*out)[samp] = (double)((unsigned char *)in)[samp];
1526  break;
1527  case Isis::UnsignedWord:
1528  (*out)[samp] =
1529  (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1530  break;
1531  case Isis::SignedWord:
1532  (*out)[samp] = (double)swapper.ShortInt((short int *)in+samp);
1533  break;
1534  case Isis::Real:
1535  (*out)[samp] = (double)swapper.Float((float *)in+samp);
1536  break;
1537  case Isis::Double:
1538  (*out)[samp] = (double)swapper.Double((double *)in+samp);
1539  break;
1540  default:
1541  break;
1542  }
1543 
1544  // Sets out to isis special pixel or leaves it if valid
1545  (*out)[samp] = TestPixel((*out)[samp]);
1546 
1547  if (Isis::IsValidPixel((*out)[samp])) {
1548  (*out)[samp] = mult * ((*out)[samp]) + base;
1549  }
1550  } // End sample loop
1551 
1552  if (funct == NULL) {
1553  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1554  OutputCubes[0]->write(*out);
1555  }
1556  else {
1557  funct(*out);
1558  (*((Isis::Brick *)out))++;
1559  }
1560 
1562 
1563  // Handle any line suffix bytes
1564  pos = fin.tellg();
1565  if (p_saveDataPost) {
1566  tempPost.push_back(new char[p_dataPostBytes]);
1567  fin.read(tempPost.back(), p_dataPostBytes);
1568  }
1569  else {
1570  fin.seekg(p_dataPostBytes, ios_base::cur);
1571  }
1572 
1573  // Check the last io
1574  if (!fin.good()) {
1575  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1576  toString((int)pos) + "]. Byte count [" +
1577  toString(p_dataPreBytes) + "]" ;
1578  throw IException(IException::Io, msg, _FILEINFO_);
1579  }
1580 
1581 
1582  // Save off the prefix bytes vector
1583  if (p_saveDataPre) {
1584  p_dataPre.push_back(tempPre);
1585  tempPre.clear();
1586  }
1587 
1588  // Save off the suffix bytes vector
1589  if (p_saveDataPost) {
1590  p_dataPost.push_back(tempPost);
1591  tempPost.clear();
1592  }
1593 
1594  // Check the last io
1595  if (!fin.good()) {
1596  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1597  toString((int)pos) + "]. Byte count [" +
1598  toString(p_fileHeaderBytes) + "]" ;
1599  throw IException(IException::Io, msg, _FILEINFO_);
1600  }
1601 
1602  } // End band loop
1603 
1604  } // End line loop
1605 
1606  // Handle the file trailer
1607  pos = fin.tellg();
1608  if (p_saveFileTrailer) {
1609  fin.seekg(0, ios_base::end);
1610  streampos e = fin.tellg();
1611  p_fileTrailerBytes = (int)(e - pos + (streampos)1);
1612  p_fileTrailer = new char[p_fileTrailerBytes];
1613  fin.seekg(pos);
1614  fin.read(p_fileTrailer, p_fileTrailerBytes);
1615 
1616  // Check the io
1617  if (!fin.good()) {
1618  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1619  toString((int)pos) + "]. Byte count [" +
1620  toString(p_fileTrailerBytes) + "]" ;
1621  throw IException(IException::Io, msg, _FILEINFO_);
1622  }
1623 
1624  }
1625 
1626  // Close the file and clean up
1627  fin.close();
1628  delete [] in;
1629 
1630  }
1631 
1632 
1643  void ProcessImport::ProcessBip(void funct(Isis::Buffer &out)) {
1644 
1645  // Figure out the number of bytes to read for a single line
1646  int readBytes = Isis::SizeOf(p_pixelType);
1647  readBytes = readBytes * p_ns * p_nb;
1648  char *in = new char [readBytes];
1649 
1650  // Set up an Isis::EndianSwapper object
1651  QString tok(Isis::ByteOrderName(p_byteOrder));
1652  tok = tok.toUpper();
1653  Isis::EndianSwapper swapper(tok);
1654 
1655  ifstream fin;
1656  // Open input file
1657  Isis::FileName inFile(p_inFile);
1658  QString inFileName(inFile.expanded());
1659  fin.open(inFileName.toAscii().data(), ios::in | ios::binary);
1660  if (!fin.is_open()) {
1661  QString msg = "Cannot open input file [" + p_inFile + "]";
1662  throw IException(IException::Io, msg, _FILEINFO_);
1663  }
1664 
1665  // Handle the file header
1666  streampos pos = fin.tellg();
1667  if (p_saveFileHeader) {
1668  p_fileHeader = new char[p_fileHeaderBytes];
1669  fin.read(p_fileHeader, p_fileHeaderBytes);
1670  }
1671  else {
1672  fin.seekg(p_fileHeaderBytes, ios_base::beg);
1673  }
1674 
1675  // Check the last io
1676  if (!fin.good()) {
1677  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1678  toString((int)pos) + "]. Byte count [" +
1679  toString(p_fileHeaderBytes) + "]" ;
1680  throw IException(IException::Io, msg, _FILEINFO_);
1681  }
1682 
1683  OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
1684 
1685  // Construct a line buffer manager
1686  Isis::Buffer *out = NULL;
1687 
1688  if (funct != NULL) {
1689  out = new Isis::Buffer(p_ns, p_nl, p_nb, p_pixelType);
1690  }
1691  else {
1692  out = new Isis::LineManager(*OutputCubes[0]);
1693  }
1694 
1695  // Loop once for each line in the image
1698 
1699  // Loop for each line
1700  for(int line = 0; line < p_nl; line++) {
1701  // Check the last io
1702  if (!fin.good()) {
1703  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1704  toString((int)pos) + "]. Byte count [" +
1705  toString(p_dataHeaderBytes) + "]" ;
1706  throw IException(IException::Io, msg, _FILEINFO_);
1707  }
1708 
1709  // Space for storing prefix and suffix data pointers
1710  vector<char *> tempPre, tempPost;
1711 
1712 
1713  // Handle any line prefix bytes
1714  pos = fin.tellg();
1715  if (p_saveDataPre) {
1716  tempPre.push_back(new char[p_dataPreBytes]);
1717  fin.read(tempPre.back(), p_dataPreBytes);
1718  }
1719  else {
1720  fin.seekg(p_dataPreBytes, ios_base::cur);
1721  }
1722 
1723  // Check the last io
1724  if (!fin.good()) {
1725  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1726  toString((int)pos) + "]. Byte count [" +
1727  toString(p_dataPreBytes) + "]" ;
1728  throw IException(IException::Io, msg, _FILEINFO_);
1729  }
1730 
1731  // Get a line of data from the input file
1732  pos = fin.tellg();
1733  fin.read(in, readBytes);
1734  if (!fin.good()) {
1735  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1736  toString((int)pos) + "]. Byte count [" +
1737  toString(readBytes) + "]" ;
1738  throw IException(IException::Io, msg, _FILEINFO_);
1739  }
1740 
1741  // Loop for each band
1742  for(int band = 0; band < p_nb; band++) {
1743  // Set the base multiplier
1744  double base, mult;
1745  if (p_base.size() > 1) {
1746  base = p_base[band];
1747  mult = p_mult[band];
1748  }
1749  else {
1750  base = p_base[0];
1751  mult = p_mult[0];
1752  }
1753 
1754  // Swap the bytes if necessary and convert any out of bounds pixels
1755  // to special pixels
1756  int osamp = 0;
1757  for(int samp = band; samp < p_ns * p_nb; samp += p_nb) {
1758  switch(p_pixelType) {
1759  case Isis::UnsignedByte:
1760  (*out)[osamp] = (double)((unsigned char *)in)[samp];
1761  break;
1762  case Isis::UnsignedWord:
1763  (*out)[osamp] =
1764  (double)swapper.UnsignedShortInt((unsigned short int *)in+samp);
1765  break;
1766  case Isis::SignedWord:
1767  (*out)[osamp] = (double)swapper.ShortInt((short int *)in+samp);
1768  break;
1769  case Isis::Real:
1770  (*out)[osamp] = (double)swapper.Float((float *)in+samp);
1771  break;
1772  case Isis::Double:
1773  (*out)[samp] = (double)swapper.Double((double *)in+samp);
1774  break;
1775  default:
1776  break;
1777  }
1778 
1779  // Sets out to isis special pixel or leaves it if valid
1780  (*out)[osamp] = TestPixel((*out)[osamp]);
1781 
1782  if (Isis::IsValidPixel((*out)[osamp])) {
1783  (*out)[osamp] = mult * ((*out)[osamp]) + base;
1784  }
1785  osamp++;
1786  } // End sample loop
1787 
1788  if (funct == NULL) {
1789  //Set the buffer position and write the line to the output file
1790  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1791  OutputCubes[0]->write(*out);
1792  }
1793  else {
1794  funct(*out);
1795  }
1796 
1797  // Handle any line suffix bytes
1798  pos = fin.tellg();
1799  if (p_saveDataPost) {
1800  tempPost.push_back(new char[p_dataPostBytes]);
1801  fin.read(tempPost.back(), p_dataPostBytes);
1802  }
1803  else {
1804  fin.seekg(p_dataPostBytes, ios_base::cur);
1805  }
1806 
1807  // Check the last io
1808  if (!fin.good()) {
1809  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1810  toString((int)pos) + "]. Byte count [" +
1811  toString(p_dataPreBytes) + "]" ;
1812  throw IException(IException::Io, msg, _FILEINFO_);
1813  }
1814 
1815 
1816  // Save off the prefix bytes vector
1817  if (p_saveDataPre) {
1818  p_dataPre.push_back(tempPre);
1819  tempPre.clear();
1820  }
1821 
1822  // Save off the suffix bytes vector
1823  if (p_saveDataPost) {
1824  p_dataPost.push_back(tempPost);
1825  tempPost.clear();
1826  }
1827 
1828  // Check the last io
1829  if (!fin.good()) {
1830  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1831  toString((int)pos) + "]. Byte count [" +
1832  toString(p_fileHeaderBytes) + "]" ;
1833  throw IException(IException::Io, msg, _FILEINFO_);
1834  }
1835 
1836  } // End band loop
1837 
1839 
1840  } // End line loop
1841 
1842  // Handle the file trailer
1843  pos = fin.tellg();
1844  if (p_saveFileTrailer) {
1845  fin.seekg(0, ios_base::end);
1846  streampos e = fin.tellg();
1847  p_fileTrailerBytes = (int)(e - pos + (streampos)1);
1848  p_fileTrailer = new char[p_fileTrailerBytes];
1849  fin.seekg(pos);
1850  fin.read(p_fileTrailer, p_fileTrailerBytes);
1851 
1852  // Check the io
1853  if (!fin.good()) {
1854  QString msg = "Cannot read file [" + p_inFile + "]. Position [" +
1855  toString((int)pos) + "]. Byte count [" +
1856  toString(p_fileTrailerBytes) + "]" ;
1857  throw IException(IException::Io, msg, _FILEINFO_);
1858  }
1859 
1860  }
1861 
1862  // Close the file and clean up
1863  fin.close();
1864  delete [] in;
1865 
1866  }
1867 
1868 
1884  void ProcessImport::ProcessJp2(void funct(Isis::Buffer &out)) {
1885 
1886  // Set up an Isis::Jp2Decoder object
1887  JP2Decoder *JP2_decoder;
1888  JP2_decoder = new JP2Decoder(p_inFile);
1889 
1890  // Open JP2 file
1891  JP2_decoder->OpenFile();
1892 
1893  // Make sure JP2 file dimensions match PDS labels
1894  p_ns = JP2_decoder->GetSampleDimension();
1895  p_nl = JP2_decoder->GetLineDimension();
1896  p_nb = JP2_decoder->GetBandDimension();
1897 
1898  // Figure out the number of bytes to read for a single line
1899  // from all bands
1900  int sizeofpixel = Isis::SizeOf(p_pixelType);
1901  int startsamp = p_dataPreBytes / sizeofpixel;
1902  int endsamp = startsamp + p_ns;
1903  int readBytes = sizeofpixel * p_ns * p_nb
1905  char **in = new char* [p_nb];
1906  for(int i = 0; i < p_nb; i++) {
1907  in[i] = new char [readBytes];
1908  }
1909 
1910  // Construct a line buffer manager
1911  Isis::Buffer *out = NULL;
1912 
1913  if (funct != NULL) {
1914  out = new Isis::Buffer(p_ns, p_nl, p_nb, p_pixelType);
1915  }
1916  else {
1917  out = new Isis::LineManager(*OutputCubes[0]);
1918  }
1919 
1920  // Loop once for each line in the image
1921  p_progress->SetMaximumSteps(p_nb * p_nl);
1923 
1924  // Loop for each line
1925  for(int line = 0; line < p_nl; line++) {
1926  if (p_pixelType == Isis::UnsignedByte) {
1927  JP2_decoder->Read((unsigned char **)in);
1928  }
1929  else {
1930  JP2_decoder->Read((short int **)in);
1931  }
1932  // Loop for each band
1933  for(int band = 0; band < p_nb; band++) {
1934  // Set the base multiplier
1935  double base, mult;
1936  if (p_base.size() > 1) {
1937  base = p_base[band];
1938  mult = p_mult[band];
1939  }
1940  else {
1941  base = p_base[0];
1942  mult = p_mult[0];
1943  }
1944 
1945  // Space for storing prefix and suffix data pointers
1946  vector<char *> tempPre, tempPost;
1947 
1948 
1949  // Handle any line prefix bytes
1950  if (p_saveDataPre) {
1951  tempPre.push_back(new char[p_dataPreBytes]);
1952  memcpy(&tempPre[0], in[band], p_dataPreBytes);
1953  }
1954 
1955  // Swap the bytes if necessary and convert any out of bounds pixels
1956  // to special pixels
1957  for(int samp = startsamp; samp < endsamp; samp++) {
1958  switch(p_pixelType) {
1959  case Isis::UnsignedByte:
1960  (*out)[samp] = (double)((unsigned char *)in[band])[samp];
1961  break;
1962  case Isis::UnsignedWord:
1963  (*out)[samp] = (double)((unsigned short int *)in[band])[samp];
1964  break;
1965  case Isis::SignedWord:
1966  (*out)[samp] = (double)((short int *)in[band])[samp];
1967  break;
1968  default:
1969  break;
1970  }
1971 
1972  // Sets out to isis special pixel or leaves it if valid
1973  (*out)[samp] = TestPixel((*out)[samp]);
1974 
1975  if (Isis::IsValidPixel((*out)[samp])) {
1976  (*out)[samp] = mult * ((*out)[samp]) + base;
1977  }
1978  } // End sample loop
1979 
1980  if (funct == NULL) {
1981  //Set the buffer position and write the line to the output file
1982  ((Isis::LineManager *)out)->SetLine((band * p_nl) + line + 1);
1983  OutputCubes[0]->write(*out);
1984  }
1985  else {
1986  funct(*out);
1987  }
1988 
1990 
1991  // Handle any line suffix bytes
1992  if (p_saveDataPost) {
1993  tempPost.push_back(new char[p_dataPostBytes]);
1994  memcpy(&tempPost[0], &in[band][p_dataPreBytes+p_ns*sizeofpixel],
1995  p_dataPostBytes);
1996  }
1997 
1998  // Save off the prefix bytes vector
1999  if (p_saveDataPre) {
2000  p_dataPre.push_back(tempPre);
2001  tempPre.clear();
2002  }
2003 
2004  // Save off the suffix bytes vector
2005  if (p_saveDataPost) {
2006  p_dataPost.push_back(tempPost);
2007  tempPost.clear();
2008  }
2009 
2010  } // End band loop
2011 
2012  } // End line loop
2013 
2014  // Close the file and clean up
2015  delete JP2_decoder;
2016  delete [] in;
2017  }
2018 
2019 
2020 #if 0
2021 
2028  void ProcessImport::AddLabel(Isis::Pvl &label) {
2029 
2030  label.Root();
2031 
2032  Isis::Pvl *output = OutputCubes[0]->Label();
2033  output->Find("IsisCube");
2034  output->Insert(label);
2035  }
2036 
2043  void ProcessImport::AddImportLabel(Isis::Pvl &importLab) {
2044 
2045  importLab.Root();
2046 
2047  Isis::Pvl *output = OutputCubes[0]->Label();
2048  output->Find("IsisCube");
2049  output->addObject("ImportLabel");
2050  output->Insert(importLab);
2051  }
2052 #endif
2053 
2054 
2063  void ProcessImport::SetInputFile(const QString &file) {
2064  p_inFile = file;
2065  if (!Isis::FileName(file).fileExists()) {
2066  QString msg = "File [" + file + "] does not exist";
2067  throw IException(IException::User, msg, _FILEINFO_);
2068  }
2069  }
2070 
2071 
2081  if (p_inFile.size() <= 0) {
2082  QString msg = "No input file has been set";
2084  }
2085  return p_inFile;
2086  }
2087 
2088 
2089 }
2090