/* Copyright (C) 2012 fmaj7b5.info This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef FM7b5_IMAGE_H #define FM7b5_IMAGE_H #include #include //#include "MDView.h" namespace FM7b5 { template class Image { public: typedef T pixel_type; typedef Image image_type; typedef std::uint_fast8_t raw_type; Image(); Image(const size_t width, const size_t height, const size_t stride = 0); ~Image(); T& operator()(const size_t x, const size_t y); const T& operator()(const size_t x, const size_t y) const; size_t width() const; size_t height() const; size_t bpp() const; size_t stride() const; size_t size() const; T* data(); const T* data() const; protected: size_t m_width; size_t m_height; size_t m_byte_per_pixel; size_t m_stride; size_t m_size; std::shared_ptr m_data; size_t align_up(const size_t size, const size_t alignment); }; template Image::Image() : m_width(0), m_height(0), m_byte_per_pixel(sizeof(T)), m_stride(0), m_size(0), m_data(0) {} template Image::Image(const size_t width, const size_t height, const size_t stride) : m_width(width), m_height(height), m_byte_per_pixel(sizeof(T)), m_stride(stride == 0 ? sizeof(T) * width : stride), m_size(m_stride * height) { m_data.reset(new raw_type[m_size]); } template Image::~Image() {} template T& Image::operator()(const size_t x, const size_t y) { return *(reinterpret_cast(m_data.get() + m_stride * y + m_byte_per_pixel * x)); } template const T& Image::operator()(const size_t x, const size_t y) const { return *(reinterpret_cast(m_data.get() + m_stride * y + m_byte_per_pixel * x)); } template size_t Image::width() const { return m_width; } template size_t Image::height() const { return m_height; } template size_t Image::bpp() const { return m_byte_per_pixel; } template size_t Image::stride() const { return m_stride; } template size_t Image::size() const { return m_size; } template T* Image::data() { return reinterpret_cast(m_data.get()); } template const T* Image::data() const { return reinterpret_cast(m_data.get()); } template size_t Image::align_up(const size_t size, const size_t alignment) { return size + (alignment - (size % alignment)); } /* typedefs for convinience */ typedef Image ImageGray; } #endif /* FM7b5_IMAGE_H */