A.I.M.S


jpegR.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-B license 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-B license 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-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMS_IO_JPEGR_H
35 #define AIMS_IO_JPEGR_H
36 
37 #include <aims/data/data.h>
38 #include <aims/io/jpegheader.h>
39 #include <aims/io/datatypecode.h>
44 #include <stdio.h>
45 extern "C"
46 {
47 #include <jpeglib.h>
48 }
49 
50 
51 namespace aims
52 {
53 
54  template<class T>
55  class JpegReader
56  {
57  public:
58  JpegReader( 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, const std::string & filename,
66  unsigned zfame, unsigned tframe );
67 
68  private:
69  std::string _name;
70  };
71 
72 
73  template <class T>
74  inline JpegReader<T> &
75  operator >> ( JpegReader<T> & reader, AimsData<T> & thing )
76  {
77  reader.read( thing );
78  return reader;
79  }
80 
81 
82  template <class T>
83  inline
85  const carto::AllocatorContext & context,
86  carto::Object options )
87  {
88  JpegHeader *hdr = new JpegHeader( _name );
89  try
90  {
91  hdr->read();
92  }
93  catch( std::exception & e )
94  {
95  delete hdr;
96  throw;
97  }
98 
99  int frame = -1, border = 0;
100  options->getProperty( "frame", frame );
101  options->getProperty( "border", border );
102 
103  std::vector<std::string> files = hdr->inputFilenames();
104  std::vector<int> dims;
105  hdr->getProperty( "volume_dimensions", dims );
106 
107  if( dims.size() < 1 )
108  dims.push_back( hdr->dimX() );
109  if( dims.size() < 2 )
110  dims.push_back( hdr->dimY() );
111  if( dims.size() < 3 )
112  dims.push_back( hdr->dimZ() );
113  if( dims.size() < 4 )
114  dims.push_back( hdr->dimT() );
115 
116  unsigned tmin = 0, tmax = dims[3] - 1;
117  if( frame >= 0 )
118  {
119  if( tmax < (unsigned) frame )
120  {
121  delete hdr;
122  throw std::domain_error( "frame higher than file dimT" );
123  }
124  if( (unsigned) frame < tmax )
125  files.erase( files.begin() + ( frame + 1 ) * hdr->dimZ(),
126  files.end() );
127  if( frame > 0 )
128  files.erase( files.begin(), files.begin() + frame * hdr->dimZ() );
129  tmin = frame;
130  tmax = frame;
131  }
132 
133  carto::AllocatorContext
134  cont2( context.accessMode(),
136  ( new carto::FileDataSource
137  ( *files.begin(), 0, carto::DataSource::Read ) ),
138  false, context.useFactor() );
139 
140  AimsData<T> data( hdr->dimX(), hdr->dimY(), hdr->dimZ(),
141  tmax - tmin + 1, border, cont2 );
142  data.setSizeX( hdr->sizeX() );
143  data.setSizeY( hdr->sizeY() );
144  data.setSizeZ( hdr->sizeZ() );
145  data.setSizeT( hdr->sizeT() );
146 
147  dims[3] = tmax - tmin + 1;
148  hdr->setProperty( "volume_dimension", dims );
149 
150  // force data type into header
152  hdr->setType( dtc.dataType() );
153  std::string dir = carto::FileUtil::dirname( _name );
154  if( !dir.empty() )
156 
157  unsigned i = 0, s, t, ns = (unsigned) data.dimZ(), nt = tmax - tmin + 1;
158  for( t=0; t<nt; ++t )
159  for( s=0; s<ns; ++s, ++i )
160  readFrame( data, dir + files[i], s, t );
161 
162  thing = data;
163  if( hdr->hasProperty( "filenames" ) )
164  hdr->removeProperty( "filenames" );
165  thing.setHeader( hdr );
166  }
167 
168  template<class T>
169  inline
171  const std::string & name, unsigned z,
172  unsigned t )
173  {
174  struct jpeg_decompress_struct cinfo;
175  struct jpeg_error_mgr jerr;
176  FILE *fp;
177  unsigned i;
178  JSAMPROW row_pointer[1];
179 
180  cinfo.err = jpeg_std_error( &jerr );
181  jpeg_create_decompress( &cinfo );
182 
183  fp = fopen( name.c_str(), "rb" );
184  if( !fp )
185  throw carto::file_error( name );
186 
187  jpeg_stdio_src( &cinfo, fp );
188  if( jpeg_read_header( &cinfo, true ) != 1 )
189  throw carto::format_error( name );
190  else
191  {
193  if( dtc.dataType() == "RGB" )
194  {
195  cinfo.out_color_space = JCS_RGB;
196  cinfo.out_color_components = 3;
197  }
198  else
199  {
200  cinfo.out_color_space = JCS_GRAYSCALE;
201  cinfo.out_color_components = 1;
202  }
203  jpeg_start_decompress( &cinfo );
204  for( i=0; i<cinfo.image_height; ++i )
205  {
206  row_pointer[0] = (JSAMPROW) &data( 0, i, z, t );
207  jpeg_read_scanlines( &cinfo, row_pointer, 1 );
208  }
209  }
210 
211  jpeg_finish_decompress( &cinfo );
212  fclose( fp );
213  jpeg_destroy_decompress( &cinfo );
214  }
215 
216 }
217 
218 #endif
GenesisReader< T > & operator>>(GenesisReader< T > &reader, AimsData< T > &thing)
Definition: genesisR.h:70
std::string dataType()
void readFrame(AimsData< T > &thing, const std::string &filename, unsigned zfame, unsigned tframe)
called by read(), but you can call it for single frame reading (axial slice)
Definition: jpegR.h:170
virtual bool getProperty(const std::string &key, Object &value) const =0
int nt
virtual bool hasProperty(const std::string &) const
void read(AimsData< T > &thing, const carto::AllocatorContext &context, carto::Object options)
Definition: jpegR.h:84
int dimZ() const
Definition: jpegheader.h:66
int dimX() const
Definition: jpegheader.h:64
float sizeT() const
Definition: jpegheader.h:72
int dimT() const
Definition: jpegheader.h:67
void setSizeX(float sizex)
virtual bool removeProperty(const std::string &)
void setHeader(aims::Header *hdr)
float sizeX() const
Definition: jpegheader.h:69
static char separator()
int dimY() const
Definition: jpegheader.h:65
The descriptor class of the .dim GIS header.
Definition: jpegheader.h:52
static std::string dirname(const std::string &)
JpegReader(const std::string &name)
Definition: jpegR.h:58
virtual std::vector< std::string > inputFilenames()
virtual bool getProperty(const std::string &, Object &) const
float sizeY() const
Definition: jpegheader.h:70
void setType(const std::string &t)
virtual void setProperty(const std::string &, Object)
float sizeZ() const
Definition: jpegheader.h:71