Isis 3 Programmer Reference
QtExporter.cpp
1 #include "QtExporter.h"
2 
3 #include <QImage>
4 #include <QImageWriter>
5 
6 #include "Buffer.h"
7 #include "ExportDescription.h"
8 #include "FileName.h"
9 #include "IException.h"
10 #include "IString.h"
11 
12 using namespace Isis;
13 
14 
15 namespace Isis {
21  QtExporter::QtExporter(QString format) : ImageExporter() {
22  m_qimage = NULL;
23  m_format = format;
24 
25  // Setup the required extension and world file
26  if (format == "png")
27  setExtension("png");
28  else if (format == "jpeg")
29  setExtension("jpg");
30  else if (format == "tiff")
31  setExtension("tif");
32  else if (format == "gif")
33  setExtension("gif");
34  else if (format == "bmp")
35  setExtension("bmp");
36  }
37 
38 
43  delete m_qimage;
44  m_qimage = NULL;
45  }
46 
54  // the Qt exporter only exports unsigned byte
55  if (desc.pixelType() != UnsignedByte) {
56  QString msg = "Invalid pixel type. The Qt exporter for file type [";
57  msg += m_format;
58  msg += "] requires an unsigned byte (i.e. 8BIT) output.";
60  }
62  }
63 
72  initialize(desc);
73  checkDataSize(samples(), lines(), 1);
74  m_qimage = new QImage(samples(), lines(), QImage::Format_Indexed8);
75  m_qimage->setColorCount(256);
76 
77  // Create the color table (black = 0 to white = 255)
78  QVector<QRgb> colors;
79  for (int i = 0; i < 256; i++) {
80  colors.push_back(qRgb(i, i, i));
81  }
82  m_qimage->setColorTable(colors);
83  }
84 
85 
94  initialize(desc);
95  checkDataSize(samples(), lines(), 3);
96  m_qimage = new QImage(samples(), lines(), QImage::Format_RGB32);
97  }
98 
99 
108  initialize(desc);
109  checkDataSize(samples(), lines(), 4);
110  m_qimage = new QImage(samples(), lines(), QImage::Format_ARGB32);
111  }
112 
113 
119  void QtExporter::writeGrayscale(vector<Buffer *> &in) const {
120  Buffer &grayLine = *in[0];
121 
122  // Loop for each column and load the pixel, which will
123  // be in the range of [0,255]
124  int lineIndex = grayLine.Line() - 1;
125  for (int sampleIndex = 0; sampleIndex < grayLine.SampleDimension(); sampleIndex++) {
126  int pixelValue = outputPixelValue(grayLine[sampleIndex]);
127 
128  // Since the plausable "exception" thrown by setPixel cannot be caught,
129  // the following if statement does it informally.
130  m_qimage->setPixel(sampleIndex, lineIndex, pixelValue);
131  if (!m_qimage->valid(sampleIndex, lineIndex)) {
132  QString msg = "Qt has detected your file size as exceeding 2GB.";
133  msg += " While your image might be under 2GB, your image labels are more";
134  msg += " than likely pushing the file size over 2GB.";
136  }
137  }
138  }
139 
140 
146  void QtExporter::writeRgb(vector<Buffer *> &in) const {
147  Buffer &redLine = *in[0];
148  Buffer &greenLine = *in[1];
149  Buffer &blueLine = *in[2];
150 
151  QRgb *line = (QRgb *) m_qimage->scanLine(redLine.Line() - 1);
152  for (int s = 0; s < redLine.SampleDimension(); s++) {
153  int red = outputPixelValue(redLine[s]);
154  int green = outputPixelValue(greenLine[s]);
155  int blue = outputPixelValue(blueLine[s]);
156 
157  line[s] = qRgb(red, green, blue);
158  }
159  }
160 
161 
167  void QtExporter::writeRgba(vector<Buffer *> &in) const {
168  Buffer &redLine = *in[0];
169  Buffer &greenLine = *in[1];
170  Buffer &blueLine = *in[2];
171  Buffer &alphaLine = *in[3];
172 
173  QRgb *line = (QRgb *) m_qimage->scanLine(redLine.Line() - 1);
174  for (int s = 0; s < redLine.SampleDimension(); s++) {
175  int red = outputPixelValue(redLine[s]);
176  int green = outputPixelValue(greenLine[s]);
177  int blue = outputPixelValue(blueLine[s]);
178  int alpha = outputPixelValue(alphaLine[s]);
179 
180  line[s] = qRgba(red, green, blue, alpha);
181  }
182  }
183 
184 
193  void QtExporter::write(FileName outputName, int quality,
194  QString compression) {
195  ImageExporter::write(outputName, quality);
196 
197  outputName = outputName.addExtension(extension());
198 
199  // The return status is wrong for JPEG images, so the code will always
200  // continue
201  if (!m_qimage->save(outputName.expanded(), m_format.toLatin1().data(),
202  quality)) {
203 
204  QString err = "Unable to save [" + outputName.expanded() +
205  "] to the disk";
207  }
208  }
209 
210 
219  void QtExporter::checkDataSize(BigInt samples, BigInt lines, int bands) {
220  // Qt has a 2GB limit on file sizes it can handle
221  BigInt maxSize = (BigInt) 2 * 1024 * 1024 * 1024;
222 
223  BigInt size = samples * lines * bands;
224  if (size >= maxSize) {
225  QString gigaBytes = toString(size / (1024.0 * 1024.0 * 1024.0));
226  QString msg = "Cube exceeds max size of 2GB. Qimage cannot support ";
227  msg += "that much raw data. Your cube is " + gigaBytes + " GB.";
229  }
230  }
231 
232 
240  bool QtExporter::canWriteFormat(QString format) {
241  bool supported = false;
242  QList<QByteArray> list = QImageWriter::supportedImageFormats();
243  QList<QByteArray>::Iterator it = list.begin();
244  while (it != list.end() && !supported) {
245  if (*it == QString(format)) supported = true;
246  it++;
247  }
248  return supported;
249  }
250 };
251 
Buffer for reading and writing cube data.
Definition: Buffer.h:69
long long int BigInt
Big int.
Definition: Constants.h:65
QString extension() const
Gets the extension for the output image.
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:219
File name manipulation and expansion.
Definition: FileName.h:116
int bands() const
Number of bands (channels) in the output image.
Export Isis cubes into standard formats.
Definition: ImageExporter.h:70
FileName addExtension(const QString &extension) const
Adds a new extension to the file name.
Definition: FileName.cpp:241
int samples() const
Number of samples (columns) in the output image.
virtual int outputPixelValue(double dn) const
Return the output clamped integer pixel value from the input double-precision DN. ...
virtual ~QtExporter()
Destruct the exporter.
Definition: QtExporter.cpp:42
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
void setExtension(QString extension)
Sets the extension for the output image and generates the extension for the world file from it...
QImage * m_qimage
Structure holding all output image data in memory.
Definition: QtExporter.h:88
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:193
virtual void writeRgba(vector< Buffer *> &in) const
Write a line of RGBA data to the output image.
Definition: QtExporter.cpp:167
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:107
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:93
QtExporter(QString format)
Construct the Qt exporter.
Definition: QtExporter.cpp:21
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
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:71
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:134
int Line(const int index=0) const
Returns the line position associated with a shape buffer index.
Definition: Buffer.cpp:161
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
static bool canWriteFormat(QString format)
Returns true if the format is supported by QImageWriter.
Definition: QtExporter.cpp:240
Describes how a series of cubes should be exported.
virtual void writeGrayscale(vector< Buffer *> &in) const
Write a line of grayscale data to the output image.
Definition: QtExporter.cpp:119
int lines() const
Number of lines (rows) in the output image.
virtual void initialize(ExportDescription &desc)=0
Generic initialization with the export description.
virtual void writeRgb(vector< Buffer *> &in) const
Write a line of RGB data to the output image.
Definition: QtExporter.cpp:146
int SampleDimension() const
Returns the number of samples in the shape buffer.
Definition: Buffer.h:86
QString m_format
The lowercase abbreviated format of the output image.
Definition: QtExporter.h:91
PixelType pixelType() const
Returns the pixel type.
virtual void write(FileName outputName, int quality=100, QString compression="none")
Export the Isis cube channels to the given standard image.
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void initialize(ExportDescription &desc)
Generic initialization with the export description.
Definition: QtExporter.cpp:53