1// Aseprite Document Library
2// Copyright (C) 2019 Igara Studio S.A.
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/grid_io.h"
12
13#include "base/serialization.h"
14#include "doc/image.h"
15#include "doc/image_io.h"
16#include "doc/grid.h"
17
18#include <iostream>
19
20namespace doc {
21
22using namespace base::serialization;
23using namespace base::serialization::little_endian;
24
25bool write_grid(std::ostream& os, const Grid& grid)
26{
27 write32(os, grid.tileSize().w);
28 write32(os, grid.tileSize().h);
29 write32(os, grid.origin().x);
30 write32(os, grid.origin().y);
31 write32(os, grid.tileCenter().x);
32 write32(os, grid.tileCenter().y);
33 write32(os, grid.tileOffset().x);
34 write32(os, grid.tileOffset().y);
35 write32(os, grid.oddRowOffset().x);
36 write32(os, grid.oddRowOffset().y);
37 write32(os, grid.oddColOffset().x);
38 write32(os, grid.oddColOffset().y);
39 write8(os, grid.hasMask());
40 if (grid.hasMask())
41 return write_image(os, grid.mask().get());
42 else
43 return true;
44}
45
46Grid read_grid(std::istream& is, bool setId)
47{
48 gfx::Size tileSize;
49 gfx::Point origin;
50 gfx::Point tileCenter;
51 gfx::Point tileOffset;
52 gfx::Point oddRowOffset;
53 gfx::Point oddColOffset;
54 tileSize.w = read32(is);
55 tileSize.h = read32(is);
56 origin.x = read32(is);
57 origin.y = read32(is);
58 tileCenter.x = read32(is);
59 tileCenter.y = read32(is);
60 tileOffset.x = read32(is);
61 tileOffset.y = read32(is);
62 oddRowOffset.x = read32(is);
63 oddRowOffset.y = read32(is);
64 oddColOffset.x = read32(is);
65 oddColOffset.y = read32(is);
66 bool hasMask = read8(is);
67
68 Grid grid;
69 grid.tileSize(tileSize);
70 grid.origin(origin);
71 grid.tileCenter(tileCenter);
72 grid.tileOffset(tileOffset);
73 grid.oddRowOffset(oddRowOffset);
74 grid.oddColOffset(oddColOffset);
75
76 if (hasMask) {
77 ImageRef mask(read_image(is));
78 if (mask)
79 grid.setMask(mask);
80 }
81 return grid;
82}
83
84} // namespace doc
85