public IImageProcessor <TPixel> CreatePixelSpecificProcessor <TPixel>(Configuration configuration, Image <TPixel> source, Rectangle sourceRectangle) where TPixel : unmanaged, IPixel <TPixel>
        {
            if (source is Image <Rgb24> rgbSource)
            {
                var processor = new RgbAnalysisProcessor(configuration, _settings, rgbSource);

                CropAnalysis = processor.GetCropAnalysis();

                if (_settings.AnalyzeWeights)
                {
                    WeightAnalysis = processor.GetWeightAnalysis(CropAnalysis);
                }

                return((IImageProcessor <TPixel>)processor);
            }
            else if (source is Image <Rgba32> rgbaSource)
            {
                var processor = new RgbaAnalysisProcessor(configuration, _settings, rgbaSource);

                CropAnalysis = processor.GetCropAnalysis();

                return((IImageProcessor <TPixel>)processor);
            }

            throw new NotSupportedException("Unsupported pixel type");
        }
        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;
        }
Example #3
0
        public WeightAnalyzer(Bitmap bitmap, Color background)
        {
            _mapResolution = 5;

            var weightMap     = GetWeightMap(bitmap);
            var bounds        = new Rectangle(0, 0, _mapResolution, _mapResolution);
            var bytesPerPixel = Image.GetPixelFormatSize(bitmap.PixelFormat) / 8;
            var data          = (BitmapData)null;
            var vector        = new PointF(0, 0);

            try
            {
                data = weightMap.LockBits(bounds, ImageLockMode.ReadOnly, bitmap.PixelFormat);

                if (bytesPerPixel == 3)
                {
                    vector = GetVectorRgb(data, background);
                }
                else
                {
                    vector = GetVectorRgba(data, background);
                }
            }
            finally
            {
                if (weightMap != null)
                {
                    if (data != null)
                    {
                        weightMap.UnlockBits(data);
                    }

                    weightMap.Dispose();
                }
            }

            _analysis = new WeightAnalysis
            {
                Weight = vector
            };
        }
        public override ICloningImageProcessor <TPixel> CreatePixelSpecificCloningProcessor <TPixel>(Configuration configuration, Image <TPixel> source, Rectangle sourceRectangle)
        {
            if (source is Image <Rgb24> rgbSource)
            {
                var processor = new RgbAutoCropProcessor(configuration, _settings, rgbSource);

                CropAnalysis   = processor.CropAnalysis;
                WeightAnalysis = processor.WeightAnalysis;

                return((ICloningImageProcessor <TPixel>)processor);
            }
            else if (source is Image <Rgba32> rgbaSource)
            {
                var processor = new RgbaAutoCropProcessor(configuration, _settings, rgbaSource);

                CropAnalysis   = processor.CropAnalysis;
                WeightAnalysis = processor.WeightAnalysis;

                return((ICloningImageProcessor <TPixel>)processor);
            }

            throw new NotSupportedException("Unsupported pixel type");
        }
        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 bool TryAnalyzeCrop(this IImageProcessingContext context, IAutoCropSettings settings, out ICropAnalysis cropAnalysis, out IWeightAnalysis weightAnalysis)
 {
     try
     {
         AnalyzeCrop(context, settings, out cropAnalysis, out weightAnalysis);
         return(true);
     }
     catch (Exception)
     {
         cropAnalysis   = null;
         weightAnalysis = null;
         return(false);
     }
 }
Example #8
0
 public PreCalculatedAutoCropProcessor(IAutoCropSettings settings, ICropAnalysis cropAnalysis, IWeightAnalysis weightAnalysis = null) : base(settings)
 {
     _settings      = settings;
     CropAnalysis   = cropAnalysis;
     WeightAnalysis = weightAnalysis;
 }