/// <summary> /// Constructor of LedStrip /// </summary> /// <param name="size">Size of strip in led number</param> /// <param name="spiBus">Id of spi bus</param> /// <param name="order">Order of colors</param> /// <param name="frequency">Frequency of spi bus (default 4M).</param> /// <param name="spiMode">Mode of spi bus (default Mode0)</param> public LedStrip(int size, string spiBus, ColorOrder order, int frequency = 4 * 1000 * 1000, SpiMode spiMode = SpiMode.Mode0) { if (size < 1) { throw new ArgumentOutOfRangeException(nameof(size), "must be greater than 0"); } Size = size; _leds = new Led[size]; for (int i = 0; i < size; i++) { _leds[i] = new Led(order); } var nbBytes = 4 + 4 * size + (size >> 4) + 1; _dummy = new byte[nbBytes]; _data = new byte[nbBytes]; SpiConnectionSettings settings = new SpiConnectionSettings() { ChipSelectType = SpiChipSelectType.None, DataBitLength = 8, ClockFrequency = frequency, Mode = spiMode }; _spi = SpiController.FromName(spiBus).GetDevice(settings); PrepareStart(); PrepareEnd(); }
/// <summary> /// Create new instance of the <see cref="Bitmap"/> class, with a specified path to a BMP file. /// </summary> /// <param name="path">Path to file.</param> /// <param name="colorOrder">Order of colors in each pixel.</param> /// <exception cref="ArgumentException"> /// <list type="bullet"> /// <item>Thrown if path is invalid.</item> /// <item>Memory error.</item> /// </list> /// </exception> /// <exception cref="ArgumentNullException"> /// <list type="bullet"> /// <item>Thrown if path is null.</item> /// <item>Memory error.</item> /// </list> /// </exception> /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="IOException">Thrown on IO error.</exception> /// <exception cref="NotSupportedException"> /// <list type="bullet"> /// <item>Thrown on fatal error (contact support).</item> /// <item>The path refers to non-file.</item> /// </list> /// </exception> /// <exception cref="ObjectDisposedException">Thrown if the stream is closed.</exception> /// <exception cref="Exception"> /// <list type="bullet"> /// <item>Thrown if header is not from a BMP.</item> /// <item>Info header size has the wrong value.</item> /// <item>Number of planes is not 1. Can not read file.</item> /// <item>Total Image Size is smaller than pure image size.</item> /// </list> /// </exception> /// <exception cref="NotImplementedException">Thrown if pixelsize is other then 32 / 24 or the file compressed.</exception> /// <exception cref="SecurityException">Thrown if the caller does not have permissions to read / write the file.</exception> /// <exception cref="FileNotFoundException">Thrown if the file cannot be found.</exception> /// <exception cref="DirectoryNotFoundException">Thrown if the specified path is invalid.</exception> /// <exception cref="PathTooLongException">Thrown if the specified path is exceed the system-defined max length.</exception> public Bitmap(string path, ColorOrder colorOrder = ColorOrder.BGR) : base(0, 0, ColorDepth.ColorDepth32) //Call the image constructor with wrong values { using (var fs = new FileStream(path, FileMode.Open)) { CreateBitmap(fs, colorOrder); } }
/// <summary> /// Create new instance of the <see cref="Bitmap"/> class, with a specified image data byte array. /// </summary> /// <param name="imageData">byte array.</param> /// <param name="colorOrder">Order of colors in each pixel.</param> /// <exception cref="ArgumentNullException">Thrown if imageData is null / memory error.</exception> /// <exception cref="ArgumentException">Thrown on memory error.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="IOException">Thrown on IO error.</exception> /// <exception cref="NotSupportedException">Thrown on fatal error (contact support).</exception> /// <exception cref="ObjectDisposedException">Thrown on fatal error (contact support).</exception> /// <exception cref="Exception"> /// <list type="bullet"> /// <item>Thrown if header is not from a BMP.</item> /// <item>Info header size has the wrong value.</item> /// <item>Number of planes is not 1.</item> /// <item>Total Image Size is smaller than pure image size.</item> /// </list> /// </exception> /// <exception cref="NotImplementedException">Thrown if pixelsize is other then 32 / 24 or the file compressed.</exception> public Bitmap(byte[] imageData, ColorOrder colorOrder = ColorOrder.BGR) : base(0, 0, ColorDepth.ColorDepth32) //Call the image constructor with wrong values { using (var ms = new MemoryStream(imageData)) { CreateBitmap(ms, colorOrder); } }
/// <summary> /// Constructor of LedStrip /// </summary> /// <param name="size">Size of strip in led number</param> /// <param name="spiBus">Id of spi bus</param> /// <param name="chipSelect">ChipSelect pin. May be a dummy pin (as not connected) as it don't be used</param> /// <param name="order">Order of colors</param> public LedStrip(int size, string spiBus, int chipSelect, ColorOrder order) { if (size < 1) { throw new ArgumentOutOfRangeException(nameof(size), "must be greater than 0"); } Size = size; _leds = new Led[size]; for (int i = 0; i < size; i++) { _leds[i] = new Led(order); } _dummy = new byte[4 + 4 * size + size / 8 / 2]; _data = new byte[4 + 4 * size + size / 8 / 2]; SpiConnectionSettings settings = new SpiConnectionSettings() { ChipSelectLine = chipSelect, DataBitLength = 8, ClockFrequency = 10 * 1000 * 1000 }; _spi = SpiController.FromName(spiBus).GetDevice(settings); PrepareStart(); PrepareEnd(); }
private void AddColors() { ColorOrder order = FindObjectOfType <ColorOrder>(); foreach (Color c in _colorsToAdd) { ColorManager.AddColor(c); notitfyAddingNewColor -= order.OnNotifiedColorAdded; notitfyAddingNewColor += order.OnNotifiedColorAdded; if (notitfyAddingNewColor != null) { notitfyAddingNewColor(this); } } _colorsToAdd = new List <Color>(); }
private Color GetColorFromImage(byte[] image, int ImagePos, ColorOrder colorOrder) { Color color; if (ImagePos > (image.Length - 3)) { return(Color.Black); } int C1 = image[ImagePos++]; int C2 = image[ImagePos++]; int C3 = image[ImagePos]; switch (colorOrder) { case ColorOrder.BGR: color = Color.FromArgb(C3, C2, C1); break; case ColorOrder.BRG: color = Color.FromArgb(C2, C3, C1); break; case ColorOrder.GBR: color = Color.FromArgb(C3, C1, C2); break; case ColorOrder.GRB: color = Color.FromArgb(C2, C1, C3); break; case ColorOrder.RBG: color = Color.FromArgb(C1, C3, C2); break; case ColorOrder.RGB: color = Color.FromArgb(C1, C2, C3); break; default: color = Color.FromArgb(C1, C2, C3); break; } return(color); }
public List <ColorOrder> FindWhereId_flag(int idFlag) { Color_FlagQuery color_FlagQuery = new Color_FlagQuery(_context); List <Color_Flag> color_flags = color_FlagQuery.FindWhereIdFlag(idFlag); if (color_flags == null) { return(null); } List <ColorOrder> colors = new List <ColorOrder>(); ColorOrder colorWhitOrder; foreach (Color_Flag color_flag in color_flags) { colorWhitOrder = new ColorOrder((Color)this.getByPk(color_flag.Id_color), color_flag.order); colors.Add(colorWhitOrder); } return(colors); }
public static Color Order(this Color current, ColorOrder order) { var r = current.r; var g = current.g; var b = current.b; var a = current.a; if (order == ColorOrder.BGRA) { return(new Color(b, g, r, a)); } if (order == ColorOrder.ARGB) { return(new Color(a, r, g, b)); } if (order == ColorOrder.ABGR) { return(new Color(a, b, g, r)); } return(current); }
// For more information about the format: https://docs.microsoft.com/en-us/previous-versions/ms969901(v=msdn.10)?redirectedfrom=MSDN /// <summary> /// Create bitmap from stream. /// </summary> /// <param name="stream">Stream.</param> /// <param name="colorOrder">Order of colors in each pixel.</param> /// <exception cref="ArgumentException">Thrown on memory error.</exception> /// <exception cref="ArgumentNullException">Thrown on memory error.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="IOException">Thrown on IO error.</exception> /// <exception cref="NotSupportedException"> /// <list type="bullet"> /// <item>Thrown on fatal error (contact support).</item> /// <item>The stream does not support seeking.</item> /// </list> /// </exception> /// <exception cref="ObjectDisposedException">Thrown if the stream is closed.</exception> /// <exception cref="Exception"> /// <list type="bullet"> /// <item>Thrown if header is not from a BMP.</item> /// <item>Info header size has the wrong value.</item> /// <item>Number of planes is not 1. Can not read file.</item> /// <item>Total Image Size is smaller than pure image size.</item> /// </list> /// </exception> /// <exception cref="NotImplementedException">Thrown if pixelsize is other then 32 / 24 or the file compressed.</exception> private void CreateBitmap(Stream stream, ColorOrder colorOrder) { #region BMP Header Byte[] _int = new byte[4]; Byte[] _short = new byte[2]; //Assume that we are using the BMP (Windows) V3 header format //reading magic number to identify if BMP file (BM as string - 42 4D as Hex) - bytes 0 -> 2 stream.Read(_short, 0, 2); if ("42-4D" != BitConverter.ToString(_short)) { throw new Exception("Header is not from a BMP"); } //read size of BMP file - byte 2 -> 6 stream.Read(_int, 0, 4); uint fileSize = BitConverter.ToUInt32(_int, 0); stream.Position = 10; //read header - bytes 10 -> 14 is the offset of the bitmap image data stream.Read(_int, 0, 4); uint pixelTableOffset = BitConverter.ToUInt32(_int, 0); //now reading size of BITMAPINFOHEADER should be 40 - bytes 14 -> 18 stream.Read(_int, 0, 4); uint infoHeaderSize = BitConverter.ToUInt32(_int, 0); if (infoHeaderSize != 40 && infoHeaderSize != 56 && infoHeaderSize != 124) // 124 - is BITMAPV5INFOHEADER, 56 - is BITMAPV3INFOHEADER, where we ignore the additional values see https://web.archive.org/web/20150127132443/https://forums.adobe.com/message/3272950 { throw new Exception("Info header size has the wrong value!"); } //now reading width of image in pixels - bytes 18 -> 22 stream.Read(_int, 0, 4); uint imageWidth = BitConverter.ToUInt32(_int, 0); //now reading height of image in pixels - byte 22 -> 26 stream.Read(_int, 0, 4); uint imageHeight = BitConverter.ToUInt32(_int, 0); //now reading number of planes should be 1 - byte 26 -> 28 stream.Read(_short, 0, 2); ushort planes = BitConverter.ToUInt16(_short, 0); if (planes != 1) { throw new Exception("Number of planes is not 1! Can not read file!"); } //now reading size of bits per pixel (1, 4, 8, 24, 32) - bytes 28 - 30 stream.Read(_short, 0, 2); ushort pixelSize = BitConverter.ToUInt16(_short, 0); //TODO: Be able to handle other pixel sizes if (!(pixelSize == 32 || pixelSize == 24)) { throw new NotImplementedException("Can only handle 32bit or 24bit bitmaps!"); } //now reading compression type - bytes 30 -> 34 stream.Read(_int, 0, 4); uint compression = BitConverter.ToUInt32(_int, 0); //TODO: Be able to handle compressed files if (compression != 0 && compression != 3) //3 is BI_BITFIELDS again ignore for now is for Adobe Images { //Global.mDebugger.Send("Can only handle uncompressed files!"); throw new NotImplementedException("Can only handle uncompressed files!"); } //now reading total image data size(including padding) - bytes 34 -> 38 stream.Read(_int, 0, 4); uint totalImageSize = BitConverter.ToUInt32(_int, 0); if (totalImageSize == 0) { totalImageSize = (uint)((((imageWidth * pixelSize) + 31) & ~31) >> 3) * imageHeight; // Look at the link above for the explanation Global.mDebugger.SendInternal("Calcualted image size: " + totalImageSize); } #endregion BMP Header //Set the bitmap to have the correct values Width = imageWidth; Height = imageHeight; Depth = (ColorDepth)pixelSize; Global.mDebugger.SendInternal("Width: " + Width); Global.mDebugger.SendInternal("Height: " + Height); Global.mDebugger.SendInternal("Depth: " + pixelSize); rawData = new int[Width * Height]; #region Pixel Table //Calculate padding int paddingPerRow; int pureImageSize = (int)(imageWidth * imageHeight * pixelSize / 8); if (totalImageSize != 0) { int remainder = (int)totalImageSize - pureImageSize; if (remainder < 0) { throw new Exception("Total Image Size is smaller than pure image size"); } paddingPerRow = remainder / (int)imageHeight; pureImageSize = (int)totalImageSize; } else { //total image size is 0 if it is not compressed paddingPerRow = 0; } //Read data stream.Position = (int)pixelTableOffset; int position = 0; Byte[] pixelData = new byte[pureImageSize]; stream.Read(pixelData, 0, pureImageSize); Byte[] pixel = new byte[4]; //All must have the same size for (int y = 0; y < imageHeight; y++) { for (int x = 0; x < imageWidth; x++) { if (pixelSize == 32) { pixel[0] = pixelData[position++]; pixel[1] = pixelData[position++]; pixel[2] = pixelData[position++]; pixel[3] = pixelData[position++]; } else { if (colorOrder == ColorOrder.BGR) { pixel[3] = pixelData[position++]; pixel[2] = pixelData[position++]; pixel[1] = pixelData[position++]; pixel[0] = 0; } else { pixel[0] = pixelData[position++]; pixel[1] = pixelData[position++]; pixel[2] = pixelData[position++]; pixel[3] = 0; } } rawData[x + (imageHeight - (y + 1)) * imageWidth] = BitConverter.ToInt32(pixel, 0); //This bits should be A, R, G, B but order is switched } position += paddingPerRow; } #endregion Pixel Table }
public AnimationConverter(ColorOrder colorOder, PixelOrder pixelOrder, DataType dataType) { InputFile = new BMAFormat(); SetInputFileOptions(colorOder, pixelOrder, dataType); }
private void CopyColorToBuf(byte[] image, int ImagePos, byte R, byte G, byte B, ColorOrder colorOrder) { if (ImagePos > (image.Length - 3)) { return; } switch (colorOrder) { case ColorOrder.BGR: image[ImagePos++] = B; image[ImagePos++] = G; image[ImagePos] = R; break; case ColorOrder.BRG: image[ImagePos++] = B; image[ImagePos++] = R; image[ImagePos] = G; break; case ColorOrder.GBR: image[ImagePos++] = G; image[ImagePos++] = B; image[ImagePos] = R; break; case ColorOrder.GRB: image[ImagePos++] = G; image[ImagePos++] = R; image[ImagePos] = B; break; case ColorOrder.RBG: image[ImagePos++] = R; image[ImagePos++] = B; image[ImagePos] = G; break; case ColorOrder.RGB: image[ImagePos++] = R; image[ImagePos++] = G; image[ImagePos] = B; break; default: image[ImagePos++] = R; image[ImagePos++] = G; image[ImagePos] = B; break; } }
private void CopyColorToBuf(byte[] image, int ImagePos, Color color, ColorOrder colorOrder) { CopyColorToBuf(image, ImagePos, color.R, color.G, color.B, colorOrder); }
public bool Encode(string FilePath, int StartFrame, int EndFrame, ColorOrder colorOrder, PixelOrder pixelOrder, DataType dataType) { //if (Lock) return false; int x, y, data_pos = 0; Frame frame; OutputFile = new BMAFormat(); OutputFile.colorOrder = colorOrder; OutputFile.dataType = dataType; OutputFile.pixelOrder = pixelOrder; OutputFile.fps = (byte)animation.Fps; OutputFile.FramesCount = EndFrame - StartFrame + 1; OutputFile.Width = (ushort)animation.SizeX; OutputFile.Height = (ushort)animation.SizeY; bool CT_Enable = (OutputFile.dataType == DataType.WithColorTable); if (CT_Enable) { if (!CreateColorTable()) { return(false); } } OutputFile.Data = new byte[OutputFile.FramesCount * OutputFile.Height * OutputFile.Width * 3]; for (int frame_num = StartFrame; frame_num <= EndFrame; frame_num++) { frame = animation.frames[frame_num]; if (frame.bitmap == null) { FrameRender(frame); } switch (OutputFile.pixelOrder) { case PixelOrder.VS_TR: for (x = animation.SizeX - 1; x >= 0; x--) { for (y = 0; y < animation.SizeY; y++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } x--; if (x < 0) { break; } for (y = animation.SizeY - 1; y >= 0; y--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.VS_TL: for (x = 0; x < animation.SizeX; x++) { for (y = 0; y < animation.SizeY; y++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } x++; if (x == animation.SizeX) { break; } for (y = animation.SizeY - 1; y >= 0; y--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.VS_BR: for (x = animation.SizeX - 1; x >= 0; x--) { for (y = animation.SizeY - 1; y >= 0; y--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } x--; if (x < 0) { break; } for (y = 0; y < animation.SizeY; y++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.VS_BL: for (x = 0; x < animation.SizeX; x++) { for (y = animation.SizeY - 1; y >= 0; y--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } x++; if (x == animation.SizeX) { break; } for (y = 0; y < animation.SizeY; y++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.HS_TL: for (y = 0; y < animation.SizeY; y++) { for (x = 0; x < animation.SizeX; x++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } y++; if (y == animation.SizeY) { break; } for (x = animation.SizeX - 1; x >= 0; x--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.HS_TR: for (y = 0; y < animation.SizeY; y++) { for (x = animation.SizeX - 1; x >= 0; x--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } y++; if (y == animation.SizeY) { break; } for (x = 0; x < animation.SizeX; x++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.HS_BL: for (y = animation.SizeY - 1; y >= 0; y--) { for (x = 0; x < animation.SizeX; x++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } y--; if (y < 0) { break; } for (x = animation.SizeX - 1; x >= 0; x--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.HS_BR: for (y = animation.SizeY - 1; y >= 0; y--) { for (x = animation.SizeX - 1; x >= 0; x--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } y--; if (y < 0) { break; } for (x = 0; x < animation.SizeX; x++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.HL_TL: for (y = 0; y < animation.SizeY; y++) { for (x = 0; x < animation.SizeX; x++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.HL_TR: for (y = 0; y < animation.SizeY; y++) { for (x = animation.SizeX - 1; x >= 0; x--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.HL_BL: for (y = animation.SizeY - 1; y >= 0; y--) { for (x = 0; x < animation.SizeX; x++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.HL_BR: for (y = animation.SizeY - 1; y >= 0; y--) { for (x = animation.SizeX - 1; x >= 0; x--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.VL_TR: for (x = animation.SizeX - 1; x >= 0; x--) { for (y = 0; y < animation.SizeY; y++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.VL_TL: for (x = 0; x < animation.SizeX; x++) { for (y = 0; y < animation.SizeY; y++) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.VL_BR: for (x = animation.SizeX - 1; x >= 0; x--) { for (y = animation.SizeY - 1; y >= 0; y--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; case PixelOrder.VL_BL: for (x = 0; x < animation.SizeX; x++) { for (y = animation.SizeY - 1; y >= 0; y--) { WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos); } } break; default: break; } } //*****************Save Animation to file******************* FileInfo f_info = new FileInfo(FilePath); switch (f_info.Extension) { case ".bma": using (BinaryWriter writer = new BinaryWriter(File.Open(FilePath, FileMode.OpenOrCreate))) { writer.Flush(); writer.Seek(0, SeekOrigin.Begin); writer.Write('b'); writer.Write('m'); writer.Write('a'); writer.Write((byte)OutputFile.version); writer.Write((byte)OutputFile.pixelOrder); writer.Write((byte)OutputFile.colorOrder); writer.Write((byte)OutputFile.dataType); writer.Write(OutputFile.FramesCount); writer.Write(OutputFile.fps); writer.Write(OutputFile.Width); writer.Write(OutputFile.Height); if (OutputFile.dataType == DataType.Default) { writer.Write(OutputFile.Data, 0, (int)(OutputFile.FramesCount * OutputFile.Width * OutputFile.Height * 3)); } else if (OutputFile.dataType == DataType.WithColorTable) { writer.Write((ushort)(OutputFile.ColorTableSize * 3)); for (int ct_pos = 0; ct_pos < OutputFile.ColorTableSize; ct_pos++) { writer.Write(OutputFile.ColorTable[ct_pos, 0]); writer.Write(OutputFile.ColorTable[ct_pos, 1]); writer.Write(OutputFile.ColorTable[ct_pos, 2]); } writer.Write(OutputFile.Data, 0, (int)(OutputFile.FramesCount * OutputFile.Width * OutputFile.Height)); } else { return(false); } } return(true); default: return(false); } }
public void SetInputFileOptions(ColorOrder colorOder, PixelOrder pixelOrder, DataType dataType) { InputFile.colorOrder = colorOder; InputFile.pixelOrder = pixelOrder; InputFile.dataType = dataType; }