erebus

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/liberebus/src/image.inl	Sun Apr 27 16:02:47 2014 +0300
     1.3 @@ -0,0 +1,143 @@
     1.4 +#include <string.h>
     1.5 +#include "imago2.h"
     1.6 +#include "image.h"
     1.7 +
     1.8 +template <typename T>
     1.9 +Image<T>::Image()
    1.10 +{
    1.11 +	pixels = 0;
    1.12 +	own_pixels = true;
    1.13 +	width = height = 0;
    1.14 +}
    1.15 +
    1.16 +template <typename T>
    1.17 +Image<T>::~Image()
    1.18 +{
    1.19 +	destroy();
    1.20 +}
    1.21 +
    1.22 +template <typename T>
    1.23 +Image<T>::Image(const Image<T> &img)
    1.24 +{
    1.25 +	pixels = 0;
    1.26 +	own_pixels = false;
    1.27 +
    1.28 +	create(img.width, img.height, img.pixels);
    1.29 +}
    1.30 +
    1.31 +template <typename T>
    1.32 +Image<T> &Image<T>::operator =(const Image<T> &img)
    1.33 +{
    1.34 +	if(this != &img) {
    1.35 +		destroy();
    1.36 +		create(img.width, img.height, img.pixels);
    1.37 +	}
    1.38 +	return *this;
    1.39 +}
    1.40 +
    1.41 +template <typename T>
    1.42 +Image<T>::Image(const Image<T> &&img)
    1.43 +{
    1.44 +	width = img.width;
    1.45 +	height = img.height;
    1.46 +	pixels = img.pixels;
    1.47 +	own_pixels = img.own_pixels;
    1.48 +
    1.49 +	img.pixels = 0;
    1.50 +	img.width = img.height = 0;
    1.51 +}
    1.52 +
    1.53 +template <typename T>
    1.54 +Image<T> &&Image<T>::operator =(const Image<T> &&img)
    1.55 +{
    1.56 +	if(this != &img) {
    1.57 +		width = img.width;
    1.58 +		height = img.height;
    1.59 +		pixels = img.pixels;
    1.60 +		own_pixels = img.own_pixels;
    1.61 +
    1.62 +		img.pixels = 0;
    1.63 +		img.width = img.height = 0;
    1.64 +	}
    1.65 +	return *this;
    1.66 +}
    1.67 +
    1.68 +template <typename T>
    1.69 +void Image<T>::create(int xsz, int ysz, const T *pixels)
    1.70 +{
    1.71 +	destroy();
    1.72 +
    1.73 +	this->pixels = new T[xsz * ysz * 4];
    1.74 +	if(pixels) {
    1.75 +		memcpy(this->pixels, pixels, xsz * ysz * 4 * sizeof(T));
    1.76 +	} else {
    1.77 +		memset(this->pixels, 0, xsz * ysz * 4 * sizeof(T));
    1.78 +	}
    1.79 +	width = xsz;
    1.80 +	height = ysz;
    1.81 +	own_pixels = true;
    1.82 +}
    1.83 +
    1.84 +template <typename T>
    1.85 +void Image<T>::destroy()
    1.86 +{
    1.87 +	if(own_pixels) {
    1.88 +		delete [] pixels;
    1.89 +	}
    1.90 +	pixels = 0;
    1.91 +	width = height = 0;
    1.92 +	own_pixels = true;
    1.93 +}
    1.94 +
    1.95 +template <typename T>
    1.96 +int Image<T>::get_width() const
    1.97 +{
    1.98 +	return width;
    1.99 +}
   1.100 +
   1.101 +template <typename T>
   1.102 +int Image<T>::get_height() const
   1.103 +{
   1.104 +	return height;
   1.105 +}
   1.106 +
   1.107 +template <typename T>
   1.108 +void Image<T>::set_pixels(int xsz, int ysz, T *pixels)
   1.109 +{
   1.110 +	destroy();
   1.111 +
   1.112 +	this->pixels = pixels;
   1.113 +	width = xsz;
   1.114 +	height = ysz;
   1.115 +	own_pixels = false;
   1.116 +}
   1.117 +
   1.118 +template <typename T>
   1.119 +T *Image<T>::get_pixels() const
   1.120 +{
   1.121 +	return pixels;
   1.122 +}
   1.123 +
   1.124 +bool Image<unsigned char>::load(const char *fname)
   1.125 +{
   1.126 +	int xsz, ysz;
   1.127 +	unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32);
   1.128 +	if(!pix) {
   1.129 +		return false;
   1.130 +	}
   1.131 +
   1.132 +	create(xsz, ysz, pix);
   1.133 +	return true;
   1.134 +}
   1.135 +
   1.136 +bool Image<float>::load(const char *fname)
   1.137 +{
   1.138 +	int xsz, ysz;
   1.139 +	float *pix = (float*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBAF);
   1.140 +	if(!pix) {
   1.141 +		return false;
   1.142 +	}
   1.143 +
   1.144 +	create(xsz, ysz, pix);
   1.145 +	return true;
   1.146 +}
   1.147 \ No newline at end of file