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

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

image.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_IMAGE__
34 #define __INCLUDE_MIST_IMAGE__
35
36
37 #ifndef __INCLUDE_MIST_H__
38 #include "../mist.h"
39 #endif
40
41 #ifndef __INCLUDE_MIST_BMP__
42 #include "bmp.h"
43 #endif
44
45 #ifndef __INCLUDE_MIST_PNG__
46 #include "png.h"
47 #endif
48
49 #ifndef __INCLUDE_MIST_GIF__
50 #include "gif.h"
51 #endif
52
53 #ifndef __INCLUDE_MIST_TIFF__
54 #include "tiff.h"
55 #endif
56
57 #ifndef __INCLUDE_MIST_JPEG__
58 #include "jpeg.h"
59 #endif
60
61 #ifndef __INCLUDE_MIST_PNM__
62 #include "pnm.h"
63 #endif
64
65 #ifndef __INCLUDE_MIST_TGA__
66 #include "tga.h"
67 #endif
68
69 #include <iostream>
70 #include <string>
71
72
73 // mist名前空間の始まり
74 _MIST_BEGIN
75
76
77 namespace __image_controller__
78 {
79 inline std::string to_lower_case( const std::string &str )
80 {
81 std::string s = "";
82 for( std::string::size_type i = 0 ; i < str.size( ) ; i++ )
83 {
84 s += static_cast< char >( tolower( str[ i ] ) );
85 }
86 return( s );
87 }
88
89 inline std::string get_ext( const std::string &str )
90 {
91 std::string::size_type index = str.rfind( '.' );
92 if( index == str.npos )
93 {
94 return( "" );
95 }
96 return( str.substr( index ) );
97 }
98 }
99
100
108
109
110
121 template < class T, class Allocator >
122 inline bool read_image( mist::array2< T, Allocator > &image, const std::string &filename )
123 {
124 typedef typename mist::array2< T, Allocator >::size_type size_type;
125
126 std::string ext = __image_controller__::to_lower_case( __image_controller__::get_ext( filename ) );
127 if( ext == "" )
128 {
129 return( false );
130 }
131
132 bool ret = false;
133 if( ext == ".jpeg" || ext == ".jpg" )
134 {
135 ret = mist::read_jpeg( image, filename );
136 }
137 else if( ext == ".bmp" )
138 {
139 ret = mist::read_bmp( image, filename );
140 }
141 else if( ext == ".tiff" || ext == ".tif" )
142 {
143 ret = mist::read_tiff( image, filename );
144 }
145 else if( ext == ".png" )
146 {
147 ret = mist::read_png( image, filename );
148 }
149 else if( ext == ".pbm" || ext == ".pgm" || ext == ".ppm" || ext == ".pnm" )
150 {
151 ret = mist::read_pnm( image, filename );
152 }
153 else if( ext == ".tga" )
154 {
155 ret = mist::read_tga( image, filename );
156 }
157 else if( ext == ".gif" )
158 {
159 ret = mist::read_gif( image, filename );
160 }
161 else
162 {
163 ret = false;
164 }
165 return( ret );
166 }
167
178 template < class T, class Allocator >
179 inline bool read_image( mist::array2< T, Allocator > &image, const std::wstring &filename )
180 {
181 return( read_image( image, wstr2str( filename ) ) );
182 }
183
195 template < class T, class Allocator >
196 inline bool write_image( const mist::array2< T, Allocator > &image, const std::string &filename )
197 {
198 typedef typename mist::array2< T, Allocator >::size_type size_type;
199
200 std::string ext = __image_controller__::to_lower_case( __image_controller__::get_ext( filename ) );
201 if( ext == "" )
202 {
203 std::cerr << "Unknown file type!" << std::endl;
204 return( false );
205 }
206
207 if( image.width( ) == 0 )
208 {
209 std::cerr << "Image width is zero!" << std::endl;
210 return( false );
211 }
212 else if( image.height( ) == 0 )
213 {
214 std::cerr << "Image height is zero!" << std::endl;
215 return( false );
216 }
217
218 bool ret = false;
219 if( ext == ".jpeg" || ext == ".jpg" )
220 {
221 ret = mist::write_jpeg( image, filename );
222 }
223 else if( ext == ".bmp" )
224 {
225 ret = mist::write_bmp( image, filename );
226 }
227 else if( ext == ".tiff" || ext == ".tif" )
228 {
229 ret = mist::write_tiff( image, filename );
230 }
231 else if( ext == ".png" )
232 {
233 ret = mist::write_png( image, filename );
234 }
235 else if( ext == ".pbm" )
236 {
237 ret = mist::write_pnm( image, filename, 1 );
238 }
239 else if( ext == ".pgm" )
240 {
241 ret = mist::write_pnm( image, filename, 2 );
242 }
243 else if( ext == ".ppm" || ext == ".pnm" )
244 {
245 ret = mist::write_pnm( image, filename, 3 );
246 }
247 else if( ext == ".tga" )
248 {
249 ret = mist::write_tga( image, filename );
250 }
251 else
252 {
253 ret = false;
254 }
255 return( ret );
256 }
257
269 template < class T, class Allocator >
270 inline bool write_image( const mist::array2< T, Allocator > &image, const std::wstring &filename )
271 {
272 return( write_image( image, wstr2str( filename ) ) );
273 }
274
276 // 画像入出力グループの終わり
277
278
279 // mist名前空間の終わり
280 _MIST_END
281
282
283 #endif // __INCLUDE_MIST_IMAGE__
284