erebus

annotate liberebus/src/image.inl @ 44:a2afaf8af09b

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