1 #include <mbgl/util/image.hpp>
2 #include <mbgl/util/premultiply.hpp>
3 #include <mbgl/util/char_array_buffer.hpp>
4 #include <mbgl/util/logging.hpp>
5 
6 #include <istream>
7 #include <sstream>
8 
9 extern "C"
10 {
11 #include <png.h>
12 }
13 
14 template<size_t max, typename... Args>
sprintf(const char * msg,Args...args)15 static std::string sprintf(const char *msg, Args... args) {
16     char res[max];
17     int len = snprintf(res, sizeof(res), msg, args...);
18     return std::string(res, len);
19 }
20 
__anon9c775e750102() 21 const static bool png_version_check __attribute__((unused)) = []() {
22     const png_uint_32 version = png_access_version_number();
23     if (version != PNG_LIBPNG_VER) {
24         throw std::runtime_error(sprintf<96>(
25             "libpng version mismatch: headers report %d.%d.%d, but library reports %d.%d.%d",
26             PNG_LIBPNG_VER / 10000, (PNG_LIBPNG_VER / 100) % 100, PNG_LIBPNG_VER % 100,
27             version / 10000, (version / 100) % 100, version % 100));
28     }
29     return true;
30 }();
31 
32 namespace mbgl {
33 
user_error_fn(png_structp,png_const_charp error_msg)34 static void user_error_fn(png_structp, png_const_charp error_msg) {
35     throw std::runtime_error(std::string("failed to read invalid png: '") + error_msg + "'");
36 }
37 
user_warning_fn(png_structp,png_const_charp warning_msg)38 static void user_warning_fn(png_structp, png_const_charp warning_msg) {
39     Log::Warning(Event::Image, "ImageReader (PNG): %s", warning_msg);
40 }
41 
png_read_data(png_structp png_ptr,png_bytep data,png_size_t length)42 static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) {
43     auto* fin = reinterpret_cast<std::istream*>(png_get_io_ptr(png_ptr));
44     fin->read(reinterpret_cast<char*>(data), length);
45     std::streamsize read_count = fin->gcount();
46     if (read_count < 0 || static_cast<png_size_t>(read_count) != length)
47     {
48         png_error(png_ptr, "Read Error");
49     }
50 }
51 
52 struct png_struct_guard {
png_struct_guardmbgl::png_struct_guard53     png_struct_guard(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
54         : p_(png_ptr_ptr),
55           i_(info_ptr_ptr) {}
56 
~png_struct_guardmbgl::png_struct_guard57     ~png_struct_guard() {
58         png_destroy_read_struct(p_,i_,nullptr);
59     }
60 
61     png_structpp p_;
62     png_infopp i_;
63 };
64 
decodePNG(const uint8_t * data,size_t size)65 PremultipliedImage decodePNG(const uint8_t* data, size_t size) {
66     util::CharArrayBuffer dataBuffer { reinterpret_cast<const char*>(data), size };
67     std::istream stream(&dataBuffer);
68 
69     png_byte header[8] = { 0 };
70     stream.read(reinterpret_cast<char*>(header), 8);
71     if (stream.gcount() != 8)
72         throw std::runtime_error("PNG reader: Could not read image");
73 
74     int is_png = !png_sig_cmp(header, 0, 8);
75     if (!is_png)
76         throw std::runtime_error("File or stream is not a png");
77 
78     png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
79     if (!png_ptr)
80         throw std::runtime_error("failed to allocate png_ptr");
81 
82     // catch errors in a custom way to avoid the need for setjmp
83     png_set_error_fn(png_ptr, png_get_error_ptr(png_ptr), user_error_fn, user_warning_fn);
84 
85     png_infop info_ptr;
86     png_struct_guard sguard(&png_ptr, &info_ptr);
87     info_ptr = png_create_info_struct(png_ptr);
88     if (!info_ptr)
89         throw std::runtime_error("failed to create info_ptr");
90 
91     png_set_read_fn(png_ptr, &stream, png_read_data);
92     png_set_sig_bytes(png_ptr, 8);
93     png_read_info(png_ptr, info_ptr);
94 
95     png_uint_32 width = 0;
96     png_uint_32 height = 0;
97     int bit_depth = 0;
98     int color_type = 0;
99     png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, nullptr, nullptr, nullptr);
100 
101     UnassociatedImage image({ static_cast<uint32_t>(width), static_cast<uint32_t>(height) });
102 
103     if (color_type == PNG_COLOR_TYPE_PALETTE)
104         png_set_expand(png_ptr);
105 
106     if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
107         png_set_expand(png_ptr);
108 
109     if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
110         png_set_expand(png_ptr);
111 
112     if (bit_depth == 16)
113         png_set_strip_16(png_ptr);
114 
115     if (color_type == PNG_COLOR_TYPE_GRAY ||
116         color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
117         png_set_gray_to_rgb(png_ptr);
118 
119     png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
120 
121     if (png_get_interlace_type(png_ptr,info_ptr) == PNG_INTERLACE_ADAM7) {
122         png_set_interlace_handling(png_ptr); // FIXME: libpng bug?
123         // according to docs png_read_image
124         // "..automatically handles interlacing,
125         // so you don't need to call png_set_interlace_handling()"
126     }
127 
128     png_read_update_info(png_ptr, info_ptr);
129 
130     // we can read whole image at once
131     // alloc row pointers
132     const std::unique_ptr<png_bytep[]> rows(new png_bytep[height]);
133     for (unsigned row = 0; row < height; ++row)
134         rows[row] = image.data.get() + row * width * 4;
135     png_read_image(png_ptr, rows.get());
136 
137     png_read_end(png_ptr, nullptr);
138 
139     return util::premultiply(std::move(image));
140 }
141 
142 } // namespace mbgl
143