Repository navigation
Expand file tree
/
Copy pathwebp_reader.cpp
More file actions
278 lines (240 loc) · 7.12 KB
/
Copy pathwebp_reader.cpp
File metadata and controls
278 lines (240 loc) · 7.12 KB
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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// mapnik
#include <mapnik/make_unique.hpp>
#include <mapnik/debug.hpp>
#include <mapnik/image_reader.hpp>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
extern "C"
{
#include <webp/types.h>
#include <webp/decode.h>
}
#pragma clang diagnostic pop
// boost
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#pragma GCC diagnostic pop
// stl
#include <fstream>
namespace mapnik
{
struct external_buffer_policy
{
external_buffer_policy( uint8_t const* data, std::size_t size)
: data_(data),
size_(size) {}
uint8_t const* data() const
{
return data_;
}
std::size_t size() const
{
return size_;
}
uint8_t const* data_;
std::size_t size_;
};
struct internal_buffer_policy
{
internal_buffer_policy(std::size_t size)
: data_((size!=0) ? static_cast<uint8_t*>(::operator new(sizeof(uint8_t) * size)) : 0),
size_(size)
{}
uint8_t * data() const
{
return data_;
}
std::size_t size() const
{
return size_;
}
~internal_buffer_policy()
{
::operator delete(data_), data_=0;
}
uint8_t * data_;
std::size_t size_;
};
template <typename T>
class webp_reader : public image_reader
{
using buffer_policy_type = T;
private:
struct config_guard
{
config_guard(WebPDecoderConfig & config)
: config_(config) {}
~config_guard()
{
WebPFreeDecBuffer(&config_.output);
}
WebPDecoderConfig & config_;
};
std::unique_ptr<buffer_policy_type> buffer_;
size_t size_;
unsigned width_;
unsigned height_;
bool has_alpha_;
public:
explicit webp_reader(char const* data, std::size_t size);
explicit webp_reader(std::string const& filename);
~webp_reader();
unsigned width() const final;
unsigned height() const final;
boost::optional<box2d<double> > bounding_box() const final;
inline bool has_alpha() const final { return has_alpha_; }
void read(unsigned x,unsigned y,image_rgba8& image) final;
image_any read(unsigned x, unsigned y, unsigned width, unsigned height) final;
private:
void init();
};
namespace
{
image_reader* create_webp_reader(char const * data, std::size_t size)
{
return new webp_reader<external_buffer_policy>(data, size);
}
image_reader* create_webp_reader2(std::string const& filename)
{
return new webp_reader<internal_buffer_policy>(filename);
}
const bool registered = register_image_reader("webp", create_webp_reader);
const bool registered2 = register_image_reader("webp", create_webp_reader2);
}
// ctor
template <typename T>
webp_reader<T>::webp_reader(char const* data, std::size_t size)
: buffer_(new buffer_policy_type(reinterpret_cast<uint8_t const*>(data), size)),
width_(0),
height_(0),
has_alpha_(false)
{
init();
}
template <typename T>
webp_reader<T>::webp_reader(std::string const& filename)
: buffer_(nullptr),
size_(0),
width_(0),
height_(0),
has_alpha_(false)
{
std::ifstream file(filename.c_str(), std::ios::binary);
if (!file)
{
throw image_reader_exception("WEBP: Can't read file:" + filename);
}
std::streampos beg = file.tellg();
file.seekg (0, std::ios::end);
std::streampos end = file.tellg();
std::size_t file_size = end - beg;
file.seekg (0, std::ios::beg);
auto buffer = std::make_unique<buffer_policy_type>(file_size);
file.read(reinterpret_cast<char*>(buffer->data()), buffer->size());
if (!file)
{
throw image_reader_exception("WEBP: Failed to read:" + filename);
}
buffer_ = std::move(buffer);
init();
}
// dtor
template <typename T>
webp_reader<T>::~webp_reader()
{
//
}
template <typename T>
void webp_reader<T>::init()
{
WebPDecoderConfig config;
config_guard guard(config);
if (!WebPInitDecoderConfig(&config))
{
throw image_reader_exception("WEBP reader: WebPInitDecoderConfig failed");
}
if (WebPGetFeatures(buffer_->data(), buffer_->size(), &config.input) == VP8_STATUS_OK) {
width_ = config.input.width;
height_ = config.input.height;
has_alpha_ = config.input.has_alpha;
}
else
{
throw image_reader_exception("WEBP reader: WebPGetFeatures failed");
}
}
template <typename T>
unsigned webp_reader<T>::width() const
{
return width_;
}
template <typename T>
unsigned webp_reader<T>::height() const
{
return height_;
}
template <typename T>
boost::optional<box2d<double> > webp_reader<T>::bounding_box() const
{
return boost::optional<box2d<double> >();
}
template <typename T>
void webp_reader<T>::read(unsigned x0, unsigned y0,image_rgba8& image)
{
WebPDecoderConfig config;
config_guard guard(config);
if (!WebPInitDecoderConfig(&config))
{
throw image_reader_exception("WEBP reader: WebPInitDecoderConfig failed");
}
config.options.use_cropping = 1;
config.options.crop_left = x0;
config.options.crop_top = y0;
config.options.crop_width = std::min(static_cast<std::size_t>(width_ - x0), image.width());
config.options.crop_height = std::min(static_cast<std::size_t>(height_ - y0), image.height());
if (WebPGetFeatures(buffer_->data(), buffer_->size(), &config.input) != VP8_STATUS_OK)
{
throw image_reader_exception("WEBP reader: WebPGetFeatures failed");
}
config.output.colorspace = MODE_RGBA;
config.output.u.RGBA.rgba = reinterpret_cast<uint8_t *>(image.bytes());
config.output.u.RGBA.stride = 4 * image.width();
config.output.u.RGBA.size = image.width() * image.height() * 4;
config.output.is_external_memory = 1;
if (WebPDecode(buffer_->data(), buffer_->size(), &config) != VP8_STATUS_OK)
{
throw image_reader_exception("WEBP reader: WebPDecode failed");
}
}
template <typename T>
image_any webp_reader<T>::read(unsigned x, unsigned y, unsigned width, unsigned height)
{
image_rgba8 data(width,height);
read(x, y, data);
return image_any(std::move(data));
}
}