setIndex() public méthode

public setIndex ( uint index, uint value ) : void
index uint
value uint
Résultat void
Exemple #1
0
        static uint computeAlphaIndices(AlphaBlock4x4 src, AlphaBlockDXT5 block)
        {
            byte[] alphas = new byte[8];
            block.evaluatePalette(alphas, false);     // @@ Use target decoder.

            uint totalError = 0;

            for (uint i = 0; i < 16; i++)
            {
                byte alpha = src.alpha[i];

                uint besterror = 256 * 256;
                uint best      = 8;
                for (uint p = 0; p < 8; p++)
                {
                    int  d     = alphas[p] - alpha;
                    uint error = (uint)(d * d);

                    if (error < besterror)
                    {
                        besterror = error;
                        best      = p;
                    }
                }
                //nvDebugCheck(best < 8);

                totalError += besterror;
                block.setIndex(i, best);
            }

            return(totalError);
        }
        static void computeAlphaIndices_RGBM(ColorSet src, ColorBlock RGB, AlphaBlockDXT5 dst)
        {
            byte[] alphas = new byte[8];
            dst.evaluatePalette(alphas, /*d3d9=*/ false); // @@ Use target decoder.

            for (uint i = 0; i < 16; i++)
            {
                float R = src.color[i].x;
                float G = src.color[i].y;
                float B = src.color[i].z;

                float r = (float)(RGB.color[i].r) / 255.0f;
                float g = (float)(RGB.color[i].g) / 255.0f;
                float b = (float)(RGB.color[i].b) / 255.0f;

                float minDist   = float.MaxValue;
                uint  bestIndex = 8;
                for (uint p = 0; p < 8; p++)
                {
                    // Compute M.
                    float M = (float)(alphas[p]) / 255.0f * (1 - threshold) + threshold;

                    // Decode color.
                    float fr = r * M;
                    float fg = g * M;
                    float fb = b * M;

                    // Measure error.
                    float error = Mathf.Pow(R - fr, 2) + Mathf.Pow(G - fg, 2) + Mathf.Pow(B - fb, 2);

                    if (error < minDist)
                    {
                        minDist   = error;
                        bestIndex = p;
                    }
                }

                dst.setIndex(i, bestIndex);
            }
        }
        static void computeAlphaIndices(AlphaBlock4x4 src, AlphaBlockDXT5 dst)
        {
            byte[] alphas = new byte[8];
            dst.evaluatePalette(alphas, /*d3d9=*/ false); // @@ Use target decoder.

            for (uint i = 0; i < 16; i++)
            {
                byte alpha = src.alpha[i];

                uint minDist   = int.MaxValue;
                uint bestIndex = 8;
                for (uint p = 0; p < 8; p++)
                {
                    uint dist = alphaDistance(alpha, alphas[p]);

                    if (dist < minDist)
                    {
                        minDist   = dist;
                        bestIndex = p;
                    }
                }
                dst.setIndex(i, bestIndex);
            }
        }
        static void computeAlphaIndices_RGBM(ColorSet src, ColorBlock RGB, AlphaBlockDXT5 dst)
        {
            byte[] alphas = new byte[8];
            dst.evaluatePalette(alphas, /*d3d9=*/false); // @@ Use target decoder.

            for (uint i = 0; i < 16; i++)
            {
                float R = src.color[i].x;
                float G = src.color[i].y;
                float B = src.color[i].z;

                float r = (float)(RGB.color[i].r) / 255.0f;
                float g = (float)(RGB.color[i].g) / 255.0f;
                float b = (float)(RGB.color[i].b) / 255.0f;

                float minDist = float.MaxValue;
                uint bestIndex = 8;
                for (uint p = 0; p < 8; p++)
                {
                    // Compute M.
                    float M = (float)(alphas[p]) / 255.0f * (1 - threshold) + threshold;

                    // Decode color.
                    float fr = r * M;
                    float fg = g * M;
                    float fb = b * M;

                    // Measure error.
                    float error = Mathf.Pow(R - fr, 2) + Mathf.Pow(G - fg, 2) + Mathf.Pow(B - fb, 2);

                    if (error < minDist)
                    {
                        minDist = error;
                        bestIndex = p;
                    }
                }

                dst.setIndex(i, bestIndex);
            }
        }
        static void computeAlphaIndices(AlphaBlock4x4 src, AlphaBlockDXT5 dst)
        {
            byte[] alphas = new byte[8];
            dst.evaluatePalette(alphas, /*d3d9=*/false); // @@ Use target decoder.

            for (uint i = 0; i < 16; i++)
            {
                byte alpha = src.alpha[i];

                uint minDist = int.MaxValue;
                uint bestIndex = 8;
                for (uint p = 0; p < 8; p++)
                {
                    uint dist = alphaDistance(alpha, alphas[p]);

                    if (dist < minDist)
                    {
                        minDist = dist;
                        bestIndex = p;
                    }
                }
                dst.setIndex(i, bestIndex);
            }
        }
Exemple #6
0
        static void optimizeAlpha8(AlphaBlock4x4 src, AlphaBlockDXT5 block)
        {
            float alpha2_sum    = 0;
            float beta2_sum     = 0;
            float alphabeta_sum = 0;
            float alphax_sum    = 0;
            float betax_sum     = 0;

            for (uint i = 0; i < 16; i++)
            {
                uint  idx = block.index(i);
                float alpha;
                if (idx < 2)
                {
                    alpha = 1.0f - idx;
                }
                else
                {
                    alpha = (8.0f - idx) / 7.0f;
                }

                float beta = 1 - alpha;

                alpha2_sum    += alpha * alpha;
                beta2_sum     += beta * beta;
                alphabeta_sum += alpha * beta;
                alphax_sum    += alpha * src.alpha[i];
                betax_sum     += beta * src.alpha[i];
            }

            float factor = 1.0f / (alpha2_sum * beta2_sum - alphabeta_sum * alphabeta_sum);

            float a = (alphax_sum * beta2_sum - betax_sum * alphabeta_sum) * factor;
            float b = (betax_sum * alpha2_sum - alphax_sum * alphabeta_sum) * factor;

            byte alpha0 = (byte)(Math.Min(Math.Max(a, 0.0f), 255.0f));
            byte alpha1 = (byte)(Math.Min(Math.Max(b, 0.0f), 255.0f));

            if (alpha0 < alpha1)
            {
                swap(ref alpha0, ref alpha1);

                // Flip indices:
                for (uint i = 0; i < 16; i++)
                {
                    uint idx = block.index(i);
                    if (idx < 2)
                    {
                        block.setIndex(i, 1 - idx);
                    }
                    else
                    {
                        block.setIndex(i, 9 - idx);
                    }
                }
            }
            else if (alpha0 == alpha1)
            {
                for (uint i = 0; i < 16; i++)
                {
                    block.setIndex(i, 0);
                }
            }

            block.alpha0 = alpha0;
            block.alpha1 = alpha1;
        }