From b7cd42132348073be3a6b6b9b0b4a3b25a4fd80d Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Tue, 21 Jul 2020 19:48:50 +0100 Subject: [PATCH] implement rough initial version of convert to greyscale. The code which used the Brush spec type can now convert the image to greyscale and save the image with the grid still in the colour the end-user selected. This code is very rough and will need cleaning up. --- DeathSocket/DeathSocket.fsproj | 4 +- DeathSocket/GridPainter.fs | 4 +- DeathSocket/ImageServices.fs | 73 ++++++++++++++++++++++++++++------ DeathSocketCLI/Commands.fs | 20 +++++----- TestCentre/LibraryTests.fs | 8 ++-- 5 files changed, 78 insertions(+), 31 deletions(-) diff --git a/DeathSocket/DeathSocket.fsproj b/DeathSocket/DeathSocket.fsproj index fe88a24..1e8d801 100644 --- a/DeathSocket/DeathSocket.fsproj +++ b/DeathSocket/DeathSocket.fsproj @@ -32,7 +32,7 @@ - + True @@ -46,7 +46,7 @@ - + diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index a155da0..7509ba4 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -34,13 +34,13 @@ namespace DeathSocket /// is not in use or needed by another program/process. /// This is because it is locked whilst in this function. /// - let applyGridToImageAsync (spec: ImageSpec) = + let applyGridToImageAsync (greyScale: bool) (spec: ImageSpec) = async { try match spec with | Brush b -> validateIO b.originalPath b.savePath |> ignore - drawBrushSpecGrid b + drawBrushSpecGrid b greyScale | RGBA r -> validateIO r.originalPath r.savePath |> ignore drawRGBAGrid r diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index 9949829..d55341c 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -191,21 +191,68 @@ assume any function with a "*Spec" type as a parameter will use this "temp" file. *) - let drawBrushSpecGrid (spec: BrushSpec) = + let drawBrushSpecGrid (spec: BrushSpec) (greyScale: bool) = use original = Bitmap.FromFile spec.originalPath use temp = new Bitmap(original) - use clone = - temp.Clone(new Rectangle(0, 0, temp.Width, temp.Height), PixelFormat.Format32bppArgb) - use graphics = Graphics.FromImage(clone) - use pen = new Pen (spec.colour, width = spec.penWidth) - graphics.DrawImage(original,new Rectangle(0, 0, clone.Width, clone.Height)) - let horizontalLines = - createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows) - let verticalLines = - createVerticalLines (clone.Size.Width) (clone.Size.Height) (spec.columns) - for line in horizontalLines do graphics.DrawLines (pen, line) - for line in verticalLines do graphics.DrawLines (pen, line) - clone.Save (spec.savePath) + //use clone = + // temp.Clone(new Rectangle(0, 0, temp.Width, temp.Height), PixelFormat.Format32bppArgb) + + // The orignial is above... + + if greyScale = true then + + use temp1 = new Bitmap(original.Width, original.Height) + use g = Graphics.FromImage(temp1) + let aM: float32[][] = [| + [| (float32 0.3); (float32 0.3); (float32 0.3); (float32 0.0); (float32 0.0) |]; + [| (float32 0.59); (float32 0.59); (float32 0.59); (float32 0.0); (float32 0.0) |]; + [| (float32 0.11); (float32 0.11); (float32 0.11); (float32 0.0); (float32 0.0) |]; + [| (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0); (float32 0.0) |]; + [| (float32 0.0); (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0) |]; + |] + + let colourMatrix = new ColorMatrix(aM) + use attributes = new ImageAttributes() + attributes.SetColorMatrix(colourMatrix) + g.DrawImage(temp, new Rectangle(0, 0, temp.Width, temp.Height), + 0, 0, temp.Width, temp.Height, GraphicsUnit.Pixel, attributes) + + // use clone = temp1 + use graphics = Graphics.FromImage(temp1) + use pen = new Pen (spec.colour, width = spec.penWidth) + graphics.DrawImage(temp1 ,new Rectangle(0, 0, temp1.Width, temp1.Height)) + let horizontalLines = + createHorizontalLines (temp1.Size.Width) (temp1.Size.Height) (spec.rows) + let verticalLines = + createVerticalLines (temp1.Size.Width) (temp1.Size.Height) (spec.columns) + for line in horizontalLines do graphics.DrawLines (pen, line) + for line in verticalLines do graphics.DrawLines (pen, line) + temp1.Save (spec.savePath) + else + use clone = temp.Clone(new Rectangle(0, 0, temp.Width, temp.Height), PixelFormat.Format32bppArgb) + use graphics = Graphics.FromImage(clone) + use pen = new Pen (spec.colour, width = spec.penWidth) + graphics.DrawImage(original,new Rectangle(0, 0, clone.Width, clone.Height)) + let horizontalLines = + createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows) + let verticalLines = + createVerticalLines (clone.Size.Width) (clone.Size.Height) (spec.columns) + for line in horizontalLines do graphics.DrawLines (pen, line) + for line in verticalLines do graphics.DrawLines (pen, line) + clone.Save (spec.savePath) + + // The original is below... + + //use graphics = Graphics.FromImage(clone) + //use pen = new Pen (spec.colour, width = spec.penWidth) + //graphics.DrawImage(original,new Rectangle(0, 0, clone.Width, clone.Height)) + //let horizontalLines = + // createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows) + //let verticalLines = + // createVerticalLines (clone.Size.Width) (clone.Size.Height) (spec.columns) + //for line in horizontalLines do graphics.DrawLines (pen, line) + //for line in verticalLines do graphics.DrawLines (pen, line) + //clone.Save (spec.savePath) let drawRGBAGrid (spec: RGBASpec) = use original = Bitmap.FromFile spec.originalPath diff --git a/DeathSocketCLI/Commands.fs b/DeathSocketCLI/Commands.fs index e924dbb..17ebe66 100644 --- a/DeathSocketCLI/Commands.fs +++ b/DeathSocketCLI/Commands.fs @@ -32,15 +32,15 @@ let exit () = Environment.Exit (Environment.ExitCode) [] - [] + [] [] - [] - let ``add-default`` imgPath newPath = + [] + let ``add-default`` imgPath newPath greyScale= try printfn "[INFO.] Adding default grid to image..." Brush (buildDefaultSpec imgPath newPath) - |> applyGridToImageAsync + |> applyGridToImageAsync greyScale |> Async.Start showEndOfCommandMessage with @@ -55,11 +55,11 @@ [] [] - let ``add-grid`` imgPath numRows numColumns pWidth colour newPath = + let ``add-grid`` imgPath numRows numColumns pWidth colour newPath greyScale= try printfn "[INFO.] Adding grid to image..." Brush (buildSpec imgPath numRows numColumns pWidth colour newPath) - |> applyGridToImageAsync + |> applyGridToImageAsync greyScale |> Async.Start showEndOfCommandMessage with @@ -109,16 +109,16 @@ printfn "%s" item.Key showEndOfCommandMessage - let``add-skia-grid`` imgPath numRows numColumns pWidth colour newPath = + let``add-skia-grid`` imgPath numRows numColumns pWidth colour newPath greyScale= printfn "[INFO.] Adding SkiaSharp grid to image..." Skia (buildSkiaSpec imgPath numRows numColumns pWidth colour newPath) - |> applyGridToImageAsync + |> applyGridToImageAsync greyScale |> Async.Start showEndOfCommandMessage - let``add-skia-rgb-grid`` imgPath numRows numColumns pWidth r g b newPath = + let``add-skia-rgb-grid`` imgPath numRows numColumns pWidth r g b newPath greyScale = printfn "[INFO.] Adding SkiaSharp grid to image..." SkiaRGB (buildSkiaRGBSpec imgPath numRows numColumns pWidth r g b newPath) - |> applyGridToImageAsync + |> applyGridToImageAsync greyScale |> Async.Start showEndOfCommandMessage \ No newline at end of file diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index 17abe0c..019743b 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -134,7 +134,7 @@ penWidth = float32 (newPenWidth()) rows = 10 columns = 10 }) - |> applyGridToImageAsync + |> applyGridToImageAsync true // true is temp. |> Async.RunSynchronously (File.Exists sPath) = true @@ -152,7 +152,7 @@ penWidth = float32 (newPenWidth()) rows = 10 columns = 10 }) - |> applyGridToImageAsync + |> applyGridToImageAsync true // true is temp. |> Async.RunSynchronously (File.Exists sPath) = true @@ -199,7 +199,7 @@ penWidth = float32 (newPenWidth()) rows = newNum () columns = newNum () }) - |> applyGridToImageAsync + |> applyGridToImageAsync true // true is temp. |> Async.RunSynchronously (File.Exists sPath) = true @@ -217,7 +217,7 @@ penWidth = float32 (newPenWidth()) rows = newNum () columns = newNum () }) - |> applyGridToImageAsync + |> applyGridToImageAsync true // true is temp. |> Async.RunSynchronously (File.Exists sPath) = true