USGS

Isis 3.0 Object Programmers' Reference

Home

ImageExporter.cpp

00001 #include "ImageExporter.h"
00002 
00003 #include "Buffer.h"
00004 #include "CubeAttribute.h"
00005 #include "ExportDescription.h"
00006 #include "FileName.h"
00007 #include "JP2Exporter.h"
00008 #include "ProcessExport.h"
00009 #include "QtExporter.h"
00010 #include "TiffExporter.h"
00011 
00012 using namespace Isis;
00013 
00014 
00015 namespace Isis {
00019   ImageExporter::ImageExporter() {
00020     m_process = NULL;
00021     m_process = new ProcessExport;
00022 
00023     m_writeMethod = NULL;
00024 
00025     m_extension = "";
00026     m_world = "";
00027 
00028     m_samples = 0;
00029     m_lines = 0;
00030     m_bands = 0;
00031 
00032     m_dataMin = 0.0;
00033     m_dataMax = 255.0;
00034   }
00035 
00036 
00040   ImageExporter::~ImageExporter() {
00041     delete m_process;
00042     m_process = NULL;
00043   }
00044 
00045 
00056   void ImageExporter::operator()(vector<Buffer *> &in) const {
00057     (this->*m_writeMethod)(in);
00058   }
00059 
00060 
00071   void ImageExporter::write(FileName outputName, int quality) {
00072     ProcessExport &p = getProcess();
00073     if (!p.HasInputRange()) p.SetInputRange();
00074     p.ProcessCubes(*this);
00075 
00076     outputName = outputName.addExtension(m_extension);
00077     createWorldFile(outputName);
00078   }
00079 
00080 
00086   int ImageExporter::samples() const {
00087     return m_samples;
00088   }
00089 
00090 
00096   int ImageExporter::lines() const {
00097     return m_lines;
00098   }
00099 
00100 
00106   int ImageExporter::bands() const {
00107     return m_bands;
00108   }
00109 
00110 
00119   double ImageExporter::getInputMinimum(int channel) const {
00120     return m_process->GetInputMinimum(channel);
00121   }
00122 
00123 
00132   double ImageExporter::getInputMaximum(int channel) const {
00133     return m_process->GetInputMaximum(channel);
00134   }
00135 
00136 
00145   void ImageExporter::setOutputRange(double min, double max) {
00146     m_dataMin = min;
00147     m_dataMax = max;
00148   }
00149 
00150 
00157   void ImageExporter::setExtension(QString extension) {
00158     m_extension = extension;
00159 
00160     // World file extension is the first and last characters of the extension
00161     // with an added 'w' at the end
00162     int last = extension.length() - 1;
00163     m_world = extension.mid(0, 1) + extension.mid(last) + "w";
00164   }
00165 
00166 
00177   Cube * ImageExporter::setInput(ExportDescription &desc) {
00178     switch (desc.channelCount()) {
00179       case 1:
00180         m_writeMethod = &ImageExporter::writeGrayscale;
00181         break;
00182       case 3:
00183         m_writeMethod = &ImageExporter::writeRgb;
00184         break;
00185       case 4:
00186         m_writeMethod = &ImageExporter::writeRgba;
00187         break;
00188       default:
00189         throw IException(IException::Programmer,
00190             "Cannot export an image with [" + QString(desc.channelCount()) +
00191             "] channels",
00192             _FILEINFO_);
00193     }
00194 
00195     ProcessExport &p = getProcess();
00196     Cube *cube = addChannel(desc, 0);
00197     m_samples = cube->sampleCount();
00198     m_lines = cube->lineCount();
00199     m_bands = desc.channelCount();
00200 
00201     for (int i = 1; i < desc.channelCount(); i++) addChannel(desc, i);
00202 
00203     p.SetOutputRange(1.0, 255.0);
00204     p.SetOutputNull(0.0);
00205     return cube;
00206   }
00207 
00208 
00215   ProcessExport & ImageExporter::getProcess() const {
00216     return *m_process;
00217   }
00218 
00219 
00228   int ImageExporter::getPixel(double dn) const {
00229     return (dn < m_dataMin) ? m_dataMin : (dn > m_dataMax) ? m_dataMax : dn;
00230   }
00231 
00232 
00242   Cube * ImageExporter::addChannel(ExportDescription &desc, int i) {
00243     ProcessExport &p = getProcess();
00244 
00245     const ExportDescription::ChannelDescription &channel = desc.getChannel(i);
00246     Cube *cube = p.SetInputCube(
00247         channel.filename().expanded(), channel.attributes(), Isis::OneBand);
00248 
00249     if (channel.hasCustomRange())
00250       p.SetInputRange(channel.getInputMinimum(), channel.getInputMaximum(), i);
00251 
00252     return cube;
00253   }
00254 
00255 
00261   void ImageExporter::createWorldFile(FileName outputName) {
00262     outputName = outputName.removeExtension();
00263     outputName = outputName.addExtension(m_world);
00264 
00265     ProcessExport &p = getProcess();
00266     p.CreateWorldFile(outputName.expanded());
00267     p.EndProcess();
00268   }
00269 
00270 
00287   ImageExporter * ImageExporter::fromFormat(QString format) {
00288     ImageExporter *exporter = NULL;
00289 
00290     format = format.toLower();
00291     if (TiffExporter::canWriteFormat(format)) {
00292       exporter = new TiffExporter();
00293     }
00294     else if (QtExporter::canWriteFormat(format)) {
00295       exporter = new QtExporter(format);
00296     }
00297     else if (JP2Exporter::canWriteFormat(format)) {
00298       exporter = new JP2Exporter();
00299     }
00300     else {
00301       throw IException(IException::Programmer,
00302           "Cannot export image as format [" + format + "]",
00303           _FILEINFO_);
00304     }
00305 
00306     return exporter;
00307   }
00308 };
00309