public void CopyPixelsFrom(byte *srcData, int srcWidth, int srcHeight, int srcStride, bool flipVertical = false) { using (this.UseWrite()) { if (srcWidth <= 0 || srcHeight <= 0) { return; } int copyBytes = srcWidth <= this.Width ? Math.Min(srcStride, this.Stride) : (this.Width * 4); if (!flipVertical) { byte *dest = this.Data; byte *src = srcData; byte *srcDataEnd = srcData + srcStride * srcHeight; do { Ut.MemCpy(dest, src, copyBytes); dest += this.Stride; src += srcStride; }while (dest < this.DataEnd && src < srcDataEnd); } else { byte *dest = this.Data; byte *src = srcData + srcStride * srcHeight; do { src -= srcStride; Ut.MemCpy(dest, src, copyBytes); dest += this.Stride; }while (dest < this.DataEnd && src > srcData); } } }
/// <summary>Flips the image vertically.</summary> public void FlipVert() { using (UseWrite()) { int yTop = 0; int yBtm = Height - 1; byte *temp = stackalloc byte[Stride]; while (yTop < yBtm) { Ut.MemCpy(temp, Data + yTop * Stride, Stride); Ut.MemCpy(Data + yTop * Stride, Data + yBtm * Stride, Stride); Ut.MemCpy(Data + yBtm * Stride, temp, Stride); yTop++; yBtm--; } } }
public MagickImage ToMagickImage() { // Copy the pixel data while eliminating the Stride var data = new byte[Width * Height * 4]; using (UseRead()) fixed(byte *dataPtr = data) for (int y = 0; y < Height; y++) { Ut.MemCpy(dataPtr + y * Width * 4, Data + y * Stride, Width * 4); } // Construct a MagickImage by reading the BGRA pixel data // Cannot use GetWritablePixels due to an occasional access violation exception that's proved too elusive to track down within the available time. return(new MagickImage(data, new MagickReadSettings { Width = Width, Height = Height, Format = MagickFormat.Bgra })); }
/// <summary>A convenience method to save a GDI image directly to a .tga file.</summary> public static unsafe void Save(BitmapBase image, string filename) { using (image.UseRead()) using (var file = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read)) { var header = new byte[18]; header[2] = 2; header[12] = (byte)(image.Width); header[13] = (byte)(image.Width >> 8); header[14] = (byte)(image.Height); header[15] = (byte)(image.Height >> 8); header[16] = 32; header[17] = 32; file.Write(header); byte[] dummy = new byte[image.Width * 4]; for (int y = 0; y < image.Height; y++) { Ut.MemCpy(dummy, image.Data + y * image.Stride, image.Width * 4); file.Write(dummy); } } }