| 1 | // Aseprite Document Library |
| 2 | // Copyright (c) 2001-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/mask_io.h" |
| 12 | |
| 13 | #include "base/serialization.h" |
| 14 | #include "doc/mask.h" |
| 15 | |
| 16 | #include <iostream> |
| 17 | #include <memory> |
| 18 | |
| 19 | namespace doc { |
| 20 | |
| 21 | using namespace base::serialization; |
| 22 | using namespace base::serialization::little_endian; |
| 23 | |
| 24 | // Serialized Mask data: |
| 25 | // |
| 26 | // WORD[4] x, y, w, h |
| 27 | // for each line ("h" times) |
| 28 | // for each packet ("((w+7)/8)" times) |
| 29 | // BYTE 8 pixels of the mask |
| 30 | // BYTE for Indexed images |
| 31 | |
| 32 | void write_mask(std::ostream& os, const Mask* mask) |
| 33 | { |
| 34 | const gfx::Rect& bounds = mask->bounds(); |
| 35 | |
| 36 | write16(os, bounds.x); // Xpos |
| 37 | write16(os, bounds.y); // Ypos |
| 38 | write16(os, mask->bitmap() ? bounds.w: 0); // Width |
| 39 | write16(os, mask->bitmap() ? bounds.h: 0); // Height |
| 40 | |
| 41 | if (mask->bitmap()) { |
| 42 | int size = BitmapTraits::getRowStrideBytes(bounds.w); |
| 43 | |
| 44 | for (int c=0; c<bounds.h; c++) |
| 45 | os.write((char*)mask->bitmap()->getPixelAddress(0, c), size); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Mask* read_mask(std::istream& is) |
| 50 | { |
| 51 | int x = int16_t(read16(is)); // Xpos (it's a signed int16 because we support negative mask coordinates) |
| 52 | int y = int16_t(read16(is)); // Ypos |
| 53 | int w = read16(is); // Width |
| 54 | int h = read16(is); // Height |
| 55 | |
| 56 | std::unique_ptr<Mask> mask(new Mask()); |
| 57 | |
| 58 | if (w > 0 && h > 0) { |
| 59 | int size = BitmapTraits::getRowStrideBytes(w); |
| 60 | |
| 61 | mask->add(gfx::Rect(x, y, w, h)); |
| 62 | for (int c=0; c<mask->bounds().h; c++) |
| 63 | is.read((char*)mask->bitmap()->getPixelAddress(0, c), size); |
| 64 | } |
| 65 | |
| 66 | return mask.release(); |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | |