vrchess

diff src/image.cc @ 0:b326d53321f7

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 25 Apr 2014 05:20:53 +0300
parents
children 879194e4b1f0
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/image.cc	Fri Apr 25 05:20:53 2014 +0300
     1.3 @@ -0,0 +1,94 @@
     1.4 +#include <string.h>
     1.5 +#include "imago2.h"
     1.6 +#include "image.h"
     1.7 +
     1.8 +Image::Image()
     1.9 +{
    1.10 +	pixels = 0;
    1.11 +	own_pixels = true;
    1.12 +	width = height = 0;
    1.13 +}
    1.14 +
    1.15 +Image::~Image()
    1.16 +{
    1.17 +	destroy();
    1.18 +}
    1.19 +
    1.20 +Image::Image(const Image &img)
    1.21 +{
    1.22 +	pixels = 0;
    1.23 +	own_pixels = false;
    1.24 +
    1.25 +	create(img.width, img.height, img.pixels);
    1.26 +}
    1.27 +
    1.28 +Image &Image::operator =(const Image &img)
    1.29 +{
    1.30 +	if(this != &img) {
    1.31 +		destroy();
    1.32 +		create(img.width, img.height, img.pixels);
    1.33 +	}
    1.34 +	return *this;
    1.35 +}
    1.36 +
    1.37 +void Image::create(int xsz, int ysz, unsigned char *pixels)
    1.38 +{
    1.39 +	destroy();
    1.40 +
    1.41 +	this->pixels = new unsigned char[xsz * ysz * 4];
    1.42 +	if(pixels) {
    1.43 +		memcpy(this->pixels, pixels, xsz * ysz * 4);
    1.44 +	} else {
    1.45 +		memset(this->pixels, 0, xsz * ysz * 4);
    1.46 +	}
    1.47 +	width = xsz;
    1.48 +	height = ysz;
    1.49 +	own_pixels = true;
    1.50 +}
    1.51 +
    1.52 +void Image::destroy()
    1.53 +{
    1.54 +	if(own_pixels) {
    1.55 +		delete [] pixels;
    1.56 +	}
    1.57 +	pixels = 0;
    1.58 +	width = height = 0;
    1.59 +	own_pixels = true;
    1.60 +}
    1.61 +
    1.62 +int Image::get_width() const
    1.63 +{
    1.64 +	return width;
    1.65 +}
    1.66 +
    1.67 +int Image::get_height() const
    1.68 +{
    1.69 +	return height;
    1.70 +}
    1.71 +
    1.72 +void Image::set_pixels(int xsz, int ysz, unsigned char *pixels)
    1.73 +{
    1.74 +	destroy();
    1.75 +
    1.76 +	this->pixels = pixels;
    1.77 +	width = xsz;
    1.78 +	height = ysz;
    1.79 +	own_pixels = false;
    1.80 +}
    1.81 +
    1.82 +unsigned char *Image::get_pixels() const
    1.83 +{
    1.84 +	return pixels;
    1.85 +}
    1.86 +
    1.87 +bool Image::load(const char *fname)
    1.88 +{
    1.89 +	int xsz, ysz;
    1.90 +	unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz);
    1.91 +	if(!pix) {
    1.92 +		return false;
    1.93 +	}
    1.94 +
    1.95 +	create(xsz, ysz, pix);
    1.96 +	return true;
    1.97 +}
    1.98 \ No newline at end of file