vrchess

annotate 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
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 Image::Image()
nuclear@0 6 {
nuclear@0 7 pixels = 0;
nuclear@0 8 own_pixels = true;
nuclear@0 9 width = height = 0;
nuclear@0 10 }
nuclear@0 11
nuclear@0 12 Image::~Image()
nuclear@0 13 {
nuclear@0 14 destroy();
nuclear@0 15 }
nuclear@0 16
nuclear@0 17 Image::Image(const Image &img)
nuclear@0 18 {
nuclear@0 19 pixels = 0;
nuclear@0 20 own_pixels = false;
nuclear@0 21
nuclear@0 22 create(img.width, img.height, img.pixels);
nuclear@0 23 }
nuclear@0 24
nuclear@0 25 Image &Image::operator =(const Image &img)
nuclear@0 26 {
nuclear@0 27 if(this != &img) {
nuclear@0 28 destroy();
nuclear@0 29 create(img.width, img.height, img.pixels);
nuclear@0 30 }
nuclear@0 31 return *this;
nuclear@0 32 }
nuclear@0 33
nuclear@0 34 void Image::create(int xsz, int ysz, unsigned char *pixels)
nuclear@0 35 {
nuclear@0 36 destroy();
nuclear@0 37
nuclear@0 38 this->pixels = new unsigned char[xsz * ysz * 4];
nuclear@0 39 if(pixels) {
nuclear@0 40 memcpy(this->pixels, pixels, xsz * ysz * 4);
nuclear@0 41 } else {
nuclear@0 42 memset(this->pixels, 0, xsz * ysz * 4);
nuclear@0 43 }
nuclear@0 44 width = xsz;
nuclear@0 45 height = ysz;
nuclear@0 46 own_pixels = true;
nuclear@0 47 }
nuclear@0 48
nuclear@0 49 void Image::destroy()
nuclear@0 50 {
nuclear@0 51 if(own_pixels) {
nuclear@0 52 delete [] pixels;
nuclear@0 53 }
nuclear@0 54 pixels = 0;
nuclear@0 55 width = height = 0;
nuclear@0 56 own_pixels = true;
nuclear@0 57 }
nuclear@0 58
nuclear@0 59 int Image::get_width() const
nuclear@0 60 {
nuclear@0 61 return width;
nuclear@0 62 }
nuclear@0 63
nuclear@0 64 int Image::get_height() const
nuclear@0 65 {
nuclear@0 66 return height;
nuclear@0 67 }
nuclear@0 68
nuclear@0 69 void Image::set_pixels(int xsz, int ysz, unsigned char *pixels)
nuclear@0 70 {
nuclear@0 71 destroy();
nuclear@0 72
nuclear@0 73 this->pixels = pixels;
nuclear@0 74 width = xsz;
nuclear@0 75 height = ysz;
nuclear@0 76 own_pixels = false;
nuclear@0 77 }
nuclear@0 78
nuclear@0 79 unsigned char *Image::get_pixels() const
nuclear@0 80 {
nuclear@0 81 return pixels;
nuclear@0 82 }
nuclear@0 83
nuclear@0 84 bool Image::load(const char *fname)
nuclear@0 85 {
nuclear@0 86 int xsz, ysz;
nuclear@0 87 unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz);
nuclear@0 88 if(!pix) {
nuclear@0 89 return false;
nuclear@0 90 }
nuclear@0 91
nuclear@0 92 create(xsz, ysz, pix);
nuclear@0 93 return true;
nuclear@0 94 }