private static void CopyFromZYX <TColor>(Image <TColor> image) where TColor : struct, IPixel <TColor> { using (PixelAccessor <TColor> pixels = image.Lock()) { byte red = 1; byte green = 2; byte blue = 3; byte alpha = 255; using (PixelArea <TColor> row = new PixelArea <TColor>(1, ComponentOrder.Zyx)) { row.Bytes[0] = blue; row.Bytes[1] = green; row.Bytes[2] = red; pixels.CopyFrom(row, 0); Color color = (Color)(object)pixels[0, 0]; Assert.Equal(red, color.R); Assert.Equal(green, color.G); Assert.Equal(blue, color.B); Assert.Equal(alpha, color.A); } } }
public void CopyToThenCopyFromWithOffset <TPixel>(TestImageProvider <TPixel> provider, ComponentOrder order) where TPixel : struct, IPixel <TPixel> { using (Image <TPixel> destImage = new Image <TPixel>(8, 8)) { using (Image <TPixel> srcImage = provider.GetImage()) { Fill(srcImage, new Rectangle(4, 4, 8, 8), NamedColors <TPixel> .Red); using (PixelAccessor <TPixel> srcPixels = srcImage.Lock()) { using (PixelArea <TPixel> area = new PixelArea <TPixel>(8, 8, order)) { srcPixels.CopyTo(area, 4, 4); using (PixelAccessor <TPixel> destPixels = destImage.Lock()) { destPixels.CopyFrom(area, 0, 0); } } } } provider.Utility.SourceFileOrDescription = order.ToString(); provider.Utility.SaveTestOutputFile(destImage, "bmp"); using (Image <TPixel> expectedImage = new Image <TPixel>(8, 8).Fill(NamedColors <TPixel> .Red)) { Assert.True(destImage.IsEquivalentTo(expectedImage)); } } }
private static void CopyFromZYXW <TPixel>(Image <TPixel> image) where TPixel : struct, IPixel <TPixel> { using (PixelAccessor <TPixel> pixels = image.Lock()) { byte red = 1; byte green = 2; byte blue = 3; byte alpha = 4; using (PixelArea <TPixel> row = new PixelArea <TPixel>(1, ComponentOrder.Zyxw)) { row.Bytes[0] = blue; row.Bytes[1] = green; row.Bytes[2] = red; row.Bytes[3] = alpha; pixels.CopyFrom(row, 0); Rgba32 color = (Rgba32)(object)pixels[0, 0]; Assert.Equal(red, color.R); Assert.Equal(green, color.G); Assert.Equal(blue, color.B); Assert.Equal(alpha, color.A); } } }
private static void CopyFromZYXW <TColor, TPacked>(Image <TColor, TPacked> image) where TColor : struct, IPackedPixel <TPacked> where TPacked : struct { using (PixelAccessor <TColor, TPacked> pixels = image.Lock()) { byte red = 1; byte green = 2; byte blue = 3; byte alpha = 4; using (PixelRow <TColor, TPacked> row = new PixelRow <TColor, TPacked>(1, ComponentOrder.ZYXW)) { row.Bytes[0] = blue; row.Bytes[1] = green; row.Bytes[2] = red; row.Bytes[3] = alpha; pixels.CopyFrom(row, 0); Color color = (Color)(object)pixels[0, 0]; Assert.Equal(red, color.R); Assert.Equal(green, color.G); Assert.Equal(blue, color.B); Assert.Equal(alpha, color.A); } } }
private void RestoreToBackground(ImageBase <TColor, TPacked> frame) { if (this.restoreArea == null) { return; } // Optimization for when the size of the frame is the same as the image size. if (this.restoreArea.Value.Width == this.decodedImage.Width && this.restoreArea.Value.Height == this.decodedImage.Height) { using (PixelAccessor <TColor, TPacked> pixelAccessor = frame.Lock()) { pixelAccessor.Reset(); } } else { using (PixelArea <TColor, TPacked> emptyRow = new PixelArea <TColor, TPacked>(this.restoreArea.Value.Width, ComponentOrder.XYZW)) { using (PixelAccessor <TColor, TPacked> pixelAccessor = frame.Lock()) { for (int y = this.restoreArea.Value.Top; y < this.restoreArea.Value.Top + this.restoreArea.Value.Height; y++) { pixelAccessor.CopyFrom(emptyRow, y, this.restoreArea.Value.Left); } } } } this.restoreArea = null; }
/// <summary> /// Reads the 32 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 ReadRgb32 <TPixel>(PixelAccessor <TPixel> pixels, int width, int height, bool inverted) where TPixel : struct, IPixel <TPixel> { int padding = CalculatePadding(width, 4); using (PixelArea <TPixel> row = new PixelArea <TPixel>(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 24 bit color palette from the stream /// </summary> /// <typeparam name="TColor">The pixel format.</typeparam> /// <param name="pixels">The <see cref="PixelAccessor{TColor}"/> 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 <TColor>(PixelAccessor <TColor> pixels, int width, int height, bool inverted) where TColor : struct, IPackedPixel, IEquatable <TColor> { int padding = CalculatePadding(width, 3); using (PixelArea <TColor> row = new PixelArea <TColor>(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 (PixelRow <TColor, TPacked> row = new PixelRow <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); } } }
public void CopyStretchedRGBTo_FromOrigo <TPixel>(TestImageProvider <TPixel> provider) where TPixel : struct, IPixel <TPixel> { using (Image <TPixel> src = provider.GetImage()) using (Image <TPixel> dest = new Image <TPixel>(8, 8)) using (PixelArea <TPixel> area = new PixelArea <TPixel>(8, 8, ComponentOrder.Xyz)) using (PixelAccessor <TPixel> s = src.Lock()) using (PixelAccessor <TPixel> d = dest.Lock()) { s.CopyRGBBytesStretchedTo(area, 0, 0); d.CopyFrom(area, 0, 0); Assert.Equal(s[0, 0], d[0, 0]); Assert.Equal(s[7, 0], d[7, 0]); Assert.Equal(s[0, 7], d[0, 7]); Assert.Equal(s[7, 7], d[7, 7]); } }
public void CopyStretchedRGBTo_FromOrigo <TColor>(TestImageProvider <TColor> provider) where TColor : struct, IPackedPixel, IEquatable <TColor> { using (Image <TColor> src = provider.GetImage()) using (Image <TColor> dest = provider.Factory.CreateImage(8, 8)) using (PixelArea <TColor> area = new PixelArea <TColor>(8, 8, ComponentOrder.Xyz)) using (PixelAccessor <TColor> s = src.Lock()) using (PixelAccessor <TColor> d = dest.Lock()) { s.CopyRGBBytesStretchedTo(area, 0, 0); d.CopyFrom(area, 0, 0); Assert.Equal(s[0, 0], d[0, 0]); Assert.Equal(s[7, 0], d[7, 0]); Assert.Equal(s[0, 7], d[0, 7]); Assert.Equal(s[7, 7], d[7, 7]); } }
public void CopyStretchedRGBTo_WithOffset <TPixel>(TestImageProvider <TPixel> provider) where TPixel : struct, IPixel <TPixel> { using (Image <TPixel> src = provider.GetImage()) using (PixelArea <TPixel> area = new PixelArea <TPixel>(8, 8, ComponentOrder.Xyz)) using (Image <TPixel> dest = new Image <TPixel>(8, 8)) using (PixelAccessor <TPixel> s = src.Lock()) using (PixelAccessor <TPixel> d = dest.Lock()) { s.CopyRGBBytesStretchedTo(area, 7, 6); d.CopyFrom(area, 0, 0); Assert.Equal(s[6, 7], d[0, 0]); Assert.Equal(s[6, 8], d[0, 1]); Assert.Equal(s[7, 8], d[1, 1]); Assert.Equal(s[6, 9], d[0, 2]); Assert.Equal(s[6, 9], d[0, 3]); Assert.Equal(s[6, 9], d[0, 7]); Assert.Equal(s[7, 9], d[1, 2]); Assert.Equal(s[7, 9], d[1, 3]); Assert.Equal(s[7, 9], d[1, 7]); Assert.Equal(s[9, 9], d[3, 2]); Assert.Equal(s[9, 9], d[3, 3]); Assert.Equal(s[9, 9], d[3, 7]); Assert.Equal(s[9, 7], d[3, 0]); Assert.Equal(s[9, 7], d[4, 0]); Assert.Equal(s[9, 7], d[7, 0]); Assert.Equal(s[9, 9], d[3, 2]); Assert.Equal(s[9, 9], d[4, 2]); Assert.Equal(s[9, 9], d[7, 2]); Assert.Equal(s[9, 9], d[4, 3]); Assert.Equal(s[9, 9], d[7, 7]); } }
public void CopyTo_Then_CopyFrom_OnFullImageRect <TPixel>(TestImageProvider <TPixel> provider, ComponentOrder order) where TPixel : struct, IPixel <TPixel> { using (Image <TPixel> src = provider.GetImage()) { using (Image <TPixel> dest = new Image <TPixel>(src.Width, src.Height)) { using (PixelArea <TPixel> area = new PixelArea <TPixel>(src.Width, src.Height, order)) { using (PixelAccessor <TPixel> srcPixels = src.Lock()) { srcPixels.CopyTo(area, 0, 0); } using (PixelAccessor <TPixel> destPixels = dest.Lock()) { destPixels.CopyFrom(area, 0, 0); } } Assert.True(src.IsEquivalentTo(dest, false)); } } }
public void CopyToThenCopyFromWithOffset <TColor>(TestImageProvider <TColor> provider, ComponentOrder order) where TColor : struct, IPackedPixel, IEquatable <TColor> { using (Image <TColor> destImage = new Image <TColor>(8, 8)) { TColor color; using (Image <TColor> srcImage = provider.GetImage()) { color = default(TColor); color.PackFromBytes(255, 0, 0, 255); Fill(srcImage, new Rectangle(4, 4, 8, 8), color); using (PixelAccessor <TColor> srcPixels = srcImage.Lock()) { using (PixelArea <TColor> area = new PixelArea <TColor>(8, 8, order)) { srcPixels.CopyTo(area, 4, 4); using (PixelAccessor <TColor> destPixels = destImage.Lock()) { destPixels.CopyFrom(area, 0, 0); } } } } provider.Utility.SourceFileOrDescription = order.ToString(); provider.Utility.SaveTestOutputFile(destImage, "bmp"); using (Image <TColor> expectedImage = new Image <TColor>(8, 8).Fill(color)) { Assert.True(destImage.IsEquivalentTo(expectedImage)); } } }
/// <summary> /// Reads the frames colors, mapping indices to colors. /// </summary> /// <param name="indices">The indexed pixels.</param> /// <param name="colorTable">The color table containing the available colors.</param> /// <param name="descriptor">The <see cref="GifImageDescriptor"/></param> private unsafe void ReadFrameColors(byte[] indices, byte[] colorTable, GifImageDescriptor descriptor) { int imageWidth = this.logicalScreenDescriptor.Width; int imageHeight = this.logicalScreenDescriptor.Height; ImageFrame <TColor, TPacked> previousFrame = null; ImageFrame <TColor, TPacked> currentFrame = null; ImageBase <TColor, TPacked> image; if (this.nextFrame == null) { image = this.decodedImage; image.Quality = colorTable.Length / 3; // This initializes the image to become fully transparent because the alpha channel is zero. image.InitPixels(imageWidth, imageHeight); } else { if (this.graphicsControlExtension != null && this.graphicsControlExtension.DisposalMethod == DisposalMethod.RestoreToPrevious) { previousFrame = this.nextFrame; } currentFrame = this.nextFrame.Clone(); image = currentFrame; this.RestoreToBackground(image); this.decodedImage.Frames.Add(currentFrame); } if (this.graphicsControlExtension != null && this.graphicsControlExtension.DelayTime > 0) { image.FrameDelay = this.graphicsControlExtension.DelayTime; } int i = 0; int interlacePass = 0; // The interlace pass int interlaceIncrement = 8; // The interlacing line increment int interlaceY = 0; // The current interlaced line using (PixelAccessor <TColor, TPacked> pixelAccessor = image.Lock()) { using (PixelArea <TColor, TPacked> pixelRow = new PixelArea <TColor, TPacked>(imageWidth, ComponentOrder.XYZW)) { for (int y = descriptor.Top; y < descriptor.Top + descriptor.Height; y++) { // Check if this image is interlaced. int writeY; // the target y offset to write to if (descriptor.InterlaceFlag) { // If so then we read lines at predetermined offsets. // When an entire image height worth of offset lines has been read we consider this a pass. // With each pass the number of offset lines changes and the starting line changes. if (interlaceY >= descriptor.Height) { interlacePass++; switch (interlacePass) { case 1: interlaceY = 4; break; case 2: interlaceY = 2; interlaceIncrement = 4; break; case 3: interlaceY = 1; interlaceIncrement = 2; break; } } writeY = interlaceY + descriptor.Top; interlaceY += interlaceIncrement; } else { writeY = y; } pixelRow.Reset(); byte *pixelBase = pixelRow.PixelBase; for (int x = 0; x < descriptor.Width; x++) { int index = indices[i]; if (this.graphicsControlExtension == null || this.graphicsControlExtension.TransparencyFlag == false || this.graphicsControlExtension.TransparencyIndex != index) { int indexOffset = index * 3; *(pixelBase + 0) = colorTable[indexOffset]; *(pixelBase + 1) = colorTable[indexOffset + 1]; *(pixelBase + 2) = colorTable[indexOffset + 2]; *(pixelBase + 3) = 255; } i++; pixelBase += 4; } pixelAccessor.CopyFrom(pixelRow, writeY, descriptor.Left); } } } if (previousFrame != null) { this.nextFrame = previousFrame; return; } if (currentFrame == null) { this.nextFrame = this.decodedImage.ToFrame(); } else { this.nextFrame = currentFrame.Clone(); } if (this.graphicsControlExtension != null && this.graphicsControlExtension.DisposalMethod == DisposalMethod.RestoreToBackground) { this.restoreArea = new Rectangle(descriptor.Left, descriptor.Top, descriptor.Width, descriptor.Height); } }
private static void DecodeImageData_Rgb_888(byte[] imageData, PixelAccessor <Rgb888> pixels, Rectangle destination) { var srcPixels = new PixelArea <Rgb888>(destination.Width, destination.Height, imageData, ComponentOrder.Xyz); pixels.CopyFrom(srcPixels, destination.Y, destination.X); }