FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TF2-Source-Code/tf2_src/mathlib/imagequant.cpp at master · sr2echa/TF2-Source-Code · GitHub
sr2echa
/
TF2-Source-Code
Public
Notifications
You must be signed in to change notification settings
Fork
22
Star
81
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TF2-Source-Code
/
tf2_src
/
mathlib
/
imagequant.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
96 lines (88 loc) · 2.26 KB
Breadcrumbs
TF2-Source-Code
/
tf2_src
/
mathlib
/
imagequant.cpp
Copy path
File metadata and controls
96 lines (88 loc) · 2.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
========= Copyright Valve Corporation, All rights reserved. ============//
//
//
Purpose:
//
//
$NoKeywords: $
//
//
=============================================================================//
#
include
<
quantize.h
>
#
include
<
minmax.h
>
#
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
;y<Height;y++)
for
(x=
0
;x<Width;x++)
{
for
(c=
0
;c<
3
;c++)
NthSample
(s,y*Width+x,
N_DIMENSIONS
)->
Value
[c]=
PIXEL
(x,y,c);
//
now, let's generate extra values to quantize on
for
(
int
i=
0
;i<
N_EXTRAVALUES
;i++)
{
int
val1=
0
;
for
(c=
0
;c<
3
;c++)
val1+=
PIXEL
(x,y,c)*ExtraValueXForms[i*
3
+c];
val1>>=
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
(c=
0
;c<
3
;c++)
out_palette[p*
3
+c]=v->
Mean
[c];
}
memset
(Error,
0
,
sizeof
(Error));
for
(y=
0
;y<Height;y++)
{
int
ErrorUse=y &
1
;
int
ErrorUpdate=ErrorUse^
1
;
for
(x=
0
;x<Width;x++)
{
uint8 samp[
3
];
for
(c=
0
;c<
3
;c++)
{
int
tryc=
PIXEL
(x,y,c);
if
(! (flags &
QUANTFLAGS_NODITHER
))
{
tryc+=Error[x][c][ErrorUse];
Error[x][c][ErrorUse]=
0
;
}
samp[c]=(uint8)
min
(
255
,
max
(
0
,tryc));
}
struct
QuantizedValue
*f=
FindMatch
(samp,
3
,Weights,q);
out_pixels[Width*y+x]=(uint8) (f->
value
);
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);
}
Back
|
FazBrowse Home
|
New Git URL