From 6f38ad9d655d4e6eb88f0f2b89905efd816b0c10 Mon Sep 17 00:00:00 2001 From: niksedk Date: Wed, 12 Jan 2011 06:22:45 +0000 Subject: [PATCH] Ocr via image-compare can now expand selection to the left (for right-to-left languages) git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@242 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Forms/VobSubOcr.cs | 50 ++++++++++++++++++++++++++++++------------ src/Logic/Settings.cs | 6 +++++ 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/Forms/VobSubOcr.cs b/src/Forms/VobSubOcr.cs index 0cfdd75fb..03063d4a6 100644 --- a/src/Forms/VobSubOcr.cs +++ b/src/Forms/VobSubOcr.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Drawing; using System.IO; using System.Reflection; @@ -10,7 +11,6 @@ using System.Xml; using Nikse.SubtitleEdit.Logic; using Nikse.SubtitleEdit.Logic.OCR; using Nikse.SubtitleEdit.Logic.VobSub; -using System.Diagnostics; namespace Nikse.SubtitleEdit.Forms { @@ -163,6 +163,7 @@ namespace Nikse.SubtitleEdit.Forms italicToolStripMenuItem.Text = Configuration.Settings.Language.General.Italic; saveImageAsToolStripMenuItem.Text = language.SaveSubtitleImageAs; saveAllImagesToolStripMenuItem.Text = language.SaveAllSubtitleImagesAs; + checkBoxRightToLeft.Checked = Configuration.Settings.VobSubOcr.RightToLeft; comboBoxTesseractLanguages.Left = labelTesseractLanguage.Left + labelTesseractLanguage.Width; @@ -764,6 +765,7 @@ namespace Nikse.SubtitleEdit.Forms } else if (index+1 < list.Count && list[index+1].Bitmap != null) // only allow expand to EndOfLine or space { + index++; expandSelectionList.Add(list[index]); } @@ -860,21 +862,40 @@ namespace Nikse.SubtitleEdit.Forms return line; } - private static ImageSplitterItem GetExpandedSelection(Bitmap bitmap, List expandSelectionList) + private ImageSplitterItem GetExpandedSelection(Bitmap bitmap, List expandSelectionList) { - int minimumX = expandSelectionList[0].X; - int maximumX = expandSelectionList[expandSelectionList.Count - 1].X + expandSelectionList[expandSelectionList.Count - 1].Bitmap.Width; - int minimumY = expandSelectionList[0].Y; - int maximumY = expandSelectionList[0].Y + expandSelectionList[0].Bitmap.Height; - foreach (ImageSplitterItem item in expandSelectionList) + if (checkBoxRightToLeft.Checked) { - if (item.Y < minimumY) - minimumY = item.Y; - if (item.Y + item.Bitmap.Height > maximumY) - maximumY = item.Y + item.Bitmap.Height; + int minimumX = expandSelectionList[expandSelectionList.Count - 1].X - expandSelectionList[expandSelectionList.Count - 1].Bitmap.Width; + int maximumX = expandSelectionList[0].X; + int minimumY = expandSelectionList[0].Y; + int maximumY = expandSelectionList[0].Y + expandSelectionList[0].Bitmap.Height; + foreach (ImageSplitterItem item in expandSelectionList) + { + if (item.Y < minimumY) + minimumY = item.Y; + if (item.Y + item.Bitmap.Height > maximumY) + maximumY = item.Y + item.Bitmap.Height; + } + Bitmap part = ImageSplitter.Copy(bitmap, new Rectangle(minimumX, minimumY, maximumX - minimumX, maximumY - minimumY)); + return new ImageSplitterItem(minimumX, minimumY, part); + } + else + { + int minimumX = expandSelectionList[0].X; + int maximumX = expandSelectionList[expandSelectionList.Count - 1].X + expandSelectionList[expandSelectionList.Count - 1].Bitmap.Width; + int minimumY = expandSelectionList[0].Y; + int maximumY = expandSelectionList[0].Y + expandSelectionList[0].Bitmap.Height; + foreach (ImageSplitterItem item in expandSelectionList) + { + if (item.Y < minimumY) + minimumY = item.Y; + if (item.Y + item.Bitmap.Height > maximumY) + maximumY = item.Y + item.Bitmap.Height; + } + Bitmap part = ImageSplitter.Copy(bitmap, new Rectangle(minimumX, minimumY, maximumX - minimumX, maximumY - minimumY)); + return new ImageSplitterItem(minimumX, minimumY, part); } - Bitmap part = ImageSplitter.Copy(bitmap, new Rectangle(minimumX, minimumY, maximumX - minimumX, maximumY - minimumY)); - return new ImageSplitterItem(minimumX, minimumY, part); } private static string GetStringWithItalicTags(List matches) @@ -1064,6 +1085,7 @@ namespace Nikse.SubtitleEdit.Forms private void ButtonStartOcrClick(object sender, EventArgs e) { + Configuration.Settings.VobSubOcr.RightToLeft = checkBoxRightToLeft.Checked; _lastLine = null; buttonOK.Enabled = false; buttonCancel.Enabled = false; @@ -1130,7 +1152,7 @@ namespace Nikse.SubtitleEdit.Forms p.Text = text; textBoxCurrentText.Text = text; } - SetButtonsEnabledAfterOcrDone(); + SetButtonsEnabledAfterOcrDone(); } private Bitmap ResizeBitmap(Bitmap b, int width, int height) diff --git a/src/Logic/Settings.cs b/src/Logic/Settings.cs index 55247dba7..c3f678931 100644 --- a/src/Logic/Settings.cs +++ b/src/Logic/Settings.cs @@ -321,6 +321,7 @@ namespace Nikse.SubtitleEdit.Logic public int LastModiLanguageId { get; set; } public string LastOcrMethod { get; set; } public string TesseractLastLanguage { get; set; } + public bool RightToLeft { get; set; } public VobSubOcrSettings() { @@ -329,6 +330,7 @@ namespace Nikse.SubtitleEdit.Logic LastImageCompareFolder = "English"; LastModiLanguageId = 9; LastOcrMethod = "Tesseract"; + RightToLeft = false; } } @@ -838,6 +840,9 @@ namespace Nikse.SubtitleEdit.Logic subNode = node.SelectSingleNode("TesseractLastLanguage"); if (subNode != null) settings.VobSubOcr.TesseractLastLanguage = subNode.InnerText; + subNode = node.SelectSingleNode("RightToLeft"); + if (subNode != null) + settings.VobSubOcr.RightToLeft = Convert.ToBoolean(subNode.InnerText); foreach (XmlNode listNode in doc.DocumentElement.SelectNodes("MultipleSearchAndReplaceList/MultipleSearchAndReplaceItem")) { @@ -1012,6 +1017,7 @@ namespace Nikse.SubtitleEdit.Logic textWriter.WriteElementString("LastModiLanguageId", settings.VobSubOcr.LastModiLanguageId.ToString()); textWriter.WriteElementString("LastOcrMethod", settings.VobSubOcr.LastOcrMethod); textWriter.WriteElementString("TesseractLastLanguage", settings.VobSubOcr.TesseractLastLanguage); + textWriter.WriteElementString("RightToLeft", settings.VobSubOcr.RightToLeft.ToString()); textWriter.WriteEndElement(); textWriter.WriteStartElement("MultipleSearchAndReplaceList", "");