1 // This is core/vil/file_formats/vil_tiff.h
2 #ifndef vil_tiff_file_format_h_
3 #define vil_tiff_file_format_h_
4 //:
5 // \file
6 // \author awf@robots.ox.ac.uk
7 // \date   16 Feb 2000
8 //
9 // \verbatim
10 //  Modifications
11 //   3 Oct 2001 Peter Vanroose - Implemented get_property and set_property
12 //   5 Jan 2002 Ian Scott      - Converted to vil
13 //   9 Dec 2003 Peter Vanroose - Added support for 1-bit pixel (bitmapped) images
14 //   21 Dec 2005 J.L. Mundy - Substantial rewrite to handle a more
15 //       complete tiff 6.0 standard. Files with tiles can now be read and
16 //       written. Only tiled images are considered blocked, i.e. not strips.
17 //       Block dimensions must be a multiple of 16, for compatibility with
18 //       compression schemes. Tiff files with separate color bands are not handled
19 //   24 Mar 2007 J.L. Mundy - added smart pointer on TIFF handle to support
20 //       multiple resources from a single tiff file; required for pyramid
21 //   KNOWN BUG - 24bit samples for both nplanes = 1 and nplanes = 3
22 //   KNOWN BUG - bool pixel format write - crashes due to incorrect block size
23 // \endverbatim
24 
25 #include <vector>
26 #include <iostream>
27 #ifdef _MSC_VER
28 #  include <vcl_msvc_warnings.h>
29 #endif
30 #include <cassert>
31 #include <vil/vil_config.h>
32 #include <vil/vil_file_format.h>
33 #include <vil/vil_image_resource.h>
34 #include <vil/vil_memory_chunk.h>
35 #include <vil/vil_blocked_image_resource.h>
36 #include <vil/vil_pyramid_image_resource.h>
37 #include <vil/file_formats/vil_tiff_header.h>
38 #include <tiffio.h>
39 #if HAS_GEOTIFF
40 #include <xtiffio.h>
41 #include <vil/file_formats/vil_geotiff_header.h>
42 #endif
43 
44 
45 //: Loader for tiff files
46 class vil_tiff_file_format : public vil_file_format
47 {
48  public:
49   char const *tag() const override;
50   vil_image_resource_sptr make_input_image(vil_stream *vs) override;
51 
52   vil_pyramid_image_resource_sptr
53   make_input_pyramid_image(char const* file) override;
54 
55   //: Construct a pyramid image resource from a base image.
56   //  All levels are stored in the same resource file. Each level has the same
57   //  scale ratio (0.5) to the preceding level. Level 0 is the original
58   //  base image. The resource is returned open for reading.
59   //  The temporary directory is for storing intermediate image
60   //  resources during the construction of the pyramid. Files are
61   //  be removed from the directory after completion.  If temp_dir is 0
62   //  then the intermediate resources are created in memory.
63   vil_pyramid_image_resource_sptr
64   make_pyramid_image_from_base(char const* filename,
65                                vil_image_resource_sptr const& base_image,
66                                unsigned nlevels,
67                                char const* temp_dir) override;
68 
69   vil_image_resource_sptr make_output_image(vil_stream* vs,
70                                                     unsigned ni,
71                                                     unsigned nj,
72                                                     unsigned nplanes,
73                                                     enum vil_pixel_format) override;
74 
75   vil_blocked_image_resource_sptr
76   make_blocked_output_image(vil_stream* vs,
77                             unsigned ni,
78                             unsigned nj,
79                             unsigned nplanes,
80                             unsigned size_block_i,
81                             unsigned size_block_j,
82                             enum vil_pixel_format) override;
83 
84 
85   vil_pyramid_image_resource_sptr
86   make_pyramid_output_image(char const* file) override;
87 };
88 
89 struct tif_stream_structures;
90 class vil_tiff_header;
91 //Need to create a smartpointer mechanism for the tiff
92 //file in order to handle multiple images, e.g. for pyramid
93 //resource
94 //A reference counting wrapper for the TIFF handle
95 struct tif_ref_cnt
96 {
tif_ref_cnttif_ref_cnt97   tif_ref_cnt(TIFF* tif):tif_(tif), cnt_(0){}
tiftif_ref_cnt98   TIFF* tif(){return tif_;}
reftif_ref_cnt99   void ref(){cnt_++;}
unreftif_ref_cnt100   void unref(){
101     if (--cnt_<=0)
102     {
103 #if HAS_GEOTIFF
104     XTIFFClose(tif_);
105 #else
106     TIFFClose(tif_);
107 #endif // HAS_GEOTIFF
108       delete this;
109     }
110   }
111  private:
112   TIFF* tif_;
113   unsigned cnt_;
114 };
115 
116 //The smart pointer to the tiff handle
117 struct tif_smart_ptr
118 {
119   tif_smart_ptr() = default;
120 
tif_smart_ptrtif_smart_ptr121   tif_smart_ptr(tif_ref_cnt* tptr):tptr_(tptr)
122   { if (tptr_) tptr_->ref(); }
123 
tif_smart_ptrtif_smart_ptr124   tif_smart_ptr(tif_smart_ptr const& tp)
125   {tptr_ = tp.tptr_; if (tptr_) tptr_->ref();}
126 
~tif_smart_ptrtif_smart_ptr127   ~tif_smart_ptr()
128   {
129     // the strange order of events in this function is to avoid
130     // heap corruption if unref() causes *this to be deleted.
131     tif_ref_cnt* old_ptr = tptr_;
132     tptr_ = nullptr;
133     if (old_ptr)
134       old_ptr->unref();
135   }
136   //: Inverse bool
137   bool operator!() const
138   {
139     return (tptr_ != nullptr)? false : true;
140   }
141 
142   //: Convenient get TIFF* for header construction; assumes temporary use
tiftif_smart_ptr143   TIFF* tif() const {if (tptr_) return tptr_->tif(); return nullptr;}
144  private:
145    tif_ref_cnt *tptr_{nullptr};
146 };
147 
148 //: Generic image interface for image TIFF image files (could have multiple images)
149 class vil_tiff_image : public vil_blocked_image_resource
150 {
151   friend class vil_tiff_file_format;
152  public:
153   enum compression_methods
154   {
155     // the enum value must correspond to COMPRESSION_* constants defined in tiff.h.
156     // and has the same integer value.  For instance,  NONE corresponds to COMPRESSION_NONE
157     NONE = 1,             /* dump mode */
158     LZW = 5,             /* Lempel-Ziv  & Welch */
159     OJPEG = 6,           /* !6.0 JPEG */
160     JPEG = 7,            /* %JPEG DCT compression */
161     PACKBITS = 32773,    /* Macintosh RLE */
162     THUNDERSCAN = 32809, /* ThunderScan RLE */
163     PIXARFILM =32908,    /* Pixar companded 10bit LZW */
164     PIXARLOG = 32909,    /* Pixar companded 11bit ZIP */
165     DEFLATE = 32946,     /* Deflate compression */
166     ADOBE_DEFLATE = 8,   /* Deflate compression,
167                                 as recognized by Adobe */
168     JP2000 =  34712,     /* Leadtools JPEG2000 */
169     LZMA =  34925        /* LZMA2 */
170   };
171  public:
172   vil_tiff_image(tif_smart_ptr const& tif,
173                  vil_tiff_header* th, const unsigned nimages = 1);
174 
175   ~vil_tiff_image() override;
176 
177   //: Dimensions:  planes x width x height x components
178   unsigned nplanes() const override;
179   unsigned ni() const override;
180   unsigned nj() const override;
181 
182   enum vil_pixel_format pixel_format() const override;
183 
184   //: returns "tiff"
185   char const *file_format() const override;
186 
187 #if HAS_GEOTIFF
188   //: are there any geotiff tags
is_GEOTIFF()189   bool is_GEOTIFF() { return h_->is_GEOTIFF(); }
190 #endif
191 
192            // --- Block interface ---
193 
194   //: Block size in columns (must be a multiple of 16)
195   unsigned size_block_i() const override;
196 
197   //: Block size in rows (must be a multiple of 16)
198   unsigned size_block_j() const override;
199 
200   //: Number of blocks in image width
201   unsigned n_block_i() const override;
202 
203   //: Number of blocks in image height
204   unsigned n_block_j() const override;
205 
206   vil_image_view_base_sptr get_block( unsigned  block_index_i,
207                                               unsigned  block_index_j ) const override;
208 
209   bool put_block( unsigned  block_index_i, unsigned  block_index_j,
210                           const vil_image_view_base& blk ) override;
211 
212   //: Put the data in this view back into the image source.
213   bool put_view(const vil_image_view_base& im, unsigned i0, unsigned j0) override;
214 
215   //: Return true if the property given in the first argument has been set.
216   // currently defined:
217   //  "quantisation_depth" - number of relevant bits per pixel
218   //  "size_block_i" and "size_block_j" - block dimensions
219 
220   bool get_property(char const *tag, void *prop = nullptr) const override;
221 
222   bool set_compression_method(compression_methods cm);
223 
224   bool set_compression_quality(int quality);
225 
226 #if HAS_GEOTIFF
227   //* returns null if the tiff file does not include any geotiff tags
228   vil_geotiff_header* get_geotiff_header();
229 #endif
230 
231   friend class vil_tiff_pyramid_resource;
232 
233   //:indicates the number of images in the tiff file
nimages()234   unsigned int nimages() const {return nimages_;}
235 
236   //:the image index for multiple image files
index()237   unsigned int index() const {return index_;}
238   //:point to a particular image in the file
set_index(const unsigned int index)239   void set_index(const unsigned int index)
240     {assert(index<nimages_); index_=index;}
241   //: Get a smart pointer to opentiff object
tiff()242   tif_smart_ptr const& tiff() const
243   {
244     return t_;
245   }
246 
247   //: return the non-standard no_data_value (encoded as a string, defined by gdal)
no_data_value()248   std::string no_data_value() {return h_->no_data_value();}
249 
250  private:
251   //: the TIFF handle to the open resource file
252   tif_smart_ptr t_;
253 
254   //: the TIFF header information
255   vil_tiff_header* h_;
256   //: the default image header index
257   unsigned int index_;
258   //: number of images in the file
259   unsigned int nimages_;
260 #if 0
261   //to keep the tiff file open during reuse of multiple tiff resources
262   //in a single file otherwise the resource destructor would close the file
263   void clear_TIFF() { t_ = 0; }
264 #endif
265   //: the number of samples in a block
266   unsigned samples_per_block() const;
267 
268   enum vil_pixel_format compute_pixel_format();
269 
270   void copy_byte_block(vxl_byte* data, const vxl_uint_32 nbytes,
271                        vil_memory_chunk_sptr& cnk) const;
272 
273   //: convert a packed block to an expanded view
274   vil_image_view_base_sptr
275     view_from_buffer(vil_pixel_format& fmt,
276                      vil_memory_chunk_sptr const& buf,
277                      unsigned samples_per_block,
278                      unsigned bits_per_sample) const;
279 
280   //: the key methods for decoding the file data
281   vil_image_view_base_sptr fill_block_from_tile(vil_memory_chunk_sptr const & buf) const;
282 
283   vil_image_view_base_sptr fill_block_from_strip(vil_memory_chunk_sptr const & buf) const;
284 
285 #if 0
286   vil_image_view_base_sptr get_block_internal( unsigned block_index_i,
287                                                unsigned block_index_j ) const;
288   void
289     get_blocks_internal( unsigned start_block_i,
290                          unsigned end_block_i,
291                          unsigned start_block_j,
292                          unsigned end_block_j,
293                          std::vector< std::vector< vil_image_view_base_sptr > >& blocks ) const;
294 #endif
295   bool put_block(unsigned bi, unsigned bj, unsigned i0,
296                  unsigned j0, const vil_image_view_base& im);
297 
298   unsigned block_index(unsigned block_i, unsigned block_j) const;
299 
300   //: fill out the block with leading zeros or trailing zeros if necessary
301   void pad_block_with_zeros(unsigned ioff, unsigned joff,
302                             unsigned iclip, unsigned jclip,
303                             unsigned bytes_per_pixel,
304                             vxl_byte* block_buf) const;
305 
306   //: fill the block with view data
307   void fill_block_from_view(unsigned bi, unsigned bj,
308                             unsigned i0, unsigned j0,
309                             unsigned ioff, unsigned joff,
310                             unsigned iclip, unsigned jclip,
311                             const vil_image_view_base& im,
312                             vxl_byte*& block_buf);
313 
314   void bitpack_block(unsigned bytes_per_block,
315                      const vxl_byte* in_block_buf,
316                      vxl_byte* out_block_buf);
317 
318   bool write_block_to_file(unsigned bi, unsigned bj,
319                            unsigned block_size_bytes,
320                            vxl_byte* block_buf);
321 }; //End of single image TIFF resource
322 
323 
324 ///--------- Representation of Pyramid Images by multi-image TIFF -------
325 //
326 //
327 // It is assumed that image scales are not necessarily ordered with
328 // respect to tiff header index. This data structure maintains essential
329 // information about each pyramid level. The struct can be sorted on scale
330 // to form an ordered pyramid.
331 struct tiff_pyramid_level
332 {
333  public:
tiff_pyramid_leveltiff_pyramid_level334   tiff_pyramid_level(unsigned header_index, unsigned ni,
335                      unsigned nj, unsigned nplanes, vil_pixel_format fmt)
336    : header_index_(header_index), scale_(1.0f), ni_(ni), nj_(nj),
337      nplanes_(nplanes), pix_fmt_(fmt), cur_level_(0)
338   {}
339 
340   ~tiff_pyramid_level() = default;
341 
342   //:the tiff header index
343   unsigned header_index_;
344 
345   //:scale associated with level
346   float scale_;
347 
348   //:the image width
349   unsigned ni_;
350 
351   //: the image length
352   unsigned nj_;
353 
354   //:the number of planes
355   unsigned nplanes_;
356 
357   //: the pixel format
358   vil_pixel_format pix_fmt_;
359 
360   //: temporary variable for current level
361   unsigned cur_level_;
362 
printtiff_pyramid_level363   void print(const unsigned l) const {
364     std::cout << "level[" << l << "] hindex " << header_index_
365               << " scale: " << scale_ << "  width: " << ni_ << std::endl;
366   }
367 };
368 
369 //:Pyramid resource built on the multi-image capability of the TIFF format
370 // If read is true then the resource is open for reading else it is open for writing
371 class vil_tiff_pyramid_resource : public vil_pyramid_image_resource
372 {
373  public:
374   vil_tiff_pyramid_resource(tif_smart_ptr const& t, bool read = true);
375 
376   ~vil_tiff_pyramid_resource() override;
377 
378   //: The number of planes (or components) of the image.
379   // This method refers to the base (max resolution) image
380   // Dimensions:  Planes x ni x nj.
381   // This concept is treated as a synonym to components.
nplanes()382   inline unsigned nplanes() const override
383   { if (levels_[0]) return levels_[0]->nplanes_; return 1; }
384 
385   //: The number of pixels in each row.
386   // Dimensions:  Planes x ni x nj.
ni()387   inline unsigned ni() const override
388   { if (levels_[0]) return levels_[0]->ni_; return 0; }
389 
390   //: The number of pixels in each column.
391   // Dimensions:  Planes x ni x nj.
nj()392   inline unsigned nj() const override
393   { if (levels_[0]) return levels_[0]->nj_; return 0; }
394 
395   //: Pixel Format.
pixel_format()396   inline enum vil_pixel_format pixel_format() const override
397   { if (levels_[0]) return levels_[0]->pix_fmt_; return VIL_PIXEL_FORMAT_UNKNOWN; }
398 
399   //: Return a string describing the file format.
400   // Only file images have a format, others return 0
file_format()401   char const* file_format() const override { return "ptif"; }
402 
403          // --- Methods particular to pyramid resource ---
404 
405   //: number of pyramid levels
nlevels()406   unsigned nlevels() const override { return (unsigned)(levels_.size()); }
407 
408   //:Get a partial view from the image from a specified pyramid level
409   vil_image_view_base_sptr get_copy_view(unsigned i0, unsigned n_i,
410                                                  unsigned j0, unsigned n_j,
411                                                  unsigned level) const override;
412 
413   //:Get a partial view from the image in the pyramid closest to scale.
414   // The origin and size parameters are in the coordinate system of the base image.
415   // The scale factor is with respect to the base image (base scale = 1.0).
416   vil_image_view_base_sptr get_copy_view(unsigned i0, unsigned n_i,
417                                                  unsigned j0, unsigned n_j,
418                                                  const float scale,
419                                                  float& actual_scale) const override;
420 
421   //:
422   // Caution! The resource is assigned a header and the data is permanently
423   // written into the file. Be sure you want to commit to the file.
424   bool put_resource(vil_image_resource_sptr const& resc) override;
425 
426   //: returns the image resource at the specified pyramid level
427   vil_image_resource_sptr get_resource(const unsigned level) const override;
428 
429   //: for debug purposes
print(const unsigned level)430   void print(const unsigned level) override
431   { if (level<levels_.size()) levels_[level]->print(level); }
432  protected:
433   //:default constructor
434   vil_tiff_pyramid_resource();
435   //utility methods
436   //:normalize the scale factors so that the base image scale = 1.0
437   void normalize_scales();
438 
439   //:find the image resource with scale closest to specified scale
440   tiff_pyramid_level* closest(const float scale) const;
441 
442   //: If true resource is open for read, else open for write
443   bool read_{true};
444 
445   //: the tiff handle
446   tif_smart_ptr t_;
447 
448   //The set of images in the pyramid. levels_[0] is the base image
449   std::vector<tiff_pyramid_level*> levels_;
450 }; //End of pyramid image
451 
452 
453 //------------------------ Lifted from vil_nitf2_image ------------------------
454 //            If this happens again then maybe should elevate to a
455 //            utility class -- JLM
456 //
457 
458 //:
459 // This function does a lot of work for \sa byte_align_data().  It will strip one value
460 // of ni bits and return it as a value of type T (with zero padding on the MSBs).
461 // Both io and ni are lengths (in bits - not bytes).
462 //
463 // \param i0: Offset (in bits - not bytes) from in_val[0].  This will be the start
464 //            of the bits extracted from in_val.
465 // \param ni: number of bits (starting from i0) that will be extracted from in_val.
466 template< class T >
tiff_get_bits(const T * in_val,unsigned i0,unsigned ni)467 T tiff_get_bits( const T* in_val, unsigned i0, unsigned ni )
468 {
469   unsigned sample_offset = i0 / ( sizeof(T)*8 );
470   unsigned bit_offset = i0 % ( sizeof(T)*8 );
471 
472   unsigned strip_left = bit_offset;
473   int strip_right = ( sizeof( T ) * 8 ) - ( bit_offset + ni );
474   T temp = in_val[sample_offset];
475   if ( strip_left > 0 ){
476     //strip off the appropriate bits from the std::left (replacing them with zeros)
477     temp <<= strip_left;
478     temp >>= strip_left;
479   }
480   if ( strip_right > 0 ){
481     //strip off the appropriate bits from the std::right
482     //the bit shift operator wasn't having the correct effect, so that'w
483     //why the for loop
484     for ( int i = 0 ; i < strip_right ; i++ ) temp /= 2;
485     //temp >>= strip_right;
486   }
487   else if ( strip_right < 0 ){
488     //we didn't have enough bits in the first element of the in_val array
489     //need to get some from the next element
490 
491     for ( int i = 0 ; i < (-strip_right) ; ++i ) temp *= 2;
492     temp += tiff_get_bits<T>( in_val+sample_offset+1, 0, -strip_right );
493 
494 #if 0
495     T next = in_val[sample_offset+1];
496     //shift right a bigger amount
497     int new_strip_right = strip_right + (sizeof(T)*8);
498     for ( int i = 0 ; i < new_strip_right ; i++ ) next /= 2;
499     // next is the LSB part
500     unsigned new_strip_left = 1- strip_right;
501     temp <<= new_strip_left;
502     temp += next;
503 #endif
504   }
505 #ifdef DEBUG
506   std::cout << "Out val = " << std::hex << temp << std::dec << '\n';
507 #endif
508   return temp;
509 }
510 
511 //:
512 // This function will byte align the data in in_data and store the result in out_data.  For example, let's
513 // say that you had in_data is of type unsigned char and contains the following data: 110010111001111010000110.
514 // In other words:
515 // in_data[0] = 203 (11001011)
516 // in_data[1] = 158 (10011110)
517 // in_data[2] = 134 (10000110)
518 // Let's further say you called this function like this: byte_align_data( in_data, 8, 3, out_data ).
519 // Then, when the function finished, out_data would look like this:
520 // out_data[0] = 6 (00000110)
521 // out_data[1] = 2 (00000010)
522 // out_data[2] = 7 (00000111)
523 // out_data[3] = 1 (00000001)
524 // out_data[4] = 7 (00000111)
525 // out_data[5] = 2 (00000010)
526 // out_data[6] = 0 (00000000)
527 // out_data[7] = 6 (00000110)
528 //
529 // Basically, what the function did was group the bitstream into groups of three and then store all of the
530 // values into out_data.  It had to zero pad all the values (on the MSB side) to get them into out_data.  That's
531 // why out_data is bigger.
532 //
533 // This function works with other unsigned types of data too.  For example, let's say in_data was of type unsigned int
534 // and contained the following bits: 0100110010111000 0111101100000000 1111000011110000 (note that this bitstream is shown
535 // in big endian notation, that will not be the case if you are on a little endian machine -- this is just for illustration)
536 // in other words:
537 // in_data[0] = 19640 (0100110010111000) [shown in big endian for illustrative purposes only]
538 // in_data[1] = 31488 (0111101100000000) [shown in big endian for illustrative purposes only]
539 // in_data[2] = 61680 (1111000011110000) [shown in big endian for illustrative purposes only]
540 // Let's further say, you called this function like this byte_align_data( in_data, 4, 12, out_data ).
541 // Then out_data would be aligned along two byte (sixteen bit) boundaries and would look like this:
542 // out_data[0] = 1227 (0000010011001011) [shown in big endian for illustrative purposes only]
543 // out_data[1] = 2171 (0000100001111011) [shown in big endian for illustrative purposes only]
544 // out_data[2] = 15   (0000000000001111) [shown in big endian for illustrative purposes only]
545 // out_data[3] = 240  (0000000011110000) [shown in big endian for illustrative purposes only]
546 //
547 // Because of the fact that this function uses bit shifting operators, and the behavior of the std::right
548 // shift operator is implementation specific when applied to a negative number, you should probably
549 // only use this function on unsigned data.
550 //
551 // \param in_data: The input data.  It must be at least (num_samples*in_bits_per_sample/8) bytes long.
552 //                The values should have the endianness of your platform.
553 // \param num_samples: The number of actual samples in in_data.
554 // \param in_bits_per_sample: The bits per sample in in_data
555 // \param out_data: I'll store the output data here.  It must be at least (num_samples*sizeof(T)) bytes long
556 //                 The values will have the endianness of your platform.
557 //
558 // Note that inBitsPerSampe must not be >= sizeof(T).  If they were to be equal, then this function
559 // would have nothing to do (in_data is already byte aligned).  If in_bits_per_sample were less than sizeof(T),
560 // then each element of out_data would be too small to store the corresponding elements in in_data.
561 //
562 // Note that there is a specialization for the bool case which just casts it to an 8 bit quantity then calls
563 // this same function.  This is because the logic in get_bits<> doesn't work for the bool case.
564 template< class T >
tiff_byte_align_data(T * in_data,unsigned num_samples,unsigned in_bits_per_sample,T * out_data)565 T* tiff_byte_align_data( T* in_data, unsigned num_samples, unsigned in_bits_per_sample, T* out_data )
566 {
567   assert( in_bits_per_sample < sizeof(T)*8 );
568 
569   //grab each value from the bitstream (in_data) that we need... one
570   //at a time
571   unsigned bit_offset = 0;
572   for ( unsigned o = 0 ; o < num_samples ; o++ ){
573     out_data[o] = tiff_get_bits<T>( in_data, bit_offset, in_bits_per_sample );
574     //printf("bo = %i, in = %x, out =  %x\n", bit_offset, in_data[o], out_data[o]);
575     bit_offset+=in_bits_per_sample;
576   }
577 
578   return out_data;
579 }
580 
581 template<> bool* tiff_byte_align_data<bool>( bool* in_data, unsigned num_samples, unsigned in_bits_per_sample, bool* out_data );
582 
583 #endif // vil_tiff_file_format_h_
584