diff --git a/DeathSocket/DeathSocket.fsproj b/DeathSocket/DeathSocket.fsproj index 077a558..02ea7ad 100644 --- a/DeathSocket/DeathSocket.fsproj +++ b/DeathSocket/DeathSocket.fsproj @@ -25,6 +25,7 @@ + diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index cd4479d..9045f8d 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -14,6 +14,7 @@ namespace DeathSocket module GridPainter = open ValidationServices + open ImagePrep open ImageServices open SkiaSharp diff --git a/DeathSocket/ImagePrep.fs b/DeathSocket/ImagePrep.fs new file mode 100644 index 0000000..799d454 --- /dev/null +++ b/DeathSocket/ImagePrep.fs @@ -0,0 +1,66 @@ +module internal ImagePrep + + open System.IO + open DeathSocket + open SkiaSharp + open System.Drawing + + let determineLongestDimension pDims aDims = + let width = (fst pDims) - (fst aDims) + let height = (snd pDims) - (snd aDims) + match width > height with + | true -> Width + | false -> Height + + let determineImageScale pDimension aDimension = + (double aDimension) / (double pDimension) + + let determineLineScale (lineWidth: double) (scale: double) = + lineWidth / scale + + let adjustLineThickness pDimensions aDimensions lineWidth = + match (determineLongestDimension pDimensions aDimensions) with + | Width -> + let thickness = + determineImageScale (fst pDimensions) (fst aDimensions) + |> determineLineScale lineWidth + thickness + | Height -> + let thickness = + determineImageScale (snd pDimensions) (snd aDimensions) + |> determineLineScale lineWidth + thickness + + let createSKHorizontalLines width height rows = + let interval = float32 (height / rows) + [|for point in 1 .. (rows - 1) -> + [| SKPoint ((float32 0), (interval * (float32 point))) + SKPoint ((float32 width), (interval * (float32 point)))|]|] + + let createSKVerticalLines width height columns = + let interval = float32 (width / columns) + [| for point in 1 .. (columns - 1) -> + [| SKPoint ((interval * (float32 point)), (float32 0)) + SKPoint ((interval * (float32 point)), (float32 height))|]|] + + let determineSkiaDimensions filePath = + use fileStream = File.Open (filePath, FileMode.Open) + use skStream = new SKManagedStream (fileStream) + use bitmap = SKBitmap.Decode (skStream) + (bitmap.Width, bitmap.Height) + + let createHorizontalLines width height rows = + let interval = height / rows + [| for point in 1 .. (rows - 1) -> + [|Point (0, (interval * point)) + Point (width, (interval * point) )|]|] + + let createVerticalLines width height columns = + let interval = width / columns + [| for point in 1 .. (columns - 1) -> + [| Point ((interval * point), 0) + Point ((interval * point), height)|]|] + + let determineSystemDrawingDimensions filePath = + use bitmap = Bitmap.FromFile filePath + (bitmap.Width, bitmap.Height) \ No newline at end of file diff --git a/DeathSocket/ValidationServices.fs b/DeathSocket/ValidationServices.fs new file mode 100644 index 0000000..a2f3edc --- /dev/null +++ b/DeathSocket/ValidationServices.fs @@ -0,0 +1,31 @@ +module internal ValidationServices + + open System.IO + + let validateFilePath path = + match File.Exists path with + | true -> () + | false -> raise (new FileNotFoundException ("No file found at " + path)) + + let validateSaveFileType file = + match Path.GetExtension file with + | ".jpg" -> () + | ".JPG" -> () + | ".png" -> () + | ".PNG" -> () + | _ -> invalidArg "savePath" "The file type must be a .jpg or .png file." + + let validateIO iPath oPath = + validateFilePath iPath + validateSaveFileType oPath + + let validateDimensions dimensions = + match dimensions with + | (0, _) -> invalidArg "Width" "Width must be greater than 0." + | (_, 0) -> invalidArg "Height" "Height must be greater than 0." + | (_, _) -> () + + let validateLineThickness thickness = + match thickness with + | thickness when thickness <= 0.0 -> invalidArg "LineThickness" "LineThickness must be greater than 0." + | _ -> () \ No newline at end of file