OpenStructure
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>
27 #include <ost/img/alg/normalizer_factory.hh>
29 
30 
31 namespace ost { namespace io { namespace detail {
33 typedef void (*TIFFWarningHandler)(const char*, const char*, va_list);
34 
36 void tiff_warning_handler(const char *mod, const char* fmt, va_list ap);
37 
41  handler_ = TIFFSetWarningHandler(tiff_warning_handler);
42  }
43 
45  TIFFSetWarningHandler(handler_);
46  }
47 
49 };
50 
52 class complexint32_t:public std::complex<int32_t>{
53 public:
54 
55  operator std::complex<Real>()
56  {
57  return std::complex<Real>(real(),imag());
58  }
59 };
60 
62 class complexint16:public std::complex<int16>{
63 public:
64 
65  operator std::complex<Real>()
66  {
67  return std::complex<Real>(real(),imag());
68  }
69 };
70 
72 class complexint8:public std::complex<int8>{
73 public:
74 
75  operator std::complex<Real>()
76  {
77  return std::complex<Real>(real(),imag());
78  }
79 };
80 
82 int32_t CustomTIFFReadProcIStream(void* thandle, void* tdata, int32_t tsize);
84 int32_t CustomTIFFReadProcIStream(void* thandle, void* tdata, int32_t tsize);
86 int32_t CustomTIFFReadProcOStream(void* thandle, void* tdata, int32_t tsize);
88 int32_t CustomTIFFWriteProcIStream(void* thandle, void* tdata, int32_t tsize);
90 int32_t CustomTIFFWriteProcOStream(void* thandle, void* tdata, int32_t tsize);
92 uint32_t CustomTIFFSeekProcIStream(void* thandle, uint32_t toff, int dir);
94 uint32_t CustomTIFFSeekProcOStream(void* thandle, uint32_t toff, int dir);
96 int CustomTIFFCloseProc(void* thandle);
98 uint32_t CustomTIFFSizeProcIStream(void* thandle);
100 uint32_t CustomTIFFSizeProcOStream(void* thandle);
102 int CustomTIFFMapFileProc(void* thandle, void** tdata, uint32* toff);
104 void CustomTIFFUnmapFileProc(void* thandle, void* tdata, uint32 toff);
105 
107 template<typename IN_TYPE,typename OUT_TYPE, class IST>
108 void do_tiff_read(tdata_t buf,uint16 rps, uint32_t width, IST* is,int& current_row, int spp)
109 {
110  IN_TYPE* dp = static_cast<IN_TYPE*>(buf);
111  for(uint r=0;r<rps;r++) {
112  for(uint c=0;c<width;c++) {
113  is->Value(img::Point(c,current_row))=static_cast<OUT_TYPE>(dp[(r*width+c)*spp]);
114  }
115  current_row++;
116  }
117 }
118 
120 template<typename IN_TYPE,typename OUT_TYPE, class IST>
121 void do_tiff_write(TIFF *tif, IST* is,uint32_t rowsperstrip,uint32_t width,uint32_t height, uint32_t strip,const img::NormalizerPtr& nptr)
122 {
123  uint datalength=rowsperstrip*width;
124  if((strip+1)*rowsperstrip>height){
125  datalength=(height-strip*rowsperstrip)*width;
126  }
127  OUT_TYPE* buf=new OUT_TYPE[datalength];
128  img::Point start = is->GetExtent().GetStart();
129 
130  uint i=0;
131  for(uint r=strip*rowsperstrip;r<(strip+1)*rowsperstrip && r<height;r++) {
132  for(uint c=0;c<width;c++) {
133  buf[i] = static_cast<OUT_TYPE>(nptr->Convert(is->Value(img::Point(c,r)+start)));
134  ++i;
135  }
136  }
137  TIFFWriteEncodedStrip(tif,strip,buf, sizeof(OUT_TYPE)*datalength);
138  delete[] buf;
139 }
140 
141 }}} // ns
142 
143 #endif // TIFF_HH