-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.hxx
74 lines (67 loc) · 1.73 KB
/
image.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef __IMAGELOADER__
#define __IMAGELOADER__
#include<Magick++.h>
#include<cstdint>
#include<cmath>
#include<string>
#include<vector>
#include<tuple>
#include<list>
using std::list;
using std::tuple;
using std::vector;
using std::string;
using namespace Magick;
/* Image Loader Singleton Class */
class ImageLoader {
explicit ImageLoader(char *argv0)
{
InitializeMagick(argv0);
}
public:
static ImageLoader* get_instance(char *argv0)
{
static ImageLoader *inst = nullptr;
if (inst == nullptr) inst = new ImageLoader(argv0);
return inst;
}
tuple<vector<uint32_t>, vector<vector<vector<uint32_t>>>>
load(const string path, uint32_t width, uint32_t height)
{
vector<Image> frames;
readImages(&frames, path);
double rows_by_cols = double(frames.begin() -> rows()) /
double(frames.begin() -> columns());
if (height < round(rows_by_cols * width)) {
double cols_by_rows = 1. / rows_by_cols;
width = round(cols_by_rows * height);
} else {
height = round(rows_by_cols * width);
}
Geometry new_size(width, height);
new_size.aspect(false);
vector<vector<vector<uint32_t>>> ret(frames.size());
vector<uint32_t> delay(frames.size());
uint32_t frm = 0;
for (auto& frame: frames) {
if (frame.columns() > width)
frame.resize(new_size);
ret[frm].resize(height);
for (uint32_t r = 0; r < height; r++) {
ret[frm][r].resize(width);
for (uint32_t c = 0; c < width; c++) {
ColorRGB rgb = frame.pixelColor(c, r);
uint32_t pix = 0;
pix |= uint32_t(rgb.red() * 255) << 16;
pix |= uint32_t(rgb.green() * 255) << 8;
pix |= uint32_t(rgb.blue() * 255) << 0;
ret[frm][r][c] = pix;
}
}
delay[frm] = frame.animationDelay();
++frm;
}
return tie(delay, ret);
}
};
#endif