1// Aseprite Document Library
2// Copyright (c) 2020 Igara Studio S.A.
3// Copyright (c) 2001-2015 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "doc/rgbmap_rgb5a3.h"
13
14#include "doc/color_scales.h"
15#include "doc/palette.h"
16
17namespace doc {
18
19#define RSIZE 32
20#define GSIZE 32
21#define BSIZE 32
22#define ASIZE 8
23#define MAPSIZE (RSIZE*GSIZE*BSIZE*ASIZE)
24
25RgbMapRGB5A3::RgbMapRGB5A3()
26 : m_map(MAPSIZE)
27 , m_palette(nullptr)
28 , m_modifications(0)
29 , m_maskIndex(0)
30{
31}
32
33void RgbMapRGB5A3::regenerateMap(const Palette* palette, int maskIndex)
34{
35 // Skip useless regenerations
36 if (m_palette == palette &&
37 m_modifications == palette->getModifications() &&
38 m_maskIndex == maskIndex)
39 return;
40
41 m_palette = palette;
42 m_modifications = palette->getModifications();
43 m_maskIndex = maskIndex;
44
45 // Mark all entries as invalid (need to be regenerated)
46 for (uint16_t& entry : m_map)
47 entry |= INVALID;
48}
49
50int RgbMapRGB5A3::generateEntry(int i, int r, int g, int b, int a) const
51{
52 return m_map[i] =
53 m_palette->findBestfit(
54 scale_5bits_to_8bits(r>>3),
55 scale_5bits_to_8bits(g>>3),
56 scale_5bits_to_8bits(b>>3),
57 scale_3bits_to_8bits(a>>5), m_maskIndex);
58}
59
60} // namespace doc
61