/
afanasevn
/
PdfEncoder
Обзор
Документация
Войти
/
afanasevn
/
PdfEncoder
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/PdfEncoder.Infrastructure/Pdf/PdfEmbeddedImageCoverageCalculator.cs
68 строк
2 KB
IBS\NAfanasev
Parse results functional update
07 июл 2026, 00:06
07 июл 2026, 00:06
8e97443
Код
Авторство
О чём код?
using PdfEncoder.Application.Abstractions; using PdfEncoder.Application.Options; using UglyToad.PdfPig.Content; namespace PdfEncoder.Infrastructure.Pdf; /// <summary> /// Расчёт доли площади страницы, занятой embedded images. /// </summary> public static class PdfEmbeddedImageCoverageCalculator { /// <summary> /// Возвращает признак наличия images и coverage ratio для страницы. /// </summary> /// <param name="page">Страница PdfPig.</param> public static PageImageMetrics Analyze(Page page) { var images = page.GetImages().ToList(); if (images.Count == 0) { return new PageImageMetrics(HasEmbeddedImages: false, CoverageRatio: 0); } var pageArea = page.Width * page.Height; if (pageArea <= 0) { return new PageImageMetrics(HasEmbeddedImages: true, CoverageRatio: 0); } var imageArea = images.Sum(CalculateBoundsArea); var ratio = Math.Clamp(imageArea / pageArea, 0, 1); return new PageImageMetrics(HasEmbeddedImages: true, CoverageRatio: ratio); } /// <summary> /// True, если страницу нужно обрабатывать full-page OCR по порогам coverage. /// </summary> /// <param name="page">Классификация страницы.</param> /// <param name="options">Пороги из <see cref="PipelineOptions"/>.</param> public static bool RequiresFullPageOcr( PageClassification page, PipelineOptions options) { if (page.ImageCoverageRatio >= options.FullPageOcrImageCoverageRatio) { return true; } return page.HasTextLayer && page.HasEmbeddedImages && page.ImageCoverageRatio >= options.HybridPageOcrImageCoverageRatio; } private static double CalculateBoundsArea(IPdfImage image) { var bounds = image.BoundingBox; return Math.Max(0, bounds.Width) * Math.Max(0, bounds.Height); } } /// <summary> /// Метрики embedded images на странице. /// </summary> /// <param name="HasEmbeddedImages">Есть ли images на странице.</param> /// <param name="CoverageRatio">Доля площади страницы (0..1).</param> public readonly record struct PageImageMetrics( bool HasEmbeddedImages, double CoverageRatio);