Esempio n. 1
0
        internal override void WriteInternal(byte[] array)
        {
            if (rleData != null)
            {
                throw new Exception(
                          "Cannot write to RLE image in Decompress mode.");
            }

            using (var dataStream = new MemoryStream())
            {
                var rleWriter = new RleWriter(dataStream);
                for (int row = 0; row < Size.Height; row++)
                {
                    int rowIndex = row * BytesPerRow;
                    rleRowLengths[row] = rleWriter.Write(
                        array, rowIndex, BytesPerRow);
                }

                // Save compressed data
                dataStream.Flush();
                rleData = dataStream.ToArray();
                Debug.Assert(rleRowLengths.Total == rleData.Length,
                             "RLE row lengths do not sum to the compressed data length.");
            }
        }
Esempio n. 2
0
        public void RleWriterTest()
        {
            var testData = new TestData(rowCount, bytesPerRow);

            byte[] encodedData = null;
            byte[] decodedData = null;
            try
            {
                // This method does not check ambiguous cases where there are multiple
                // ways to encode the same data.  Instead, it checks that the RLE is a
                // valid representation of the original data, by encoding and then
                // decoding.  We know that decoding works correctly because the
                // RleReaderTest checks it with specific tests packets.
                var rleStream = new MemoryStream();
                var rleWriter = new RleWriter(rleStream);
                var offset    = 0;
                for (int i = 0; i < testData.DataLengths.Length; i++)
                {
                    var dataLength = testData.DataLengths[i];
                    rleWriter.Write(testData.Data, offset, dataLength);
                    offset += dataLength;
                }

                rleStream.Flush();
                encodedData = rleStream.ToArray();
                decodedData = DecodeRleData(encodedData, testData.DataLengths);
            }
            catch (Exception e)
            {
                Assert.Fail("Failed with seed = " + testData.Seed + "\n" + e.ToString());
            }

            Assert.AreEqual(testData.Data, decodedData,
                            "Decoded RLE stream differs from original data, seed = " + testData.Seed);
        }