| 48 | } |
| 49 | |
| 50 | std::shared_ptr<ImageCodec> ImageCodec::MakeFrom(const std::string& filePath) { |
| 51 | static WeakMap<std::string, ImageCodec> imageCodecMap = {}; |
| 52 | if (filePath.empty()) { |
| 53 | return nullptr; |
| 54 | } |
| 55 | if (auto cached = imageCodecMap.find(filePath)) { |
| 56 | return cached; |
| 57 | } |
| 58 | std::shared_ptr<ImageCodec> codec = nullptr; |
| 59 | auto stream = Stream::MakeFromFile(filePath); |
| 60 | if (stream && stream->size() > 14) { |
| 61 | Buffer buffer(14); |
| 62 | if (stream->read(buffer.data(), 14) == 14) { |
| 63 | auto data = buffer.release(); |
| 64 | #ifdef TGFX_USE_WEBP_DECODE |
| 65 | if (WebpCodec::IsWebp(data)) { |
| 66 | codec = WebpCodec::MakeFrom(filePath); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | #ifdef TGFX_USE_PNG_DECODE |
| 71 | if (codec == nullptr && PngCodec::IsPng(data)) { |
| 72 | codec = PngCodec::MakeFrom(filePath); |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | #ifdef TGFX_USE_JPEG_DECODE |
| 77 | if (codec == nullptr && JpegCodec::IsJpeg(data)) { |
| 78 | codec = JpegCodec::MakeFrom(filePath); |
| 79 | } |
| 80 | #endif |
| 81 | } |
| 82 | } |
| 83 | if (codec == nullptr) { |
| 84 | codec = MakeNativeCodec(filePath); |
| 85 | } |
| 86 | if (codec && !ImageInfo::IsValidSize(codec->width(), codec->height())) { |
| 87 | codec = nullptr; |
| 88 | } |
| 89 | if (codec) { |
| 90 | imageCodecMap.insert(filePath, codec); |
| 91 | } |
| 92 | return codec; |
| 93 | } |
| 94 | |
| 95 | std::shared_ptr<ImageCodec> ImageCodec::MakeFrom(std::shared_ptr<Data> imageBytes) { |
| 96 | if (imageBytes == nullptr || imageBytes->size() == 0) { |
nothing calls this directly
no test coverage detected