Accord.Imaging.UnmanagedImage.Clone C# (CSharp) Method

Clone() public method

Clone the unmanaged images.

The method does complete cloning of the object.

public Clone ( ) : UnmanagedImage
return UnmanagedImage
        public UnmanagedImage Clone()
        {
            // allocate memory for the image
            IntPtr newImageData = System.Runtime.InteropServices.Marshal.AllocHGlobal(stride * height);
            System.GC.AddMemoryPressure(stride * height);

            UnmanagedImage newImage = new UnmanagedImage(newImageData, width, height, stride, pixelFormat);
            newImage.mustBeDisposed = true;

            Accord.SystemTools.CopyUnmanagedMemory(newImageData, imageData, stride * height);

            return newImage;
        }

Usage Example

コード例 #1
0
        /// <summary>
        ///   Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        /// 
        protected unsafe override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            int width = sourceData.Width;
            int height = sourceData.Height;
            PixelFormat format = sourceData.PixelFormat;
            int pixelSize = System.Drawing.Bitmap.GetPixelFormatSize(format) / 8;

            sourceData.Clone();

            UnmanagedImage temp = UnmanagedImage.Create(width, height, format);

            int lineWidth = width * pixelSize;

            int srcStride = temp.Stride;
            int srcOffset = srcStride - lineWidth;
            int dstStride = destinationData.Stride;
            int dstOffset = dstStride - lineWidth;

            byte* srcStart = (byte*)temp.ImageData.ToPointer();
            byte* dstStart = (byte*)destinationData.ImageData.ToPointer();


            // first
            Convolution c = new Convolution(masks[0]);
            c.Apply(sourceData, destinationData);

            // others
            for (int i = 1; i < masks.Length; i++)
            {
                c.Kernel = masks[i];
                c.Apply(sourceData, temp);

                byte* src = srcStart;
                byte* dst = dstStart;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < lineWidth; x++, src++, dst++)
                    {
                        if (*src > *dst)
                            *dst = *src;
                    }

                    dst += dstOffset;
                    src += srcOffset;
                }
            }
        }