Exemplo n.º 1
0
        /// <summary>
        /// Reads the DDS file of this reader and returns a handle to its main image data.
        /// </summary>
        public unsafe DDSImage Read()
        {
            ReadMagicNumber();

            var header = ReadHeader();

            int imageWidth  = (int)header.Width;
            int imageHeight = (int)header.Height;

            var format      = CreateCompressionFormat(header.PixelFormat.FourCC);
            var surfaceData = ReadSurfaceData(imageWidth, imageHeight, format);

            return(DDSImage.CreateFromData(imageWidth, imageHeight, surfaceData, format));
        }
Exemplo n.º 2
0
        public void ReadDataForSoleBlock()
        {
            var format = new Mock <IBlockCompressionFormat>();

            format.Setup(f => f.BlockSize).Returns(BlockFormat.BC1ByteSize);

            var buffer   = Enumerable.Repeat((byte)255, BlockFormat.BC1ByteSize).ToArray();
            var ddsImage = DDSImage.CreateFromData(BlockFormat.Dimension, BlockFormat.Dimension,
                                                   buffer, format.Object);

            var data = ddsImage.GetBlockData(new Point(0, 0));

            CollectionAssert.AreEqual(buffer, data);
        }
        public void WriteImageToMemory()
        {
            var format = new Mock <IBlockCompressionFormat>();

            format.Setup(f => f.FourCC).Returns(FourCC.BC1Unorm);

            var imageData = new byte[8] {
                1, 2, 3, 4, 5, 6, 7, 8
            };
            int imageWidth  = 4;
            int imageHeight = 4;

            var ddsImage = DDSImage.CreateFromData(imageWidth, imageHeight, imageData, format.Object);
            var stream   = new MemoryStream();
            var writer   = new DDSFileWriter(stream);

            writer.Write(ddsImage);

            // Reset stream position for read
            stream.Position = 0;

            var reader = new BinaryReader(stream);

            {
                int size        = Marshal.SizeOf(DDSFile.MagicNumber);
                var data        = reader.ReadBytes(size);
                var magicNumber = BitConverter.ToUInt32(data, 0);

                Assert.AreEqual(DDSFile.MagicNumber, magicNumber);
            }
            {
                int size   = Marshal.SizeOf(typeof(DDSFileHeader));
                var data   = reader.ReadBytes(size);
                var handle = GCHandle.Alloc(data, GCHandleType.Pinned);
                var header = (DDSFileHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(DDSFileHeader));
                handle.Free();

                Assert.AreEqual(imageWidth, header.Width);
                Assert.AreEqual(imageHeight, header.Height);
            }
            {
                var data = reader.ReadBytes(imageData.Length);

                CollectionAssert.AreEqual(imageData, imageData);
            }

            writer.Dispose();
            reader.Dispose();
        }
        /// <summary>
        /// Creates a <see cref="ICompressedImage"/> mock of 3x2 blocks (48 texels)
        /// with a different set of arbitrary byte values in each block.
        /// </summary>
        /// <remarks>
        /// Block bytes:
        ///  _____  _____  _____
        /// |     ||     ||     |
        /// | 0x1 || 0x2 || 0x3 |
        /// |_____||_____||_____|
        /// |     ||     ||     |
        /// | 0x4 || 0x5 || 0x6 |
        /// |_____||_____||_____|
        /// </remarks>
        private static ICompressedImage CreateDDSImageMock(IBlockCompressionFormat format)
        {
            var blockBytes = new byte[]
            {
                1, 2, 3,
                4, 5, 6
            };

            const int blockSize      = 8;
            const int numberOfBlocks = 6;

            var buffer = new byte[numberOfBlocks * blockSize];

            for (int i = 0, bufferIndex = 0; i < blockBytes.Length; ++i, bufferIndex += blockSize)
            {
                for (int j = bufferIndex; j < blockSize + bufferIndex; ++j)
                {
                    buffer[j] = blockBytes[i];
                }
            }

            // Assert bytes have been set correctly
            // (for start and end byte value of each block only)
            Assert.AreEqual(1, buffer[0]);
            Assert.AreEqual(1, buffer[7]);
            Assert.AreEqual(2, buffer[8]);
            Assert.AreEqual(2, buffer[15]);
            Assert.AreEqual(3, buffer[16]);
            Assert.AreEqual(3, buffer[23]);

            Assert.AreEqual(4, buffer[24]);
            Assert.AreEqual(4, buffer[31]);
            Assert.AreEqual(5, buffer[32]);
            Assert.AreEqual(5, buffer[39]);
            Assert.AreEqual(6, buffer[40]);
            Assert.AreEqual(6, buffer[47]);

            const int width  = 12;
            const int height = 8;

            return(DDSImage.CreateFromData(width, height, buffer, format));
        }
Exemplo n.º 5
0
        public void ReadDataForLastBlock()
        {
            const int numberOfBlocks = 4;

            var format = new Mock <IBlockCompressionFormat>();

            format.Setup(f => f.BlockSize).Returns(BlockFormat.BC1ByteSize);

            var bytes  = Enumerable.Repeat((byte)200, BlockFormat.BC1ByteSize).ToArray();
            var buffer = new byte[numberOfBlocks * BlockFormat.BC1ByteSize];

            buffer.CopyFrom(bytes, buffer.Length - BlockFormat.BC1ByteSize);

            var dds = DDSImage.CreateFromData(numberOfBlocks * BlockFormat.Dimension,
                                              BlockFormat.Dimension, buffer, format.Object);

            var blockData = dds.GetBlockData(new Point(3, 0));

            CollectionAssert.AreEqual(bytes, blockData);
        }