| 1 | // Aseprite Document IO Library |
|---|---|
| 2 | // Copyright (c) 2021 Igara Studio S.A. |
| 3 | // Copyright (c) 2017 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #include "dio/decode_file.h" |
| 9 | |
| 10 | #include "dio/aseprite_decoder.h" |
| 11 | #include "dio/decoder.h" |
| 12 | #include "dio/detect_format.h" |
| 13 | #include "dio/file_interface.h" |
| 14 | #include "doc/document.h" |
| 15 | |
| 16 | #include <cassert> |
| 17 | |
| 18 | namespace dio { |
| 19 | |
| 20 | bool decode_file(DecodeDelegate* delegate, |
| 21 | FileInterface* f) |
| 22 | { |
| 23 | assert(delegate); |
| 24 | assert(f); |
| 25 | |
| 26 | uint8_t buf[12]; |
| 27 | size_t n = f->readBytes(&buf[0], 12); |
| 28 | FileFormat format = detect_format_by_file_content_bytes(&buf[0], n); |
| 29 | f->seek(0); // Rewind |
| 30 | |
| 31 | Decoder* decoder = nullptr; |
| 32 | |
| 33 | switch (format) { |
| 34 | case FileFormat::ASE_ANIMATION: decoder = new AsepriteDecoder; break; |
| 35 | } |
| 36 | |
| 37 | bool result = false; |
| 38 | |
| 39 | if (decoder) { |
| 40 | decoder->initialize(delegate, f); |
| 41 | result = decoder->decode(); |
| 42 | delete decoder; |
| 43 | } |
| 44 | |
| 45 | return result; |
| 46 | } |
| 47 | |
| 48 | } // namespace dio |
| 49 |