Exemplo n.º 1
0
        public void EncodeOneBlockPureWhiteRGBAThenDecodeIt()
        {
            // Arrange
            const int width            = 4;  // One block is 4x4
            const int height           = 4;
            const int channelsPerPixel = 4;

            byte[] whiteRGBA = new byte[] { 255, 255, 255, 255 };
            byte[,,] pixels = new byte[width, height, channelsPerPixel] {
                { { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 } },
                { { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 } },
                { { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 } },
                { { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 } },
            };
            TempByteImageFormat image = new TempByteImageFormat(pixels);

            // Act
            byte[] encodedBytes = PvrtcCompress.EncodeRgba4Bpp(image);

            TempByteImageFormat decoded = PvrtcDecompress.DecodeRgba4Bpp(encodedBytes, width);

            byte[] firstPixel = decoded.GetPixelChannels(0, 0);

            // Assert
            Assert.AreEqual(8, encodedBytes.Length);

            Assert.AreEqual(width, decoded.GetWidth());
            Assert.AreEqual(height, decoded.GetHeight());
            Assert.AreEqual(channelsPerPixel, decoded.GetChannelsPerPixel());
            CollectionAssert.AreEqual(whiteRGBA, firstPixel);
        }
Exemplo n.º 2
0
        public void EncodeFourBlocksPureBlackRGBThenDecodeIt()
        {
            // Arrange
            const int width            = 8;  // One block is 4x4, so 8x8 is 4 blocks
            const int height           = 8;
            const int channelsPerPixel = 3;

            byte[] blackRGB = new byte[] { 0, 0, 0 };
            byte[,,] pixels = new byte[width, height, channelsPerPixel] {
                { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } },
                { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } },
                { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } },
                { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } },
                { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } },
                { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } },
                { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } },
                { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } },
            };
            TempByteImageFormat image = new TempByteImageFormat(pixels);

            // Act
            byte[] encodedBytes = PvrtcCompress.EncodeRgb4Bpp(image);

            TempByteImageFormat decoded = PvrtcDecompress.DecodeRgb4Bpp(encodedBytes, width);

            byte[] firstPixel = decoded.GetPixelChannels(0, 0);

            // Assert
            Assert.AreEqual(32, encodedBytes.Length);

            Assert.AreEqual(width, decoded.GetWidth());
            Assert.AreEqual(height, decoded.GetHeight());
            Assert.AreEqual(channelsPerPixel, decoded.GetChannelsPerPixel());
            CollectionAssert.AreEqual(blackRGB, firstPixel);
        }
        public void IndexingTest()
        {
            // Arrange
            FileStream pngStream = new FileStream("half.png", FileMode.Open, FileAccess.Read);

            int[] indexes = new int[] { 0, 1, 12, 37, 56, 132, 200 };

            // Act
            var image = new Bitmap(pngStream);

            TempByteImageFormat test3d = new TempByteImageFormat(ReadTo3DBytes(image));
            TempByteImageFormat test1d = new TempByteImageFormat(ReadTo1DBytes(image), image.Width, image.Height, 3);

            // Assert
            for (int x = 0; x < indexes.Length; x++)
            {
                for (int y = 0; y < indexes.Length; y++)
                {
                    byte[] pixel3d = test3d.GetPixelChannels(x, y);
                    byte[] pixel1d = test1d.GetPixelChannels(x, y);

                    CollectionAssert.AreEqual(pixel3d, pixel1d, $"Pixels at {x} x {y} should be equal");
                }
            }
        }
Exemplo n.º 4
0
        public void EncodeRBGWhiteBlock()
        {
            // Arrange
            byte[] expected = new byte[8] {
                0, 0, 0, 0, 254, 255, 255, 255
            };

            const int width            = 4;  // One block is 4x4
            const int height           = 4;
            const int channelsPerPixel = 3;

            byte[] whiteRGB = new byte[] { 255, 255, 255 };
            byte[,,] pixels = new byte[width, height, channelsPerPixel] {
                { { 255, 255, 255 }, { 255, 255, 255 }, { 255, 255, 255 }, { 255, 255, 255 } },
                { { 255, 255, 255 }, { 255, 255, 255 }, { 255, 255, 255 }, { 255, 255, 255 } },
                { { 255, 255, 255 }, { 255, 255, 255 }, { 255, 255, 255 }, { 255, 255, 255 } },
                { { 255, 255, 255 }, { 255, 255, 255 }, { 255, 255, 255 }, { 255, 255, 255 } },
            };
            TempByteImageFormat image = new TempByteImageFormat(pixels);

            // Act
            byte[] encodedBytes = PvrtcCompress.EncodeRgb4Bpp(image);

            // Assert
            Assert.AreEqual(8, encodedBytes.Length);
            CollectionAssert.AreEqual(expected, encodedBytes);
        }
Exemplo n.º 5
0
        public void DecodeFile()
        {
            // Arrange
            FileStream pvrStream = new FileStream("hotair.4bpp.pvr", FileMode.Open, FileAccess.Read);

            byte[] array = new byte[512 * 512 / 2];

            // Act
            pvrStream.Seek(0x44, SeekOrigin.Begin);
            pvrStream.Read(array, 0, array.Length);
            TempByteImageFormat temp = PvrtcDecompress.DecodeRgb4Bpp(array, 512);

            // Assert
        }
        public void CheckThatRawContentWorks()
        {
            // Arrange
            FileStream pngStream = new FileStream("half.png", FileMode.Open, FileAccess.Read);

            // Act
            var image = new Bitmap(pngStream);

            byte[] bytes1d = ReadTo1DBytes(image);
            TempByteImageFormat test1d_1 = new TempByteImageFormat(bytes1d, image.Width, image.Height, 3, createCopy: true);

            // Assert
            Assert.Greater(bytes1d.Length, 1000, "There should be some bytes in image data");
            CollectionAssert.AreEqual(bytes1d, test1d_1.GetRawContent());
        }
        private static (long checksum, int colorCount) DoDitheringAndGetChecksumAndColorCount(DitheringBase <byte> dithering, Bitmap bitmap)
        {
            using (var image = bitmap)
            {
                byte[,,] bytes = ReadTo3DBytes(image);

                TempByteImageFormat temp = new TempByteImageFormat(bytes);
                dithering.DoDithering(temp);

                WriteToBitmap(image, temp.GetPixelChannels);
                //image.Save("temp123.png");

                return(GetImageTotalPixelSum(image), CountTotalColors(image));
            }
        }
    /// <summary>
    /// Constructor for temp byte image format
    /// </summary>
    /// <param name="input">Existing TempByteImageFormat</param>
    public TempByteImageFormat(TempByteImageFormat input)
    {
        if (input.content1d != null)
        {
            this.content1d = input.content1d;
            this.content3d = null;
        }
        else
        {
            this.content3d = input.content3d;
            this.content1d = null;
        }

        this.width            = input.width;
        this.height           = input.height;
        this.channelsPerPixel = input.channelsPerPixel;
    }
Exemplo n.º 9
0
        public void DecodeRGBTextureFromByteArray()
        {
            // Arrange
            byte[] input = new byte[8] {
                0, 0, 0, 0, 254, 255, 255, 255
            };
            int width = 4;

            byte[] whiteRGB = new byte[] { 255, 255, 255 };

            // Act
            TempByteImageFormat temp = PvrtcDecompress.DecodeRgb4Bpp(input, width);

            byte[] firstPixel = temp.GetPixelChannels(0, 0);

            // Assert
            CollectionAssert.AreEqual(whiteRGB, firstPixel);
        }
        public void LoadTest1d()
        {
            // Arrange
            FileStream pngStream = new FileStream("half.png", FileMode.Open, FileAccess.Read);

            // Act
            var   image      = new Bitmap(pngStream);
            Color firstColor = image.GetPixel(0, 0);

            TempByteImageFormat test = new TempByteImageFormat(ReadTo1DBytes(image), image.Width, image.Height, 3);

            byte[] firstPixel = test.GetPixelChannels(0, 0);

            // Assert
            Assert.AreEqual(firstColor.R, firstPixel[0]);
            Assert.AreEqual(firstColor.G, firstPixel[1]);
            Assert.AreEqual(firstColor.B, firstPixel[2]);
        }
Exemplo n.º 11
0
        public static void DitherAndWritePngStream(Stream outputStream, Bitmap bitmap, DitheringBase <byte> ditherer, bool writeToSameBitmap = true)
        {
            byte[] bytes = ReadWriteBitmaps.ReadBitmapToColorBytes(bitmap);

            TempByteImageFormat temp = new TempByteImageFormat(bytes, bitmap.Width, bitmap.Height, 3);

            temp = (TempByteImageFormat)ditherer.DoDithering(temp);

            if (writeToSameBitmap)
            {
                ReadWriteBitmaps.WriteToBitmap(bitmap, temp.GetPixelChannels);
                bitmap.Save(outputStream, System.Drawing.Imaging.ImageFormat.Png);
            }
            else
            {
                Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat);
                ReadWriteBitmaps.WriteToBitmap(tempBitmap, temp.GetPixelChannels);
                tempBitmap.Save(outputStream, System.Drawing.Imaging.ImageFormat.Png);
            }
        }
        public void CheckThat1dCopyWorks()
        {
            // Arrange
            FileStream pngStream = new FileStream("half.png", FileMode.Open, FileAccess.Read);

            // Act
            var image = new Bitmap(pngStream);

            byte[] bytes1d = ReadTo1DBytes(image);
            TempByteImageFormat test1d_1 = new TempByteImageFormat(bytes1d, image.Width, image.Height, 3, createCopy: false);
            TempByteImageFormat test1d_2 = new TempByteImageFormat(bytes1d, image.Width, image.Height, 3, createCopy: true);

            bytes1d[0] = 0;

            byte[] firstPixel1 = test1d_1.GetPixelChannels(0, 0);
            byte[] firstPixel2 = test1d_2.GetPixelChannels(0, 0);

            // Assert
            Assert.AreEqual(0, firstPixel1[0]);
            Assert.AreNotEqual(0, firstPixel2[0]);
        }
Exemplo n.º 13
0
 /// <summary>
 /// Constructor for temp byte image format
 /// </summary>
 /// <param name="input">Existing TempByteImageFormat</param>
 public TempByteImageFormat(TempByteImageFormat input) : this(input.content)
 {
 }