画像の簡易入出力とテストプログラムを追加
[cuda.git] / libutils / Image.h
1 /*
2         Copyright (C) 2012  fmaj7b5.info
3
4         This program is free software: you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation, either version 2 of the License, or
7         (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef FM7b5_IMAGE_H
19 #define FM7b5_IMAGE_H
20
21 #include <memory>
22 #include <cstdint>
23
24 //#include "MDView.h"
25
26 namespace FM7b5
27 {
28         template <class T>
29         class Image
30         {
31         public:
32                 typedef T pixel_type;
33                 typedef Image<T> image_type;
34                 typedef std::uint8_t raw_type;
35
36                 Image();
37                 Image(const size_t width, const size_t height, const size_t stride = 0);
38                 ~Image();
39
40                 T& operator()(const size_t x, const size_t y);
41                 const T& operator()(const size_t x, const size_t y) const;
42
43                 size_t width() const;
44                 size_t height() const;
45                 size_t bpp() const;
46                 size_t stride() const;
47                 size_t size() const;
48
49                 T* data();
50                 const T* data() const;
51
52         protected:
53                 size_t m_width;
54                 size_t m_height;
55                 size_t m_byte_per_pixel;
56                 size_t m_stride;
57                 size_t m_size;
58                 std::shared_ptr<raw_type> m_data;
59
60                 size_t align_up(const size_t size, const size_t alignment);
61         };
62
63
64         template <class T>
65         Image<T>::Image()
66                 : m_width(0), m_height(0),
67                   m_byte_per_pixel(sizeof(T)),
68                   m_stride(0), m_size(0), m_data(0)
69         {}
70
71         template <class T>
72         Image<T>::Image(const size_t width, const size_t height, const size_t stride)
73                 : m_width(width), m_height(height),
74                   m_byte_per_pixel(sizeof(T)),
75                   m_stride(stride == 0 ? sizeof(T) * width : stride),
76                   m_size(m_stride * height)
77         {
78                 m_data.reset(new raw_type[m_size]);
79         }
80
81         template <class T>
82         Image<T>::~Image()
83         {}
84
85         template <class T>
86         T&
87         Image<T>::operator()(const size_t x, const size_t y)
88         {
89                 return *(reinterpret_cast<T*>(m_data.get() + m_stride * y + m_byte_per_pixel * x));
90         }
91
92         template <class T>
93         const T&
94         Image<T>::operator()(const size_t x, const size_t y) const
95         {
96                 return *(reinterpret_cast<T*>(m_data.get() + m_stride * y + m_byte_per_pixel * x));
97         }
98
99         template <class T>
100         size_t
101         Image<T>::width() const
102         {
103                 return m_width;
104         }
105
106         template <class T>
107         size_t
108         Image<T>::height() const
109         {
110                 return m_height;
111         }
112
113         template <class T>
114         size_t
115         Image<T>::bpp() const
116         {
117                 return m_byte_per_pixel;
118         }
119
120         template <class T>
121         size_t
122         Image<T>::stride() const
123         {
124                 return m_stride;
125         }
126
127         template <class T>
128         size_t
129         Image<T>::size() const
130         {
131                 return m_size;
132         }
133
134         template <class T>
135         T*
136         Image<T>::data()
137         {
138                 return reinterpret_cast<T*>(m_data.get());
139         }
140
141         template <class T>
142         const T*
143         Image<T>::data() const
144         {
145                 return reinterpret_cast<const T*>(m_data.get());
146         }
147
148         template <class T>
149         size_t
150         Image<T>::align_up(const size_t size, const size_t alignment)
151         {
152                 return size + (alignment - (size % alignment));
153         }
154
155         /* typedefs for convinience */
156         typedef Image<std::uint8_t> ImageGray;
157 }
158
159 #endif /* FM7b5_IMAGE_H */