private void buttonImport_Click(object sender, EventArgs e) { int importedCount = 0; int notImportedCount = 0; openFileDialog1.Filter = "nOCR files|nOCR_*.xml"; openFileDialog1.InitialDirectory = Configuration.DataDirectory; openFileDialog1.FileName = string.Empty; openFileDialog1.Title = "Import existing nOCR database into current"; if (openFileDialog1.ShowDialog(this) == DialogResult.OK) { foreach (NOcrChar newChar in VobSubOcr.LoadNOcr(openFileDialog1.FileName)) { bool found = false; foreach (NOcrChar oldChar in _nocrChars) { if (oldChar.Text == newChar.Text && oldChar.Width == newChar.Width && oldChar.Height == newChar.Height && oldChar.MarginTop == newChar.MarginTop && oldChar.ExpandCount == newChar.ExpandCount && oldChar.LinesForeground.Count == newChar.LinesForeground.Count && oldChar.LinesBackground.Count == newChar.LinesBackground.Count) { found = true; for (int i = 0; i < oldChar.LinesForeground.Count; i++) { if (oldChar.LinesForeground[i].Start.X != newChar.LinesForeground[i].Start.X || oldChar.LinesForeground[i].Start.Y != newChar.LinesForeground[i].Start.Y || oldChar.LinesForeground[i].End.X != newChar.LinesForeground[i].End.X || oldChar.LinesForeground[i].End.Y != newChar.LinesForeground[i].End.Y) { found = false; } } for (int i = 0; i < oldChar.LinesBackground.Count; i++) { if (oldChar.LinesBackground[i].Start.X != newChar.LinesBackground[i].Start.X || oldChar.LinesBackground[i].Start.Y != newChar.LinesBackground[i].Start.Y || oldChar.LinesBackground[i].End.X != newChar.LinesBackground[i].End.X || oldChar.LinesBackground[i].End.Y != newChar.LinesBackground[i].End.Y) { found = false; } } } } if (!found) { _nocrChars.Add(newChar); importedCount++; } else { notImportedCount++; } } MessageBox.Show(string.Format("Number of characters imported: {0}\r\nNumber of characters not imported (already present): {1}", importedCount, notImportedCount)); } }
private void buttonSaveAs_Click(object sender, EventArgs e) { if (_languages != null && comboBoxLanguages.SelectedIndex >= 0 && comboBoxLanguages.SelectedIndex < _languages.Count) { SelectedLanguageString = _languages[comboBoxLanguages.SelectedIndex]; } else { SelectedLanguageString = null; } var subs = new List <VobSubMergedPack>(); foreach (var x in listBox1.Items) { subs.Add((x as SubListBoxItem).SubPack); } using (var formSubOcr = new VobSubOcr()) { formSubOcr.InitializeQuick(subs, _palette, Configuration.Settings.VobSubOcr, SelectedLanguageString); var subtitle = formSubOcr.ReadyVobSubRip(); using (var exportBdnXmlPng = new ExportPngXml()) { exportBdnXmlPng.InitializeFromVobSubOcr(subtitle, new Core.SubtitleFormats.SubRip(), "VOBSUB", "DVD", formSubOcr, SelectedLanguageString); exportBdnXmlPng.ShowDialog(this); } } }
private void MakeExpandImage() { var splitterItem = _splitterItems[_startIndex]; if (splitterItem.NikseBitmap == null) { return; } var expandList = new List <ImageSplitterItem> { splitterItem }; for (int i = 1; i < listBoxInspectItems.Items.Count; i++) { if (i < numericUpDownExpandCount.Value) { splitterItem = _splitterItems[_startIndex + i + _extraCount]; if (splitterItem.NikseBitmap == null) { break; } expandList.Add(splitterItem); } } ExpandItem = VobSubOcr.GetExpandedSelectionNew(_wholeImage, expandList); var newBmp = ExpandItem.NikseBitmap.GetBitmap(); pictureBoxInspectItem.Image = newBmp; pictureBoxInspectItem.Width = newBmp.Width; pictureBoxInspectItem.Height = newBmp.Height; }
private static void ConvertBluRaySubtitle(string fileName, string toFormat, string offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IList <SubtitleFormat> formats, bool overwrite, string pacCodePage, double?targetFrameRate, bool removeTextForHi, bool fixCommonErrors, bool redoCasing) { SubtitleFormat format = Utilities.GetSubtitleFormatByFriendlyName(toFormat) ?? new SubRip(); var log = new StringBuilder(); Console.WriteLine("Loading subtitles from file \"{0}\"", fileName); var bluRaySubtitles = BluRaySupParser.ParseBluRaySup(fileName, log); Subtitle sub; using (var vobSubOcr = new VobSubOcr()) { Console.WriteLine("Using OCR to extract subtitles"); vobSubOcr.FileName = Path.GetFileName(fileName); vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName); sub = vobSubOcr.SubtitleFromOcr; Console.WriteLine("Extracted subtitles from file \"{0}\"", fileName); } if (sub != null) { Console.WriteLine("Converted subtitle"); BatchConvertSave(toFormat, offset, targetEncoding, outputFolder, count, ref converted, ref errors, formats, fileName, sub, format, overwrite, pacCodePage, targetFrameRate, removeTextForHi, fixCommonErrors, redoCasing); } }
internal void Initialize(Bitmap vobSubImage, ImageSplitterItem character, Point position, bool italicChecked, bool showShrink, VobSubOcr.CompareMatch bestGuess, List <VobSubOcr.ImageCompareAddition> additions, VobSubOcr vobSubForm) { listBoxLinesForeground.Items.Clear(); listBoxlinesBackground.Items.Clear(); NikseBitmap nbmp = new NikseBitmap(vobSubImage); nbmp.ReplaceTransparentWith(Color.Black); vobSubImage = nbmp.GetBitmap(); radioButtonHot.Checked = true; ShrinkSelection = false; ExpandSelection = false; textBoxCharacters.Text = string.Empty; _vobSubForm = vobSubForm; _additions = additions; _nocrChar = new NOcrChar(); _nocrChar.MarginTop = character.Y - character.ParentY; _imageWidth = character.NikseBitmap.Width; _imageHeight = character.NikseBitmap.Height; _drawLineOn = false; _warningNoNotForegroundLinesShown = false; buttonShrinkSelection.Visible = showShrink; if (position.X != -1 && position.Y != -1) { StartPosition = FormStartPosition.Manual; Left = position.X; Top = position.Y; } pictureBoxSubtitleImage.Image = vobSubImage; pictureBoxCharacter.Image = character.NikseBitmap.GetBitmap(); Bitmap org = (Bitmap)vobSubImage.Clone(); Bitmap bm = new Bitmap(org.Width, org.Height); Graphics g = Graphics.FromImage(bm); g.DrawImage(org, 0, 0, org.Width, org.Height); g.DrawRectangle(Pens.Red, character.X, character.Y, character.NikseBitmap.Width, character.NikseBitmap.Height - 1); g.Dispose(); pictureBoxSubtitleImage.Image = bm; pictureBoxCharacter.Top = labelCharacters.Top + 16; SizePictureBox(); checkBoxItalic.Checked = italicChecked; _history = new List <NOcrChar>(); _historyIndex = -1; _nocrChar.Width = _imageWidth; _nocrChar.Height = _imageHeight; GenerateLineSegments(150, false, _nocrChar, new NikseBitmap(pictureBoxCharacter.Image as Bitmap)); ShowOcrPoints(); pictureBoxCharacter.Invalidate(); }
private void subtitleListView1_SelectedIndexChanged(object sender, EventArgs e) { if (subtitleListView1.SelectedItems.Count < 1) { timeUpDownStartTime.TimeCode = new TimeCode(); timeUpDownEndTime.TimeCode = new TimeCode(); numericUpDownX.Value = 0; numericUpDownY.Value = 0; pictureBoxMovableImage.Image?.Dispose(); pictureBoxScreen.Image?.Dispose(); return; } var idx = subtitleListView1.SelectedItems[0].Index; if (_bluRaySubtitles != null) { var sub = _bluRaySubtitles[idx]; var extra = _extra[idx]; timeUpDownStartTime.TimeCode = sub.StartTimeCode; timeUpDownEndTime.TimeCode = sub.EndTimeCode; checkBoxIsForced.Checked = extra.Forced; numericUpDownX.Value = extra.X; numericUpDownY.Value = extra.Y; pictureBoxMovableImage.Image?.Dispose(); pictureBoxScreen.Image?.Dispose(); var bmp = extra.Bitmap != null ? (Bitmap)extra.Bitmap.Clone() : sub.GetBitmap(); var nikseBitmap = new NikseBitmap(bmp); nikseBitmap.ReplaceTransparentWith(Color.Black); bmp.Dispose(); bmp = nikseBitmap.GetBitmap(); var screenBmp = new Bitmap((int)numericUpDownScreenWidth.Value, (int)numericUpDownScreenHeight.Value); using (var g = Graphics.FromImage(screenBmp)) { using (var brush = new SolidBrush(Color.Black)) { g.FillRectangle(brush, 0, 0, screenBmp.Width, screenBmp.Height); } //g.DrawImage(bmp, extra.X, extra.Y); } pictureBoxMovableImage.Width = bmp.Width; var widthAspect = pictureBoxScreen.Width / numericUpDownScreenWidth.Value; var heightAspect = pictureBoxScreen.Height / numericUpDownScreenHeight.Value; var scaledBmp = VobSubOcr.ResizeBitmap(bmp, (int)Math.Round(bmp.Width * widthAspect), (int)Math.Round(bmp.Height * heightAspect)); pictureBoxMovableImage.Width = scaledBmp.Width; pictureBoxMovableImage.Height = scaledBmp.Height; pictureBoxMovableImage.SizeMode = PictureBoxSizeMode.StretchImage; pictureBoxMovableImage.Image = scaledBmp; pictureBoxMovableImage.Left = (int)Math.Round(extra.X * widthAspect); pictureBoxMovableImage.Top = (int)Math.Round(extra.Y * heightAspect); pictureBoxScreen.Image = screenBmp; labelCurrentSize.Text = string.Format("Size: {0}x{1}", bmp.Width, bmp.Height); } }
internal void Initialize(Bitmap bitmap, int pixelsIsSpace, bool rightToLeft, NOcrDb nOcrDb, VobSubOcr vobSubOcr, double unItalicFactor) { _bitmap = bitmap; var nbmp = new NikseBitmap(bitmap); nbmp.ReplaceNonWhiteWithTransparent(); bitmap = nbmp.GetBitmap(); _bitmap2 = bitmap; _nocrChars = nOcrDb.OcrCharacters; _matchList = new List <VobSubOcr.CompareMatch>(); _vobSubOcr = vobSubOcr; _unItalicFactor = unItalicFactor; int minLineHeight = 6; _imageList = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom, minLineHeight); // _imageList = NikseBitmapImageSplitter.SplitBitmapToLetters(nbmp, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom); int index = 0; while (index < _imageList.Count) { ImageSplitterItem item = _imageList[index]; if (item.NikseBitmap == null) { listBoxInspectItems.Items.Add(item.SpecialCharacter); _matchList.Add(null); } else { nbmp = item.NikseBitmap; nbmp.ReplaceNonWhiteWithTransparent(); item.Y += nbmp.CropTopTransparent(0); nbmp.CropTransparentSidesAndBottom(0, true); nbmp.ReplaceTransparentWith(Color.Black); //get nocr matches Nikse.SubtitleEdit.Forms.VobSubOcr.CompareMatch match = vobSubOcr.GetNOcrCompareMatchNew(item, nbmp, nOcrDb, _unItalicFactor, false, false); if (match == null) { listBoxInspectItems.Items.Add("?"); _matchList.Add(null); } else { listBoxInspectItems.Items.Add(match.Text); _matchList.Add(match); } } index++; } }
private void SaveAllImagesWithHtmlIndexViewToolStripMenuItem_Click(object sender, EventArgs e) { var subtitles = GetSelectedSubtitles(); if (subtitles == null) { return; } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, _fileName); formSubOcr.SaveAllImagesWithHtmlIndexViewToolStripMenuItem_Click(sender, e); } }
private void ExportTo(string exportType) { var subtitles = GetSelectedSubtitles(); if (subtitles == null) { return; } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, _fileName); using (var exportBdnXmlPng = new ExportPngXml()) { exportBdnXmlPng.InitializeFromVobSubOcr(formSubOcr.SubtitleFromOcr, new SubRip(), exportType, _fileName, formSubOcr, null); exportBdnXmlPng.ShowDialog(this); } } }
private void buttonSaveAs_Click(object sender, EventArgs e) { var subtitles = GetSelectedSubtitles(); if (subtitles == null) { return; } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, _fileName); var subtitle = formSubOcr.ReadyVobSubRip(); using (var exportBdnXmlPng = new ExportPngXml()) { exportBdnXmlPng.InitializeFromVobSubOcr(subtitle, new Core.SubtitleFormats.SubRip(), ExportPngXml.ExportFormats.BluraySup, _fileName, formSubOcr, null); exportBdnXmlPng.ShowDialog(this); } } }
private static void ConvertVobSubSubtitle(string fileName, string toFormat, string offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IList <SubtitleFormat> formats, bool overwrite, string pacCodePage, double?targetFrameRate) { var format = Utilities.GetSubtitleFormatByFriendlyName(toFormat) ?? new SubRip(); Console.WriteLine("Loading subtitles from file \"{0}\"", fileName); Subtitle sub; using (var vobSubOcr = new VobSubOcr()) { Console.WriteLine("Using OCR to extract subtitles"); vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr); sub = vobSubOcr.SubtitleFromOcr; Console.WriteLine("Extracted subtitles from file \"{0}\"", fileName); } if (sub != null) { Console.WriteLine("Converted subtitle"); BatchConvertSave(toFormat, offset, targetEncoding, outputFolder, count, ref converted, ref errors, formats, fileName, sub, format, overwrite, pacCodePage, targetFrameRate); } }
internal void Initialize(Bitmap vobSubImage, ImageSplitterItem character, Point position, bool italicChecked, bool showShrink, VobSubOcr.CompareMatch bestGuess, List <VobSubOcr.ImageCompareAddition> additions, VobSubOcr vobSubForm) { ShrinkSelection = false; ExpandSelection = false; textBoxCharacters.Text = string.Empty; if (bestGuess != null) { buttonGuess.Visible = false; // hm... not too useful :( buttonGuess.Text = bestGuess.Text; } else { buttonGuess.Visible = false; } _vobSubForm = vobSubForm; _additions = additions; buttonShrinkSelection.Visible = showShrink; checkBoxItalic.Checked = italicChecked; if (position.X != -1 && position.Y != -1) { StartPosition = FormStartPosition.Manual; Left = position.X; Top = position.Y; } pictureBoxSubtitleImage.Image = vobSubImage; pictureBoxCharacter.Image = character.NikseBitmap.GetBitmap(); if (_additions.Count > 0) { var last = _additions[_additions.Count - 1]; buttonLastEdit.Visible = true; if (last.Italic) { buttonLastEdit.Font = new Font(buttonLastEdit.Font.FontFamily, buttonLastEdit.Font.Size, FontStyle.Italic); } else { buttonLastEdit.Font = new Font(buttonLastEdit.Font.FontFamily, buttonLastEdit.Font.Size); } pictureBoxLastEdit.Visible = true; pictureBoxLastEdit.Image = last.Image.GetBitmap(); buttonLastEdit.Text = string.Format(Configuration.Settings.Language.VobSubOcrCharacter.EditLastX, last.Text); pictureBoxLastEdit.Top = buttonLastEdit.Top - last.Image.Height + buttonLastEdit.Height; } else { buttonLastEdit.Visible = false; pictureBoxLastEdit.Visible = false; } using (var org = (Bitmap)vobSubImage.Clone()) { var bm = new Bitmap(org.Width, org.Height); using (var g = Graphics.FromImage(bm)) { g.DrawImage(org, 0, 0, org.Width, org.Height); g.DrawRectangle(Pens.Red, character.X, character.Y, character.NikseBitmap.Width, character.NikseBitmap.Height - 1); } pictureBoxSubtitleImage.Image = bm; } pictureBoxCharacter.Top = labelCharacters.Top + 16; pictureBoxLastEdit.Left = buttonLastEdit.Left + buttonLastEdit.Width + 5; }
private void buttonAddBetterMatch_Click(object sender, EventArgs e) { var expandSelectionList = new List <ImageSplitterItem>(); if (listBoxInspectItems.SelectedIndex < 0) { return; } int index = listBoxInspectItems.SelectedIndex; var img = _imageList[index]; if (img.NikseBitmap == null) { return; } var match = _matchList[index]; var vobSubOcrNOcrCharacter = new VobSubOcrNOcrCharacter(); vobSubOcrNOcrCharacter.Initialize(_bitmap, img, new Point(0, 0), false, expandSelectionList.Count > 1, null, null, _vobSubOcr); DialogResult result = vobSubOcrNOcrCharacter.ShowDialog(this); bool expandSelection = false; bool shrinkSelection = false; if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ExpandSelection) { expandSelection = true; expandSelectionList.Add(img); } while (result == DialogResult.OK && (vobSubOcrNOcrCharacter.ShrinkSelection || vobSubOcrNOcrCharacter.ExpandSelection)) { if (expandSelection || shrinkSelection) { expandSelection = false; if (shrinkSelection && index > 0) { shrinkSelection = false; } else if (index + 1 < _imageList.Count && _imageList[index + 1].NikseBitmap != null) // only allow expand to EndOfLine or space { index++; expandSelectionList.Add(_imageList[index]); } img = VobSubOcr.GetExpandedSelection(new NikseBitmap(_bitmap), expandSelectionList, false); // true } vobSubOcrNOcrCharacter.Initialize(_bitmap2, img, new Point(0, 0), false, expandSelectionList.Count > 1, null, null, _vobSubOcr); result = vobSubOcrNOcrCharacter.ShowDialog(this); if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ShrinkSelection) { shrinkSelection = true; index--; if (expandSelectionList.Count > 0) { expandSelectionList.RemoveAt(expandSelectionList.Count - 1); } } else if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ExpandSelection) { expandSelection = true; index++; expandSelectionList.Add(_imageList[index]); } } if (result == DialogResult.OK) { if (expandSelectionList.Count > 1) { vobSubOcrNOcrCharacter.NOcrChar.ExpandCount = expandSelectionList.Count; } _nocrChars.Add(vobSubOcrNOcrCharacter.NOcrChar); _vobSubOcr.SaveNOcrWithCurrentLanguage(); DialogResult = DialogResult.OK; } }
internal void Initialize(Bitmap bitmap, int pixelsIsSpace, bool rightToLeft, NOcrDb nOcrDb, VobSubOcr vobSubOcr, bool italic, int minLineHeight, bool deepSeek) { _bitmap = bitmap; var nikseBitmap = new NikseBitmap(bitmap); nikseBitmap.ReplaceNonWhiteWithTransparent(); _nOcrChars = nOcrDb.OcrCharacters; _nOcrDb = nOcrDb; _matchList = new List <VobSubOcr.CompareMatch>(); _imageList = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nikseBitmap, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom, minLineHeight, false); int index = 0; _indexLookup = new Dictionary <int, int>(); while (index < _imageList.Count) { var item = _imageList[index]; if (item.NikseBitmap == null) { _indexLookup.Add(listBoxInspectItems.Items.Count, index); listBoxInspectItems.Items.Add(item.SpecialCharacter); _matchList.Add(null); } else { var match = vobSubOcr.GetNOcrCompareMatchNew(item, nikseBitmap, nOcrDb, italic, deepSeek, index, _imageList); if (match == null) { _indexLookup.Add(listBoxInspectItems.Items.Count, index); listBoxInspectItems.Items.Add("?"); _matchList.Add(null); } else { _indexLookup.Add(listBoxInspectItems.Items.Count, index); listBoxInspectItems.Items.Add(match.Text); _matchList.Add(match); if (match.ExpandCount > 0) { index += match.ExpandCount - 1; } } } index++; } }
private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e) { labelImageSize.Text = string.Empty; labelExpandCount.Text = string.Empty; if (listBoxInspectItems.SelectedIndex < 0) { return; } var idx = _indexLookup[listBoxInspectItems.SelectedIndex]; var img = _imageList[idx]; var match = _matchList[listBoxInspectItems.SelectedIndex]; if (img.NikseBitmap != null) { pictureBoxInspectItem.Width = img.NikseBitmap.Width; pictureBoxInspectItem.Height = img.NikseBitmap.Height; labelImageSize.Top = pictureBoxInspectItem.Top + img.NikseBitmap.Height + 7; labelImageSize.Text = img.NikseBitmap.Width + "x" + img.NikseBitmap.Height; if (match != null && match.ExpandCount > 1) { var expandList = new List <ImageSplitterItem>(); for (int i = idx; i < idx + match.ExpandCount; i++) { expandList.Add(_imageList[i]); } var expandItem = VobSubOcr.GetExpandedSelectionNew(new NikseBitmap(_bitmap), expandList); var old = expandItem.NikseBitmap.GetBitmap(); pictureBoxInspectItem.Image = old; pictureBoxCharacter.Image = old; pictureBoxInspectItem.Width = old.Width; pictureBoxInspectItem.Height = old.Height; labelExpandCount.Text = $"Expand count: {match.ExpandCount}"; } else { var old = img.NikseBitmap.GetBitmap(); pictureBoxInspectItem.Image = old; pictureBoxCharacter.Image = old; } SizePictureBox(); } else { pictureBoxInspectItem.Image = null; pictureBoxCharacter.Image = null; } buttonAddBetterMatch.Text = LanguageSettings.Current.VobSubOcrCharacterInspect.AddBetterMatch; if (match == null) { if (img.NikseBitmap != null && img.SpecialCharacter == null) { // no match found buttonUpdate.Enabled = false; buttonDelete.Enabled = false; textBoxText.Text = string.Empty; _nOcrChar = null; pictureBoxCharacter.Invalidate(); buttonEditDB.Enabled = true; buttonAddBetterMatch.Enabled = true; buttonAddBetterMatch.Text = LanguageSettings.Current.VobSubOcrCharacterInspect.Add; textBoxText.Enabled = true; checkBoxItalic.Enabled = true; } else { // spaces+new lines _nOcrChar = null; pictureBoxCharacter.Invalidate(); buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonEditDB.Enabled = false; buttonAddBetterMatch.Enabled = false; textBoxText.Text = string.Empty; textBoxText.Enabled = false; checkBoxItalic.Checked = false; checkBoxItalic.Enabled = false; } } else if (match.NOcrCharacter == null) { // no match found buttonUpdate.Enabled = false; buttonDelete.Enabled = false; textBoxText.Text = string.Empty; checkBoxItalic.Checked = match.Italic; _nOcrChar = null; pictureBoxCharacter.Invalidate(); buttonEditDB.Enabled = true; buttonAddBetterMatch.Enabled = true; buttonAddBetterMatch.Text = LanguageSettings.Current.VobSubOcrCharacterInspect.Add; textBoxText.Enabled = true; checkBoxItalic.Enabled = true; } else { buttonUpdate.Enabled = true; buttonDelete.Enabled = true; textBoxText.Text = match.Text; checkBoxItalic.Checked = match.Italic; _nOcrChar = match.NOcrCharacter; pictureBoxCharacter.Invalidate(); buttonEditDB.Enabled = true; buttonAddBetterMatch.Enabled = true; textBoxText.Enabled = true; checkBoxItalic.Enabled = true; } }
private void ListBoxFileNamesSelectedIndexChanged(object sender, EventArgs e) { checkBoxItalic.Checked = _italics[listBoxFileNames.SelectedIndex]; string name = listBoxFileNames.Items[listBoxFileNames.SelectedIndex].ToString(); string databaseName = _directoryPath + "Images.db"; Bitmap bmp = null; labelExpandCount.Text = string.Empty; labelImageInfo.Text = string.Empty; if (_binOcrDb != null) { var bob = GetSelectedBinOcrBitmap(); if (bob != null) { bmp = bob.ToOldBitmap(); labelImageInfo.Text = string.Format("Top:{0}, {1} colored pixels of {2}", bob.Y, bob.NumberOfColoredPixels, (bob.Width * bob.Height)); //bool italicI; //var isI = bob.IsLowercaseI(out italicI); //labelImageInfo.Text = string.Format("T:{0} j{1} :{2} i{3}{4} '{5} #{6}/{7}", bob.Y, bob.IsLowercaseJ(), bob.IsColon(), isI, italicI ? "i" : "", bob.IsApostrophe(), bob.NumberOfColoredPixels, (bob.Width * bob.Height)); if (bob.ExpandCount > 0) { labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount); } } } else if (File.Exists(databaseName)) { using (var f = new FileStream(databaseName, FileMode.Open)) { if (name.Contains(']')) { name = name.Substring(name.IndexOf(']') + 1).Trim(); } f.Position = Convert.ToInt64(name); bmp = new ManagedBitmap(f).ToOldBitmap(); } } if (bmp == null) { bmp = new Bitmap(1, 1); labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.ImageFileNotFound; } pictureBox1.Image = bmp; pictureBox1.Width = bmp.Width + 2; pictureBox1.Height = bmp.Height + 2; pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; var bmp2 = VobSubOcr.ResizeBitmap(bmp, bmp.Width * 2, bmp.Height * 2); pictureBox2.Image = bmp2; pictureBox2.Width = bmp2.Width + 2; pictureBox2.Height = bmp2.Height + 2; pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage; if (Additions != null && Additions.Count > 0) { if (_binOcrDb != null) { var bob = GetSelectedBinOcrBitmap(); foreach (var a in Additions) { if (bob != null && bob.Text != null && bob.Key == a.Name) { textBoxText.Text = a.Text; checkBoxItalic.Checked = a.Italic; break; } } } else { string target = GetSelectedFileName(); foreach (var a in Additions) { if (target.StartsWith(a.Name, StringComparison.Ordinal)) { textBoxText.Text = a.Text; break; } } } } }
private void TrainLetter(ref int numberOfCharactersLearned, ref int numberOfCharactersSkipped, NOcrDb nOcrD, string s, bool bold, bool italic, bool doubleLetter) { var bmp = GenerateImageFromTextWithStyle("H " + s, bold, italic); var nikseBitmap = new NikseBitmap(bmp); nikseBitmap.MakeTwoColor(280); nikseBitmap.CropTop(0, Color.FromArgb(0, 0, 0, 0)); var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nikseBitmap, 10, false, false, 25, false); if (list.Count == 3) { var item = list[2]; var match = nOcrD.GetMatch(item.NikseBitmap, item.Top, false, 25); if (match == null || match.Text != s) { labelInfo.Refresh(); Application.DoEvents(); var nOcrChar = new NOcrChar(s) { Width = item.NikseBitmap.Width, Height = item.NikseBitmap.Height, MarginTop = item.Top, Italic = italic, }; VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value + (doubleLetter ? 20 : 0), false, nOcrChar, item.NikseBitmap); nOcrD.Add(nOcrChar); numberOfCharactersLearned++; labelInfo.Text = string.Format(Configuration.Settings.Language.VobSubOcr.NowTraining, numberOfCharactersLearned, _subtitleFontName, numberOfCharactersSkipped); bmp.Dispose(); } else { numberOfCharactersSkipped++; } } else if (!doubleLetter) { if (list.Count == 4 && list[2].NikseBitmap != null && list[3].NikseBitmap != null) { // e.g. quote (") var expandItem = VobSubOcr.GetExpandedSelectionNew(nikseBitmap, new List <ImageSplitterItem> { list[2], list[3] }); var match = nOcrD.GetMatchExpanded(nikseBitmap, expandItem, 2, list); if (match != null && match.Text == s) { numberOfCharactersSkipped++; return; } var nOcrChar = new NOcrChar(s) { Width = expandItem.NikseBitmap.Width, Height = expandItem.NikseBitmap.Height, MarginTop = expandItem.Top, Italic = italic, ExpandCount = 2, }; VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value + 5, false, nOcrChar, expandItem.NikseBitmap); nOcrD.Add(nOcrChar); return; } if (list.Count == 5 && list[2].NikseBitmap != null && list[3].NikseBitmap != null && list[4].NikseBitmap != null) { // e.g. "%" var expandItem = VobSubOcr.GetExpandedSelectionNew(nikseBitmap, new List <ImageSplitterItem> { list[2], list[3], list[4] }); var match = nOcrD.GetMatchExpanded(nikseBitmap, expandItem, 2, list); if (match != null && match.Text == s) { numberOfCharactersSkipped++; return; } var nOcrChar = new NOcrChar(s) { Width = expandItem.NikseBitmap.Width, Height = expandItem.NikseBitmap.Height, MarginTop = expandItem.Top, Italic = italic, ExpandCount = 3, }; nOcrD.Add(nOcrChar); VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value + 10, false, nOcrChar, expandItem.NikseBitmap); return; } numberOfCharactersSkipped++; } }
private static void ConvertVobSubSubtitle(string fileName, string toFormat, string offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IList<SubtitleFormat> formats, bool overwrite, string pacCodePage, double? targetFrameRate) { var format = Utilities.GetSubtitleFormatByFriendlyName(toFormat) ?? new SubRip(); Console.WriteLine("Loading subtitles from file \"{0}\"", fileName); Subtitle sub; using (var vobSubOcr = new VobSubOcr()) { Console.WriteLine("Using OCR to extract subtitles"); vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr); sub = vobSubOcr.SubtitleFromOcr; Console.WriteLine("Extracted subtitles from file \"{0}\"", fileName); } if (sub != null) { Console.WriteLine("Converted subtitle"); BatchConvertSave(toFormat, offset, targetEncoding, outputFolder, count, ref converted, ref errors, formats, fileName, sub, format, overwrite, pacCodePage, targetFrameRate); } }
private void numericUpDown1_ValueChanged(object sender, EventArgs e) { pictureBoxSubtitleImage.Image = VobSubOcr.UnItalic(_bmp, (double)numericUpDown1.Value); }
private void buttonAddBetterMatch_Click(object sender, EventArgs e) { var expandSelectionList = new List <ImageSplitterItem>(); if (listBoxInspectItems.SelectedIndex < 0) { return; } int index = _indexLookup[listBoxInspectItems.SelectedIndex]; var img = _imageList[index]; if (img.NikseBitmap == null) { return; } var match = _matchList[listBoxInspectItems.SelectedIndex]; if (match?.ExpandCount > 1) // expand match { addBetterMultiMatchToolStripMenuItem_Click(null, null); return; } using (var vobSubOcrNOcrCharacter = new VobSubOcrNOcrCharacter()) { var text = textBoxText.Text; vobSubOcrNOcrCharacter.Initialize(_bitmap, img, new Point(-1, -1), checkBoxItalic.Checked, true, expandSelectionList.Count > 1, text); DialogResult result = vobSubOcrNOcrCharacter.ShowDialog(this); var expandSelection = false; var shrinkSelection = false; if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ExpandSelection) { expandSelection = true; if (img.NikseBitmap != null) { expandSelectionList.Add(img); } } while (result == DialogResult.OK && (vobSubOcrNOcrCharacter.ShrinkSelection || vobSubOcrNOcrCharacter.ExpandSelection)) { if (expandSelection || shrinkSelection) { expandSelection = false; if (shrinkSelection && index > 0) { shrinkSelection = false; } else if (index + 1 < _imageList.Count && _imageList[index + 1].NikseBitmap != null) // only allow expand to EndOfLine or space { index++; expandSelectionList.Add(_imageList[index]); } img = VobSubOcr.GetExpandedSelectionNew(new NikseBitmap(_bitmap), expandSelectionList); // true } vobSubOcrNOcrCharacter.Initialize(_bitmap, img, new Point(0, 0), checkBoxItalic.Checked, true, expandSelectionList.Count > 1, string.Empty); result = vobSubOcrNOcrCharacter.ShowDialog(this); if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ShrinkSelection) { shrinkSelection = true; index--; if (expandSelectionList.Count > 0) { expandSelectionList.RemoveAt(expandSelectionList.Count - 1); } } else if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ExpandSelection) { expandSelection = true; if (_imageList[index + 1].NikseBitmap != null) { index++; expandSelectionList.Add(_imageList[index]); } } } if (result == DialogResult.OK) { if (expandSelectionList.Count > 1) { vobSubOcrNOcrCharacter.NOcrChar.ExpandCount = expandSelectionList.Count; } _nOcrChars.Add(vobSubOcrNOcrCharacter.NOcrChar); DialogResult = DialogResult.OK; } } }