From c89dab50dd5ec0c6e24a8bdcb9535f7d1a18d9cf Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Mon, 15 Oct 2018 20:22:58 +0100 Subject: [PATCH] add initial bits of code for expanding the amount of spec's. Begin adding code to use the new specifications. --- DeathSocket/ColourServices.fs | 14 +++++++++++ DeathSocket/DeathSocket.fsproj | 1 + DeathSocket/Domain.fs | 39 ++++++++++++++++++++++++++++++ DeathSocket/GridPainter.fs | 19 ++++++++++++++- DeathSocket/ImageServices.fs | 43 ++++++++++++++++++++++++++++++++-- DeathSocket/ScratchPad.fsx | 4 ++-- DeathSocketCLI/AssemblyInfo.fs | 4 ++-- TestCentre/LibraryTests.fs | 2 +- 8 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 DeathSocket/ColourServices.fs diff --git a/DeathSocket/ColourServices.fs b/DeathSocket/ColourServices.fs new file mode 100644 index 0000000..606ca0f --- /dev/null +++ b/DeathSocket/ColourServices.fs @@ -0,0 +1,14 @@ +module ColourServices + +open System.Drawing +open DeathSocket.Domain + + // not tested + let convertRGBAToBrush (spec: RGBASpec) = + let a = int spec.alpha + let r = int spec.red + let g = int spec.green + let b = int spec.blue + let colour = Color.FromArgb (a, r, g, b) + new SolidBrush(colour) + diff --git a/DeathSocket/DeathSocket.fsproj b/DeathSocket/DeathSocket.fsproj index 3f8cc01..a4e484c 100644 --- a/DeathSocket/DeathSocket.fsproj +++ b/DeathSocket/DeathSocket.fsproj @@ -7,6 +7,7 @@ + diff --git a/DeathSocket/Domain.fs b/DeathSocket/Domain.fs index 34cc817..c9d1b72 100644 --- a/DeathSocket/Domain.fs +++ b/DeathSocket/Domain.fs @@ -21,4 +21,43 @@ /// The number of rows the grid will have. rows: int ///The number of columns the grid will have. + columns: int } + + /// + /// The specification which uses System.Drawing brush to draw a grid. + /// + // REPLACING IMAGESPEC + type BrushSpec = + { /// 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 } + + type RGBASpec = + { originalPath: string + savePath: string + alpha: float + red: float + green: float + blue: float + penWidth: float32 + rows: int + columns: int } + + type CMYKSpec = + { originalPath: string + savePath: string + cyan: float + magenta: float + yellow: float + key: float32 + rows: int columns: int } \ No newline at end of file diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index fbb3bba..cc0342d 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -28,7 +28,8 @@ namespace DeathSocket /// is not in use or needed by another program/process. /// This is because it is locked whilst in this function. /// - let applyGridAsync spec = + let applyGridAsync (spec: ImageSpec) = + // The spec is to be changed to Brush Spec. async { try validateFilePath spec.originalPath |> ignore @@ -39,6 +40,20 @@ namespace DeathSocket printfn "File could not be found at %s" ex.Message } + // Not tested. + let applyRGBAGridAsync (spec: RGBASpec) = + async { + try + validateFilePath spec.originalPath |> ignore + validatFileType spec.savePath |> ignore + drawRGBAGrid spec + with + | :? FileNotFoundException as ex -> + printfn "File could not be found at %s" ex.Message + } + + // let applyCMYKGridAsync -- to be added at a later date. + /// /// 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. @@ -48,6 +63,7 @@ namespace DeathSocket /// The number of rows the grid should have. /// You will probably only need these when dealing with GUI's. let determineHorizontalLines width height rows = + // To Be moved to its own service and made public. No wrapper needed. createHorizontalLines width height rows /// @@ -59,4 +75,5 @@ namespace DeathSocket /// The number of columns the grid should have. /// You will probably only need these when dealing with GUI's. let determineVerticalLines width height columns = + // To Be moved to its own service and made public. No wrapper needed. createVerticalLines width height columns \ No newline at end of file diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index b6219ed..1152496 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -3,20 +3,25 @@ open System.Drawing open System.Drawing.Imaging open DeathSocket + open Validation + open ColourServices + // To Be moved to its own service and made public. No wrapper needed. let createHorizontalLines width height rows = let interval = height / rows [| for point in 1 .. (rows - 1) -> [|Point (0, (interval * point)) Point (width, (interval * point) )|]|] + // To Be moved to its own service and made public. No wrapper needed. let createVerticalLines width height columns = let interval = width / columns [| for point in 1 .. (columns - 1) -> [| Point ((interval * point), 0) Point ((interval * point), height)|]|] - let drawGrid spec = + // To be deleted -- replaced with brush spec. + let drawGrid (spec: ImageSpec) = // 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) @@ -30,4 +35,38 @@ 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) \ No newline at end of file + clone.Save (spec.savePath) + + // not tested but same as Draw Grid -- this is its replacement + let drawBrushSpecGrid (spec: BrushSpec) = + // 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) + 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) + + // Not tested + let drawRGBAGrid (spec: RGBASpec) = + // 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) + use graphics = Graphics.FromImage(clone) + use pen = new Pen ((convertRGBAToBrush spec), 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) diff --git a/DeathSocket/ScratchPad.fsx b/DeathSocket/ScratchPad.fsx index 63444cb..13eaa89 100644 --- a/DeathSocket/ScratchPad.fsx +++ b/DeathSocket/ScratchPad.fsx @@ -30,7 +30,7 @@ let verticalLines = createVerticalLines 300 600 10 Death Socket assumes either JPEG or PNG files so use other files at your own risk. Cannot guarantee they will work. Also, either in this spec. can be changed to suit your needs. *) -let spec = +let spec :ImageSpec = { originalPath = desktop + "/test.jpg" savePath = desktop + "/grid.png" colour = Brushes.Chartreuse @@ -39,4 +39,4 @@ let spec = columns = 10 } // Run this when you have finished building the spec. -GridPainter.applyGrid spec |> Async.RunSynchronously +GridPainter.applyGridAsync spec |> Async.RunSynchronously diff --git a/DeathSocketCLI/AssemblyInfo.fs b/DeathSocketCLI/AssemblyInfo.fs index 2352b17..00d540e 100644 --- a/DeathSocketCLI/AssemblyInfo.fs +++ b/DeathSocketCLI/AssemblyInfo.fs @@ -38,8 +38,8 @@ open System.Runtime.InteropServices // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [] -[] -[] +[] +[] do () \ No newline at end of file diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index 4d15747..2810f14 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -86,7 +86,7 @@ resetSavingTestArea () let oPath = generateLoadPath () let sPath = generateSavePath oPath - let spec = + let (spec: ImageSpec) = { originalPath = oPath savePath = sPath colour = randomBrush () :?> Brush