Exemple #1
0
 internal static Rgb565 Pack(RgbF32 cl)
 {
     return(Pack(cl.R, cl.G, cl.B));
 }
Exemple #2
0
 internal void Unpack(out RgbF32 cl)
 {
     cl.R = RF;
     cl.G = GF;
     cl.B = BF;
 }
Exemple #3
0
        private void SpanValues( out RgbF32 r0, out RgbF32 r1 )
        {
            bool hasAlpha = alphaMask != 0;

            int cSteps = hasAlpha ? 3 : 4;
            var pC = hasAlpha ? pC3 : pC4;
            var pD = hasAlpha ? pD3 : pD4;

            var values = this.qvalues;

            // Find Min and Max points, as starting point
            RgbF32 X = UseUniformWeighting ? new RgbF32( 1, 1, 1 ) :
                new RgbF32( RWeight, GWeight, BWeight );
            RgbF32 Y = new RgbF32( 0, 0, 0 );

            for( int i = 0; i < values.Length; i++ )
            {
                var v = values[i];

            #if COLOR_WEIGHTS
                if( (alphaMask & (1 << i)) != 0 )
            #endif
                {
                    if( v.R < X.R ) X.R = v.R;
                    if( v.G < X.G ) X.G = v.G;

                    if( v.B < X.B ) X.B = v.B;
                    if( v.R > Y.R ) Y.R = v.R;

                    if( v.G > Y.G ) Y.G = v.G;
                    if( v.B > Y.B ) Y.B = v.B;
                }
            }

            // Diagonal axis
            RgbF32 AB;

            AB.R = Y.R - X.R;
            AB.G = Y.G - X.G;
            AB.B = Y.B - X.B;

            float fAB = AB.R * AB.R + AB.G * AB.G + AB.B * AB.B;

            // Single color block.. no need to root-find
            if( fAB < float.Epsilon )
            {
                r0 = X;
                r1 = Y;
                return;
            }

            // Try all four axis directions, to determine which diagonal best fits data
            float fABInv = 1.0f / fAB;

            RgbF32 Dir;
            Dir.R = AB.R * fABInv;
            Dir.G = AB.G * fABInv;
            Dir.B = AB.B * fABInv;

            RgbF32 Mid;
            Mid.R = (X.R + Y.R) * 0.5f;
            Mid.G = (X.G + Y.G) * 0.5f;
            Mid.B = (X.B + Y.B) * 0.5f;

            fDir[0] = fDir[1] = fDir[2] = fDir[3] = 0.0F;

            for( int i = 0; i < values.Length; i++ )
            {
                var v = values[i];

                RgbF32 Pt;
                Pt.R = (v.R - Mid.R) * Dir.R;
                Pt.G = (v.G - Mid.G) * Dir.G;
                Pt.B = (v.B - Mid.B) * Dir.B;

                float f;

            #if COLOR_WEIGHTS
                f = Pt.R + Pt.G + Pt.B;
                fDir[0] += v.a * f * f;

                f = Pt.R + Pt.G - Pt.B;
                fDir[1] += v.a * f * f;

                f = Pt.R - Pt.G + Pt.B;
                fDir[2] += v.a * f * f;

                f = Pt.R - Pt.G - Pt.B;
                fDir[3] += v.a * f * f;
            #else
                f = Pt.R + Pt.G + Pt.B;
                fDir[0] += f * f;

                f = Pt.R + Pt.G - Pt.B;
                fDir[1] += f * f;

                f = Pt.R - Pt.G + Pt.B;
                fDir[2] += f * f;

                f = Pt.R - Pt.G - Pt.B;
                fDir[3] += f * f;
            #endif
            }

            float fDirMax = fDir[0];
            int  iDirMax = 0;

            for( int iDir = 1; iDir < fDir.Length; iDir++ )
            {
                var d = fDir[iDir];
                if( d > fDirMax )
                {
                    fDirMax = d;
                    iDirMax = iDir;
                }
            }

            if( (iDirMax & 2) != 0 )
            {
                float f = X.G; X.G = Y.G; Y.G = f;
            }

            if( (iDirMax & 1) != 0 )
            {
                float f = X.B; X.B = Y.B; Y.B = f;
            }

            // Two color block.. no need to root-find
            if( fAB < 1.0f / 4096.0f )
            {
                r0 = X;
                r1 = Y;
                return;
            }

            // Use Newton's Method to find local minima of sum-of-squares error.
            float fSteps = (float)(cSteps - 1);

            for( int iIteration = 0; iIteration < 8; iIteration++ )
            {
                // Calculate new steps

                for( int iStep = 0; iStep < cSteps; iStep++ )
                {
                    interpValues[iStep].R = X.R * pC[iStep] + Y.R * pD[iStep];
                    interpValues[iStep].G = X.G * pC[iStep] + Y.G * pD[iStep];
                    interpValues[iStep].B = X.B * pC[iStep] + Y.B * pD[iStep];
                }

                // Calculate color direction
                Dir.R = Y.R - X.R;
                Dir.G = Y.G - X.G;
                Dir.B = Y.B - X.B;

                float fLen = (Dir.R * Dir.R + Dir.G * Dir.G + Dir.B * Dir.B);

                if( fLen < (1.0f / 4096.0f) )
                    break;

                float fScale = fSteps / fLen;

                Dir.R *= fScale;
                Dir.G *= fScale;
                Dir.B *= fScale;

                // Evaluate function, and derivatives
                float d2X, d2Y;
                RgbF32 dX, dY;
                d2X = d2Y = dX.R = dX.G = dX.B = dY.R = dY.G = dY.B = 0.0f;

                for( int i = 0; i < values.Length; i++ )
                {
                    var v = values[i];

                    float fDot = (v.R - X.R) * Dir.R +
                                 (v.G - X.G) * Dir.G +
                                 (v.B - X.B) * Dir.B;

                    int iStep;
                    if( fDot <= 0.0f )
                        iStep = 0;
                    else if( fDot >= fSteps )
                        iStep = cSteps - 1;
                    else
                        iStep = (int)(fDot + 0.5f);

                    RgbF32 Diff;
                    Diff.R = interpValues[iStep].R - v.R;
                    Diff.G = interpValues[iStep].G - v.G;
                    Diff.B = interpValues[iStep].B - v.B;

            #if COLOR_WEIGHTS
                    float fC = pC[iStep] * v.a * (1.0f / 8.0f);
                    float fD = pD[iStep] * v.a * (1.0f / 8.0f);
            #else
                    float fC = pC[iStep] * (1.0f / 8.0f);
                    float fD = pD[iStep] * (1.0f / 8.0f);
            #endif // COLOR_WEIGHTS

                    d2X += fC * pC[iStep];
                    dX.R += fC * Diff.R;
                    dX.G += fC * Diff.G;
                    dX.B += fC * Diff.B;

                    d2Y += fD * pD[iStep];
                    dY.R += fD * Diff.R;
                    dY.G += fD * Diff.G;
                    dY.B += fD * Diff.B;
                }

                // Move endpoints
                if( d2X > 0.0f )
                {
                    float f = -1.0f / d2X;

                    X.R += dX.R * f;
                    X.G += dX.G * f;
                    X.B += dX.B * f;
                }

                if( d2Y > 0.0f )
                {
                    float f = -1.0f / d2Y;

                    Y.R += dY.R * f;
                    Y.G += dY.G * f;
                    Y.B += dY.B * f;
                }

                if( (dX.R * dX.R < fEpsilon) && (dX.G * dX.G < fEpsilon) && (dX.B * dX.B < fEpsilon) &&
                   (dY.R * dY.R < fEpsilon) && (dY.G * dY.G < fEpsilon) && (dY.B * dY.B < fEpsilon) )
                {
                    break;
                }
            }

            r0 = X;
            r1 = Y;
        }
Exemple #4
0
 public static void Lerp(out RgbF32 o, RgbF32 a, RgbF32 b, float t)
 {
     o.R = a.R + t * (b.R - a.R);
     o.G = a.G + t * (b.G - a.G);
     o.B = a.B + t * (b.B - a.B);
 }
Exemple #5
0
 internal void Unpack( out RgbF32 cl )
 {
     cl.R = RF;
     cl.G = GF;
     cl.B = BF;
 }
Exemple #6
0
 internal static Rgb565 Pack( RgbF32 cl )
 {
     return Pack( cl.R, cl.G, cl.B );
 }
        private void SpanValues(out RgbF32 r0, out RgbF32 r1)
        {
            bool hasAlpha = alphaMask != 0;

            int cSteps = hasAlpha ? 3 : 4;
            var pC     = hasAlpha ? pC3 : pC4;
            var pD     = hasAlpha ? pD3 : pD4;

            var values = this.qvalues;

            // Find Min and Max points, as starting point
            RgbF32 X = UseUniformWeighting ? new RgbF32(1, 1, 1) :
                       new RgbF32(RWeight, GWeight, BWeight);
            RgbF32 Y = new RgbF32(0, 0, 0);

            for (int i = 0; i < values.Length; i++)
            {
                var v = values[i];

#if COLOR_WEIGHTS
                if ((alphaMask & (1 << i)) != 0)
#endif
                {
                    if (v.R < X.R)
                    {
                        X.R = v.R;
                    }
                    if (v.G < X.G)
                    {
                        X.G = v.G;
                    }

                    if (v.B < X.B)
                    {
                        X.B = v.B;
                    }
                    if (v.R > Y.R)
                    {
                        Y.R = v.R;
                    }

                    if (v.G > Y.G)
                    {
                        Y.G = v.G;
                    }
                    if (v.B > Y.B)
                    {
                        Y.B = v.B;
                    }
                }
            }

            // Diagonal axis
            RgbF32 AB;

            AB.R = Y.R - X.R;
            AB.G = Y.G - X.G;
            AB.B = Y.B - X.B;

            float fAB = AB.R * AB.R + AB.G * AB.G + AB.B * AB.B;

            // Single color block.. no need to root-find
            if (fAB < float.Epsilon)
            {
                r0 = X;
                r1 = Y;
                return;
            }

            // Try all four axis directions, to determine which diagonal best fits data
            float fABInv = 1.0f / fAB;

            RgbF32 Dir;
            Dir.R = AB.R * fABInv;
            Dir.G = AB.G * fABInv;
            Dir.B = AB.B * fABInv;

            RgbF32 Mid;
            Mid.R = (X.R + Y.R) * 0.5f;
            Mid.G = (X.G + Y.G) * 0.5f;
            Mid.B = (X.B + Y.B) * 0.5f;

            fDir[0] = fDir[1] = fDir[2] = fDir[3] = 0.0F;

            for (int i = 0; i < values.Length; i++)
            {
                var v = values[i];

                RgbF32 Pt;
                Pt.R = (v.R - Mid.R) * Dir.R;
                Pt.G = (v.G - Mid.G) * Dir.G;
                Pt.B = (v.B - Mid.B) * Dir.B;

                float f;

#if COLOR_WEIGHTS
                f        = Pt.R + Pt.G + Pt.B;
                fDir[0] += v.a * f * f;

                f        = Pt.R + Pt.G - Pt.B;
                fDir[1] += v.a * f * f;

                f        = Pt.R - Pt.G + Pt.B;
                fDir[2] += v.a * f * f;

                f        = Pt.R - Pt.G - Pt.B;
                fDir[3] += v.a * f * f;
#else
                f        = Pt.R + Pt.G + Pt.B;
                fDir[0] += f * f;

                f        = Pt.R + Pt.G - Pt.B;
                fDir[1] += f * f;

                f        = Pt.R - Pt.G + Pt.B;
                fDir[2] += f * f;

                f        = Pt.R - Pt.G - Pt.B;
                fDir[3] += f * f;
#endif
            }

            float fDirMax = fDir[0];
            int   iDirMax = 0;

            for (int iDir = 1; iDir < fDir.Length; iDir++)
            {
                var d = fDir[iDir];
                if (d > fDirMax)
                {
                    fDirMax = d;
                    iDirMax = iDir;
                }
            }

            if ((iDirMax & 2) != 0)
            {
                float f = X.G; X.G = Y.G; Y.G = f;
            }

            if ((iDirMax & 1) != 0)
            {
                float f = X.B; X.B = Y.B; Y.B = f;
            }


            // Two color block.. no need to root-find
            if (fAB < 1.0f / 4096.0f)
            {
                r0 = X;
                r1 = Y;
                return;
            }

            // Use Newton's Method to find local minima of sum-of-squares error.
            float fSteps = (float)(cSteps - 1);

            for (int iIteration = 0; iIteration < 8; iIteration++)
            {
                // Calculate new steps

                for (int iStep = 0; iStep < cSteps; iStep++)
                {
                    interpValues[iStep].R = X.R * pC[iStep] + Y.R * pD[iStep];
                    interpValues[iStep].G = X.G * pC[iStep] + Y.G * pD[iStep];
                    interpValues[iStep].B = X.B * pC[iStep] + Y.B * pD[iStep];
                }

                // Calculate color direction
                Dir.R = Y.R - X.R;
                Dir.G = Y.G - X.G;
                Dir.B = Y.B - X.B;

                float fLen = (Dir.R * Dir.R + Dir.G * Dir.G + Dir.B * Dir.B);

                if (fLen < (1.0f / 4096.0f))
                {
                    break;
                }

                float fScale = fSteps / fLen;

                Dir.R *= fScale;
                Dir.G *= fScale;
                Dir.B *= fScale;


                // Evaluate function, and derivatives
                float  d2X, d2Y;
                RgbF32 dX, dY;
                d2X = d2Y = dX.R = dX.G = dX.B = dY.R = dY.G = dY.B = 0.0f;

                for (int i = 0; i < values.Length; i++)
                {
                    var v = values[i];

                    float fDot = (v.R - X.R) * Dir.R +
                                 (v.G - X.G) * Dir.G +
                                 (v.B - X.B) * Dir.B;

                    int iStep;
                    if (fDot <= 0.0f)
                    {
                        iStep = 0;
                    }
                    else if (fDot >= fSteps)
                    {
                        iStep = cSteps - 1;
                    }
                    else
                    {
                        iStep = (int)(fDot + 0.5f);
                    }


                    RgbF32 Diff;
                    Diff.R = interpValues[iStep].R - v.R;
                    Diff.G = interpValues[iStep].G - v.G;
                    Diff.B = interpValues[iStep].B - v.B;

#if COLOR_WEIGHTS
                    float fC = pC[iStep] * v.a * (1.0f / 8.0f);
                    float fD = pD[iStep] * v.a * (1.0f / 8.0f);
#else
                    float fC = pC[iStep] * (1.0f / 8.0f);
                    float fD = pD[iStep] * (1.0f / 8.0f);
#endif // COLOR_WEIGHTS

                    d2X  += fC * pC[iStep];
                    dX.R += fC * Diff.R;
                    dX.G += fC * Diff.G;
                    dX.B += fC * Diff.B;

                    d2Y  += fD * pD[iStep];
                    dY.R += fD * Diff.R;
                    dY.G += fD * Diff.G;
                    dY.B += fD * Diff.B;
                }


                // Move endpoints
                if (d2X > 0.0f)
                {
                    float f = -1.0f / d2X;

                    X.R += dX.R * f;
                    X.G += dX.G * f;
                    X.B += dX.B * f;
                }

                if (d2Y > 0.0f)
                {
                    float f = -1.0f / d2Y;

                    Y.R += dY.R * f;
                    Y.G += dY.G * f;
                    Y.B += dY.B * f;
                }

                if ((dX.R * dX.R < fEpsilon) && (dX.G * dX.G < fEpsilon) && (dX.B * dX.B < fEpsilon) &&
                    (dY.R * dY.R < fEpsilon) && (dY.G * dY.G < fEpsilon) && (dY.B * dY.B < fEpsilon))
                {
                    break;
                }
            }

            r0 = X;
            r1 = Y;
        }
        public BC1Block Encode()
        {
            BC1Block ret;

            if (alphaMask == 0xFFFF)
            {
                ret.PackedValue = BC1Block.TransparentValue;
                return(ret);
            }

            QuantizeValues();

            RgbF32 r0, r1;

            SpanValues(out r0, out r1);

            //quantize the endpoints

            bool weightValues = !UseUniformWeighting;

            if (weightValues)
            {
                r0.R *= RInvWeight;
                r0.G *= GInvWeight;
                r0.B *= BInvWeight;

                r1.R *= RInvWeight;
                r1.G *= GInvWeight;
                r1.B *= BInvWeight;
            }

            var pr0 = Rgb565.Pack(r0);
            var pr1 = Rgb565.Pack(r1);

            if (alphaMask == 0 && pr0.PackedValue == pr1.PackedValue)
            {
                return(new BC1Block(pr0, pr1));
            }

            pr0.Unpack(out r0);
            pr1.Unpack(out r1);

            if (weightValues)
            {
                r0.R *= RWeight;
                r0.G *= GWeight;
                r0.B *= BWeight;

                r1.R *= RWeight;
                r1.G *= GWeight;
                r1.B *= BWeight;
            }

            //interp out the steps

            RgbF32 s0;

            if ((alphaMask != 0) == (pr0.PackedValue <= pr1.PackedValue))
            {
                ret             = new BC1Block(pr0, pr1);
                interpValues[0] = s0 = r0;
                interpValues[1] = r1;
            }
            else
            {
                ret             = new BC1Block(pr1, pr0);
                interpValues[0] = s0 = r1;
                interpValues[1] = r0;
            }

            uint[] pSteps;

            if (alphaMask != 0)
            {
                pSteps = pSteps3;

                RgbF32.Lerp(out interpValues[2], interpValues[0], interpValues[1], 0.5F);
            }
            else
            {
                pSteps = pSteps4;

                RgbF32.Lerp(out interpValues[2], interpValues[0], interpValues[1], 1.0F / 3.0F);
                RgbF32.Lerp(out interpValues[3], interpValues[0], interpValues[1], 2.0F / 3.0F);
            }

            //find the best values

            RgbF32 dir;

            dir.R = interpValues[1].R - s0.R;
            dir.G = interpValues[1].G - s0.G;
            dir.B = interpValues[1].B - s0.B;

            float fSteps = alphaMask != 0 ? 2 : 3;
            float fScale = (pr0.PackedValue != pr1.PackedValue) ?
                           (fSteps / (dir.R * dir.R + dir.G * dir.G + dir.B * dir.B)) : 0.0F;

            dir.R *= fScale;
            dir.G *= fScale;
            dir.B *= fScale;

            bool dither = DitherRgb;

            if (dither)
            {
                if (error == null)
                {
                    error = new RgbF32[16];
                }
                else
                {
                    Array.Clear(error, 0, 16);
                }
            }

            for (int i = 0; i < values.Length; i++)
            {
                if ((alphaMask & (1 << i)) != 0)
                {
                    ret.PackedValue |= 3U << (32 + i * 2);
                }
                else
                {
                    var cl = values[i];

                    if (weightValues)
                    {
                        cl.R *= RWeight;
                        cl.G *= GWeight;
                        cl.B *= BWeight;
                    }

                    if (dither)
                    {
                        var e = error[i];

                        cl.R += e.R;
                        cl.G += e.G;
                        cl.B += e.B;
                    }

                    float fDot =
                        (cl.R - s0.R) * dir.R +
                        (cl.G - s0.G) * dir.G +
                        (cl.B - s0.B) * dir.B;

                    uint iStep;

                    if (fDot <= 0)
                    {
                        iStep = 0;
                    }
                    else if (fDot >= fSteps)
                    {
                        iStep = 1;
                    }
                    else
                    {
                        iStep = pSteps[(int)(fDot + 0.5F)];
                    }

                    ret.PackedValue |= (ulong)iStep << (32 + i * 2);

                    if (dither)
                    {
                        RgbF32 e, d, interp = interpValues[iStep];

                        d.R = cl.R - interp.R;
                        d.G = cl.G - interp.G;
                        d.B = cl.B - interp.B;

                        if ((i & 3) != 3)
                        {
                            e = error[i + 1];

                            e.R += d.R * (7.0F / 16.0F);
                            e.G += d.G * (7.0F / 16.0F);
                            e.B += d.B * (7.0F / 16.0F);

                            error[i + 1] = e;
                        }

                        if (i < 12)
                        {
                            if ((i & 3) != 0)
                            {
                                e = error[i + 3];

                                e.R += d.R * (3.0F / 16.0F);
                                e.G += d.G * (3.0F / 16.0F);
                                e.B += d.B * (3.0F / 16.0F);

                                error[i + 3] = e;
                            }

                            e = error[i + 4];

                            e.R += d.R * (5.0F / 16.0F);
                            e.G += d.G * (5.0F / 16.0F);
                            e.B += d.B * (5.0F / 16.0F);

                            error[i + 4] = e;

                            if (3 != (i & 3))
                            {
                                e = error[i + 5];

                                e.R += d.R * (1.0F / 16.0F);
                                e.G += d.G * (1.0F / 16.0F);
                                e.B += d.B * (1.0F / 16.0F);

                                error[i + 5] = e;
                            }
                        }
                    }
                }
            }

            return(ret);
        }
Exemple #9
0
 public static void Lerp( out RgbF32 o, RgbF32 a, RgbF32 b, float t )
 {
     o.R = a.R + t * (b.R - a.R);
     o.G = a.G + t * (b.G - a.G);
     o.B = a.B + t * (b.B - a.B);
 }