| 5 |
| #include <dlfcn.h> |
| 6 | #else |
| 7 | #include <Windows.h> |
| 8 | #endif |
| 9 | #include <cstring> |
| 10 |
| 11 | using lottie_image_load_f = unsigned char *(*)(const char *filename, int *x, |
| 12 | int *y, int *comp, int req_comp); |
| 13 | using lottie_image_load_data_f = unsigned char *(*)(const char *data, int len, |
| 14 | int *x, int *y, int *comp, |
| 15 | int req_comp); |
| 16 | using lottie_image_free_f = void (*)(unsigned char *); |
| 17 |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 |
| 22 | extern unsigned char *lottie_image_load(char const *filename, int *x, int *y, |
| 23 | int *comp, int req_comp); |
| 24 | extern unsigned char *lottie_image_load_from_data(const char *imageData, |
| 25 | int len, int *x, int *y, |
| 26 | int *comp, int req_comp); |
| 27 | extern void lottie_image_free(unsigned char *data); |
| 28 |
| 29 | #ifdef __cplusplus |
| 30 | } |
| 31 | #endif |
| 32 |
| 33 | struct VImageLoader::Impl { |
| 34 | lottie_image_load_f imageLoad{nullptr}; |
| 35 | lottie_image_free_f imageFree{nullptr}; |
| 36 | lottie_image_load_data_f imageFromData{nullptr}; |
| 37 |
| 38 | #ifndef LOTTIE_STATIC_IMAGE_LOADER |
| 39 | #ifdef WIN32 |
| 40 | HMODULE dl_handle{nullptr}; |
| 41 | bool moduleLoad() |
| 42 | { |
| 43 | dl_handle = LoadLibraryA("librlottie-image-loader.dll"); |
| 44 | return (dl_handle == nullptr); |
| 45 | } |
| 46 | void moduleFree() |
| 47 | { |
| 48 | if (dl_handle) FreeLibrary(dl_handle); |
| 49 | } |
| 50 | void init() |
| 51 | { |
| 52 | imageLoad = |
| 53 | (lottie_image_load_f)GetProcAddress(dl_handle, "lottie_image_load"); |
| 54 | imageFree = |
| 55 | (lottie_image_free_f)GetProcAddress(dl_handle, "lottie_image_free"); |
| 56 | imageFromData = (lottie_image_load_data_f)GetProcAddress( |
| 57 | dl_handle, "lottie_image_load_from_data"); |
| 58 | } |
| 59 | #else |
| 60 | void *dl_handle{nullptr}; |
| 61 | void init() |
| 62 | { |
| 63 | imageLoad = (lottie_image_load_f)dlsym(dl_handle, "lottie_image_load"); |
| 64 | imageFree = (lottie_image_free_f)dlsym(dl_handle, "lottie_image_free"); |
| 65 | imageFromData = (lottie_image_load_data_f)dlsym( |
| 66 | dl_handle, "lottie_image_load_from_data"); |
| 67 | } |
| 68 |
| 69 | void moduleFree() |
| 70 | { |
| 71 | if (dl_handle) dlclose(dl_handle); |
| 72 | } |
| 73 | #ifdef __APPLE__ |
| 74 | bool moduleLoad() |
| 75 | { |
| 76 | dl_handle = dlopen("librlottie-image-loader.dylib", RTLD_LAZY); |
| 77 | return (dl_handle == nullptr); |
| 78 | } |
| 79 | #else |
| 80 | bool moduleLoad() |
| 81 | { |
| 82 | dl_handle = dlopen("librlottie-image-loader.so", RTLD_LAZY); |
| 83 | return (dl_handle == nullptr); |
| 84 | } |
| 85 | #endif |
| 86 | #endif |
| 87 | #else |
| 88 | void *dl_handle{nullptr}; |
| 89 | void init() |
| 90 | { |
| 91 | imageLoad = lottie_image_load; |
| 92 | imageFree = lottie_image_free; |
| 93 | imageFromData = lottie_image_load_from_data; |
| 94 | } |
| 95 | void moduleFree() {} |
| 96 | bool moduleLoad() { return false; } |
| 97 | #endif |
| 98 |
| 99 | Impl() |
| 100 | { |
| 101 | if (moduleLoad()) { |
| 102 | vWarning << "Failed to dlopen librlottie-image-loader library"; |
| 103 | return; |
| 104 | } |
| 105 |
| 106 | init(); |
| 107 |
| 108 | if (!imageLoad) |
| 109 | vWarning << "Failed to find symbol lottie_image_load in " |
| 110 | "librlottie-image-loader library"; |
| 111 |
| 112 | if (!imageFree) |
| 113 | vWarning << "Failed to find symbol lottie_image_free in " |
| 114 | "librlottie-image-loader library"; |
| 115 |
| 116 | if (!imageFromData) |
| 117 | vWarning << "Failed to find symbol lottie_image_load_data in " |
| 118 | "librlottie-image-loader library"; |
| 119 | } |
| 120 |
| 121 | ~Impl() { moduleFree(); } |
| 122 |
| 123 | VBitmap createBitmap(unsigned char *data, int width, int height, |
| 124 | int channel) |
| 125 | { |
| 126 | // premultiply alpha |
| 127 | if (channel == 4) |
| 128 | convertToBGRAPremul(data, width, height); |
| 129 | else |
| 130 | convertToBGRA(data, width, height); |
| 131 |
| 132 | // create a bitmap of same size. |
| 133 | VBitmap result = |
| 134 | VBitmap(width, height, VBitmap::Format::ARGB32); |
| 135 |
| 136 | // copy the data to bitmap buffer |
| 137 | memcpy(result.data(), data, width * height * 4); |
| 138 |
| 139 | // free the image data |
| 140 | imageFree(data); |
| 141 |
| 142 | return result; |
| 143 | } |
| 144 |
| 145 | VBitmap load(const char *fileName) |
| 146 | { |
| 147 | if (!imageLoad) return VBitmap(); |
| 148 |
| 149 | int width, height, n; |
| 150 | unsigned char *data = imageLoad(fileName, &width, &height, &n, 4); |
| 151 |
| 152 | if (!data) { |
| 153 | return VBitmap(); |
| 154 | } |
| 155 |
| 156 | return createBitmap(data, width, height, n); |
| 157 | } |
| 158 |
| 159 | VBitmap load(const char *imageData, int len) |
| 160 | { |
| 161 | if (!imageFromData) return VBitmap(); |
| 162 |
| 163 | int width, height, n; |
| 164 | unsigned char *data = |
| 165 | imageFromData(imageData, len, &width, &height, &n, 4); |
| 166 |
| 167 | if (!data) { |
| 168 | return VBitmap(); |
| 169 | } |
| 170 |
| 171 | return createBitmap(data, width, height, n); |
| 172 | } |
| 173 | /* |
| 174 | * convert from RGBA to BGRA and premultiply |
| 175 | */ |
| 176 | void convertToBGRAPremul(unsigned char *bits, int width, int height) |
| 177 | { |
| 178 | int pixelCount = width * height; |
| 179 | unsigned char *pix = bits; |
| 180 | for (int i = 0; i < pixelCount; i++) { |
| 181 | unsigned char r = pix[0]; |
| 182 | unsigned char g = pix[1]; |
| 183 | unsigned char b = pix[2]; |
| 184 | unsigned char a = pix[3]; |
| 185 |
| 186 | r = (r * a) / 255; |
| 187 | g = (g * a) / 255; |
| 188 | b = (b * a) / 255; |
| 189 |
| 190 | pix[0] = b; |
| 191 | pix[1] = g; |
| 192 | pix[2] = r; |
| 193 |
| 194 | pix += 4; |
| 195 | } |
| 196 | } |
| 197 | /* |
| 198 | * convert from RGBA to BGRA |
| 199 | */ |
| 200 | void convertToBGRA(unsigned char *bits, int width, int height) |
| 201 | { |
| 202 | int pixelCount = width * height; |
| 203 | unsigned char *pix = bits; |
| 204 | for (int i = 0; i < pixelCount; i++) { |
| 205 | unsigned char r = pix[0]; |
| 206 | unsigned char b = pix[2]; |
| 207 | pix[0] = b; |
| 208 | pix[2] = r; |
| 209 | pix += 4; |
| 210 | } |
| 211 | } |
| 212 | }; |
| 213 |
| 214 | VImageLoader::VImageLoader() : mImpl(std::make_unique<VImageLoader::Impl>()) {} |
| 215 |
| 216 | VImageLoader::~VImageLoader() {} |
| 217 |
| 218 | VBitmap VImageLoader::load(const char *fileName) |
| 219 | { |
| 220 | return mImpl->load(fileName); |
| 221 | } |
| 222 |
| 223 | VBitmap VImageLoader::load(const char *data, int len) |
| 224 | { |
| 225 | return mImpl->load(data, len); |
| 226 | } |
| 227 |