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