public static int[,] ApplyFilter(ImageChunk wholeChunk, ConvolutionFilterMatrix filter)
        {
            int[,] accumlator = new int[wholeChunk.Width, wholeChunk.Height];

            using (var pixels = wholeChunk.LockPixels())
            {
                for (int y = 0; y < wholeChunk.Height; y++)
                {
                    for (int x = 0; x < wholeChunk.Width; x++)
                    {
                        for (int filterXOffset = 0; filterXOffset < filter.Width; filterXOffset++)
                        {
                            for (int filterYOffset = 0; filterYOffset < filter.Height; filterYOffset++)
                            {
                                int pixelXOffset = x + filterXOffset - 1;
                                int pixelYOffset = y + filterYOffset - 1;

                                if (pixelXOffset >= 0 && pixelXOffset < wholeChunk.Width && pixelYOffset >= 0 &&
                                    pixelYOffset < wholeChunk.Height)
                                {
                                    Color pixel = pixels.GetPixel(pixelXOffset, pixelYOffset);
                                    int   gray  = (pixel.R + pixel.G + pixel.B) / 3;

                                    accumlator[x, y] += (int)filter.Multiplier.Multiply((filter[filterXOffset, filterYOffset] * gray));
                                }
                            }
                        }
                    }
                }
            }

            return(accumlator);
        }
Beispiel #2
0
 private void PopulateHistogram(ImageChunk chunk)
 {
     foreach (var color in chunk.GetAllColors())
     {
         Add(color);
     }
 }
Beispiel #3
0
        public LoadModule(ImageChunk image)
        {
            if (image == null)
                throw new ArgumentNullException("image");

            this.image = image;
        }
Beispiel #4
0
        public RgbHistogram(ImageChunk chunk)
        {
            Histograms[ColorAspects.R] = new Histogram(Buckets);
            Histograms[ColorAspects.G] = new Histogram(Buckets);
            Histograms[ColorAspects.B] = new Histogram(Buckets);

            PopulateHistogram(chunk);
        }
Beispiel #5
0
        public LoadModule(ImageChunk image)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }

            this.image = image;
        }
Beispiel #6
0
        private ImageStats GetImageStats(ImageRegion region, FastBitmap imageBitmap)
        {
            var imageChunk = new ImageChunk(imageBitmap, region);

            return(new ImageStats(
                       _averageGreyCalculator.CaluclateAverageDark(imageChunk),
                       _averageGreyByRegionCalculator.GetStatByRegion(imageChunk),
                       _histogramCalculator.CaluclateHistogram(imageChunk)));
        }
Beispiel #7
0
        public override void EventOccurred(IEventData data, EventType type)
        {
            if (type != EventType.RENDER_IMAGE)
            {
                return;
            }

            var    counter     = _increaseCounter();
            var    renderInfo  = (ImageRenderInfo)data;
            var    imageObject = renderInfo.GetImage();
            Bitmap image;

            try
            {
                var imageBytes = imageObject.GetImageBytes();

                image = new Bitmap(new MemoryStream(imageBytes));
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp);

                return;
            }

            var smask = imageObject.GetPdfObject().GetAsStream(PdfName.SMask);

            if (smask != null)
            {
                try
                {
                    var maskImageObject = new PdfImageXObject(smask);
                    var maskBytes       = maskImageObject.GetImageBytes();
                    using var maskImage = new Bitmap(new MemoryStream(maskBytes));

                    image = GenerateMaskedImage(image, maskImage);
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp);
                }
            }

            var matix      = renderInfo.GetImageCtm();
            var imageChunk = new ImageChunk
            {
                X     = matix.Get(Geom.Matrix.I31),
                Y     = matix.Get(Geom.Matrix.I32),
                W     = matix.Get(Geom.Matrix.I11),
                H     = matix.Get(Geom.Matrix.I22),
                Image = image
            };

            _chunkDictionairy.Add(counter, imageChunk);

            base.EventOccurred(data, type);
        }
Beispiel #8
0
        public Dictionary <Point, T> GetStatByRegion(ImageChunk chunk)
        {
            var statByRegionDictionary = new Dictionary <Point, T>();

            foreach (ImageChunk subChunk in chunk.GetSubChunks(_regionCreationStrategy))
            {
                statByRegionDictionary[CalculateLocalizedOrigin(subChunk.Origin, chunk)] = _statCalculatorFunc(subChunk);
            }

/*
 *          foreach (var subRegion in _regionCreationStrategy.GetRegions(outerRegion))
 *          {
 *              statByRegionDictionary[CalculateLocalizedOrigin(subRegion.Origin, outerRegion)] = _statCalculatorFunc(new ImageChunk(bitmap, subRegion));
 *          }
 */

            return(statByRegionDictionary);
        }
Beispiel #9
0
        public override async Task ShowMessage(MessageRequest request, IServerStreamWriter <MessageReply> responseStream, ServerCallContext context)
        {
            using var tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;

            var meta     = new MessageReply();
            var metaData = new ImageMetaData();

            metaData.FileName = "kitty.jpg";
            metaData.MimeType = "image/jpeg";
            meta.MetaData     = metaData;

            await responseStream.WriteAsync(meta);

            var kitty = Path.Combine(_env.ContentRootPath, "kitty.jpg");

            using var reader = new FileStream(kitty, FileMode.Open);

            int chunkSize = 100;
            int bytesRead;

            byte[] buffer   = new byte[chunkSize];
            int    position = 0;
            long   length   = reader.Length;

            while ((bytesRead = await reader.ReadAsync(buffer, 0, buffer.Length)) > 0)
            {
                var reply = new MessageReply();
                var chunk = new ImageChunk();
                chunk.Length = bytesRead;
                chunk.Data   = ByteString.CopyFrom(buffer);
                reply.Chunk  = chunk;

                await responseStream.WriteAsync(reply);

                position += bytesRead;
                Console.WriteLine(position);
            }
            reader.Close();
            Console.WriteLine("Done");
        }
Beispiel #10
0
/*
 *      public int CaluclateAverageDark(ImageRegion region, FastBitmap bitmap)
 *      {
 *          int totalGrey = 0;
 *
 *          bitmap.Lock();
 *          for (int xOffset = 0; xOffset < region.Width; xOffset++)
 *          {
 *              for (int yOffset = 0; yOffset < region.Height; yOffset++)
 *              {
 *                  Color color = bitmap.GetPixel(region.X + xOffset, region.Y + yOffset);
 *
 *                  totalGrey += color.R + color.G + color.B;
 *              }
 *          }
 *          bitmap.Unlock();
 *
 *          return totalGrey/(3*region.Width*region.Height);
 *      }
 */

        public int CaluclateAverageDark(ImageChunk chunk)
        {
            int totalGrey = 0;
            int area;

            using (ImageChunk.ImageChunkPixels pixels = chunk.LockPixels())
            {
                for (int xOffset = 0; xOffset < pixels.Width; xOffset++)
                {
                    for (int yOffset = 0; yOffset < pixels.Height; yOffset++)
                    {
                        Color color = pixels.GetPixel(pixels.X + xOffset, pixels.Y + yOffset);

                        totalGrey += color.R + color.G + color.B;
                    }
                }

                area = 3 * pixels.Width * pixels.Height;
            }

            return(totalGrey / area);
        }
Beispiel #11
0
 public ImageByte(ImageChunk image, int index)
 {
     this.image = image;
     this.index = index;
 }
Beispiel #12
0
 private Point CalculateLocalizedOrigin(Point pointInRegion, ImageChunk chunk)
 {
     return(new Point(pointInRegion.X - chunk.X, pointInRegion.Y - chunk.Y));
 }
Beispiel #13
0
 public ImageByte(ImageChunk image, int index)
 {
     this.image = image;
     this.index = index;
 }
 public RgbHistogram CaluclateHistogram(ImageChunk chunk)
 {
     return(new RgbHistogram(chunk));
 }
Beispiel #15
0
        private void LoadButton_OnClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter           = "Image files (*.jpg)|*.jpg|All files (*.*)|*.*";
            openFileDialog1.FilterIndex      = 1;
            openFileDialog1.RestoreDirectory = true;

//            ConvolutionFilterMatrix filter = new ConvolutionFilterMatrix("-1 -1 -1,0 0 0,1 1 1");
//            ConvolutionFilterMatrix filter = new ConvolutionFilterMatrix("-1 0 1,-1 0 1,-1 0 1");

            IEnumerable <ConvolutionFilterMatrix> filters = new ConvolutionFilterMatrix[]
            {
                new ConvolutionFilterMatrix("-1 -1 -1,0 0 0,1 1 1"),
                new ConvolutionFilterMatrix("1 1 1,0 0 0,-1 -1 -1"),
                new ConvolutionFilterMatrix("-1 0 1,-1 0 1,-1 0 1"),
                new ConvolutionFilterMatrix("1 0 -1,1 0 -1,1 0 -1"),
                new ConvolutionFilterMatrix("-1 0 0 0 0,0 -2 0 0 0,0 0 6 0 0,0 0 0 -2 0,0 0 0 0 -1"),
                new ConvolutionFilterMatrix("0 0 0 0 -1,0 0 0 -2 0,0 0 6 0 0,0 -2 0 0 0,-1 0 0 0 0"),
//                new ConvolutionFilterMatrix("-1 0 0 0 0,0 -1 0 0 0,0 0 0 0 0,0 0 0 1 0,0 0 0 0 1"), // My experiment 45 degree filter
                new ConvolutionFilterMatrix("-1 0 0,0 0 0,0 0 1", new LowValuesHighZeroLowConvolutionFilterMultiplier()),
                new ConvolutionFilterMatrix("1 0 0,0 0 0,0 0 -1", new LowValuesHighZeroLowConvolutionFilterMultiplier()),
                new ConvolutionFilterMatrix("0 0 1,0 0 0,-1 0 0", new LowValuesHighZeroLowConvolutionFilterMultiplier()),
                new ConvolutionFilterMatrix("0 0 -1,0 0 0,1 0 0", new LowValuesHighZeroLowConvolutionFilterMultiplier()),
//                new ConvolutionFilterMatrix("0 0 0 0 0,0 0 0 0 0,0 0 6 0 0,0 0 0 0 0,0 0 0 0 0"),
                new ConvolutionFilterMatrix("0 0 1,0 1 -1,1 -1 0"),
                new ConvolutionFilterMatrix("0 0 0,0 1 0,0 0 0"),
            };

            var showDialog = openFileDialog1.ShowDialog();

            if (showDialog.HasValue && showDialog.Value)
            {
                BitmapImage origBitmapImage = new BitmapImage(new Uri(openFileDialog1.FileName));
                originalImage.Source = origBitmapImage;

                Bitmap     b      = new Bitmap(openFileDialog1.FileName);
                FastBitmap bitmap = new FastBitmap(b);

                ImageChunk wholeChunk = new ImageChunk(bitmap, new ImageRegion(0, 0, bitmap.Width, bitmap.Height));

                List <FilterResult> results = new List <FilterResult>();

                foreach (ConvolutionFilterMatrix filter in filters)
                {
                    var        filterAccumulator = ConvolutionFilterResultGenerator.ApplyFilter(wholeChunk, filter);
                    FastBitmap resultBitmap      = ConvolutionFilterResultGenerator.ResultToBitmap(filterAccumulator);
//                    resultBitmap.ToBitmap().Save(@"c:\src\tmp.jpg");

                    results.Add(new FilterResult()
                    {
                        Filter          = filter,
                        Total           = filterAccumulator.Flatten().Sum(),
                        NormalizedTotal = filterAccumulator.Flatten().Select(v => v < 0 ? 0 : (v > 255 ? 255 : 0)).Sum(),
                        ResultBitmap    = BitmapToImageSource(resultBitmap.ToBitmap())
                    });
                }

                this.filterResultList.ItemsSource = results.OrderByDescending(f => f.Total).ToList();
            }
        }