File failed to load: https://isis.astrogeology.usgs.gov/6.0.0/Object/assets/jax/output/NativeMML/config.js
Isis 3 Programmer Reference
QtExporter.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "QtExporter.h"
8 
9 #include <QImage>
10 #include <QImageWriter>
11 
12 #include "Buffer.h"
13 #include "ExportDescription.h"
14 #include "FileName.h"
15 #include "IException.h"
16 #include "IString.h"
17 
18 using namespace Isis;
19 
20 
21 namespace Isis {
27  QtExporter::QtExporter(QString format) : ImageExporter() {
28  m_qimage = NULL;
29  m_format = format;
30 
31  // Setup the required extension and world file
32  if (format == "png")
33  setExtension("png");
34  else if (format == "jpeg")
35  setExtension("jpg");
36  else if (format == "tiff")
37  setExtension("tif");
38  else if (format == "gif")
39  setExtension("gif");
40  else if (format == "bmp")
41  setExtension("bmp");
42  }
43 
44 
49  delete m_qimage;
50  m_qimage = NULL;
51  }
52 
60  // the Qt exporter only exports unsigned byte
61  if (desc.pixelType() != UnsignedByte) {
62  QString msg = "Invalid pixel type. The Qt exporter for file type [";
63  msg += m_format;
64  msg += "] requires an unsigned byte (i.e. 8BIT) output.";
65  throw IException(IException::Unknown, msg, _FILEINFO_);
66  }
68  }
69 
78  initialize(desc);
79  checkDataSize(samples(), lines(), 1);
80  m_qimage = new QImage(samples(), lines(), QImage::Format_Indexed8);
81  m_qimage->setColorCount(256);
82 
83  // Create the color table (black = 0 to white = 255)
84  QVector<QRgb> colors;
85  for (int i = 0; i < 256; i++) {
86  colors.push_back(qRgb(i, i, i));
87  }
88  m_qimage->setColorTable(colors);
89  }
90 
91 
100  initialize(desc);
101  checkDataSize(samples(), lines(), 3);
102  m_qimage = new QImage(samples(), lines(), QImage::Format_RGB32);
103  }
104 
105 
114  initialize(desc);
115  checkDataSize(samples(), lines(), 4);
116  m_qimage = new QImage(samples(), lines(), QImage::Format_ARGB32);
117  }
118 
119 
125  void QtExporter::writeGrayscale(vector<Buffer *> &in) const {
126  Buffer &grayLine = *in[0];
127 
128  // Loop for each column and load the pixel, which will
129  // be in the range of [0,255]
130  int lineIndex = grayLine.Line() - 1;
131  for (int sampleIndex = 0; sampleIndex < grayLine.SampleDimension(); sampleIndex++) {
132  int pixelValue = outputPixelValue(grayLine[sampleIndex]);
133 
134  // Since the plausable "exception" thrown by setPixel cannot be caught,
135  // the following if statement does it informally.
136  m_qimage->setPixel(sampleIndex, lineIndex, pixelValue);
137  if (!m_qimage->valid(sampleIndex, lineIndex)) {
138  QString msg = "Qt has detected your file size as exceeding 2GB.";
139  msg += " While your image might be under 2GB, your image labels are more";
140  msg += " than likely pushing the file size over 2GB.";
141  throw IException(IException::User, msg, _FILEINFO_);
142  }
143  }
144  }
145 
146 
152  void QtExporter::writeRgb(vector<Buffer *> &in) const {
153  Buffer &redLine = *in[0];
154  Buffer &greenLine = *in[1];
155  Buffer &blueLine = *in[2];
156 
157  QRgb *line = (QRgb *) m_qimage->scanLine(redLine.Line() - 1);
158  for (int s = 0; s < redLine.SampleDimension(); s++) {
159  int red = outputPixelValue(redLine[s]);
160  int green = outputPixelValue(greenLine[s]);
161  int blue = outputPixelValue(blueLine[s]);
162 
163  line[s] = qRgb(red, green, blue);
164  }
165  }
166 
167 
173  void QtExporter::writeRgba(vector<Buffer *> &in) const {
174  Buffer &redLine = *in[0];
175  Buffer &greenLine = *in[1];
176  Buffer &blueLine = *in[2];
177  Buffer &alphaLine = *in[3];
178 
179  QRgb *line = (QRgb *) m_qimage->scanLine(redLine.Line() - 1);
180  for (int s = 0; s < redLine.SampleDimension(); s++) {
181  int red = outputPixelValue(redLine[s]);
182  int green = outputPixelValue(greenLine[s]);
183  int blue = outputPixelValue(blueLine[s]);
184  int alpha = outputPixelValue(alphaLine[s]);
185 
186  line[s] = qRgba(red, green, blue, alpha);
187  }
188  }
189 
190 
199  void QtExporter::write(FileName outputName, int quality,
200  QString compression) {
201  ImageExporter::write(outputName, quality);
202 
203  outputName = outputName.addExtension(extension());
204 
205  // The return status is wrong for JPEG images, so the code will always
206  // continue
207  if (!m_qimage->save(outputName.expanded(), m_format.toLatin1().data(),
208  quality)) {
209 
210  QString err = "Unable to save [" + outputName.expanded() +
211  "] to the disk";
212  throw IException(IException::Programmer, err, _FILEINFO_);
213  }
214  }
215 
216 
225  void QtExporter::checkDataSize(BigInt samples, BigInt lines, int bands) {
226  // Qt has a 2GB limit on file sizes it can handle
227  BigInt maxSize = (BigInt) 2 * 1024 * 1024 * 1024;
228 
229  BigInt size = samples * lines * bands;
230  if (size >= maxSize) {
231  QString gigaBytes = toString(size / (1024.0 * 1024.0 * 1024.0));
232  QString msg = "Cube exceeds max size of 2GB. Qimage cannot support ";
233  msg += "that much raw data. Your cube is " + gigaBytes + " GB.";
234  throw IException(IException::User, msg, _FILEINFO_);
235  }
236  }
237 
238 
246  bool QtExporter::canWriteFormat(QString format) {
247  bool supported = false;
248  QList<QByteArray> list = QImageWriter::supportedImageFormats();
249  QList<QByteArray>::Iterator it = list.begin();
250  while (it != list.end() && !supported) {
251  if (*it == QString(format)) supported = true;
252  it++;
253  }
254  return supported;
255  }
256 };
257 
Isis::QtExporter::setGrayscale
virtual void setGrayscale(ExportDescription &desc)
Set the input with the description generically, check the data size for a single-band image with the ...
Definition: QtExporter.cpp:77
Isis::QtExporter::~QtExporter
virtual ~QtExporter()
Destruct the exporter.
Definition: QtExporter.cpp:48
Isis::QtExporter::initialize
void initialize(ExportDescription &desc)
Generic initialization with the export description.
Definition: QtExporter.cpp:59
Isis::QtExporter::QtExporter
QtExporter(QString format)
Construct the Qt exporter.
Definition: QtExporter.cpp:27
Isis::Buffer::SampleDimension
int SampleDimension() const
Returns the number of samples in the shape buffer.
Definition: Buffer.h:70
QList
This is free and unencumbered software released into the public domain.
Definition: BoxcarCachingAlgorithm.h:13
Isis::QtExporter::write
virtual void write(FileName outputName, int quality=100, QString compression="none")
Let the base ImageExporter handle the generic black-box writing routine, then save the image to disk.
Definition: QtExporter.cpp:199
Isis::QtExporter::setRgb
virtual void setRgb(ExportDescription &desc)
Set the input with the description generically, check the data size for a three-band image with the e...
Definition: QtExporter.cpp:99
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::QtExporter::m_qimage
QImage * m_qimage
Structure holding all output image data in memory.
Definition: QtExporter.h:72
Isis::IException::Unknown
@ Unknown
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:118
Isis::ExportDescription
Describes how a series of cubes should be exported.
Definition: ExportDescription.h:43
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::QtExporter::checkDataSize
void checkDataSize(BigInt samples, BigInt lines, int bands)
Checks that the data size for an image of the desired dimensions will be less than 2GB.
Definition: QtExporter.cpp:225
Isis::Buffer
Buffer for reading and writing cube data.
Definition: Buffer.h:53
Isis::ImageExporter::write
virtual void write(FileName outputName, int quality=100, QString compression="none")
Export the Isis cube channels to the given standard image.
Definition: ImageExporter.cpp:101
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::ExportDescription::pixelType
PixelType pixelType() const
Returns the pixel type.
Definition: ExportDescription.cpp:148
Isis::FileName::addExtension
FileName addExtension(const QString &extension) const
Adds a new extension to the file name.
Definition: FileName.cpp:225
Isis::QtExporter::writeGrayscale
virtual void writeGrayscale(vector< Buffer * > &in) const
Write a line of grayscale data to the output image.
Definition: QtExporter.cpp:125
Isis::BigInt
long long int BigInt
Big int.
Definition: Constants.h:49
Isis::ImageExporter::outputPixelValue
virtual int outputPixelValue(double dn) const
Return the output clamped integer pixel value from the input double-precision DN.
Definition: ImageExporter.cpp:325
Isis::ImageExporter::extension
QString extension() const
Gets the extension for the output image.
Definition: ImageExporter.cpp:208
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::QtExporter::writeRgba
virtual void writeRgba(vector< Buffer * > &in) const
Write a line of RGBA data to the output image.
Definition: QtExporter.cpp:173
Isis::QtExporter::setRgba
virtual void setRgba(ExportDescription &desc)
Set the input with the description generically, check the data size for a four-band image with the es...
Definition: QtExporter.cpp:113
Isis::ImageExporter
Export Isis cubes into standard formats.
Definition: ImageExporter.h:54
Isis::QtExporter::writeRgb
virtual void writeRgb(vector< Buffer * > &in) const
Write a line of RGB data to the output image.
Definition: QtExporter.cpp:152
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::ImageExporter::lines
int lines() const
Number of lines (rows) in the output image.
Definition: ImageExporter.cpp:128
Isis::ImageExporter::bands
int bands() const
Number of bands (channels) in the output image.
Definition: ImageExporter.cpp:138
Isis::ImageExporter::samples
int samples() const
Number of samples (columns) in the output image.
Definition: ImageExporter.cpp:118
QVector
This is free and unencumbered software released into the public domain.
Definition: Calculator.h:18
Isis::QtExporter::canWriteFormat
static bool canWriteFormat(QString format)
Returns true if the format is supported by QImageWriter.
Definition: QtExporter.cpp:246
Isis::QtExporter::m_format
QString m_format
The lowercase abbreviated format of the output image.
Definition: QtExporter.h:75
Isis::ImageExporter::setExtension
void setExtension(QString extension)
Sets the extension for the output image and generates the extension for the world file from it.
Definition: ImageExporter.cpp:193
Isis::ImageExporter::initialize
virtual void initialize(ExportDescription &desc)=0
Generic initialization with the export description.
Definition: ImageExporter.cpp:58
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::IException::User
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition: IException.h:126

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 USGS Astrogeology Discussion Board
To report a bug, or suggest a feature go to: ISIS Github
File Modified: 07/13/2023 15:17:10