Isis 3.0 Programmer Reference
Back | 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().toLatin1().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 
Buffer for reading and writing cube data.
Definition: Buffer.h:68
int Line(const int index=0) const
Returns the line position associated with a shape buffer index.
Definition: Buffer.cpp:161
void setHrsRange(double min, double max)
Set the range of DN values within which a pixel from the input image will be set to HRS in the output...
void ProcessCubeInPlace(const Functor &funct, bool threaded=true)
Same functionality as StartProcess(void funct(Isis::Buffer &amp;inout)) using Functors.
void EndProcess()
End the processing sequence and cleans up by closing cubes, freeing memory, etc.
double testSpecial(double pixel) const
Tests a pixel against the Null, HRS, and LRS ranges defined by the importer&#39;s handler.
Isis::Cube * SetInputCube(const QString &parameter, const int requirements=0)
Opens an input cube specified by the user and verifies requirements are met.
virtual int getGreen(int pixel) const =0
Pure virtual method for retrieving the green component of the given pixel.
File name manipulation and expansion.
Definition: FileName.h:111
ImageImporter(FileName inputName)
Construct the importer.
virtual int convertRgbToGray(int pixel) const
Convert the current pixel, taken from an RGB/A image, and blend its RGB components into a single gray...
void setLrsRange(double min, double max)
Set the range of DN values within which a pixel from the input image will be set to LRS in the output...
void setDefaultBands()
Set the number of bands to be created for the output cube based on the number of color channels in th...
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:286
Pvl * label() const
Returns a pointer to the IsisLabel object associated with the cube.
Definition: Cube.cpp:1298
void WriteHistory(Cube &cube)
Writes out the History blob to the cube.
Definition: Process.cpp:701
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:101
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:198
virtual int getAlpha(int pixel) const =0
Pure virtual method for retrieving the alpha component of the given pixel.
Imports TIFF images as Isis cubes.
Definition: TiffImporter.h:61
static ImageImporter * fromFileName(FileName inputName)
A static (factory) method for constructing an ImageImporter instance from an input filename...
Imports JPEG 2000 images as Isis cubes.
Definition: JP2Importer.h:46
void setNullRange(double min, double max)
Set the range of DN values within which a pixel from the input image will be set to Null in the outpu...
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:154
void setDimensions(int ns, int nl, int nb)
Used prior to the Create method to specify the size of the cube.
Definition: Cube.cpp:894
FileName filename() const
The filename of the input image this instance was constructed with.
virtual PvlGroup convertProjection() const
Pure virtual method for converting projection information in the file being imported to an ISIS Mappi...
Cube * import(FileName outputName)
Import the image with default output attributes.
int samples() const
The sample dimension (width) of the output image.
int Band(const int index=0) const
Returns the band position associated with a shape buffer index.
Definition: Buffer.cpp:178
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:38
int lines() const
The line dimension (height) of the output image.
void setSamples(int s)
Set the sample dimension (width) of the output image.
Manipulate and parse attributes of output cube filenames.
A single keyword-value pair.
Definition: PvlKeyword.h:98
void setLines(int l)
Set the line dimension (height) of the output image.
void setBands(int b)
Set the band dimension (depth) of the output image.
virtual ~ImageImporter()
Destruct the importer.
int bands() const
The band dimension (depth) of the output image.
Container for cube-like labels.
Definition: Pvl.h:135
Cube * createOutput(FileName outputName, CubeAttributeOutput &att)
Create the output cube from the given filename and attributes.
Imports a series of standard image formats with Qt facilities.
Definition: QtImporter.h:55
void operator()(Buffer &out) const
The method for processing the output cube in place, called for each line of the output image...
void SetProcessingDirection(ProcessingDirection direction)
Set the direction the data will be read, either all lines in a single band proceeding to the next ban...
Isis exception class.
Definition: IException.h:99
int(ImageImporter::* GetChannelMethod)(int pixel) const
Friendly alias for a method used to get a particular color channel.
Adds specific functionality to C++ strings.
Definition: IString.h:179
Imports images with standard formats into Isis as cubes.
Definition: ImageImporter.h:55
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
Process cubes by line.
virtual int getRed(int pixel) const =0
Pure virtual method for retrieving the red component of the given pixel.
virtual GetChannelMethod getBandChannel(int band) const
Retrieve the method responsible for fetching the color channel from the input image corresponding to ...
virtual int getGray(int pixel) const =0
Pure virtual method for retrieving the gray component of the given pixel.
int SampleDimension() const
Returns the number of samples in the shape buffer.
Definition: Buffer.h:85
virtual int getBlue(int pixel) const =0
Pure virtual method for retrieving the blue component of the given pixel.
IO Handler for Isis Cubes.
Definition: Cube.h:158

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