public FormattedImage Process(FormattedImage image, ILogger logger, IDictionary <string, string> commands, CommandParser parser, CultureInfo culture)
        {
            var autoCropParameter   = commands.GetValueOrDefault(_autoCropCommandKey);
            var resizeModeParameter = commands.GetValueOrDefault(_resizeModeCommandKey);
            var parsed = ParseSettings(autoCropParameter, resizeModeParameter, out var settings);

            if (parsed)
            {
                try
                {
                    ICropAnalysis analysis = null;
                    image.Image.Mutate((ctx) => ctx.AutoCrop(settings, out analysis));

                    if (analysis.Success && !commands.ContainsKey(_backgroundColorCommandKey))
                    {
                        commands.Add(_backgroundColorCommandKey, analysis.Background.ToHex());
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, ex.Message);
                }
            }

            return(image);
        }
 public AutoCropState(ICropAnalysis analysis, Image image)
 {
     Bounds             = analysis.BoundingBox;
     BorderColor        = analysis.Background;
     BytesPerPixel      = image.PixelType.BitsPerPixel / 8;
     OriginalDimensions = new Rectangle(0, 0, image.Width, image.Height);
 }
        public AutoCropState(Bitmap bitmap, ICropAnalysis cropAnalysis = null, IWeightAnalysis weightAnalysis = null)
        {
            var originalDimensions = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            BytesPerPixel      = Image.GetPixelFormatSize(bitmap.PixelFormat) / 8;
            BorderColor        = cropAnalysis?.Background ?? Color.White;
            Weight             = weightAnalysis?.Weight ?? new PointF(0, 0);
            Bounds             = cropAnalysis?.BoundingBox ?? originalDimensions;
            OriginalDimensions = originalDimensions;
        }
        public SobelAnalyzer(BitmapData bitmap, int sobelThreshold, float bucketTreshold)
        {
            var thumbnail           = GetThumbnail(bitmap);
            var thumbnailData       = thumbnail.LockBits(new Rectangle(0, 0, thumbnail.Width, thumbnail.Height), ImageLockMode.ReadOnly, thumbnail.PixelFormat);
            var thumbnailInspection = new BorderInspector(thumbnailData, _thumbnailSize, 1.0f);

            thumbnail.UnlockBits(thumbnailData);

            var features          = GetFeatureMap(thumbnail);
            var featureBox        = new Rectangle(0, 0, features.Width, features.Height);
            var featureData       = features.LockBits(featureBox, ImageLockMode.ReadOnly, features.PixelFormat);
            var featureInspection = new BorderInspector(featureData, sobelThreshold, bucketTreshold, forceRgb: true);

            if (featureInspection.Failed)
            {
                featureBox = featureBox.Contract(10);

                var additionalInspection = new BorderInspector(featureData, featureBox, sobelThreshold, bucketTreshold);
                if (!additionalInspection.Failed)
                {
                    featureInspection = additionalInspection;
                }
            }

            if (featureInspection.Failed)
            {
                _boundingBox      = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                _foundBoundingBox = false;
            }
            else
            {
                _boundingBox      = GetBoundingBoxForFeature(featureData, featureInspection.Rectangle, sobelThreshold, bitmap.Width, bitmap.Height);
                _foundBoundingBox = true;
            }

            features.UnlockBits(featureData);

            features.Dispose();
            thumbnail.Dispose();

            _analysis = new CropAnalysis
            {
                Success     = _foundBoundingBox,
                BoundingBox = _boundingBox,
                Background  = thumbnailInspection.BackgroundColor,
            };
        }
Exemple #5
0
        public BoundsAnalyzer(BitmapData bitmap, int colorThreshold, float bucketTreshold)
        {
            var outerBox = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            var imageBox = outerBox;

            var borderInspection = new BorderInspector(bitmap, imageBox, colorThreshold, bucketTreshold);

            if (borderInspection.Failed)
            {
                colorThreshold = (int)Math.Round(colorThreshold * 0.5);
                bucketTreshold = 1.0f;
                imageBox       = imageBox.Contract(10);

                var additionalInspection = new BorderInspector(bitmap, imageBox, colorThreshold, bucketTreshold);
                if (!additionalInspection.Failed)
                {
                    borderInspection = additionalInspection;
                }
            }

            if (borderInspection.Failed)
            {
                _boundingBox      = outerBox;
                _foundBoundingBox = false;
            }
            else
            {
                if (borderInspection.BytesPerPixel == 3)
                {
                    _boundingBox = GetBoundingBoxForContentRgb(bitmap, borderInspection.Rectangle, borderInspection.BackgroundColor, colorThreshold);
                }
                else
                {
                    _boundingBox = GetBoundingBoxForContentArgb(bitmap, borderInspection.Rectangle, borderInspection.BackgroundColor, colorThreshold);
                }

                _foundBoundingBox = ValidateRectangle(_boundingBox);
            }

            _cropAnalysis = new CropAnalysis
            {
                Background  = borderInspection.BackgroundColor,
                BoundingBox = _boundingBox,
                Success     = _foundBoundingBox
            };
        }
 public override IWeightAnalysis GetWeightAnalysis(ICropAnalysis cropAnalysis)
 {
     return(_weightAnalyzer.GetAnalysis(Source, cropAnalysis.Background));
 }
 public static bool TryAnalyzeCrop(this IImageProcessingContext context, IAutoCropSettings settings, out ICropAnalysis analysis)
 {
     try
     {
         AnalyzeCrop(context, settings, out analysis);
         return(true);
     }
     catch (Exception)
     {
         analysis = null;
         return(false);
     }
 }
        public static void AnalyzeCrop(this IImageProcessingContext context, IAutoCropSettings settings, out ICropAnalysis cropAnalysis, out IWeightAnalysis weightAnalysis)
        {
            var processor = new AnalysisProcessor(settings);

            context.ApplyProcessor(processor);

            cropAnalysis   = processor.CropAnalysis;
            weightAnalysis = processor.WeightAnalysis;
        }
        public static void AutoCropKnown(this IImageProcessingContext context, IAutoCropSettings settings, ICropAnalysis cropAnalysis, IWeightAnalysis weightAnalysis = null)
        {
            var processor = new PreCalculatedAutoCropProcessor(settings, cropAnalysis, weightAnalysis);

            context.ApplyProcessor(processor);
        }
        public static void AutoCrop(this IImageProcessingContext context, IAutoCropSettings settings, out ICropAnalysis analysis)
        {
            var processor = new AutoCropProcessor(settings);

            context.ApplyProcessor(processor);

            analysis = processor.CropAnalysis;
        }
Exemple #11
0
 public PreCalculatedAutoCropProcessor(IAutoCropSettings settings, ICropAnalysis cropAnalysis, IWeightAnalysis weightAnalysis = null) : base(settings)
 {
     _settings      = settings;
     CropAnalysis   = cropAnalysis;
     WeightAnalysis = weightAnalysis;
 }