USGS

Isis 3.0 Object Programmers' Reference

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  // Open the output image
70  m_image = TIFFOpen(outputName.expanded().toAscii().data(), "w");
71 
72  if (m_image == NULL) {
74  "Could not open output image", _FILEINFO_);
75  }
76 
77  TIFFSetField(m_image, TIFFTAG_IMAGEWIDTH, samples());
78  TIFFSetField(m_image, TIFFTAG_IMAGELENGTH, lines());
79  TIFFSetField(m_image, TIFFTAG_ROWSPERSTRIP, 1);
80  if (compression == "packbits") {
81  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
82  }
83  else if (compression == "lzw") {
84  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
85  }
86  else if (compression == "deflate") {
87  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
88  }
89  else if (compression == "none") {
90  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
91  }
92  else {
93  QString msg = "Invalid TIFF compression algorithm: " + compression;
95  }
96  TIFFSetField(m_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
97  TIFFSetField(m_image, TIFFTAG_PHOTOMETRIC,
98  bands() == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
99 
100  PixelType type = pixelType();
101  int bps = (type == Isis::UnsignedByte) ? 8 : 16;
102  TIFFSetField(m_image, TIFFTAG_BITSPERSAMPLE, bps);
103 
104  TIFFSetField(m_image, TIFFTAG_SAMPLESPERPIXEL, bands());
105 
106  ImageExporter::write(outputName, quality);
107  }
108 
109 
118  void TiffExporter::setBuffer(int s, int b, int dn) const {
119  PixelType type = pixelType();
120  int index = s * bands() + b;
121 
122  switch (type) {
123  case UnsignedByte:
124  m_raster[index] = (unsigned char) dn;
125  break;
126  case SignedWord:
127  ((short int *) m_raster)[index] = (short int) dn;
128  break;
129  case UnsignedWord:
130  ((short unsigned int *) m_raster)[index] = (short unsigned int) dn;
131  break;
132  default:
134  "Invalid pixel type for data [" + toString(type) + "]",
135  _FILEINFO_);
136  }
137  }
138 
139 
145  void TiffExporter::writeLine(int l) const {
146  if (!TIFFWriteScanline(m_image, m_raster, l)) {
148  "Could not write image", _FILEINFO_);
149  }
150  }
151 
152 
160  bool TiffExporter::canWriteFormat(QString format) {
161  return format == "tiff";
162  }
163 };
164