Exemplo n.º 1
0
        public void GenerationTest()
        {
            // Arrange
            PvrtcGen.InitRandomSeed(1337 /* Magic value*/);
            int       width          = 256;
            int       height         = 256;
            PvrtcType pvrtcType      = PvrtcType.Transparent4bit;
            bool      generateHeader = true;

            byte[] pvrtcBytes = PvrtcGen.GeneratePvrtcByteArray(width, height, pvrtcType, generateHeader);

            // Act

            // Assert
            Assert.AreEqual(256 * 256 / 2 + 52, pvrtcBytes.Length);
        }
Exemplo n.º 2
0
        private static void SetFormat(string format)
        {
            PvrtcType possibleValue;

            // Check if format matches any enum value
            if (Enum.TryParse(format, /*ignoreCase:*/ true, out possibleValue))
            {
                Program.pvrtcType = possibleValue;
            }
            else
            {
                // Error out
                Program.errorInParsingParameters = true;
                Program.lastError = "Cannot convert " + format + " to texture format!";
            }
        }
Exemplo n.º 3
0
    public static byte[] GeneratePvrtcByteArray(int width, int height, PvrtcType pvrtcType, bool includeHeader = true)
    {
        byte[] returnArray = null;

        if (!CheckThatWidthOrHeightIsValid(width) || !CheckThatWidthOrHeightIsValid(height))
        {
            // Incorrect resolution
            return(returnArray);
        }

        byte[] header = new byte[0];

        if (includeHeader)
        {
            header = PvrtcGen.GeneratePvrFileFormatHeader(width, height, pvrtcType);
        }

        int amountOfBlocks = 0;

        if (PvrtcType.Opaque2bit == pvrtcType || PvrtcType.Transparent2bit == pvrtcType)
        {
            // every 8x4 pixel section is a block
            amountOfBlocks = width / 8 * height / 4;
        }
        else if (PvrtcType.Opaque4bit == pvrtcType || PvrtcType.Transparent4bit == pvrtcType)
        {
            // every 4x4 pixel section is a block
            amountOfBlocks = width / 4 * height / 4;
        }

        // Every block requires 8 bytes of data
        int bytesForTextureData = amountOfBlocks * 8;

        // Allocate memory for header + texture data, nothing for metadata
        returnArray = new byte[header.Length + bytesForTextureData];

        // Copy Header to returnArray
        Buffer.BlockCopy(header, 0, returnArray, 0, header.Length);

        // Here we would write metadata, but currently it is unimplemented
        // GeneratePvrMetaData()

        // Fill the texture data
        PvrtcGen.FillRestOfArrayWithRandomBytes(returnArray, header.Length, pvrtcType);

        return(returnArray);
    }
Exemplo n.º 4
0
        private static void RunTest()
        {
            // Generates output with following settings:
            PvrtcGen.InitRandomSeed(1337 /* Magic value*/);
            Program.width          = 256;
            Program.height         = 256;
            Program.pvrtcType      = PvrtcType.Transparent4bit;
            Program.generateHeader = true;

            byte[] pvrtcBytes = PvrtcGen.GeneratePvrtcByteArray(width, height, pvrtcType, generateHeader);

            // Write bytes to output stream
            using (Stream outputStream = Console.OpenStandardOutput())
            {
                outputStream.Write(pvrtcBytes, 0, pvrtcBytes.Length);
            }
        }
Exemplo n.º 5
0
    private static byte[] GeneratePvrFileFormatHeader(int wantedWidth, int wantedHeight, PvrtcType pvrtcType)
    {
        // Currently only v3.0.0 headers are supported, their size is ALWAYS 52 bytes
        // See http://cdn.imgtec.com/sdk-documentation/PVR+File+Format.Specification.pdf

        // Version, just static number
        uint version = 0x03525650;         // 4 bytes

        // Flags, currently means pre-multiplied with alpha
        uint flags = 0;         // 4 bytes

        // Pixel Format, we only support 4 predefined values
        ulong pixelFormat = (ulong)pvrtcType;         // 8 bytes

        // Colo(u)r Space, options are linear RGB (0) or sRGB (1)
        uint colorSpace = 0;         // 4 bytes

        // Channel Type,
        uint channelType = 0;         // 4 bytes

        // Height, in pixels
        uint height = (uint)wantedHeight;         // 4 bytes

        // Width, in pixels
        uint width = (uint)wantedWidth;         // 4 bytes

        // Depth, depth of 3D texture in pixels, value 1 is used since we only do 2d textures
        uint depth = 1;         // 4 bytes

        // Number of surfaces, value 1 is used since we only do 2d textures
        uint numberOfSurfaces = 1;         // 4 bytes

        // Number of faces, value 1 is used since we only do 2d textures
        uint numberOfFaces = 1;         // 4 bytes

        // MIP-Map Count, number of mip map levels, always 1 in this case since mipmap generation is NOT supported
        uint mipmapLevel = 1;         // 4 bytes

        // Meta Data Size, always 0 since no metadata is generated
        uint metaDataSize = 0;         // 4 bytes

        byte[] returnArray = new byte[52];

        PvrtcGen.WriteValuesToByteArray(returnArray, version, flags, pixelFormat, colorSpace, channelType, height, width, depth, numberOfSurfaces, numberOfFaces, mipmapLevel, metaDataSize);

        return(returnArray);
    }
Exemplo n.º 6
0
 public static byte[] GeneratePvrtcByteArray(int sideSize, PvrtcType pvrtcType, bool includeHeader = true)
 {
     return(GeneratePvrtcByteArray(sideSize, sideSize, pvrtcType, includeHeader));
 }
Exemplo n.º 7
0
    private static void FillRestOfArrayWithRandomBytes(byte[] byteArray, int startPos, PvrtcType pvrtcType)
    {
        Random rand = new Random();

        if (randomSeed != 0)
        {
            // Use chosen seed if required
            rand = new Random(randomSeed);
        }

        int sizeOfRandomByteArray = 16;         // 16 bytes is two blocks, just go with it since we ALWAYS have at least 2 blocks

        byte[] randomGeneratedByteArray = new byte[sizeOfRandomByteArray];

        int currentPos = startPos;
        int endPos     = byteArray.Length;

        while (currentPos < endPos)
        {
            // All byte combinations are valid for PVRTC
            rand.NextBytes(randomGeneratedByteArray);

            // But with Opaque ones we want to force opaque flags so that output only contains opaque alpha
            if (PvrtcType.Opaque2bit == pvrtcType || PvrtcType.Opaque4bit == pvrtcType)
            {
                PvrtcGen.TurnRandomByteArrayIntoOpaque(randomGeneratedByteArray);
            }

            Buffer.BlockCopy(randomGeneratedByteArray, 0, byteArray, currentPos, sizeOfRandomByteArray);
            currentPos += sizeOfRandomByteArray;
        }
    }