/// <summary> /// Reads the 24 bit color palette from the stream /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="pixels">The <see cref="PixelAccessor{TPixel}"/> to assign the palette to.</param> /// <param name="width">The width of the bitmap.</param> /// <param name="height">The height of the bitmap.</param> /// <param name="inverted">Whether the bitmap is inverted.</param> private void ReadRgb24 <TPixel>(PixelAccessor <TPixel> pixels, int width, int height, bool inverted) where TPixel : struct, IPixel <TPixel> { int padding = CalculatePadding(width, 3); using (var row = new PixelArea <TPixel>(width, ComponentOrder.Zyx, padding)) { for (int y = 0; y < height; y++) { row.Read(this.currentStream); int newY = Invert(y, height, inverted); pixels.CopyFrom(row, newY); } } }
/// <summary> /// Reads the 32 bit color palette from the stream /// </summary> /// <typeparam name="TColor">The pixel format.</typeparam> /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam> /// <param name="pixels">The <see cref="PixelAccessor{TColor, TPacked}"/> to assign the palette to.</param> /// <param name="width">The width of the bitmap.</param> /// <param name="height">The height of the bitmap.</param> /// <param name="inverted">Whether the bitmap is inverted.</param> private void ReadRgb32 <TColor, TPacked>(PixelAccessor <TColor, TPacked> pixels, int width, int height, bool inverted) where TColor : struct, IPackedPixel <TPacked> where TPacked : struct { int padding = CalculatePadding(width, 4); using (PixelArea <TColor, TPacked> row = new PixelArea <TColor, TPacked>(width, ComponentOrder.ZYXW, padding)) { for (int y = 0; y < height; y++) { row.Read(this.currentStream); int newY = Invert(y, height, inverted); pixels.CopyFrom(row, newY); } } }
/// <summary> /// Reads the 16 bit color palette from the stream /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="pixels">The <see cref="PixelAccessor{TPixel}"/> to assign the palette to.</param> /// <param name="width">The width of the bitmap.</param> /// <param name="height">The height of the bitmap.</param> /// <param name="inverted">Whether the bitmap is inverted.</param> private void ReadRgb16 <TPixel>(PixelAccessor <TPixel> pixels, int width, int height, bool inverted) where TPixel : struct, IPixel <TPixel> { // We divide here as we will store the colors in our floating point format. const int ScaleR = 8; // 256/32 const int ScaleG = 4; // 256/64 const int ComponentCount = 2; TPixel color = default(TPixel); Rgba32 rgba = new Rgba32(0, 0, 0, 255); using (PixelArea <TPixel> row = new PixelArea <TPixel>(width, ComponentOrder.Xyz)) { for (int y = 0; y < height; y++) { row.Read(this.currentStream); int newY = Invert(y, height, inverted); Span <TPixel> pixelRow = pixels.GetRowSpan(newY); int offset = 0; for (int x = 0; x < width; x++) { short temp = BitConverter.ToInt16(row.Bytes, offset); rgba.R = (byte)(((temp & Rgb16RMask) >> 11) * ScaleR); rgba.G = (byte)(((temp & Rgb16GMask) >> 5) * ScaleG); rgba.B = (byte)((temp & Rgb16BMask) * ScaleR); color.PackFromRgba32(rgba); pixelRow[x] = color; offset += ComponentCount; } } } }