erebus

view liberebus/src/image.inl @ 28:4a0a288ffb27

phong/lafortune BRDF
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 06 Jun 2014 14:39:40 +0300
parents 474a0244f57d
children
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 void Image<T>::clear()
123 {
124 memset(pixels, 0, width * height * 4 * sizeof(T));
125 }
127 template <typename T>
128 inline bool load_image(Image<T> *img, const char *fname)
129 {
130 return false;
131 }
133 template <>
134 inline bool load_image<unsigned char>(Image<unsigned char> *img, const char *fname)
135 {
136 int xsz, ysz;
137 unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32);
138 if(!pix) {
139 return false;
140 }
142 img->create(xsz, ysz, pix);
143 img_free_pixels(pix);
144 return true;
145 }
147 template <>
148 inline bool load_image<float>(Image<float> *img, const char *fname)
149 {
150 int xsz, ysz;
151 float *pix = (float*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBAF);
152 if(!pix) {
153 return false;
154 }
156 img->create(xsz, ysz, pix);
157 img_free_pixels(pix);
158 return true;
159 }
161 template <typename T>
162 bool Image<T>::load(const char *fname)
163 {
164 return load_image(this, fname);
165 }