aimsgui 6.0.0
qtformatsW.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_QTFORMATSW_H
35#define AIMS_IO_QTFORMATSW_H
36
38#include <aims/io/datatypecode.h>
39#include <aims/data/pheader.h>
40#include <cartodata/volume/volume.h>
41#include <aims/rgb/rgb.h>
42#include <cartobase/exception/ioexcept.h>
43#include <cartobase/stream/fileutil.h>
44#include <cartobase/type/string_conversion.h>
45#include <qimage.h>
46#include <qapplication.h>
47
48
49namespace aims
50{
51
52 template<typename T>
54 {
55 public:
56 QtFormatsWriter( const std::string& name ) : _name( name ) {}
58
59 void write( const carto::Volume<T> & thing );
62 void writeFrame( const carto::Volume<T> & thing,
63 const std::string & filename,
64 unsigned zfame, unsigned tframe );
65 std::string format() const;
66
67 private:
68 std::string _name;
69
70 static QRgb convertColor( const T & );
71 };
72
73
74 template <typename T>
75 inline QtFormatsWriter<T> &
76 operator << ( QtFormatsWriter<T> & writer, const carto::Volume<T> & thing )
77 {
78 writer.write( thing );
79 return writer;
80 }
81
82
83 template <typename T>
84 inline
86 {
87 std::string fmt = format();
88 // std::cout << "QtFormatsWriter< " << carto::DataTypeCode<T>::name() << " >::write, format: " << fmt << std::endl;
89 if( !qApp && ( fmt == "EPS" || fmt == "EPSF" || fmt == "EPSI" ) )
90 throw carto::format_error( fmt + " format needs a QApplication",
91 _name );
92 unsigned t, z, dt = thing.getSizeT(), dz = thing.getSizeZ();
93 std::vector<float> vs = thing.getVoxelSize();
94 QtFormatsHeader hdr( _name, carto::DataTypeCode<T>().dataType(),
95 thing.getSizeX(), thing.getSizeY(),
96 thing.getSizeZ(), thing.getSizeT(),
97 vs[0], vs[1], vs[2], vs[3] );
98 const carto::PropertySet *ph = &thing.header();
100
101 std::string dir = carto::FileUtil::dirname( _name );
102 std::vector<std::string> files = hdr.outputFilenames();
103
104 hdr.setProperty( "file_type", format() );
105 hdr.setProperty( "filenames", files );
106 hdr.setProperty( "data_type", carto::DataTypeCode<T>::name() );
107 if( !dir.empty() )
109
110 unsigned i = 0;
111 for( t=0; t<dt; ++t )
112 for( z=0; z<dz; ++z, ++i )
113 writeFrame( thing, dir + files[i], z, t );
114
115 hdr.writeMinf( dir + carto::FileUtil::removeExtension( files[0] ) + '.'
116 + hdr.extension() + ".minf" );
117 }
118
119
120 template<typename T>
121 inline
122 std::string QtFormatsWriter<T>::format() const
123 {
124 std::string f = carto::stringUpper( carto::FileUtil::extension( _name ) );
125 if( f == "JPG" )
126 return "JPEG";
127 else
128 return f;
129 }
130
131
132 template<typename T>
133 inline
135 const std::string & filename,
136 unsigned z, unsigned t )
137 {
138 int x, y, dx = thing.getSizeX(), dy = thing.getSizeY();
139 QImage im( dx, dy, QImage::Format_RGB32 );
142 im = im.convertToFormat( QImage::Format_ARGB32 );
143 std::vector<float> vs = thing.getVoxelSize();
144 im.setDotsPerMeterX( (int) rint( 1000 / vs[0] ) );
145 im.setDotsPerMeterY( (int) rint( 1000 / vs[1] ) );
146 for( y=0; y<dy; ++y )
147 for( x=0; x<dx; ++x )
148 im.setPixel( x, y, convertColor( thing.at( x, y, z, t ) ) );
149 if( !im.save( filename.c_str(),
150 format().c_str(),
151 100 ) )
153 }
154
155
156 template<>
157 inline
159 const carto::Volume<uint8_t> & thing,
160 const std::string & filename,
161 unsigned z, unsigned t )
162 {
163 int x, y, dx = thing.getSizeX(), dy = thing.getSizeY();
164 QImage im( dx, dy, QImage::Format_Indexed8 );
165 std::vector<float> vs = thing.getVoxelSize();
166 im.setDotsPerMeterX( (int) rint( 1000 / vs[0] ) );
167 im.setDotsPerMeterY( (int) rint( 1000 / vs[1] ) );
168 for( x=0; x<256; ++x )
169 im.setColor( x, qRgb( x, x, x ) );
170 for( y=0; y<dy; ++y )
171 for( x=0; x<dx; ++x )
172 im.setPixel( x, y, thing.at( x, y, z, t ) );
173 if( !im.save( filename.c_str(),
175 ) ).c_str(),
176 100 ) )
178 }
179
180
181 template<typename T> inline
182 QRgb QtFormatsWriter<T>::convertColor( const T & x )
183 {
184 return qRgb( x, x, x );
185 }
186
187
188 template<> inline
189 QRgb QtFormatsWriter<AimsRGB>::convertColor( const AimsRGB & x )
190 {
191 return qRgb( x.red(), x.green(), x.blue() );
192 }
193
194
195 template<> inline
196 QRgb QtFormatsWriter<AimsRGBA>::convertColor( const AimsRGBA & x )
197 {
198 return qRgba( x.red(), x.green(), x.blue(), x.alpha() );
199 }
200
201}
202
203#endif
204
virtual bool writeMinf(const std::string &filename)
The descriptor class of the .dim GIS header.
virtual std::string extension() const
void writeFrame(const carto::Volume< T > &thing, const std::string &filename, unsigned zfame, unsigned tframe)
called by write(), but you can call it for single frame writing (axial slice)
Definition qtformatsW.h:134
void write(const carto::Volume< T > &thing)
Definition qtformatsW.h:85
std::string format() const
Definition qtformatsW.h:122
QtFormatsWriter(const std::string &name)
Definition qtformatsW.h:56
virtual std::vector< std::string > outputFilenames() const
std::string name()
virtual void copyProperties(Object source)
static char separator()
static std::string dirname(const std::string &)
static std::string extension(const std::string &)
static std::string removeExtension(const std::string &)
const PropertySet & header() const
Object reference(Object &value)
virtual void setProperty(const std::string &, Object)
int getSizeY() const
std::vector< float > getVoxelSize() const
int getSizeX() const
int getSizeZ() const
int getSizeT() const
const T & at(long x, long y=0, long z=0, long t=0) const
const uint8_t & alpha() const
const uint8_t & blue() const
const uint8_t & red() const
const uint8_t & green() const
const uint8_t & red() const
const uint8_t & green() const
const uint8_t & blue() const
static void launchErrnoExcept(const std::string &filename="")
AIMSDATA_API PovWriter< D, T > & operator<<(PovWriter< D, T > &writer, const AimsTimeSurface< D, T > &thing)
std::string stringUpper(const std::string &)
carto::VoxelRGB AimsRGB