Path: blob/master/tools/n64graphics_ci_dir/exoquant/exoquant.h
7861 views
/*1ExoQuant v0.723Copyright (c) 2004 Dennis Ranke45Permission is hereby granted, free of charge, to any person obtaining a copy of6this software and associated documentation files (the "Software"), to deal in7the Software without restriction, including without limitation the rights to8use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies9of the Software, and to permit persons to whom the Software is furnished to do10so, subject to the following conditions:1112The above copyright notice and this permission notice shall be included in all13copies or substantial portions of the Software.1415THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21SOFTWARE.22*/2324/******************************************************************************25* Usage:26* ------27*28* exq_data *pExq = exq_init(); // init quantizer (per image)29* exq_feed(pExq, <ptr to image>, <num of pixels); // feed pixel data (32bpp)30* exq_quantize(pExq, <num of colors>); // find palette31* exq_get_palette(pExq, <ptr to buffer>, <num of colors>); // get palette32* exq_map_image(pExq, <num of pixels>, <ptr to input>, <ptr to output>);33* or:34* exq_map_image_ordered(pExq, <width>, <height>, <input>, <output>);35* // map image to palette36* exq_free(pExq); // free memory again37*38* Notes:39* ------40*41* All 32bpp data (input data and palette data) is considered a byte stream42* of the format:43* R0 G0 B0 A0 R1 G1 B1 A1 ...44* If you want to use a different order, the easiest way to do this is to45* change the SCALE_x constants in expquant.h, as those are the only differences46* between the channels.47*48******************************************************************************/4950#ifndef __EXOQUANT_H51#define __EXOQUANT_H5253#ifdef __cplusplus54extern "C" {55#endif5657/* type definitions */58typedef double exq_float;5960typedef struct _exq_color61{62exq_float r, g, b, a;63} exq_color;6465typedef struct _exq_histogram66{67exq_color color;68unsigned char ored, ogreen, oblue, oalpha;69int palIndex;70exq_color ditherScale;71int ditherIndex[4];72int num;73struct _exq_histogram *pNext;74struct _exq_histogram *pNextInHash;75} exq_histogram;7677typedef struct _exq_node78{79exq_color dir, avg;80exq_float vdif;81exq_float err;82int num;83exq_histogram *pHistogram;84exq_histogram *pSplit;85} exq_node;8687#define EXQ_HASH_BITS 1688#define EXQ_HASH_SIZE (1 << (EXQ_HASH_BITS))8990typedef struct _exq_data91{92exq_histogram *pHash[EXQ_HASH_SIZE];93exq_node node[256];94int numColors;95int numBitsPerChannel;96int optimized;97int transparency;98} exq_data;99100/* interface */101102exq_data *exq_init();103void exq_no_transparency(exq_data *pExq);104void exq_free(exq_data *pExq);105void exq_feed(exq_data *pExq, unsigned char *pData,106int nPixels);107void exq_quantize(exq_data *pExq, int nColors);108void exq_quantize_hq(exq_data *pExq, int nColors);109void exq_quantize_ex(exq_data *pExq, int nColors, int hq);110exq_float exq_get_mean_error(exq_data *pExq);111void exq_get_palette(exq_data *pExq, unsigned char *pPal,112int nColors);113void exq_set_palette(exq_data *pExq, unsigned char *pPal,114int nColors);115void exq_map_image(exq_data *pExq, int nPixels,116unsigned char *pIn, unsigned char *pOut);117void exq_map_image_ordered(exq_data *pExq, int width,118int height, unsigned char *pIn,119unsigned char *pOut);120void exq_map_image_random(exq_data *pExq, int nPixels,121unsigned char *pIn, unsigned char *pOut);122123/* internal functions */124125void exq_map_image_dither(exq_data *pExq, int width,126int height, unsigned char *pIn,127unsigned char *pOut, int ordered);128129void exq_sum_node(exq_node *pNode);130void exq_optimize_palette(exq_data *pExp, int iter);131132unsigned char exq_find_nearest_color(exq_data *pExp, exq_color *pColor);133exq_histogram *exq_find_histogram(exq_data *pExp, unsigned char *pCol);134135void exq_sort(exq_histogram **ppHist,136exq_float (*sortfunc)(const exq_histogram *pHist));137exq_float exq_sort_by_r(const exq_histogram *pHist);138exq_float exq_sort_by_g(const exq_histogram *pHist);139exq_float exq_sort_by_b(const exq_histogram *pHist);140exq_float exq_sort_by_a(const exq_histogram *pHist);141exq_float exq_sort_by_dir(const exq_histogram *pHist);142143extern exq_color exq_sort_dir;144145#ifdef __cplusplus146}147#endif148149#endif // __EXOQUANT_H150151152