Example #1
0
        public unsafe SharpDX.WIC.Bitmap CreateWICBitmapFromGDI(System.Drawing.Bitmap gdiBitmap)
        {
            var wicFactory = new ImagingFactory();
            var wicBitmap  = new SharpDX.WIC.Bitmap(wicFactory, gdiBitmap.Width, gdiBitmap.Height, PixelFormat.Format32bppBGRA, BitmapCreateCacheOption.CacheOnLoad);

            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, gdiBitmap.Width, gdiBitmap.Height);
            var   btmpData = gdiBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            byte *pGDIData = (byte *)btmpData.Scan0;

            using (BitmapLock bl = wicBitmap.Lock(BitmapLockFlags.Write))
            {
                byte *pWICData = (byte *)bl.Data.DataPointer;
                for (int y = 0; y < gdiBitmap.Height; y++)
                {
                    int offsetWIC = y * bl.Stride;
                    int offsetGDI = y * btmpData.Stride;
                    for (int x = 0; x < gdiBitmap.Width; x++)
                    {
                        pWICData[offsetWIC + 0] = pGDIData[offsetGDI + 0];
                        pWICData[offsetWIC + 1] = pGDIData[offsetGDI + 1];
                        pWICData[offsetWIC + 2] = pGDIData[offsetGDI + 2];
                        pWICData[offsetWIC + 3] = pGDIData[offsetGDI + 3];
                        offsetWIC += 4;
                        offsetGDI += 4;
                    }
                }
            }
            gdiBitmap.UnlockBits(btmpData);
            return(wicBitmap);
        }
Example #2
0
    /// <summary>
    /// Creates a new instance of <see cref="RawBitmapData"/> from a <see cref="Bitmap"/>
    /// </summary>
    /// <param name="bmp">The bitmap to get the raw data from</param>
    public RawBitmapData(Bitmap bmp)
    {
        using var bmpLock = new BitmapLock(bmp);

        Width       = bmp.Width;
        Height      = bmp.Height;
        PixelData   = bmpLock.Pixels;
        PixelFormat = bmp.PixelFormat;
    }
        /// <summary>
        /// Converts the import file data from the input stream to the output stream
        /// </summary>
        /// <param name="fileBytes">The file bytes</param>
        /// <param name="inputStream">The input stream to import from</param>
        /// <param name="outputStream">The destination stream</param>
        /// <param name="format">The file format to use</param>
        public void ConvertImportData(byte[] fileBytes, Stream inputStream, Stream outputStream, FileExtension format)
        {
            // Load the bitmap
            using var bmp = new Bitmap(inputStream);

            // Load the current file
            OpenSpaceGFFile gf = GetFileContent(fileBytes);

            // IDEA: If bmp is not in supported format, then convert it?

            RawBitmapData rawBitmapData;

            // Get the bitmap lock
            using (var bmpLock = new BitmapLock(bmp))
            {
                // Get the raw bitmap data
                rawBitmapData = new RawBitmapData(bmp.Width, bmp.Height, bmpLock.Pixels, bmp.PixelFormat);

                // Force the new pixel format to be 888 or 8888 if set to do so
                if (RCPServices.Data.Archive_GF_ForceGF8888Import)
                {
                    gf.GFPixelFormat = gf.GFPixelFormat.SupportsTransparency() ? OpenSpaceGFFormat.Format_32bpp_BGRA_8888 : OpenSpaceGFFormat.Format_24bpp_BGR_888;
                }

                // Check if the format should be updated for transparency
                if (RCPServices.Data.Archive_GF_UpdateTransparency != Archive_GF_TransparencyMode.PreserveFormat)
                {
                    // NOTE: Only 24 and 32 bpp bitmaps are supported
                    // Check if the imported file is transparent
                    var isTransparent = bmp.PixelFormat switch
                    {
                        PixelFormat.Format32bppArgb => (RCPServices.Data.Archive_GF_UpdateTransparency == Archive_GF_TransparencyMode.UpdateBasedOnPixelFormat ||
                                                        bmpLock.UtilizesAlpha()),
                        PixelFormat.Format24bppRgb => false,
                        _ => (bool?)null
                    };

                    // NOTE: Currently only supported for formats with 3 or 4 channels
                    // Check if the format should be updated for transparency
                    if (gf.Channels >= 3 && isTransparent != null)
                    {
                        // Update the format
                        gf.GFPixelFormat = isTransparent.Value ? OpenSpaceGFFormat.Format_32bpp_BGRA_8888 : OpenSpaceGFFormat.Format_24bpp_BGR_888;
                    }
                }
            }

            // Import the bitmap
            gf.ImportFromBitmap(Settings, rawBitmapData, RCPServices.Data.Archive_GF_GenerateMipmaps);

            // Serialize the data to get the bytes
            BinarySerializableHelpers.WriteToStream(gf, outputStream, Settings, RCPServices.App.GetBinarySerializerLogger());
        }

        #endregion
    }
        /// <inheritdoc />
        /// <summary>
        ///   Creates a single bitmap from the captured frames and returns an object with its information
        /// </summary>
        /// <returns>
        ///   A <see cref="VideoFrame" /> instance that can be either a <see cref="D3D11VideoFrame"/> or
        ///   a <see cref="BitmapVideoFrame" />
        /// </returns>
        public override VideoFrame LockFrame()
        {
            long presentTimeTicks = (long)(this.lastPresentTime * 10e6 / this.perfFreq);

            if (this.sources.Length == 1 && this.sources.First().Alive)
            {
                // TODO: if multiple textures are owned by a single adapter, merge them using CopySubresourceRegion
                return(new D3D11VideoFrame(this.sources[0].Texture, presentTimeTicks));
            }

            using (var factory = new ImagingFactory2()) {
                // create Bitmap but DON'T dispose it here
                var bmp = new Bitmap(factory,
                                     this.virtualRect.Width,
                                     this.virtualRect.Height,
                                     PixelFormat.Format32bppBGRA,
                                     BitmapCreateCacheOption.CacheOnDemand);

                // caller is responsible for disposing BitmapLock
                BitmapLock data = bmp.Lock(BitmapLockFlags.Write);
                int        minX = this.sources.Select(s => s.Region.Left).Min();
                int        minY = this.sources.Select(s => s.Region.Top).Min();

                // map textures
                foreach (DxgiCaptureSource source in this.sources)
                {
                    using (var res = source.Texture.QueryInterface <SharpDX.Direct3D11.Resource>()) {
                        DataBox map = source.Device.ImmediateContext.MapSubresource(res, 0, MapMode.Read, MapFlags.None);

                        // merge partial captures into one big bitmap
                        IntPtr dstScan0  = data.Data.DataPointer,
                               srcScan0  = map.DataPointer;
                        int dstStride    = data.Stride,
                            srcStride    = map.RowPitch;
                        int srcWidth     = source.Region.Right - source.Region.Left,
                            srcHeight    = source.Region.Bottom - source.Region.Top;
                        int dstPixelSize = dstStride / data.Size.Width,
                            srcPixelSize = srcStride / srcWidth;
                        int dstX         = source.Region.Left - minX,
                            dstY         = source.Region.Top - minY;

                        for (int y = 0; y < srcHeight; y++)
                        {
                            Utilities.CopyMemory(IntPtr.Add(dstScan0, dstPixelSize * dstX + (y + dstY) * dstStride),
                                                 IntPtr.Add(srcScan0, y * srcStride), srcPixelSize * srcWidth);
                        }

                        // release system memory
                        source.Device.ImmediateContext.UnmapSubresource(res, 0);
                    }
                }

                // return locked bitmap frame
                return(new BitmapVideoFrame(bmp, data, presentTimeTicks));
            }
        }
        /// <summary>
        ///   Creates a new video frame from a locked WIC bitmap
        /// </summary>
        /// <param name="bitmap">WIC bitmap</param>
        /// <param name="bitmapLock">Bitmap lock object</param>
        /// <param name="presentTime">Time, in 100-nanosecond units, of the frame capture</param>

        internal BitmapVideoFrame(BitmapSource bitmap, BitmapLock bitmapLock, long presentTime)
        {
            Width  = bitmap.Size.Width;
            Height = bitmap.Size.Height;

            PixelFormat = bitmap.PixelFormat;
            Bitmap      = bitmap;
            BitmapLock  = bitmapLock;

            PresentTime = presentTime;
        }
Example #6
0
        /// <inheritdoc />
        /// <summary>
        ///   Creates a single bitmap from the captured frames and returns an object with its information
        /// </summary>
        /// <returns>A <see cref="BitmapData" /> containing raw bitmap information</returns>
        public BitmapData LockFrameBitmap()
        {
            using (var factory = new ImagingFactory2()) {
                using (var bmp = new Bitmap(factory,
                                            CaptureBounds.Width,
                                            CaptureBounds.Height,
                                            PixelFormat.Format32bppBGRA,
                                            BitmapCreateCacheOption.CacheOnDemand)) {
                    // caller is responsible for disposing BitmapLock
                    BitmapLock data = bmp.Lock(BitmapLockFlags.Write);
                    int        minX = this.rects.Select(b => b.X).Min();
                    int        minY = this.rects.Select(b => b.Y).Min();

                    // map textures
                    for (int i = 0; i < StagingTextures.Length; i++)
                    {
                        DataBox map = StagingTextures[i]
                                      .Device.ImmediateContext
                                      .MapSubresource(StagingTextures[i], 0, MapMode.Read, MapFlags.None);

                        IntPtr dstScan0 = data.Data.DataPointer,
                               srcScan0 = map.DataPointer;

                        int dstStride    = data.Stride,
                            srcStride    = map.RowPitch;
                        int srcWidth     = this.rects[i].Width,
                            srcHeight    = this.rects[i].Height;
                        int dstPixelSize = dstStride / data.Size.Width,
                            srcPixelSize = srcStride / srcWidth;
                        int dstX         = this.rects[i].X - minX,
                            dstY         = this.rects[i].Y - minY;

                        for (int y = 0; y < srcHeight; y++)
                        {
                            Utilities.CopyMemory(IntPtr.Add(dstScan0,
                                                            dstPixelSize * dstX + (y + dstY) * dstStride),
                                                 IntPtr.Add(srcScan0, y * srcStride),
                                                 srcPixelSize * srcWidth);
                        }

                        StagingTextures[i].Device.ImmediateContext.UnmapSubresource(StagingTextures[i], 0);
                    }

                    return(new BitmapData {
                        Width = CaptureBounds.Width,
                        Height = CaptureBounds.Height,
                        PixelFormat = bmp.PixelFormat,
                        Scan0 = data.Data.DataPointer,
                        Stride = data.Stride,
                        LockPointer = data.NativePointer
                    });
                }
            }
        }
Example #7
0
    /// <summary>
    /// Gets a bitmap from the raw image data
    /// </summary>
    /// <returns>The bitmap</returns>
    public Bitmap GetBitmap()
    {
        // Create the bitmap
        Bitmap bmp = new Bitmap(Width, Height, PixelFormat);

        // Lock and update the pixels
        using (var bmpLock = new BitmapLock(bmp))
            bmpLock.Pixels = PixelData;

        // Return the bitmap
        return(bmp);
    }
        /// <inheritdoc />
        /// <summary>
        ///   Creates a single bitmap from the captured frames and returns an object with its information
        /// </summary>
        /// <returns>A <see cref="BitmapData" /> containing raw bitmap information</returns>
        public override BitmapData LockFrameBitmap()
        {
            using (var factory
                       = new ImagingFactory2()) {
                var bmp
                    = new Bitmap(factory,
                                 CaptureBounds.Width,
                                 CaptureBounds.Height,
                                 PixelFormat.Format32bppBGRA,
                                 BitmapCreateCacheOption.CacheOnDemand);

                // caller is responsible for disposing BitmapLock
                BitmapLock data
                    = bmp.Lock(BitmapLockFlags.Write);
                int minX
                    = this.rects.Select(b => b.X).Min();
                int minY
                    = this.rects.Select(b => b.Y).Min();

                // map textures
                for (int i
                         = 0; i < Surfaces.Length; i++)
                {
                    DataRectangle map
                        = Surfaces[i].LockRectangle(LockFlags.ReadOnly);
                    IntPtr dstScan0
                        = data.Data.DataPointer,
                           srcScan0
                        = map.DataPointer;

                    int dstStride
                        = data.Stride,
                        srcStride
                        = map.Pitch;
                    int srcWidth
                        = this.regions[i].Width,
                        srcHeight
                        = this.regions[i].Height;
                    int dstPixelSize
                        = dstStride / data.Size.Width,
                        srcPixelSize
                        = srcStride / srcWidth;
                    int dstX
                        = this.rects[i].X - minX,
                        dstY
                        = this.rects[i].Y - minY;

                    for (int y
                             = 0; y < srcHeight; y++)
                    {
                        Utilities.CopyMemory(IntPtr.Add(dstScan0,
                                                        dstPixelSize * dstX + (y + dstY) * dstStride),
                                             IntPtr.Add(srcScan0, y * srcStride),
                                             srcPixelSize * srcWidth);
                    }

                    Surfaces[i].UnlockRectangle();
                }

                return(new BitmapData {
                    Width
                        = CaptureBounds.Width,
                    Height
                        = CaptureBounds.Height,
                    PixelFormat
                        = bmp.PixelFormat,
                    Scan0
                        = data.Data.DataPointer,
                    Stride
                        = data.Stride,
                    LockPointer
                        = data.NativePointer
                });
            }
        }
Example #9
0
 public Image(RenderTarget renderTarget, BitmapLock bitmapLock, BitmapProperties?bitmapProperties) : base(
         renderTarget, bitmapLock, bitmapProperties)
 {
     _width  = (int)Size.Width;
     _height = (int)Size.Height;
 }
Example #10
0
 public Image(RenderTarget renderTarget, BitmapLock bitmapLock) : base(renderTarget, bitmapLock)
 {
     _width  = (int)Size.Width;
     _height = (int)Size.Height;
 }
 public LockedBitmap(WriteableWicBitmapImpl parent, BitmapLock l, PixelFormat format)
 {
     _parent = parent;
     _lock   = l;
     _format = format;
 }
Example #12
0
 public LockedBitmap(BitmapLock l, PixelFormat format)
 {
     _lock   = l;
     _format = format;
 }