Super Mario 64 Source
A Super Mario 64 decompilation, brought to you by a bunch of clever folks.
exoquant.h
Go to the documentation of this file.
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 {
69  unsigned char ored, ogreen, oblue, oalpha;
70  int palIndex;
72  int ditherIndex[4];
73  int num;
77 
78 typedef struct _exq_node
79 {
80  exq_color dir, avg;
81  exq_float vdif;
82  exq_float err;
83  int num;
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 {
94  exq_node node[256];
95  int numColors;
97  int optimized;
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
exq_histogram * pHistogram
Definition: exoquant.h:84
exq_float g
Definition: exoquant.h:63
#define EXQ_HASH_SIZE
Definition: exoquant.h:89
struct _exq_histogram * pNextInHash
Definition: exoquant.h:75
exq_float a
Definition: exoquant.h:63
void exq_optimize_palette(exq_data *pExp, int iter)
Definition: exoquant.c:415
int num
Definition: exoquant.h:83
int numBitsPerChannel
Definition: exoquant.h:96
void exq_get_palette(exq_data *pExq, unsigned char *pPal, int nColors)
Definition: exoquant.c:232
void exq_map_image_random(exq_data *pExq, int nPixels, unsigned char *pIn, unsigned char *pOut)
Definition: exoquant.c:484
exq_data * exq_init()
Definition: exoquant.c:40
exq_float exq_sort_by_r(const exq_histogram *pHist)
Definition: exoquant.c:680
void exq_quantize_ex(exq_data *pExq, int nColors, int hq)
Definition: exoquant.c:148
unsigned char ored
Definition: exoquant.h:69
void exq_no_transparency(exq_data *pExq)
Definition: exoquant.c:58
int transparency
Definition: exoquant.h:98
void exq_quantize(exq_data *pExq, int nColors)
Definition: exoquant.c:138
void exq_feed(exq_data *pExq, unsigned char *pData, int nPixels)
Definition: exoquant.c:89
double exq_float
Definition: exoquant.h:59
exq_float exq_sort_by_dir(const exq_histogram *pHist)
Definition: exoquant.c:702
exq_color ditherScale
Definition: exoquant.h:71
exq_float err
Definition: exoquant.h:82
void exq_free(exq_data *pExq)
Definition: exoquant.c:63
struct _exq_color exq_color
unsigned char exq_find_nearest_color(exq_data *pExp, exq_color *pColor)
Definition: exoquant.c:602
struct _exq_histogram * pNext
Definition: exoquant.h:74
void exq_map_image_ordered(exq_data *pExq, int width, int height, unsigned char *pIn, unsigned char *pOut)
Definition: exoquant.c:478
struct _exq_histogram exq_histogram
exq_float r
Definition: exoquant.h:63
Definition: exoquant.h:91
exq_float exq_sort_by_b(const exq_histogram *pHist)
Definition: exoquant.c:690
int num
Definition: exoquant.h:73
struct _exq_data exq_data
void exq_set_palette(exq_data *pExq, unsigned char *pPal, int nColors)
Definition: exoquant.c:267
exq_color color
Definition: exoquant.h:68
Definition: exoquant.h:78
exq_float exq_sort_by_g(const exq_histogram *pHist)
Definition: exoquant.c:685
exq_float exq_get_mean_error(exq_data *pExq)
Definition: exoquant.c:216
void exq_map_image_dither(exq_data *pExq, int width, int height, unsigned char *pIn, unsigned char *pOut, int ordered)
Definition: exoquant.c:490
int optimized
Definition: exoquant.h:97
exq_histogram * exq_find_histogram(exq_data *pExp, unsigned char *pCol)
Definition: exoquant.c:585
exq_color exq_sort_dir
Definition: exoquant.c:700
int palIndex
Definition: exoquant.h:70
exq_color dir
Definition: exoquant.h:80
exq_float b
Definition: exoquant.h:63
exq_float vdif
Definition: exoquant.h:81
exq_float exq_sort_by_a(const exq_histogram *pHist)
Definition: exoquant.c:695
exq_histogram * pSplit
Definition: exoquant.h:85
Definition: exoquant.h:66
void exq_sum_node(exq_node *pNode)
Definition: exoquant.c:284
Definition: exoquant.h:61
void exq_map_image(exq_data *pExq, int nPixels, unsigned char *pIn, unsigned char *pOut)
Definition: exoquant.c:440
void exq_sort(exq_histogram **ppHist, exq_float(*sortfunc)(const exq_histogram *pHist))
Definition: exoquant.c:626
int numColors
Definition: exoquant.h:95
struct _exq_node exq_node
void exq_quantize_hq(exq_data *pExq, int nColors)
Definition: exoquant.c:143