erebus

view liberebus/src/image.inl @ 2:474a0244f57d

fixed specialization mistake fixed line endings added makefiles
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Apr 2014 06:31:10 +0300
parents 4abdce1361b9
children c8a6fb04fefa
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 template <typename T>
122 inline bool load_image(Image<T> *img, const char *fname)
123 {
124 return false;
125 }
127 template <>
128 inline bool load_image<unsigned char>(Image<unsigned char> *img, const char *fname)
129 {
130 int xsz, ysz;
131 unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32);
132 if(!pix) {
133 return false;
134 }
136 img->create(xsz, ysz, pix);
137 img_free_pixels(pix);
138 return true;
139 }
141 template <>
142 inline bool load_image<float>(Image<float> *img, const char *fname)
143 {
144 int xsz, ysz;
145 float *pix = (float*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBAF);
146 if(!pix) {
147 return false;
148 }
150 img->create(xsz, ysz, pix);
151 img_free_pixels(pix);
152 return true;
153 }
155 template <typename T>
156 bool Image<T>::load(const char *fname)
157 {
158 return load_image(this, fname);
159 }