volmetrics

view src/image.cc @ 4:1fbbe10c8e08

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