From 507da455bf3ed86c582de6e7bfe82078a63784dd Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Tue, 21 Jul 2020 20:35:25 +0100 Subject: [PATCH] add grey scale functionality to RGBASpec image type. The code can now convert the image into grey scale and add a grid on top of it when using the RGBASpec. The commands in the console program can now pass on the grey scale variable to. The Skia Sharp functions still need this functionality added it. --- DeathSocket/GridPainter.fs | 4 ++-- DeathSocket/ImageServices.fs | 24 +++++++++++++++++++++--- DeathSocketCLI/Commands.fs | 8 ++++---- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index 7b86272..d0e7264 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -50,10 +50,10 @@ namespace DeathSocket | false -> drawRGBAGrid r | Skia s -> validateIO s.originalPath s.savePath |> ignore - drawSkiaGrid s // greyScale + drawSkiaGrid s // greyScale not implemented | SkiaRGB sR -> validateIO sR.originalPath sR.savePath |> ignore - drawSkiaRGBGrid sR // greyScale + drawSkiaRGBGrid sR // greyScale not implemented | _ -> printfn "[DEATH SOCKET INFO] Inappropriate ImageSpec used here. Please use none buffer-based spec's when using this function." with | :? FileNotFoundException as ex -> diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index 78c234b..aacd528 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -255,12 +255,30 @@ clone.Save (spec.savePath) let drawRGBAGridGrey (spec: RGBASpec) = - // THIS NEEDS REFACTORING TO CONVERT IMAGE TO GREYSCALE. + // This part converts the images to greyscale. 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 clone = new Bitmap(temp.Width, temp.Height) + use g = Graphics.FromImage(clone) + + // This can be refactored out into its own function or reachable from + // anywhere in this module. It's used more than once and doesn't use 'use'. + 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) // Maybe put in own function? + 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 graphics = Graphics.FromImage(clone) + + // This part applies the grid. use pen = new Pen ((makeBrushFromRGBASpec spec), width = spec.penWidth) graphics.DrawImage (original,new Rectangle(0, 0, clone.Width, clone.Height)) let horizontalLines = diff --git a/DeathSocketCLI/Commands.fs b/DeathSocketCLI/Commands.fs index ba02549..9c1b4d9 100644 --- a/DeathSocketCLI/Commands.fs +++ b/DeathSocketCLI/Commands.fs @@ -51,10 +51,10 @@ [] [] + "(pen-width: float32) (colour: string) (new-path: string) (greyScale: bool)")>] [] [] + "add-grid C:/orignal-image.png 10 5 2 red C:/new-image.png true">] let ``add-grid`` imgPath numRows numColumns pWidth colour newPath greyScale = try printfn "[INFO.] Adding grid to image..." @@ -112,13 +112,13 @@ 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 greyScale + |> applyGridToImageAsync greyScale // Not implement in this context. |> Async.Start showEndOfCommandMessage 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 greyScale + |> applyGridToImageAsync greyScale // Not implement in this context. |> Async.Start showEndOfCommandMessage \ No newline at end of file