Exemple #1
0
    public byte[] AlphaMap_Compressed(MemoryStream ADTtexstream) // compressed
    {
        StreamTools s = new StreamTools();

        byte[] textureArray = new byte[4096];
        int    alphaOffset  = 0;

        while (alphaOffset < 4096)
        {
            //read a byte//
            byte onebyte = (byte)ADTtexstream.ReadByte();
            //translate byte to 8 bits//
            byte[] bytearr = new byte[1];
            bytearr[0] = onebyte;
            BitArray bitarr = new BitArray(bytearr);
            //is first bit true?//
            bool fc = bitarr.Get(7); //true=fill, false=copy
            //next 7 bits determine how many times we fill/copy, max 127
            int[]  array    = new int[1];
            bool[] bitArray = new bool[7];
            for (int i = 0; i < 7; i++)
            {
                bitArray[i] = bitarr.Get(6 - i);
            }
            int times = s.BoolArrayToInt(bitArray);
            if (times == 0)
            {
                alphaOffset = 4096;
                break;
            }
            if (times != 0)
            {
                //fill mode//
                if (fc == true) // repeat the byte following the one we just read *count* number of times into the alpha map
                {
                    byte secondbytefill = (byte)ADTtexstream.ReadByte();
                    for (int j = 0; j < times; j++)
                    {
                        textureArray[alphaOffset] = secondbytefill;
                        alphaOffset++;
                    }
                }
                //copy mode//
                if (fc == false)  //  read *count* number of following bytes into the alpha map
                {
                    for (int jc = 0; jc < times; jc++)
                    {
                        byte secondbytecopy = (byte)ADTtexstream.ReadByte();
                        textureArray[alphaOffset] = secondbytecopy;
                        alphaOffset++;
                    }
                }
            }
        }
        alphaOffset = 0;
        return(textureArray);
    }