File: tiff_support.hpp

package info (click to toggle)
gamera 3.4.1%2Bsvn1423-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 22,292 kB
  • ctags: 25,015
  • sloc: xml: 122,324; ansic: 50,812; cpp: 50,489; python: 34,987; makefile: 119; sh: 101
file content (416 lines) | stat: -rw-r--r-- 13,157 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 *
 * Copyright (C) 2001-2009 Ichiro Fujinaga, Michael Droettboom,
 * Karl MacMillan, and Christoph Dalitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef kwm10222002_tiff_support
#define kwm10222002_tiff_support

#include "gamera.hpp"
#include <tiffio.h>
#include <string>
#include <exception>
#include <stdexcept>
#include <bitset>

namespace Gamera {

// forward declarations
ImageInfo* tiff_info(const char* filename);
Image* load_tiff(const char* filename, int compressed);
template<class T>
void save_tiff(const T& matrix, const char* filename);

/*
  Get information about tiff images

  This function gets informtion about tiff images and places it in and
  ImageInfo object.  See image_info.hpp for more information.
*/
ImageInfo* tiff_info(const char* filename) {
  TIFFErrorHandler saved_handler = TIFFSetErrorHandler(NULL);
  TIFF* tif = 0;
  tif = TIFFOpen(filename, "r");
  if (tif == 0) {
    TIFFSetErrorHandler(saved_handler);
    throw std::invalid_argument("Failed to open image header");
  }
  // Create this later so it isn't leaked if filename does
  // not exist or is not a TIFF file.
  ImageInfo* info = new ImageInfo();

  /*
    The tiff library seems very sensitive to type yet provides only a
    stupid non-type-checked interface.  The following seems to work well
    (notice that resolution is floating point).  KWM 6/6/01
   */
  try {
    unsigned short tmp;
    uint32 size;
    TIFFGetFieldDefaulted(tif, TIFFTAG_IMAGEWIDTH, &size);
    info->ncols((size_t)size);
    TIFFGetFieldDefaulted(tif, TIFFTAG_IMAGELENGTH, &size);
    info->nrows((size_t)size);
    TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &tmp);
    info->depth((size_t)tmp);
    float res;
    TIFFGetFieldDefaulted(tif, TIFFTAG_XRESOLUTION, &res);
    info->x_resolution(res);
    TIFFGetFieldDefaulted(tif, TIFFTAG_YRESOLUTION, &res);
    info->y_resolution(res);
    TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &tmp);
    info->ncolors((size_t)tmp);
    TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &tmp);
    info->inverted(tmp == PHOTOMETRIC_MINISWHITE);
    
    TIFFClose(tif);
  } catch (std::exception e) {
    TIFFSetErrorHandler(saved_handler);
    delete info;
  }
  TIFFSetErrorHandler(saved_handler);
  return info;
}
namespace {

  template<class T>
  void tiff_load_onebit(T& matrix, ImageInfo& info, const char* filename) {
    // open the image
    TIFF* tif = TIFFOpen(filename, "r");
    tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif));
    
    // load the data
    for (size_t i = 0; i < info.nrows(); i++) {
      TIFFReadScanline(tif, buf, i);
      char* data = (char *)buf;
      std::bitset<8> bits;
      int tmp;
      for (size_t j = 0, k = 7, bit_index = 0; j < info.ncols(); j++, k--) {
        if (k == 7) {
          bits = data[bit_index];
          bit_index++;
        }
        if (bits[k])
          tmp = pixel_traits<OneBitPixel>::black();
        else
          tmp = pixel_traits<OneBitPixel>::white(); 
        matrix.set(Point(j, i), tmp);
        if (k == 0)
          k = 8;
      }
    }
    // do the cleanup
    _TIFFfree(buf);
    TIFFClose(tif);
  }

  template<class T>
  void tiff_load_greyscale(T& matrix, ImageInfo& info, const char* filename) {
    // open the image
    TIFF* tif = TIFFOpen(filename, "r");
    tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif));
    
    typename T::row_iterator mi = matrix.row_begin();
    typename T::col_iterator mj;
    unsigned char* data;
    if (info.inverted()) {
      for (size_t i = 0; i < info.nrows(); i++, mi++) {
        mj = mi.begin();
        TIFFReadScanline(tif, buf, i);
        data = (unsigned char *)buf;
        for (size_t j = 0; j < info.ncols(); j++, mj++) {
          *mj = 255 - data[j];
        }
      }
    } else {
      for (size_t i = 0; i < info.nrows(); i++, mi++) {
        mj = mi.begin();
        TIFFReadScanline(tif, buf, i);
        data = (unsigned char *)buf;
        for (size_t j = 0; j < info.ncols(); j++, mj++) {
          *mj = data[j];
        }
      }
    }
    
    // do the cleanup
    _TIFFfree(buf);
    TIFFClose(tif);
  }

  template<class T>
  void tiff_load_grey16(T& matrix, ImageInfo& info, const char* filename) {
    // open the image
    TIFF* tif = TIFFOpen(filename, "r");
    tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif));
    
    typename T::row_iterator mi = matrix.row_begin();
    typename T::col_iterator mj;
    unsigned short* data;
    for (size_t i = 0; i < info.nrows(); i++, mi++) {
      mj = mi.begin();
      TIFFReadScanline(tif, buf, i);
      data = (unsigned short *)buf;
      for (size_t j = 0; j < info.ncols(); j++, mj++) {
        *mj = data[j];
      }
    }
    
    // do the cleanup
    _TIFFfree(buf);
    TIFFClose(tif);
  }

  template<class T>
  void tiff_load_rgb(T& matrix, ImageInfo& info, const char* filename) {
    // open the image
    TIFF* tif = TIFFOpen(filename, "r");
    tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif));
    
    typename T::row_iterator mi = matrix.row_begin();
    typename T::col_iterator mj;
    unsigned char* data;
    for (size_t i = 0; i < info.nrows(); i++, mi++) {
      mj = mi.begin();
      TIFFReadScanline(tif, buf, i);
      data = (unsigned char *)buf;
      for (size_t j = 0; j < info.ncols() * 3; j += 3, mj++) {
        (*mj).red(data[j]);
        (*mj).green(data[j + 1]);
        (*mj).blue(data[j + 2]);
      }
    }
    // do the cleanup
    _TIFFfree(buf);
    TIFFClose(tif);
  }

    template<class Pixel>
  struct tiff_saver {

  };


  // runtime test for endianness
  // (safer and less cumbersome than querying compiler macros)
  bool byte_order_little_endian() {
    long numberone = 1;
    return (*((char*)(&numberone)));
  }

  template<>
  struct tiff_saver<OneBitPixel> {
    template<class T>
    void operator()(const T& matrix, TIFF* tif) {
      TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
      tsize_t scanline_size = TIFFScanlineSize(tif);
      if (scanline_size % 4) // round up to multiple of 4
        scanline_size += 4 - (scanline_size % 4);
      tdata_t buf = _TIFFmalloc(scanline_size);
      if (!buf)
        throw std::runtime_error("Error allocating scanline");
      TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
      std::bitset<32> bits;
      uint32* data = (uint32 *)buf;
      bool little_endian = byte_order_little_endian();
      typename T::const_vec_iterator it = matrix.vec_begin();
      for (size_t i = 0; i < matrix.nrows(); i++) {
        size_t bit_index = 0;
        int k = 31;
        for (size_t j = 0; j < matrix.ncols(); k--) {
          if (k < 0) {
            data[bit_index] = bits.to_ulong();
            if (little_endian)
              byte_swap32((unsigned char *)&data[bit_index]);
            bit_index++;
            k = 32;
            continue;
          }
          if (is_black(*it))
            bits[k] = 1;
          else
            bits[k] = 0;
          j++;
          it++;
        }
        // The last 32 pixels need to be saved, even if they are not full
        if (k != 31) {
          data[bit_index] = bits.to_ulong();
          if (little_endian)
            byte_swap32((unsigned char *)&data[bit_index]);
        }
        TIFFWriteScanline(tif, buf, i);
      }
      _TIFFfree(buf);
    }
  };

  template<>
  struct tiff_saver<GreyScalePixel> {
    template<class T>
    void operator()(const T& matrix, TIFF* tif) {
      TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
      tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif));
      if (!buf)
        throw std::runtime_error("Error allocating scanline");
      typename T::value_type pix;
      unsigned char* data = (unsigned char *)buf;
      for (size_t i = 0; i < matrix.nrows(); i++) {
        for (size_t j = 0; j < matrix.ncols(); j++) {
          pix = matrix[i][j];
          data[j] = (unsigned char)pix;
        }
        TIFFWriteScanline(tif, buf, i);
      }
      _TIFFfree(buf);
    }
  };

  template<>
  struct tiff_saver<Grey16Pixel> {
    template<class T>
    void operator()(const T& matrix, TIFF* tif) {
      TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
      tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif));
      if (!buf)
        throw std::runtime_error("Error allocating scanline");
      typename T::value_type pix;
      unsigned short* data = (unsigned short *)buf;
      for (size_t i = 0; i < matrix.nrows(); i++) {
        for (size_t j = 0; j < matrix.ncols(); j++) {
          pix = matrix[i][j];
          data[j] = (unsigned short)pix;
        }
        TIFFWriteScanline(tif, buf, i);
      }
      _TIFFfree(buf);
    }
  };

  template<>
  struct tiff_saver<RGBPixel> {
    template<class T>
    void operator()(const T& matrix, TIFF* tif) {
      TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
      tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif));
      if (!buf)
        throw std::runtime_error("Error allocating scanline");
      typename T::value_type pix;
      unsigned char* data = (unsigned char *)buf;
      for (size_t i = 0; i < matrix.nrows(); i++) {
        for (size_t j = 0, k = 0; j < matrix.ncols(); j++) {
          pix = matrix[i][j];
          data[k++] = pix.red();
          data[k++] = pix.green();
          data[k++] = pix.blue();
        }
        TIFFWriteScanline(tif, buf, i);
      }
      _TIFFfree(buf);
    }
  };
}

Image* load_tiff(const char* filename, int storage) {
  TIFFErrorHandler saved_handler = TIFFSetErrorHandler(NULL);
  ImageInfo* info = tiff_info(filename);
  if (info->ncolors() == 1) {
    if (info->depth() == 1) {
      if (storage == DENSE) {
        typedef TypeIdImageFactory<ONEBIT, DENSE> fact_type;
        fact_type::image_type*
          image = fact_type::create(Point(0, 0), Dim(info->ncols(), info->nrows()));
        image->resolution(info->x_resolution());
        tiff_load_onebit(*image, *info, filename);
        delete info;
        TIFFSetErrorHandler(saved_handler);
        return image;
      } else {
        typedef TypeIdImageFactory<ONEBIT, RLE> fact_type;
        fact_type::image_type*
          image = fact_type::create(Point(0, 0), Dim(info->ncols(), info->nrows()));
        image->resolution(info->x_resolution());
        tiff_load_onebit(*image, *info, filename);
        delete info;
        TIFFSetErrorHandler(saved_handler);
        return image;
      }
    }
  }
  if (storage == RLE) {
    delete info;
    TIFFSetErrorHandler(saved_handler);
    throw std::runtime_error("Pixel type must be OneBit to use RLE data.");
  }
  if (info->ncolors() == 3) {
    typedef TypeIdImageFactory<RGB, DENSE> fact;
    fact::image_type* image =
      fact::create(Point(0, 0), Dim(info->ncols(), info->nrows()));
    tiff_load_rgb(*image, *info, filename);
    delete info;
    TIFFSetErrorHandler(saved_handler);
    return image;
  } else if (info->depth() == 8) {
    typedef TypeIdImageFactory<GREYSCALE, DENSE> fact_type;
    fact_type::image_type*
      image = fact_type::create(Point(0, 0), Dim(info->ncols(), info->nrows()));
    image->resolution(info->x_resolution());
    tiff_load_greyscale(*image, *info, filename);
    delete info;
    TIFFSetErrorHandler(saved_handler);
    return image;
  } else if (info->depth() == 16) {
    typedef TypeIdImageFactory<GREY16, DENSE> fact_type;
    fact_type::image_type*
      image = fact_type::create(Point(0, 0), Dim(info->ncols(), info->nrows()));
    image->resolution(info->x_resolution());
    tiff_load_greyscale(*image, *info, filename);
    delete info;
    TIFFSetErrorHandler(saved_handler);
    return image;
  }
  delete info;
  TIFFSetErrorHandler(saved_handler);
  throw std::runtime_error("Unable to load image of this type!");
  return 0;
}

template<class T>
void save_tiff(const T& matrix, const char* filename) {
  TIFF* tif = 0;
  tif = TIFFOpen(filename, "w");
  if (tif == 0)
    throw std::invalid_argument("Failed to create image.");

  TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, matrix.ncols());
  TIFFSetField(tif, TIFFTAG_IMAGELENGTH, matrix.nrows());
  TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, matrix.depth());
  TIFFSetField(tif, TIFFTAG_XRESOLUTION, matrix.resolution());
  TIFFSetField(tif, TIFFTAG_YRESOLUTION, matrix.resolution());
  TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, matrix.ncolors());
  TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);


  tiff_saver<typename T::value_type> saver;
  saver(matrix, tif);
        
  TIFFClose(tif);
}

}
#endif