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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
#include "vector_vimageloader.h"
#include "config.h"
#include "vector_vdebug.h"
#include <cstring>
#ifdef _WIN32
# include <windows.h>
#else
# include <dlfcn.h>
#endif // _WIN32
using lottie_image_load_f = unsigned char *(*)(const char *filename, int *x,
int *y, int *comp, int req_comp);
using lottie_image_load_data_f = unsigned char *(*)(const char *data, int len,
int *x, int *y, int *comp,
int req_comp);
using lottie_image_free_f = void (*)(unsigned char *);
#ifdef __cplusplus
extern "C" {
#endif
extern unsigned char *lottie_image_load(char const *filename, int *x, int *y,
int *comp, int req_comp);
extern unsigned char *lottie_image_load_from_data(const char *imageData,
int len, int *x, int *y,
int *comp, int req_comp);
extern void lottie_image_free(unsigned char *data);
#ifdef __cplusplus
}
#endif
struct VImageLoader::Impl {
lottie_image_load_f imageLoad{nullptr};
lottie_image_free_f imageFree{nullptr};
lottie_image_load_data_f imageFromData{nullptr};
#ifdef LOTTIE_IMAGE_MODULE_SUPPORT
# ifdef _WIN32
HMODULE dl_handle{nullptr};
bool moduleLoad()
{
dl_handle = LoadLibraryA(LOTTIE_IMAGE_MODULE_PLUGIN);
return (dl_handle == nullptr);
}
void moduleFree()
{
if (dl_handle) FreeLibrary(dl_handle);
}
void init()
{
imageLoad = reinterpret_cast<lottie_image_load_f>(
GetProcAddress(dl_handle, "lottie_image_load"));
imageFree = reinterpret_cast<lottie_image_free_f>(
GetProcAddress(dl_handle, "lottie_image_free"));
imageFromData = reinterpret_cast<lottie_image_load_data_f>(
GetProcAddress(dl_handle, "lottie_image_load_from_data"));
}
# else // _WIN32
void *dl_handle{nullptr};
void init()
{
imageLoad = reinterpret_cast<lottie_image_load_f>(
dlsym(dl_handle, "lottie_image_load"));
imageFree = reinterpret_cast<lottie_image_free_f>(
dlsym(dl_handle, "lottie_image_free"));
imageFromData = reinterpret_cast<lottie_image_load_data_f>(
dlsym(dl_handle, "lottie_image_load_from_data"));
}
void moduleFree()
{
if (dl_handle) dlclose(dl_handle);
}
bool moduleLoad()
{
dl_handle = dlopen(LOTTIE_IMAGE_MODULE_PLUGIN, RTLD_LAZY);
return (dl_handle == nullptr);
}
# endif // _WIN32
#else // LOTTIE_IMAGE_MODULE_SUPPORT
void init()
{
imageLoad = lottie_image_load;
imageFree = lottie_image_free;
imageFromData = lottie_image_load_from_data;
}
void moduleFree() {}
bool moduleLoad() { return false; }
#endif // LOTTIE_IMAGE_MODULE_SUPPORT
Impl()
{
if (moduleLoad()) {
vWarning << "Failed to dlopen librlottie-image-loader library";
return;
}
init();
if (!imageLoad)
vWarning << "Failed to find symbol lottie_image_load in "
"librlottie-image-loader library";
if (!imageFree)
vWarning << "Failed to find symbol lottie_image_free in "
"librlottie-image-loader library";
if (!imageFromData)
vWarning << "Failed to find symbol lottie_image_load_data in "
"librlottie-image-loader library";
}
~Impl() { moduleFree(); }
VBitmap createBitmap(unsigned char *data, int width, int height,
int channel)
{
// premultiply alpha
if (channel == 4)
convertToBGRAPremul(data, width, height);
else
convertToBGRA(data, width, height);
// create a bitmap of same size.
VBitmap result =
VBitmap(width, height, VBitmap::Format::ARGB32_Premultiplied);
// copy the data to bitmap buffer
memcpy(result.data(), data, width * height * 4);
// free the image data
imageFree(data);
return result;
}
VBitmap load(const char *fileName)
{
if (!imageLoad) return VBitmap();
int width, height, n;
unsigned char *data = imageLoad(fileName, &width, &height, &n, 4);
if (!data) {
return VBitmap();
}
return createBitmap(data, width, height, n);
}
VBitmap load(const char *imageData, size_t len)
{
if (!imageFromData) return VBitmap();
int width, height, n;
unsigned char *data =
imageFromData(imageData, static_cast<int>(len), &width, &height, &n, 4);
if (!data) {
return VBitmap();
}
return createBitmap(data, width, height, n);
}
/*
* convert from RGBA to BGRA and premultiply
*/
void convertToBGRAPremul(unsigned char *bits, int width, int height)
{
int pixelCount = width * height;
unsigned char *pix = bits;
for (int i = 0; i < pixelCount; i++) {
unsigned char r = pix[0];
unsigned char g = pix[1];
unsigned char b = pix[2];
unsigned char a = pix[3];
r = (r * a) / 255;
g = (g * a) / 255;
b = (b * a) / 255;
pix[0] = b;
pix[1] = g;
pix[2] = r;
pix += 4;
}
}
/*
* convert from RGBA to BGRA
*/
void convertToBGRA(unsigned char *bits, int width, int height)
{
int pixelCount = width * height;
unsigned char *pix = bits;
for (int i = 0; i < pixelCount; i++) {
unsigned char r = pix[0];
unsigned char b = pix[2];
pix[0] = b;
pix[2] = r;
pix += 4;
}
}
};
VImageLoader::VImageLoader() : mImpl(std::make_unique<VImageLoader::Impl>()) {}
VImageLoader::~VImageLoader() {}
VBitmap VImageLoader::load(const char *fileName)
{
return mImpl->load(fileName);
}
VBitmap VImageLoader::load(const char *data, size_t len)
{
return mImpl->load(data, int(len));
}
|