summaryrefslogtreecommitdiff
path: root/dxr3palettemanager.c
blob: 745e0301928e08b98d9c204600ca01ab4e6be07f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * dxr3palettemanager.c:
 *
 * See the main source file 'dxr3.c' for copyright information and
 * how to reach the author.
 *
 */ 

/*
	ToDo:
		- cDxr3PaletteManager: Should we use here std::vector?
*/

#include <string.h> 
#include "dxr3palettemanager.h"
#include "dxr3tools.h"

// ================================== 
//! constructor
cDxr3PaletteManager::cDxr3PaletteManager() 
{
    memset(m_colors, 0, sizeof(int) * MAX_COLORS);
    memset(m_users, 0, sizeof(int) * MAX_COLORS);
    memset(m_pal, 0, sizeof(int) * MAX_COLORS);
    m_changed = false;
};

// ==================================
void cDxr3PaletteManager::AddColor(int color) 
{
    int freeIndex = MAX_COLORS;
    bool found = false;

    for (int i = 0; i < MAX_COLORS && !found; ++i) 
	{
        if (color == m_colors[i]) 
		{
            if (m_users[i] == 0) m_changed = true;
            ++m_users[i];
            found = true;
        }
        if (m_users[i] == 0 && freeIndex >= MAX_COLORS) 
		{
            freeIndex = i;
        }
    }
    if (!found && freeIndex < MAX_COLORS) 
	{
        m_colors[freeIndex] = color;
        m_users[freeIndex] = 1;
        m_changed = true;
    }
}

// ==================================
void cDxr3PaletteManager::RemoveColor(int color) 
{
    bool found = false;
    for (int i = 0; i < MAX_COLORS && !found; ++i) 
	{
        if (color == m_colors[i]) 
		{
            if (m_users[i] > 0) --m_users[i];
            found = true;
        }
    }
}

// ==================================
int cDxr3PaletteManager::GetIndex(int color) 
{
    bool found = false;
    int index = 0;
    for (int i = 0; i < MAX_COLORS && !found; ++i) 
	{
        if (color == m_colors[i]) 
		{
            index = i;
            found = true;
        }
    }
    return index; 
}

// ==================================
int cDxr3PaletteManager::GetCount() 
{
    return MAX_COLORS;
}

// ==================================
int cDxr3PaletteManager::operator[](int index) 
{
    assert(index < MAX_COLORS && index > 0);
    return m_colors[index];
}

// ==================================
bool cDxr3PaletteManager::HasChanged() 
{
    bool retval = m_changed;
    m_changed = false;
    return retval;
}

// ==================================
uint32_t* cDxr3PaletteManager::GetPalette() 
{   
    for (int i = 0; i < MAX_COLORS; ++i) 
	{
        m_pal[i] = Tools::Rgb2YCrCb(m_colors[i]);
    }
    
    return m_pal;
}