nuclear@0: #include nuclear@0: #include "imago2.h" nuclear@0: #include "image.h" nuclear@0: nuclear@0: Image::Image() nuclear@0: { nuclear@0: pixels = 0; nuclear@0: own_pixels = true; nuclear@0: width = height = 0; nuclear@0: } nuclear@0: nuclear@0: Image::~Image() nuclear@0: { nuclear@0: destroy(); nuclear@0: } nuclear@0: nuclear@0: Image::Image(const Image &img) nuclear@0: { nuclear@0: pixels = 0; nuclear@0: own_pixels = false; nuclear@0: nuclear@0: create(img.width, img.height, img.pixels); nuclear@0: } nuclear@0: nuclear@0: Image &Image::operator =(const Image &img) nuclear@0: { nuclear@0: if(this != &img) { nuclear@0: destroy(); nuclear@0: create(img.width, img.height, img.pixels); nuclear@0: } nuclear@0: return *this; nuclear@0: } nuclear@0: nuclear@0: void Image::create(int xsz, int ysz, unsigned char *pixels) nuclear@0: { nuclear@0: destroy(); nuclear@0: nuclear@0: this->pixels = new unsigned char[xsz * ysz * 4]; nuclear@0: if(pixels) { nuclear@0: memcpy(this->pixels, pixels, xsz * ysz * 4); nuclear@0: } else { nuclear@0: memset(this->pixels, 0, xsz * ysz * 4); nuclear@0: } nuclear@0: width = xsz; nuclear@0: height = ysz; nuclear@0: own_pixels = true; nuclear@0: } nuclear@0: nuclear@0: void Image::destroy() nuclear@0: { nuclear@0: if(own_pixels) { nuclear@0: delete [] pixels; nuclear@0: } nuclear@0: pixels = 0; nuclear@0: width = height = 0; nuclear@0: own_pixels = true; nuclear@0: } nuclear@0: nuclear@0: int Image::get_width() const nuclear@0: { nuclear@0: return width; nuclear@0: } nuclear@0: nuclear@0: int Image::get_height() const nuclear@0: { nuclear@0: return height; nuclear@0: } nuclear@0: nuclear@0: void Image::set_pixels(int xsz, int ysz, unsigned char *pixels) nuclear@0: { nuclear@0: destroy(); nuclear@0: nuclear@0: this->pixels = pixels; nuclear@0: width = xsz; nuclear@0: height = ysz; nuclear@0: own_pixels = false; nuclear@0: } nuclear@0: nuclear@0: unsigned char *Image::get_pixels() const nuclear@0: { nuclear@0: return pixels; nuclear@0: } nuclear@0: nuclear@0: bool Image::load(const char *fname) nuclear@0: { nuclear@0: int xsz, ysz; nuclear@0: unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz); nuclear@0: if(!pix) { nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: create(xsz, ysz, pix); nuclear@0: return true; nuclear@0: }