| 1 | /* |
| 2 | * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. |
| 3 | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | |
| 11 | * The above copyright notice and this permission notice shall be included in all |
| 12 | * copies or substantial portions of the Software. |
| 13 | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <memory.h> |
| 24 | #include <webp/decode.h> |
| 25 | |
| 26 | #include "tvgWebpLoader.h" |
| 27 | |
| 28 | |
| 29 | /************************************************************************/ |
| 30 | /* Internal Class Implementation */ |
| 31 | /************************************************************************/ |
| 32 | |
| 33 | |
| 34 | void WebpLoader::run(unsigned tid) |
| 35 | { |
| 36 | //TODO: acquire the current colorspace format & pre-multiplied alpha image. |
| 37 | surface.buf8 = WebPDecodeBGRA(data, size, nullptr, nullptr); |
| 38 | surface.stride = (uint32_t)w; |
| 39 | surface.w = (uint32_t)w; |
| 40 | surface.h = (uint32_t)h; |
| 41 | surface.channelSize = sizeof(uint32_t); |
| 42 | surface.cs = ColorSpace::ARGB8888; |
| 43 | surface.premultiplied = false; |
| 44 | |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /************************************************************************/ |
| 49 | /* External Class Implementation */ |
| 50 | /************************************************************************/ |
| 51 | |
| 52 | WebpLoader::WebpLoader() : ImageLoader(FileType::Webp) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | |
| 57 | WebpLoader::~WebpLoader() |
| 58 | { |
| 59 | done(); |
| 60 | |
| 61 | if (freeData) free(data); |
| 62 | data = nullptr; |
| 63 | size = 0; |
| 64 | freeData = false; |
| 65 | WebPFree(surface.buf8); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | bool WebpLoader::open(const string& path) |
| 70 | { |
| 71 | #ifdef THORVG_FILE_IO_SUPPORT |
| 72 | auto webpFile = fopen(path.c_str(), "rb"); |
| 73 | if (!webpFile) return false; |
| 74 | |
| 75 | auto ret = false; |
| 76 | |
| 77 | //determine size |
| 78 | if (fseek(webpFile, 0, SEEK_END) < 0) goto finalize; |
| 79 | if (((size = ftell(webpFile)) < 1)) goto finalize; |
| 80 | if (fseek(webpFile, 0, SEEK_SET)) goto finalize; |
| 81 | |
| 82 | data = (unsigned char *) malloc(size); |
| 83 | if (!data) goto finalize; |
| 84 | |
| 85 | freeData = true; |
| 86 | |
| 87 | if (fread(data, size, 1, webpFile) < 1) goto finalize; |
| 88 | |
| 89 | int width, height; |
| 90 | if (!WebPGetInfo(data, size, &width, &height)) goto finalize; |
| 91 | |
| 92 | w = static_cast<float>(width); |
| 93 | h = static_cast<float>(height); |
| 94 | |
| 95 | ret = true; |
| 96 | |
| 97 | finalize: |
| 98 | fclose(webpFile); |
| 99 | return ret; |
| 100 | #else |
| 101 | return false; |
| 102 | #endif |
| 103 | } |
| 104 | |
| 105 | |
| 106 | bool WebpLoader::open(const char* data, uint32_t size, bool copy) |
| 107 | { |
| 108 | if (copy) { |
| 109 | this->data = (unsigned char *) malloc(size); |
| 110 | if (!this->data) return false; |
| 111 | memcpy((unsigned char *)this->data, data, size); |
| 112 | freeData = true; |
| 113 | } else { |
| 114 | this->data = (unsigned char *) data; |
| 115 | freeData = false; |
| 116 | } |
| 117 | |
| 118 | int width, height; |
| 119 | if (!WebPGetInfo(this->data, size, &width, &height)) return false; |
| 120 | |
| 121 | w = static_cast<float>(width); |
| 122 | h = static_cast<float>(height); |
| 123 | surface.cs = ColorSpace::ARGB8888; |
| 124 | this->size = size; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | bool WebpLoader::read() |
| 130 | { |
| 131 | if (!LoadModule::read()) return true; |
| 132 | |
| 133 | if (!data || w == 0 || h == 0) return false; |
| 134 | |
| 135 | TaskScheduler::request(this); |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | RenderSurface* WebpLoader::bitmap() |
| 142 | { |
| 143 | this->done(); |
| 144 | |
| 145 | return ImageLoader::bitmap(); |
| 146 | } |