コード例 #1
0
ファイル: BC2ABlockEncoder.cs プロジェクト: pdjonov/BCn
        public BC2ABlock Encode()
        {
            //clamp the values into range

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

                if( v < 0 ) values[i] = 0;
                else if( v > 1 ) values[i] = 1;
            }

            //encode, optionally dithering as we go

            bool dither = Dither;

            BC2ABlock ret = new BC2ABlock();

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

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

                if( dither )
                    v += error[i];

                int u = (int)(v * 15F + 0.5F);

                ret.PackedValue|= (ulong)u << (i * 4);

                if( dither )
                {
                    float d = v - u * (1F / 15F);

                    if( (i & 3) != 3 )
                        error[i + 1] += d * (7F / 16F);

                    if( i < 12 )
                    {
                        if( (i & 3) != 0 )
                            error[i + 3] += d * (3F / 16F);

                        error[i + 4] += d * (5F / 16F);

                        if( (i & 3) != 3 )
                            error[i + 5] += d * (1F / 16F);
                    }
                }
            }

            return ret;
        }
コード例 #2
0
ファイル: BC2ABlockEncoder.cs プロジェクト: pdjonov/BCn
        public BC2ABlock Encode()
        {
            //clamp the values into range

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

                if (v < 0)
                {
                    values[i] = 0;
                }
                else if (v > 1)
                {
                    values[i] = 1;
                }
            }

            //encode, optionally dithering as we go

            bool dither = Dither;

            BC2ABlock ret = new BC2ABlock();

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

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

                if (dither)
                {
                    v += error[i];
                }

                int u = (int)(v * 15F + 0.5F);

                ret.PackedValue |= (ulong)u << (i * 4);

                if (dither)
                {
                    float d = v - u * (1F / 15F);

                    if ((i & 3) != 3)
                    {
                        error[i + 1] += d * (7F / 16F);
                    }

                    if (i < 12)
                    {
                        if ((i & 3) != 0)
                        {
                            error[i + 3] += d * (3F / 16F);
                        }

                        error[i + 4] += d * (5F / 16F);

                        if ((i & 3) != 3)
                        {
                            error[i + 5] += d * (1F / 16F);
                        }
                    }
                }
            }

            return(ret);
        }