1 // Aseprite Document Library
2 // Copyright (c) 2001-2017 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifndef DOC_REMAP_H_INCLUDED
8 #define DOC_REMAP_H_INCLUDED
9 #pragma once
10 
11 #include "base/debug.h"
12 #include <vector>
13 
14 namespace doc {
15 
16   class Palette;
17   class PalettePicks;
18 
19   class Remap {
20   public:
21     Remap(int entries = 1) : m_map(entries, 0) { }
22 
size()23     int size() const {
24       return (int)m_map.size();
25     }
26 
27     // Maps input "fromIndex" value, to "toIndex" output.
map(int fromIndex,int toIndex)28     void map(int fromIndex, int toIndex) {
29       ASSERT(fromIndex >= 0 && fromIndex < size());
30       ASSERT(toIndex >= 0 && toIndex < size());
31 
32       m_map[fromIndex] = toIndex;
33     }
34 
35     int operator[](int index) const {
36       //ASSERT(index >= 0 && index < size());
37       if (index >= 0 && index < size())
38         return m_map[index];
39       else
40         return index;           // No remap
41     }
42 
43     void merge(const Remap& other);
44     Remap invert() const;
45 
getMemSize()46     int getMemSize() const {
47       return sizeof(*this) + sizeof(int)*size();
48     }
49 
50     // Returns true if the remap can be safely used in 8-bit images.
51     bool isFor8bit() const;
52 
53     // Returns true if the remap can be inverted. Remaps can be
54     // inverted when each input is mapped to one output (e.g. two
55     // inputs are not mapped to the same output). This kind of remaps
56     // are really easy to undone: You can store the inverted remap as
57     // undo data, without saving all images' pixels.
58     bool isInvertible(const PalettePicks& usedEntries) const;
59 
60   private:
61     std::vector<int> m_map;
62   };
63 
64   // Creates a map to move a set of selected entries before the given
65   // index "beforeIndex".
66   Remap create_remap_to_move_picks(const PalettePicks& picks, int beforeIndex);
67 
68   Remap create_remap_to_expand_palette(int size, int count, int beforeIndex);
69 
70   Remap create_remap_to_change_palette(
71     const Palette* oldPalette, const Palette* newPalette,
72     const int oldMaskIndex,
73     const bool remapMaskIndex);
74 
75 } // namespace doc
76 
77 #endif
78