aimsdata  5.0.5
Neuroimaging data handling
jpegW.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_JPEGW_H
35 #define AIMS_IO_JPEGW_H
36 
37 #include <aims/io/jpegheader.h>
38 #include <aims/io/datatypecode.h>
39 #include <aims/data/pheader.h>
40 #include <aims/data/data.h>
44 #include <iomanip>
45 #include <stdio.h>
46 extern "C"
47 {
48 #include <jpeglib.h>
49 }
50 
51 
52 namespace aims
53 {
54 
55  template<class T>
56  class JpegWriter
57  {
58  public:
59  JpegWriter( const std::string& name ) : _name( name ) {}
61 
62  void write( const AimsData<T> & thing );
65  void writeFrame( const AimsData<T> & thing, const std::string & filename,
66  unsigned zfame, unsigned tframe );
68  std::string removeExtension( const std::string& name ) const;
69 
70  private:
71  std::string _name;
72  };
73 
74  template <class T>
75  inline JpegWriter<T> &
76  operator << ( JpegWriter<T> & writer, const AimsData<T> & thing )
77  {
78  writer.write( thing );
79  return writer;
80  }
81 
82 
83  template <class T>
84  inline
85  void JpegWriter<T>::write( const AimsData<T>& thing )
86  {
87  unsigned t, z, dt = thing.dimT(), dz = thing.dimZ();
88  JpegHeader hdr( _name, carto::DataTypeCode<T>().dataType(), thing.dimX(),
89  thing.dimY(), thing.dimZ(), thing.dimT(), thing.sizeX(),
90  thing.sizeY(), thing.sizeZ(), thing.sizeT() );
91  const PythonHeader
92  *ph = dynamic_cast<const PythonHeader *>( thing.header() );
93  if( ph )
94  hdr.copy( *ph );
95 
96  std::string dir = carto::FileUtil::dirname( _name );
97  std::vector<std::string> files = hdr.outputFilenames();
98 
99  hdr.setProperty( "file_type", std::string( "JPEG" ) );
100  hdr.setProperty( "object_type", carto::DataTypeCode<T>().objectType() );
101  hdr.setProperty( "data_type", carto::DataTypeCode<T>().dataType() );
102  hdr.setProperty( "filenames", files );
103  if( !dir.empty() )
105 
106  unsigned i = 0;
107  for( t=0; t<dt; ++t )
108  for( z=0; z<dz; ++z, ++i )
109  writeFrame( thing, dir + files[i], z, t );
110 
111  hdr.writeMinf( dir + carto::FileUtil::removeExtension( files[0] )
112  + hdr.extension() + ".minf" );
113  }
114 
115  template<class T>
116  inline
118  const std::string & filename, unsigned z,
119  unsigned t )
120  {
121  struct jpeg_compress_struct cinfo;
122  struct jpeg_error_mgr jerr;
123  FILE *fp;
124  unsigned i;
125  JSAMPROW row_pointer[1];
126 
127  cinfo.err = jpeg_std_error( &jerr );
128  jpeg_create_compress( &cinfo );
129 
130  cinfo.image_width = thing.dimX();
131  cinfo.image_height = thing.dimY();
132  cinfo.in_color_space = ( sizeof( T ) == 3 ? JCS_RGB : JCS_GRAYSCALE );
133  cinfo.input_components = ( sizeof( T ) == 3 ? 3 : 1 );
134  cinfo.input_gamma = 1;
135  jpeg_set_defaults( &cinfo );
136  if( sizeof( T ) != 3 )
137  cinfo.data_precision = 8 * sizeof( T );
138  cinfo.smoothing_factor = 0;
139  jpeg_set_quality( &cinfo, 100, TRUE );
140  cinfo.density_unit = 1;
141  cinfo.X_density = (UINT16) ( 25.4 / thing.sizeX() );
142  cinfo.Y_density = (UINT16) ( 25.4 / thing.sizeY() );
143 
144  fp = fopen( filename.c_str(), "wb" );
145  if( !fp )
146  throw carto::file_error( filename );
147 
148  jpeg_stdio_dest( &cinfo, fp );
149  jpeg_start_compress( &cinfo, TRUE );
150 
151  for( i=0; i<cinfo.image_height; ++i )
152  {
153  row_pointer[0] = (JSAMPROW) &thing( 0, i, z, t );
154  jpeg_write_scanlines( &cinfo, row_pointer, 1 );
155  }
156 
157  jpeg_finish_compress( &cinfo );
158  fclose( fp );
159  jpeg_destroy_compress( &cinfo );
160  }
161 
162 }
163 
164 #endif
165 
int dimZ() const
Attributed python-like header, stores all needed information about an object, currently used for volu...
Definition: pheader.h:51
void write(const AimsData< T > &thing)
Definition: jpegW.h:85
float sizeZ() const
int dimY() const
virtual void copy(const PythonHeader &, bool keepUuid=false)
float sizeT() const
The class for EcatSino data write operation.
Definition: border.h:44
static std::string removeExtension(const std::string &)
float sizeX() const
static char separator()
The descriptor class of the .dim GIS header.
Definition: jpegheader.h:52
JpegWriter(const std::string &name)
Definition: jpegW.h:59
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: jpegW.h:117
static std::string dirname(const std::string &)
const aims::Header * header() const
float sizeY() const
std::string removeExtension(const std::string &name) const
Return a name without .jpg extension.
int dimT() const
int dimX() const