aimsgui 6.0.0
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 <cartodata/volume/volume.h>
39#include <aims/io/datatypecode.h>
40#include <aims/rgb/rgb.h>
41#include <cartobase/stream/fileutil.h>
42#include <cartobase/exception/file.h>
43#include <cartobase/exception/format.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
51namespace aims
52{
53
54 template<typename T>
56 {
57 public:
58 QtFormatsReader( const std::string& name ) : _name( name ) {}
60
61 void read( carto::Volume<T>& thing,
62 const carto::AllocatorContext & context,
63 carto::Object options );
66 void readFrame( carto::Volume<T> & thing, QtFormatsHeader* hdr,
67 const std::string & filename, unsigned zfame,
68 unsigned tframe );
69
70 private:
71 std::string _name;
72 static T convertColor( const QRgb & );
73 };
74
75
76 template <typename T>
77 inline QtFormatsReader<T> &
79 {
80 reader.read( thing );
81 return reader;
82 }
83
84
85 template <typename T>
86 inline
88 const carto::AllocatorContext & context,
89 carto::Object options )
90 {
91 QtFormatsHeader *hdr = new QtFormatsHeader( _name );
92 try
93 {
94 hdr->read();
95 }
96 catch( std::exception & e )
97 {
98 delete hdr;
99 throw;
100 }
101
102 int frame = -1, border = 0;
103 options->getProperty( "frame", frame );
104 options->getProperty( "border", border );
105
106 std::vector<std::string> files = hdr->inputFilenames();
107
108 unsigned tmin = 0, tmax = hdr->dimT() - 1;
109 if( frame >= 0 )
110 {
111 if( tmax < (unsigned) frame )
112 {
113 delete hdr;
114 throw std::domain_error( "frame higher than file dimT" );
115 }
116 if( (unsigned) frame < tmax )
117 files.erase( files.begin() + ( frame + 1 ) * hdr->dimZ(),
118 files.end() );
119 if( frame > 0 )
120 files.erase( files.begin(), files.begin() + frame * hdr->dimZ() );
121 tmin = frame;
122 tmax = frame;
123 }
124
125 carto::AllocatorContext al
126 ( context.accessMode(),
128 ( new carto::FileDataSource( *files.begin(), 0,
129 carto::DataSource::Read ) ),
130 false, context.useFactor() );
131
132 carto::VolumeRef<T> data( hdr->dimX(), hdr->dimY(), hdr->dimZ(),
133 tmax - tmin + 1, border, al );
134
135 std::vector<float> vs( 4, 1. );
136 vs[0] = hdr->sizeX();
137 vs[1] = hdr->sizeY();
138 vs[2] = hdr->sizeZ();
139 vs[3] = hdr->sizeT();
140 data->header().setProperty( "voxel_size", vs );
141
142 std::vector<int> dims(4);
143 dims[0] = hdr->dimX();
144 dims[1] = hdr->dimY();
145 dims[2] = hdr->dimZ();
146 dims[3] = tmax - tmin + 1;
147 hdr->setProperty( "volume_dimension", dims );
148
149 // force data type into header
151 hdr->setType( dtc.dataType() );
152 std::string dir = carto::FileUtil::dirname( _name );
153 if( !dir.empty() )
155
156 unsigned i = 0, s, t, ns = (unsigned) data.getSizeZ(),
157 nt = tmax - tmin + 1;
158 for( t=0; t<nt; ++t )
159 for( s=0; s<ns; ++s, ++i )
160 readFrame( *data, hdr, dir + files[i], s, t );
161
162 thing = *data;
163 if( hdr->hasProperty( "filenames" ) )
164 hdr->removeProperty( "filenames" );
165 thing.header().copyProperties( hdr );
166 }
167
168 template<typename T>
169 inline
171 QtFormatsHeader * hdr,
172 const std::string & name, unsigned z,
173 unsigned t )
174 {
175 // std::cout << "readFrame: " << name << ", z: " << z << ", t: " << t << "\n";
176 const QImage *imp = 0;
177 QImage ima;
178 QImageReader qio;
179
180 if( hdr->filename() == name && hdr->hasRead() )
181 imp = &hdr->qimage();
182 else
183 {
184 std::string format;
185 hdr->getProperty( "file_type", format );
186 qio.setFileName( name.c_str() );
187 qio.setFormat( format.c_str() );
188 bool lock = false;
189 if( format == "JP2" )
190 {
191 lock = true;
193 }
194 ima = qio.read();
195 if( lock )
197 if( ima.isNull() )
198 throw carto::format_mismatch_error( name );
199 imp = &ima;
200 }
201
202 const QImage & im = *imp;
203 int y, dx = data.getSizeX(), dy = data.getSizeY();
204
205/* std::cout << "-- QTPLUGIN::readFrame - image depth: " << carto::toString(im.depth()) << std::endl
206 << "-- QTPLUGIN::readFrame - sizeof(T): " << carto::toString(sizeof(T)) << std::endl
207 << "-- QTPLUGIN::readFrame - im.colorCount(): " << carto::toString(im.colorCount()) << std::endl; */
208
209// we must convert anyway because the Qt internal format is BGRA
210// if( im.depth() == (sizeof(T) * 8) && im.colorCount() == 0 )
211// for( y=0; y<dy; ++y )
212// memcpy( &data.at( 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.at( 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
The descriptor class of the .dim GIS header.
void setType(const std::string &t)
bool hasRead() const
static carto::Mutex & qformatsMutex()
virtual void read()
QtFormatsReader(const std::string &name)
Definition qtformatsR.h:58
void read(carto::Volume< T > &thing, const carto::AllocatorContext &context, carto::Object options)
Definition qtformatsR.h:87
void readFrame(carto::Volume< 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:170
std::string filename() const
virtual std::vector< std::string > inputFilenames()
std::string dataType()
virtual void copyProperties(Object source)
static char separator()
static std::string dirname(const std::string &)
const PropertySet & header() const
void setProperty(const std::string &, const T &)
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 getSizeY() const
int getSizeX() const
int getSizeZ() const
const PropertySet & header() const
const T & at(long x, long y=0, long z=0, long t=0) const
GenesisReader< T > & operator>>(GenesisReader< T > &reader, AimsData< T > &thing)
carto::VoxelRGBA AimsRGBA
carto::VoxelRGB AimsRGB