conworlds

annotate src/image.cc @ 15:9b0db7dbde6e

hmmm doesn't work properly yes
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 24 Aug 2014 14:36:00 +0300
parents b326d53321f7
children
rev   line source
nuclear@2 1 #include <string.h>
nuclear@2 2 #include "imago2.h"
nuclear@2 3 #include "image.h"
nuclear@2 4
nuclear@2 5 Image::Image()
nuclear@2 6 {
nuclear@2 7 pixels = 0;
nuclear@2 8 own_pixels = true;
nuclear@2 9 width = height = 0;
nuclear@2 10 }
nuclear@2 11
nuclear@2 12 Image::~Image()
nuclear@2 13 {
nuclear@2 14 destroy();
nuclear@2 15 }
nuclear@2 16
nuclear@2 17 Image::Image(const Image &img)
nuclear@2 18 {
nuclear@2 19 pixels = 0;
nuclear@2 20 own_pixels = false;
nuclear@2 21
nuclear@2 22 create(img.width, img.height, img.pixels);
nuclear@2 23 }
nuclear@2 24
nuclear@2 25 Image &Image::operator =(const Image &img)
nuclear@2 26 {
nuclear@2 27 if(this != &img) {
nuclear@2 28 destroy();
nuclear@2 29 create(img.width, img.height, img.pixels);
nuclear@2 30 }
nuclear@2 31 return *this;
nuclear@2 32 }
nuclear@2 33
nuclear@2 34 void Image::create(int xsz, int ysz, unsigned char *pixels)
nuclear@2 35 {
nuclear@2 36 destroy();
nuclear@2 37
nuclear@2 38 this->pixels = new unsigned char[xsz * ysz * 4];
nuclear@2 39 if(pixels) {
nuclear@2 40 memcpy(this->pixels, pixels, xsz * ysz * 4);
nuclear@2 41 } else {
nuclear@2 42 memset(this->pixels, 0, xsz * ysz * 4);
nuclear@2 43 }
nuclear@2 44 width = xsz;
nuclear@2 45 height = ysz;
nuclear@2 46 own_pixels = true;
nuclear@2 47 }
nuclear@2 48
nuclear@2 49 void Image::destroy()
nuclear@2 50 {
nuclear@2 51 if(own_pixels) {
nuclear@2 52 delete [] pixels;
nuclear@2 53 }
nuclear@2 54 pixels = 0;
nuclear@2 55 width = height = 0;
nuclear@2 56 own_pixels = true;
nuclear@2 57 }
nuclear@2 58
nuclear@2 59 int Image::get_width() const
nuclear@2 60 {
nuclear@2 61 return width;
nuclear@2 62 }
nuclear@2 63
nuclear@2 64 int Image::get_height() const
nuclear@2 65 {
nuclear@2 66 return height;
nuclear@2 67 }
nuclear@2 68
nuclear@2 69 void Image::set_pixels(int xsz, int ysz, unsigned char *pixels)
nuclear@2 70 {
nuclear@2 71 destroy();
nuclear@2 72
nuclear@2 73 this->pixels = pixels;
nuclear@2 74 width = xsz;
nuclear@2 75 height = ysz;
nuclear@2 76 own_pixels = false;
nuclear@2 77 }
nuclear@2 78
nuclear@2 79 unsigned char *Image::get_pixels() const
nuclear@2 80 {
nuclear@2 81 return pixels;
nuclear@2 82 }
nuclear@2 83
nuclear@2 84 bool Image::load(const char *fname)
nuclear@2 85 {
nuclear@2 86 int xsz, ysz;
nuclear@2 87 unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz);
nuclear@2 88 if(!pix) {
nuclear@2 89 return false;
nuclear@2 90 }
nuclear@2 91
nuclear@2 92 create(xsz, ysz, pix);
nuclear@2 93 return true;
nuclear@2 94 }