画像の簡易入出力とテストプログラムを追加
[cuda.git] / libutils / Image.h
diff --git a/libutils/Image.h b/libutils/Image.h
new file mode 100644 (file)
index 0000000..4e60da0
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+       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 <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef FM7b5_IMAGE_H
+#define FM7b5_IMAGE_H
+
+#include <memory>
+#include <cstdint>
+
+//#include "MDView.h"
+
+namespace FM7b5
+{
+       template <class T>
+       class Image
+       {
+       public:
+               typedef T pixel_type;
+               typedef Image<T> image_type;
+               typedef std::uint8_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<raw_type> m_data;
+
+               size_t align_up(const size_t size, const size_t alignment);
+       };
+
+
+       template <class T>
+       Image<T>::Image()
+               : m_width(0), m_height(0),
+                 m_byte_per_pixel(sizeof(T)),
+                 m_stride(0), m_size(0), m_data(0)
+       {}
+
+       template <class T>
+       Image<T>::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 <class T>
+       Image<T>::~Image()
+       {}
+
+       template <class T>
+       T&
+       Image<T>::operator()(const size_t x, const size_t y)
+       {
+               return *(reinterpret_cast<T*>(m_data.get() + m_stride * y + m_byte_per_pixel * x));
+       }
+
+       template <class T>
+       const T&
+       Image<T>::operator()(const size_t x, const size_t y) const
+       {
+               return *(reinterpret_cast<T*>(m_data.get() + m_stride * y + m_byte_per_pixel * x));
+       }
+
+       template <class T>
+       size_t
+       Image<T>::width() const
+       {
+               return m_width;
+       }
+
+       template <class T>
+       size_t
+       Image<T>::height() const
+       {
+               return m_height;
+       }
+
+       template <class T>
+       size_t
+       Image<T>::bpp() const
+       {
+               return m_byte_per_pixel;
+       }
+
+       template <class T>
+       size_t
+       Image<T>::stride() const
+       {
+               return m_stride;
+       }
+
+       template <class T>
+       size_t
+       Image<T>::size() const
+       {
+               return m_size;
+       }
+
+       template <class T>
+       T*
+       Image<T>::data()
+       {
+               return reinterpret_cast<T*>(m_data.get());
+       }
+
+       template <class T>
+       const T*
+       Image<T>::data() const
+       {
+               return reinterpret_cast<const T*>(m_data.get());
+       }
+
+       template <class T>
+       size_t
+       Image<T>::align_up(const size_t size, const size_t alignment)
+       {
+               return size + (alignment - (size % alignment));
+       }
+
+       /* typedefs for convinience */
+       typedef Image<std::uint8_t> ImageGray;
+}
+
+#endif /* FM7b5_IMAGE_H */
\ No newline at end of file