Example #1
0
        /// <summary>
        /// Reads a binary stream into a LZ77 store
        /// </summary>
        /// <param name="binaryReader">Reference to the binary reader</param>
        /// <param name="colorDepth">Colour depth the image is stored in</param>
        /// <returns>LZ77Store array</returns>
        public static LZ77Store[] DecodeBinaryStream(ref BinaryReader binaryReader, ColorModel.RGB.ColorDepth colorDepth)
        {
            var dataList = new List <LZ77Store>();

            // Create a bit reader from the base stream of the binaryreader
            var bitReader = new BitReader(binaryReader.BaseStream);

            // No packed data is the size of a byte, the remaining data will be picked up anyway
            while (bitReader.BaseStream.Position < bitReader.BaseStream.Length - 1)
            {
                // If is long form
                if (IsLongForm(ref bitReader))
                {
                    dataList.Add(new LZ77StoreLong <ColorModel.RGB>(
                                     bitReader.ReadByte(),
                                     bitReader.ReadByte()
                                     ));
                }
                // If is short form
                else
                {
                    // Calculate length of bits
                    var colorBitLength = (int)colorDepth / 3;

                    if (colorDepth == ColorModel.RGB.ColorDepth.Eight)
                    {
                        // Add the data item
                        dataList.Add(new LZ77StoreShort <ColorModel.RGB>(
                                         new ColorModel.RGB(
                                             bitReader.ReadSmallBits(3), 3,
                                             bitReader.ReadSmallBits(3), 3,
                                             bitReader.ReadSmallBits(2), 2,
                                             colorDepth
                                             )
                                         ));
                    }
                    else
                    {
                        // Add the data item
                        dataList.Add(new LZ77StoreShort <ColorModel.RGB>(
                                         new ColorModel.RGB(
                                             bitReader.ReadSmallBits(colorBitLength), colorBitLength,
                                             bitReader.ReadSmallBits(colorBitLength), colorBitLength,
                                             bitReader.ReadSmallBits(colorBitLength), colorBitLength,
                                             colorDepth
                                             )
                                         ));
                    }
                }
            }

            return(dataList.ToArray());
        }
        /// <summary>
        /// Get the pixel matrix from the data at a specified bit depth
        /// </summary>
        /// <param name="depth">Bit depth to return the data as</param>
        /// <returns>Multidimensional RGB data</returns>
        public ColorModel.RGB[,] GetPixelMatrix(ColorModel.RGB.ColorDepth depth)
        {
            // Copy the data
            var copiedData = this.GetDataCopy();

            // If the bits match what we have, return the copied data
            if (this.data.data[0, 0].bits == depth)
            {
                return(copiedData);
            }
            else
            {
                // Return a converted version
                return(copiedData.ConvertAll2D(new Converter <ColorModel.RGB, ColorModel.RGB>(x => x.ToDepth(depth))));
            }
        }