public void RestoreRegion(PdnRegion region) { if (region != null) { BitmapLayer activeLayer = (BitmapLayer)ActiveLayer; activeLayer.Surface.CopySurface(this.ScratchSurface, region); activeLayer.Invalidate(region); } }
/// <summary> /// Returns a new Document that is a flattened version of this one /// "Flattened" means it is one layer that is simply a bitmap of /// the compositied image. /// </summary> /// <returns></returns> public Document Flatten() { Document newDocument = new Document(width, height); newDocument.ReplaceMetaDataFrom(this); BitmapLayer layer = Layer.CreateBackgroundLayer(width, height); newDocument.Layers.Add(layer); Flatten(layer.Surface); return(newDocument); }
public void RestoreSavedRegion() { if (this.saveRegion != null) { BitmapLayer activeLayer = (BitmapLayer)ActiveLayer; activeLayer.Surface.CopySurface(this.ScratchSurface, this.saveRegion); activeLayer.Invalidate(this.saveRegion); this.saveRegion.Dispose(); this.saveRegion = null; } }
// TODO: add "name" parameter, keep this for legacy and fill it in with "Background" // goal is to put complete burden of loc on the client public static BitmapLayer CreateBackgroundLayer(int width, int height) { // set colors to 0xffffffff // note: we use alpha of 255 here so that "invert colors" works as expected // that is, for just 1 layer we invert the initial white->black // but on subsequent layers we invert transparent white -> transparent black, which shows up as white for the most part BitmapLayer layer = new BitmapLayer(width, height, ColorBgra.White); layer.Name = PdnResources.GetString("Layer.Background.Name"); // tag it as a background layer layer.properties.isBackground = true; return(layer); }
protected BitmapLayer(BitmapLayer copyMe) : base(copyMe) { this.surface = copyMe.Surface.Clone(); this.properties = (BitmapLayerProperties)copyMe.properties.Clone(); }
/// <summary> /// Creates a document that consists of one BitmapLayer. /// </summary> /// <param name="image">The Image to make a copy of that will be the first layer ("Background") in the document.</param> public static Document FromImage(Image image) { if (image == null) { throw new ArgumentNullException("image"); } Document document = new Document(image.Width, image.Height); BitmapLayer layer = Layer.CreateBackgroundLayer(image.Width, image.Height); layer.Surface.Clear(ColorBgra.FromBgra(0, 0, 0, 0)); Bitmap asBitmap = image as Bitmap; // Copy pixels if (asBitmap != null && asBitmap.PixelFormat == PixelFormat.Format32bppArgb) { unsafe { BitmapData bData = asBitmap.LockBits(new Rectangle(0, 0, asBitmap.Width, asBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); try { for (int y = 0; y < bData.Height; ++y) { uint * srcPtr = (uint *)((byte *)bData.Scan0.ToPointer() + (y * bData.Stride)); ColorBgra *dstPtr = layer.Surface.GetRowAddress(y); for (int x = 0; x < bData.Width; ++x) { dstPtr->Bgra = *srcPtr; ++srcPtr; ++dstPtr; } } } finally { asBitmap.UnlockBits(bData); bData = null; } } } else if (asBitmap != null && asBitmap.PixelFormat == PixelFormat.Format24bppRgb) { unsafe { BitmapData bData = asBitmap.LockBits(new Rectangle(0, 0, asBitmap.Width, asBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); try { for (int y = 0; y < bData.Height; ++y) { byte * srcPtr = (byte *)bData.Scan0.ToPointer() + (y * bData.Stride); ColorBgra *dstPtr = layer.Surface.GetRowAddress(y); for (int x = 0; x < bData.Width; ++x) { byte b = *srcPtr; byte g = *(srcPtr + 1); byte r = *(srcPtr + 2); byte a = 255; *dstPtr = ColorBgra.FromBgra(b, g, r, a); srcPtr += 3; ++dstPtr; } } } finally { asBitmap.UnlockBits(bData); bData = null; } } } else { using (RenderArgs args = new RenderArgs(layer.Surface)) { args.Graphics.CompositingMode = CompositingMode.SourceCopy; args.Graphics.SmoothingMode = SmoothingMode.None; args.Graphics.DrawImage(image, args.Bounds, args.Bounds, GraphicsUnit.Pixel); } } // Transfer metadata PropertyItem[] pis; try { pis = image.PropertyItems; } catch (Exception ex) { Tracing.Ping("Exception while retreiving image's PropertyItems: " + ex.ToString()); pis = null; // ignore the error and continue on } if (pis != null) { for (int i = 0; i < pis.Length; ++i) { document.Metadata.AddExifValues(new PropertyItem[] { pis[i] }); } } // Finish up document.Layers.Add(layer); document.Invalidate(); return(document); }
public void SaveRegion(PdnRegion saveMeRegion, Rectangle saveMeBounds) { BitmapLayer activeLayer = (BitmapLayer)ActiveLayer; if (savedTiles == null) { savedTiles = new BitVector2D( (activeLayer.Width + saveTileGranularity - 1) / saveTileGranularity, (activeLayer.Height + saveTileGranularity - 1) / saveTileGranularity); savedTiles.Clear(false); } Rectangle regionBounds; if (saveMeRegion == null) { regionBounds = saveMeBounds; } else { regionBounds = saveMeRegion.GetBoundsInt(); } Rectangle bounds = Rectangle.Union(regionBounds, saveMeBounds); bounds.Intersect(activeLayer.Bounds); int leftTile = bounds.Left / saveTileGranularity; int topTile = bounds.Top / saveTileGranularity; int rightTile = (bounds.Right - 1) / saveTileGranularity; int bottomTile = (bounds.Bottom - 1) / saveTileGranularity; for (int tileY = topTile; tileY <= bottomTile; ++tileY) { Rectangle rowAccumBounds = Rectangle.Empty; for (int tileX = leftTile; tileX <= rightTile; ++tileX) { if (!savedTiles.Get(tileX, tileY)) { Rectangle tileBounds = new Rectangle(tileX * saveTileGranularity, tileY * saveTileGranularity, saveTileGranularity, saveTileGranularity); tileBounds.Intersect(activeLayer.Bounds); if (rowAccumBounds == Rectangle.Empty) { rowAccumBounds = tileBounds; } else { rowAccumBounds = Rectangle.Union(rowAccumBounds, tileBounds); } savedTiles.Set(tileX, tileY, true); } else { if (rowAccumBounds != Rectangle.Empty) { using (Surface dst = ScratchSurface.CreateWindow(rowAccumBounds), src = activeLayer.Surface.CreateWindow(rowAccumBounds)) { dst.CopySurface(src); } rowAccumBounds = Rectangle.Empty; } } } if (rowAccumBounds != Rectangle.Empty) { using (Surface dst = ScratchSurface.CreateWindow(rowAccumBounds), src = activeLayer.Surface.CreateWindow(rowAccumBounds)) { dst.CopySurface(src); } rowAccumBounds = Rectangle.Empty; } } if (this.saveRegion != null) { this.saveRegion.Dispose(); this.saveRegion = null; } if (saveMeRegion != null) { this.saveRegion = saveMeRegion.Clone(); } }