aimsgui  5.1.2
qtformatsR.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL license version 2 under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL license version 2 as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL license version 2 and that you accept its terms.
32  */
33 
34 #ifndef AIMS_IO_QTFORMATSR_H
35 #define AIMS_IO_QTFORMATSR_H
36 
37 #include <aims/data/data.h>
39 #include <aims/io/datatypecode.h>
40 #include <aims/rgb/rgb.h>
44 #include <soma-io/datasource/filedatasource.h>
45 #include <cartobase/thread/mutex.h>
46 #include <qcolor.h>
47 #include <QImage>
48 #include <QImageReader>
49 #include <QImageWriter>
50 
51 namespace aims
52 {
53 
54  template<typename T>
56  {
57  public:
58  QtFormatsReader( const std::string& name ) : _name( name ) {}
60 
61  void read( AimsData<T>& thing, const carto::AllocatorContext & context,
62  carto::Object options );
65  void readFrame( AimsData<T> & thing, QtFormatsHeader* hdr,
66  const std::string & filename, unsigned zfame,
67  unsigned tframe );
68 
69  private:
70  std::string _name;
71  static T convertColor( const QRgb & );
72  };
73 
74 
75  template <typename T>
76  inline QtFormatsReader<T> &
78  {
79  reader.read( thing );
80  return reader;
81  }
82 
83 
84  template <typename T>
85  inline
87  const carto::AllocatorContext & context,
88  carto::Object options )
89  {
90  QtFormatsHeader *hdr = new QtFormatsHeader( _name );
91  try
92  {
93  hdr->read();
94  }
95  catch( std::exception & e )
96  {
97  delete hdr;
98  throw;
99  }
100 
101  int frame = -1, border = 0;
102  options->getProperty( "frame", frame );
103  options->getProperty( "border", border );
104 
105  std::vector<std::string> files = hdr->inputFilenames();
106 
107  unsigned tmin = 0, tmax = hdr->dimT() - 1;
108  if( frame >= 0 )
109  {
110  if( tmax < (unsigned) frame )
111  {
112  delete hdr;
113  throw std::domain_error( "frame higher than file dimT" );
114  }
115  if( (unsigned) frame < tmax )
116  files.erase( files.begin() + ( frame + 1 ) * hdr->dimZ(),
117  files.end() );
118  if( frame > 0 )
119  files.erase( files.begin(), files.begin() + frame * hdr->dimZ() );
120  tmin = frame;
121  tmax = frame;
122  }
123 
124  carto::AllocatorContext al
125  ( context.accessMode(),
127  ( new carto::FileDataSource( *files.begin(), 0,
128  carto::DataSource::Read ) ),
129  false, context.useFactor() );
130 
131  AimsData<T> data( hdr->dimX(), hdr->dimY(), hdr->dimZ(),
132  tmax - tmin + 1, border, al );
133 
134  data.setSizeX( hdr->sizeX() );
135  data.setSizeY( hdr->sizeY() );
136  data.setSizeZ( hdr->sizeZ() );
137  data.setSizeT( hdr->sizeT() );
138 
139  std::vector<int> dims(4);
140  dims[0] = hdr->dimX();
141  dims[1] = hdr->dimY();
142  dims[2] = hdr->dimZ();
143  dims[3] = tmax - tmin + 1;
144  hdr->setProperty( "volume_dimension", dims );
145 
146  if( dims.size() < 1 )
147  dims.push_back( hdr->dimX() );
148  if( dims.size() < 2 )
149  dims.push_back( hdr->dimY() );
150  if( dims.size() < 3 )
151  dims.push_back( hdr->dimZ() );
152  if( dims.size() < 4 )
153  dims.push_back( hdr->dimT() );
154 
155  // force data type into header
157  hdr->setType( dtc.dataType() );
158  std::string dir = carto::FileUtil::dirname( _name );
159  if( !dir.empty() )
161 
162  unsigned i = 0, s, t, ns = (unsigned) data.dimZ(), nt = tmax - tmin + 1;
163  for( t=0; t<nt; ++t )
164  for( s=0; s<ns; ++s, ++i )
165  readFrame( data, hdr, dir + files[i], s, t );
166 
167  thing = data;
168  if( hdr->hasProperty( "filenames" ) )
169  hdr->removeProperty( "filenames" );
170  thing.setHeader( hdr );
171  }
172 
173  template<typename T>
174  inline
176  QtFormatsHeader * hdr,
177  const std::string & name, unsigned z,
178  unsigned t )
179  {
180  // std::cout << "readFrame: " << name << ", z: " << z << ", t: " << t << "\n";
181  const QImage *imp = 0;
182  QImage ima;
183  QImageReader qio;
184 
185  if( hdr->filename() == name && hdr->hasRead() )
186  imp = &hdr->qimage();
187  else
188  {
189  std::string format;
190  hdr->getProperty( "file_type", format );
191  qio.setFileName( name.c_str() );
192  qio.setFormat( format.c_str() );
193  bool lock = false;
194  if( format == "JP2" )
195  {
196  lock = true;
198  }
199  ima = qio.read();
200  if( lock )
202  if( ima.isNull() )
203  throw carto::format_mismatch_error( name );
204  imp = &ima;
205  }
206 
207  const QImage & im = *imp;
208  int y, dx = data.dimX(), dy = data.dimY();
209 
210  if( im.depth() == sizeof(T) && im.colorCount() == 0 )
211  for( y=0; y<dy; ++y )
212  memcpy( &data( 0, y, z, t ), im.scanLine( y ), dx * sizeof( T ) );
213  else
214  for( y=0; y<dy; ++y )
215  for( int x=0; x<dx; ++x )
216  data( x, y, z, t ) = convertColor( im.pixel( x, y ) );
217  }
218 
219 
220  template<typename T> inline
221  T QtFormatsReader<T>::convertColor( const QRgb & x )
222  {
223  return (T) sqrt( ( ( (double) qRed( x ) ) * (double) qRed( x )
224  + qGreen( x ) * (double) qGreen( x )
225  + qBlue( x ) * (double) qBlue( x ) ) / 3 );
226  }
227 
228 
229  template<> inline
230  AimsRGB QtFormatsReader<AimsRGB>::convertColor( const QRgb & x )
231  {
232  return AimsRGB( (byte) qRed( x ), (byte) qGreen( x ), (byte) qBlue( x ) );
233  }
234 
235 
236  template<> inline
237  AimsRGBA QtFormatsReader<AimsRGBA>::convertColor( const QRgb & x )
238  {
239  return AimsRGBA( (byte) qRed( x ), (byte) qGreen( x ), (byte) qBlue( x ),
240  (byte) qAlpha( x ) );
241  }
242 
243 }
244 
245 #endif
246 
void setSizeY(float sizey)
void setSizeT(float sizet)
void setHeader(aims::Header *hdr)
int dimY() const
int dimX() const
int dimZ() const
void setSizeZ(float sizez)
void setSizeX(float sizex)
The descriptor class of the .dim GIS header.
void setType(const std::string &t)
bool hasRead() const
static carto::Mutex & qformatsMutex()
virtual void read()
void readFrame(AimsData< T > &thing, QtFormatsHeader *hdr, const std::string &filename, unsigned zfame, unsigned tframe)
called by read(), but you can call it for single frame reading (axial slice)
Definition: qtformatsR.h:175
QtFormatsReader(const std::string &name)
Definition: qtformatsR.h:58
void read(AimsData< T > &thing, const carto::AllocatorContext &context, carto::Object options)
Definition: qtformatsR.h:86
virtual std::vector< std::string > inputFilenames()
std::string filename() const
std::string dataType()
static char separator()
static std::string dirname(const std::string &)
virtual bool getProperty(const std::string &, Object &) const
virtual bool removeProperty(const std::string &)
virtual void setProperty(const std::string &, Object)
virtual bool hasProperty(const std::string &) const
int nt
GenesisReader< T > & operator>>(GenesisReader< T > &reader, AimsData< T > &thing)