Пример #1
0
        /// <summary>
        /// Convert binary data into an image in 4BPP (Genesis).
        /// </summary>
        /// <param name="stream">MemoryStream which contains data.</param>
        /// <param name="parameters">Parameters for the image.</param>
        /// <returns>Return the data converted into a SKBitmap.</returns>
        public SKBitmap Convert(MemoryStream stream, IImagePattern parameters)
        {
            if (stream == null || parameters == null)
            {
                return(null);
            }

            SKBitmap bitmap = null;

            using (var binaryReader = new BinaryReader(stream, Encoding.UTF8, true))
            {
                var imageInfo = new SKImageInfo(parameters.Width, parameters.Height, SKColorType.Rgba8888);

                bitmap = new SKBitmap(imageInfo);

                int tileHeight          = parameters.TilePattern.Height;
                int tileWidth           = parameters.TilePattern.Width;
                int numberOfRowsOfTiles = parameters.Height / tileHeight;
                int numberOfColsOfTiles = parameters.Width / tileWidth;

                var pixels = bitmap.Pixels;

                for (var row = 0; row < numberOfRowsOfTiles; row++)
                {
                    for (var column = 0; column < numberOfColsOfTiles; column++)
                    {
                        for (var height = 0; height < tileHeight; height++)
                        {
                            for (var width = 0; width < tileWidth; width += 2)
                            {
                                var byteRead = binaryReader.ReadByte();

                                var indexColor1 = (byteRead >> 4) & 0x0F;
                                var indexColor2 = byteRead & 0x0F;

                                SKColor color1;
                                SKColor color2;
                                if (parameters.Palette.Count > 0)
                                {
                                    color1 = parameters.Palette[indexColor1];
                                    color2 = parameters.Palette[indexColor2];
                                }
                                else
                                {
                                    color1 = this.defaultPalette[indexColor1];
                                    color2 = this.defaultPalette[indexColor2];
                                }

                                pixels[(height * parameters.Width) + width + (column * tileWidth) + (row * tileHeight * parameters.Width)]     = color1;
                                pixels[(height * parameters.Width) + width + 1 + (column * tileWidth) + (row * tileHeight * parameters.Width)] = color2;
                            }
                        }
                    }
                }

                bitmap.Pixels = pixels;
            }

            return(bitmap);
        }
Пример #2
0
        /// <summary>
        /// Convert an image in 4BPP (Genesis) into binary data.
        /// </summary>
        /// <param name="image">Image source to convert.</param>
        /// <param name="parameters">Parameters for the image.</param>
        /// <returns>Returns image converted in binary data.</returns>
        public MemoryStream ConvertBack(SKBitmap image, IImagePattern parameters)
        {
            if (image == null || parameters == null)
            {
                return(null);
            }

            var stream = new MemoryStream();

            int tileHeight          = parameters.TilePattern.Height;
            int tileWidth           = parameters.TilePattern.Width;
            int numberOfRowsOfTiles = parameters.Height / tileHeight;
            int numberOfColsOfTiles = parameters.Width / tileWidth;

            var pixels = image.Pixels;

            for (var row = 0; row < numberOfRowsOfTiles; row++)
            {
                for (var column = 0; column < numberOfColsOfTiles; column++)
                {
                    for (var height = 0; height < tileHeight; height++)
                    {
                        for (var width = 0; width < tileWidth; width += 2)
                        {
                            var color1 = pixels[(height * parameters.Width) + width + (column * tileWidth) + (row * tileHeight * parameters.Width)];
                            var color2 = pixels[(height * parameters.Width) + width + 1 + (column * tileWidth) + (row * tileHeight * parameters.Width)];

                            int bit1;
                            int bit2;
                            if (parameters.Palette.Count > 0)
                            {
                                bit1 = parameters.Palette.IndexOf(color1);
                                bit2 = parameters.Palette.IndexOf(color2);
                            }
                            else
                            {
                                bit1 = this.defaultPalette.IndexOf(color1);
                                bit2 = this.defaultPalette.IndexOf(color2);
                            }

                            var byteToWrite = (bit1 << 4) | bit2;

                            stream.WriteByte((byte)byteToWrite);
                        }
                    }
                }
            }

            return(stream);
        }
Пример #3
0
        /// <summary>
        /// Convert binary data into an image in 2BPP planar (Gameboy).
        /// </summary>
        /// <param name="stream">MemoryStream which contains data.</param>
        /// <param name="parameters">Parameters for the image.</param>
        /// <returns>Return the data converted into a SKBitmap.</returns>
        public SKBitmap Convert(MemoryStream stream, IImagePattern parameters)
        {
            if (stream == null || parameters == null)
            {
                return(null);
            }

            SKBitmap bitmap = null;

            using (var binaryReader = new BinaryReader(stream, Encoding.UTF8, true))
            {
                var imageInfo = new SKImageInfo(parameters.Width, parameters.Height, SKColorType.Rgba8888);

                bitmap = new SKBitmap(imageInfo);

                int tileHeight          = parameters.TilePattern.Height;
                int tileWidth           = parameters.TilePattern.Width;
                int numberOfRowsOfTiles = parameters.Height / tileHeight;
                int numberOfColsOfTiles = parameters.Width / tileWidth;

                var pixels = bitmap.Pixels;

                int numberOfTiles    = numberOfRowsOfTiles * numberOfColsOfTiles;
                int numberTotalOfBit = numberOfTiles * parameters.TilePattern.Size * 8;

                var imageBits = new BitList(numberTotalOfBit, false);

                var imageTiles = new BitList();
                for (int i = 0; i < numberOfTiles; i++)
                {
                    var tileBytes = binaryReader.ReadBytes(parameters.TilePattern.Size);
                    var tileBits  = new BitList(tileBytes);

                    var tile = tileBits.RearrangeBitsWith2Planes(parameters.TilePattern);

                    imageTiles.AddBitList(tile);
                }

                int index = 0;
                for (var row = 0; row < numberOfRowsOfTiles; row++)
                {
                    for (var column = 0; column < numberOfColsOfTiles; column++)
                    {
                        for (var height = 0; height < tileHeight; height++)
                        {
                            for (var width = 0; width < tileWidth; width++)
                            {
                                var bitColor = System.Convert.ToInt32(imageTiles.GetBinaryString(index++) + imageTiles.GetBinaryString(index++), 2);

                                SKColor color;
                                if (parameters.Palette.Count > 0)
                                {
                                    color = parameters.Palette[bitColor];
                                }
                                else
                                {
                                    color = this.defaultPalette[bitColor];
                                }

                                pixels[(height * parameters.Width) + width + (column * tileWidth) + (row * tileHeight * parameters.Width)] = color;
                            }
                        }
                    }
                }

                bitmap.Pixels = pixels;
            }

            return(bitmap);
        }
Пример #4
0
        /// <summary>
        /// Convert an image in 2BPP planar (Gameboy) into binary data.
        /// </summary>
        /// <param name="image">Image source to convert.</param>
        /// <param name="parameters">Parameters for the image.</param>
        /// <returns>Returns image converted in binary data.</returns>
        public MemoryStream ConvertBack(SKBitmap image, IImagePattern parameters)
        {
            if (image == null || parameters == null)
            {
                return(null);
            }

            var stream = new MemoryStream();

            int tileHeight          = parameters.TilePattern.Height;
            int tileWidth           = parameters.TilePattern.Width;
            int numberOfRowsOfTiles = parameters.Height / tileHeight;
            int numberOfColsOfTiles = parameters.Width / tileWidth;

            var pixels = image.Pixels;

            int numberOfTiles    = numberOfRowsOfTiles * numberOfColsOfTiles;
            int numberTotalOfBit = numberOfTiles * parameters.TilePattern.Size * 8;

            var imageBits = new BitList();

            for (var row = 0; row < numberOfRowsOfTiles; row++)
            {
                for (var column = 0; column < numberOfColsOfTiles; column++)
                {
                    var tile = new BitList();
                    for (var height = 0; height < tileHeight; height++)
                    {
                        for (var width = 0; width < tileWidth; width++)
                        {
                            var color = pixels[(height * parameters.Width) + width + (column * tileWidth) + (row * tileHeight * parameters.Width)];

                            int bit;
                            if (parameters.Palette.Count > 0)
                            {
                                bit = parameters.Palette.IndexOf(color);
                            }
                            else
                            {
                                bit = this.defaultPalette.IndexOf(color);
                            }

                            var bitString = System.Convert.ToString(bit, 2).PadLeft(2, '0');
                            var bits      = bitString.SplitByLength(1);

                            foreach (var b in bits)
                            {
                                tile.Add(b == "1" ? true : false);
                            }
                        }
                    }

                    if (tile.Count != parameters.TilePattern.Size * 8)
                    {
                        throw new JHacksonException(LocalizationManager.GetMessage("image.tile.incorrectNumberOfBits", row, column, imageBits.Count, numberTotalOfBit));
                    }

                    imageBits.AddBitList(tile.RearrangeBitsWith2PlanesBack(parameters.TilePattern));
                }
            }

            if (imageBits.Count != numberTotalOfBit)
            {
                throw new JHacksonException(LocalizationManager.GetMessage("image.incorrectBitsImageSize", imageBits.Count, numberTotalOfBit));
            }

            var bytes = imageBits.ToBytes();

            stream.Write(bytes, 0, bytes.Length);

            return(stream);
        }