From e3cd2ac9ef04d628dab6a27d32c6c92b873981c3 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sat, 23 May 2020 08:20:50 +0200 Subject: [PATCH] Optimize BIC (minor) --- src/Logic/Ocr/Binary/BinaryOcrBitmap.cs | 60 +++++++------------------ src/Logic/Ocr/Binary/BinaryOcrDb.cs | 7 +-- 2 files changed, 21 insertions(+), 46 deletions(-) diff --git a/src/Logic/Ocr/Binary/BinaryOcrBitmap.cs b/src/Logic/Ocr/Binary/BinaryOcrBitmap.cs index 0eea4e28b..4eccfe204 100644 --- a/src/Logic/Ocr/Binary/BinaryOcrBitmap.cs +++ b/src/Logic/Ocr/Binary/BinaryOcrBitmap.cs @@ -53,7 +53,7 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary Height = height; _colors = new byte[Width * Height]; Hash = MurMurHash3.Hash(_colors); - CalcuateNumberOfColoredPixels(); + NumberOfColoredPixels = 0; } public BinaryOcrBitmap(Stream stream) @@ -113,27 +113,25 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary Width = nbmp.Width; Height = nbmp.Height; _colors = new byte[Width * Height]; + var numberOfColoredPixels = 0; for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { - SetPixelViaAlpha(x, y, nbmp.GetAlpha(x, y)); + var alpha = nbmp.GetAlpha(x, y); + if (alpha < 100) + { + _colors[Width * y + x] = 0; + } + else + { + _colors[Width * y + x] = 1; + numberOfColoredPixels++; + } } } + NumberOfColoredPixels = numberOfColoredPixels; Hash = MurMurHash3.Hash(_colors); - CalcuateNumberOfColoredPixels(); - } - - private void CalcuateNumberOfColoredPixels() - { - NumberOfColoredPixels = 0; - for (int i = 0; i < _colors.Length; i++) - { - if (_colors[i] > 0) - { - NumberOfColoredPixels++; - } - } } public bool AreColorsEqual(BinaryOcrBitmap other) @@ -216,33 +214,9 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary return _colors[index]; } - public void SetPixel(int x, int y, int c) + public void SetPixel(int x, int y) { - _colors[Width * y + x] = (byte)c; - } - - public void SetPixel(int x, int y, Color c) - { - if (c.A < 100) - { - _colors[Width * y + x] = 0; - } - else - { - _colors[Width * y + x] = 1; - } - } - - public void SetPixelViaAlpha(int x, int y, int alpha) - { - if (alpha < 100) - { - _colors[Width * y + x] = 0; - } - else - { - _colors[Width * y + x] = 1; - } + _colors[Width * y + x] = 1; } /// @@ -322,13 +296,13 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary int c = bob.GetPixel(x, y); if (c > 0) { - nbmp.SetPixel(bob.X - minX + x, bob.Y - minY + y, 1); + nbmp.SetPixel(bob.X - minX + x, bob.Y - minY + y); } } } } - return nbmp.ToOldBitmap(color); // Resursive + return nbmp.ToOldBitmap(color); // Recursive } else { diff --git a/src/Logic/Ocr/Binary/BinaryOcrDb.cs b/src/Logic/Ocr/Binary/BinaryOcrDb.cs index 581f284c6..d56e3f004 100644 --- a/src/Logic/Ocr/Binary/BinaryOcrDb.cs +++ b/src/Logic/Ocr/Binary/BinaryOcrDb.cs @@ -1,9 +1,9 @@ -using System; +using Nikse.SubtitleEdit.Core; +using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; -using Nikse.SubtitleEdit.Core; namespace Nikse.SubtitleEdit.Logic.Ocr.Binary { @@ -135,10 +135,11 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary public int FindExactMatch(BinaryOcrBitmap bob) { + var bobHash = bob.Hash; for (int i = 0; i < CompareImages.Count; i++) { var b = CompareImages[i]; - if (bob.Hash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels) + if (bobHash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels) { if (AllowEqual(b, bob)) {