protected override bool TryCopyTo(BitmapContent destinationBitmap, Rectangle sourceRegion, Rectangle destinationRegion) { SurfaceFormat destinationFormat; if (!destinationBitmap.TryGetFormat(out destinationFormat)) { return(false); } SurfaceFormat format; TryGetFormat(out format); // A shortcut for copying the entire bitmap to another bitmap of the same type and format var fullRegion = new Rectangle(0, 0, Width, Height); if ((format == destinationFormat) && (sourceRegion == fullRegion) && (sourceRegion == destinationRegion)) { destinationBitmap.SetPixelData(GetPixelData()); return(true); } // No other support for copying from a DXT texture yet return(false); }
bool WriteDataInternal(IntPtr data, int length) { Marshal.Copy(data, _buffer, _offset, length); _offset += length; if (_offset == _buffer.Length) { _content.SetPixelData(_buffer); } return(true); }
/// <summary> /// Copies one bitmap into another. /// The destination bitmap can be in any format and size. If the destination is larger or smaller, the source bitmap is scaled accordingly. /// </summary> /// <param name="sourceBitmap">BitmapContent being copied.</param> /// <param name="sourceRegion">Region of sourceBitmap.</param> /// <param name="destinationBitmap">BitmapContent being overwritten.</param> /// <param name="destinationRegion">Region of bitmap to be overwritten.</param> public static void Copy(BitmapContent sourceBitmap, Rectangle sourceRegion, BitmapContent destinationBitmap, Rectangle destinationRegion) { ValidateCopyArguments(sourceBitmap, sourceRegion, destinationBitmap, destinationRegion); SurfaceFormat sourceFormat; if (!sourceBitmap.TryGetFormat(out sourceFormat)) { throw new InvalidOperationException("Could not retrieve surface format of source bitmap"); } SurfaceFormat destinationFormat; if (!destinationBitmap.TryGetFormat(out destinationFormat)) { throw new InvalidOperationException("Could not retrieve surface format of destination bitmap"); } // If the formats are the same and the regions are the full bounds of the bitmaps and they are the same size, do a simpler copy if (sourceFormat == destinationFormat && sourceRegion == destinationRegion && sourceRegion == new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height) && destinationRegion == new Rectangle(0, 0, destinationBitmap.Width, destinationBitmap.Height)) { destinationBitmap.SetPixelData(sourceBitmap.GetPixelData()); return; } // The basic process is // 1. Copy from source bitmap region to a new PixelBitmapContent<Vector4> using sourceBitmap.TryCopyTo() // 2. If source and destination regions are a different size, resize Vector4 version // 3. Copy from Vector4 to destination region using destinationBitmap.TryCopyFrom() // Copy from the source to the intermediate Vector4 format var intermediate = new PixelBitmapContent <Vector4>(sourceRegion.Width, sourceRegion.Height); var intermediateRegion = new Rectangle(0, 0, intermediate.Width, intermediate.Height); if (sourceBitmap.TryCopyTo(intermediate, sourceRegion, intermediateRegion)) { // Resize the intermediate if required if (intermediate.Width != destinationRegion.Width || intermediate.Height != destinationRegion.Height) { intermediate = intermediate.Resize(destinationRegion.Width, destinationRegion.Height) as PixelBitmapContent <Vector4>; } // Copy from the intermediate to the destination if (destinationBitmap.TryCopyFrom(intermediate, new Rectangle(0, 0, intermediate.Width, intermediate.Height), destinationRegion)) { return; } } // If we got here, one of the above steps didn't work throw new InvalidOperationException("Could not copy between " + sourceFormat + " and " + destinationFormat); }
protected override bool TryCopyTo(BitmapContent destinationBitmap, Rectangle sourceRegion, Rectangle destinationRegion) { SurfaceFormat destinationFormat; if (!destinationBitmap.TryGetFormat(out destinationFormat)) return false; SurfaceFormat format; TryGetFormat(out format); // A shortcut for copying the entire bitmap to another bitmap of the same type and format var fullRegion = new Rectangle(0, 0, Width, Height); if ((format == destinationFormat) && (sourceRegion == fullRegion) && (sourceRegion == destinationRegion)) { destinationBitmap.SetPixelData(GetPixelData()); return true; } // No other support for copying from a DXT texture yet return false; }
/// <summary> /// Copies one bitmap into another. /// The destination bitmap can be in any format and size. If the destination is larger or smaller, the source bitmap is scaled accordingly. /// </summary> /// <param name="sourceBitmap">BitmapContent being copied.</param> /// <param name="sourceRegion">Region of sourceBitmap.</param> /// <param name="destinationBitmap">BitmapContent being overwritten.</param> /// <param name="destinationRegion">Region of bitmap to be overwritten.</param> public static void Copy(BitmapContent sourceBitmap, Rectangle sourceRegion, BitmapContent destinationBitmap, Rectangle destinationRegion) { ValidateCopyArguments(sourceBitmap, sourceRegion, destinationBitmap, destinationRegion); SurfaceFormat sourceFormat; if (!sourceBitmap.TryGetFormat(out sourceFormat)) throw new InvalidOperationException("Could not retrieve surface format of source bitmap"); SurfaceFormat destinationFormat; if (!destinationBitmap.TryGetFormat(out destinationFormat)) throw new InvalidOperationException("Could not retrieve surface format of destination bitmap"); // If the formats are the same and the regions are the full bounds of the bitmaps and they are the same size, do a simpler copy if (sourceFormat == destinationFormat && sourceRegion == destinationRegion && sourceRegion == new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height) && destinationRegion == new Rectangle(0, 0, destinationBitmap.Width, destinationBitmap.Height)) { destinationBitmap.SetPixelData(sourceBitmap.GetPixelData()); return; } // The basic process is // 1. Copy from source bitmap region to a new PixelBitmapContent<Vector4> using sourceBitmap.TryCopyTo() // 2. If source and destination regions are a different size, resize Vector4 version // 3. Copy from Vector4 to destination region using destinationBitmap.TryCopyFrom() // Copy from the source to the intermediate Vector4 format var intermediate = new PixelBitmapContent<Vector4>(sourceRegion.Width, sourceRegion.Height); var intermediateRegion = new Rectangle(0, 0, intermediate.Width, intermediate.Height); if (sourceBitmap.TryCopyTo(intermediate, sourceRegion, intermediateRegion)) { // Resize the intermediate if required if (intermediate.Width != destinationRegion.Width || intermediate.Height != destinationRegion.Height) intermediate = intermediate.Resize(destinationRegion.Width, destinationRegion.Height) as PixelBitmapContent<Vector4>; // Copy from the intermediate to the destination if (destinationBitmap.TryCopyFrom(intermediate, new Rectangle(0, 0, intermediate.Width, intermediate.Height), destinationRegion)) return; } // If we got here, one of the above steps didn't work throw new InvalidOperationException("Could not copy between " + sourceFormat + " and " + destinationFormat); }
protected override bool TryCopyTo(BitmapContent destinationBitmap, Rectangle sourceRegion, Rectangle destinationRegion) { SurfaceFormat destinationFormat; if (!destinationBitmap.TryGetFormat(out destinationFormat)) { return(false); } // A shortcut for copying the entire bitmap to another bitmap of the same type and format if (_format == destinationFormat && (sourceRegion == new Rectangle(0, 0, Width, Height)) && sourceRegion == destinationRegion) { destinationBitmap.SetPixelData(GetPixelData()); return(true); } // If the destination is not Vector4 or requires resizing, send it through BitmapContent.Copy if (!(destinationBitmap is PixelBitmapContent <Vector4>) || sourceRegion.Width != destinationRegion.Width || sourceRegion.Height != destinationRegion.Height) { try { BitmapContent.Copy(this, sourceRegion, destinationBitmap, destinationRegion); return(true); } catch (InvalidOperationException) { return(false); } } // Convert to a Vector4 format var dest = destinationBitmap as PixelBitmapContent <Vector4>; if (default(T) is IPackedVector) { Parallel.For(0, sourceRegion.Height, (y) => { var row = GetRow(sourceRegion.Top + y); for (int x = 0; x < sourceRegion.Width; ++x) { dest.SetPixel(destinationRegion.Left + x, destinationRegion.Top + y, ((IPackedVector)row[sourceRegion.Left + x]).ToVector4()); } }); } else { var converter = new Vector4Converter() as IVector4Converter <T>; // If no converter could be created, converting from this format is not supported if (converter == null) { return(false); } Parallel.For(0, sourceRegion.Height, (y) => { var row = GetRow(sourceRegion.Top + y); for (int x = 0; x < sourceRegion.Width; ++x) { dest.SetPixel(destinationRegion.Left + x, destinationRegion.Top + y, converter.ToVector4(row[sourceRegion.Left + x])); } }); } return(true); }