USGS

Isis 3.0 Object Programmers' Reference

Home

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->setNumColors(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 
192  void QtExporter::write(FileName outputName, int quality) {
193  ImageExporter::write(outputName, quality);
194 
195  // The return status is wrong for JPEG images, so the code will always
196  // continue
197  if (!m_qimage->save(outputName.expanded(), m_format.toAscii().data(),
198  quality)) {
199 
200  QString err = "Unable to save [" + outputName.expanded() +
201  "] to the disk";
203  }
204  }
205 
206 
215  void QtExporter::checkDataSize(BigInt samples, BigInt lines, int bands) {
216  // Qt has a 2GB limit on file sizes it can handle
217  BigInt maxSize = (BigInt) 2 * 1024 * 1024 * 1024;
218 
219  BigInt size = samples * lines * bands;
220  if (size >= maxSize) {
221  QString gigaBytes = toString(size / (1024.0 * 1024.0 * 1024.0));
222  QString msg = "Cube exceeds max size of 2GB. Qimage cannot support ";
223  msg += "that much raw data. Your cube is " + gigaBytes + " GB.";
225  }
226  }
227 
228 
236  bool QtExporter::canWriteFormat(QString format) {
237  bool supported = false;
238  QList<QByteArray> list = QImageWriter::supportedImageFormats();
239  QList<QByteArray>::Iterator it = list.begin();
240  while (it != list.end() && !supported) {
241  if (*it == QString(format)) supported = true;
242  it++;
243  }
244  return supported;
245  }
246 };
247