1// Aseprite Document Library
2// Copyright (c) 2020 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#include "doc/util.h"
8
9#include "doc/image.h"
10#include "doc/image_impl.h"
11#include "doc/tileset.h"
12
13namespace doc {
14
15void fix_old_tileset(
16 Tileset* tileset)
17{
18 // Check if the first tile is already the empty tile, in this
19 // case we can use this tileset as a new tileset without any
20 // conversion.
21 if (tileset->size() > 0 && is_empty_image(tileset->get(0).get())) {
22 tileset->setBaseIndex(1);
23 }
24 else {
25 // Add the empty tile in the index = 0
26 tileset->insert(0, tileset->makeEmptyTile());
27
28 // The tile 1 will be displayed as tile 0 in the editor
29 tileset->setBaseIndex(0);
30 }
31}
32
33void fix_old_tilemap(
34 Image* image,
35 const Tileset* tileset,
36 const tile_t tileIDMask,
37 const tile_t tileFlagsMask)
38{
39 int delta = (tileset->baseIndex() == 0 ? 1: 0);
40
41 // Convert old empty tile (0xffffffff) to new empty tile (index 0 = notile)
42 transform_image<TilemapTraits>(
43 image,
44 [tileIDMask, tileFlagsMask, delta](color_t c) -> color_t {
45 color_t res = c;
46 if (c == 0xffffffff)
47 res = notile;
48 else
49 res = (c & tileFlagsMask) | ((c & tileIDMask)+delta);
50 return res;
51 });
52}
53
54} // namespace dooc
55