erebus

annotate 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
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@0 121 bool Image<unsigned char>::load(const char *fname)
nuclear@0 122 {
nuclear@0 123 int xsz, ysz;
nuclear@0 124 unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32);
nuclear@0 125 if(!pix) {
nuclear@0 126 return false;
nuclear@0 127 }
nuclear@0 128
nuclear@0 129 create(xsz, ysz, pix);
nuclear@0 130 return true;
nuclear@0 131 }
nuclear@0 132
nuclear@0 133 bool Image<float>::load(const char *fname)
nuclear@0 134 {
nuclear@0 135 int xsz, ysz;
nuclear@0 136 float *pix = (float*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBAF);
nuclear@0 137 if(!pix) {
nuclear@0 138 return false;
nuclear@0 139 }
nuclear@0 140
nuclear@0 141 create(xsz, ysz, pix);
nuclear@0 142 return true;
nuclear@0 143 }