Esempio n. 1
0
        private void BinaryOcrTrain_Shown(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_autoDetectFontText))
            {
                return;
            }

            BinaryOcrTrain_ResizeEnd(sender, e);
            SelectAll_Click(this, EventArgs.Empty);
            int numberOfCharactersLearned = 0;
            int numberOfCharactersSkipped = 0;

            foreach (ListViewItem fontItem in listViewFonts.CheckedItems)
            {
                _subtitleFontName = fontItem.Text;
                labelInfo.Text    = $"Checking font '{_subtitleFontName}'... {AutoDetectedFonts.Count} hits";
                labelInfo.Refresh();
                Application.DoEvents();

                for (_subtitleFontSize = 20; _subtitleFontSize <= 100; _subtitleFontSize++)
                {
                    if (!string.IsNullOrEmpty(_autoDetectFontText))
                    {
                        var s     = _autoDetectFontText;
                        var bicDb = new BinaryOcrDb(null);
                        TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, s, false, false);
                        if (bicDb.FindExactMatch(_autoDetectFontBob) >= 0)
                        {
                            AutoDetectedFonts.Add(_subtitleFontName + " " + _subtitleFontSize);
                        }
                        else
                        {
                            // allow for error %
                            var smallestDifference = int.MaxValue;
                            foreach (var compareItem in bicDb.CompareImages)
                            {
                                if (compareItem.Width == _autoDetectFontBob.Width && compareItem.Height == _autoDetectFontBob.Height) // precise math in size
                                {
                                    if (Math.Abs(compareItem.NumberOfColoredPixels - _autoDetectFontBob.NumberOfColoredPixels) < 3)
                                    {
                                        int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, _autoDetectFontBob);
                                        if (dif < smallestDifference)
                                        {
                                            if (!BinaryOcrDb.AllowEqual(compareItem, _autoDetectFontBob))
                                            {
                                                continue;
                                            }

                                            smallestDifference = dif;
                                            if (dif < 3)
                                            {
                                                break; // foreach ending
                                            }
                                        }
                                    }
                                }
                            }

                            if (smallestDifference < 13)
                            {
                                AutoDetectedFonts.Add($"{_subtitleFontName} {_subtitleFontSize} - diff={smallestDifference}");
                            }
                        }

                        bicDb = new BinaryOcrDb(null);
                        TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, s, false, true);
                        if (bicDb.FindExactMatch(_autoDetectFontBob) >= 0)
                        {
                            AutoDetectedFonts.Add(_subtitleFontName + " " + _subtitleFontSize + " italic");
                        }

                        if (checkBoxBold.Checked)
                        {
                            bicDb = new BinaryOcrDb(null);
                            TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, s, true, false);
                            if (bicDb.FindExactMatch(_autoDetectFontBob) >= 0)
                            {
                                AutoDetectedFonts.Add(_subtitleFontName + " " + _subtitleFontSize + " bold");
                            }
                        }
                    }
                }
            }

            DialogResult = DialogResult.OK;
        }
Esempio n. 2
0
        private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            labelImageInfo.Text   = string.Empty;
            labelExpandCount.Text = string.Empty;

            if (listBoxInspectItems.SelectedIndex < 0)
            {
                return;
            }

            _selectedCompareNode            = null;
            _selectedCompareBinaryOcrBitmap = null;

            var img = _imageSources[listBoxInspectItems.SelectedIndex];

            pictureBoxInspectItem.Image = img;
            if (img != null)
            {
                pictureBoxInspectItem.Width  = img.Width + 2;
                pictureBoxInspectItem.Height = img.Height + 2;
            }

            pictureBoxCompareBitmap.Image       = null;
            pictureBoxCompareBitmapDouble.Image = null;

            int index = listBoxInspectItems.SelectedIndex;
            var match = _matches[index];

            _selectedMatch = match;
            if (!string.IsNullOrEmpty(match.Name))
            {
                if (_binOcrDb != null)
                {
                    bool bobFound = false;
                    foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImages)
                    {
                        if (match.Name == bob.Key)
                        {
                            textBoxText.Text                = bob.Text;
                            checkBoxItalic.Checked          = bob.Italic;
                            _selectedCompareBinaryOcrBitmap = bob;
                            SetDbItemView(bob.ToOldBitmap());
                            var matchBob = new BinaryOcrBitmap(new NikseBitmap(_imageSources[listBoxInspectItems.SelectedIndex]));
                            if (matchBob.Hash == bob.Hash && matchBob.Width == bob.Width && matchBob.Height == bob.Height && matchBob.NumberOfColoredPixels == bob.NumberOfColoredPixels)
                            {
                                buttonAddBetterMatch.Enabled = false; // exact match
                            }
                            else
                            {
                                buttonAddBetterMatch.Enabled = true;
                            }
                            bobFound = true;
                            break;
                        }
                    }
                    if (!bobFound)
                    {
                        foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImagesExpanded)
                        {
                            if (match.Name == bob.Key)
                            {
                                textBoxText.Text                = bob.Text;
                                checkBoxItalic.Checked          = bob.Italic;
                                _selectedCompareBinaryOcrBitmap = bob;

                                var oldBitmap = bob.ToOldBitmap();
                                SetDbItemView(oldBitmap);

                                int dif = 1;
                                if (oldBitmap.Width == match.ImageSplitterItem.NikseBitmap.Width && oldBitmap.Height == match.ImageSplitterItem.NikseBitmap.Height)
                                {
                                    dif = NikseBitmapImageSplitter.IsBitmapsAlike(match.ImageSplitterItem.NikseBitmap, oldBitmap);
                                }
                                buttonAddBetterMatch.Enabled = dif > 0; // if exact match then don't allow "Add better match"
                                labelExpandCount.Text        = $"Expand count: {bob.ExpandCount}";
                                break;
                            }
                        }
                    }
                }
                else
                {
                    foreach (XmlNode node in ImageCompareDocument.DocumentElement.ChildNodes)
                    {
                        if (node.Attributes["Text"] != null && node.InnerText == match.Name)
                        {
                            string text = node.Attributes["Text"].InnerText;
                            textBoxText.Text       = text;
                            checkBoxItalic.Checked = node.Attributes["Italic"] != null;
                            string databaseName = Path.Combine(_directoryPath, "Images.db");
                            using (var f = new FileStream(databaseName, FileMode.Open))
                            {
                                try
                                {
                                    string name = node.InnerText;
                                    int    pos  = Convert.ToInt32(name);
                                    f.Position = pos;
                                    var mbmp   = new ManagedBitmap(f);
                                    var bitmap = mbmp.ToOldBitmap();
                                    SetDbItemView(mbmp.ToOldBitmap());
                                    labelImageInfo.Text = string.Format(Configuration.Settings.Language.VobSubEditCharacters.Image + " - {0}x{1}", bitmap.Width, bitmap.Height);
                                }
                                catch (Exception exception)
                                {
                                    labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.Image;
                                    MessageBox.Show(exception.Message);
                                }
                            }

                            _selectedCompareNode = node;
                            break;
                        }
                    }
                }
            }

            buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.AddBetterMatch;
            if (_selectedMatch.Text == Configuration.Settings.Language.VobSubOcr.NoMatch)
            {
                buttonUpdate.Enabled                  = false;
                buttonDelete.Enabled                  = false;
                buttonAddBetterMatch.Enabled          = true;
                buttonAddBetterMatch.Text             = Configuration.Settings.Language.VobSubOcrCharacterInspect.Add;
                textBoxText.Enabled                   = true;
                textBoxText.Text                      = string.Empty;
                checkBoxItalic.Enabled                = true;
                pictureBoxCompareBitmap.Visible       = true;
                pictureBoxCompareBitmapDouble.Visible = true;
                labelDoubleSize.Visible               = true;
            }
            else if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null)
            {
                buttonUpdate.Enabled                  = false;
                buttonDelete.Enabled                  = false;
                buttonAddBetterMatch.Enabled          = true;
                textBoxText.Enabled                   = true;
                textBoxText.Text                      = string.Empty;
                checkBoxItalic.Enabled                = false;
                pictureBoxCompareBitmap.Visible       = false;
                pictureBoxCompareBitmapDouble.Visible = false;
                labelDoubleSize.Visible               = false;
                if (img == null)
                {
                    buttonAddBetterMatch.Enabled = false;
                }
            }
            else
            {
                buttonUpdate.Enabled = true;
                buttonDelete.Enabled = true;
                if (_selectedCompareNode != null)
                {
                    buttonAddBetterMatch.Enabled = true;
                }
                textBoxText.Enabled                   = true;
                checkBoxItalic.Enabled                = true;
                pictureBoxCompareBitmap.Visible       = true;
                pictureBoxCompareBitmapDouble.Visible = true;
                labelDoubleSize.Visible               = true;
            }
        }