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
|
#include <mbgl/util/image.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/string.hpp>
#include <png.h>
#include <cassert>
#include <cstdlib>
#include <stdexcept>
#include <cstring>
#include <mbgl/platform/default/image_reader.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
// Check png library version.
const static bool png_version_check = []() {
const png_uint_32 version = png_access_version_number();
if (version != PNG_LIBPNG_VER) {
throw std::runtime_error(mbgl::util::sprintf<96>(
"libpng version mismatch: headers report %d.%d.%d, but library reports %d.%d.%d",
PNG_LIBPNG_VER / 10000, (PNG_LIBPNG_VER / 100) % 100, PNG_LIBPNG_VER % 100,
version / 10000, (version / 100) % 100, version % 100));
}
return true;
}();
#pragma GCC diagnostic pop
namespace mbgl {
namespace util {
std::string compress_png(size_t width, size_t height, const uint8_t* rgba) {
png_voidp error_ptr = 0;
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, error_ptr, NULL, NULL);
if (!png_ptr) {
Log::Error(Event::Image, "couldn't create png_ptr");
return "";
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!png_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp)0);
Log::Error(Event::Image, "couldn't create info_ptr");
return "";
}
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
jmp_buf *jmp_context = (jmp_buf *)png_get_error_ptr(png_ptr);
if (jmp_context) {
png_destroy_write_struct(&png_ptr, &info_ptr);
return "";
}
std::string result;
png_set_write_fn(png_ptr, &result, [](png_structp png_ptr_, png_bytep data, png_size_t length) {
std::string *out = static_cast<std::string *>(png_get_io_ptr(png_ptr_));
out->append(reinterpret_cast<char *>(data), length);
}, NULL);
struct ptrs {
ptrs(size_t count) : rows(new png_bytep[count]) {}
~ptrs() { delete[] rows; }
png_bytep *rows = nullptr;
} pointers(height);
for (size_t i = 0; i < height; i++) {
pointers.rows[i] = (png_bytep)((png_bytep)rgba + width * 4 * i);
}
png_set_rows(png_ptr, info_ptr, pointers.rows);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
return result;
}
Image::Image(std::string const& data)
{
try
{
auto reader = getImageReader(reinterpret_cast<const uint8_t*>(data.c_str()), data.size());
width = reader->width();
height = reader->height();
img = reader->read();
}
catch (ImageReaderException const& ex)
{
Log::Error(Event::Image, "%s", ex.what());
img.reset();
width = 0;
height = 0;
}
catch (...) // catch the rest
{
Log::Error(Event::Image, "exception in constructor");
img.reset();
width = 0;
height = 0;
}
}
}
}
|