]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/image.cpp
2835d05e872936d0cc069fa806812ff3ab952661
[libs/gui.git] / source / graphics / image.cpp
1 #include <msp/core/refptr.h>
2 #include <msp/fs/utils.h>
3 #include <msp/io/file.h>
4 #include <msp/io/memory.h>
5 #include "image.h"
6 #include "imageloader.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace Graphics {
12
13 Image::Data::Data():
14         fmt(RGB),
15         width(0),
16         height(0),
17         owned_pixels(0),
18         pixels(0)
19 { }
20
21 Image::Data::Data(const Data &other):
22         fmt(other.fmt),
23         width(other.width),
24         height(other.height),
25         stride(other.stride),
26         owned_pixels(other.pixels ? new char[stride*height] : 0),
27         pixels(owned_pixels)
28 {
29         if(pixels)
30                 copy(other.pixels, other.pixels+stride*height, pixels);
31 }
32
33 Image::Data &Image::Data::operator=(const Data &other)
34 {
35         delete[] owned_pixels;
36         pixels = owned_pixels = 0;
37
38         fmt = other.fmt;
39         width = other.width;
40         height = other.height;
41         stride = other.stride;
42
43         if(other.pixels)
44         {
45                 pixels = owned_pixels = new char[stride*height];
46                 copy(other.pixels, other.pixels+stride*height, pixels);
47         }
48
49         return *this;
50 }
51
52 Image::Data::~Data()
53 {
54         delete[] owned_pixels;
55 }
56
57
58 void Image::load_file(const string &fn)
59 {
60         RefPtr<ImageLoader> loader = ImageLoader::open_file(fn);
61         load(*loader);
62 }
63
64 void Image::load_io(IO::Seekable &io)
65 {
66         RefPtr<ImageLoader> loader = ImageLoader::open_io(io);
67         load(*loader);
68 }
69
70 void Image::load(ImageLoader &loader)
71 {
72         if(loader.get_state()==ImageLoader::INITIAL)
73                 data = Data();
74         loader.load(data);
75 }
76
77 void Image::load_into(ImageLoader &loader, void *buffer)
78 {
79         data.pixels = reinterpret_cast<char *>(buffer);
80         load(loader);
81 }
82
83 void Image::load_headers(ImageLoader &loader)
84 {
85         if(loader.get_state()==ImageLoader::INITIAL)
86                 data = Data();
87         loader.load_headers(data);
88 }
89
90 } // namespace Graphics
91 } // namespace Msp