示例#1
0
        public ClusterFit(ColourSet colours, SquishFlags flags, float?metric)
            : base(colours, flags)
        {
            // set the iteration count
            _mIterationCount = (MFlags & SquishFlags.KColourIterativeClusterFit) != 0 ? 8 : 1;

            // initialise the metric (old perceptual = 0.2126f, 0.7152f, 0.0722f)
            if (metric != null)
            {
                //m_metric = Vec4( metric[0], metric[1], metric[2], 1.0f );
            }
            else
            {
                _mMetric = new Vector4(1.0f);
            }

            // initialise the best error
            _mBesterror = new Vector4(float.MaxValue);

            // cache some values
            var count  = MColours.Count;
            var values = MColours.Points;

            // get the covariance matrix
            var covariance = Sym3X3.ComputeWeightedCovariance(count, values, MColours.Weights);

            // compute the principle component
            _mPrinciple = Sym3X3.ComputePrincipleComponent(covariance);
        }
示例#2
0
        public SingleColourFit(ColourSet colours, SquishFlags flags)
            : base(colours, flags)
        {
            // grab the single colour
            var values = MColours.Points;

            _mColour[0] = (byte)ColourBlock.FloatToInt(255.0f * values[0].X, 255);
            _mColour[1] = (byte)ColourBlock.FloatToInt(255.0f * values[0].Y, 255);
            _mColour[2] = (byte)ColourBlock.FloatToInt(255.0f * values[0].Z, 255);

            // initialise the best error
            _mBesterror = int.MaxValue;
        }
示例#3
0
        private static void CompressMasked(byte[] rgba, int mask, ref byte[] block, int offset, SquishFlags flags, float?metric)
        {
            // fix any bad flags
            flags = FixFlags(flags);

            // get the block locations
            var colourBlock = offset;
            var alphaBlock  = offset;

            if ((flags & (SquishFlags.KDxt3 | SquishFlags.KDxt5)) != 0)
            {
                colourBlock += 8;
            }

            // create the minimal point set
            var colours = new ColourSet(rgba, mask, flags);

            // check the compression type and compress colour
            if (colours.Count == 1)
            {
                // always do a single colour fit
                var fit = new SingleColourFit(colours, flags);
                fit.Compress(ref block, colourBlock);
            }
            else if ((flags & SquishFlags.KColourRangeFit) != 0 || colours.Count == 0)
            {
                // do a range fit
                var fit = new RangeFit(colours, flags, metric);
                fit.Compress(ref block, colourBlock);
            }
            else
            {
                // default to a cluster fit (could be iterative or not)
                var fit = new ClusterFit(colours, flags, metric);
                fit.Compress(ref block, colourBlock);
            }

            // compress alpha separately if necessary
            if ((flags & SquishFlags.KDxt3) != 0)
            {
                CompressAlphaDxt3(rgba, mask, ref block, alphaBlock);
            }
            else if ((flags & SquishFlags.KDxt5) != 0)
            {
                CompressAlphaDxt5(rgba, mask, ref block, alphaBlock);
            }
        }
示例#4
0
        public RangeFit(ColourSet colours, SquishFlags flags, float?metric)
            : base(colours, flags)
        {
            // initialise the metric (old perceptual = 0.2126f, 0.7152f, 0.0722f)
            if (metric != null)
            {
                //m_metric = new Vector3( metric[0], metric[1], metric[2] );
            }
            else
            {
                _mMetric = new Vector3(1.0f);
            }

            // initialise the best error
            _mBesterror = float.MaxValue;

            // cache some values
            var count   = MColours.Count;
            var values  = MColours.Points;
            var weights = MColours.Weights;

            // get the covariance matrix
            var covariance = Sym3X3.ComputeWeightedCovariance(count, values, weights);

            // compute the principle component
            var principle = Sym3X3.ComputePrincipleComponent(covariance);

            // get the min and max range as the codebook endpoints
            var start = new Vector3(0.0f);
            var end   = new Vector3(0.0f);

            if (count > 0)
            {
                float max;

                // compute the range
                start = end = values[0];
                var min = max = Vector3.Dot(values[0], principle);
                for (var i = 1; i < count; ++i)
                {
                    var val = Vector3.Dot(values[i], principle);
                    if (val < min)
                    {
                        start = values[i];
                        min   = val;
                    }
                    else if (val > max)
                    {
                        end = values[i];
                        max = val;
                    }
                }
            }

            // clamp the output to [0, 1]
            var one  = new Vector3(1.0f);
            var zero = new Vector3(0.0f);

            start = Vector3.Min(one, Vector3.Max(zero, start));
            end   = Vector3.Min(one, Vector3.Max(zero, end));

            // clamp to the grid and save
            var grid    = new Vector3(31.0f, 63.0f, 31.0f);
            var gridrcp = new Vector3(1.0f / 31.0f, 1.0f / 63.0f, 1.0f / 31.0f);
            var half    = new Vector3(0.5f);

            _mStart = Helpers.Truncate(grid * start + half) * gridrcp;
            _mEnd   = Helpers.Truncate(grid * end + half) * gridrcp;
        }