From d11fe0c82c413c0bcb0b4f0ff464291aff4f36d4 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 31 Oct 2015 18:09:50 +0100 Subject: Refactored Image. --- plugingui/image.cc | 113 +++++++++++++++++++++++++++-------------------------- plugingui/image.h | 27 ++++++------- 2 files changed, 69 insertions(+), 71 deletions(-) diff --git a/plugingui/image.cc b/plugingui/image.cc index 65b8242..fd79707 100644 --- a/plugingui/image.cc +++ b/plugingui/image.cc @@ -26,94 +26,95 @@ */ #include "image.h" -#include -#include -#include -#include -#include -#include +#include +#include #include #include "resource.h" -// http://blog.hammerian.net/2009/reading-png-images-from-memory/ - #include "lodepng/lodepng.h" -GUI::Image::Image(const char* data, size_t size) +namespace GUI { + +Image::Image(const char* data, size_t size) { - load(data, size); + load(data, size); } -GUI::Image::Image(std::string filename) +Image::Image(const std::string& filename) { - GUI::Resource rc(filename); - load(rc.data(), rc.size()); + Resource rc(filename); + load(rc.data(), rc.size()); } -GUI::Image::~Image() +Image::~Image() { - free(image_data); + delete[] image_data; } -void GUI::Image::setError(int err) +void Image::setError(int err) { - GUI::Resource rc(":png_error"); + Resource rc(":png_error"); - const unsigned char *p = (const unsigned char *)rc.data(); + const unsigned char* p = (const unsigned char*)rc.data(); - uint32_t iw, ih; + std::uint32_t iw, ih; - memcpy(&iw, p, sizeof(uint32_t)); p += sizeof(uint32_t); - memcpy(&ih, p, sizeof(uint32_t)); p += sizeof(uint32_t); + std::memcpy(&iw, p, sizeof(uint32_t)); + p += sizeof(uint32_t); - w = iw; - h = ih; + std::memcpy(&ih, p, sizeof(uint32_t)); + p += sizeof(uint32_t); - DEBUG(image, "w:%d, h:%d\n", (int)w, (int)h); + _width = iw; + _height = ih; - image_data = (unsigned char*)malloc(rc.size() - 8); - memcpy(image_data, p, rc.size() - 8); + size_t image_size = rc.size() - (sizeof(iw) + sizeof(ih)); + image_data = new unsigned char[image_size]; + memcpy(image_data, p, image_size); } -void GUI::Image::load(const char* data, size_t size) +void Image::load(const char* data, size_t size) { - //unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, - // const unsigned char* in, size_t insize); - unsigned iw, ih; - unsigned res = lodepng_decode32((unsigned char**)&image_data, &iw, &ih, - (const unsigned char*)data, size); - w = iw; - h = ih; - - if(res != 0) { - ERR(image, "[read_png_file] Error during init_io"); - setError(3); - return; - } + unsigned int res = lodepng_decode32((unsigned char**)&image_data, + &_width, &_height, + (const unsigned char*)data, size); + + if(res != 0) + { + ERR(image, "[read_png_file] Error during init_io"); + setError(3); + return; + } } -size_t GUI::Image::width() +size_t Image::width() { - return w; + return _width; } -size_t GUI::Image::height() +size_t Image::height() { - return h; + return _height; } -GUI::Colour GUI::Image::getPixel(size_t x, size_t y) +Colour Image::getPixel(size_t x, size_t y) { - if(x > width() || y > height()) return GUI::Colour(0,0,0,0); - unsigned char *ptr = &image_data[(x + y * width()) * 4]; - float r = ptr[0]; - float g = ptr[1]; - float b = ptr[2]; - float a = ptr[3]; - GUI::Colour c(r / 255.0, - g / 255.0, - b / 255.0, - a / 255.0); - return c; + if(x > _width || y > _height) + { + return Colour(0,0,0,0); + } + + unsigned char* ptr = &image_data[(x + y * width()) * 4]; + + float r = ptr[0]; + float g = ptr[1]; + float b = ptr[2]; + float a = ptr[3]; + + Colour c(r / 255.0, g / 255.0, b / 255.0, a / 255.0); + + return c; } + +} // GUI:: diff --git a/plugingui/image.h b/plugingui/image.h index cec1518..1a6e40a 100644 --- a/plugingui/image.h +++ b/plugingui/image.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_IMAGE_H__ -#define __DRUMGIZMO_IMAGE_H__ +#pragma once #include @@ -36,24 +35,22 @@ namespace GUI { class Image { public: - Image(const char* data, size_t size); - Image(std::string filename); - ~Image(); + Image(const char* data, size_t size); + Image(const std::string& filename); + ~Image(); - size_t width(); - size_t height(); + size_t width(); + size_t height(); - Colour getPixel(size_t x, size_t y); + Colour getPixel(size_t x, size_t y); private: - void setError(int err); + void setError(int err); - void load(const char* data, size_t size); + void load(const char* data, size_t size); - size_t w, h; - unsigned char *image_data; + size_t _width, _height; + unsigned char* image_data; }; -}; - -#endif/*__DRUMGIZMO_IMAGE_H__*/ +} // GUI:: -- cgit v1.2.3