Example #1
0
        private int GenerateStrokesWithRawScaling(Bitmap input, ColorImage area, ArrayList list, int S, int factor, int level)
        {
            int x, y;

            System.Console.WriteLine("Generating stroke positions image.");

            /*
             * Bitmap areascaled = new Bitmap(area.Width / factor, area.Height / factor);
             * Graphics g = Graphics.FromImage(areascaled);
             * g.ScaleTransform(1.0f / (float)factor, 1.0f / (float)factor);
             * g.DrawImage(area, 0, 0, area.Width, area.Height);
             * areascaled.Save("areascaled-" + factor.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
             * ColorImage img = new ColorImage(areascaled);
             */

            ColorImage scaledWidth = ColorImage.ScaleWidth(area, area.Width / factor);
            ColorImage img         = ColorImage.ScaleHeight(scaledWidth, area.Height / factor);

            this.dither = new FloydSteinbergDither(4 * S / System.Math.Sqrt(level), 2.0f / level);

            UnsafeBitmap dithered = new UnsafeBitmap(this.dither.Dither(img));

            //StatusReporter.Instance.SubmitWorkingImage(ImageTools.GenWorkingImage(dithered));
            //dithered.Save("dithered-" + factor.ToString() + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);

            Bitmap   inscaled = new Bitmap(input.Width / factor, input.Height / factor);
            Graphics g2       = Graphics.FromImage(inscaled);

            g2.ScaleTransform(1.0f / (float)factor, 1.0f / (float)factor);
            g2.DrawImage(input, 0, 0, input.Width, input.Height);
            //inscaled.Save("inputscaled-" + factor.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            System.Console.WriteLine("Collecting strokes.");

            int maxY = dithered.Height;
            int maxX = dithered.Width;

            ColorImage inScaled = new ColorImage(new UnsafeBitmap(inscaled));              // should be &inputscaled

            for (y = 0; y < maxY; y++)
            {
                for (x = 0; x < maxX; x++)
                {
                    if (dithered.GetPixel(x, y).R == 0)
                    {
                        //StatusReporter.Instance.SubmitWorkingImage(ImageTools.GenWorkingImage(dithered, x, y, S, S));
                        Stroke s = GenerateStroke(inScaled, x, y, S, factor, level);
                        list.Add(s);
                    }
                }
            }

            return(list.Count);
        }
Example #2
0
        private static Rectangle TrimTransparentFindHeight(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Height - 1; y >= rect.Y; y--)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        return(rect);
                    }
                }
            }

            return(rect);
        }
Example #3
0
        private static Rectangle TrimTransparentFindWidth(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int x = rect.Width - 1; x >= rect.X; x--)
            {
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        return(rect);
                    }
                }
            }

            return(rect);
        }
Example #4
0
        private static Rectangle TrimTransparentFindY(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Y; y < rect.Height; y++)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Y = y;
                        return(rect);
                    }
                }
            }

            return(rect);
        }
Example #5
0
        public ColorImage(UnsafeBitmap img)
        {
            DateTime start = DateTime.Now;

            this.Width  = img.Width;
            this.Height = img.Height;

            this.singledata = new Color[this.Height * this.Width];

            for (int j = 0; j < this.Height; j++)
            {
                for (int i = 0; i < this.Width; i++)
                {
                    this.singledata[j * this.Width + i] = img.GetPixel(i, j);
                }
            }
        }
        public static double[] ExtractGreyValues(UnsafeBitmap bitmap)
        {
            double[] data = new double[bitmap.Height * bitmap.Width];

            int count = 0;

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    Color  c      = bitmap.GetPixel(x, y);
                    double dvalue = (0.299 * c.R) + (0.587 * c.G) + (0.114 * c.B);
                    data[count++] = dvalue;
                }
            }

            return(data);
        }
        private Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
        {
            if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size)
            {
                Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb);

                using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly))
                    using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly))
                        using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                        {
                            int pixelCount = blackBitmap.PixelCount;

                            for (int i = 0; i < pixelCount; i++)
                            {
                                ColorBgra white = whiteBitmap.GetPixel(i);
                                ColorBgra black = blackBitmap.GetPixel(i);

                                double alpha = (black.Red - white.Red + 255) / 255.0;

                                if (alpha == 1)
                                {
                                    resultBitmap.SetPixel(i, white);
                                }
                                else if (alpha > 0)
                                {
                                    white.Blue  = (byte)(black.Blue / alpha);
                                    white.Green = (byte)(black.Green / alpha);
                                    white.Red   = (byte)(black.Red / alpha);
                                    white.Alpha = (byte)(255 * alpha);

                                    resultBitmap.SetPixel(i, white);
                                }
                            }
                        }

                return(result);
            }

            return(whiteBackground);
        }
        private void TrimShadow(Bitmap bitmap)
        {
            int sizeLimit  = 10;
            int alphaLimit = 200;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true))
            {
                for (int i = 0; i < sizeLimit; i++)
                {
                    int y     = i;
                    int width = bitmap.Width;

                    // Left top
                    for (int x = 0; x < sizeLimit; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Right top
                    for (int x = width - 1; x > width - sizeLimit - 1; x--)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    y = bitmap.Height - i - 1;

                    // Left bottom
                    for (int x = 0; x < sizeLimit; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Right bottom
                    for (int x = width - 1; x > width - sizeLimit - 1; x--)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
        private static Rectangle TrimTransparentFindY(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Y; y < rect.Height; y++)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Y = y;
                        return rect;
                    }
                }
            }

            return rect;
        }
        private static Rectangle TrimTransparentFindWidth(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int x = rect.Width - 1; x >= rect.X; x--)
            {
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        return rect;
                    }
                }
            }

            return rect;
        }
        private static Rectangle TrimTransparentFindHeight(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Height - 1; y >= rect.Y; y--)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        return rect;
                    }
                }
            }

            return rect;
        }
        private static void TrimShadow(Bitmap bitmap)
        {
            int sizeLimit = 10;
            int alphaLimit = 200;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadWrite))
            {
                for (int i = 0; i < sizeLimit; i++)
                {
                    int y = i;
                    int width = bitmap.Width;

                    // Left top
                    for (int x = 0; x < sizeLimit; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Right top
                    for (int x = width - 1; x > width - sizeLimit - 1; x--)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    y = bitmap.Height - i - 1;

                    // Left bottom
                    for (int x = 0; x < sizeLimit; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Right bottom
                    for (int x = width - 1; x > width - sizeLimit - 1; x--)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
        private static Bitmap QuickTrimTransparent(Bitmap bitmap)
        {
            Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            Rectangle rect = source;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadOnly))
            {
                int middleX = rect.Width / 2;
                int middleY = rect.Height / 2;

                // Find X
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.X = x;
                        break;
                    }
                }

                // Find Y
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Y = y;
                        break;
                    }
                }

                // Find Width
                for (int x = rect.Width - 1; x >= rect.X; x--)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        break;
                    }
                }

                // Find Height
                for (int y = rect.Height - 1; y >= rect.Y; y--)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        break;
                    }
                }
            }

            if (source != rect)
            {
                return CaptureHelpers.CropBitmap(bitmap, rect);
            }

            return bitmap;
        }
        private static Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
        {
            if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size)
            {
                Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb);

                using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                {
                    int pixelCount = blackBitmap.PixelCount;

                    for (int i = 0; i < pixelCount; i++)
                    {
                        ColorBgra white = whiteBitmap.GetPixel(i);
                        ColorBgra black = blackBitmap.GetPixel(i);

                        double alpha = (black.Red - white.Red + 255) / 255.0;

                        if (alpha == 1)
                        {
                            resultBitmap.SetPixel(i, white);
                        }
                        else if (alpha > 0)
                        {
                            white.Blue = (byte)(black.Blue / alpha);
                            white.Green = (byte)(black.Green / alpha);
                            white.Red = (byte)(black.Red / alpha);
                            white.Alpha = (byte)(255 * alpha);

                            resultBitmap.SetPixel(i, white);
                        }
                    }
                }

                return result;
            }

            return whiteBackground;
        }
Example #15
0
        private Padding GuessEdges(Bitmap img1, Bitmap img2)
        {
            Padding   result = new Padding();
            Rectangle rect   = new Rectangle(0, 0, img1.Width, img1.Height);

            using (UnsafeBitmap bmp1 = new UnsafeBitmap(img1, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap bmp2 = new UnsafeBitmap(img2, true, ImageLockMode.ReadOnly))
                {
                    bool valueFound = false;

                    // Left edge
                    for (int x = rect.X; !valueFound && x < rect.Width; x++)
                    {
                        for (int y = rect.Y; y < rect.Height; y++)
                        {
                            if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                            {
                                valueFound  = true;
                                result.Left = x;
                                rect.X      = x;
                                break;
                            }
                        }
                    }

                    valueFound = false;

                    // Top edge
                    for (int y = rect.Y; !valueFound && y < rect.Height; y++)
                    {
                        for (int x = rect.X; x < rect.Width; x++)
                        {
                            if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                            {
                                valueFound = true;
                                result.Top = y;
                                rect.Y     = y;
                                break;
                            }
                        }
                    }

                    valueFound = false;

                    // Right edge
                    for (int x = rect.Width - 1; !valueFound && x >= rect.X; x--)
                    {
                        for (int y = rect.Y; y < rect.Height; y++)
                        {
                            if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                            {
                                valueFound   = true;
                                result.Right = rect.Width - x - 1;
                                rect.Width   = x + 1;
                                break;
                            }
                        }
                    }

                    valueFound = false;

                    // Bottom edge
                    for (int y = rect.Height - 1; !valueFound && y >= rect.X; y--)
                    {
                        for (int x = rect.X; x < rect.Width; x++)
                        {
                            if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                            {
                                valueFound    = true;
                                result.Bottom = rect.Height - y - 1;
                                rect.Height   = y + 1;
                                break;
                            }
                        }
                    }
                }

            return(result);
        }
Example #16
0
        private int CalculateVerticalOffset(Bitmap img1, Bitmap img2, int ignoreRightOffset = 50)
        {
            int lastMatchCount  = 0;
            int lastMatchOffset = 0;

            Rectangle rect = new Rectangle(Options.TrimLeftEdge, Options.TrimTopEdge,
                                           img1.Width - Options.TrimLeftEdge - Options.TrimRightEdge - (img1.Width > ignoreRightOffset ? ignoreRightOffset : 0),
                                           img1.Height - Options.TrimTopEdge - Options.TrimBottomEdge);

            using (UnsafeBitmap bmp1 = new UnsafeBitmap(img1, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap bmp2 = new UnsafeBitmap(img2, true, ImageLockMode.ReadOnly))
                {
                    for (int y = rect.Y; y < rect.Bottom; y++)
                    {
                        bool isLineMatches = true;

                        for (int x = rect.X; x < rect.Right; x++)
                        {
                            if (bmp2.GetPixel(x, y) != bmp1.GetPixel(x, rect.Bottom - 1))
                            {
                                isLineMatches = false;
                                break;
                            }
                        }

                        if (isLineMatches)
                        {
                            int lineMatchesCount = 1;
                            int y3 = 2;

                            for (int y2 = y - 1; y2 >= rect.Y; y2--)
                            {
                                bool isLineMatches2 = true;

                                for (int x2 = rect.X; x2 < rect.Right; x2++)
                                {
                                    if (bmp2.GetPixel(x2, y2) != bmp1.GetPixel(x2, rect.Bottom - y3))
                                    {
                                        isLineMatches2 = false;
                                        break;
                                    }
                                }

                                if (isLineMatches2)
                                {
                                    lineMatchesCount++;
                                    y3++;
                                }
                                else
                                {
                                    break;
                                }
                            }

                            if (lineMatchesCount > lastMatchCount)
                            {
                                lastMatchCount  = lineMatchesCount;
                                lastMatchOffset = y - rect.Y + 1;
                            }
                        }
                    }
                }

            return(lastMatchOffset);
        }
Example #17
0
 /// <summary>
 /// Processing the input
 /// </summary>
 /// <param name="image"></param>
 private void GetOnOffState(ref Bitmap image)
 {
     if (line_drawed)
     {
         Rectangle    rect    = new Rectangle(pt.X - 3, pt.Y - 3, 6, 6);
         Bitmap       img     = image.Clone(rect, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
         UnsafeBitmap uBitmap = new UnsafeBitmap(img);
         uBitmap.LockBitmap();
         float avgBritness = 0;
         for (int x = 0; x < 6; x++)
         {
             for (int y = 0; y < 6; y++)
             {
                 byte red, green, blue;
                 red          = uBitmap.GetPixel(x, y).red;
                 green        = uBitmap.GetPixel(x, y).green;
                 blue         = uBitmap.GetPixel(x, y).blue;
                 avgBritness += (299 * red + 587 * green + 114 * blue) / 1000;
             }
         }
         avgBritness /= 36;
         uBitmap.UnlockBitmap();
         // nUD_brightness threshold for brightness value ( "On" state)
         if (avgBritness > (float)nUD_brightness.Value)
         {
             if (check)
             {
                 if (sw.IsRunning)
                 {
                     if (sw.ElapsedMilliseconds > nUD_char.Value && sw.ElapsedMilliseconds <= nUD_word.Value)
                     {
                         Append_morse(" ");
                     }
                     else if (sw.ElapsedMilliseconds > nUD_word.Value)
                     {
                         Append_morse("/");
                     }
                     Append_logging("Off Time : " + Convert.ToString(sw.ElapsedMilliseconds) + "ms\n");
                     sw.Reset();
                     sw.Start();
                 }
                 else
                 {
                     sw.Reset();
                     sw.Start();
                 }
                 check = false;
             }
         }
         //"off" state
         else
         {
             if (!check)
             {
                 if (sw.IsRunning)
                 {
                     if (sw.ElapsedMilliseconds < nUD_dot.Value)
                     {
                         Append_morse(".");
                     }
                     else
                     {
                         Append_morse("-");
                     }
                     Append_logging("On Time : " + Convert.ToString(sw.ElapsedMilliseconds) + " ms\n");
                     sw.Reset();
                     sw.Start();
                 }
                 check = true;
             }
         }
     }
     else
     {
         MessageBox.Show("Please click the pictureBox again and set the place where from the morse code is captured.");
     }
 }