36 #ifndef __INCLUDE_MIST_JPEG__
37 #define __INCLUDE_MIST_JPEG__
40 #ifndef __INCLUDE_MIST_H__
45 #ifndef __INCLUDE_MIST_COLOR_H__
46 #include "../config/color.h"
49 #ifndef __INCLUDE_MIST_LIMITS__
50 #include "../limits.h"
58 #if defined( __MIST_WINDOWS__ ) && __MIST_WINDOWS__ > 0
61 #define HAVE_INT32 // JPEG用INT32型を持っている宣言
62 #define HAVE_BOOLEAN // JPEG用boolean型を持っている宣言
76 namespace __jpeg_controller__
78 template <
class T,
class Allocator >
79 struct jpeg_controller
81 typedef _pixel_converter_< T > pixel_converter;
82 typedef typename pixel_converter::color_type color_type;
84 static bool read( array2< T, Allocator > &image,
const std::string &filename )
87 fin = fopen( filename.c_str( ),
"rb" );
88 if( fin == NULL )
return(
false );
92 jpeg_decompress_struct dinfo;
96 dinfo.err = jpeg_std_error( &jerr );
97 jpeg_create_decompress( &dinfo );
100 jpeg_stdio_src( &dinfo, fin );
102 int n = jpeg_read_header( &dinfo,
true );
105 jpeg_destroy_decompress( &dinfo );
109 jpeg_start_decompress( &dinfo );
110 scanlen = dinfo.output_width * dinfo.output_components;
112 image.resize( dinfo.output_width, dinfo.output_height );
114 JSAMPLE *buffer =
new JSAMPLE[ scanlen ];
115 for( j = 0 ; j < dinfo.output_height ; j++ )
117 bitmap[0] = &buffer[0];
118 if( dinfo.output_scanline < dinfo.output_height ) jpeg_read_scanlines( &dinfo, bitmap, 1 );
119 for( i = 0 ; i < dinfo.output_width ; i++ )
121 switch( dinfo.output_components )
124 image( i, j ) = pixel_converter::convert_to( buffer[ i ], buffer[ i ], buffer[ i ] );
128 image( i, j ) = pixel_converter::convert_to( buffer[ i * 3 + 0 ], buffer[ i * 3 + 1 ], buffer[ i * 3 + 2 ] );
137 jpeg_finish_decompress( &dinfo );
142 jpeg_destroy_decompress( &dinfo );
147 static bool write(
const array2< T, Allocator > &image,
const std::string &filename,
int quality )
149 if( image.width( ) == 0 )
151 std::cerr <<
"Image width is zero!" << std::endl;
154 else if( image.height( ) == 0 )
156 std::cerr <<
"Image height is zero!" << std::endl;
161 fout = fopen( filename.c_str( ),
"wb" );
162 if( fout == NULL )
return(
false );
165 JDIMENSION w =
static_cast< JDIMENSION
>( image.width( ) ), h = static_cast< JDIMENSION >( image.height( ) );
167 jpeg_compress_struct cinfo;
172 cinfo.err = jpeg_std_error( &jerr );
173 jpeg_create_compress( &cinfo );
175 jpeg_stdio_dest( &cinfo, fout );
176 cinfo.image_width = w;
177 cinfo.image_height = h;
178 cinfo.input_components = 3;
179 cinfo.in_color_space = JCS_RGB;
180 jpeg_set_defaults( &cinfo );
181 jpeg_set_quality( &cinfo, quality,
true );
182 jpeg_start_compress( &cinfo,
true );
184 JSAMPLE *buffer =
new JSAMPLE[ w * h * 3 ];
187 for( j = 0 ; j < h ; j++ )
189 for( i = 0 ; i < w ; i++ )
191 color_type c =
limits_0_255( pixel_converter::convert_from( image( i, j ) ) );
192 *p++ =
static_cast< JSAMPLE
>( c.r );
193 *p++ =
static_cast< JSAMPLE
>( c.g );
194 *p++ =
static_cast< JSAMPLE
>( c.b );
198 while( cinfo.next_scanline < cinfo.image_height )
200 bitmap[0] = &buffer[ cinfo.next_scanline * scanlen ];
201 jpeg_write_scanlines( &cinfo, bitmap, 1 );
204 jpeg_finish_compress( &cinfo );
209 jpeg_destroy_compress( &cinfo );
243 template <
class T,
class Allocator >
246 return( __jpeg_controller__::jpeg_controller< T, Allocator >::read( image, filename ) );
260 template <
class T,
class Allocator >
278 template <
class T,
class Allocator >
281 return( __jpeg_controller__::jpeg_controller< T, Allocator >::write( image, filename, quality ) );
296 template <
class T,
class Allocator >
314 #endif // __INCLUDE_MIST_JPEG__