Пример #1
0
        private static bool IsMatchPointBackGround(NOcrPoint op, bool loose, NikseBitmap nbmp, NOcrChar nOcrChar)
        {
            foreach (Point point in op.ScaledGetPoints(nOcrChar, nbmp.Width, nbmp.Height))
            {
                if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                {
                    Color c = nbmp.GetPixel(point.X, point.Y);
                    if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                    {
                        return(false);
                    }

                    if (nbmp.Width > 10 && point.X + 1 < nbmp.Width)
                    {
                        c = nbmp.GetPixel(point.X + 1, point.Y);
                        if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                        {
                            return(false);
                        }
                    }

                    if (loose)
                    {
                        if (nbmp.Width > 10 && point.X >= 1)
                        {
                            c = nbmp.GetPixel(point.X - 1, point.Y);
                            if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                            {
                                return(false);
                            }
                        }

                        if (nbmp.Height > 10 && point.Y + 1 < nbmp.Height)
                        {
                            c = nbmp.GetPixel(point.X, point.Y + 1);
                            if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                            {
                                return(false);
                            }
                        }

                        if (nbmp.Height > 10 && point.Y >= 1)
                        {
                            c = nbmp.GetPixel(point.X, point.Y - 1);
                            if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            return(true);
        }
Пример #2
0
        public static bool IsMatch(NikseBitmap bitmap, NOcrChar oc, int errorsAllowed)
        {
            var index  = 0;
            var errors = 0;

            while (index < oc.LinesForeground.Count)
            {
                var op = oc.LinesForeground[index];
                foreach (var point in op.ScaledGetPoints(oc, bitmap.Width, bitmap.Height))
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < bitmap.Width && point.Y < bitmap.Height)
                    {
                        var c = bitmap.GetPixel(point.X, point.Y);
                        if (c.A <= 150 || c.R + c.G + c.B <= VobSubOcr.NocrMinColor)
                        {
                            errors++;
                            if (errors > errorsAllowed)
                            {
                                return(false);
                            }
                        }
                    }
                }

                index++;
            }

            index = 0;
            while (index < oc.LinesBackground.Count)
            {
                var op = oc.LinesBackground[index];
                foreach (var point in op.ScaledGetPoints(oc, bitmap.Width, bitmap.Height))
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < bitmap.Width && point.Y < bitmap.Height)
                    {
                        var c = bitmap.GetPixel(point.X, point.Y);
                        if (c.A > 150 && c.R + c.G + c.B > VobSubOcr.NocrMinColor)
                        {
                            errors++;
                            if (errors > errorsAllowed)
                            {
                                return(false);
                            }
                        }
                    }
                }
                index++;
            }

            return(true);
        }
Пример #3
0
        private static Dictionary <Color, int> GetBitmapPalette(NikseBitmap bitmap)
        {
            var pal = new Dictionary <Color, int>();

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    var c = bitmap.GetPixel(x, y);
                    if (c != Color.Transparent)
                    {
                        if (pal.Count < 100)
                        {
                            if (!HasCloseColor(c, pal, 1))
                            {
                                pal.Add(c, pal.Count);
                            }
                        }
                        else if (pal.Count < 240)
                        {
                            if (!HasCloseColor(c, pal, 5))
                            {
                                pal.Add(c, pal.Count);
                            }
                        }
                        else if (pal.Count < 254 && !HasCloseColor(c, pal, 25))
                        {
                            pal.Add(c, pal.Count);
                        }
                    }
                }
            }
            pal.Add(Color.Transparent, pal.Count); // last entry must be transparent
            return(pal);
        }
Пример #4
0
        private bool IsMatch()
        {
            NikseBitmap nbmp = new NikseBitmap(pictureBoxCharacter.Image as Bitmap);
            var         bmp  = pictureBoxCharacter.Image as Bitmap;

            foreach (NOcrPoint op in _nocrChar.LinesForeground)
            {
                foreach (Point point in op.ScaledGetPoints(_nocrChar, nbmp.Width, nbmp.Height))
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                    {
                        Color c = nbmp.GetPixel(point.X, point.Y);
                        if (c.A > 150 && c.R + c.G + c.B > VobSubOcr.NocrMinColor)
                        {
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            foreach (NOcrPoint op in _nocrChar.LinesBackground)
            {
                foreach (Point point in op.ScaledGetPoints(_nocrChar, nbmp.Width, nbmp.Height))
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                    {
                        Color c = nbmp.GetPixel(point.X, point.Y);
                        if (c.A > 150 && c.R + c.G + c.B > VobSubOcr.NocrMinColor)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
        private bool IsMatch()
        {
            var nbmp = new NikseBitmap(pictureBoxCharacter.Image as Bitmap);

            foreach (var op in NOcrChar.LinesForeground)
            {
                foreach (var point in op.ScaledGetPoints(NOcrChar, nbmp.Width, nbmp.Height))
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                    {
                        var c = nbmp.GetPixel(point.X, point.Y);
                        if (c.A > 150)
                        {
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            foreach (var op in NOcrChar.LinesBackground)
            {
                foreach (var point in op.GetPoints())
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                    {
                        var c = nbmp.GetPixel(point.X, point.Y);
                        if (c.A > 150)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
Пример #6
0
 private void InitializeViaNikseBmp(NikseBitmap nbmp)
 {
     Width   = nbmp.Width;
     Height  = nbmp.Height;
     _colors = new byte[Width * Height];
     for (int y = 0; y < Height; y++)
     {
         for (int x = 0; x < Width; x++)
         {
             this.SetPixel(x, y, nbmp.GetPixel(x, y));
         }
     }
     Hash = MurMurHash3.Hash(_colors);
     CalcuateNumberOfColoredPixels();
 }
Пример #7
0
        private static Dictionary <Color, int> GetBitmapPalette(NikseBitmap bitmap)
        {
            var pal = new Dictionary <Color, int>();

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    Color c = bitmap.GetPixel(x, y);
                    if (!pal.ContainsKey(c) && c != Color.Transparent && pal.Count < 254)
                    {
                        pal.Add(c, pal.Count);
                    }
                }
            }
            pal.Add(Color.Transparent, pal.Count); // last entry must be transparent
            return(pal);
        }
Пример #8
0
        private static List <Color> GetBitmapPalette(NikseBitmap bitmap)
        {
            var pal    = new List <Color>();
            var lookup = new HashSet <int>();

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    var c = bitmap.GetPixel(x, y);
                    if (c != Color.Transparent)
                    {
                        if (lookup.Contains(c.ToArgb()))
                        {
                            // exact color already exists
                        }
                        else if (pal.Count < 100)
                        {
                            if (!HasCloseColor(c, pal, 1))
                            {
                                pal.Add(c);
                                lookup.Add(c.ToArgb());
                            }
                        }
                        else if (pal.Count < 240)
                        {
                            if (!HasCloseColor(c, pal, 5))
                            {
                                pal.Add(c);
                                lookup.Add(c.ToArgb());
                            }
                        }
                        else if (pal.Count < 254 && !HasCloseColor(c, pal, 25))
                        {
                            pal.Add(c);
                            lookup.Add(c.ToArgb());
                        }
                    }
                }
            }

            pal.Add(Color.Transparent); // last entry must be transparent
            return(pal);
        }
Пример #9
0
        public void NikseBitmapSetGetPixel()
        {
            var nbmp = new NikseBitmap(10, 11);
            var c1   = System.Drawing.Color.FromArgb(0, 1, 2, 3);
            var c2   = System.Drawing.Color.FromArgb(6, 7, 8, 9);

            for (int y = 0; y < nbmp.Height; y++)
            {
                for (int x = 0; x < nbmp.Width; x++)
                {
                    if (x % 2 == 0)
                    {
                        nbmp.SetPixel(x, y, c1);
                    }
                    else
                    {
                        nbmp.SetPixel(x, y, c2);
                    }
                }
            }
            for (int y = 0; y < nbmp.Height; y++)
            {
                for (int x = 0; x < nbmp.Width; x++)
                {
                    var c = nbmp.GetPixel(x, y);
                    if (x % 2 == 0)
                    {
                        Assert.AreEqual(c1, c);
                    }
                    else
                    {
                        Assert.AreEqual(c2, c);
                    }
                }
            }
        }
Пример #10
0
        private static bool IsMatchPointForeGround(NOcrPoint op, bool loose, NikseBitmap nbmp, NOcrChar nOcrChar)
        {
            if (Math.Abs(op.Start.X - op.End.X) < 2 && Math.Abs(op.End.Y - op.Start.Y) < 2)
            {
                return(false);
            }

            foreach (Point point in op.ScaledGetPoints(nOcrChar, nbmp.Width, nbmp.Height))
            {
                if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                {
                    Color c = nbmp.GetPixel(point.X, point.Y);
                    if (c.A > 150)
                    {
                    }
                    else
                    {
                        return(false);
                    }

                    if (loose)
                    {
                        if (nbmp.Width > 10 && point.X + 1 < nbmp.Width)
                        {
                            c = nbmp.GetPixel(point.X + 1, point.Y);
                            if (c.A > 150)
                            {
                            }
                            else
                            {
                                return(false);
                            }
                        }

                        if (nbmp.Width > 10 && point.X >= 1)
                        {
                            c = nbmp.GetPixel(point.X - 1, point.Y);
                            if (c.A > 150)
                            {
                            }
                            else
                            {
                                return(false);
                            }
                        }

                        if (nbmp.Height > 10 && point.Y + 1 < nbmp.Height)
                        {
                            c = nbmp.GetPixel(point.X, point.Y + 1);
                            if (c.A > 150)
                            {
                            }
                            else
                            {
                                return(false);
                            }
                        }

                        if (nbmp.Height > 10 && point.Y >= 1)
                        {
                            c = nbmp.GetPixel(point.X, point.Y - 1);
                            if (c.A > 150)
                            {
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            return(true);
        }
Пример #11
0
 private static Dictionary<Color, int> GetBitmapPalette(NikseBitmap bitmap)
 {
     var pal = new Dictionary<Color, int>();
     for (int y = 0; y < bitmap.Height; y++)
     {
         for (int x = 0; x < bitmap.Width; x++)
         {
             var c = bitmap.GetPixel(x, y);
             if (c != Color.Transparent)
             {
                 if (pal.Count < 200 && !pal.ContainsKey(c))
                     pal.Add(c, pal.Count);
                 else if (pal.Count < 254 && !HasCloseColor(c, pal, 5))
                     pal.Add(c, pal.Count);
             }
         }
     }
     pal.Add(Color.Transparent, pal.Count); // last entry must be transparent
     return pal;
 }
Пример #12
0
        /// <summary>
        /// Create RLE buffer from bitmap
        /// </summary>
        /// <param name="bm">Bitmap to compress</param>
        /// <param name="palette">Palette used for bitmap encoding</param>
        /// <returns>RLE buffer</returns>
        private static byte[] EncodeImage(NikseBitmap bm, Dictionary<Color, int> palette)
        {
            var bytes = new List<Byte>();
            for (int y = 0; y < bm.Height; y++)
            {
                var ofs = y * bm.Width;
                //eol = false;
                int x;
                int len;
                for (x = 0; x < bm.Width; x += len, ofs += len)
                {
                    Color c = bm.GetPixel(x, y);
                    byte color;
                    if (palette.ContainsKey(c))
                        color = (byte)palette[c];
                    else
                        color = FindBestMatch(c, palette);

                    for (len = 1; x + len < bm.Width; len++)
                        if (bm.GetPixel(x + len, y) != c)
                            break;

                    if (len <= 2 && color != 0)
                    {
                        // only a single occurrence -> add color
                        bytes.Add(color);
                        if (len == 2)
                            bytes.Add(color);
                    }
                    else
                    {
                        if (len > 0x3fff)
                            len = 0x3fff;
                        bytes.Add(0); // rle id
                        // commented out due to bug in SupRip
                        /*if (color == 0 && x+len == bm.Width)
                        {
                            bytes.Add(0);
                            eol = true;
                        }
                        else */
                        if (color == 0 && len < 0x40)
                        {
                            // 00 xx -> xx times 0
                            bytes.Add((byte)len);
                        }
                        else if (color == 0)
                        {
                            // 00 4x xx -> xxx zeroes
                            bytes.Add((byte)(0x40 | (len >> 8)));
                            bytes.Add((byte)len);
                        }
                        else if (len < 0x40)
                        {
                            // 00 8x cc -> x times value cc
                            bytes.Add((byte)(0x80 | len));
                            bytes.Add(color);
                        }
                        else
                        {
                            // 00 cx yy cc -> xyy times value cc
                            bytes.Add((byte)(0xc0 | (len >> 8)));
                            bytes.Add((byte)len);
                            bytes.Add(color);
                        }
                    }
                }
                if (/*!eol &&*/ x == bm.Width)
                {
                    bytes.Add(0); // rle id
                    bytes.Add(0);
                }
            }
            int size = bytes.Count;
            var retval = new byte[size];
            for (int i = 0; i < size; i++)
                retval[i] = bytes[i];
            return retval;
        }
Пример #13
0
 private void InitializeViaNikseBmp(NikseBitmap nbmp)
 {
     Width = nbmp.Width;
     Height = nbmp.Height;
     _colors = new byte[Width * Height];
     for (int y = 0; y < Height; y++)
     {
         for (int x = 0; x < Width; x++)
         {
             this.SetPixel(x, y, nbmp.GetPixel(x, y));
         }
     }
     Hash = MurMurHash3.Hash(_colors);
     CalcuateNumberOfColoredPixels();
 }
Пример #14
0
        private static List <Color> GetBitmapPalette(NikseBitmap bitmap)
        {
            var pal    = new List <Color>();
            var lookup = new HashSet <int>();

            // first we try with exact colors
            for (var y = 0; y < bitmap.Height; y++)
            {
                for (var x = 0; x < bitmap.Width; x++)
                {
                    var c = bitmap.GetPixel(x, y);
                    if (c.A > 0)
                    {
                        if (lookup.Contains(c.ToArgb()))
                        {
                            // exact color already exists
                        }
                        else
                        {
                            pal.Add(c);
                            lookup.Add(c.ToArgb());
                        }

                        if (pal.Count >= 254)
                        {
                            break;
                        }
                    }
                }
                if (pal.Count >= 254)
                {
                    break;
                }
            }

            if (pal.Count < 254)
            {
                pal.Add(Color.FromArgb(0, 0, 0, 0)); // last entry must be transparent
                return(pal);
            }


            // get close colors (image has probably been processed in SE)
            pal    = new List <Color>();
            lookup = new HashSet <int>();
            for (var y = 0; y < bitmap.Height; y++)
            {
                for (var x = 0; x < bitmap.Width; x++)
                {
                    var c = bitmap.GetPixel(x, y);
                    if (c.A > 0)
                    {
                        if (lookup.Contains(c.ToArgb()))
                        {
                            // exact color already exists
                        }
                        else if (pal.Count < 100)
                        {
                            if (!HasCloseColor(c, pal, 1))
                            {
                                pal.Add(c);
                                lookup.Add(c.ToArgb());
                            }
                        }
                        else if (pal.Count < 240)
                        {
                            if (!HasCloseColor(c, pal, 5))
                            {
                                pal.Add(c);
                                lookup.Add(c.ToArgb());
                            }
                        }
                        else if (pal.Count < 254 && !HasCloseColor(c, pal, 25))
                        {
                            pal.Add(c);
                            lookup.Add(c.ToArgb());
                        }
                    }
                }
            }

            pal.Add(Color.FromArgb(0, 0, 0, 0)); // last entry must be transparent
            return(pal);
        }
 private bool IsMatch()
 {
     NikseBitmap nbmp = new NikseBitmap(pictureBoxCharacter.Image as Bitmap);
     foreach (NOcrPoint op in _nocrChar.LinesForeground)
     {
         foreach (Point point in op.ScaledGetPoints(_nocrChar, nbmp.Width, nbmp.Height))
         {
             if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
             {
                 Color c = nbmp.GetPixel(point.X, point.Y);
                 if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                 {
                 }
                 else
                 {
                     return false;
                 }
             }
         }
     }
     foreach (NOcrPoint op in _nocrChar.LinesBackground)
     {
         foreach (Point point in op.GetPoints())
         {
             if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
             {
                 Color c = nbmp.GetPixel(point.X, point.Y);
                 if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                 {
                     return false;
                 }
             }
         }
     }
     return true;
 }
 public static List<ImageSplitterItem> SplitVerticalTransparentOrBlack(NikseBitmap bmp)
 {
     int startY = 0;
     int size = 0;
     var parts = new List<ImageSplitterItem>();
     for (int y = 0; y < bmp.Height; y++)
     {
         bool allTransparent = true;
         for (int x = 0; x < bmp.Width; x++)
         {
             Color c = bmp.GetPixel(x, y);
             if (c.A > 20 && c.R + c.G + c.B > 20)
             {
                 allTransparent = false;
                 break;
             }
         }
         if (allTransparent)
         {
             if (size > 2 && size <= 15)
             {
                 size++; // at least 15 pixels, like top of 'i' or top of 'È'
             }
             else
             {
                 if (size > 8)
                 {
                     NikseBitmap part = bmp.CopyRectangle(new Rectangle(0, startY, bmp.Width, size + 1));
                     //                            part.Save("c:\\line_0_to_width.bmp");
                     parts.Add(new ImageSplitterItem(0, startY, part));
                     //                            bmp.Save("c:\\original.bmp");
                 }
                 size = 0;
                 startY = y;
             }
         }
         else
         {
             size++;
         }
     }
     if (size > 2)
     {
         if (size == bmp.Height)
             parts.Add(new ImageSplitterItem(0, startY, bmp));
         else
             parts.Add(new ImageSplitterItem(0, startY, bmp.CopyRectangle(new Rectangle(0, startY, bmp.Width, size + 1))));
     }
     return parts;
 }
Пример #17
0
        public NOcrChar GetMatch(NikseBitmap nbmp)
        {
            const int NocrMinColor = 300;
            const int topMargin    = 1;
            double    widthPercent = nbmp.Height * 100.0 / nbmp.Width;

            foreach (NOcrChar oc in OcrCharacters)
            {
                if (Math.Abs(widthPercent - oc.WidthPercent) < 20 && Math.Abs(oc.MarginTop - topMargin) < 5)
                { // only very accurate matches
                    bool ok    = true;
                    var  index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesForeground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, nbmp.Width, nbmp.Height))
                        {
                            if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(point.X, point.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                }
                                else
                                {
                                    Point p = new Point(point.X - 1, point.Y);
                                    if (p.X < 0)
                                    {
                                        p.X = 1;
                                    }
                                    c = nbmp.GetPixel(p.X, p.Y);
                                    if (nbmp.Width > 20 && c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                    {
                                    }
                                    else
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        index++;
                    }
                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesBackground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, nbmp.Width, nbmp.Height))
                        {
                            if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(point.X, point.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                    Point p = new Point(point.X, point.Y);
                                    if (oc.Width > 19 && point.X > 0)
                                    {
                                        p.X = p.X - 1;
                                    }
                                    c = nbmp.GetPixel(p.X, p.Y);
                                    if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        index++;
                    }
                    if (ok)
                    {
                        return(oc);
                    }
                }
            }
            return(null);
        }
Пример #18
0
        /// <summary>
        /// Create RLE buffer from bitmap
        /// </summary>
        /// <param name="bm">Bitmap to compress</param>
        /// <param name="palette">Palette used for bitmap encoding</param>
        /// <returns>RLE buffer</returns>
        private static byte[] EncodeImage(NikseBitmap bm, Dictionary <Color, int> palette)
        {
            var bytes = new List <byte>();

            for (int y = 0; y < bm.Height; y++)
            {
                //eol = false;
                int x;
                int len;
                for (x = 0; x < bm.Width; x += len)
                {
                    Color c = bm.GetPixel(x, y);
                    byte  color;
                    if (palette.ContainsKey(c))
                    {
                        color = (byte)palette[c];
                    }
                    else
                    {
                        color = FindBestMatch(c, palette);
                    }

                    for (len = 1; x + len < bm.Width; len++)
                    {
                        if (bm.GetPixel(x + len, y) != c)
                        {
                            break;
                        }
                    }

                    if (len <= 2 && color != 0)
                    {
                        // only a single occurrence -> add color
                        bytes.Add(color);
                        if (len == 2)
                        {
                            bytes.Add(color);
                        }
                    }
                    else
                    {
                        if (len > 0x3fff)
                        {
                            len = 0x3fff;
                        }
                        bytes.Add(0); // rle id
                        // commented out due to bug in SupRip

                        /*if (color == 0 && x+len == bm.Width)
                         * {
                         *  bytes.Add(0);
                         *  eol = true;
                         * }
                         * else */
                        if (color == 0 && len < 0x40)
                        {
                            // 00 xx -> xx times 0
                            bytes.Add((byte)len);
                        }
                        else if (color == 0)
                        {
                            // 00 4x xx -> xxx zeroes
                            bytes.Add((byte)(0x40 | (len >> 8)));
                            bytes.Add((byte)len);
                        }
                        else if (len < 0x40)
                        {
                            // 00 8x cc -> x times value cc
                            bytes.Add((byte)(0x80 | len));
                            bytes.Add(color);
                        }
                        else
                        {
                            // 00 cx yy cc -> xyy times value cc
                            bytes.Add((byte)(0xc0 | (len >> 8)));
                            bytes.Add((byte)len);
                            bytes.Add(color);
                        }
                    }
                }
                if (/*!eol &&*/ x == bm.Width)
                {
                    bytes.Add(0); // rle id
                    bytes.Add(0);
                }
            }
            int size   = bytes.Count;
            var retval = new byte[size];

            for (int i = 0; i < size; i++)
            {
                retval[i] = bytes[i];
            }
            return(retval);
        }
Пример #19
0
 private static Dictionary<Color, int> GetBitmapPalette(NikseBitmap bitmap)
 {
     var pal = new Dictionary<Color, int>();
     for (int y = 0; y < bitmap.Height; y++)
     {
         for (int x = 0; x < bitmap.Width; x++)
         {
             Color c = bitmap.GetPixel(x, y);
             if (!pal.ContainsKey(c) && c != Color.Transparent && pal.Count < 255)
                 pal.Add(c, pal.Count);
         }
     }
     pal.Add(Color.Transparent, pal.Count); // last entry must be transparent
     return pal;
 }
        internal static int IsBitmapsAlike(NikseBitmap bmp1, ManagedBitmap bmp2)
        {
            int different = 0;
            int maxDiff = bmp1.Width * bmp1.Height / 5;

            for (int x = 1; x < bmp1.Width; x++)
            {
                for (int y = 1; y < bmp1.Height; y++)
                {
                    if (!IsColorClose(bmp1.GetPixel(x, y), bmp2.GetPixel(x, y), 20))
                        different++;
                }
                if (different > maxDiff)
                    return different + 10;
            }
            return different;
        }
        private static bool IsCursiveVerticalLineTransparent(NikseBitmap bmp, int size, int y, int x, List<Point> cursivePoints)
        {
            bool cursiveOk = true;
            int newY = y;
            int newX = x;
            while (cursiveOk && newY < bmp.Height - 1)
            {
                Color c0 = bmp.GetPixel(newX, newY);
                if (c0.A == 0 || IsColorClose(c0, Color.Black, 280))
                {
                    newY++;
                }
                else
                {
                    byte[] c1 = bmp.GetPixelColors(newX - 1, newY - 1);
                    byte[] c2 = bmp.GetPixelColors(newX - 1, newY);
                    if ((c1[0] == 0 || IsColorClose(c1[0], c1[1], c1[2], c1[3], Color.Black, 280)) && // still dark color...
                        (c2[0] == 0 || IsColorClose(c2[0], c2[1], c2[2], c2[3], Color.Black, 280)))
                    {
                        cursivePoints.Add(new Point(newX, newY));
                        if (newX > 1)
                            newX--;
                        else
                            cursiveOk = false;

                        newY++;
                    }
                    else
                    {
                        cursiveOk = false;
                    }
                }

                if (newX < x - size)
                    cursiveOk = false;
            }
            return cursiveOk;
        }
Пример #22
0
 private static int CalculateNumberOfForegroundColors(NikseBitmap nikseBitmap)
 {
     int count = 0;
     for (int y = 0; y < nikseBitmap.Height; y++)
     {
         for (int x = 0; x < nikseBitmap.Width; x++)
         {
             Color c = nikseBitmap.GetPixel(x, y);
             if (c.A > 100 && c.R + c.G + c.B > 200)
                 count++;
         }
     }
     return count;
 }
Пример #23
0
        public NOcrChar GetMatch(NikseBitmap nbmp)
        {
            const int NocrMinColor = 300;
            int topMargin = 1;
            int index = 0;
            double widthPercent = nbmp.Height * 100.0 / nbmp.Width;

            foreach (NOcrChar oc in OcrCharacters)
            {
                if (Math.Abs(widthPercent - oc.WidthPercent) < 20 && Math.Abs(oc.MarginTop - topMargin) < 5)
                { // only very accurate matches

                    bool ok = true;
                    index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesForeground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, nbmp.Width, nbmp.Height))
                        {
                            if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(point.X, point.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                }
                                else
                                {
                                    Point p = new Point(point.X - 1, point.Y);
                                    if (p.X < 0)
                                        p.X = 1;
                                    c = nbmp.GetPixel(p.X, p.Y);
                                    if (nbmp.Width > 20 && c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                    {
                                    }
                                    else
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        index++;
                    }
                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesBackground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, nbmp.Width, nbmp.Height))
                        {
                            if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(point.X, point.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                    Point p = new Point(point.X, point.Y);
                                    if (oc.Width > 19 && point.X > 0)
                                        p.X = p.X - 1;
                                    c = nbmp.GetPixel(p.X, p.Y);
                                    if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        index++;
                    }
                    if (ok)
                        return oc;
                }
            }

            return null;
        }
Пример #24
0
        private static NOcrChar NOcrFindExpandedMatch(NikseBitmap nbmp, ImageSplitterItem targetItem, List<NOcrChar> nOcrChars)
        {
            int w = targetItem.NikseBitmap.Width;
            foreach (NOcrChar oc in nOcrChars)
            {
                if (oc.ExpandCount > 1 && oc.Width > w && targetItem.X + oc.Width < nbmp.Width)
                {
                    bool ok = true;
                    var index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesForeground[index];
                        foreach (Point point in op.GetPoints())
                        {
                            Point p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nbmp.Width && p.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(p.X, p.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                }
                                else
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesBackground[index];
                        foreach (Point point in op.GetPoints())
                        {
                            Point p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nbmp.Width && p.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(p.X, p.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    if (ok)
                        return oc;

                    ok = true;
                    index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesForeground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, oc.Width, oc.Height - 1))
                        {
                            Point p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nbmp.Width && p.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(p.X, p.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                }
                                else
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesBackground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, oc.Width, oc.Height - 1))
                        {
                            Point p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nbmp.Width && p.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(p.X, p.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    if (ok)
                        return oc;
                }
            }
            return null;
        }
        private static bool IsMatchPointBackGround(NOcrPoint op, bool loose, NikseBitmap nbmp, NOcrChar nOcrChar)
        {
            foreach (Point point in op.ScaledGetPoints(nOcrChar, nbmp.Width, nbmp.Height))
            {
                if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                {
                    Color c = nbmp.GetPixel(point.X, point.Y);
                    if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                    {
                        return false;
                    }

                    if (nbmp.Width > 10 && point.X + 1 < nbmp.Width)
                    {
                        c = nbmp.GetPixel(point.X + 1, point.Y);
                        if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                        {
                            return false;
                        }
                    }

                    if (loose)
                    {
                        if (nbmp.Width > 10 && point.X >= 1)
                        {
                            c = nbmp.GetPixel(point.X - 1, point.Y);
                            if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                            {
                                return false;
                            }
                        }

                        if (nbmp.Height > 10 && point.Y + 1 < nbmp.Height)
                        {
                            c = nbmp.GetPixel(point.X, point.Y + 1);
                            if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                            {
                                return false;
                            }
                        }

                        if (nbmp.Height > 10 && point.Y >= 1)
                        {
                            c = nbmp.GetPixel(point.X, point.Y - 1);
                            if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                            {
                                return false;
                            }
                        }
                    }
                }
            }
            return true;
        }