Beispiel #1
0
 /// <summary>
 /// Calculates the gradient for each pixel in this image then returns a bitmap
 /// in which the color corresponds to the gradient.
 /// </summary>
 /// <returns></returns>
 public virtual void GenerateGradientBitmap(ProgressCallback callback)
 {
     Reset();
     directBitmapReader = new DirectBitmapReader(grayBitmap.Bitmap);
     bitmap             = new Bitmap(grayBitmap.Bitmap.Width, grayBitmap.Bitmap.Height);
     using (DirectBitmapWriter dbw = new DirectBitmapWriter(bitmap))
     {
         for (int i = 1; i < Bitmap.Height - 1; i++)
         {
             if (callback != null)
             {
                 callback(i * 100 / (Bitmap.Height - 2));
             }
             for (int j = 1; j < Bitmap.Width - 1; j++)
             {
                 byte grad = GetPixelGradient(j, i);
                 dbw.SetGrayPixel(j, i, grad);
                 if (grad > maximumGradientValue)
                 {
                     maximumGradientValue = grad;
                 }
             }
         }
     }
 }
Beispiel #2
0
        /// <summary>
        ///  ascii version of the image loader. works fine.
        /// but is dog-slow (when debugging only).  Would go faster if it read the whole file in at once instead
        /// of line by line?
        /// </summary>
        /// <param name="filename">file containing the image to load.</param>
        /// <returns>string</returns>
        private void loadAscii(Stream file, out string segmentationPoints, ProgressCallback callback)
        {
            using (StreamReader sr = new StreamReader(file))
            {
                // grok out the magic number etc, which are in plain text.
                String line = sr.ReadLine();
                if (line != "P2")
                {
                    throw new ApplicationException("unsupported PGM filetype, try type P2 or P5 only.");
                }

                // spin through comments if any.
                segmentationPoints = string.Empty;
                while ((line = sr.ReadLine()).StartsWith("#"))
                {
                    if (Regex.IsMatch(line, frmMain.SegmentPointRegex))
                    {
                        segmentationPoints = line.Substring(line.IndexOf("("));
                    }
                }

                // parse out the width and height.
                string[] pieces;
                pieces = line.Split(' ');
                int imgCols = Int32.Parse(pieces[0]);
                int imgRows = Int32.Parse(pieces[1]);

                // Initialize the image with the size just read in.
                bitmap = new Bitmap(imgCols, imgRows);
                using (DirectBitmapWriter dba = new DirectBitmapWriter(bitmap))
                {
                    // get the max gray value.
                    maximumGrayValue = Int32.Parse(sr.ReadLine());

                    // Prepare to receive pixels: first row, first column
                    int pixelRow = 0, pixelColumn = 0;
                    while (pixelRow < imgRows)
                    {
                        // Each line will contain only a few of the pixels on a single row of the image.
                        line = sr.ReadLine();
                        string[] colEntries = line.Split(' ');
                        for (int ii = 0; ii < colEntries.Length; ii++)
                        {
                            if (colEntries[ii].Length == 0)
                            {
                                continue;                                                         // skip "  " double spaces
                            }
                            byte thisColor = byte.Parse(colEntries[ii]);
                            dba.SetGrayPixel(pixelColumn, pixelRow, thisColor);
                            if (++pixelColumn == imgCols)
                            {
                                pixelColumn = 0;
                                pixelRow++;
                                callback(pixelRow * 100 / imgRows);
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        ///  ascii version of the image loader. works fine. 
        /// but is dog-slow (when debugging only).  Would go faster if it read the whole file in at once instead
        /// of line by line? 
        /// </summary>
        /// <param name="filename">file containing the image to load.</param>
        /// <returns>string</returns>
        private void loadAscii(Stream file, out string segmentationPoints, ProgressCallback callback)
        {
            using (StreamReader sr = new StreamReader(file))
            {
                // grok out the magic number etc, which are in plain text.
                String line = sr.ReadLine();
                if (line != "P2")
                    throw new ApplicationException("unsupported PGM filetype, try type P2 or P5 only.");

                // spin through comments if any.
                segmentationPoints = string.Empty;
                while ((line = sr.ReadLine()).StartsWith("#"))
                    if (Regex.IsMatch(line, frmMain.SegmentPointRegex))
                        segmentationPoints = line.Substring(line.IndexOf("("));

                // parse out the width and height.
                string[] pieces;
                pieces = line.Split(' ');
                int imgCols = Int32.Parse(pieces[0]);
                int imgRows = Int32.Parse(pieces[1]);

                // Initialize the image with the size just read in.
                bitmap = new Bitmap(imgCols, imgRows);
                using (DirectBitmapWriter dba = new DirectBitmapWriter(bitmap))
                {

                    // get the max gray value.
                    maximumGrayValue = Int32.Parse(sr.ReadLine());

                    // Prepare to receive pixels: first row, first column
                    int pixelRow = 0, pixelColumn = 0;
                    while (pixelRow < imgRows)
                    {
                        // Each line will contain only a few of the pixels on a single row of the image.
                        line = sr.ReadLine();
                        string[] colEntries = line.Split(' ');
                        for (int ii = 0; ii < colEntries.Length; ii++)
                        {
                            if (colEntries[ii].Length == 0) continue; // skip "  " double spaces
                            byte thisColor = byte.Parse(colEntries[ii]);
                            dba.SetGrayPixel(pixelColumn, pixelRow, thisColor);
                            if (++pixelColumn == imgCols)
                            {
                                pixelColumn = 0;
                                pixelRow++;
                                callback(pixelRow * 100 / imgRows);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Calculates the gradient for each pixel in this image then returns a bitmap 
        /// in which the color corresponds to the gradient.
        /// </summary>
        /// <returns></returns>
        public virtual void GenerateGradientBitmap(ProgressCallback callback)
        {
            Reset();
            directBitmapReader = new DirectBitmapReader(grayBitmap.Bitmap);
            bitmap = new Bitmap(grayBitmap.Bitmap.Width, grayBitmap.Bitmap.Height);
            using (DirectBitmapWriter dbw = new DirectBitmapWriter(bitmap))
            {

                for (int i = 1; i < Bitmap.Height - 1; i++)
                {
                    if (callback != null) callback(i * 100 / (Bitmap.Height - 2));
                    for (int j = 1; j < Bitmap.Width - 1; j++)
                    {
                        byte grad = GetPixelGradient(j, i);
                        dbw.SetGrayPixel(j, i, grad);
                        if (grad > maximumGradientValue) maximumGradientValue = grad;
                    }
                }
            }
        }