private void Sort(ref ExqHistogramEntry ppHist, SortFunction sortfunc)
        {
            ExqHistogramEntry pLow, pHigh, pCur, pNext;
            int      n   = 0;
            ExqFloat sum = 0;

            for (pCur = ppHist; pCur != null; pCur = pCur.pNext)
            {
                n++;
                sum += sortfunc(pCur);
            }

            if (n < 2)
            {
                return;
            }

            sum /= n;

            pLow = pHigh = null;
            for (pCur = ppHist; pCur != null; pCur = pNext)
            {
                pNext = pCur.pNext;
                if (sortfunc(pCur) < sum)
                {
                    pCur.pNext = pLow;
                    pLow       = pCur;
                }
                else
                {
                    pCur.pNext = pHigh;
                    pHigh      = pCur;
                }
            }

            if (pLow == null)
            {
                ppHist = pHigh;
                return;
            }
            if (pHigh == null)
            {
                ppHist = pLow;
                return;
            }

            Sort(ref pLow, sortfunc);
            Sort(ref pHigh, sortfunc);

            ppHist = pLow;
            while (pLow.pNext != null)
            {
                pLow = pLow.pNext;
            }

            pLow.pNext = pHigh;
        }
        public ExqFloat GetMeanError()
        {
            int      n   = 0;
            ExqFloat err = 0;

            for (int i = 0; i < pExq.numColors; i++)
            {
                n   += pExq.node[i].num;
                err += pExq.node[i].err;
            }

            return(Math.Sqrt(err / n) * 256);
        }
        private byte FindNearestColor(ExqColor pColor)
        {
            ExqColor dif   = new ExqColor();
            ExqFloat bestv = 16;
            int      besti = 0;

            for (int i = 0; i < pExq.numColors; i++)
            {
                dif.r = pColor.r - pExq.node[i].avg.r;
                dif.g = pColor.g - pExq.node[i].avg.g;
                dif.b = pColor.b - pExq.node[i].avg.b;
                dif.a = pColor.a - pExq.node[i].avg.a;
                if (dif.r * dif.r + dif.g * dif.g + dif.b * dif.b + dif.a * dif.a < bestv)
                {
                    bestv = dif.r * dif.r + dif.g * dif.g + dif.b * dif.b + dif.a * dif.a;
                    besti = i;
                }
            }
            return((byte)besti);
        }