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