Пример #1
0
 internal Pix(IntPtr pointer)
     : base(pointer)
 {
     Width  = Leptonica5Pix.pixGetWidth(HandleRef);
     Height = Leptonica5Pix.pixGetHeight(HandleRef);
     Depth  = Leptonica5Pix.pixGetDepth(HandleRef);
 }
Пример #2
0
 public void AddPix(IntPtr pixPointer)
 {
     if (Leptonica5Pix.pixaAddPix(HandleRef.Handle, pixPointer, 1) != 0)
     {
         throw new InvalidOperationException("Leptonica failed.");
     }
 }
Пример #3
0
        private static IEnumerable <IPix> ReadTiff(IntPtr pixaPointer)
        {
            try
            {
                var pagesCount = Leptonica5Pix.pixaGetCount(pixaPointer);
                if (pagesCount <= 0)
                {
                    throw new InvalidOperationException("File has no pages.");
                }

                var pages = new List <Pix>();
                for (var i = 0; i < pagesCount; i++)
                {
                    try
                    {
                        var pagePointer = Leptonica5Pix.pixaGetPix(pixaPointer, i, 2).GetPointerOrThrow();
                        var page        = new Pix(pagePointer);
                        pages.Add(page);
                    }
                    catch
                    {
                        Destroy(pages);
                        throw;
                    }
                }

                return(pages);
            }
            finally
            {
                Leptonica5Pix.pixaDestroy(ref pixaPointer);
            }
        }
Пример #4
0
 public void AddBox(IntPtr boxPointer)
 {
     if (Leptonica5Pix.boxaAddBox(HandleRef.Handle, boxPointer, 0) != 0)
     {
         throw new InvalidOperationException("Leptonica failed.");
     }
 }
Пример #5
0
 internal Pix(int imageWidth, int imageHeight, int imageDepth)
     : base(() => Leptonica5Pix.pixCreate(imageWidth, imageHeight, imageDepth))
 {
     Width  = imageWidth;
     Height = imageHeight;
     Depth  = imageDepth;
 }
Пример #6
0
        public unsafe IEnumerable <IPix> Create(ReadOnlyMemory <byte> data)
        {
            using var pointer = data.Pin();
            if (Leptonica5Pix.findFileFormatBuffer(pointer.Pointer, out var format) != 0)
            {
                throw new InvalidOperationException("File format not supported.");
            }

            var sizeInMemory = data.Length;

            switch (format)
            {
            case ImageFileFormat.Tiff:
            case ImageFileFormat.TiffPackbits:
            case ImageFileFormat.TiffRle:
            case ImageFileFormat.TiffG3:
            case ImageFileFormat.TiffG4:
            case ImageFileFormat.TiffLzw:
            case ImageFileFormat.TiffZip:
                return(ReadTiff(pointer.Pointer, sizeInMemory));

            default:
                return(new [] { ReadImage(pointer.Pointer, sizeInMemory) });
            }
        }
Пример #7
0
 internal Pix(string imageFilePath)
     : base(() => Leptonica5Pix.pixRead(imageFilePath))
 {
     Width  = Leptonica5Pix.pixGetWidth(HandleRef);
     Height = Leptonica5Pix.pixGetHeight(HandleRef);
     Depth  = Leptonica5Pix.pixGetDepth(HandleRef);
 }
Пример #8
0
 private static void Destroy(IEnumerable <Pix> pages)
 {
     foreach (var page in pages)
     {
         var pointer = page.HandleRef.Handle;
         Leptonica5Pix.pixDestroy(ref pointer);
     }
 }
Пример #9
0
        public PixData(Pix pix)
        {
            if (pix == null)
            {
                throw new ArgumentNullException(nameof(pix));
            }

            Data         = Leptonica5Pix.pixGetData(pix.HandleRef);
            WordsPerLine = Leptonica5Pix.pixGetWpl(pix.HandleRef);
        }
Пример #10
0
        public ImageFileFormat GetFileFormat(ReadOnlyMemory <byte> memory)
        {
            using var pointer = memory.Pin();
            if (Leptonica5Pix.findFileFormatBuffer(pointer.Pointer, out var format) != 0)
            {
                return(ImageFileFormat.Unknown);
            }

            return(format);
        }
Пример #11
0
        public ImageFileFormat GetFileFormat(ReadOnlySpan <byte> span)
        {
            fixed(byte *p = span)
            {
                if (Leptonica5Pix.findFileFormatBuffer(p, out var format) != 0)
                {
                    return(ImageFileFormat.Unknown);
                }

                return(format);
            }
        }
Пример #12
0
        public void AddPix(IPix pix)
        {
            if (pix == null)
            {
                throw new ArgumentNullException(nameof(pix));
            }

            if (Leptonica5Pix.pixaAddPix(HandleRef.Handle, pix.HandleRef.Handle, 1) != 0)
            {
                throw new InvalidOperationException("Leptonica failed.");
            }
        }
Пример #13
0
        public IEnumerable <IPix> Create(string imageFilePath)
        {
            if (Leptonica5Pix.findFileFormat(imageFilePath, out var format) != 0)
            {
                throw new InvalidOperationException("File format not supported.");
            }

            switch (format)
            {
            case ImageFileFormat.Default:
            case ImageFileFormat.Tiff:
            case ImageFileFormat.TiffPackbits:
            case ImageFileFormat.TiffRle:
            case ImageFileFormat.TiffG3:
            case ImageFileFormat.TiffG4:
            case ImageFileFormat.TiffLzw:
            case ImageFileFormat.TiffZip:
                return(ReadTiff(imageFilePath));

            default:
                return(new [] { ReadImage(imageFilePath) });
            }
        }
Пример #14
0
 public Boxa(int initialPointersCount = 0)
     : base(() => Leptonica5Pix.boxaCreate(initialPointersCount))
 {
 }
Пример #15
0
 protected override void DestroyObject(ref IntPtr pointer) => Leptonica5Pix.boxaDestroy(ref pointer);
Пример #16
0
        private static unsafe IEnumerable <IPix> ReadTiff(void *nativePointer, int size)
        {
            var pointer = Leptonica5Pix.pixaReadMemMultipageTiff(nativePointer, size).GetPointerOrThrow();

            return(ReadTiff(pointer));
        }
Пример #17
0
 public PixColormap(int depth)
     : base(() => Leptonica5Pix.pixcmapCreate(depth))
 {
 }
Пример #18
0
        private static IEnumerable <IPix> ReadTiff(string filename)
        {
            var pointer = Leptonica5Pix.pixaReadMultipageTiff(filename).GetPointerOrThrow();

            return(ReadTiff(pointer));
        }
Пример #19
0
        private static IPix ReadImage(string filename)
        {
            var pixPointer = Leptonica5Pix.pixRead(filename).GetPointerOrThrow();

            return(new Pix(pixPointer));
        }
Пример #20
0
        private static unsafe IPix ReadImage(void *pointer, int size)
        {
            var pixPointer = Leptonica5Pix.pixReadMem(pointer, size).GetPointerOrThrow();

            return(new Pix(pixPointer));
        }
Пример #21
0
 public bool AddColor(IPixColor color)
 {
     return(Leptonica5Pix.pixcmapAddColor(HandleRef, color.Red, color.Green, color.Blue) == 0);
 }
Пример #22
0
 protected override void DestroyObject(ref IntPtr pointer)
 {
     Leptonica5Pix.pixcmapDestroy(ref pointer);
 }