Esempio n. 1
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;
        }
    }
Esempio n. 2
0
    private static bool CheckThatWidthOrHeightIsValid(int widthOrHeight)
    {
        // Only sizes between [8 4096] are supported, must also be power of two
        if (widthOrHeight < 8 || widthOrHeight > 4096)
        {
            return(false);
        }

        if (!PvrtcGen.IsPowerOfTwo((ulong)widthOrHeight))
        {
            return(false);
        }

        return(true);
    }
Esempio n. 3
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);
        }
Esempio n. 4
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);
    }
Esempio n. 5
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);
    }
Esempio n. 6
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);
            }
        }
Esempio n. 7
0
        public static void Main(string[] args)
        {
            // Check that there is at least one paramater
            if (args.Length < 1)
            {
                Console.WriteLine("You have to give parameters! (use -help to show possible parameters)");
                return;
            }

            // Parse commandline/terminal commands, check simple options first
            Action simpleCommand = null;

            for (int i = 0; i < args.Length; i++)
            {
                // Check if simpleoptions includes the parameter
                if (simpleOptions.ContainsKey(args[i]))
                {
                    // Since it contains the parameter, do the simple command from it
                    simpleCommand = simpleOptions[args[i]].Item2;
                    break;
                }
            }

            if (simpleCommand != null)
            {
                // With simple command we just execute that command and exit out
                simpleCommand.Invoke();
                return;
            }

            // If there were no simple options, do more complex stuff next
            for (int i = 0; i < args.Length; i++)
            {
                // Check if normalOptions includes the parameter
                if (normalOptions.ContainsKey(args[i]))
                {
                    // Check that there is at least one more parameter
                    if (i < (args.Length - 1))
                    {
                        normalOptions[args[i]].Item2(args[i + 1]);
                    }
                    else
                    {
                        // Complain about missing parameter and exit
                        Console.WriteLine("Missing parameter for " + args[i]);
                        return;
                    }

                    // Check after every loop that given parameter was valid
                    if (errorInParsingParameters)
                    {
                        // Complain about error and exit
                        Console.WriteLine(lastError);
                        return;
                    }
                }
            }

            // Do actual generate since everything seems to be OK
            if (outputFilename != null)
            {
                byte[] pvrtcBytes = PvrtcGen.GeneratePvrtcByteArray(width, height, pvrtcType, generateHeader);

                // Write file if we have something to write
                if (pvrtcBytes != null)
                {
                    File.WriteAllBytes(outputFilename, pvrtcBytes);
                }
                else
                {
                    Console.WriteLine("Could not generate PVRTC data, most likely an incorrect resolution value");
                }
            }
            else
            {
                Console.WriteLine("Missing -file parameter");
            }
        }