Isis 3 Programmer Reference
TiffExporter.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "TiffExporter.h"
8 
9 #include <QDebug>
10 
11 #include "Buffer.h"
12 #include "FileName.h"
13 #include "IException.h"
14 #include "IString.h"
15 
16 using namespace Isis;
17 
18 
19 namespace Isis {
24  m_image = NULL;
25  m_raster = NULL;
26 
27  setExtension("tif");
28  }
29 
30 
35  if (m_image) {
36  TIFFClose(m_image);
37  m_image = NULL;
38  }
39 
40  delete [] m_raster;
41  m_raster = NULL;
42  }
43 
44 
50  PixelType type = pixelType();
51  int mult = (type == Isis::UnsignedByte) ? 1 : 2;
52  int size = samples() * bands() * mult;
53 
54  try {
55  m_raster = new unsigned char[size];
56  }
57  catch (...) {
59  "Could not allocate enough memory", _FILEINFO_);
60  }
61  }
62 
63 
73  void TiffExporter::write(FileName outputName, int quality,
74  QString compression) {
75 
76  outputName = outputName.addExtension(extension());
77 
78  // Open the output image
79  m_image = TIFFOpen(outputName.expanded().toLatin1().data(), "w");
80 
81  if (m_image == NULL) {
83  "Could not open output image", _FILEINFO_);
84  }
85 
86  TIFFSetField(m_image, TIFFTAG_IMAGEWIDTH, samples());
87  TIFFSetField(m_image, TIFFTAG_IMAGELENGTH, lines());
88  TIFFSetField(m_image, TIFFTAG_ROWSPERSTRIP, 1);
89  if (compression == "packbits") {
90  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
91  }
92  else if (compression == "lzw") {
93  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
94  }
95  else if (compression == "deflate") {
96  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
97  }
98  else if (compression == "none") {
99  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
100  }
101  else {
102  QString msg = "Invalid TIFF compression algorithm: " + compression;
103  throw IException(IException::Programmer, msg, _FILEINFO_);
104  }
105  TIFFSetField(m_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
106  TIFFSetField(m_image, TIFFTAG_PHOTOMETRIC,
107  bands() == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
108 
109  PixelType type = pixelType();
110  int bps = (type == Isis::UnsignedByte) ? 8 : 16;
111  TIFFSetField(m_image, TIFFTAG_BITSPERSAMPLE, bps);
112 
113  TIFFSetField(m_image, TIFFTAG_SAMPLESPERPIXEL, bands());
114 
115  ImageExporter::write(outputName, quality);
116  }
117 
118 
127  void TiffExporter::setBuffer(int s, int b, int dn) const {
128  PixelType type = pixelType();
129  int index = s * bands() + b;
130 
131  switch (type) {
132  case UnsignedByte:
133  m_raster[index] = (unsigned char) dn;
134  break;
135  case SignedWord:
136  ((short int *) m_raster)[index] = (short int) dn;
137  break;
138  case UnsignedWord:
139  ((short unsigned int *) m_raster)[index] = (short unsigned int) dn;
140  break;
141  default:
143  "Invalid pixel type for data [" + toString(type) + "]",
144  _FILEINFO_);
145  }
146  }
147 
148 
154  void TiffExporter::writeLine(int l) const {
155  if (!TIFFWriteScanline(m_image, m_raster, l)) {
157  "Could not write image", _FILEINFO_);
158  }
159  }
160 
161 
169  bool TiffExporter::canWriteFormat(QString format) {
170  return format == "tiff";
171  }
172 };
173 
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::IException::Unknown
@ Unknown
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:118
Isis::TiffExporter::m_image
TIFF * m_image
Object responsible for writing data to the output image.
Definition: TiffExporter.h:59
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::TiffExporter::m_raster
unsigned char * m_raster
Array containing all color channels for a line.
Definition: TiffExporter.h:62
Isis::StreamExporter
Exports cubes into a standard format in incremental pieces.
Definition: StreamExporter.h:34
Isis::ImageExporter::write
virtual void write(FileName outputName, int quality=100, QString compression="none")
Export the Isis cube channels to the given standard image.
Definition: ImageExporter.cpp:101
Isis::FileName::expanded
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:196
Isis::TiffExporter::TiffExporter
TiffExporter()
Construct the TIFF exporter.
Definition: TiffExporter.cpp:23
Isis::FileName::addExtension
FileName addExtension(const QString &extension) const
Adds a new extension to the file name.
Definition: FileName.cpp:225
Isis::TiffExporter::~TiffExporter
virtual ~TiffExporter()
Destruct the exporter.
Definition: TiffExporter.cpp:34
Isis::ImageExporter::extension
QString extension() const
Gets the extension for the output image.
Definition: ImageExporter.cpp:208
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::PixelType
PixelType
Enumerations for Isis Pixel Types.
Definition: PixelType.h:27
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::ImageExporter::lines
int lines() const
Number of lines (rows) in the output image.
Definition: ImageExporter.cpp:128
Isis::ImageExporter::bands
int bands() const
Number of bands (channels) in the output image.
Definition: ImageExporter.cpp:138
Isis::TiffExporter::writeLine
virtual void writeLine(int l) const
Writes a line of buffered data to the output image on disk.
Definition: TiffExporter.cpp:154
Isis::TiffExporter::setBuffer
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.
Definition: TiffExporter.cpp:127
Isis::ImageExporter::pixelType
PixelType pixelType() const
Returns the pixel type.
Definition: ImageExporter.cpp:307
Isis::ImageExporter::samples
int samples() const
Number of samples (columns) in the output image.
Definition: ImageExporter.cpp:118
Isis::TiffExporter::canWriteFormat
static bool canWriteFormat(QString format)
Returns true if the format is "tiff".
Definition: TiffExporter.cpp:169
Isis::TiffExporter::write
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...
Definition: TiffExporter.cpp:73
Isis::ImageExporter::setExtension
void setExtension(QString extension)
Sets the extension for the output image and generates the extension for the world file from it.
Definition: ImageExporter.cpp:193
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::TiffExporter::createBuffer
virtual void createBuffer()
Creates the buffer to store a chunk of streamed line data with one or more bands.
Definition: TiffExporter.cpp:49