示例#1
0
文件: BitPix.cs 项目: Shinobytes/core
        /// <summary>
        ///     Creates an array of raw bytes, including the necessary header.
        ///     The first 5 bytes of the byte array is the header. (Width, Height, LayerCount)
        /// </summary>
        /// <param name="bitpix"></param>
        /// <returns></returns>
        public static byte[] ToEncodedBytes(BitPix bitpix)
        {
            var resultIndex = 0;
            var w           = BitPixUtilities.ToBytes((short)bitpix.Width);
            var h           = BitPixUtilities.ToBytes((short)bitpix.Height);
            var l           = (byte)bitpix.Layers.GetLength(0);

            var result = new byte[2 + 2 + 1 + bitpix.GetTotalByteCount()]; //  w + h + l + img

            result[resultIndex++] = w[0];
            result[resultIndex++] = w[1];
            result[resultIndex++] = h[0];
            result[resultIndex++] = h[1];
            result[resultIndex++] = l;

            var layerCount = bitpix.Layers.GetLength(0);

            for (var i = 0; i < layerCount; i++)
            {
                var layerContentSize = bitpix.Layers[i].Length;
                for (var j = 0; j < layerContentSize; j++)
                {
                    result[resultIndex++] = bitpix.Layers[i][j];
                }
            }

            return(result);
        }
示例#2
0
文件: BitPix.cs 项目: Shinobytes/core
        public static BitPix FromString(string bitpixString)
        {
            if (string.IsNullOrEmpty(bitpixString))
            {
                throw new ArgumentNullException(nameof(bitpixString));
            }
            // no reason to go above 8 layers as it wouldnt really be suitable as a bitpix
            // 8 layers means 1 byte per pixel. (full grayscale)
            const int maximumSuitableLayers = 8;

            var widthHeight     = (int)Math.Sqrt(bitpixString.Length);
            var layers          = 1;
            var lowestValueNum  = (byte)'0';
            var highestValueNum = (byte)0;

            // 1. find highest value to determine the layer count
            //    if the highest value is 0, then 1 layer is used.
            for (var i = 0; i < bitpixString.Length; i++)
            {
                var value = (byte)bitpixString[i];
                if (value > highestValueNum)
                {
                    highestValueNum = value;
                }
            }

            var highestValue = highestValueNum - lowestValueNum;

            for (var layer = 1; layer <= maximumSuitableLayers; layer++)
            {
                var lastCount = BitPixUtilities.NumberOfBitCombinations(layer - 1);
                var thisCount = BitPixUtilities.NumberOfBitCombinations(layer);
                if (highestValue >= lastCount && highestValue < thisCount)
                {
                    layers = layer;
                    break;
                }
            }

            // 2. loop through each char of the string and convert it to the number it is.
            var pix = BitPix.CreateEmpty(widthHeight, layers);

            for (var y = 0; y < widthHeight; y++)
            {
                for (var x = 0; x < widthHeight; x++)
                {
                    var charValue = (byte)bitpixString[y * widthHeight + x];
                    var value     = (byte)(charValue - lowestValueNum);
                    pix.SetColorIndexAt(x, y, value);
                }
            }

            return(pix);
        }
示例#3
0
文件: BitPix.cs 项目: Shinobytes/core
        public static BitPix FromBytes(int width, int height, int layers, byte[] data, int index = 0)
        {
            var pix = BitPix.CreateEmpty(width, height, layers);

            for (var i = 0; i < pix.Layers.GetLength(0); i++)
            {
                for (var j = 0; j < pix.Layers[i].Length; j++)
                {
                    pix.Layers[i][j] = data[index++];
                }
            }
            return(pix);
        }
示例#4
0
文件: BitPix.cs 项目: Shinobytes/core
        public static string ToString(BitPix bitpix)
        {
            var lowestValueNum = (byte)'0';
            var returnString   = "";

            for (var y = 0; y < bitpix.Height; y++)
            {
                for (var x = 0; x < bitpix.Width; x++)
                {
                    var index = bitpix.GetColorIndexAt(x, y);
                    returnString += (char)(lowestValueNum + index);
                }
            }
            return(returnString);
        }