From 5475392c3f27da1445cfc713b5e94d9a2a39211e Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Fri, 25 Feb 2022 13:08:03 -0700 Subject: [PATCH] Covert to black and white --- .../FileHandlers/DEP08SIASM/FileRead.cs | 2 - .../FileHandlers/DownloadJpegFile/FileRead.cs | 2 - Adaptation/FileHandlers/jpeg/ProcessData.cs | 70 ++++++++++++------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/Adaptation/FileHandlers/DEP08SIASM/FileRead.cs b/Adaptation/FileHandlers/DEP08SIASM/FileRead.cs index 34b261e..185da83 100644 --- a/Adaptation/FileHandlers/DEP08SIASM/FileRead.cs +++ b/Adaptation/FileHandlers/DEP08SIASM/FileRead.cs @@ -117,8 +117,6 @@ public class FileRead : Shared.FileRead, IFileRead throw new Exception(); } - void IFileRead.Callback(object state) => throw new Exception(string.Concat("Not ", nameof(_IsDummy))); - protected static List GetDescriptions(JsonElement[] jsonElements) { List results = new(); diff --git a/Adaptation/FileHandlers/DownloadJpegFile/FileRead.cs b/Adaptation/FileHandlers/DownloadJpegFile/FileRead.cs index 11e5f50..bc5a31d 100644 --- a/Adaptation/FileHandlers/DownloadJpegFile/FileRead.cs +++ b/Adaptation/FileHandlers/DownloadJpegFile/FileRead.cs @@ -108,8 +108,6 @@ public class FileRead : Shared.FileRead, IFileRead void IFileRead.CheckTests(Test[] tests, bool extra) => throw new Exception(string.Concat("Not ", nameof(_IsDuplicator))); - void IFileRead.Callback(object state) => Callback(state); - private Tuple> GetExtractResult(string reportFullPath, DateTime dateTime) { if (reportFullPath is null) diff --git a/Adaptation/FileHandlers/jpeg/ProcessData.cs b/Adaptation/FileHandlers/jpeg/ProcessData.cs index 9337713..a21ef15 100644 --- a/Adaptation/FileHandlers/jpeg/ProcessData.cs +++ b/Adaptation/FileHandlers/jpeg/ProcessData.cs @@ -105,41 +105,56 @@ public class ProcessData : IProcessData #pragma warning disable CA1416 - private static (byte[], Color[]) Get(IFileRead fileRead, int endY, int endX, int startY, int startX, int outputQuality, string saveFileName) + private static (MemoryStream memoryStream, Color[]) Get(IFileRead fileRead, int thresHold, int endY, int endX, int startY, int startX) { - byte[] bytes; + Color color; List colors = new(); - Point startPoint = new(startX, startY); - using MemoryStream memoryStream = new(); - EncoderParameters encoderParameters = new(1); - System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Jpeg; - ImageCodecInfo imageCodecInfo = (from l in ImageCodecInfo.GetImageEncoders() where l.FormatID == imageFormat.Guid select l).First(); - encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, outputQuality); - Rectangle rectangle = new(startPoint, new Size(endX - startX, endY - startY)); + MemoryStream memoryStream = new(); + int middle = (int)(endY - startY * .5); + Bitmap selectedBitmap = new(endX - startX, endY - startY); using Bitmap bitmap = Image.FromFile(fileRead.ReportFullPath) as Bitmap; - using Bitmap clonedBitmap = bitmap.Clone(rectangle, bitmap.PixelFormat); - if (!string.IsNullOrEmpty(saveFileName)) + System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Png; + for (int x = startX; x < endX; x++) { - if (!saveFileName.EndsWith(".png")) - throw new NotImplementedException("Configured to save as *.png"); - clonedBitmap.Save(saveFileName, System.Drawing.Imaging.ImageFormat.Png); - for (int i = 0; i < rectangle.Width; i++) - colors.Add(clonedBitmap.GetPixel(i, (int)(rectangle.Height * .5))); + for (int y = startY; y < endY; y++) + { + color = bitmap.GetPixel(x, y); + if (y == middle) + colors.Add(color); + if (color.R > thresHold || color.G > thresHold || color.B > thresHold) + selectedBitmap.SetPixel(x - startX, y - startY, Color.Black); + } } - clonedBitmap.Save(memoryStream, imageCodecInfo, encoderParameters); - bytes = memoryStream.GetBuffer(); - return new(bytes, colors.ToArray()); + selectedBitmap.Save(memoryStream, imageFormat); + return new(memoryStream, colors.ToArray()); + } + + private static void SaveToFile(MemoryStream memoryStream, string extension, string saveFileName) + { + System.Drawing.Imaging.ImageFormat imageFormat = extension switch + { + ".bmp" => System.Drawing.Imaging.ImageFormat.Bmp, + ".gif" => System.Drawing.Imaging.ImageFormat.Gif, + ".jpeg" => System.Drawing.Imaging.ImageFormat.Jpeg, + ".jpg" => System.Drawing.Imaging.ImageFormat.Jpeg, + ".png" => System.Drawing.Imaging.ImageFormat.Png, + ".tiff" => System.Drawing.Imaging.ImageFormat.Tiff, + _ => throw new Exception("Extension not mapped"), + }; + using Bitmap bitmap = new(memoryStream); + bitmap.Save(saveFileName, imageFormat); } #pragma warning restore CA1416 - private static (byte[], Color[]) Get(IFileRead fileRead, string saveFileName) => Get(fileRead, 68, 1559, 32, 1094, 95, saveFileName); + private static (MemoryStream, Color[]) Get(IFileRead fileRead, int thresHold) => Get(fileRead, thresHold, 68, 1559, 32, 1094); private void Parse(IFileRead fileRead, List fileInfoCollection) { + int thresHold = 76; + (MemoryStream memoryStream, Color[] colors) = Get(fileRead, thresHold); + byte[] bytes = memoryStream.GetBuffer(); using TesseractEngine engine = new(string.Empty, "eng", EngineMode.Default); - //using Pix img = Pix.LoadFromFile(fileRead.ReportFullPath); - (byte[] bytes, Color[] colors) = Get(fileRead, saveFileName: string.Empty); using Pix img = Pix.LoadFromMemory(bytes); using Page page = engine.Process(img); string text = page.GetText().Trim(); @@ -150,13 +165,14 @@ public class ProcessData : IProcessData int red = 0; int green = 0; _Log.Debug("Looking by color"); - string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, ".png"); - (bytes, colors) = Get(fileRead, saveFileName); + string extension = ".png"; + string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, extension); + SaveToFile(memoryStream, extension, saveFileName); foreach (Color color in colors) { - if (color.R > 127) + if (color.R > thresHold) red += 1; - if (color.G > 127) + if (color.G > thresHold) green += 1; } if (red > green) @@ -165,6 +181,8 @@ public class ProcessData : IProcessData text = "Green*"; fileInfoCollection.Add(new FileInfo(saveFileName)); } + if (memoryStream is not null) + memoryStream.Dispose(); _Details.Add(text); }