From d098f126915e1c2596b2b2e0312553428e820c7b Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 20 Sep 2018 22:20:54 +0100 Subject: [PATCH 01/12] add listAllColours function to lib and rename applyGrid to async. --- DeathSocket/GridPainter.fs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index 088c846..c87cb28 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -6,12 +6,22 @@ namespace DeathSocket open Validation open ImageServices + open System.Drawing + open System.Reflection - let applyGrid spec = + let applyGridAsync spec = async { try validateFilePath |> ignore drawGrid spec |> ignore with - | :? FileNotFoundException as ex -> printfn "File could not be found at %s" ex.Message - } \ No newline at end of file + | :? FileNotFoundException as ex -> + printfn "File could not be found at %s" ex.Message + } + + let ListAllColours = + let properties = + typeof.GetProperties + (BindingFlags.Public ||| BindingFlags.Static) + seq { for prop in properties -> prop } + |> Seq.toList \ No newline at end of file From 78912be35be4b375cd8bd334daa19cbb64ae1cdf Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 20 Sep 2018 22:22:21 +0100 Subject: [PATCH 02/12] leave comments in GridPainter about testing and XML comments. --- DeathSocket/GridPainter.fs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index c87cb28..c88f021 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -9,6 +9,7 @@ namespace DeathSocket open System.Drawing open System.Reflection + // Needs XML comments. let applyGridAsync spec = async { try @@ -19,6 +20,8 @@ namespace DeathSocket printfn "File could not be found at %s" ex.Message } + // Needs XML comments. + // Tests have not been written. let ListAllColours = let properties = typeof.GetProperties From d05e8301511d13fab6751c884135aae2afbb0280 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Sat, 22 Sep 2018 17:43:01 +0100 Subject: [PATCH 03/12] change the column count variable to row count in createHorizontalLines. Apply the applyGrid (async) name change to the CLI and Test Centre. --- DeathSocket/ImageServices.fs | 2 +- DeathSocketCLI/Commands.fs | 4 ++-- TestCentre/LibraryTests.fs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index ca5830b..51109e4 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -21,7 +21,7 @@ let graphics = Graphics.FromImage img let pen = new Pen (spec.colour, width = spec.penWidth) let horizontalLines = - createHorizontalLines (img.Size.Width) (img.Size.Height) (spec.columns) + createHorizontalLines (img.Size.Width) (img.Size.Height) (spec.rows) let verticalLines = createVerticalLines (img.Size.Width) (img.Size.Height) (spec.columns) for line in horizontalLines do graphics.DrawLines (pen, line) for line in verticalLines do graphics.DrawLines (pen, line) diff --git a/DeathSocketCLI/Commands.fs b/DeathSocketCLI/Commands.fs index aad33ee..40aa03e 100644 --- a/DeathSocketCLI/Commands.fs +++ b/DeathSocketCLI/Commands.fs @@ -38,7 +38,7 @@ try printfn "[INFO.] Adding default grid to image..." buildDefaultSpec imgPath newPath - |> applyGrid + |> applyGridAsync |> Async.RunSynchronously showEndOfCommandMessage with @@ -57,7 +57,7 @@ try printfn "[INFO.] Adding grid to image..." buildSpec imgPath numRows numColumns pWidth colour newPath - |> applyGrid + |> applyGridAsync |> Async.RunSynchronously showEndOfCommandMessage with diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index deaf25a..335dbc2 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -85,7 +85,7 @@ penWidth = float32 1 rows = 10 columns = 10 } - applyGrid spec + applyGridAsync spec |> Async.RunSynchronously (File.Exists sPath) = true From 08af0d1e0d9081971d3de6ba7aa14df1bd673cab Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Sat, 22 Sep 2018 18:47:37 +0100 Subject: [PATCH 04/12] create wrappers for creating horizontal and vertical line functions. Rename the ListAllColours to allSystemBrushes. --- DeathSocket/GridPainter.fs | 12 +++++++++++- DeathSocket/ImageServices.fs | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index c88f021..ae0b7b0 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -22,7 +22,17 @@ namespace DeathSocket // Needs XML comments. // Tests have not been written. - let ListAllColours = + let determineHorizontalLines width height rows = + createHorizontalLines width height rows + + // Needs XML comments. + // Tests have not been written. + let determineVerticalLines width height columns = + createVerticalLines width height columns + + // Needs XML comments. + // Tests have not been written. + let allSystemBrushes = let properties = typeof.GetProperties (BindingFlags.Public ||| BindingFlags.Static) diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index 51109e4..e7ce9ac 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -22,7 +22,8 @@ let pen = new Pen (spec.colour, width = spec.penWidth) let horizontalLines = createHorizontalLines (img.Size.Width) (img.Size.Height) (spec.rows) - let verticalLines = createVerticalLines (img.Size.Width) (img.Size.Height) (spec.columns) + let verticalLines = + createVerticalLines (img.Size.Width) (img.Size.Height) (spec.columns) for line in horizontalLines do graphics.DrawLines (pen, line) for line in verticalLines do graphics.DrawLines (pen, line) img.Save (spec.savePath, ImageFormat.Png) From 2e2fc0d4c83fe2a5c26a172567b229f54110f859 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Mon, 1 Oct 2018 00:19:19 +0100 Subject: [PATCH 05/12] update drawGrid to save images with indexed pixels. --- DeathSocket/ImageServices.fs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index e7ce9ac..8b077ab 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -17,8 +17,11 @@ Point ((interval * point), height)|]|] let drawGrid spec = - let img = Bitmap.FromFile spec.originalPath + // The originalImg is for images with indexed pixels. + let orignialImg = Bitmap.FromFile spec.originalPath + let img = new Bitmap (orignialImg.Width, orignialImg.Height) let graphics = Graphics.FromImage img + graphics.DrawImage (orignialImg, 0, 0) let pen = new Pen (spec.colour, width = spec.penWidth) let horizontalLines = createHorizontalLines (img.Size.Width) (img.Size.Height) (spec.rows) @@ -27,6 +30,7 @@ for line in horizontalLines do graphics.DrawLines (pen, line) for line in verticalLines do graphics.DrawLines (pen, line) img.Save (spec.savePath, ImageFormat.Png) + orignialImg.Dispose () img.Dispose () graphics.Dispose () pen.Dispose () \ No newline at end of file From e77baa242dcddb7dcda12f50c2e8aa278991302a Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Mon, 1 Oct 2018 16:44:33 +0100 Subject: [PATCH 06/12] remove allSystemBrushes from Grid Painter. Update drawGrid to use "use" statements and remove the manual Dispose calls. Make minor spelling/editing chanes (in comments). --- DeathSocket/GridPainter.fs | 15 ++------------- DeathSocket/ImageServices.fs | 14 +++++--------- TestCentre/Script.fsx | 2 +- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index ae0b7b0..b0d8a2d 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -6,8 +6,6 @@ namespace DeathSocket open Validation open ImageServices - open System.Drawing - open System.Reflection // Needs XML comments. let applyGridAsync spec = @@ -17,7 +15,7 @@ namespace DeathSocket drawGrid spec |> ignore with | :? FileNotFoundException as ex -> - printfn "File could not be found at %s" ex.Message + printfn "File could not be found at %s" ex.Message } // Needs XML comments. @@ -28,13 +26,4 @@ namespace DeathSocket // Needs XML comments. // Tests have not been written. let determineVerticalLines width height columns = - createVerticalLines width height columns - - // Needs XML comments. - // Tests have not been written. - let allSystemBrushes = - let properties = - typeof.GetProperties - (BindingFlags.Public ||| BindingFlags.Static) - seq { for prop in properties -> prop } - |> Seq.toList \ No newline at end of file + createVerticalLines width height columns \ No newline at end of file diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index 8b077ab..51b4fa9 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -18,19 +18,15 @@ let drawGrid spec = // The originalImg is for images with indexed pixels. - let orignialImg = Bitmap.FromFile spec.originalPath - let img = new Bitmap (orignialImg.Width, orignialImg.Height) - let graphics = Graphics.FromImage img + use orignialImg = Bitmap.FromFile spec.originalPath + use img = new Bitmap (orignialImg.Width, orignialImg.Height) + use graphics = Graphics.FromImage img graphics.DrawImage (orignialImg, 0, 0) - let pen = new Pen (spec.colour, width = spec.penWidth) + use pen = new Pen (spec.colour, width = spec.penWidth) let horizontalLines = createHorizontalLines (img.Size.Width) (img.Size.Height) (spec.rows) let verticalLines = createVerticalLines (img.Size.Width) (img.Size.Height) (spec.columns) for line in horizontalLines do graphics.DrawLines (pen, line) for line in verticalLines do graphics.DrawLines (pen, line) - img.Save (spec.savePath, ImageFormat.Png) - orignialImg.Dispose () - img.Dispose () - graphics.Dispose () - pen.Dispose () \ No newline at end of file + img.Save (spec.savePath, ImageFormat.Png) \ No newline at end of file diff --git a/TestCentre/Script.fsx b/TestCentre/Script.fsx index 7c24da5..e053c75 100644 --- a/TestCentre/Script.fsx +++ b/TestCentre/Script.fsx @@ -44,7 +44,7 @@ resetLoadingTestArea () (* Populating LoadingTestArea Folder Scripts =============================================================================== -The following scripts are to help you populate a test folder of images. +The following scripts are to help you populate a test folder with test images. You can then use these images in LibraryTests.fs -- Property Tests. The tests consists of loading images from LoadingTestArea, transforming them and saving them in SavingTestArea. *) From 4bccd6bb028915542c4e8aa6d310cec7b5d7c899 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Mon, 1 Oct 2018 16:54:19 +0100 Subject: [PATCH 07/12] add unit tests for determining horizontal and vertical lines in lib. --- TestCentre/LibraryTests.fs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index 335dbc2..34dbfd6 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -93,6 +93,8 @@ open TestingHelpers open Xunit + open DeathSocket + open System (* This test is a precaution (a test for the tests if you will...). It is here to make sure the property test has what it needs to run. @@ -103,4 +105,14 @@ let ``LoadingTestArea contains at least 100 test images`` () = let length = imagesInLoadingTestArea.Length let imagesAreThere = if length < 100 then false else true - Assert.True imagesAreThere \ No newline at end of file + Assert.True imagesAreThere + + [] + let ``Divide By Zero Exception is thrown when 0 rows is used when determining horizontal lines`` () = + let result () = GridPainter.determineHorizontalLines 100 100 0 + Assert.Throws(fun () -> result() |> ignore) + + [] + let ``Divide By Zero Exception is thrown when 0 columns is used when determining vertical lines`` () = + let result () = GridPainter.determineVerticalLines 100 100 0 + Assert.Throws(fun () -> result() |> ignore) \ No newline at end of file From a531d98454fe11507437d486be265d008473f842 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Mon, 1 Oct 2018 17:15:50 +0100 Subject: [PATCH 08/12] add property tests for vertical and horizontal line functions in lib. --- DeathSocket/GridPainter.fs | 2 -- TestCentre/LibraryTests.fs | 28 +++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index b0d8a2d..0dbe187 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -19,11 +19,9 @@ namespace DeathSocket } // Needs XML comments. - // Tests have not been written. let determineHorizontalLines width height rows = createHorizontalLines width height rows // Needs XML comments. - // Tests have not been written. let determineVerticalLines width height columns = createVerticalLines width height columns \ No newline at end of file diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index 34dbfd6..8395d4e 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -21,9 +21,12 @@ open System.Reflection open System.IO + let rand = Random () + + (* These are duplicates from ConsoleTests.fs (both of them). See point about helpers. Tests for checking these locations can be found in - ConsoleTests.fs.*) + ConsoleTests.fs. *) let loadLocation = __SOURCE_DIRECTORY__ + "/LoadingTestArea" let saveLocation = __SOURCE_DIRECTORY__ + "/SavingTestArea" @@ -35,14 +38,18 @@ |> Seq.toArray let randomBrush () = - let item = allColours.[Random().Next(allColours.Length)] + let item = allColours.[rand.Next(allColours.Length)] item.GetValue(null, null) + (* Max. value is arbitrary (change for performance). + Min. value is to stop divide by zero exceptions. + Intended for horizontal and vertical line tests. *) + let newNum () = rand.Next(1, 1000) + let imagesInLoadingTestArea = Directory.GetFileSystemEntries (loadLocation, "*.png") let generateLoadPath () = - let rand = Random () let files = imagesInLoadingTestArea files.[rand.Next(files.Length)] @@ -51,7 +58,7 @@ saveLocation + "/" + fileName (* To "manually" clear out the SavingTestArea folder, use this function - in script.fsx. More information can be found there, also.*) + in script.fsx. More information can be found there, also. *) let resetSavingTestArea () = let files = Directory.GetFileSystemEntries(saveLocation, "*.png") match files.Length with @@ -68,13 +75,14 @@ open DeathSocket.GridPainter open TestingHelpers open System.IO + open System [] let ``Can apply grid to image and save it`` () = (* You should end up with one image left over in SavingTestArea. Comment out the "reset" function to see all the images produced, by this test. This will mean you will need to manually delete the - images yourself if you do.*) + images yourself if you do. *) resetSavingTestArea () let oPath = generateLoadPath () let sPath = generateSavePath oPath @@ -89,6 +97,16 @@ |> Async.RunSynchronously (File.Exists sPath) = true + [] + let ``Can return a collection of points which represent a grids horizontal lines`` () = + let result = determineHorizontalLines (newNum()) (newNum()) (newNum()) + result.Length > 0 + + [] + let ``Can return a collection of points which represent a grids vertical lines`` () = + let result = determineVerticalLines (newNum()) (newNum()) (newNum()) + result.Length > 0 + module UnitTests = open TestingHelpers From 80e1c7e555d615b89b3e54386cc13c88b0b4ddf1 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Mon, 1 Oct 2018 18:26:48 +0100 Subject: [PATCH 09/12] add XML comments to public facing library code. --- DeathSocket/Domain.fs | 29 +++++++++++++++-------------- DeathSocket/GridPainter.fs | 36 +++++++++++++++++++++++++++++++++--- DeathSocketCLI/Validation.fs | 3 +-- TestCentre/LibraryTests.fs | 4 ++-- 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/DeathSocket/Domain.fs b/DeathSocket/Domain.fs index 04be768..34cc817 100644 --- a/DeathSocket/Domain.fs +++ b/DeathSocket/Domain.fs @@ -1,23 +1,24 @@ namespace DeathSocket + /// The domain types used by Death Socket. [] module Domain = open System.Drawing - open System.Drawing.Imaging + /// + /// The specification used by Death Socket when adding a grid to an image. + /// type ImageSpec = - { originalPath: string; - savePath: string; - colour: Brush; - penWidth: float32 - rows: int; - columns: int } - - type StreamSpec = - { imagePath: string; - format: ImageFormat; - colour: Brush; - penWidth: float32 - rows: int; + { /// The original path of the image which the grid is being added to. + originalPath: string + /// The location of the new gridded image. + savePath: string + /// The (System.Drawing) brush used to draw the grid. This determines the colour. + colour: Brush + /// The thickness of the line on the grid. + penWidth: float32 + /// The number of rows the grid will have. + rows: int + ///The number of columns the grid will have. columns: int } \ No newline at end of file diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index 0dbe187..5ff908b 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -2,12 +2,30 @@ namespace DeathSocket open System.IO + /// Provides functions which help draw gridded overlays onto images. module GridPainter = open Validation open ImageServices - // Needs XML comments. + /// + /// Uses the information included in spec and creates a gridded image. + /// It then asynchronously saves it. + /// Please stick to .bmp, .jpg or .png files. + /// The others (image) file types have not been tested. + /// + /// + /// The specification used to generate the new gridded image + /// + /// + /// If the file the grid is being applied to cannot be found, + /// a FileNotFoundException will be thrown. + /// + /// let applyGridAsync spec = async { try @@ -18,10 +36,22 @@ namespace DeathSocket printfn "File could not be found at %s" ex.Message } - // Needs XML comments. + /// + /// Determines the (Pen) points needed to draw the appropriate number of horizontal lines (I.E. rows). + /// Each item in the array includes a start and end co-ordinate (point) for each line. + /// + /// The width of the image. + /// The height of the image. + /// The number of rows the grid should have. let determineHorizontalLines width height rows = createHorizontalLines width height rows - // Needs XML comments. + /// + /// Determines the (Pen) points needed to draw the appropriate number of vertical lines (I.E. columns). + /// Each item in the array includes a start and end co-ordinate (point) for each line. + /// + /// The width of the image. + /// The height of the image. + /// The number of columns the grid should have. let determineVerticalLines width height columns = createVerticalLines width height columns \ No newline at end of file diff --git a/DeathSocketCLI/Validation.fs b/DeathSocketCLI/Validation.fs index c798351..e4cd715 100644 --- a/DeathSocketCLI/Validation.fs +++ b/DeathSocketCLI/Validation.fs @@ -58,5 +58,4 @@ rows = 10 columns = 10 } stream.Dispose () - image.Dispose () - spec \ No newline at end of file + image.Dispose () \ No newline at end of file diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index 8395d4e..4d15747 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -128,9 +128,9 @@ [] let ``Divide By Zero Exception is thrown when 0 rows is used when determining horizontal lines`` () = let result () = GridPainter.determineHorizontalLines 100 100 0 - Assert.Throws(fun () -> result() |> ignore) + Assert.Throws(fun () -> result () |> ignore) [] let ``Divide By Zero Exception is thrown when 0 columns is used when determining vertical lines`` () = let result () = GridPainter.determineVerticalLines 100 100 0 - Assert.Throws(fun () -> result() |> ignore) \ No newline at end of file + Assert.Throws(fun () -> result () |> ignore) \ No newline at end of file From e1493a468964f884343375dba4d4e20e12988efd Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Mon, 1 Oct 2018 19:30:02 +0100 Subject: [PATCH 10/12] add return type in build default spec function (CLI). Set applyGrid function to not build a new Bitmap (like previous commits) in preparation of a new (temp.) test branch. --- DeathSocket/GridPainter.fs | 4 ++++ DeathSocket/ImageServices.fs | 15 ++++++++------- DeathSocketCLI/Validation.fs | 7 ++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index 5ff908b..e06a7ff 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -3,6 +3,8 @@ namespace DeathSocket open System.IO /// Provides functions which help draw gridded overlays onto images. + /// Grid Painter, and all of Death Socket, uses the System.Drawing brushes/colours. + /// If you are using System.Media brushes and colour, you will need to convert them. module GridPainter = open Validation @@ -43,6 +45,7 @@ namespace DeathSocket /// The width of the image. /// The height of the image. /// The number of rows the grid should have. + /// You will probably only need these when dealing with GUI's. let determineHorizontalLines width height rows = createHorizontalLines width height rows @@ -53,5 +56,6 @@ namespace DeathSocket /// The width of the image. /// The height of the image. /// The number of columns the grid should have. + /// You will probably only need these when dealing with GUI's. let determineVerticalLines width height columns = createVerticalLines width height columns \ No newline at end of file diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index 51b4fa9..20ed85f 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -3,6 +3,7 @@ open System.Drawing open System.Drawing.Imaging open DeathSocket + open System.Drawing.Drawing2D let createHorizontalLines width height rows = let interval = height / rows @@ -17,16 +18,16 @@ Point ((interval * point), height)|]|] let drawGrid spec = - // The originalImg is for images with indexed pixels. - use orignialImg = Bitmap.FromFile spec.originalPath - use img = new Bitmap (orignialImg.Width, orignialImg.Height) - use graphics = Graphics.FromImage img - graphics.DrawImage (orignialImg, 0, 0) - use pen = new Pen (spec.colour, width = spec.penWidth) + let img = Bitmap.FromFile spec.originalPath + let graphics = Graphics.FromImage(img) + let pen = new Pen (spec.colour, width = spec.penWidth) let horizontalLines = createHorizontalLines (img.Size.Width) (img.Size.Height) (spec.rows) let verticalLines = createVerticalLines (img.Size.Width) (img.Size.Height) (spec.columns) for line in horizontalLines do graphics.DrawLines (pen, line) for line in verticalLines do graphics.DrawLines (pen, line) - img.Save (spec.savePath, ImageFormat.Png) \ No newline at end of file + img.Save (spec.savePath) + img.Dispose() + graphics.Dispose() + pen.Dispose() \ No newline at end of file diff --git a/DeathSocketCLI/Validation.fs b/DeathSocketCLI/Validation.fs index e4cd715..1225133 100644 --- a/DeathSocketCLI/Validation.fs +++ b/DeathSocketCLI/Validation.fs @@ -48,8 +48,8 @@ columns = numColumns } let buildDefaultSpec imgPath newPath = - let stream = new FileStream (imgPath, FileMode.Open) - let image = Image.FromStream (stream, false, false) + use stream = new FileStream (imgPath, FileMode.Open) + use image = Image.FromStream (stream, false, false) let spec = { originalPath = imgPath savePath = newPath @@ -58,4 +58,5 @@ rows = 10 columns = 10 } stream.Dispose () - image.Dispose () \ No newline at end of file + image.Dispose () + spec \ No newline at end of file From 56acaba83b9d5b38150f389663803d8594cffc9a Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Mon, 1 Oct 2018 20:36:48 +0100 Subject: [PATCH 11/12] update the drawGrid function to work with images with indexed pixels. The change, also, stop images with no indexed pixels from being cropped. --- DeathSocket/ImageServices.fs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index 20ed85f..d3166da 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -18,16 +18,17 @@ Point ((interval * point), height)|]|] let drawGrid spec = - let img = Bitmap.FromFile spec.originalPath - let graphics = Graphics.FromImage(img) - let pen = new Pen (spec.colour, width = spec.penWidth) + // The temp. file is used as a way to convert images with index pixels. + 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 (img.Size.Width) (img.Size.Height) (spec.rows) + createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows) let verticalLines = - createVerticalLines (img.Size.Width) (img.Size.Height) (spec.columns) + 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) - img.Save (spec.savePath) - img.Dispose() - graphics.Dispose() - pen.Dispose() \ No newline at end of file + clone.Save (spec.savePath) \ No newline at end of file From 6e8ccd476900805e41254f6460a3637859d36577 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Mon, 1 Oct 2018 21:18:12 +0100 Subject: [PATCH 12/12] add validation for the intended save file type. Make "drawing" commands async. Minor spelling changes (comments). --- DeathSocket/GridPainter.fs | 3 ++- DeathSocket/ImageServices.fs | 3 +-- DeathSocket/Validation.fs | 13 ++++++++++++- DeathSocketCLI/Commands.fs | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index e06a7ff..fbb3bba 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -31,7 +31,8 @@ namespace DeathSocket let applyGridAsync spec = async { try - validateFilePath |> ignore + validateFilePath spec.originalPath |> ignore + validatFileType spec.savePath |> ignore drawGrid spec |> ignore with | :? FileNotFoundException as ex -> diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index d3166da..b6219ed 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -3,7 +3,6 @@ open System.Drawing open System.Drawing.Imaging open DeathSocket - open System.Drawing.Drawing2D let createHorizontalLines width height rows = let interval = height / rows @@ -18,7 +17,7 @@ Point ((interval * point), height)|]|] let drawGrid spec = - // The temp. file is used as a way to convert images with index pixels. + // The temp. file is used as a way to convert images with indexed pixels. 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) diff --git a/DeathSocket/Validation.fs b/DeathSocket/Validation.fs index 1931f22..a192a9b 100644 --- a/DeathSocket/Validation.fs +++ b/DeathSocket/Validation.fs @@ -1,9 +1,20 @@ module internal Validation -open System.IO + open System.IO let validateFilePath path = match File.Exists path with | true -> () | false -> raise (new FileNotFoundException (path + " could not be found.")) + let validatFileType file = + match Path.GetExtension file with + | ".bmp" -> () + | ".BMP" -> () + | ".jpg" -> () + | ".JPG" -> () + | ".png" -> () + | ".PNG" -> () + | ".tif" -> () + | ".TIF" -> () + | _ -> invalidArg "savePath" "The file type must be a .bmp, .jpg, .png or .tif file." \ No newline at end of file diff --git a/DeathSocketCLI/Commands.fs b/DeathSocketCLI/Commands.fs index 40aa03e..53fbc9c 100644 --- a/DeathSocketCLI/Commands.fs +++ b/DeathSocketCLI/Commands.fs @@ -39,7 +39,7 @@ printfn "[INFO.] Adding default grid to image..." buildDefaultSpec imgPath newPath |> applyGridAsync - |> Async.RunSynchronously + |> Async.Start showEndOfCommandMessage with | :? FileNotFoundException as ex -> "[ERROR] No file was found at " + ex.FileName @@ -58,7 +58,7 @@ printfn "[INFO.] Adding grid to image..." buildSpec imgPath numRows numColumns pWidth colour newPath |> applyGridAsync - |> Async.RunSynchronously + |> Async.Start showEndOfCommandMessage with | :? FileNotFoundException as ex -> "[ERROR] No file was found at " + ex.FileName