예제 #1
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);
        }
예제 #2
0
파일: BitPix.cs 프로젝트: Shinobytes/core
        public static BitPix CreateEmpty(int width, int height, int colorLayers)
        {
            if (width * height % 2 != 0)
            {
                throw new ArgumentException("The `width * height` MUST be divisble by 2.");
            }
            var numOfColors = BitPixUtilities.NumberOfBitCombinations(colorLayers);
            var layers      = new byte[colorLayers][];

            for (var i = 0; i < layers.GetLength(0); i++)
            {
                // layers[i] = new byte[((width * height) / 8) * colorLayers];
                layers[i] = new byte[(width * height) / 8];
            }
            return(new BitPix(width, height, numOfColors, layers));
        }
예제 #3
0
파일: BitPix.cs 프로젝트: Shinobytes/core
 public static BitPix CreateEmpty(int colorLayers)
 {
     return(new BitPix(8, 8, BitPixUtilities.NumberOfBitCombinations(colorLayers), new long[colorLayers]));
 }
예제 #4
0
파일: BitPix.cs 프로젝트: Shinobytes/core
 public static BitPix FromLongs(long[] l)
 {
     return(new BitPix(8, 8, BitPixUtilities.NumberOfBitCombinations(l.Length), l.Length));
 }