| 1 | // Aseprite Document Library |
| 2 | // Copyright (c) 2017-2018 David Capello |
| 3 | // |
| 4 | // This file is released under the terms of the MIT license. |
| 5 | // Read LICENSE.txt for more information. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "doc/slice_io.h" |
| 12 | |
| 13 | #include "base/serialization.h" |
| 14 | #include "doc/slice.h" |
| 15 | #include "doc/string_io.h" |
| 16 | #include "doc/user_data_io.h" |
| 17 | |
| 18 | #include <iostream> |
| 19 | #include <memory> |
| 20 | |
| 21 | namespace doc { |
| 22 | |
| 23 | using namespace base::serialization; |
| 24 | using namespace base::serialization::little_endian; |
| 25 | |
| 26 | void write_slice(std::ostream& os, const Slice* slice) |
| 27 | { |
| 28 | write32(os, slice->id()); |
| 29 | write_string(os, slice->name()); |
| 30 | write_user_data(os, slice->userData()); |
| 31 | |
| 32 | // Number of keys |
| 33 | write32(os, slice->size()); |
| 34 | for (const auto& key : *slice) { |
| 35 | write32(os, key.frame()); |
| 36 | write_slicekey(os, *key.value()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | Slice* read_slice(std::istream& is, bool setId) |
| 41 | { |
| 42 | ObjectId id = read32(is); |
| 43 | std::string name = read_string(is); |
| 44 | UserData userData = read_user_data(is); |
| 45 | size_t nkeys = read32(is); |
| 46 | |
| 47 | std::unique_ptr<Slice> slice(new Slice); |
| 48 | slice->setName(name); |
| 49 | slice->setUserData(userData); |
| 50 | while (nkeys--) { |
| 51 | frame_t fr = read32(is); |
| 52 | slice->insert(fr, read_slicekey(is)); |
| 53 | } |
| 54 | |
| 55 | if (setId) |
| 56 | slice->setId(id); |
| 57 | return slice.release(); |
| 58 | } |
| 59 | |
| 60 | void write_slicekey(std::ostream& os, const SliceKey& sliceKey) |
| 61 | { |
| 62 | write32(os, sliceKey.bounds().x); |
| 63 | write32(os, sliceKey.bounds().y); |
| 64 | write32(os, sliceKey.bounds().w); |
| 65 | write32(os, sliceKey.bounds().h); |
| 66 | write32(os, sliceKey.center().x); |
| 67 | write32(os, sliceKey.center().y); |
| 68 | write32(os, sliceKey.center().w); |
| 69 | write32(os, sliceKey.center().h); |
| 70 | write32(os, sliceKey.pivot().x); |
| 71 | write32(os, sliceKey.pivot().y); |
| 72 | } |
| 73 | |
| 74 | SliceKey read_slicekey(std::istream& is) |
| 75 | { |
| 76 | gfx::Rect bounds, center; |
| 77 | gfx::Point pivot; |
| 78 | bounds.x = read32(is); |
| 79 | bounds.y = read32(is); |
| 80 | bounds.w = read32(is); |
| 81 | bounds.h = read32(is); |
| 82 | center.x = read32(is); |
| 83 | center.y = read32(is); |
| 84 | center.w = read32(is); |
| 85 | center.h = read32(is); |
| 86 | pivot.x = read32(is); |
| 87 | pivot.y = read32(is); |
| 88 | return SliceKey(bounds, center, pivot); |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |