erebus

annotate liberebus/src/image.inl @ 19:6204e4d3f445

scene loading
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 27 May 2014 07:43:55 +0300
parents 4abdce1361b9
children c8a6fb04fefa
rev   line source
nuclear@0 1 #include <string.h>
nuclear@0 2 #include "imago2.h"
nuclear@0 3 #include "image.h"
nuclear@0 4
nuclear@0 5 template <typename T>
nuclear@0 6 Image<T>::Image()
nuclear@0 7 {
nuclear@0 8 pixels = 0;
nuclear@0 9 own_pixels = true;
nuclear@0 10 width = height = 0;
nuclear@0 11 }
nuclear@0 12
nuclear@0 13 template <typename T>
nuclear@0 14 Image<T>::~Image()
nuclear@0 15 {
nuclear@0 16 destroy();
nuclear@0 17 }
nuclear@0 18
nuclear@0 19 template <typename T>
nuclear@0 20 Image<T>::Image(const Image<T> &img)
nuclear@0 21 {
nuclear@0 22 pixels = 0;
nuclear@0 23 own_pixels = false;
nuclear@0 24
nuclear@0 25 create(img.width, img.height, img.pixels);
nuclear@0 26 }
nuclear@0 27
nuclear@0 28 template <typename T>
nuclear@0 29 Image<T> &Image<T>::operator =(const Image<T> &img)
nuclear@0 30 {
nuclear@0 31 if(this != &img) {
nuclear@0 32 destroy();
nuclear@0 33 create(img.width, img.height, img.pixels);
nuclear@0 34 }
nuclear@0 35 return *this;
nuclear@0 36 }
nuclear@0 37
nuclear@0 38 template <typename T>
nuclear@0 39 Image<T>::Image(const Image<T> &&img)
nuclear@0 40 {
nuclear@0 41 width = img.width;
nuclear@0 42 height = img.height;
nuclear@0 43 pixels = img.pixels;
nuclear@0 44 own_pixels = img.own_pixels;
nuclear@0 45
nuclear@0 46 img.pixels = 0;
nuclear@0 47 img.width = img.height = 0;
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 template <typename T>
nuclear@0 51 Image<T> &&Image<T>::operator =(const Image<T> &&img)
nuclear@0 52 {
nuclear@0 53 if(this != &img) {
nuclear@0 54 width = img.width;
nuclear@0 55 height = img.height;
nuclear@0 56 pixels = img.pixels;
nuclear@0 57 own_pixels = img.own_pixels;
nuclear@0 58
nuclear@0 59 img.pixels = 0;
nuclear@0 60 img.width = img.height = 0;
nuclear@0 61 }
nuclear@0 62 return *this;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 template <typename T>
nuclear@0 66 void Image<T>::create(int xsz, int ysz, const T *pixels)
nuclear@0 67 {
nuclear@0 68 destroy();
nuclear@0 69
nuclear@0 70 this->pixels = new T[xsz * ysz * 4];
nuclear@0 71 if(pixels) {
nuclear@0 72 memcpy(this->pixels, pixels, xsz * ysz * 4 * sizeof(T));
nuclear@0 73 } else {
nuclear@0 74 memset(this->pixels, 0, xsz * ysz * 4 * sizeof(T));
nuclear@0 75 }
nuclear@0 76 width = xsz;
nuclear@0 77 height = ysz;
nuclear@0 78 own_pixels = true;
nuclear@0 79 }
nuclear@0 80
nuclear@0 81 template <typename T>
nuclear@0 82 void Image<T>::destroy()
nuclear@0 83 {
nuclear@0 84 if(own_pixels) {
nuclear@0 85 delete [] pixels;
nuclear@0 86 }
nuclear@0 87 pixels = 0;
nuclear@0 88 width = height = 0;
nuclear@0 89 own_pixels = true;
nuclear@0 90 }
nuclear@0 91
nuclear@0 92 template <typename T>
nuclear@0 93 int Image<T>::get_width() const
nuclear@0 94 {
nuclear@0 95 return width;
nuclear@0 96 }
nuclear@0 97
nuclear@0 98 template <typename T>
nuclear@0 99 int Image<T>::get_height() const
nuclear@0 100 {
nuclear@0 101 return height;
nuclear@0 102 }
nuclear@0 103
nuclear@0 104 template <typename T>
nuclear@0 105 void Image<T>::set_pixels(int xsz, int ysz, T *pixels)
nuclear@0 106 {
nuclear@0 107 destroy();
nuclear@0 108
nuclear@0 109 this->pixels = pixels;
nuclear@0 110 width = xsz;
nuclear@0 111 height = ysz;
nuclear@0 112 own_pixels = false;
nuclear@0 113 }
nuclear@0 114
nuclear@0 115 template <typename T>
nuclear@0 116 T *Image<T>::get_pixels() const
nuclear@0 117 {
nuclear@0 118 return pixels;
nuclear@0 119 }
nuclear@0 120
nuclear@2 121 template <typename T>
nuclear@2 122 inline bool load_image(Image<T> *img, const char *fname)
nuclear@2 123 {
nuclear@2 124 return false;
nuclear@2 125 }
nuclear@2 126
nuclear@2 127 template <>
nuclear@2 128 inline bool load_image<unsigned char>(Image<unsigned char> *img, const char *fname)
nuclear@0 129 {
nuclear@0 130 int xsz, ysz;
nuclear@0 131 unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32);
nuclear@0 132 if(!pix) {
nuclear@0 133 return false;
nuclear@0 134 }
nuclear@0 135
nuclear@2 136 img->create(xsz, ysz, pix);
nuclear@2 137 img_free_pixels(pix);
nuclear@0 138 return true;
nuclear@0 139 }
nuclear@0 140
nuclear@2 141 template <>
nuclear@2 142 inline bool load_image<float>(Image<float> *img, const char *fname)
nuclear@0 143 {
nuclear@0 144 int xsz, ysz;
nuclear@0 145 float *pix = (float*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBAF);
nuclear@0 146 if(!pix) {
nuclear@0 147 return false;
nuclear@0 148 }
nuclear@0 149
nuclear@2 150 img->create(xsz, ysz, pix);
nuclear@2 151 img_free_pixels(pix);
nuclear@0 152 return true;
nuclear@2 153 }
nuclear@2 154
nuclear@2 155 template <typename T>
nuclear@2 156 bool Image<T>::load(const char *fname)
nuclear@2 157 {
nuclear@2 158 return load_image(this, fname);
nuclear@2 159 }