- メインページ
- モジュール
- ネームスペース
- データ構造
- ファイル

- ファイル一覧
- グローバル

raw.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28
33 #ifndef __INCLUDE_MIST_RAW__
34 #define __INCLUDE_MIST_RAW__
35
36
37 #ifndef __INCLUDE_MIST_H__
38 #include "../mist.h"
39 #endif
40
41 // カラー画像の設定を読み込む
42 #ifndef __INCLUDE_MIST_COLOR_H__
43 #include "../config/color.h"
44 #endif
45
46 #ifndef __INCLUDE_MIST_ENDIAN__
47 #include "../config/endian.h"
48 #endif
49
50 #include <iostream>
51 #include <string>
52 #include <zlib.h>
53
54
55
56 // mist名前空間の始まり
57 _MIST_BEGIN
58
59
60 namespace __raw_controller__
61 {
62 struct resize_image
63 {
64 // ファイルから読み出されたデータ量が，指定されたものよりも少ない場合
65 template < class T, class Allocator >
66 static bool resize( array1< T, Allocator > &image, typename array1< T, Allocator >::size_type num_elements )
67 {
68 typedef typename array1< T, Allocator >::size_type size_type;
69
70 if( num_elements == 0 )
71 {
72 return( false );
73 }
74 else if( image.size( ) == num_elements )
75 {
76 return( true );
77 }
78 else
79 {
80 array1< T, Allocator > tmp( image );
81 image.resize( num_elements );
82 for( size_type i = 0 ; i < image.size( ) ; i++ )
83 {
84 image[ i ] = tmp[ i ];
85 }
86 }
87 return( true );
88 }
89
90 template < class T, class Allocator >
91 static bool resize( array2< T, Allocator > &image, typename array2< T, Allocator >::size_type num_elements )
92 {
93 typedef typename array2< T, Allocator >::size_type size_type;
94
95 size_type num_lines = num_elements / image.width( );
96 if( num_lines == 0 )
97 {
98 return( false );
99 }
100 else if( image.height( ) == num_lines )
101 {
102 return( true );
103 }
104 else
105 {
106 array2< T, Allocator > tmp( image );
107 image.resize( image.width( ), num_lines );
108 for( size_type i = 0 ; i < image.size( ) ; i++ )
109 {
110 image[i] = tmp[i];
111 }
112 }
113 return( true );
114 }
115
116 template < class T, class Allocator >
117 static bool resize( array3< T, Allocator > &image, typename array3< T, Allocator >::size_type num_elements )
118 {
119 typedef typename array3< T, Allocator >::size_type size_type;
120
121 size_type num_slices = num_elements / ( image.width( ) * image.height( ) );
122 if( num_slices == 0 )
123 {
124 return( false );
125 }
126 else if( image.depth( ) == num_slices )
127 {
128 return( true );
129 }
130 else
131 {
132 array3< T, Allocator > tmp( image );
133 image.resize( image.width( ), image.height( ), num_slices );
134 for( size_type i = 0 ; i < image.size( ) ; i++ )
135 {
136 image[ i ] = tmp[ i ];
137 }
138 }
139 return( true );
140 }
141 };
142
143 struct raw_controller
144 {
145 template < class Array, class Functor, class ValueType >
146 static bool read( Array &image,
147 const std::string &filename,
148 typename Array::size_type w,
149 typename Array::size_type h,
150 typename Array::size_type d,
151 typename Array::value_type offset,
152 bool from_little_endian,
153 typename Array::size_type skip_num_bytes,
154 Functor f,
155 ValueType /* v */
156 )
157 {
158 typedef typename Array::value_type value_type;
159 typedef typename Array::size_type size_type;
160
161 gzFile fp;
162
163 if( ( fp = gzopen( filename.c_str( ), "rb" ) ) == NULL )
164 {
165 std::cerr << "Error occured while opening RAW file format in [" << filename << "]" << std::endl;
166 return( false );
167 }
168
169 image.resize( w, h, d );
170
171 size_type byte = sizeof( ValueType );
172 unsigned char tmparray[ sizeof( ValueType ) * 4096 ];
173 byte_array< ValueType > data;
174
175 f( 0.0 );
176
177 size_type i = 0;
178 unsigned int pre_progress = 0;
179 while( !gzeof( fp ) )
180 {
181 size_type num = gzread( fp, ( void * )tmparray, sizeof( ValueType ) * 4096 );
182
183 if( num <= skip_num_bytes )
184 {
185 skip_num_bytes -= num;
186 continue;
187 }
188
189 size_type j = skip_num_bytes;
190 skip_num_bytes = 0;
191
192 for( ; i < image.size( ) && j < num ; j += byte )
193 {
194 for( size_type l = 0 ; l < byte ; l++ )
195 {
196 data[ l ] = tmparray[ j + l ];
197 }
198 image[ i++ ] = static_cast< value_type >( to_current_endian( data, from_little_endian ).get_value( ) ) + offset;
199 }
200
201 if( i >= image.size( ) )
202 {
203 break;
204 }
205
206 // 進行状況を0〜100％で表示する
207 // コールバック関数の戻り値が false になった場合は処理を中断し，制御を返す
208 unsigned int progress = static_cast< unsigned int >( static_cast< double >( i + 1 ) / static_cast< double >( image.size( ) ) * 100.0 );
209 if( progress != pre_progress )
210 {
211 pre_progress = progress;
212 if( !f( progress ) )
213 {
214 image.clear( );
215 gzclose( fp );
216 return( false );
217 }
218 }
219 }
220
221 gzclose( fp );
222
223 // ファイルから読み出されたデータ量が，指定されたものよりも少ない場合
224 if( !resize_image::resize( image, i ) )
225 {
226 // 1枚もデータが読み込まれなかった場合
227 f( 100.1 );
228 return( false );
229 }
230 else
231 {
232 f( 100.1 );
233 return( true );
234 }
235 }
236
237
238 template < class Array, class Functor, class ValueType >
239 static bool write( const Array &image, const std::string &filename, typename Array::value_type offset, bool to_little_endian, Functor f, ValueType v )
240 {
241 typedef typename Array::value_type value_type;
242 typedef typename Array::size_type size_type;
243
244 FILE *fp;
245
246 if( ( fp = fopen( filename.c_str( ), "wb" ) ) == NULL )
247 {
248 std::cerr << "Error occured while opening RAW file format in [" << filename << "]" << std::endl;
249 return( false );
250 }
251
252 size_type w = image.width( );
253 size_type h = image.height( );
254 size_type d = image.depth( );
255 unsigned char tmparray[ sizeof( ValueType ) * 4096 ];
256 byte_array< ValueType > data;
257
258 f( 0.0 );
259
260 size_type i = 0, n, l, pre_progress = 0;
261 while( i < image.size( ) )
262 {
263 for( n = 0 ; i < image.size( ) && n < sizeof( ValueType ) * 4096 ; i++, n += sizeof( ValueType ) )
264 {
265 data.set_value( static_cast< ValueType >( image[ i ] + offset ) );
266 data = from_current_endian( data, to_little_endian );
267 for( l = 0 ; l < sizeof( ValueType ) ; l++ )
268 {
269 tmparray[ n + l ] = data[ l ];
270 }
271 }
272 fwrite( tmparray, 1, n, fp );
273
274 // 進行状況を0〜100％で表示する
275 // コールバック関数の戻り値が false になった場合は処理を中断し，制御を返す
276 size_type progress = static_cast< size_type >( static_cast< double >( i ) / static_cast< double >( image.size( ) ) * 100.0 );
277 if( progress != pre_progress )
278 {
279 pre_progress = progress;
280 if( !f( static_cast< double >( progress ) ) )
281 {
282 fclose( fp );
283 return( false );
284 }
285 }
286 }
287
288 fclose( fp );
289
290 f( 100.1 );
291
292 return( true );
293 }
294
295 template < class Array, class Functor, class ValueType >
296 static bool write_gz( const Array &image, const std::string &filename, typename Array::value_type offset, bool to_little_endian, Functor f, ValueType v )
297 {
298 typedef typename Array::value_type value_type;
299 typedef typename Array::size_type size_type;
300
301 gzFile fp;
302
303 if( ( fp = gzopen( filename.c_str( ), "wb" ) ) == NULL )
304 {
305 std::cerr << "Error occured while opening RAW + GZ file format in [" << filename << "]" << std::endl;
306 return( false );
307 }
308
309 unsigned char tmparray[ sizeof( ValueType ) * 4096 ];
310 byte_array< ValueType > data;
311
312 f( 0.0 );
313
314 size_type i = 0, n, l, pre_progress = 0;
315 while( i < image.size( ) )
316 {
317 for( n = 0 ; i < image.size( ) && n < sizeof( ValueType ) * 4096 ; i++, n += sizeof( ValueType ) )
318 {
319 data.set_value( static_cast< ValueType >( image[ i ] + offset ) );
320 data = from_current_endian( data, to_little_endian );
321 for( l = 0 ; l < sizeof( ValueType ) ; l++ )
322 {
323 tmparray[ n + l ] = data[ l ];
324 }
325 }
326 gzwrite( fp, tmparray, static_cast< unsigned int >( n ) );
327
328 // 進行状況を0〜100％で表示する
329 // コールバック関数の戻り値が false になった場合は処理を中断し，制御を返す
330 size_type progress = static_cast< size_type >( static_cast< double >( i ) / static_cast< double >( image.size( ) ) * 100.0 );
331 if( progress != pre_progress )
332 {
333 pre_progress = progress;
334 if( !f( static_cast< double >( progress ) ) )
335 {
336 gzclose( fp );
337 return( false );
338 }
339 }
340 }
341
342 gzclose( fp );
343
344 f( 100.1 );
345
346 return( true );
347 }
348 };
349 }
350
351
354
365
366
367
368
382 template < class T, class Allocator, class ValueType, class Functor >
383 bool read_raw( array< T, Allocator > &image, const std::string &filename, typename array< T, Allocator >::size_type w,
384 typename array< T, Allocator >::value_type offset, bool from_little_endian, ValueType __dmy__, Functor callback )
385 {
386 return( __raw_controller__::raw_controller::read( image, filename, w, 1, 1, offset, from_little_endian, 0, callback, __dmy__ ) );
387 }
388
403 template < class T, class Allocator, class ValueType, class Functor >
404 bool write_raw( const array< T, Allocator > &image, const std::string &filename, typename array< T, Allocator >::value_type offset, bool to_little_endian, ValueType __dmy__, Functor callback )
405 {
406 return( __raw_controller__::raw_controller::write( image, filename, offset, to_little_endian, callback, __dmy__ ) );
407 }
408
423 template < class T, class Allocator, class ValueType, class Functor >
424 bool write_raw_gz( const array< T, Allocator > &image, const std::string &filename, typename array< T, Allocator >::value_type offset, bool to_little_endian, ValueType __dmy__, Functor callback )
425 {
426 return( __raw_controller__::raw_controller::write_gz( image, filename, offset, to_little_endian, callback, __dmy__ ) );
427 }
428
429
430
443 template < class T, class Allocator, class Functor >
444 bool read_raw( array< T, Allocator > &image, const std::string &filename, typename array< T, Allocator >::size_type w,
445 typename array< T, Allocator >::value_type offset, bool from_little_endian, Functor callback )
446 {
447 typename array< T, Allocator >::value_type v( 0 );
448 return( read_raw( image, filename, w, offset, from_little_endian, v, callback ) );
449 }
450
451
452
464 template < class T, class Allocator, class Functor >
465 bool write_raw( const array< T, Allocator > &image, const std::string &filename, typename array< T, Allocator >::value_type offset, bool to_little_endian, Functor callback )
466 {
467 typename array< T, Allocator >::value_type v( 0 );
468 return( write_raw( image, filename, offset, to_little_endian, v, callback ) );
469 }
470
471
483 template < class T, class Allocator, class Functor >
484 bool write_raw_gz( const array< T, Allocator > &image, const std::string &filename, typename array< T, Allocator >::value_type offset, bool to_little_endian, Functor callback )
485 {
486 typename array< T, Allocator >::value_type v( 0 );
487 return( write_raw_gz( image, filename, offset, to_little_endian, v, callback ) );
488 }
489
490
502 template < class T, class Allocator >
503 bool read_raw( array< T, Allocator > &image, const std::string &filename, typename array< T, Allocator >::size_type w, typename array< T, Allocator >::value_type offset = 0, bool from_little_endian = false )
504 {
505 return( read_raw( image, filename, w, offset, from_little_endian, __mist_dmy_callback__( ) ) );
506 }
507
508
519 template < class T, class Allocator >
520 bool write_raw( const array< T, Allocator > &image, const std::string &filename, typename array< T, Allocator >::value_type offset = 0, bool to_little_endian = false )
521 {
522 return( write_raw( image, filename, offset, to_little_endian, __mist_dmy_callback__( ) ) );
523 }
524
525
536 template < class T, class Allocator >
537 bool write_raw_gz( const array< T, Allocator > &image, const std::string &filename, typename array< T, Allocator >::value_type offset = typename array< T, Allocator >::value_type( 0 ), bool to_little_endian = false )
538 {
539 return( write_raw_gz( image, filename, offset, to_little_endian, __mist_dmy_callback__( ) ) );
540 }
541
542
543
544
545
546
561 template < class T, class Allocator, class ValueType, class Functor >
562 bool read_raw( array1< T, Allocator > &image, const std::string &filename, typename array1< T, Allocator >::size_type w,
563 double x, typename array1< T, Allocator >::value_type offset, bool from_little_endian, ValueType __dmy__, Functor callback )
564 {
565 image.reso1( x );
566 return( __raw_controller__::raw_controller::read( image, filename, w, 1, 1, offset, from_little_endian, 0, callback, __dmy__ ) );
567 }
568
569
583 template < class T, class Allocator, class Functor >
584 bool read_raw( array1< T, Allocator > &image, const std::string &filename, typename array1< T, Allocator >::size_type w,
585 double x, typename array1< T, Allocator >::value_type offset, bool from_little_endian, Functor callback )
586 {
587 typename array1< T, Allocator >::value_type v( 0 );
588 return( read_raw( image, filename, w, x, offset, from_little_endian, v, callback ) );
589 }
590
591
604 template < class T, class Allocator >
605 bool read_raw( array1< T, Allocator > &image, const std::string &filename, typename array1< T, Allocator >::size_type w,
606 double x = 1.0, typename array1< T, Allocator >::value_type offset = 0, bool from_little_endian = false )
607 {
608 return( read_raw( image, filename, w, x, offset, from_little_endian, __mist_dmy_callback__( ) ) );
609 }
610
611
612
613
630 template < class T, class Allocator, class ValueType, class Functor >
631 bool read_raw( array2< T, Allocator > &image, const std::string &filename,
632 typename array2< T, Allocator >::size_type w, typename array2< T, Allocator >::size_type h,
633 double x, double y, typename array2< T, Allocator >::value_type offset, bool from_little_endian, ValueType __dmy__, Functor callback )
634 {
635 image.reso1( x );
636 image.reso2( y );
637 return( __raw_controller__::raw_controller::read( image, filename, w, h, 1, offset, from_little_endian, 0, callback, __dmy__ ) );
638 }
639
640
656 template < class T, class Allocator, class Functor >
657 bool read_raw( array2< T, Allocator > &image, const std::string &filename,
658 typename array2< T, Allocator >::size_type w, typename array2< T, Allocator >::size_type h,
659 double x, double y, typename array2< T, Allocator >::value_type offset, bool from_little_endian, Functor callback )
660 {
661 typename array2< T, Allocator >::value_type v( 0 );
662 return( read_raw( image, filename, w, h, x, y, offset, from_little_endian, v, callback ) );
663 }
664
665
666
681 template < class T, class Allocator >
682 bool read_raw( array2< T, Allocator > &image, const std::string &filename,
683 typename array2< T, Allocator >::size_type w, typename array2< T, Allocator >::size_type h,
684 double x = 1.0, double y = 1.0, typename array2< T, Allocator >::value_type offset = 0, bool from_little_endian = false )
685 {
686 return( read_raw( image, filename, w, h, x, y, offset, from_little_endian, __mist_dmy_callback__( ) ) );
687 }
688
689
690
691
710 template < class T, class Allocator, class ValueType, class Functor >
711 bool read_raw( array3< T, Allocator > &image, const std::string &filename,
712 typename array3< T, Allocator >::size_type w, typename array3< T, Allocator >::size_type h, typename array3< T, Allocator >::size_type d,
713 double x, double y, double z, typename array3< T, Allocator >::value_type offset, bool from_little_endian, ValueType __dmy__, Functor callback )
714 {
715 image.reso1( x );
716 image.reso2( y );
717 image.reso3( z );
718 return( __raw_controller__::raw_controller::read( image, filename, w, h, d, offset, from_little_endian, 0, callback, __dmy__ ) );
719 }
720
721
722
723
741 template < class T, class Allocator, class Functor >
742 bool read_raw( array3< T, Allocator > &image, const std::string &filename,
743 typename array3< T, Allocator >::size_type w, typename array3< T, Allocator >::size_type h, typename array3< T, Allocator >::size_type d,
744 double x, double y, double z, typename array3< T, Allocator >::value_type offset, bool from_little_endian, Functor callback )
745 {
746 typename array3< T, Allocator >::value_type v( 0 );
747 return( read_raw( image, filename, w, h, d, x, y, z, offset, from_little_endian, v, callback ) );
748 }
749
750
751
768 template < class T, class Allocator >
769 bool read_raw( array3< T, Allocator > &image, const std::string &filename,
770 typename array3< T, Allocator >::size_type w, typename array3< T, Allocator >::size_type h, typename array3< T, Allocator >::size_type d,
771 double x = 1.0, double y = 1.0, double z = 1.0, typename array3< T, Allocator >::value_type offset = typename array3< T, Allocator >::value_type( 0 ), bool from_little_endian = false )
772 {
773 return( read_raw( image, filename, w, h, d, x, y, z, offset, from_little_endian, __mist_dmy_callback__( ) ) );
774 }
775
776
777
778
780 // RAW画像入出力グループの終わり
781
783 // 画像入出力グループの終わり
784
785
786 // mist名前空間の終わり
787 _MIST_END
788
789
790 #endif // __INCLUDE_MIST_RAW__
791