From 6ff3ef55f8406d45d24cb5b280fc19b888c2138e Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 23 Nov 2015 16:35:32 -0800 Subject: [PATCH] [core] Remove unused --- .../mbgl/platform/default/image_reader.hpp | 4 +- include/mbgl/platform/default/jpeg_reader.hpp | 4 +- include/mbgl/platform/default/png_reader.hpp | 4 +- platform/default/image.cpp | 3 +- platform/default/jpeg_reader.cpp | 19 ++--- platform/default/png_reader.cpp | 71 ++++++++----------- 6 files changed, 42 insertions(+), 63 deletions(-) diff --git a/include/mbgl/platform/default/image_reader.hpp b/include/mbgl/platform/default/image_reader.hpp index 8e293d8be..e46bb08ea 100644 --- a/include/mbgl/platform/default/image_reader.hpp +++ b/include/mbgl/platform/default/image_reader.hpp @@ -29,9 +29,7 @@ struct ImageReader : private noncopyable { virtual unsigned width() const=0; virtual unsigned height() const=0; - virtual bool hasAlpha() const=0; - virtual bool premultipliedAlpha() const=0; - virtual void read(unsigned x, unsigned y, unsigned width, unsigned height, uint8_t* image)=0; + virtual std::unique_ptr read()=0; virtual ~ImageReader() {} }; diff --git a/include/mbgl/platform/default/jpeg_reader.hpp b/include/mbgl/platform/default/jpeg_reader.hpp index d87700535..2f988374c 100644 --- a/include/mbgl/platform/default/jpeg_reader.hpp +++ b/include/mbgl/platform/default/jpeg_reader.hpp @@ -55,9 +55,7 @@ public: ~JpegReader(); unsigned width() const; unsigned height() const; - inline bool hasAlpha() const { return false; } - inline bool premultipliedAlpha() const { return true; } - void read(unsigned x, unsigned y, unsigned w, unsigned h, uint8_t* image); + std::unique_ptr read(); private: void init(); static void on_error(j_common_ptr cinfo); diff --git a/include/mbgl/platform/default/png_reader.hpp b/include/mbgl/platform/default/png_reader.hpp index 9d6af91da..c869e78ea 100644 --- a/include/mbgl/platform/default/png_reader.hpp +++ b/include/mbgl/platform/default/png_reader.hpp @@ -53,9 +53,7 @@ public: ~PngReader(); unsigned width() const; unsigned height() const; - inline bool hasAlpha() const { return has_alpha_; } - bool premultipliedAlpha() const { return true; } // png_set_alpha_mode(png, PNG_ALPHA_PREMULTIPLIED, 2.2) - void read(unsigned x,unsigned y, unsigned width, unsigned height, uint8_t* image); + std::unique_ptr read(); private: void init(); static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length); diff --git a/platform/default/image.cpp b/platform/default/image.cpp index bbd5d02b6..4da607d97 100644 --- a/platform/default/image.cpp +++ b/platform/default/image.cpp @@ -81,8 +81,7 @@ Image::Image(std::string const& data) auto reader = getImageReader(reinterpret_cast(data.c_str()), data.size()); width = reader->width(); height = reader->height(); - img = std::make_unique(width * height * 4); - reader->read(0, 0, width, height, img.get()); + img = reader->read(); } catch (ImageReaderException const& ex) { diff --git a/platform/default/jpeg_reader.cpp b/platform/default/jpeg_reader.cpp index dac18f32d..a537eeeb0 100644 --- a/platform/default/jpeg_reader.cpp +++ b/platform/default/jpeg_reader.cpp @@ -145,8 +145,10 @@ unsigned JpegReader::height() const } template -void JpegReader::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8_t* image) +std::unique_ptr JpegReader::read() { + std::unique_ptr image = std::make_unique(width_ * height_ * 4); + stream_.clear(); stream_.seekg(0, std::ios_base::beg); @@ -167,19 +169,16 @@ void JpegReader::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8 row_stride = cinfo.output_width * cinfo.output_components; buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); - w = std::min(w,width_ - x0); - h = std::min(h,height_ - y0); - - const std::unique_ptr out_row(new unsigned int[w]); + const std::unique_ptr out_row(new unsigned int[width_]); unsigned row = 0; while (cinfo.output_scanline < cinfo.output_height) { jpeg_read_scanlines(&cinfo, buffer, 1); - if (row >= y0 && row < y0 + h) + if (row < height_) { - for (unsigned int x = 0; x < w; ++x) + for (unsigned int x = 0; x < width_; ++x) { - unsigned col = x + x0; + unsigned col = x; r = buffer[0][cinfo.output_components * col]; if (cinfo.output_components > 2) { @@ -191,11 +190,13 @@ void JpegReader::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8 } out_row[x] = (0xff << 24) | (b << 16) | (g << 8) | r; } - std::copy((char*)out_row.get(), (char*)out_row.get() + w*4, image + (row - y0)*width_*4); + std::copy((char*)out_row.get(), (char*)out_row.get() + width_*4, image.get() + row*width_*4); } ++row; } jpeg_finish_decompress(&cinfo); + + return image; } template class JpegReader; diff --git a/platform/default/png_reader.cpp b/platform/default/png_reader.cpp index 204c4874f..1f7207337 100644 --- a/platform/default/png_reader.cpp +++ b/platform/default/png_reader.cpp @@ -115,8 +115,10 @@ unsigned PngReader::height() const } template -void PngReader::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8_t* image) +std::unique_ptr PngReader::read() { + std::unique_ptr image = std::make_unique(width_ * height_ * 4); + stream_.clear(); stream_.seekg(0, std::ios_base::beg); @@ -162,54 +164,37 @@ void PngReader::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8_ png_set_alpha_mode(png_ptr, PNG_ALPHA_PREMULTIPLIED, PNG_GAMMA_LINEAR); #endif - if (x0 == 0 && y0 == 0 && w >= width_ && h >= height_) + if (png_get_interlace_type(png_ptr,info_ptr) == PNG_INTERLACE_ADAM7) { - 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_ * 4 ; - png_read_image(png_ptr, rows.get()); + 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.get() + row * width_ * 4 ; + png_read_image(png_ptr, rows.get()); #ifndef PNG_ALPHA_PREMULTIPLIED - // Manually premultiply the image if libpng didn't do it for us. - for (unsigned row = 0; row < height_; ++row) { - for (unsigned x = 0; x < width_; x++) { - png_byte* ptr = &(rows[row][x * 4]); - const float a = ptr[3] / 255.0f; - ptr[0] *= a; - ptr[1] *= a; - ptr[2] *= a; - } + // Manually premultiply the image if libpng didn't do it for us. + for (unsigned row = 0; row < height_; ++row) { + for (unsigned x = 0; x < width_; x++) { + png_byte* ptr = &(rows[row][x * 4]); + const float a = ptr[3] / 255.0f; + ptr[0] *= a; + ptr[1] *= a; + ptr[2] *= a; } + } #endif - } - else - { - png_read_update_info(png_ptr, info_ptr); - w=std::min(w, width_ - x0); - h=std::min(h, height_ - y0); - unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr); - const std::unique_ptr row(new png_byte[rowbytes]); - for (unsigned i = 0; i < height_; ++i) - { - png_read_row(png_ptr,row.get(),0); - if (i >= y0 && i < (y0 + h)) - { - // TODO: premultiply this - std::copy(&row[x0 * 4], &row[x0 * 4] + w * 4, image + i * width_* 4); - } - } - } + png_read_end(png_ptr,0); + + return image; } template class PngReader;