diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-10-31 18:09:50 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-10-31 18:09:50 +0100 |
commit | d11fe0c82c413c0bcb0b4f0ff464291aff4f36d4 (patch) | |
tree | 353dd36ccadae02399c15841eb493fe08d8c7eb3 /plugingui/image.cc | |
parent | 5d76d943eca9734f7df2dc351871815385c571b3 (diff) |
Refactored Image.
Diffstat (limited to 'plugingui/image.cc')
-rw-r--r-- | plugingui/image.cc | 113 |
1 files changed, 57 insertions, 56 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 <unistd.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <stdarg.h> -#include <stdint.h> +#include <cstring> +#include <cstdint> #include <hugin.hpp> #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:: |