aimsdata 6.0.0
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>
39#include <aims/data/pheader.h>
40#include <aims/data/data.h>
41#include <cartobase/stream/fileutil.h>
42#include <cartobase/stream/sstream.h>
43#include <cartobase/exception/file.h>
44#include <iomanip>
45#include <stdio.h>
46extern "C"
47{
48#include <jpeglib.h>
49}
50
51
52namespace aims
53{
54
55 template<class T>
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 long stride = &thing( 1 ) - &thing( 0 );
151 std::vector<T> buffer;
152 if( stride != 1 )
153 // allocate buffer for un-strided data
154 buffer.resize( cinfo.image_width );
155
156 for( i=0; i<cinfo.image_height; ++i )
157 {
158
159 if( stride == 1 )
160 row_pointer[0] = (JSAMPROW) &thing( 0, i, z, t );
161 else
162 {
163 // stides along X axis: must copy things
164 const T* p = &thing( 0, i, z, t );
165 for( long k=0; k<cinfo.image_width; ++k, p+=stride )
166 buffer[k] = *p;
167 row_pointer[0] = (JSAMPROW) &buffer[0];
168 }
169 jpeg_write_scanlines( &cinfo, row_pointer, 1 );
170 }
171
172 jpeg_finish_compress( &cinfo );
173 fclose( fp );
174 jpeg_destroy_compress( &cinfo );
175 }
176
177}
178
179#endif
180
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
The descriptor class of the .dim GIS header.
Definition jpegheader.h:53
virtual std::string extension() const
standard file format extension of specialized headers
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
JpegWriter(const std::string &name)
Definition jpegW.h:59
void write(const AimsData< T > &thing)
Definition jpegW.h:85
std::string removeExtension(const std::string &name) const
Return a name without .jpg extension.
Attributed python-like header, stores all needed information about an object, currently used for volu...
Definition pheader.h:52
virtual void copy(const PythonHeader &, bool keepUuid=false)
virtual bool writeMinf(const std::string &filename)
write meta-info header, non-const version (may change some attributes)
virtual std::vector< std::string > outputFilenames() const
static char separator()
static std::string dirname(const std::string &)
static std::string removeExtension(const std::string &)
virtual void setProperty(const std::string &, Object)
The class for EcatSino data write operation.
AIMSDATA_API PovWriter< D, T > & operator<<(PovWriter< D, T > &writer, const AimsTimeSurface< D, T > &thing)
Definition povW.h:72