USGS

Isis 3.0 Object Programmers' Reference

Home

ImageImporter.cpp
1 #include "ImageImporter.h"
2 
3 #include <QImageReader>
4 
5 #include "Buffer.h"
6 #include "CubeAttribute.h"
7 #include "FileName.h"
8 #include "History.h"
9 #include "JP2Decoder.h"
10 #include "JP2Importer.h"
11 #include "ProcessByLine.h"
12 #include "PvlGroup.h"
13 #include "QtImporter.h"
14 #include "SpecialPixel.h"
15 #include "TiffImporter.h"
16 
17 using namespace Isis;
18 
19 
20 namespace Isis {
27  m_inputName = NULL;
28  m_outCube = NULL;
29 
30  m_inputName = new FileName(inputName);
31  m_outCube = new Cube;
32 
33  m_nullMin = DBL_MAX;
34  m_nullMax = DBL_MIN;
35  m_lrsMin = DBL_MAX;
36  m_lrsMax = DBL_MIN;
37  m_hrsMin = DBL_MAX;
38  m_hrsMax = DBL_MIN;
39  }
40 
41 
46  delete m_inputName;
47  m_inputName = NULL;
48 
49  delete m_outCube;
50  m_outCube = NULL;
51  }
52 
53 
61  PvlGroup map("Mapping");
62  return map;
63  }
64 
65 
78  void ImageImporter::operator()(Buffer &out) const {
79  // Get the method responsible for finding the color component for the
80  // current output pixel
81  GetChannelMethod getChannel = getBandChannel(out.Band());
82 
83  // Updates the raw buffer of input data when only part of the image is
84  // stored in memory at a time
85  updateRawBuffer(out.Line(), out.Band());
86 
87  // Processing by line, so loop over every sample in the buffer and get its
88  // color component for the current output band, filter based on our special
89  // pixel ranges, then output the resulting DN
90  int l = out.Line() - 1;
91  for (int s = 0; s < out.SampleDimension(); s++) {
92  out[s] = testSpecial((this->*getChannel)(getPixel(s, l)));
93  }
94  }
95 
96 
104  return import(outputName, att);
105  }
106 
107 
124  ProcessByLine p;
125  Cube *cube = createOutput(outputName, att);
126 
127  Pvl *label = cube->label();
128  PvlGroup bandBin("BandBin");
129 
130  PvlKeyword name("Name");
131  if (bands() == 1) {
132  name += "Gray";
133  }
134  else if (bands() == 3 || bands() == 4) {
135  name += "Red";
136  name += "Green";
137  name += "Blue";
138  if (bands() == 4) name += "Alpha";
139  }
140  else {
142  "Cannot interpret BandBin for [" + IString(bands()) + "] band image",
143  _FILEINFO_);
144  }
145  bandBin += name;
146  PvlObject &cubeObj = label->findObject("IsisCube");
147  cubeObj.addGroup(bandBin);
148 
149  PvlGroup mapping = convertProjection();
150  if (mapping.keywords() > 0) {
151  cubeObj.addGroup(mapping);
152  }
153 
154  p.SetInputCube(cube);
155  p.WriteHistory(*cube);
156  p.SetProcessingDirection(ProcessByBrick::BandsFirst);
157  p.ProcessCubeInPlace(*this, false);
158  p.EndProcess();
159 
160  return cube;
161  }
162 
163 
174  FileName outputName, CubeAttributeOutput &att) {
175 
176  m_outCube->setDimensions(samples(), lines(), bands());
177  m_outCube->create(outputName.expanded(), att);
178  return m_outCube;
179  }
180 
181 
187  setBands((isGrayscale()) ? 1 : (isArgb()) ? 4 : 3);
188  }
189 
190 
198  void ImageImporter::setNullRange(double min, double max) {
199  m_nullMin = min;
200  m_nullMax = max;
201  }
202 
203 
211  void ImageImporter::setLrsRange(double min, double max) {
212  m_lrsMin = min;
213  m_lrsMax = max;
214  }
215 
216 
224  void ImageImporter::setHrsRange(double min, double max) {
225  m_hrsMin = min;
226  m_hrsMax = max;
227  }
228 
229 
236  m_samples = s;
237  }
238 
239 
246  m_lines = l;
247  }
248 
249 
258  if (b == 2 || b > 4)
260  "Cannot create an image with [" + IString(b) + "] bands",
261  _FILEINFO_);
262 
263  m_bands = b;
264  }
265 
266 
273  return m_samples;
274  }
275 
276 
282  int ImageImporter::lines() const {
283  return m_lines;
284  }
285 
286 
292  int ImageImporter::bands() const {
293  return m_bands;
294  }
295 
296 
303  return *m_inputName;
304  }
305 
306 
319  double ImageImporter::testSpecial(double pixel) const {
320  if (pixel <= m_nullMax && pixel >= m_nullMin) {
321  return Isis::NULL8;
322  }
323  else if (pixel <= m_hrsMax && pixel >= m_hrsMin) {
324  return Isis::HIGH_REPR_SAT8;
325  }
326  else if (pixel <= m_lrsMax && pixel >= m_lrsMin) {
327  return Isis::LOW_REPR_SAT8;
328  }
329  else {
330  return pixel;
331  }
332  }
333 
334 
349 
350  GetChannelMethod getChannel;
351  if (bands() == 1) {
352  getChannel = &ImageImporter::getGray;
353  }
354  else {
355  switch (band) {
356  case 1:
357  getChannel = &ImageImporter::getRed;
358  break;
359  case 2:
360  getChannel = &ImageImporter::getGreen;
361  break;
362  case 3:
363  getChannel = &ImageImporter::getBlue;
364  break;
365  case 4:
366  getChannel = &ImageImporter::getAlpha;
367  break;
368  default:
370  "Cannot determine channel for band [" + IString(band) + "]",
371  _FILEINFO_);
372  }
373  }
374  return getChannel;
375  }
376 
377 
393  int ImageImporter::convertRgbToGray(int pixel) const {
394  int red = getRed(pixel);
395  int green = getBlue(pixel);
396  int blue = getGreen(pixel);
397  return (red * 11 + green * 16 + blue * 5) / 32;
398  }
399 
400 
415  ImageImporter *importer = NULL;
416 
417  QString format = QImageReader::imageFormat(inputName.expanded());
418  if (format == "tiff") {
419  importer = new TiffImporter(inputName);
420  }
421  else if (format != "" && format != "jp2") {
422  importer = new QtImporter(inputName);
423  }
424  else if (JP2Decoder::IsJP2(inputName.expanded().toAscii().data())) {
425  importer = new JP2Importer(inputName);
426  }
427  else {
429  "Cannot determine image format for [" + inputName.expanded() + "]",
430  _FILEINFO_);
431  }
432 
433  return importer;
434  }
435 };
436