volmetrics

annotate src/image.cc @ 36:1df14c5ffa71

conversion to Qt
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 06 Feb 2015 22:39:51 +0200
parents 927c29b93009
children
rev   line source
elene@3 1 #include <imago2.h>
elene@3 2
elene@3 3 #include "image.h"
elene@3 4
elene@3 5 Image::Image()
elene@3 6 {
elene@3 7 pixels = 0;
elene@3 8 width = 0;
elene@3 9 height = 0;
elene@3 10 }
elene@3 11
elene@3 12 Image::~Image()
elene@3 13 {
elene@3 14 delete [] pixels;
elene@3 15 }
elene@3 16
elene@4 17 //copy constructor - assignment operator
elene@4 18 Image::Image(const Image &img)
elene@4 19 {
elene@4 20 pixels = 0;
elene@4 21 set_pixels(img.pixels, img.width, img.height);
elene@4 22 }
elene@4 23
elene@4 24 Image &Image::operator = (const Image &img)
elene@4 25 {
elene@4 26 if(&img != this) {
elene@4 27 set_pixels(img.pixels, img.width, img.height);
elene@4 28 }
elene@4 29 return *this;
elene@4 30 }
elene@4 31
elene@4 32 //move constructor - move operator c++11 R-value reference to image
elene@4 33 Image::Image(Image &&img)
elene@4 34 {
elene@4 35 pixels = img.pixels;
elene@4 36 width = img.width;
elene@4 37 height = img.height;
elene@4 38
elene@4 39 img.pixels = 0;
elene@4 40 img.width = 0;
elene@4 41 img.height = 0;
elene@4 42 }
elene@4 43
elene@4 44 Image &Image::operator = (Image &&img)
elene@4 45 {
elene@4 46 if(&img != this) {
elene@4 47 pixels = img.pixels;
elene@4 48 width = img.width;
elene@4 49 height = img.height;
elene@4 50
elene@4 51 img.pixels = 0;
elene@4 52 img.width = 0;
elene@4 53 img.height = 0;
elene@4 54 }
elene@4 55
elene@4 56 return *this;
elene@4 57 }
elene@4 58
elene@3 59 bool Image::load(const char *fname)
elene@3 60 {
elene@3 61 int new_width, new_height;
elene@3 62 float *new_pixels = (float*)img_load_pixels(fname, &new_width, &new_height, IMG_FMT_GREYF);
elene@3 63
elene@3 64 if(!new_pixels) {
elene@3 65 fprintf(stderr, "Failed to load image: %s\n", fname);
elene@3 66 return false;
elene@3 67 }
elene@3 68
elene@3 69 set_pixels(new_pixels, new_width, new_height);
elene@3 70 img_free_pixels(new_pixels);
elene@3 71 return true;
elene@3 72 }
elene@3 73
elene@3 74 float *Image::get_pixels()
elene@3 75 {
elene@3 76 return pixels;
elene@3 77 }
elene@3 78
elene@3 79 const float *Image::get_pixels() const
elene@3 80 {
elene@3 81 return pixels;
elene@3 82 }
elene@3 83
elene@3 84 void Image::set_pixels(const float *pixels, int width, int height)
elene@3 85 {
elene@3 86 if(!pixels)
elene@3 87 return;
elene@3 88
elene@3 89 delete [] this->pixels;
elene@3 90
elene@3 91 this->pixels = new float[width * height];
elene@3 92 for(int i=0; i<width * height; i++) {
elene@3 93 this->pixels[i] = pixels[i];
elene@3 94 }
elene@3 95 this->width = width;
elene@3 96 this->height = height;
elene@3 97 }
elene@3 98
elene@3 99 int Image::get_width() const
elene@3 100 {
elene@3 101 return width;
elene@3 102 }
elene@3 103
elene@3 104 int Image::get_height() const
elene@3 105 {
elene@3 106 return height;
elene@3 107 }