Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MorsGames
GitHub Repository: MorsGames/sm64plus
Path: blob/master/tools/n64graphics_ci_dir/exoquant/exoquant.h
7861 views
1
/*
2
ExoQuant v0.7
3
4
Copyright (c) 2004 Dennis Ranke
5
6
Permission is hereby granted, free of charge, to any person obtaining a copy of
7
this software and associated documentation files (the "Software"), to deal in
8
the Software without restriction, including without limitation the rights to
9
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10
of the Software, and to permit persons to whom the Software is furnished to do
11
so, subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
SOFTWARE.
23
*/
24
25
/******************************************************************************
26
* Usage:
27
* ------
28
*
29
* exq_data *pExq = exq_init(); // init quantizer (per image)
30
* exq_feed(pExq, <ptr to image>, <num of pixels); // feed pixel data (32bpp)
31
* exq_quantize(pExq, <num of colors>); // find palette
32
* exq_get_palette(pExq, <ptr to buffer>, <num of colors>); // get palette
33
* exq_map_image(pExq, <num of pixels>, <ptr to input>, <ptr to output>);
34
* or:
35
* exq_map_image_ordered(pExq, <width>, <height>, <input>, <output>);
36
* // map image to palette
37
* exq_free(pExq); // free memory again
38
*
39
* Notes:
40
* ------
41
*
42
* All 32bpp data (input data and palette data) is considered a byte stream
43
* of the format:
44
* R0 G0 B0 A0 R1 G1 B1 A1 ...
45
* If you want to use a different order, the easiest way to do this is to
46
* change the SCALE_x constants in expquant.h, as those are the only differences
47
* between the channels.
48
*
49
******************************************************************************/
50
51
#ifndef __EXOQUANT_H
52
#define __EXOQUANT_H
53
54
#ifdef __cplusplus
55
extern "C" {
56
#endif
57
58
/* type definitions */
59
typedef double exq_float;
60
61
typedef struct _exq_color
62
{
63
exq_float r, g, b, a;
64
} exq_color;
65
66
typedef struct _exq_histogram
67
{
68
exq_color color;
69
unsigned char ored, ogreen, oblue, oalpha;
70
int palIndex;
71
exq_color ditherScale;
72
int ditherIndex[4];
73
int num;
74
struct _exq_histogram *pNext;
75
struct _exq_histogram *pNextInHash;
76
} exq_histogram;
77
78
typedef struct _exq_node
79
{
80
exq_color dir, avg;
81
exq_float vdif;
82
exq_float err;
83
int num;
84
exq_histogram *pHistogram;
85
exq_histogram *pSplit;
86
} exq_node;
87
88
#define EXQ_HASH_BITS 16
89
#define EXQ_HASH_SIZE (1 << (EXQ_HASH_BITS))
90
91
typedef struct _exq_data
92
{
93
exq_histogram *pHash[EXQ_HASH_SIZE];
94
exq_node node[256];
95
int numColors;
96
int numBitsPerChannel;
97
int optimized;
98
int transparency;
99
} exq_data;
100
101
/* interface */
102
103
exq_data *exq_init();
104
void exq_no_transparency(exq_data *pExq);
105
void exq_free(exq_data *pExq);
106
void exq_feed(exq_data *pExq, unsigned char *pData,
107
int nPixels);
108
void exq_quantize(exq_data *pExq, int nColors);
109
void exq_quantize_hq(exq_data *pExq, int nColors);
110
void exq_quantize_ex(exq_data *pExq, int nColors, int hq);
111
exq_float exq_get_mean_error(exq_data *pExq);
112
void exq_get_palette(exq_data *pExq, unsigned char *pPal,
113
int nColors);
114
void exq_set_palette(exq_data *pExq, unsigned char *pPal,
115
int nColors);
116
void exq_map_image(exq_data *pExq, int nPixels,
117
unsigned char *pIn, unsigned char *pOut);
118
void exq_map_image_ordered(exq_data *pExq, int width,
119
int height, unsigned char *pIn,
120
unsigned char *pOut);
121
void exq_map_image_random(exq_data *pExq, int nPixels,
122
unsigned char *pIn, unsigned char *pOut);
123
124
/* internal functions */
125
126
void exq_map_image_dither(exq_data *pExq, int width,
127
int height, unsigned char *pIn,
128
unsigned char *pOut, int ordered);
129
130
void exq_sum_node(exq_node *pNode);
131
void exq_optimize_palette(exq_data *pExp, int iter);
132
133
unsigned char exq_find_nearest_color(exq_data *pExp, exq_color *pColor);
134
exq_histogram *exq_find_histogram(exq_data *pExp, unsigned char *pCol);
135
136
void exq_sort(exq_histogram **ppHist,
137
exq_float (*sortfunc)(const exq_histogram *pHist));
138
exq_float exq_sort_by_r(const exq_histogram *pHist);
139
exq_float exq_sort_by_g(const exq_histogram *pHist);
140
exq_float exq_sort_by_b(const exq_histogram *pHist);
141
exq_float exq_sort_by_a(const exq_histogram *pHist);
142
exq_float exq_sort_by_dir(const exq_histogram *pHist);
143
144
extern exq_color exq_sort_dir;
145
146
#ifdef __cplusplus
147
}
148
#endif
149
150
#endif // __EXOQUANT_H
151
152