aimsgui  5.1.2
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 <aims/data/data.h>
41 #include <aims/rgb/rgb.h>
45 #include <qimage.h>
46 #include <qapplication.h>
47 
48 
49 namespace aims
50 {
51 
52  template<typename T>
54  {
55  public:
56  QtFormatsWriter( const std::string& name ) : _name( name ) {}
58 
59  void write( const AimsData<T> & thing );
62  void writeFrame( const AimsData<T> & thing, const std::string & filename,
63  unsigned zfame, unsigned tframe );
64  std::string format() const;
65 
66  private:
67  std::string _name;
68 
69  static QRgb convertColor( const T & );
70  };
71 
72 
73  template <typename T>
74  inline QtFormatsWriter<T> &
75  operator << ( QtFormatsWriter<T> & writer, const AimsData<T> & thing )
76  {
77  writer.write( thing );
78  return writer;
79  }
80 
81 
82  template <typename T>
83  inline
85  {
86  std::string fmt = format();
87  // std::cout << "QtFormatsWriter< " << carto::DataTypeCode<T>::name() << " >::write, format: " << fmt << std::endl;
88  if( !qApp && ( fmt == "EPS" || fmt == "EPSF" || fmt == "EPSI" ) )
89  throw carto::format_error( fmt + " format needs a QApplication",
90  _name );
91  unsigned t, z, dt = thing.dimT(), dz = thing.dimZ();
92  QtFormatsHeader hdr( _name, carto::DataTypeCode<T>().dataType(),
93  thing.dimX(), thing.dimY(), thing.dimZ(),
94  thing.dimT(), thing.sizeX(),
95  thing.sizeY(), thing.sizeZ(), thing.sizeT() );
96  const PythonHeader
97  *ph = dynamic_cast<const PythonHeader *>( thing.header() );
98  if( ph )
99  hdr.copy( *ph );
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  if( !dir.empty() )
108 
109  unsigned i = 0;
110  for( t=0; t<dt; ++t )
111  for( z=0; z<dz; ++z, ++i )
112  writeFrame( thing, dir + files[i], z, t );
113 
114  hdr.writeMinf( dir + carto::FileUtil::removeExtension( files[0] ) + '.'
115  + hdr.extension() + ".minf" );
116  }
117 
118 
119  template<typename T>
120  inline
121  std::string QtFormatsWriter<T>::format() const
122  {
123  std::string f = carto::stringUpper( carto::FileUtil::extension( _name ) );
124  if( f == "JPG" )
125  return "JPEG";
126  else
127  return f;
128  }
129 
130 
131  template<typename T>
132  inline
134  const std::string & filename,
135  unsigned z, unsigned t )
136  {
137  int x, y, dx = thing.dimX(), dy = thing.dimY();
138  QImage im( dx, dy, QImage::Format_RGB32 );
141  im = im.convertToFormat( QImage::Format_ARGB32 );
142  im.setDotsPerMeterX( (int) rint( 1000 / thing.sizeX() ) );
143  im.setDotsPerMeterY( (int) rint( 1000 / thing.sizeY() ) );
144  for( y=0; y<dy; ++y )
145  for( x=0; x<dx; ++x )
146  im.setPixel( x, y, convertColor( thing( x, y, z, t ) ) );
147  if( !im.save( filename.c_str(),
148  format().c_str(),
149  100 ) )
151  }
152 
153 
154  template<>
155  inline
157  const std::string & filename,
158  unsigned z, unsigned t )
159  {
160  int x, y, dx = thing.dimX(), dy = thing.dimY();
161  QImage im( dx, dy, QImage::Format_Indexed8 );
162  im.setDotsPerMeterX( (int) rint( 1000 / thing.sizeX() ) );
163  im.setDotsPerMeterY( (int) rint( 1000 / thing.sizeY() ) );
164  for( x=0; x<256; ++x )
165  im.setColor( x, qRgb( x, x, x ) );
166  for( y=0; y<dy; ++y )
167  for( x=0; x<dx; ++x )
168  im.setPixel( x, y, thing( x, y, z, t ) );
169  if( !im.save( filename.c_str(),
171  ) ).c_str(),
172  100 ) )
174  }
175 
176 
177  template<typename T> inline
178  QRgb QtFormatsWriter<T>::convertColor( const T & x )
179  {
180  return qRgb( x, x, x );
181  }
182 
183 
184  template<> inline
185  QRgb QtFormatsWriter<AimsRGB>::convertColor( const AimsRGB & x )
186  {
187  return qRgb( x.red(), x.green(), x.blue() );
188  }
189 
190 
191  template<> inline
192  QRgb QtFormatsWriter<AimsRGBA>::convertColor( const AimsRGBA & x )
193  {
194  return qRgba( x.red(), x.green(), x.blue(), x.alpha() );
195  }
196 
197 }
198 
199 #endif
200 
float sizeX() const
float sizeT() const
int dimY() const
int dimX() const
int dimT() const
float sizeZ() const
float sizeY() const
int dimZ() const
const aims::Header * header() const
void write(const AffineTransformation3d &thing)
virtual void copy(const PythonHeader &, bool keepUuid=false)
virtual bool writeMinf(const std::string &filename)
The descriptor class of the .dim GIS header.
virtual std::string extension() const
void writeFrame(const AimsData< 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:133
void write(const AimsData< T > &thing)
Definition: qtformatsW.h:84
std::string format() const
Definition: qtformatsW.h:121
QtFormatsWriter(const std::string &name)
Definition: qtformatsW.h:56
virtual std::vector< std::string > outputFilenames() const
static char separator()
static std::string dirname(const std::string &)
static std::string extension(const std::string &)
static std::string removeExtension(const std::string &)
virtual void setProperty(const std::string &, Object)
const uint8_t & green() const
const uint8_t & blue() const
const uint8_t & alpha() const
const uint8_t & red() const
const uint8_t & red() const
const uint8_t & blue() const
const uint8_t & green() const
static void launchErrnoExcept(const std::string &filename="")
MotionWriter & operator<<(MotionWriter &writer, const AffineTransformation3d &thing) __attribute__((__deprecated__("OBSOLETE")))
std::string stringUpper(const std::string &)