erebus
view liberebus/src/image.inl @ 0:4abdce1361b9
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 27 Apr 2014 16:02:47 +0300 |
parents | |
children | 474a0244f57d |
line source
1 #include <string.h>
2 #include "imago2.h"
3 #include "image.h"
5 template <typename T>
6 Image<T>::Image()
7 {
8 pixels = 0;
9 own_pixels = true;
10 width = height = 0;
11 }
13 template <typename T>
14 Image<T>::~Image()
15 {
16 destroy();
17 }
19 template <typename T>
20 Image<T>::Image(const Image<T> &img)
21 {
22 pixels = 0;
23 own_pixels = false;
25 create(img.width, img.height, img.pixels);
26 }
28 template <typename T>
29 Image<T> &Image<T>::operator =(const Image<T> &img)
30 {
31 if(this != &img) {
32 destroy();
33 create(img.width, img.height, img.pixels);
34 }
35 return *this;
36 }
38 template <typename T>
39 Image<T>::Image(const Image<T> &&img)
40 {
41 width = img.width;
42 height = img.height;
43 pixels = img.pixels;
44 own_pixels = img.own_pixels;
46 img.pixels = 0;
47 img.width = img.height = 0;
48 }
50 template <typename T>
51 Image<T> &&Image<T>::operator =(const Image<T> &&img)
52 {
53 if(this != &img) {
54 width = img.width;
55 height = img.height;
56 pixels = img.pixels;
57 own_pixels = img.own_pixels;
59 img.pixels = 0;
60 img.width = img.height = 0;
61 }
62 return *this;
63 }
65 template <typename T>
66 void Image<T>::create(int xsz, int ysz, const T *pixels)
67 {
68 destroy();
70 this->pixels = new T[xsz * ysz * 4];
71 if(pixels) {
72 memcpy(this->pixels, pixels, xsz * ysz * 4 * sizeof(T));
73 } else {
74 memset(this->pixels, 0, xsz * ysz * 4 * sizeof(T));
75 }
76 width = xsz;
77 height = ysz;
78 own_pixels = true;
79 }
81 template <typename T>
82 void Image<T>::destroy()
83 {
84 if(own_pixels) {
85 delete [] pixels;
86 }
87 pixels = 0;
88 width = height = 0;
89 own_pixels = true;
90 }
92 template <typename T>
93 int Image<T>::get_width() const
94 {
95 return width;
96 }
98 template <typename T>
99 int Image<T>::get_height() const
100 {
101 return height;
102 }
104 template <typename T>
105 void Image<T>::set_pixels(int xsz, int ysz, T *pixels)
106 {
107 destroy();
109 this->pixels = pixels;
110 width = xsz;
111 height = ysz;
112 own_pixels = false;
113 }
115 template <typename T>
116 T *Image<T>::get_pixels() const
117 {
118 return pixels;
119 }
121 bool Image<unsigned char>::load(const char *fname)
122 {
123 int xsz, ysz;
124 unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32);
125 if(!pix) {
126 return false;
127 }
129 create(xsz, ysz, pix);
130 return true;
131 }
133 bool Image<float>::load(const char *fname)
134 {
135 int xsz, ysz;
136 float *pix = (float*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBAF);
137 if(!pix) {
138 return false;
139 }
141 create(xsz, ysz, pix);
142 return true;
143 }