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#include "UserInterface.h"
16
17using namespace Isis;
18
19
20namespace Isis {
25 m_image = NULL;
26 m_raster = NULL;
27
28 setExtension("tif");
29 }
30
31
36 if (m_image) {
37 TIFFClose(m_image);
38 m_image = NULL;
39 }
40
41 delete [] m_raster;
42 m_raster = NULL;
43 }
44
45
51 PixelType type = pixelType();
52 int mult = (type == Isis::UnsignedByte) ? 1 : 2;
53 int size = samples() * bands() * mult;
54
55 try {
56 m_raster = new unsigned char[size];
57 }
58 catch (...) {
60 "Could not allocate enough memory", _FILEINFO_);
61 }
62 }
63
64
74 void TiffExporter::write(FileName outputName, int quality,
75 QString compression, UserInterface *ui) {
76
77 outputName = outputName.addExtension(extension());
78
79 // Open the output image
80 m_image = TIFFOpen(outputName.expanded().toLatin1().data(), "w");
81
82 if (m_image == NULL) {
84 "Could not open output image", _FILEINFO_);
85 }
86
87 TIFFSetField(m_image, TIFFTAG_IMAGEWIDTH, samples());
88 TIFFSetField(m_image, TIFFTAG_IMAGELENGTH, lines());
89 TIFFSetField(m_image, TIFFTAG_ROWSPERSTRIP, 1);
90 if (compression == "packbits") {
91 TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
92 }
93 else if (compression == "lzw") {
94 TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
95 }
96 else if (compression == "deflate") {
97 TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
98 }
99 else if (compression == "none") {
100 TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
101 }
102 else {
103 QString msg = "Invalid TIFF compression algorithm: " + compression;
104 throw IException(IException::Programmer, msg, _FILEINFO_);
105 }
106 TIFFSetField(m_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
107 TIFFSetField(m_image, TIFFTAG_PHOTOMETRIC,
108 bands() == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
109
110 PixelType type = pixelType();
111 int bps = (type == Isis::UnsignedByte) ? 8 : 16;
112 TIFFSetField(m_image, TIFFTAG_BITSPERSAMPLE, bps);
113 int sampleFormat = (type == Isis::SignedWord) ? 2 : 1 ;
114 TIFFSetField(m_image, TIFFTAG_SAMPLEFORMAT, sampleFormat);
115
116 TIFFSetField(m_image, TIFFTAG_SAMPLESPERPIXEL, bands());
117
118 ImageExporter::write(outputName, quality, compression, ui);
119 }
120
121
130 void TiffExporter::setBuffer(int s, int b, int dn) const {
131 PixelType type = pixelType();
132 int index = s * bands() + b;
133
134 switch (type) {
135 case UnsignedByte:
136 m_raster[index] = (unsigned char) dn;
137 break;
138 case SignedWord:
139 ((short int *) m_raster)[index] = (short int) dn;
140 break;
141 case UnsignedWord:
142 ((short unsigned int *) m_raster)[index] = (short unsigned int) dn;
143 break;
144 default:
146 "Invalid pixel type for data [" + toString(type) + "]",
147 _FILEINFO_);
148 }
149 }
150
151
157 void TiffExporter::writeLine(int l) const {
158 if (!TIFFWriteScanline(m_image, m_raster, l)) {
160 "Could not write image", _FILEINFO_);
161 }
162 }
163
164
172 bool TiffExporter::canWriteFormat(QString format) {
173 return format == "tiff";
174 }
175};
176
File name manipulation and expansion.
Definition FileName.h:100
Isis exception class.
Definition IException.h:91
@ Unknown
A type of error that cannot be classified as any of the other error types.
Definition IException.h:118
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
int lines() const
Number of lines (rows) in the output image.
QString extension() const
Gets the extension for the output image.
int bands() const
Number of bands (channels) in the output image.
virtual void write(FileName outputName, int quality=100, QString compression="none", UserInterface *ui=nullptr)
Export the Isis cube channels to the given standard image.
int samples() const
Number of samples (columns) in the output image.
PixelType pixelType() const
Returns the pixel type.
void setExtension(QString extension)
Sets the extension for the output image and generates the extension for the world file from it.
Exports cubes into a standard format in incremental pieces.
static bool canWriteFormat(QString format)
Returns true if the format is "tiff".
virtual void writeLine(int l) const
Writes a line of buffered data to the output image on disk.
TIFF * m_image
Object responsible for writing data to the output image.
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.
virtual void write(FileName outputName, int quality=100, QString compression="none", UserInterface *ui=nullptr)
Open the output file for writing, initialize its fields, then let the base ImageExporter handle the g...
TiffExporter()
Construct the TIFF exporter.
unsigned char * m_raster
Array containing all color channels for a line.
virtual ~TiffExporter()
Destruct the exporter.
virtual void createBuffer()
Creates the buffer to store a chunk of streamed line data with one or more bands.
Command Line and Xml loader, validation, and access.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211
PixelType
Enumerations for Isis Pixel Types.
Definition PixelType.h:27