forked from Xavierhorwood/team-comtress-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantize.cpp
679 lines (614 loc) · 14.7 KB
/
quantize.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#ifndef STDIO_H
#include <stdio.h>
#endif
#ifndef STRING_H
#include <string.h>
#endif
#ifndef QUANTIZE_H
#include <quantize.h>
#endif
#include <stdlib.h>
#include <minmax.h>
#include <math.h>
static int current_ndims;
static struct QuantizedValue *current_root;
static int current_ssize;
static uint8 *current_weights;
double SquaredError;
#define SPLIT_THEN_SORT 1
#define SQ(x) ((x)*(x))
static struct QuantizedValue *AllocQValue(void)
{
struct QuantizedValue *ret=new QuantizedValue;
ret->Samples=0;
ret->Children[0]=ret->Children[1]=0;
ret->NSamples=0;
ret->ErrorMeasure=new double[current_ndims];
ret->Mean=new uint8[current_ndims];
ret->Mins=new uint8[current_ndims];
ret->Maxs=new uint8[current_ndims];
ret->Sums=new int [current_ndims];
memset(ret->Sums,0,sizeof(int)*current_ndims);
ret->NQuant=0;
ret->sortdim=-1;
return ret;
}
void FreeQuantization(struct QuantizedValue *t)
{
if (t)
{
delete[] t->ErrorMeasure;
delete[] t->Mean;
delete[] t->Mins;
delete[] t->Maxs;
FreeQuantization(t->Children[0]);
FreeQuantization(t->Children[1]);
delete[] t->Sums;
delete[] t;
}
}
static int QNumSort(void const *a, void const *b)
{
int32 as=((struct Sample *) a)->QNum;
int32 bs=((struct Sample *) b)->QNum;
if (as==bs) return 0;
return (as>bs)?1:-1;
}
#if SPLIT_THEN_SORT
#else
static int current_sort_dim;
static int samplesort(void const *a, void const *b)
{
uint8 as=((struct Sample *) a)->Value[current_sort_dim];
uint8 bs=((struct Sample *) b)->Value[current_sort_dim];
if (as==bs) return 0;
return (as>bs)?1:-1;
}
#endif
static int sortlong(void const *a, void const *b)
{
// treat the entire vector of values as a long integer for duplicate removal.
return memcmp(((struct Sample *) a)->Value,
((struct Sample *) b)->Value,current_ndims);
}
#define NEXTSAMPLE(s) ( (struct Sample *) (((uint8 *) s)+current_ssize))
#define SAMPLE(s,i) NthSample(s,i,current_ndims)
static void SetNDims(int n)
{
current_ssize=sizeof(struct Sample)+(n-1);
current_ndims=n;
}
int CompressSamples(struct Sample *s, int nsamples, int ndims)
{
SetNDims(ndims);
qsort(s,nsamples,current_ssize,sortlong);
// now, they are all sorted by treating all dimensions as a large number.
// we may now remove duplicates.
struct Sample *src=s;
struct Sample *dst=s;
struct Sample *lastdst=dst;
dst=NEXTSAMPLE(dst); // copy first sample to get the ball rolling
src=NEXTSAMPLE(src);
int noutput=1;
while(--nsamples) // while some remain
{
if (memcmp(src->Value,lastdst->Value,current_ndims))
{
// yikes, a difference has been found!
memcpy(dst,src,current_ssize);
lastdst=dst;
dst=NEXTSAMPLE(dst);
noutput++;
}
else
lastdst->Count++;
src=NEXTSAMPLE(src);
}
return noutput;
}
void PrintSamples(struct Sample const *s, int nsamples, int ndims)
{
SetNDims(ndims);
int cnt=0;
while(nsamples--)
{
printf("sample #%d, count=%d, values=\n { ",cnt++,s->Count);
for(int d=0;d<ndims;d++)
printf("%02x,",s->Value[d]);
printf("}\n");
s=NEXTSAMPLE(s);
}
}
void PrintQTree(struct QuantizedValue const *p,int idlevel)
{
int i;
if (p)
{
for(i=0;i<idlevel;i++)
printf(" ");
printf("node=%p NSamples=%d value=%d Mean={",p,p->NSamples,p->value);
for(i=0;i<current_ndims;i++)
printf("%x,",p->Mean[i]);
printf("}\n");
for(i=0;i<idlevel;i++)
printf(" ");
printf("Errors={");
for(i=0;i<current_ndims;i++)
printf("%f,",p->ErrorMeasure[i]);
printf("}\n");
for(i=0;i<idlevel;i++)
printf(" ");
printf("Mins={");
for(i=0;i<current_ndims;i++)
printf("%d,",p->Mins[i]);
printf("} Maxs={");
for(i=0;i<current_ndims;i++)
printf("%d,",p->Maxs[i]);
printf("}\n");
PrintQTree(p->Children[0],idlevel+2);
PrintQTree(p->Children[1],idlevel+2);
}
}
static void UpdateStats(struct QuantizedValue *v)
{
// first, find mean
int32 Means[MAXDIMS];
double Errors[MAXDIMS];
double WorstError[MAXDIMS];
int i,j;
memset(Means,0,sizeof(Means));
int N=0;
for(i=0;i<v->NSamples;i++)
{
struct Sample *s=SAMPLE(v->Samples,i);
N+=s->Count;
for(j=0;j<current_ndims;j++)
{
uint8 val=s->Value[j];
Means[j]+=val*s->Count;
}
}
for(j=0;j<current_ndims;j++)
{
if (N) v->Mean[j]=(uint8) (Means[j]/N);
Errors[j]=WorstError[j]=0.;
}
for(i=0;i<v->NSamples;i++)
{
struct Sample *s=SAMPLE(v->Samples,i);
double c=s->Count;
for(j=0;j<current_ndims;j++)
{
double diff=SQ(s->Value[j]-v->Mean[j]);
Errors[j]+=c*diff; // charles uses abs not sq()
if (diff>WorstError[j])
WorstError[j]=diff;
}
}
v->TotalError=0.;
double ErrorScale=1.; // /sqrt((double) (N));
for(j=0;j<current_ndims;j++)
{
v->ErrorMeasure[j]=(ErrorScale*Errors[j]*current_weights[j]);
v->TotalError+=v->ErrorMeasure[j];
#if SPLIT_THEN_SORT
v->ErrorMeasure[j]*=WorstError[j];
#endif
}
v->TotSamples=N;
}
static int ErrorDim;
static double ErrorVal;
static struct QuantizedValue *ErrorNode;
static void UpdateWorst(struct QuantizedValue *q)
{
if (q->Children[0])
{
// not a leaf node
UpdateWorst(q->Children[0]);
UpdateWorst(q->Children[1]);
}
else
{
if (q->TotalError>ErrorVal)
{
ErrorVal=q->TotalError;
ErrorNode=q;
ErrorDim=0;
for(int d=0;d<current_ndims;d++)
if (q->ErrorMeasure[d]>q->ErrorMeasure[ErrorDim])
ErrorDim=d;
}
}
}
static int FindWorst(void)
{
ErrorVal=-1.;
UpdateWorst(current_root);
return (ErrorVal>0);
}
static void SubdivideNode(struct QuantizedValue *n, int whichdim)
{
int NAdded=0;
int i;
#if SPLIT_THEN_SORT
// we will try the "split then sort" method. This works by finding the
// means for all samples above and below the mean along the given axis.
// samples are then split into two groups, with the selection based upon
// which of the n-dimensional means the sample is closest to.
double LocalMean[MAXDIMS][2];
int totsamps[2];
for(i=0;i<current_ndims;i++)
LocalMean[i][0]=LocalMean[i][1]=0.;
totsamps[0]=totsamps[1]=0;
uint8 minv=255;
uint8 maxv=0;
struct Sample *minS=0,*maxS=0;
for(i=0;i<n->NSamples;i++)
{
uint8 v;
int whichside=1;
struct Sample *sl;
sl=SAMPLE(n->Samples,i);
v=sl->Value[whichdim];
if (v<minv) { minv=v; minS=sl; }
if (v>maxv) { maxv=v; maxS=sl; }
if (v<n->Mean[whichdim])
whichside=0;
totsamps[whichside]+=sl->Count;
for(int d=0;d<current_ndims;d++)
LocalMean[d][whichside]+=
sl->Count*sl->Value[d];
}
if (totsamps[0] && totsamps[1])
for(i=0;i<current_ndims;i++)
{
LocalMean[i][0]/=totsamps[0];
LocalMean[i][1]/=totsamps[1];
}
else
{
// it is possible that the clustering failed to split the samples.
// this can happen with a heavily biased sample (i.e. all black
// with a few stars). If this happens, we will cluster around the
// extrema instead. LocalMean[i][0] will be the point with the lowest
// value on the dimension and LocalMean[i][1] the one with the lowest
// value.
for(i=0;i<current_ndims;i++)
{
LocalMean[i][0]=minS->Value[i];
LocalMean[i][1]=maxS->Value[i];
}
}
// now, we have 2 n-dimensional means. We will label each sample
// for which one it is nearer to by using the QNum field.
for(i=0;i<n->NSamples;i++)
{
double dist[2];
dist[0]=dist[1]=0.;
struct Sample *s=SAMPLE(n->Samples,i);
for(int d=0;d<current_ndims;d++)
for(int w=0;w<2;w++)
dist[w]+=current_weights[d]*SQ(LocalMean[d][w]-s->Value[d]);
s->QNum=(dist[0]<dist[1]);
}
// hey ho! we have now labelled each one with a candidate bin. Let's
// sort the array by moving the 0-labelled ones to the head of the array.
n->sortdim=-1;
qsort(n->Samples,n->NSamples,current_ssize,QNumSort);
for(i=0;i<n->NSamples;i++,NAdded++)
if (SAMPLE(n->Samples,i)->QNum)
break;
#else
if (whichdim != n->sortdim)
{
current_sort_dim=whichdim;
qsort(n->Samples,n->NSamples,current_ssize,samplesort);
n->sortdim=whichdim;
}
// now, the samples are sorted along the proper dimension. we need
// to find the place to cut in order to split the node. this is
// complicated by the fact that each sample entry can represent many
// samples. What we will do is start at the beginning of the array,
// adding samples to the first node, until either the number added
// is >=TotSamples/2, or there is only one left.
int TotAdded=0;
for(;;)
{
if (NAdded==n->NSamples-1)
break;
if (TotAdded>=n->TotSamples/2)
break;
TotAdded+=SAMPLE(n->Samples,NAdded)->Count;
NAdded++;
}
#endif
struct QuantizedValue *a=AllocQValue();
a->sortdim=n->sortdim;
a->Samples=n->Samples;
a->NSamples=NAdded;
n->Children[0]=a;
UpdateStats(a);
a=AllocQValue();
a->Samples=SAMPLE(n->Samples,NAdded);
a->NSamples=n->NSamples-NAdded;
a->sortdim=n->sortdim;
n->Children[1]=a;
UpdateStats(a);
}
static int colorid=0;
static void Label(struct QuantizedValue *q, int updatecolor)
{
// fill in max/min values for tree, etc.
if (q)
{
Label(q->Children[0],updatecolor);
Label(q->Children[1],updatecolor);
if (! q->Children[0]) // leaf node?
{
if (updatecolor)
{
q->value=colorid++;
for(int j=0;j<q->NSamples;j++)
{
SAMPLE(q->Samples,j)->QNum=q->value;
SAMPLE(q->Samples,j)->qptr=q;
}
}
for(int i=0;i<current_ndims;i++)
{
q->Mins[i]=q->Mean[i];
q->Maxs[i]=q->Mean[i];
}
}
else
for(int i=0;i<current_ndims;i++)
{
q->Mins[i]=min(q->Children[0]->Mins[i],q->Children[1]->Mins[i]);
q->Maxs[i]=max(q->Children[0]->Maxs[i],q->Children[1]->Maxs[i]);
}
}
}
struct QuantizedValue *FindQNode(struct QuantizedValue const *q, int32 code)
{
if (! (q->Children[0]))
if (code==q->value) return (struct QuantizedValue *) q;
else return 0;
else
{
struct QuantizedValue *found=FindQNode(q->Children[0],code);
if (! found) found=FindQNode(q->Children[1],code);
return found;
}
}
void CheckInRange(struct QuantizedValue *q, uint8 *max, uint8 *min)
{
if (q)
{
if (q->Children[0])
{
// non-leaf node
CheckInRange(q->Children[0],q->Maxs, q->Mins);
CheckInRange(q->Children[1],q->Maxs, q->Mins);
CheckInRange(q->Children[0],max, min);
CheckInRange(q->Children[1],max, min);
}
for (int i=0;i<current_ndims;i++)
{
if (q->Maxs[i]>max[i]) printf("error1\n");
if (q->Mins[i]<min[i]) printf("error2\n");
}
}
}
struct QuantizedValue *Quantize(struct Sample *s, int nsamples, int ndims,
int nvalues, uint8 *weights, int firstvalue)
{
SetNDims(ndims);
current_weights=weights;
current_root=AllocQValue();
current_root->Samples=s;
current_root->NSamples=nsamples;
UpdateStats(current_root);
while(--nvalues)
{
if (! FindWorst())
break; // if <n unique ones, stop now
SubdivideNode(ErrorNode,ErrorDim);
}
colorid=firstvalue;
Label(current_root,1);
return current_root;
}
double MinimumError(struct QuantizedValue const *q, uint8 const *sample,
int ndims, uint8 const *weights)
{
double err=0;
for(int i=0;i<ndims;i++)
{
int val1;
int val2=sample[i];
if ((q->Mins[i]<=val2) && (q->Maxs[i]>=val2)) val1=val2;
else
{
val1=(val2<=q->Mins[i])?q->Mins[i]:q->Maxs[i];
}
err+=weights[i]*SQ(val1-val2);
}
return err;
}
double MaximumError(struct QuantizedValue const *q, uint8 const *sample,
int ndims, uint8 const *weights)
{
double err=0;
for(int i=0;i<ndims;i++)
{
int val2=sample[i];
int val1=(abs(val2-q->Mins[i])>abs(val2-q->Maxs[i]))?
q->Mins[i]:
q->Maxs[i];
err+=weights[i]*SQ(val2-val1);
}
return err;
}
// heap (priority queue) routines used for nearest-neghbor searches
struct FHeap {
int heap_n;
double *heap[MAXQUANT];
};
void InitHeap(struct FHeap *h)
{
h->heap_n=0;
}
void UpHeap(int k, struct FHeap *h)
{
double *tmpk=h->heap[k];
double tmpkn=*tmpk;
while((k>1) && (tmpkn <= *(h->heap[k/2])))
{
h->heap[k]=h->heap[k/2];
k/=2;
}
h->heap[k]=tmpk;
}
void HeapInsert(struct FHeap *h,double *elem)
{
h->heap_n++;
h->heap[h->heap_n]=elem;
UpHeap(h->heap_n,h);
}
void DownHeap(int k, struct FHeap *h)
{
double *v=h->heap[k];
while(k<=h->heap_n/2)
{
int j=2*k;
if (j<h->heap_n)
if (*(h->heap[j]) >= *(h->heap[j+1]))
j++;
if (*v < *(h->heap[j]))
{
h->heap[k]=v;
return;
}
h->heap[k]=h->heap[j]; k=j;
}
h->heap[k]=v;
}
void *RemoveHeapItem(struct FHeap *h)
{
void *ret=0;
if (h->heap_n!=0)
{
ret=h->heap[1];
h->heap[1]=h->heap[h->heap_n];
h->heap_n--;
DownHeap(1,h);
}
return ret;
}
// now, nearest neighbor finder. Use a heap to traverse the tree, stopping
// when there are no nodes with a minimum error < the current error.
struct FHeap TheQueue;
#define PUSHNODE(a) { \
(a)->MinError=MinimumError(a,sample,ndims,weights); \
if ((a)->MinError < besterror) HeapInsert(&TheQueue,&(a)->MinError); \
}
struct QuantizedValue *FindMatch(uint8 const *sample, int ndims,
uint8 *weights, struct QuantizedValue *q)
{
InitHeap(&TheQueue);
struct QuantizedValue *bestmatch=0;
double besterror=1.0e63;
PUSHNODE(q);
for(;;)
{
struct QuantizedValue *test=(struct QuantizedValue *)
RemoveHeapItem(&TheQueue);
if (! test) break; // heap empty
// printf("got pop node =%p minerror=%f\n",test,test->MinError);
if (test->MinError>besterror) break;
if (test->Children[0])
{
// it's a parent node. put the children on the queue
struct QuantizedValue *c1=test->Children[0];
struct QuantizedValue *c2=test->Children[1];
c1->MinError=MinimumError(c1,sample,ndims,weights);
if (c1->MinError < besterror)
HeapInsert(&TheQueue,&(c1->MinError));
c2->MinError=MinimumError(c2,sample,ndims,weights);
if (c2->MinError < besterror)
HeapInsert(&TheQueue,&(c2->MinError));
}
else
{
// it's a leaf node. This must be a new minimum or the MinError
// test would have failed.
if (test->MinError < besterror)
{
bestmatch=test;
besterror=test->MinError;
}
}
}
if (bestmatch)
{
SquaredError+=besterror;
bestmatch->NQuant++;
for(int i=0;i<ndims;i++)
bestmatch->Sums[i]+=sample[i];
}
return bestmatch;
}
static void RecalcMeans(struct QuantizedValue *q)
{
if (q)
{
if (q->Children[0])
{
// not a leaf, invoke recursively.
RecalcMeans(q->Children[0]);
RecalcMeans(q->Children[0]);
}
else
{
// it's a leaf. Set the means
if (q->NQuant)
{
for(int i=0;i<current_ndims;i++)
{
q->Mean[i]=(uint8) (q->Sums[i]/q->NQuant);
q->Sums[i]=0;
}
q->NQuant=0;
}
}
}
}
void OptimizeQuantizer(struct QuantizedValue *q, int ndims)
{
SetNDims(ndims);
RecalcMeans(q); // reset q values
Label(q,0); // update max/mins
}
static void RecalcStats(struct QuantizedValue *q)
{
if (q)
{
UpdateStats(q);
RecalcStats(q->Children[0]);
RecalcStats(q->Children[1]);
}
}
void RecalculateValues(struct QuantizedValue *q, int ndims)
{
SetNDims(ndims);
RecalcStats(q);
Label(q,0);
}