/// <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); }
public byte GetColorBitAt(int x, int y, int layer) { var l = Layers[layer]; var idx = GetBitIndex(x, y); var bits = BitPixUtilities.ToBits(l); return(bits[idx]); }
public void SetColorBitAt(int x, int y, bool value, int layer) { var l = Layers[layer]; var idx = GetBitIndex(x, y); var bits = BitPixUtilities.ToBits(l); bits[idx] = (byte)(value ? 1 : 0); Layers[layer] = BitPixUtilities.ToBytes(bits); }
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); }
public int GetColorIndexFromBits(byte[] colorBits) { for (var i = 0; i < ColorCount; i++) { var bits = BitPixUtilities.ToBits((byte)i, colorBits.Length); if (BitPixUtilities.BitEquals(colorBits, bits)) { return(i); } } return(-1); }
public byte GetColorIndexAt(int x, int y) { var index = GetBitIndex(x, y); var bits = new byte[Layers.Length]; for (var i = 0; i < Layers.Length; i++) { bits[i] = BitPixUtilities.ToBits(Layers[i])[index]; } return((byte)GetColorIndexFromBits(bits)); }
private static byte[][] LayerFromLongs(params long[] layers) { if (layers == null || layers.Length == 0) { throw new ArgumentException("`layers` cannot be empty. At least 1 layer is required."); } var result = new byte[layers.Length][]; for (var i = 0; i < layers.Length; i++) { result[i] = BitPixUtilities.ToBytes(layers[i]); } return(result); }
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)); }
public static BitPix CreateEmpty(int colorLayers) { return(new BitPix(8, 8, BitPixUtilities.NumberOfBitCombinations(colorLayers), new long[colorLayers])); }
public static BitPix FromLongs(long[] l) { return(new BitPix(8, 8, BitPixUtilities.NumberOfBitCombinations(l.Length), l.Length)); }
public byte[] GetColorBits(byte colorIndex) { return(BitPixUtilities.ToBits(colorIndex, Layers.Length)); }