From ae2d4e740ee1f7f7dd674b4c6b1ccc697500a166 Mon Sep 17 00:00:00 2001 From: artemp Date: Fri, 7 Nov 2014 11:49:31 +0000 Subject: add image_reader abstraction layer to hanfle multiple image formats (work-in-progress) --- platform/default/image.cpp | 128 ++--------------- platform/default/image_reader.cpp | 67 +++++++++ platform/default/jpeg_reader.cpp | 291 ++++++++++++++++++++++++++++++++++++++ platform/default/png_reader.cpp | 279 ++++++++++++++++++++++++++++++++++++ 4 files changed, 652 insertions(+), 113 deletions(-) create mode 100644 platform/default/image_reader.cpp create mode 100644 platform/default/jpeg_reader.cpp create mode 100644 platform/default/png_reader.cpp (limited to 'platform') diff --git a/platform/default/image.cpp b/platform/default/image.cpp index eb4b3e8f55..bc9e720069 100644 --- a/platform/default/image.cpp +++ b/platform/default/image.cpp @@ -8,7 +8,7 @@ #include #include #include - +#include // Check png library version. const static bool png_version_check = []() { @@ -73,121 +73,23 @@ std::string compress_png(int width, int height, void *rgba) { return result; } - -struct Buffer { - Buffer(const std::string& data_) - : data(data_.data()), length(data_.size()) {} - const char *const data = 0; - const size_t length = 0; - size_t pos = 0; -}; - -void readCallback(png_structp png, png_bytep data, png_size_t length) { - Buffer *reader = static_cast(png_get_io_ptr(png)); - - // Read `length` bytes into `data`. - if (reader->pos + length > reader->length) { - png_error(png, "Read Error"); - } else { - std::memcpy(data, reader->data + reader->pos, length); - reader->pos += length; - } -} - -void errorHandler(png_structp, png_const_charp error_msg) { - Log::Error(Event::Image, "PNG: %s", error_msg); - throw std::runtime_error(error_msg); -} - -void warningHandler(png_structp, png_const_charp error_msg) { - Log::Warning(Event::General, "PNG: %s", error_msg); -} - -Image::Image(const std::string &data) { - Buffer buffer(data); - - if (buffer.length < 8 || !png_check_sig((const png_bytep)buffer.data, 8)) { - Log::Error(Event::Image, "image is not a valid PNG image"); - return; - } - - png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, errorHandler, warningHandler); - assert(png); - - png_infop info = png_create_info_struct(png); - assert(info); - - int depth, color, interlace; - - try { - png_set_read_fn(png, (png_voidp)&buffer, readCallback); - png_read_info(png, info); - png_get_IHDR(png, info, (png_uint_32*)&width, (png_uint_32*)&height, &depth, &color, &interlace, nullptr, nullptr); - bool alpha = (color & PNG_COLOR_MASK_ALPHA) || png_get_valid(png, info, PNG_INFO_tRNS); - - // From http://trac.mapnik.org/browser/trunk/src/png_reader.cpp - if (color == PNG_COLOR_TYPE_PALETTE) - png_set_expand(png); - if (color == PNG_COLOR_TYPE_GRAY) - png_set_expand(png); - if (png_get_valid(png, info, PNG_INFO_tRNS)) - png_set_expand(png); - if (depth == 16) - png_set_strip_16(png); - if (depth < 8) - png_set_packing(png); - if (color == PNG_COLOR_TYPE_GRAY || - color == PNG_COLOR_TYPE_GRAY_ALPHA) - png_set_gray_to_rgb(png); - - if (interlace == PNG_INTERLACE_ADAM7) - png_set_interlace_handling(png); - - // Always add an alpha channel. - if (!alpha) { - png_set_add_alpha(png, 0xFF, PNG_FILLER_AFTER); - } - - // FIXME work around gamma crash - double gamma; - if (png_get_gAMA(png, info, &gamma)) - png_set_gamma(png, 2.2, gamma); - - png_set_alpha_mode(png, PNG_ALPHA_PREMULTIPLIED, 2.2); - - png_read_update_info(png, info); - - png_size_t rowbytes = png_get_rowbytes(png, info); - assert(width * 4 == rowbytes); - +Image::Image(std::string const& data) +{ + try + { + std::unique_ptr reader(get_image_reader(data.c_str(), data.size())); + width = reader->width(); + height = reader->height(); img = ::std::unique_ptr(new char[width * height * 4]()); - - char *surface = img.get(); - assert(surface); - - struct ptrs { - ptrs(size_t count) : rows(new png_bytep[count]) {} - ~ptrs() { delete[] rows; } - png_bytep *rows = nullptr; - } pointers(height); - for (unsigned i = 0; i < height; ++i) { - pointers.rows[i] = (png_bytep)(surface + (i * rowbytes)); - } - - // Read image data - png_read_image(png, pointers.rows); - - png_read_end(png, nullptr); - - png_destroy_read_struct(&png, &info, nullptr); - } catch (std::exception& e) { - Log::Error(Event::Image, "loading PNG failed: %s", e.what()); - png_destroy_read_struct(&png, &info, nullptr); - if (img) { - img.reset(); - } + reader->read(0, 0, width, height, img.get()); + } + catch (image_reader_exception const& ex) + { + fprintf(stderr, "ImageReader: %s\n", ex.what()); + img.reset(); width = 0; height = 0; + } } diff --git a/platform/default/image_reader.cpp b/platform/default/image_reader.cpp new file mode 100644 index 0000000000..75d7b0c139 --- /dev/null +++ b/platform/default/image_reader.cpp @@ -0,0 +1,67 @@ +#include "mbgl/util/image_reader.hpp" +#include + +namespace mbgl { namespace util { + +inline boost::optional type_from_bytes(char const* data, size_t size) +{ + using result_type = boost::optional; + if (size >= 4) + { + unsigned int magic = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + if (magic == 0x89504E47U) + { + return result_type("png"); + } + else if (magic == 0x49492A00 || magic == 0x4D4D002A) + { + return result_type("tiff"); + } + } + if (size>=2) + { + unsigned int magic = ((data[0] << 8) | data[1]) & 0xffff; + if (magic == 0xffd8) + { + return result_type("jpeg"); + } + } + + if (size>=12) + { + if (data[0] == 'R' && data[1] == 'I' && data[2] == 'F' && data[3] == 'F' && + data[8] == 'W' && data[9] == 'E' && data[10] == 'B' && data[11] == 'P') + { + return result_type("webp"); + } + } + return result_type(); +} + +image_reader* get_image_reader(char const* data, size_t size) +{ + boost::optional type = type_from_bytes(data,size); + if (type) + { + return factory::instance().create_object(*type, data,size); + } + else + throw image_reader_exception("image_reader: can't determine type from input data"); +} + +//image_reader* get_image_reader(std::string const& filename,std::string const& type) +//{ +// return factory::instance().create_object(type,filename); +//} + +//image_reader* get_image_reader(std::string const& filename) +//{ + //boost::optional type = type_from_filename(filename); + //if (type) + //{ + // return factory::instance().create_object(*type,filename); + //} +// return 0; +//} + +}} diff --git a/platform/default/jpeg_reader.cpp b/platform/default/jpeg_reader.cpp new file mode 100644 index 0000000000..de93cfad25 --- /dev/null +++ b/platform/default/jpeg_reader.cpp @@ -0,0 +1,291 @@ +#include "mbgl/util/image_reader.hpp" +//#include + +// jpeg +extern "C" +{ +#include +} + +// boost +#pragma GCC diagnostic push +//#pragma GCC diagnostic ignored "-Wunused-local-typedef" +#include +#include +#include +#pragma GCC diagnostic pop + +// std +#include +#include + +namespace mbgl { namespace util { + +template +class jpeg_reader : public image_reader +{ +public: + using source_type = T; + using input_stream = boost::iostreams::stream; + const static unsigned BUF_SIZE = 4096; +private: + struct jpeg_stream_wrapper + { + jpeg_source_mgr manager; + input_stream * stream; + JOCTET buffer[BUF_SIZE]; + }; + + struct jpeg_info_guard + { + jpeg_info_guard(jpeg_decompress_struct * cinfo) + : i_(cinfo) {} + + ~jpeg_info_guard() + { + jpeg_destroy_decompress(i_); + } + jpeg_decompress_struct * i_; + }; + +private: + source_type source_; + input_stream stream_; + unsigned width_; + unsigned height_; +public: + explicit jpeg_reader(std::string const& file_name); + explicit jpeg_reader(char const* data, size_t size); + ~jpeg_reader(); + unsigned width() const; + unsigned height() const; + inline bool has_alpha() const { return false; } + inline bool premultiplied_alpha() const { return true; } + void read(unsigned x,unsigned y, unsigned width, unsigned height, char *image); +private: + void init(); + static void on_error(j_common_ptr cinfo); + static void on_error_message(j_common_ptr cinfo); + static void init_source(j_decompress_ptr cinfo); + static boolean fill_input_buffer(j_decompress_ptr cinfo); + static void skip(j_decompress_ptr cinfo, long count); + static void term(j_decompress_ptr cinfo); + static void attach_stream(j_decompress_ptr cinfo, input_stream* in); +}; + +namespace +{ +image_reader* create_jpeg_reader(std::string const& file) +{ + return new jpeg_reader(file); +} + +image_reader* create_jpeg_reader2(char const* data, size_t size) +{ + return new jpeg_reader(data, size); +} + +const bool registered = register_image_reader("jpeg",create_jpeg_reader); +const bool registered2 = register_image_reader("jpeg",create_jpeg_reader2); +} + +// ctors +template +jpeg_reader::jpeg_reader(std::string const& file_name) + : source_(file_name,std::ios_base::in | std::ios_base::binary), + stream_(source_), + width_(0), + height_(0) +{ + if (!stream_) throw image_reader_exception("cannot open image file "+ file_name); + init(); +} + +template +jpeg_reader::jpeg_reader(char const* data, size_t size) + : source_(data, size), + stream_(source_), + width_(0), + height_(0) +{ + if (!stream_) throw image_reader_exception("cannot open image stream"); + init(); +} + +// dtor +template +jpeg_reader::~jpeg_reader() {} + +// jpeg stream wrapper +template +void jpeg_reader::init_source (j_decompress_ptr cinfo) +{ + jpeg_stream_wrapper* wrap = reinterpret_cast(cinfo->src); + wrap->stream->seekg(0,std::ios_base::beg); +} + +template +boolean jpeg_reader::fill_input_buffer (j_decompress_ptr cinfo) +{ + jpeg_stream_wrapper* wrap = reinterpret_cast(cinfo->src); + wrap->stream->read(reinterpret_cast(&wrap->buffer[0]),BUF_SIZE); + std::streamsize size = wrap->stream->gcount(); + wrap->manager.next_input_byte = wrap->buffer; + wrap->manager.bytes_in_buffer = BUF_SIZE; + return (size > 0) ? TRUE : FALSE; +} + +template +void jpeg_reader::skip(j_decompress_ptr cinfo, long count) +{ + if (count <= 0) return; //A zero or negative skip count should be treated as a no-op. + jpeg_stream_wrapper* wrap = reinterpret_cast(cinfo->src); + + if (wrap->manager.bytes_in_buffer > 0 && count < static_cast(wrap->manager.bytes_in_buffer)) + { + wrap->manager.bytes_in_buffer -= count; + wrap->manager.next_input_byte = &wrap->buffer[BUF_SIZE - wrap->manager.bytes_in_buffer]; + } + else + { + wrap->stream->seekg(count - wrap->manager.bytes_in_buffer, std::ios_base::cur); + // trigger buffer fill + wrap->manager.next_input_byte = 0; + wrap->manager.bytes_in_buffer = 0; //bytes_in_buffer may be zero on return. + } +} + +template +void jpeg_reader::term (j_decompress_ptr /*cinfo*/) +{ +// no-op +} + +template +void jpeg_reader::attach_stream (j_decompress_ptr cinfo, input_stream* in) +{ + if (cinfo->src == 0) + { + cinfo->src = (struct jpeg_source_mgr *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(jpeg_stream_wrapper)); + } + jpeg_reader::jpeg_stream_wrapper * src = reinterpret_cast (cinfo->src); + src->manager.init_source = init_source; + src->manager.fill_input_buffer = fill_input_buffer; + src->manager.skip_input_data = skip; + src->manager.resync_to_restart = jpeg_resync_to_restart; + src->manager.term_source = term; + src->manager.bytes_in_buffer = 0; + src->manager.next_input_byte = 0; + src->stream = in; +} + +template +void jpeg_reader::on_error(j_common_ptr /*cinfo*/) +{ +} + +template +void jpeg_reader::on_error_message(j_common_ptr cinfo) +{ + char buffer[JMSG_LENGTH_MAX]; + (*cinfo->err->format_message)(cinfo, buffer); + throw image_reader_exception(std::string("JPEG Reader: libjpeg could not read image: ") + buffer); +} + +template +void jpeg_reader::init() +{ + jpeg_decompress_struct cinfo; + jpeg_info_guard iguard(&cinfo); + jpeg_error_mgr jerr; + cinfo.err = jpeg_std_error(&jerr); + jerr.error_exit = on_error; + jerr.output_message = on_error_message; + jpeg_create_decompress(&cinfo); + attach_stream(&cinfo, &stream_); + int ret = jpeg_read_header(&cinfo, TRUE); + if (ret != JPEG_HEADER_OK) + throw image_reader_exception("JPEG Reader: failed to read header"); + jpeg_start_decompress(&cinfo); + width_ = cinfo.output_width; + height_ = cinfo.output_height; + + if (cinfo.out_color_space == JCS_UNKNOWN) + { + throw image_reader_exception("JPEG Reader: failed to read unknown color space"); + } + if (cinfo.output_width == 0 || cinfo.output_height == 0) + { + throw image_reader_exception("JPEG Reader: failed to read image size of"); + } +} + +template +unsigned jpeg_reader::width() const +{ + return width_; +} + +template +unsigned jpeg_reader::height() const +{ + return height_; +} + +template +void jpeg_reader::read(unsigned x0, unsigned y0, unsigned width, unsigned height, char* image) +{ + stream_.clear(); + stream_.seekg(0, std::ios_base::beg); + + jpeg_decompress_struct cinfo; + jpeg_info_guard iguard(&cinfo); + jpeg_error_mgr jerr; + cinfo.err = jpeg_std_error(&jerr); + jerr.error_exit = on_error; + jerr.output_message = on_error_message; + jpeg_create_decompress(&cinfo); + attach_stream(&cinfo, &stream_); + int ret = jpeg_read_header(&cinfo, TRUE); + if (ret != JPEG_HEADER_OK) throw image_reader_exception("JPEG Reader read(): failed to read header"); + jpeg_start_decompress(&cinfo); + JSAMPARRAY buffer; + int row_stride; + unsigned char r,g,b; + row_stride = cinfo.output_width * cinfo.output_components; + buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); + + unsigned w = std::min(width,width_ - x0); + unsigned h = std::min(height,height_ - y0); + + const std::unique_ptr out_row(new unsigned int[w]); + unsigned row = 0; + while (cinfo.output_scanline < cinfo.output_height) + { + jpeg_read_scanlines(&cinfo, buffer, 1); + if (row >= y0 && row < y0 + h) + { + for (unsigned int x = 0; x < w; ++x) + { + unsigned col = x + x0; + r = buffer[0][cinfo.output_components * col]; + if (cinfo.output_components > 2) + { + g = buffer[0][cinfo.output_components * col + 1]; + b = buffer[0][cinfo.output_components * col + 2]; + } else { + g = r; + b = r; + } + out_row[x] = (0xff << 24) | (b << 16) | (g << 8) | r;//color(r, g, b, a).rgba(); + } + //image.setRow(row - y0, out_row.get(), w); + std::copy((char*)out_row.get(), (char*)out_row.get() + w*4, image + (row - y0)*width_*4); + } + ++row; + } + jpeg_finish_decompress(&cinfo); +} + +}} diff --git a/platform/default/png_reader.cpp b/platform/default/png_reader.cpp new file mode 100644 index 0000000000..1780ecf23c --- /dev/null +++ b/platform/default/png_reader.cpp @@ -0,0 +1,279 @@ +#include "mbgl/util/image_reader.hpp" +#include +extern "C" +{ +#include +} +// boost +#pragma GCC diagnostic push +//#pragma GCC diagnostic ignored "-Wunused-local-typedef" +#include +#include +#include +#pragma GCC diagnostic pop + +// stl +#include +#include + +namespace mbgl { namespace util { + +template +class png_reader : public image_reader +{ + using source_type = T; + using input_stream = boost::iostreams::stream; + + struct png_struct_guard + { + png_struct_guard(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) + : p_(png_ptr_ptr), + i_(info_ptr_ptr) {} + + ~png_struct_guard() + { + png_destroy_read_struct(p_,i_,0); + } + png_structpp p_; + png_infopp i_; + }; + +private: + + source_type source_; + input_stream stream_; + unsigned width_; + unsigned height_; + int bit_depth_; + int color_type_; + bool has_alpha_; +public: + explicit png_reader(std::string const& file_name); + png_reader(char const* data, std::size_t size); + ~png_reader(); + unsigned width() const; + unsigned height() const; + inline bool has_alpha() const { return has_alpha_; } + bool premultiplied_alpha() const { return false; } //http://www.libpng.org/pub/png/spec/1.1/PNG-Rationale.html + void read(unsigned x,unsigned y, unsigned width, unsigned height, char * image); +private: + void init(); + static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length); +}; + +namespace +{ + +image_reader* create_png_reader(std::string const& file) +{ + return new png_reader(file); +} + +image_reader* create_png_reader2(char const * data, std::size_t size) +{ + return new png_reader(data, size); +} + +const bool registered = register_image_reader("png",create_png_reader); +const bool registered2 = register_image_reader("png", create_png_reader2); +} + + +void user_error_fn(png_structp /*png_ptr*/, png_const_charp error_msg) +{ + throw image_reader_exception(std::string("failed to read invalid png: '") + error_msg + "'"); +} + +void user_warning_fn(png_structp /*png_ptr*/, png_const_charp warning_msg) +{ + fprintf(stderr, "ImageReader (PNG): %s\n", warning_msg); + +} + +template +void png_reader::png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) +{ + input_stream * fin = reinterpret_cast(png_get_io_ptr(png_ptr)); + fin->read(reinterpret_cast(data), length); + std::streamsize read_count = fin->gcount(); + if (read_count < 0 || static_cast(read_count) != length) + { + png_error(png_ptr, "Read Error"); + } +} + +template +png_reader::png_reader(std::string const& file_name) + : source_(file_name,std::ios_base::in | std::ios_base::binary), + stream_(source_), + width_(0), + height_(0), + bit_depth_(0), + color_type_(0), + has_alpha_(false) +{ + if (!source_.is_open()) throw image_reader_exception("PNG reader: cannot open file '"+ file_name + "'"); + if (!stream_) throw image_reader_exception("PNG reader: cannot open file '"+ file_name + "'"); + init(); +} + +template +png_reader::png_reader(char const* data, std::size_t size) + : source_(data,size), + stream_(source_), + width_(0), + height_(0), + bit_depth_(0), + color_type_(0), + has_alpha_(false) +{ + + if (!stream_) throw image_reader_exception("PNG reader: cannot open image stream"); + init(); +} + + +template +png_reader::~png_reader() {} + + +template +void png_reader::init() +{ + png_byte header[8]; + std::memset(header,0,8); + stream_.read(reinterpret_cast(header),8); + if ( stream_.gcount() != 8) + { + throw image_reader_exception("PNG reader: Could not read image"); + } + int is_png=!png_sig_cmp(header,0,8); + if (!is_png) + { + throw image_reader_exception("File or stream is not a png"); + } + png_structp png_ptr = png_create_read_struct + (PNG_LIBPNG_VER_STRING,0,0,0); + + if (!png_ptr) + { + throw image_reader_exception("failed to allocate png_ptr"); + } + + // catch errors in a custom way to avoid the need for setjmp + png_set_error_fn(png_ptr, png_get_error_ptr(png_ptr), user_error_fn, user_warning_fn); + + png_infop info_ptr; + png_struct_guard sguard(&png_ptr,&info_ptr); + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) throw image_reader_exception("failed to create info_ptr"); + + png_set_read_fn(png_ptr, (png_voidp)&stream_, png_read_data); + + png_set_sig_bytes(png_ptr,8); + png_read_info(png_ptr, info_ptr); + + png_uint_32 width, height; + png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth_, &color_type_,0,0,0); + has_alpha_ = (color_type_ & PNG_COLOR_MASK_ALPHA) || png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS); + width_=width; + height_=height; + + //MAPNIK_LOG_DEBUG(png_reader) << "png_reader: bit_depth=" << bit_depth_ << ",color_type=" << color_type_; +} + +template +unsigned png_reader::width() const +{ + return width_; +} + +template +unsigned png_reader::height() const +{ + return height_; +} + +template +void png_reader::read(unsigned x0, unsigned y0, unsigned width, unsigned height, char * image) +{ + stream_.clear(); + stream_.seekg(0, std::ios_base::beg); + + png_structp png_ptr = png_create_read_struct + (PNG_LIBPNG_VER_STRING,0,0,0); + + if (!png_ptr) + { + throw image_reader_exception("failed to allocate png_ptr"); + } + + // catch errors in a custom way to avoid the need for setjmp + png_set_error_fn(png_ptr, png_get_error_ptr(png_ptr), user_error_fn, user_warning_fn); + + png_infop info_ptr; + png_struct_guard sguard(&png_ptr,&info_ptr); + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) throw image_reader_exception("failed to create info_ptr"); + + png_set_read_fn(png_ptr, (png_voidp)&stream_, png_read_data); + png_read_info(png_ptr, info_ptr); + + if (color_type_ == PNG_COLOR_TYPE_PALETTE) + png_set_expand(png_ptr); + if (color_type_ == PNG_COLOR_TYPE_GRAY && bit_depth_ < 8) + png_set_expand(png_ptr); + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) + png_set_expand(png_ptr); + if (bit_depth_ == 16) + png_set_strip_16(png_ptr); + if (color_type_ == PNG_COLOR_TYPE_GRAY || + color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA) + png_set_gray_to_rgb(png_ptr); + + // quick hack -- only work in >=libpng 1.2.7 + png_set_add_alpha(png_ptr,0xff,PNG_FILLER_AFTER); //rgba + + double gamma; + if (png_get_gAMA(png_ptr, info_ptr, &gamma)) + png_set_gamma(png_ptr, 2.2, gamma); + + if (x0 == 0 && y0 == 0 && width >= width_ && height >= height_) + { + if (png_get_interlace_type(png_ptr,info_ptr) == PNG_INTERLACE_ADAM7) + { + png_set_interlace_handling(png_ptr); // FIXME: libpng bug? + // according to docs png_read_image + // "..automatically handles interlacing, + // so you don't need to call png_set_interlace_handling()" + } + png_read_update_info(png_ptr, info_ptr); + // we can read whole image at once + // alloc row pointers + const std::unique_ptr rows(new png_bytep[height_]); + for (unsigned row = 0; row < height_; ++row) + rows[row] = (png_bytep)image + row * width_ ; + png_read_image(png_ptr, rows.get()); + } + else + { + png_read_update_info(png_ptr, info_ptr); + //unsigned w=std::min(width, width_ - x0); + unsigned h=std::min(height, height_ - y0); + unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr); + const std::unique_ptr row(new png_byte[rowbytes]); + //START read image rows + for (unsigned i = 0; i < height_; ++i) + { + png_read_row(png_ptr,row.get(),0); + if (i >= y0 && i < (y0 + h)) + { +////image.setRow(i-y0,reinterpret_cast(&row[x0 * 4]),w); + //std::copy(image, buf + size, pData_ + i * width); + } + } + //END + } + png_read_end(png_ptr,0); +} +}} -- cgit v1.2.1