示例#1
0
文件: Pix.cs 项目: arnavdas88/dnn
        /// <summary>
        /// Creates an <see cref="Image"/> object from this Leptonica's <see cref="Pix"/> object.
        /// </summary>
        /// <returns>
        /// The <see cref="Image"/> object this method creates.
        /// </returns>
        public Image ToImage()
        {
            NativeMethods.pixGetDimensions(this.handle, out int w, out int h, out int d);
            NativeMethods.pixGetResolution(this.handle, out int xres, out int yres);
            IntPtr src = NativeMethods.pixGetData(this.handle);
            int    wpl = NativeMethods.pixGetWpl(this.handle);

            Image image = new Image(w, h, d, xres == 0 ? 200 : xres, yres == 0 ? 200 : yres);

            unsafe
            {
                fixed(ulong *bits = image.Bits)
                {
                    uint *dst = (uint *)bits;

                    Arrays.CopyStrides(image.Height, src, wpl * sizeof(uint), new IntPtr(dst), image.Stride8);

                    int count = image.Bits.Length * 2; // work with 32-bit words

                    BitUtils.BiteSwap(count, dst);

                    if (image.BitsPerPixel < 8)
                    {
                        Vectors.SwapBits(count, image.BitsPerPixel, dst);
                    }
                }
            }

            return(image);
        }
示例#2
0
文件: Pix.cs 项目: arnavdas88/dnn
        /// <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));
        }