Isis 3 Programmer Reference
ImageImporter.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "ImageImporter.h"
8 
9 #include <QImageReader>
10 
11 #include "Buffer.h"
12 #include "CubeAttribute.h"
13 #include "FileName.h"
14 #include "History.h"
15 #include "JP2Decoder.h"
16 #include "JP2Importer.h"
17 #include "ProcessByLine.h"
18 #include "PvlGroup.h"
19 #include "QtImporter.h"
20 #include "SpecialPixel.h"
21 #include "TiffImporter.h"
22 
23 using namespace Isis;
24 
25 
26 namespace Isis {
33  m_inputName = NULL;
34  m_outCube = NULL;
35 
36  m_inputName = new FileName(inputName);
37  m_outCube = new Cube;
38 
39  m_nullMin = DBL_MAX;
40  m_nullMax = DBL_MIN;
41  m_lrsMin = DBL_MAX;
42  m_lrsMax = DBL_MIN;
43  m_hrsMin = DBL_MAX;
44  m_hrsMax = DBL_MIN;
45  }
46 
47 
52  delete m_inputName;
53  m_inputName = NULL;
54 
55  delete m_outCube;
56  m_outCube = NULL;
57  }
58 
59 
67  PvlGroup map("Mapping");
68  return map;
69  }
70 
71 
84  void ImageImporter::operator()(Buffer &out) const {
85  // Get the method responsible for finding the color component for the
86  // current output pixel
87  GetChannelMethod getChannel = getBandChannel(out.Band());
88 
89  // Updates the raw buffer of input data when only part of the image is
90  // stored in memory at a time
91  updateRawBuffer(out.Line(), out.Band());
92 
93  // Processing by line, so loop over every sample in the buffer and get its
94  // color component for the current output band, filter based on our special
95  // pixel ranges, then output the resulting DN
96  int l = out.Line() - 1;
97  for (int s = 0; s < out.SampleDimension(); s++) {
98  out[s] = testSpecial((this->*getChannel)(getPixel(s, l)));
99  }
100  }
101 
102 
110  return import(outputName, att);
111  }
112 
113 
130  ProcessByLine p;
131  Cube *cube = createOutput(outputName, att);
132 
133  Pvl *label = cube->label();
134  PvlGroup bandBin("BandBin");
135 
136  PvlKeyword name("Name");
137  if (bands() == 1) {
138  name += "Gray";
139  }
140  else if (bands() == 3 || bands() == 4) {
141  name += "Red";
142  name += "Green";
143  name += "Blue";
144  if (bands() == 4) name += "Alpha";
145  }
146  else {
148  "Cannot interpret BandBin for [" + IString(bands()) + "] band image",
149  _FILEINFO_);
150  }
151  bandBin += name;
152  PvlObject &cubeObj = label->findObject("IsisCube");
153  cubeObj.addGroup(bandBin);
154 
155  PvlGroup mapping = convertProjection();
156  if (mapping.keywords() > 0) {
157  cubeObj.addGroup(mapping);
158  }
159 
160  p.SetInputCube(cube);
161  p.WriteHistory(*cube);
162  p.SetProcessingDirection(ProcessByBrick::BandsFirst);
163  p.ProcessCubeInPlace(*this, false);
164  p.EndProcess();
165 
166  return cube;
167  }
168 
169 
180  FileName outputName, CubeAttributeOutput &att) {
181 
183  m_outCube->create(outputName.expanded(), att);
184  return m_outCube;
185  }
186 
187 
193  setBands((isGrayscale()) ? 1 : (isArgb()) ? 4 : 3);
194  }
195 
196 
204  void ImageImporter::setNullRange(double min, double max) {
205  m_nullMin = min;
206  m_nullMax = max;
207  }
208 
209 
217  void ImageImporter::setLrsRange(double min, double max) {
218  m_lrsMin = min;
219  m_lrsMax = max;
220  }
221 
222 
230  void ImageImporter::setHrsRange(double min, double max) {
231  m_hrsMin = min;
232  m_hrsMax = max;
233  }
234 
235 
242  m_samples = s;
243  }
244 
245 
252  m_lines = l;
253  }
254 
255 
264  if (b == 2 || b > 4)
266  "Cannot create an image with [" + IString(b) + "] bands",
267  _FILEINFO_);
268 
269  m_bands = b;
270  }
271 
272 
279  return m_samples;
280  }
281 
282 
288  int ImageImporter::lines() const {
289  return m_lines;
290  }
291 
292 
298  int ImageImporter::bands() const {
299  return m_bands;
300  }
301 
302 
309  return *m_inputName;
310  }
311 
312 
325  double ImageImporter::testSpecial(double pixel) const {
326  if (pixel <= m_nullMax && pixel >= m_nullMin) {
327  return Isis::NULL8;
328  }
329  else if (pixel <= m_hrsMax && pixel >= m_hrsMin) {
330  return Isis::HIGH_REPR_SAT8;
331  }
332  else if (pixel <= m_lrsMax && pixel >= m_lrsMin) {
333  return Isis::LOW_REPR_SAT8;
334  }
335  else {
336  return pixel;
337  }
338  }
339 
340 
355 
356  GetChannelMethod getChannel;
357  if (bands() == 1) {
358  getChannel = &ImageImporter::getGray;
359  }
360  else {
361  switch (band) {
362  case 1:
363  getChannel = &ImageImporter::getRed;
364  break;
365  case 2:
366  getChannel = &ImageImporter::getGreen;
367  break;
368  case 3:
369  getChannel = &ImageImporter::getBlue;
370  break;
371  case 4:
372  getChannel = &ImageImporter::getAlpha;
373  break;
374  default:
376  "Cannot determine channel for band [" + IString(band) + "]",
377  _FILEINFO_);
378  }
379  }
380  return getChannel;
381  }
382 
383 
399  int ImageImporter::convertRgbToGray(int pixel) const {
400  int red = getRed(pixel);
401  int green = getBlue(pixel);
402  int blue = getGreen(pixel);
403  return (red * 11 + green * 16 + blue * 5) / 32;
404  }
405 
406 
421  ImageImporter *importer = NULL;
422 
423  QString format = QImageReader::imageFormat(inputName.expanded());
424  if (format == "tiff") {
425  importer = new TiffImporter(inputName);
426  }
427  else if (format != "" && format != "jp2") {
428  importer = new QtImporter(inputName);
429  }
430  else if (JP2Decoder::IsJP2(inputName.expanded().toLatin1().data())) {
431  importer = new JP2Importer(inputName);
432  }
433  else {
435  "Cannot determine image format for [" + inputName.expanded() + "]",
436  _FILEINFO_);
437  }
438 
439  return importer;
440  }
441 };
442 
Isis::ImageImporter::m_hrsMin
double m_hrsMin
The lower bound of the range within which input DNs will be made HRS.
Definition: ImageImporter.h:208
Isis::Buffer::SampleDimension
int SampleDimension() const
Returns the number of samples in the shape buffer.
Definition: Buffer.h:70
Isis::ImageImporter::convertRgbToGray
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...
Definition: ImageImporter.cpp:399
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
Isis::ImageImporter::getPixel
virtual int getPixel(int s, int l) const =0
Pure virtual method that returns a representation of a pixel for the input format that can then be br...
Isis::ImageImporter::setSamples
void setSamples(int s)
Set the sample dimension (width) of the output image.
Definition: ImageImporter.cpp:241
Isis::ImageImporter::m_lrsMax
double m_lrsMax
The upper bound of the range within which input DNs will be made LRS.
Definition: ImageImporter.h:205
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
Isis::ImageImporter::GetChannelMethod
int(ImageImporter::* GetChannelMethod)(int pixel) const
Friendly alias for a method used to get a particular color channel.
Definition: ImageImporter.h:97
Isis::Process::WriteHistory
void WriteHistory(Cube &cube)
Writes out the History blob to the cube.
Definition: Process.cpp:789
Isis::ImageImporter::setLrsRange
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...
Definition: ImageImporter.cpp:217
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::ImageImporter::isArgb
virtual bool isArgb() const =0
Pure virtual method for returning true if the image is RGBA.
Isis::ImageImporter::getGreen
virtual int getGreen(int pixel) const =0
Pure virtual method for retrieving the green component of the given pixel.
Isis::ImageImporter::updateRawBuffer
virtual void updateRawBuffer(int line, int band) const =0
Pure virtual method that updates the buffer used to store chunks of the input data at a time.
Isis::ImageImporter::setNullRange
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...
Definition: ImageImporter.cpp:204
Isis::JP2Importer
Imports JPEG 2000 images as Isis cubes.
Definition: JP2Importer.h:30
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::CubeAttributeOutput
Manipulate and parse attributes of output cube filenames.
Definition: CubeAttribute.h:473
Isis::ImageImporter
Imports images with standard formats into Isis as cubes.
Definition: ImageImporter.h:39
Isis::ProcessByLine
Process cubes by line.
Definition: ProcessByLine.h:97
Isis::ImageImporter::m_samples
int m_samples
The number of samples to be written to the output.
Definition: ImageImporter.h:187
Isis::ImageImporter::getBlue
virtual int getBlue(int pixel) const =0
Pure virtual method for retrieving the blue component of the given pixel.
Isis::Buffer
Buffer for reading and writing cube data.
Definition: Buffer.h:53
Isis::ImageImporter::bands
int bands() const
The band dimension (depth) of the output image.
Definition: ImageImporter.cpp:298
Isis::ProcessByLine::ProcessCubeInPlace
void ProcessCubeInPlace(const Functor &funct, bool threaded=true)
Same functionality as StartProcess(void funct(Isis::Buffer &inout)) using Functors.
Definition: ProcessByLine.h:133
Isis::FileName::expanded
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:196
Isis::ImageImporter::setDefaultBands
void setDefaultBands()
Set the number of bands to be created for the output cube based on the number of color channels in th...
Definition: ImageImporter.cpp:192
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::ImageImporter::m_hrsMax
double m_hrsMax
The upper bound of the range within which input DNs will be made HRS.
Definition: ImageImporter.h:211
Isis::Cube::setDimensions
void setDimensions(int ns, int nl, int nb)
Used prior to the Create method to specify the size of the cube.
Definition: Cube.cpp:1217
Isis::Cube::create
void create(const QString &cfile)
This method will create an isis cube for writing.
Definition: Cube.cpp:414
Isis::ImageImporter::m_inputName
FileName * m_inputName
The filename of the input image.
Definition: ImageImporter.h:181
Isis::ImageImporter::samples
int samples() const
The sample dimension (width) of the output image.
Definition: ImageImporter.cpp:278
Isis::ImageImporter::setBands
void setBands(int b)
Set the band dimension (depth) of the output image.
Definition: ImageImporter.cpp:263
Isis::PvlObject::findObject
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:274
Isis::ImageImporter::convertProjection
virtual PvlGroup convertProjection() const
Pure virtual method for converting projection information in the file being imported to an ISIS Mappi...
Definition: ImageImporter.cpp:66
Isis::Cube
IO Handler for Isis Cubes.
Definition: Cube.h:167
Isis::ImageImporter::getAlpha
virtual int getAlpha(int pixel) const =0
Pure virtual method for retrieving the alpha component of the given pixel.
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::ImageImporter::setLines
void setLines(int l)
Set the line dimension (height) of the output image.
Definition: ImageImporter.cpp:251
Isis::ImageImporter::operator()
void operator()(Buffer &out) const
The method for processing the output cube in place, called for each line of the output image.
Definition: ImageImporter.cpp:84
Isis::ImageImporter::~ImageImporter
virtual ~ImageImporter()
Destruct the importer.
Definition: ImageImporter.cpp:51
Isis::ImageImporter::m_nullMax
double m_nullMax
The upper bound of the range within which input DNs will be made Null.
Definition: ImageImporter.h:199
Isis::ImageImporter::setHrsRange
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...
Definition: ImageImporter.cpp:230
Isis::ImageImporter::m_lines
int m_lines
The number of lines to be written to the output.
Definition: ImageImporter.h:190
Isis::ImageImporter::m_bands
int m_bands
The number of bands to be written to the output.
Definition: ImageImporter.h:193
Isis::ImageImporter::testSpecial
double testSpecial(double pixel) const
Tests a pixel against the Null, HRS, and LRS ranges defined by the importer's handler.
Definition: ImageImporter.cpp:325
Isis::PvlObject::addGroup
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:186
Isis::ImageImporter::lines
int lines() const
The line dimension (height) of the output image.
Definition: ImageImporter.cpp:288
Isis::ImageImporter::isGrayscale
virtual bool isGrayscale() const =0
Pure virtual method for returning true if the image is grayscale.
Isis::Buffer::Band
int Band(const int index=0) const
Returns the band position associated with a shape buffer index.
Definition: Buffer.cpp:162
Isis::TiffImporter
Imports TIFF images as Isis cubes.
Definition: TiffImporter.h:45
Isis::ImageImporter::import
Cube * import(FileName outputName)
Import the image with default output attributes.
Definition: ImageImporter.cpp:108
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::ImageImporter::fromFileName
static ImageImporter * fromFileName(FileName inputName)
A static (factory) method for constructing an ImageImporter instance from an input filename.
Definition: ImageImporter.cpp:420
Isis::ImageImporter::filename
FileName filename() const
The filename of the input image this instance was constructed with.
Definition: ImageImporter.cpp:308
Isis::Cube::label
Pvl * label() const
Returns a pointer to the IsisLabel object associated with the cube.
Definition: Cube.cpp:1701
Isis::ImageImporter::getBandChannel
virtual GetChannelMethod getBandChannel(int band) const
Retrieve the method responsible for fetching the color channel from the input image corresponding to ...
Definition: ImageImporter.cpp:354
Isis::ProcessByBrick::SetProcessingDirection
void SetProcessingDirection(ProcessingDirection direction)
Set the direction the data will be read, either all lines in a single band proceeding to the next ban...
Definition: ProcessByBrick.cpp:380
Isis::ImageImporter::ImageImporter
ImageImporter(FileName inputName)
Construct the importer.
Definition: ImageImporter.cpp:32
Isis::ImageImporter::m_outCube
Cube * m_outCube
The owned handle on the output cube to be imported to.
Definition: ImageImporter.h:184
Isis::PvlContainer::keywords
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:86
Isis::QtImporter
Imports a series of standard image formats with Qt facilities.
Definition: QtImporter.h:39
Isis::IString
Adds specific functionality to C++ strings.
Definition: IString.h:165
Isis::ImageImporter::m_lrsMin
double m_lrsMin
The lower bound of the range within which input DNs will be made LRS.
Definition: ImageImporter.h:202
Isis::ProcessByBrick::EndProcess
void EndProcess()
End the processing sequence and cleans up by closing cubes, freeing memory, etc.
Definition: ProcessByBrick.cpp:669
Isis::ProcessByLine::SetInputCube
Isis::Cube * SetInputCube(const QString &parameter, const int requirements=0)
Opens an input cube specified by the user and verifies requirements are met.
Definition: ProcessByLine.cpp:41
Isis::Buffer::Line
int Line(const int index=0) const
Returns the line position associated with a shape buffer index.
Definition: Buffer.cpp:145
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::ImageImporter::createOutput
Cube * createOutput(FileName outputName, CubeAttributeOutput &att)
Create the output cube from the given filename and attributes.
Definition: ImageImporter.cpp:179
Isis::ImageImporter::getRed
virtual int getRed(int pixel) const =0
Pure virtual method for retrieving the red component of the given pixel.
Isis::ImageImporter::m_nullMin
double m_nullMin
The lower bound of the range within which input DNs will be made Null.
Definition: ImageImporter.h:196
Isis::ImageImporter::getGray
virtual int getGray(int pixel) const =0
Pure virtual method for retrieving the gray component of the given pixel.