Isis 3.0 Programmer Reference
Back | Home
TiffExporter.cpp
1 #include "TiffExporter.h"
2 
3 #include <QDebug>
4 
5 #include "Buffer.h"
6 #include "FileName.h"
7 #include "IException.h"
8 #include "IString.h"
9 
10 using namespace Isis;
11 
12 
13 namespace Isis {
18  m_image = NULL;
19  m_raster = NULL;
20 
21  setExtension("tif");
22  }
23 
24 
29  if (m_image) {
30  TIFFClose(m_image);
31  m_image = NULL;
32  }
33 
34  delete [] m_raster;
35  m_raster = NULL;
36  }
37 
38 
44  PixelType type = pixelType();
45  int mult = (type == Isis::UnsignedByte) ? 1 : 2;
46  int size = samples() * bands() * mult;
47 
48  try {
49  m_raster = new unsigned char[size];
50  }
51  catch (...) {
53  "Could not allocate enough memory", _FILEINFO_);
54  }
55  }
56 
57 
67  void TiffExporter::write(FileName outputName, int quality,
68  QString compression) {
69 
70  outputName = outputName.addExtension(extension());
71 
72  // Open the output image
73  m_image = TIFFOpen(outputName.expanded().toLatin1().data(), "w");
74 
75  if (m_image == NULL) {
77  "Could not open output image", _FILEINFO_);
78  }
79 
80  TIFFSetField(m_image, TIFFTAG_IMAGEWIDTH, samples());
81  TIFFSetField(m_image, TIFFTAG_IMAGELENGTH, lines());
82  TIFFSetField(m_image, TIFFTAG_ROWSPERSTRIP, 1);
83  if (compression == "packbits") {
84  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
85  }
86  else if (compression == "lzw") {
87  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
88  }
89  else if (compression == "deflate") {
90  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
91  }
92  else if (compression == "none") {
93  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
94  }
95  else {
96  QString msg = "Invalid TIFF compression algorithm: " + compression;
98  }
99  TIFFSetField(m_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
100  TIFFSetField(m_image, TIFFTAG_PHOTOMETRIC,
101  bands() == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
102 
103  PixelType type = pixelType();
104  int bps = (type == Isis::UnsignedByte) ? 8 : 16;
105  TIFFSetField(m_image, TIFFTAG_BITSPERSAMPLE, bps);
106 
107  TIFFSetField(m_image, TIFFTAG_SAMPLESPERPIXEL, bands());
108 
109  ImageExporter::write(outputName, quality);
110  }
111 
112 
121  void TiffExporter::setBuffer(int s, int b, int dn) const {
122  PixelType type = pixelType();
123  int index = s * bands() + b;
124 
125  switch (type) {
126  case UnsignedByte:
127  m_raster[index] = (unsigned char) dn;
128  break;
129  case SignedWord:
130  ((short int *) m_raster)[index] = (short int) dn;
131  break;
132  case UnsignedWord:
133  ((short unsigned int *) m_raster)[index] = (short unsigned int) dn;
134  break;
135  default:
137  "Invalid pixel type for data [" + toString(type) + "]",
138  _FILEINFO_);
139  }
140  }
141 
142 
148  void TiffExporter::writeLine(int l) const {
149  if (!TIFFWriteScanline(m_image, m_raster, l)) {
151  "Could not write image", _FILEINFO_);
152  }
153  }
154 
155 
163  bool TiffExporter::canWriteFormat(QString format) {
164  return format == "tiff";
165  }
166 };
167 
static bool canWriteFormat(QString format)
Returns true if the format is &quot;tiff&quot;.
int samples() const
Number of samples (columns) in the output image.
File name manipulation and expansion.
Definition: FileName.h:111
virtual void createBuffer()
Creates the buffer to store a chunk of streamed line data with one or more bands. ...
virtual void writeLine(int l) const
Writes a line of buffered data to the output image on disk.
virtual void setBuffer(int s, int b, int dn) const
Set the DN value at the given sample and band, resolved to a single index, of the line buffer...
TIFF * m_image
Object responsible for writing data to the output image.
Definition: TiffExporter.h:75
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:154
void setExtension(QString extension)
Sets the extension for the output image and generates the extension for the world file from it...
PixelType pixelType() const
Returns the pixel type.
int bands() const
Number of bands (channels) in the output image.
int lines() const
Number of lines (rows) in the output image.
QString extension() const
Gets the extension for the output image.
PixelType
Enumerations for Isis Pixel Types.
Definition: PixelType.h:43
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:38
unsigned char * m_raster
Array containing all color channels for a line.
Definition: TiffExporter.h:78
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:126
virtual ~TiffExporter()
Destruct the exporter.
TiffExporter()
Construct the TIFF exporter.
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:99
Exports cubes into a standard format in incremental pieces.
virtual void write(FileName outputName, int quality=100, QString compression="none")
Open the output file for writing, initialize its fields, then let the base ImageExporter handle the g...

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 ISIS Support Center
File Modified: 07/12/2023 23:30:51