USGS

Isis 3.0 Object Programmers' Reference

Home

ImageExporter.cpp
1 #include "ImageExporter.h"
2 
3 #include "Buffer.h"
4 #include "Cube.h"
5 #include "CubeAttribute.h"
6 #include "ExportDescription.h"
7 #include "FileName.h"
8 #include "JP2Exporter.h"
9 #include "PixelType.h"
10 #include "ProcessExport.h"
11 #include "QtExporter.h"
12 #include "TiffExporter.h"
13 
14 using namespace Isis;
15 using namespace std;
16 
17 
18 namespace Isis {
23  m_process = NULL;
24  m_process = new ProcessExport;
25 
26  m_writeMethod = NULL;
27 
28  m_exportDescription = NULL;
29  m_exportDescription = new ExportDescription();
30 
31  m_extension = "";
32  m_worldExtension = "";
33 
34  m_samples = 0;
35  m_lines = 0;
36  m_bands = 0;
37 
38  m_outputPixelMinimum = 0.0;
39  m_outputPixelMaximum = 0.0;
40  }
41 
42 
53  setExportDescription(desc);
54  initializeProcess();
55  }
56 
61  delete m_process;
62  m_process = NULL;
63  delete m_exportDescription;
64  m_exportDescription = NULL;
65  }
66 
67 
78  void ImageExporter::operator()(vector<Buffer *> &in) const {
79  (this->*m_writeMethod)(in);
80  }
81 
82 
95  void ImageExporter::write(FileName outputName, int quality,
96  QString compression) {
97  ProcessExport &p = process();
98  if (!p.HasInputRange()) p.SetInputRange();
99  p.ProcessCubes(*this);
100 
101  outputName = outputName.addExtension(m_extension);
102  createWorldFile(outputName);
103  }
104 
105 
112  return m_samples;
113  }
114 
115 
121  int ImageExporter::lines() const {
122  return m_lines;
123  }
124 
125 
131  int ImageExporter::bands() const {
132  return m_bands;
133  }
134 
135 
144  double ImageExporter::inputMinimum(int channel) const {
145  return m_process->GetInputMinimum(channel);
146  }
147 
148 
157  double ImageExporter::inputMaximum(int channel) const {
158  return m_process->GetInputMaximum(channel);
159  }
160 
161 
174  void ImageExporter::setOutputPixelRange(double outputPixelMinimum, double outputPixelMaximum) {
175  m_outputPixelMinimum = outputPixelMinimum;
176  m_outputPixelMaximum = outputPixelMaximum;
177  }
178 
179 
186  void ImageExporter::setExtension(QString extension) {
187  m_extension = extension;
188 
189  // World file extension is the first and last characters of the extension
190  // with an added 'w' at the end
191  int last = extension.length() - 1;
192  m_worldExtension = extension.mid(0, 1) + extension.mid(last) + "w";
193  }
194 
195 
201  QString ImageExporter::extension() const {
202  return m_extension;
203  }
204 
211  *m_exportDescription = desc;
212  }
213 
220  return *m_exportDescription;
221  }
222 
223 
240  switch (m_exportDescription->channelCount()) {
241  case 1:
242  m_writeMethod = &ImageExporter::writeGrayscale;
243  break;
244  case 3:
245  m_writeMethod = &ImageExporter::writeRgb;
246  break;
247  case 4:
248  m_writeMethod = &ImageExporter::writeRgba;
249  break;
250  default:
252  "Cannot export an image with [" + QString(m_exportDescription->channelCount()) +
253  "] channels",
254  _FILEINFO_);
255  }
256 
257  ProcessExport &p = process();
258  Cube *cube = addChannel(0);
259  m_samples = cube->sampleCount();
260  m_lines = cube->lineCount();
261  m_bands = m_exportDescription->channelCount();
262 
263  for (int i = 1; i < m_exportDescription->channelCount(); i++) addChannel(i);
264 
265  p.setFormat(ProcessExport::BIL);// why BIL and not default to BSQ??? Doesn't appear to make a
266  // difference in output images
267 
268  // set up the output pixel type, special pixels and valid output range for
269  // the stretch that will be performed by ProcessExport
270  p.SetOutputType(exportDescription().pixelType());
271  p.SetOutputRange(m_exportDescription->outputPixelValidMin(),
272  m_exportDescription->outputPixelValidMax());
273 
274  // the dafault value for the null
275  p.SetOutputNull(m_exportDescription->outputPixelNull());
276 
277  // set the absolute min/max values for all pixels (including specials) in the output image
278  setOutputPixelRange(m_exportDescription->outputPixelAbsoluteMin(),
279  m_exportDescription->outputPixelAbsoluteMax());
280  return cube;
281  }
282 
283 
291  return *m_process;
292  }
293 
294 
301  if (m_exportDescription) {
302  return m_exportDescription->pixelType();
303  }
304  else {
305  return Isis::None;
306  }
307  }
308 
309 
318  int ImageExporter::outputPixelValue(double dn) const {
319  if (dn < m_outputPixelMinimum) {
320  return m_outputPixelMinimum;
321  }
322  else if (dn > m_outputPixelMaximum) {
323  return m_outputPixelMaximum;
324  }
325  else {
326  return dn;
327  }
328  }
329 
330 
340  ProcessExport &p = process();
341 
342  const ExportDescription::ChannelDescription &channel = m_exportDescription->channel(i);
343  Cube *cube = p.SetInputCube(channel.filename().expanded(), channel.attributes(), Isis::OneBand);
344 
345  if (channel.hasCustomRange())
346  p.SetInputRange(channel.inputMinimum(), channel.inputMaximum(), i);
347 
348  return cube;
349  }
350 
351 
358  outputName = outputName.removeExtension();
359  outputName = outputName.addExtension(m_worldExtension);
360 
361  ProcessExport &p = process();
362  p.CreateWorldFile(outputName.expanded());
363  p.EndProcess();
364  }
365 
366 
384  ImageExporter *exporter = NULL;
385 
386  format = format.toLower();
387  if (TiffExporter::canWriteFormat(format)) {
388  exporter = new TiffExporter();
389  }
390  else if (QtExporter::canWriteFormat(format)) {
391  exporter = new QtExporter(format);
392  }
393  else if (JP2Exporter::canWriteFormat(format)) {
394  exporter = new JP2Exporter();
395  }
396  else {
398  "Cannot export image as format [" + format + "]",
399  _FILEINFO_);
400  }
401 
402  return exporter;
403  }
404 };
405