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