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 
65  void TiffExporter::write(FileName outputName, int quality) {
66  // Open the output image
67  m_image = TIFFOpen(outputName.expanded().toAscii().data(), "w");
68 
69  if (m_image == NULL) {
71  "Could not open output image", _FILEINFO_);
72  }
73 
74  TIFFSetField(m_image, TIFFTAG_IMAGEWIDTH, samples());
75  TIFFSetField(m_image, TIFFTAG_IMAGELENGTH, lines());
76  TIFFSetField(m_image, TIFFTAG_ROWSPERSTRIP, 1);
77  TIFFSetField(m_image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
78  TIFFSetField(m_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
79 
80  TIFFSetField(m_image, TIFFTAG_PHOTOMETRIC,
81  bands() == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
82 
83  PixelType type = pixelType();
84  int bps = (type == Isis::UnsignedByte) ? 8 : 16;
85  TIFFSetField(m_image, TIFFTAG_BITSPERSAMPLE, bps);
86 
87  TIFFSetField(m_image, TIFFTAG_SAMPLESPERPIXEL, bands());
88 
89  ImageExporter::write(outputName, quality);
90  }
91 
92 
101  void TiffExporter::setBuffer(int s, int b, int dn) const {
102  PixelType type = pixelType();
103  int index = s * bands() + b;
104 
105  switch (type) {
106  case UnsignedByte:
107  m_raster[index] = (unsigned char) dn;
108  break;
109  case SignedWord:
110  ((short int *) m_raster)[index] = (short int) dn;
111  break;
112  case UnsignedWord:
113  ((short unsigned int *) m_raster)[index] = (short unsigned int) dn;
114  break;
115  default:
117  "Invalid pixel type for data [" + toString(type) + "]",
118  _FILEINFO_);
119  }
120  }
121 
122 
128  void TiffExporter::writeLine(int l) const {
129  if (!TIFFWriteScanline(m_image, m_raster, l)) {
131  "Could not write image", _FILEINFO_);
132  }
133  }
134 
135 
143  bool TiffExporter::canWriteFormat(QString format) {
144  return format == "tiff";
145  }
146 };
147