示例#1
0
        private string GetCode(IPdfImage pdfImage, double pageHeight, int level)
        {
            imageCount++;
            var bbox = pdfImage.Bounds;

            return(GetIndent(level) + @"<span class='ocr_image' id='image_" + pageCount + "_"
                   + imageCount + "' title='" + GetCode(bbox, pageHeight) + "' />");
        }
示例#2
0
        private PageXmlDocument.PageXmlImageRegion ToPageXmlImageRegion(IPdfImage pdfImage, double pageWidth, double pageHeight)
        {
            regionCount++;
            var bbox = pdfImage.Bounds;

            return(new PageXmlDocument.PageXmlImageRegion()
            {
                Coords = ToCoords(bbox, pageWidth, pageHeight),
                Id = "r" + regionCount
            });
        }
示例#3
0
        public IEnumerable <IPdfImage> GetImages()
        {
            foreach (var image in images)
            {
                IPdfImage result = null;
                image.Match(x => { result = XObjectFactory.ReadImage(x, pdfScanner, filterProvider, resourceStore, isLenientParsing); },
                            x => { result = x; });

                yield return(result);
            }
        }
        public static bool TryGenerate(IPdfImage image, out byte[] bytes)
        {
            bytes = null;

            var isColorSpaceSupported = image.ColorSpace == ColorSpace.DeviceGray || image.ColorSpace == ColorSpace.DeviceRGB;

            if (!isColorSpaceSupported || !image.TryGetBytes(out var bytesPure))
            {
                return(false);
            }

            try
            {
                var is3Byte    = image.ColorSpace == ColorSpace.DeviceRGB;
                var multiplier = is3Byte ? 3 : 1;

                var builder = PngBuilder.Create(image.WidthInSamples, image.HeightInSamples, false);

                var isCorrectlySized = bytesPure.Count == (image.WidthInSamples * image.HeightInSamples * (image.BitsPerComponent / 8) * multiplier);

                if (!isCorrectlySized)
                {
                    return(false);
                }

                var i = 0;
                for (var y = 0; y < image.HeightInSamples; y++)
                {
                    for (var x = 0; x < image.WidthInSamples; x++)
                    {
                        if (is3Byte)
                        {
                            builder.SetPixel(bytesPure[i++], bytesPure[i++], bytesPure[i++], x, y);
                        }
                        else
                        {
                            var pixel = bytesPure[i++];
                            builder.SetPixel(pixel, pixel, pixel, x, y);
                        }
                    }
                }

                bytes = builder.Save();

                return(true);
            }
            catch
            {
                // ignored.
            }

            return(false);
        }
示例#5
0
        private AltoDocument.AltoIllustration ToAltoIllustration(IPdfImage pdfImage, decimal height)
        {
            illustrationCount++;
            var rectangle = pdfImage.Bounds;

            return(new AltoDocument.AltoIllustration
            {
                VerticalPosition = (float)Math.Round((height - rectangle.Top) * scale),
                HorizontalPosition = (float)Math.Round(rectangle.Left * scale),
                Height = (float)Math.Round(rectangle.Height * scale),
                Width = (float)Math.Round(rectangle.Width * scale),
                FileId = "",
                Rotation = 0,
                Id = "P" + pageCount + "_I" + illustrationCount.ToString("#00000")
            });
        }
        private void DrawImage(IPdfImage image, Graphics graphics)
        {
            if (image.TryGetPng(out var png))
            {
#pragma warning disable IDE0063 // Use simple 'using' statement
                using (var img = Image.FromStream(new MemoryStream(png)))
#pragma warning restore IDE0063 // Use simple 'using' statement
                {
                    img.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    graphics.DrawImage(img, new RectangleF(0, 0, 1, 1));
                }
            }
            else
            {
                if (image.TryGetBytes(out var bytes))
                {
                    try
                    {
#pragma warning disable IDE0063 // Use simple 'using' statement
                        using (var img = Image.FromStream(new MemoryStream(bytes.ToArray())))
#pragma warning restore IDE0063 // Use simple 'using' statement
                        {
                            img.RotateFlip(RotateFlipType.RotateNoneFlipY);
                            graphics.DrawImage(img, new RectangleF(0, 0, 1, 1));
                        }
                        return;
                    }
                    catch (Exception)
                    { }
                }

                try
                {
#pragma warning disable IDE0063 // Use simple 'using' statement
                    using (var img = Image.FromStream(new MemoryStream(image.RawBytes.ToArray())))
#pragma warning restore IDE0063 // Use simple 'using' statement
                    {
                        img.RotateFlip(RotateFlipType.RotateNoneFlipY);
                        graphics.DrawImage(img, new RectangleF(0, 0, 1, 1));
                    }
                }
                catch (Exception)
                {
                    graphics.FillRectangle(Brushes.HotPink, new RectangleF(0, 0, 1, 1));
                }
            }
        }
示例#7
0
 public void AddImage(IPdfImage image)
 {
     top?.AddImage(image);
 }
示例#8
0
 public void AddImage(IPdfImage image)
 {
     images.Add(image);
 }
示例#9
0
        public static bool TryGenerate(IPdfImage image, out byte[] bytes)
        {
            bytes = null;

            var hasValidDetails = image.ColorSpaceDetails != null &&
                                  !(image.ColorSpaceDetails is UnsupportedColorSpaceDetails);
            var actualColorSpace = hasValidDetails ? image.ColorSpaceDetails.BaseType : image.ColorSpace;

            var isColorSpaceSupported =
                actualColorSpace == ColorSpace.DeviceGray || actualColorSpace == ColorSpace.DeviceRGB ||
                actualColorSpace == ColorSpace.DeviceCMYK || actualColorSpace == ColorSpace.CalGray ||
                actualColorSpace == ColorSpace.CalRGB;

            if (!isColorSpaceSupported || !image.TryGetBytes(out var bytesPure))
            {
                return(false);
            }

            try
            {
                bytesPure = ColorSpaceDetailsByteConverter.Convert(image.ColorSpaceDetails, bytesPure,
                                                                   image.BitsPerComponent, image.WidthInSamples, image.HeightInSamples);

                var numberOfComponents =
                    actualColorSpace == ColorSpace.DeviceCMYK ? 4 :
                    actualColorSpace == ColorSpace.DeviceRGB ? 3 :
                    actualColorSpace == ColorSpace.CalRGB ? 3 : 1;

                var is3Byte = numberOfComponents == 3;

                var builder = PngBuilder.Create(image.WidthInSamples, image.HeightInSamples, false);

                var requiredSize = (image.WidthInSamples * image.HeightInSamples * numberOfComponents);

                var actualSize       = bytesPure.Count;
                var isCorrectlySized = bytesPure.Count == requiredSize ||
                                       // Spec, p. 37: "...error if the stream contains too much data, with the exception that
                                       // there may be an extra end-of-line marker..."
                                       (actualSize == requiredSize + 1 && bytesPure[actualSize - 1] == ReadHelper.AsciiLineFeed) ||
                                       (actualSize == requiredSize + 1 && bytesPure[actualSize - 1] == ReadHelper.AsciiCarriageReturn) ||
                                       // The combination of a CARRIAGE RETURN followed immediately by a LINE FEED is treated as one EOL marker.
                                       (actualSize == requiredSize + 2 &&
                                        bytesPure[actualSize - 2] == ReadHelper.AsciiCarriageReturn &&
                                        bytesPure[actualSize - 1] == ReadHelper.AsciiLineFeed);

                if (!isCorrectlySized)
                {
                    return(false);
                }

                var i = 0;
                for (var col = 0; col < image.HeightInSamples; col++)
                {
                    for (var row = 0; row < image.WidthInSamples; row++)
                    {
                        if (actualColorSpace == ColorSpace.DeviceCMYK)
                        {
                            /*
                             * Where CMYK in 0..1
                             * R = 255 × (1-C) × (1-K)
                             * G = 255 × (1-M) × (1-K)
                             * B = 255 × (1-Y) × (1-K)
                             */

                            var c = (bytesPure[i++] / 255d);
                            var m = (bytesPure[i++] / 255d);
                            var y = (bytesPure[i++] / 255d);
                            var k = (bytesPure[i++] / 255d);
                            var r = (byte)(255 * (1 - c) * (1 - k));
                            var g = (byte)(255 * (1 - m) * (1 - k));
                            var b = (byte)(255 * (1 - y) * (1 - k));

                            builder.SetPixel(r, g, b, row, col);
                        }
                        else if (is3Byte)
                        {
                            builder.SetPixel(bytesPure[i++], bytesPure[i++], bytesPure[i++], row, col);
                        }
                        else
                        {
                            var pixel = bytesPure[i++];
                            builder.SetPixel(pixel, pixel, pixel, row, col);
                        }
                    }
                }

                bytes = builder.Save();
                return(true);
            }
            catch
            {
                // ignored.
            }

            return(false);
        }
示例#10
0
        public static bool TryGenerate(IPdfImage image, out byte[] bytes)
        {
            bytes = null;

            var hasValidDetails = image.ColorSpaceDetails != null &&
                                  !(image.ColorSpaceDetails is UnsupportedColorSpaceDetails);
            var actualColorSpace = hasValidDetails ? image.ColorSpaceDetails.BaseType : image.ColorSpace;

            var isColorSpaceSupported =
                actualColorSpace == ColorSpace.DeviceGray || actualColorSpace == ColorSpace.DeviceRGB ||
                actualColorSpace == ColorSpace.DeviceCMYK;

            if (!isColorSpaceSupported || !image.TryGetBytes(out var bytesPure))
            {
                return(false);
            }

            try
            {
                bytesPure = ColorSpaceDetailsByteConverter.Convert(image.ColorSpaceDetails, bytesPure,
                                                                   image.BitsPerComponent, image.WidthInSamples, image.HeightInSamples);

                var numberOfComponents = actualColorSpace == ColorSpace.DeviceCMYK ? 4 : actualColorSpace == ColorSpace.DeviceRGB ? 3 : 1;
                var is3Byte            = numberOfComponents == 3;

                var builder = PngBuilder.Create(image.WidthInSamples, image.HeightInSamples, false);

                var isCorrectlySized = bytesPure.Count == (image.WidthInSamples * image.HeightInSamples * numberOfComponents);

                if (!isCorrectlySized)
                {
                    return(false);
                }

                var i = 0;
                for (var col = 0; col < image.HeightInSamples; col++)
                {
                    for (var row = 0; row < image.WidthInSamples; row++)
                    {
                        if (actualColorSpace == ColorSpace.DeviceCMYK)
                        {
                            /*
                             * Where CMYK in 0..1
                             * R = 255 × (1-C) × (1-K)
                             * G = 255 × (1-M) × (1-K)
                             * B = 255 × (1-Y) × (1-K)
                             */

                            var c = (bytesPure[i++] / 255d);
                            var m = (bytesPure[i++] / 255d);
                            var y = (bytesPure[i++] / 255d);
                            var k = (bytesPure[i++] / 255d);
                            var r = (byte)(255 * (1 - c) * (1 - k));
                            var g = (byte)(255 * (1 - m) * (1 - k));
                            var b = (byte)(255 * (1 - y) * (1 - k));

                            builder.SetPixel(r, g, b, row, col);
                        }
                        else if (is3Byte)
                        {
                            builder.SetPixel(bytesPure[i++], bytesPure[i++], bytesPure[i++], row, col);
                        }
                        else
                        {
                            var pixel = bytesPure[i++];
                            builder.SetPixel(pixel, pixel, pixel, row, col);
                        }
                    }
                }

                bytes = builder.Save();
                return(true);
            }
            catch
            {
                // ignored.
            }

            return(false);
        }