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
23using namespace Isis;
24
25
26namespace 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
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
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
289 return m_lines;
290 }
291
292
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
Buffer for reading and writing cube data.
Definition Buffer.h:53
Manipulate and parse attributes of output cube filenames.
IO Handler for Isis Cubes.
Definition Cube.h:168
void setDimensions(int ns, int nl, int nb)
Used prior to the Create method to specify the size of the cube.
Definition Cube.cpp:1223
void create(const QString &cfile)
This method will create an isis cube for writing.
Definition Cube.cpp:409
Pvl * label() const
Returns a pointer to the IsisLabel object associated with the cube.
Definition Cube.cpp:1707
File name manipulation and expansion.
Definition FileName.h:100
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
Adds specific functionality to C++ strings.
Definition IString.h:165
Imports images with standard formats into Isis as cubes.
int bands() const
The band dimension (depth) of the output image.
double m_lrsMax
The upper bound of the range within which input DNs will be made LRS.
void setLines(int l)
Set the line dimension (height) of the output image.
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...
virtual int getGreen(int pixel) const =0
Pure virtual method for retrieving the green component of the given pixel.
virtual int getBlue(int pixel) const =0
Pure virtual method for retrieving the blue component of the given pixel.
double m_nullMax
The upper bound of the range within which input DNs will be made Null.
int samples() const
The sample dimension (width) of the output image.
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.
virtual int getRed(int pixel) const =0
Pure virtual method for retrieving the red component of the given pixel.
void setSamples(int s)
Set the sample dimension (width) of the output image.
ImageImporter(FileName inputName)
Construct the importer.
FileName filename() const
The filename of the input image this instance was constructed with.
virtual int getGray(int pixel) const =0
Pure virtual method for retrieving the gray component of the given pixel.
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 setDefaultBands()
Set the number of bands to be created for the output cube based on the number of color channels in th...
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...
FileName * m_inputName
The filename of the input image.
virtual GetChannelMethod getBandChannel(int band) const
Retrieve the method responsible for fetching the color channel from the input image corresponding to ...
virtual int getAlpha(int pixel) const =0
Pure virtual method for retrieving the alpha component of the given pixel.
int m_samples
The number of samples to be written to the output.
double m_hrsMin
The lower bound of the range within which input DNs will be made HRS.
double testSpecial(double pixel) const
Tests a pixel against the Null, HRS, and LRS ranges defined by the importer's handler.
virtual PvlGroup convertProjection() const
Pure virtual method for converting projection information in the file being imported to an ISIS Mappi...
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...
int(ImageImporter::* GetChannelMethod)(int pixel) const
Friendly alias for a method used to get a particular color channel.
static ImageImporter * fromFileName(FileName inputName)
A static (factory) method for constructing an ImageImporter instance from an input filename.
double m_hrsMax
The upper bound of the range within which input DNs will be made HRS.
void operator()(Buffer &out) const
The method for processing the output cube in place, called for each line of the output image.
double m_nullMin
The lower bound of the range within which input DNs will be made Null.
virtual bool isArgb() const =0
Pure virtual method for returning true if the image is RGBA.
Cube * createOutput(FileName outputName, CubeAttributeOutput &att)
Create the output cube from the given filename and attributes.
Cube * import(FileName outputName)
Import the image with default output attributes.
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...
int m_bands
The number of bands to be written to the output.
double m_lrsMin
The lower bound of the range within which input DNs will be made LRS.
void setBands(int b)
Set the band dimension (depth) of the output image.
Cube * m_outCube
The owned handle on the output cube to be imported to.
virtual ~ImageImporter()
Destruct the importer.
int m_lines
The number of lines to be written to the output.
virtual bool isGrayscale() const =0
Pure virtual method for returning true if the image is grayscale.
int lines() const
The line dimension (height) of the output image.
Imports JPEG 2000 images as Isis cubes.
Definition JP2Importer.h:30
Process cubes by line.
Contains multiple PvlContainers.
Definition PvlGroup.h:41
Container for cube-like labels.
Definition Pvl.h:119
A single keyword-value pair.
Definition PvlKeyword.h:87
Contains Pvl Groups and Pvl Objects.
Definition PvlObject.h:61
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition PvlObject.h:276
Imports a series of standard image formats with Qt facilities.
Definition QtImporter.h:39
Imports TIFF images as Isis cubes.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16