From 39ed87570bdb2f86969d4be821c94b722dc71179 Mon Sep 17 00:00:00 2001 From: Joe Ludwig Date: Wed, 26 Jun 2013 15:22:04 -0700 Subject: First version of the SOurce SDK 2013 --- mp/src/mathlib/imagequant.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 mp/src/mathlib/imagequant.cpp (limited to 'mp/src/mathlib/imagequant.cpp') diff --git a/mp/src/mathlib/imagequant.cpp b/mp/src/mathlib/imagequant.cpp new file mode 100644 index 00000000..fddafa74 --- /dev/null +++ b/mp/src/mathlib/imagequant.cpp @@ -0,0 +1,96 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +// +//=============================================================================// +#include +#include + +#define N_EXTRAVALUES 1 +#define N_DIMENSIONS (3+N_EXTRAVALUES) + +#define PIXEL(x,y,c) Image[4*((x)+((Width*(y))))+c] + +static uint8 Weights[]={5,7,4,8}; +static int ExtraValueXForms[3*N_EXTRAVALUES]={ + 76,151,28, +}; + + + +#define MAX_QUANTIZE_IMAGE_WIDTH 4096 + +void ColorQuantize(uint8 const *Image, + int Width, + int Height, + int flags, int ncolors, + uint8 *out_pixels, + uint8 *out_palette, + int firstcolor) +{ + int Error[MAX_QUANTIZE_IMAGE_WIDTH+1][3][2]; + struct Sample *s=AllocSamples(Width*Height,N_DIMENSIONS); + int x,y,c; + for(y=0;yValue[c]=PIXEL(x,y,c); + // now, let's generate extra values to quantize on + for(int i=0;i>=8; + NthSample(s,y*Width+x,N_DIMENSIONS)->Value[c]=(uint8) + (min(255,max(0,val1))); + } + } + struct QuantizedValue *q=Quantize(s,Width*Height,N_DIMENSIONS, + ncolors,Weights,firstcolor); + delete[] s; + memset(out_palette,0x55,768); + for(int p=0;p<256;p++) + { + struct QuantizedValue *v=FindQNode(q,p); + if (v) + for(int c=0;c<3;c++) + out_palette[p*3+c]=v->Mean[c]; + } + memset(Error,0,sizeof(Error)); + for(y=0;yvalue); + if (! (flags & QUANTFLAGS_NODITHER)) + for(int i=0;i<3;i++) + { + int newerr=samp[i]-f->Mean[i]; + int orthog_error=(newerr*3)/8; + Error[x+1][i][ErrorUse]+=orthog_error; + Error[x][i][ErrorUpdate]=orthog_error; + Error[x+1][i][ErrorUpdate]=newerr-2*orthog_error; + } + } + } + if (q) FreeQuantization(q); +} + -- cgit v1.2.3