-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathimg_in.hpp
290 lines (254 loc) · 7.18 KB
/
img_in.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#pragma once
//=====================================================================//
/*! @file
@brief 画像ローダー
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2018 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include <cstdint>
#include "graphics/bmp_in.hpp"
#include "graphics/picojpeg_in.hpp"
// PNG のロードを有効にする場合
#define ENABLE_PNG
// TGA のロードを有効にする場合
// #define ENABLE_TGA
#ifdef ENABLE_PNG
#include "graphics/png_in.hpp"
#endif
namespace img {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 画像ローダー
@param[in] PLOT 描画ファンクタ
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class PLOT>
class img_in {
typedef bmp_in<PLOT> BMP;
typedef picojpeg_in<PLOT> JPEG;
BMP bmp_;
JPEG jpeg_;
#ifdef ENABLE_PNG
typedef png_in<PLOT> PNG;
PNG png_;
#endif
enum class TYPE : uint8_t {
NONE,
BMP,
JPEG,
PNG,
TGA,
};
TYPE type_;
bool img_switch_(const char* fname)
{
type_ = TYPE::NONE;
bool ret = false;
auto ext = strrchr(fname, '.');
if(ext != nullptr) {
if(utils::str::scan_ext(ext, bmp_.get_file_ext())) {
type_ = TYPE::BMP;
ret = true;
} else if(utils::str::scan_ext(ext, jpeg_.get_file_ext())) {
type_ = TYPE::JPEG;
ret = true;
#ifdef ENABLE_PNG
} else if(utils::str::scan_ext(ext, png_.get_file_ext())) {
type_ = TYPE::PNG;
ret = true;
#endif
}
} else {
// 拡張子が無い・・
}
return ret;
}
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
@param[in] plot 描画ファンクタ
*/
//-----------------------------------------------------------------//
img_in(PLOT& plot) noexcept : bmp_(plot), jpeg_(plot),
#ifdef ENABLE_PNG
png_(plot),
#endif
type_(TYPE::NONE) { }
//-----------------------------------------------------------------//
/*!
@brief BMP コンテキストへの参照
@return BMP コンテキスト
*/
//-----------------------------------------------------------------//
auto& at_bmp() noexcept { return bmp_; }
//-----------------------------------------------------------------//
/*!
@brief JPEG コンテキストへの参照
@return JPEG コンテキスト
*/
//-----------------------------------------------------------------//
auto& at_jpeg() noexcept { return jpeg_; }
#ifdef ENABLE_PNG
//-----------------------------------------------------------------//
/*!
@brief PNG コンテキストへの参照
@return PNG コンテキスト
*/
//-----------------------------------------------------------------//
auto& at_png() noexcept { return png_; }
#endif
//-----------------------------------------------------------------//
/*!
@brief 画像ファイルローダーの選択
@param[in] ext 画像ファイルの拡張子
@return エラーなら「false」を返す
*/
//-----------------------------------------------------------------//
bool select_decoder(const char* ext) noexcept {
if(ext == nullptr) return false;
char tmp[8];
tmp[0] = '.';
strcpy(&tmp[1], ext);
return img_switch_(tmp);
}
//-----------------------------------------------------------------//
/*!
@brief 画像ファイルか確認する
@param[in] fin file_io クラス
@return エラーなら「false」を返す
*/
//-----------------------------------------------------------------//
bool probe(utils::file_io& fin) noexcept {
switch(type_) {
case TYPE::BMP:
return bmp_.probe(fin);
case TYPE::JPEG:
return jpeg_.probe(fin);
#ifdef ENABLE_PNG
case TYPE::PNG:
return png_.probe(fin);
#endif
default:
break;
}
return false;
}
//-----------------------------------------------------------------//
/*!
@brief 画像ファイルか確認する
@param[in] fname ファイル名
@return エラーなら「false」を返す
*/
//-----------------------------------------------------------------//
bool probe(const char* fname) noexcept {
if(fname == nullptr) return false;
if(!img_switch_(fname)) {
return false;
}
utils::file_io fin;
if(!fin.open(fname, "rb")) {
return false;
}
auto ret = probe(fin);
fin.close();
type_ = TYPE::NONE;
return ret;
}
//-----------------------------------------------------------------//
/*!
@brief 画像ファイルの情報を取得する
@param[in] fin file_io クラス
@param[in] fo 情報を受け取る構造体
@return エラーなら「false」を返す
*/
//-----------------------------------------------------------------//
bool info(utils::file_io& fin, img::img_info& fo) noexcept {
switch(type_) {
case TYPE::BMP:
return bmp_.info(fin, fo);
case TYPE::JPEG:
return jpeg_.info(fin, fo);
#ifdef ENABLE_PNG
case TYPE::PNG:
return png_.info(fin, fo);
#endif
default:
break;
}
return false;
}
//-----------------------------------------------------------------//
/*!
@brief 画像ファイルの情報を取得する
@param[in] fname ファイル名
@param[in] fo 情報を受け取る構造体
@return エラーなら「false」を返す
*/
//-----------------------------------------------------------------//
bool info(const char* fname, img::img_info& fo) noexcept {
if(fname == nullptr) return false;
if(!img_switch_(fname)) {
return false;
}
utils::file_io fin;
if(!fin.open(fname, "rb")) {
return false;
}
auto ret = info(fin, fo);
fin.close();
type_ = TYPE::NONE;
return ret;
}
//-----------------------------------------------------------------//
/*!
@brief 画像ファイルをロードする @n
※ロードする画像タイプが「type_」に設定されている事。
@param[in] fin ファイル I/O クラス
@param[in] opt フォーマット固有の設定文字列
@return エラーがあれば「false」
*/
//-----------------------------------------------------------------//
bool load(utils::file_io& fin, const char* opt = nullptr) noexcept {
switch(type_) {
case TYPE::BMP:
return bmp_.load(fin, opt);
case TYPE::JPEG:
return jpeg_.load(fin, opt);
#ifdef ENABLE_PNG
case TYPE::PNG:
return png_.load(fin, opt);
#endif
default:
break;
}
return false;
}
//-----------------------------------------------------------------//
/*!
@brief 画像ファイルをロードする
@param[in] fname ファイル名
@param[in] opt フォーマット固有の設定文字列
@return エラーがあれば「false」
*/
//-----------------------------------------------------------------//
bool load(const char* fname, const char* opt = nullptr) noexcept
{
if(fname == nullptr) return false;
if(!img_switch_(fname)) {
return false;
}
utils::file_io fin;
if(!fin.open(fname, "rb")) {
return false;
}
auto ret = load(fin, opt);
fin.close();
type_ = TYPE::NONE;
return ret;
}
};
}