Exemplo n.º 1
0
        public static void Fit(ColordBlock block, ColordSet colors, Options options, bool isBC1)
        {
            Vector3d maxColor = Vector3d.Zero;
            Vector3d minColor = Vector3d.One;

            foreach (Vector3d color in colors.Points)
            {
                minColor = Vector.Min(minColor, color);
                maxColor = Vector.Max(maxColor, color);
            }

            Vector3d inset = (maxColor - minColor) / 8.0;

            minColor = Vector.Clamp(minColor + inset, Vector3d.Zero, Vector3d.One);
            maxColor = Vector.Clamp(maxColor - inset, Vector3d.Zero, Vector3d.One);

            Vector3d color0 = minColor;
            Vector3d color1 = maxColor;
            Vector3d color2;
            Vector3d color3;

            if (isBC1 & colors.IsTransparent)
            {
                //need alpha coding, co <= c1
                var max = Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)maxColor));
                var min = Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)minColor));

                if (max <= min)
                {
                    color0 = maxColor;
                    color1 = minColor;
                }
            }

            if (!isBC1 || (Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color0)) > Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color1))))
            {
                color2 = Vector.Lerp(color0, color1, 1.0 / 3.0);
                color3 = Vector.Lerp(color0, color1, 2.0 / 3.0);
            }
            else
            {
                color2 = Vector.Lerp(color0, color1, 1.0 / 2.0);
                color3 = new Vector3d(0.0);
            }

            for (int y = 0; y < colors.Height; ++y)
            {
                for (int x = 0; x < colors.Width; ++x)
                {
                    Vector3d color = colors.Points[x, y];

                    double d0 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color0 - color)));
                    double d1 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color1 - color)));
                    double d2 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color2 - color)));
                    double d3 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color3 - color)));

                    int b0 = d0 > d3 ? 1 : 0;
                    int b1 = d1 > d2 ? 1 : 0;
                    int b2 = d0 > d2 ? 1 : 0;
                    int b3 = d1 > d3 ? 1 : 0;
                    int b4 = d2 > d3 ? 1 : 0;

                    int x0 = b1 & b2;
                    int x1 = b0 & b3;
                    int x2 = b0 & b4;

                    block.Indices[x + (y * 4)] = (x2 | ((x0 | x1) << 1));
                }
            }

            block.Colord0 = (int)Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color0));
            block.Colord0 = (int)Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color0));
        }
Exemplo n.º 2
0
        public static void Fit(ColordBlock block, ColordSet colors, Options options, bool isBC1)
		{
            Vector3d maxColor = Vector3d.Zero;
            Vector3d minColor = Vector3d.One;

            foreach(Vector3d color in colors.Points)
            {
                minColor = Vector.Min(minColor, color);
                maxColor = Vector.Max(maxColor, color);
			}

            Vector3d inset = (maxColor - minColor) / 8.0;

            minColor = Vector.Clamp(minColor + inset, Vector3d.Zero, Vector3d.One);
            maxColor = Vector.Clamp(maxColor - inset, Vector3d.Zero, Vector3d.One);

            Vector3d color0 = minColor;
            Vector3d color1 = maxColor;
            Vector3d color2;
            Vector3d color3;

            if (isBC1 & colors.IsTransparent)
			{
				//need alpha coding, co <= c1
                var max = Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)maxColor));
                var min = Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)minColor));

                if (max <= min)
				{
					color0 = maxColor;
					color1 = minColor;
				}
			}

            if (!isBC1 || (Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color0)) > Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color1))))
            {
                color2 = Vector.Lerp(color0, color1, 1.0 / 3.0);
                color3 = Vector.Lerp(color0, color1, 2.0 / 3.0);
            }
            else
            {
                color2 = Vector.Lerp(color0, color1, 1.0 / 2.0);
                color3 = new Vector3d(0.0);
            }

            for (int y = 0; y < colors.Height; ++y)
            {
                for (int x = 0; x < colors.Width; ++x)
                {
                    Vector3d color = colors.Points[x, y];

                    double d0 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color0 - color)));
                    double d1 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color1 - color)));
                    double d2 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color2 - color)));
                    double d3 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color3 - color)));

                    int b0 = d0 > d3 ? 1 : 0;
                    int b1 = d1 > d2 ? 1 : 0;
                    int b2 = d0 > d2 ? 1 : 0;
                    int b3 = d1 > d3 ? 1 : 0;
                    int b4 = d2 > d3 ? 1 : 0;

                    int x0 = b1 & b2;
                    int x1 = b0 & b3;
                    int x2 = b0 & b4;

                    block.Indices[x + (y * 4)] = (x2 | ((x0 | x1) << 1));
                }
            }

            block.Colord0 = (int)Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color0));
            block.Colord0 = (int)Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color0));
        }