OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tiff_util.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 // Copyright (C) 2003-2010 by the IPLT authors
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 3.0 of the License, or (at your option)
10 // any later version.
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 // details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //------------------------------------------------------------------------------
20 #ifndef OST_IMG_IO_TIFF_HH
21 #define OST_IMG_IO_TIFF_HH
22 
23 #include <ost/stdint.hh>
24 #include <tiff.h>
25 #include <tiffio.h>
26 #include <boost/filesystem.hpp>
29 
30 #if defined(TIFFLIB_VERSION) && TIFFLIB_VERSION >= 20111221
31 # define INTNN_T int64_t
32 # define UINTNN uint64
33 # define UINTNN_T uint64_t
34 # define COMPLEXINTNN_T complexint64_t
35 #else
36 # define INTNN_T int32_t
37 # define UINTNN uint32
38 # define UINTNN_T uint32_t
39 # define COMPLEXINTNN_T complexint32_t
40 #endif
41 
42 namespace ost { namespace io { namespace detail {
44 typedef void (*TIFFWarningHandler)(const char*, const char*, va_list);
45 
47 void tiff_warning_handler(const char *mod, const char* fmt, va_list ap);
48 
52  handler_ = TIFFSetWarningHandler(tiff_warning_handler);
53  }
54 
56  TIFFSetWarningHandler(handler_);
57  }
58 
60 };
61 
63 class COMPLEXINTNN_T:public std::complex<INTNN_T>{
64 public:
65 
66  operator std::complex<Real>()
67  {
68  return std::complex<Real>(real(),imag());
69  }
70 };
71 
73 class complexint16:public std::complex<int16>{
74 public:
75 
76  operator std::complex<Real>()
77  {
78  return std::complex<Real>(real(),imag());
79  }
80 };
81 
83 class complexint8:public std::complex<int8>{
84 public:
85 
86  operator std::complex<Real>()
87  {
88  return std::complex<Real>(real(),imag());
89  }
90 };
91 
93 INTNN_T CustomTIFFReadProcIStream(void* thandle, void* tdata, INTNN_T tsize);
95 INTNN_T CustomTIFFReadProcIStream(void* thandle, void* tdata, INTNN_T tsize);
97 INTNN_T CustomTIFFReadProcOStream(void* thandle, void* tdata, INTNN_T tsize);
99 INTNN_T CustomTIFFWriteProcIStream(void* thandle, void* tdata, INTNN_T tsize);
101 INTNN_T CustomTIFFWriteProcOStream(void* thandle, void* tdata, INTNN_T tsize);
103 UINTNN_T CustomTIFFSeekProcIStream(void* thandle, UINTNN_T toff, int dir);
105 UINTNN_T CustomTIFFSeekProcOStream(void* thandle, UINTNN_T toff, int dir);
107 int CustomTIFFCloseProc(void* thandle);
109 UINTNN_T CustomTIFFSizeProcIStream(void* thandle);
111 UINTNN_T CustomTIFFSizeProcOStream(void* thandle);
113 int CustomTIFFMapFileProc(void* thandle, void** tdata, UINTNN* toff);
115 void CustomTIFFUnmapFileProc(void* thandle, void* tdata, UINTNN toff);
116 
118 template<typename IN_TYPE,typename OUT_TYPE, class IST>
119 void do_tiff_read(tdata_t buf,uint16 rps, UINTNN_T width, IST* is,int& current_row, int spp)
120 {
121  IN_TYPE* dp = static_cast<IN_TYPE*>(buf);
122  for(uint r=0;r<rps;r++) {
123  for(uint c=0;c<width;c++) {
124  is->Value(img::Point(c,current_row))=static_cast<OUT_TYPE>(dp[(r*width+c)*spp]);
125  }
126  current_row++;
127  }
128 }
129 
131 template<typename IN_TYPE,typename OUT_TYPE, class IST>
132 void do_tiff_write(TIFF *tif, IST* is,UINTNN_T rowsperstrip,UINTNN_T width,UINTNN_T height, UINTNN_T strip,const img::NormalizerPtr& nptr)
133 {
134  uint datalength=rowsperstrip*width;
135  if((strip+1)*rowsperstrip>height){
136  datalength=(height-strip*rowsperstrip)*width;
137  }
138  OUT_TYPE* buf=new OUT_TYPE[datalength];
139  img::Point start = is->GetExtent().GetStart();
140 
141  uint i=0;
142  for(uint r=strip*rowsperstrip;r<(strip+1)*rowsperstrip && r<height;r++) {
143  for(uint c=0;c<width;c++) {
144  buf[i] = static_cast<OUT_TYPE>(nptr->Convert(is->Value(img::Point(c,r)+start)));
145  ++i;
146  }
147  }
148  TIFFWriteEncodedStrip(tif,strip,buf, sizeof(OUT_TYPE)*datalength);
149  delete[] buf;
150 }
151 
152 }}} // ns
153 
154 #endif // TIFF_HH