Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef QUANTIZE_H
  9. #define QUANTIZE_H
  10. #ifndef STRING_H
  11. #include <string.h>
  12. #endif
  13. #define MAXDIMS 768
  14. #define MAXQUANT 16000
  15. #include <tier0/platform.h>
  16. struct Sample;
  17. struct QuantizedValue {
  18. double MinError; // minimum possible error. used
  19. // for neighbor searches.
  20. struct QuantizedValue *Children[2]; // splits
  21. int32 value; // only exists for leaf nodes
  22. struct Sample *Samples; // every sample quantized into this
  23. // entry
  24. int32 NSamples; // how many were quantized to this.
  25. int32 TotSamples;
  26. double *ErrorMeasure; // variance measure for each dimension
  27. double TotalError; // sum of errors
  28. uint8 *Mean; // average value of each dimension
  29. uint8 *Mins; // min box for children and this
  30. uint8 *Maxs; // max box for children and this
  31. int NQuant; // the number of samples which were
  32. // quantzied to this node since the
  33. // last time OptimizeQuantizer()
  34. // was called.
  35. int *Sums; // sum used by OptimizeQuantizer
  36. int sortdim; // dimension currently sorted along.
  37. };
  38. struct Sample {
  39. int32 ID; // identifier of this sample. can
  40. // be used for any purpose.
  41. int32 Count; // number of samples this sample
  42. // represents
  43. int32 QNum; // what value this sample ended up quantized
  44. // to.
  45. struct QuantizedValue *qptr; // ptr to what this was quantized to.
  46. uint8 Value[1]; // array of values for multi-dimensional
  47. // variables.
  48. };
  49. void FreeQuantization(struct QuantizedValue *t);
  50. struct QuantizedValue *Quantize(struct Sample *s, int nsamples, int ndims,
  51. int nvalues, uint8 *weights, int value0=0);
  52. int CompressSamples(struct Sample *s, int nsamples, int ndims);
  53. struct QuantizedValue *FindMatch(uint8 const *sample,
  54. int ndims,uint8 *weights,
  55. struct QuantizedValue *QTable);
  56. void PrintSamples(struct Sample const *s, int nsamples, int ndims);
  57. struct QuantizedValue *FindQNode(struct QuantizedValue const *q, int32 code);
  58. inline struct Sample *NthSample(struct Sample *s, int i, int nd)
  59. {
  60. uint8 *r=(uint8 *) s;
  61. r+=i*(sizeof(*s)+(nd-1));
  62. return (struct Sample *) r;
  63. }
  64. inline struct Sample *AllocSamples(int ns, int nd)
  65. {
  66. size_t size5=(sizeof(struct Sample)+(nd-1))*ns;
  67. void *ret=new uint8[size5];
  68. memset(ret,0,size5);
  69. for(int i=0;i<ns;i++)
  70. NthSample((struct Sample *)ret,i,nd)->Count=1;
  71. return (struct Sample *) ret;
  72. }
  73. // MinimumError: what is the min error which will occur if quantizing
  74. // a sample to the given qnode? This is just the error if the qnode
  75. // is a leaf.
  76. double MinimumError(struct QuantizedValue const *q, uint8 const *sample,
  77. int ndims, uint8 const *weights);
  78. double MaximumError(struct QuantizedValue const *q, uint8 const *sample,
  79. int ndims, uint8 const *weights);
  80. void PrintQTree(struct QuantizedValue const *p,int idlevel=0);
  81. void OptimizeQuantizer(struct QuantizedValue *q, int ndims);
  82. // RecalculateVelues: update the means in a sample tree, based upon
  83. // the samples. can be used to reoptimize when samples are deleted,
  84. // for instance.
  85. void RecalculateValues(struct QuantizedValue *q, int ndims);
  86. extern double SquaredError; // may be reset and examined. updated by
  87. // FindMatch()
  88. // the routines below can be used for uniform quantization via dart-throwing.
  89. typedef void (*GENERATOR)(void *); // generate a random sample
  90. typedef double (*COMPARER)(void const *a, void const *b);
  91. void *DartThrow(int NResults, int NTries, size_t itemsize, GENERATOR gen,
  92. COMPARER cmp);
  93. void *FindClosestDart(void *items,int NResults, size_t itemsize,
  94. COMPARER cmp, void *lookfor, int *idx);
  95. // color quantization of 24 bit images
  96. #define QUANTFLAGS_NODITHER 1 // don't do Floyd-steinberg dither
  97. extern void ColorQuantize(
  98. uint8 const *pImage, // 4 byte pixels ARGB
  99. int nWidth,
  100. int nHeight,
  101. int nFlags, // QUANTFLAGS_xxx
  102. int nColors, // # of colors to fill in in palette
  103. uint8 *pOutPixels, // where to store resulting 8 bit pixels
  104. uint8 *pOutPalette, // where to store resulting 768-byte palette
  105. int nFirstColor); // first color to use in mapping
  106. #endif