Пример #1
0
        /// <summary>
        /// Creates a new Leptonica's <see cref="Pix"/> object from the <see cref="Image"/>.
        /// </summary>
        /// <param name="image">The <see cref="Image"/> to convert.</param>
        /// <returns>
        /// The <see cref="Pix"/> object this method creates.
        /// </returns>
        public static Pix FromImage(Image image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            SafePixHandle handle = NativeMethods.pixCreate(image.Width, image.Height, image.BitsPerPixel);

            try
            {
                NativeMethods.pixSetResolution(handle, image.HorizontalResolution, image.VerticalResolution);

                unsafe
                {
                    uint *dst = (uint *)NativeMethods.pixGetData(handle).ToPointer();
                    int   wpl = NativeMethods.pixGetWpl(handle);

                    fixed(ulong *src = image.Bits)
                    {
                        Arrays.CopyStrides(image.Height, new IntPtr(src), image.Stride8, new IntPtr(dst), wpl * sizeof(uint));

                        int count = image.Height * wpl;

                        BitUtils.BiteSwap(count, dst);

                        if (image.BitsPerPixel < 8)
                        {
                            Vectors.SwapBits(count, image.BitsPerPixel, dst);
                        }
                    }
                }
            }
            catch
            {
                handle?.Dispose();
                throw;
            }

            return(new Pix(handle));
        }