Browse Source

refactor applyGridToImageAsync (utilise greyScale better).

I have seperated the drawBrushSpec function into two functions: One using the greyScale option and the other without. The code should be easier to read and not get bogged down in the if-else statement it was using previously. This is the only part of the code which convert the image to greyscale.
unstable
Craig Oates 4 years ago
parent
commit
77f198ffcc
  1. 12
      DeathSocket/GridPainter.fs
  2. 102
      DeathSocket/ImageServices.fs

12
DeathSocket/GridPainter.fs

@ -34,22 +34,24 @@ namespace DeathSocket
/// is not in use or needed by another program/process. /// is not in use or needed by another program/process.
/// This is because it is locked whilst in this function. /// This is because it is locked whilst in this function.
/// </remarks> /// </remarks>
let applyGridToImageAsync (greyScale: bool) (spec: ImageSpec) = let applyGridToImageAsync (makeGreyScale: bool) (spec: ImageSpec) =
async { async {
try try
match spec with match spec with
| Brush b -> | Brush b ->
validateIO b.originalPath b.savePath |> ignore validateIO b.originalPath b.savePath |> ignore
drawBrushSpecGrid b greyScale match makeGreyScale with
| true -> drawBrushSpecGridGrey b
| false -> drawBrushSpecGrid b
| RGBA r -> | RGBA r ->
validateIO r.originalPath r.savePath |> ignore validateIO r.originalPath r.savePath |> ignore
drawRGBAGrid r drawRGBAGrid r // greyScale
| Skia s -> | Skia s ->
validateIO s.originalPath s.savePath |> ignore validateIO s.originalPath s.savePath |> ignore
drawSkiaGrid s drawSkiaGrid s // greyScale
| SkiaRGB sR -> | SkiaRGB sR ->
validateIO sR.originalPath sR.savePath |> ignore validateIO sR.originalPath sR.savePath |> ignore
drawSkiaRGBGrid sR drawSkiaRGBGrid sR // greyScale
| _ -> printfn "[DEATH SOCKET INFO] Inappropriate ImageSpec used here. Please use none buffer-based spec's when using this function." | _ -> printfn "[DEATH SOCKET INFO] Inappropriate ImageSpec used here. Please use none buffer-based spec's when using this function."
with with
| :? FileNotFoundException as ex -> | :? FileNotFoundException as ex ->

102
DeathSocket/ImageServices.fs

@ -191,68 +191,52 @@
assume any function with a "*Spec" type as a parameter will use this "temp" assume any function with a "*Spec" type as a parameter will use this "temp"
file. *) file. *)
let drawBrushSpecGrid (spec: BrushSpec) (greyScale: bool) = let drawBrushSpecGridGrey (spec: BrushSpec) =
(* Found this code for this function at:
https://stackoverflow.com/questions/199468/c-sharp-image-clone-out-of-memory-exception *)
use original = Bitmap.FromFile spec.originalPath use original = Bitmap.FromFile spec.originalPath
use temp = new Bitmap(original) use temp = new Bitmap(original)
//use clone = use clone = new Bitmap(temp.Width, temp.Height)
// temp.Clone(new Rectangle(0, 0, temp.Width, temp.Height), PixelFormat.Format32bppArgb) use g = Graphics.FromImage(clone)
// The orignial is above... let aM: float32[][] = [|
[| (float32 0.3); (float32 0.3); (float32 0.3); (float32 0.0); (float32 0.0) |];
if greyScale = true then [| (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) |];
use temp1 = new Bitmap(original.Width, original.Height) [| (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0); (float32 0.0) |];
use g = Graphics.FromImage(temp1) [| (float32 0.0); (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0) |];
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) |]; let colourMatrix = new ColorMatrix(aM)
[| (float32 0.11); (float32 0.11); (float32 0.11); (float32 0.0); (float32 0.0) |]; use attributes = new ImageAttributes()
[| (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0); (float32 0.0) |]; attributes.SetColorMatrix(colourMatrix)
[| (float32 0.0); (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0) |]; 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)
let colourMatrix = new ColorMatrix(aM) use pen = new Pen (spec.colour, width = spec.penWidth)
use attributes = new ImageAttributes() graphics.DrawImage(clone ,new Rectangle(0, 0, clone.Width, clone.Height))
attributes.SetColorMatrix(colourMatrix) let horizontalLines =
g.DrawImage(temp, new Rectangle(0, 0, temp.Width, temp.Height), createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows)
0, 0, temp.Width, temp.Height, GraphicsUnit.Pixel, attributes) let verticalLines =
createVerticalLines (clone.Size.Width) (clone.Size.Height) (spec.columns)
// use clone = temp1 for line in horizontalLines do graphics.DrawLines (pen, line)
use graphics = Graphics.FromImage(temp1) for line in verticalLines do graphics.DrawLines (pen, line)
use pen = new Pen (spec.colour, width = spec.penWidth) clone.Save (spec.savePath)
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) let drawBrushSpecGrid (spec: BrushSpec) =
//use pen = new Pen (spec.colour, width = spec.penWidth) use original = Bitmap.FromFile spec.originalPath
//graphics.DrawImage(original,new Rectangle(0, 0, clone.Width, clone.Height)) use temp = new Bitmap(original)
//let horizontalLines = use clone = temp.Clone(new Rectangle(0, 0, temp.Width, temp.Height), PixelFormat.Format32bppArgb)
// createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows) use graphics = Graphics.FromImage(clone)
//let verticalLines = use pen = new Pen (spec.colour, width = spec.penWidth)
// createVerticalLines (clone.Size.Width) (clone.Size.Height) (spec.columns) graphics.DrawImage(clone ,new Rectangle(0, 0, clone.Width, clone.Height))
//for line in horizontalLines do graphics.DrawLines (pen, line) let horizontalLines =
//for line in verticalLines do graphics.DrawLines (pen, line) createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows)
//clone.Save (spec.savePath) 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) = let drawRGBAGrid (spec: RGBASpec) =
use original = Bitmap.FromFile spec.originalPath use original = Bitmap.FromFile spec.originalPath

Loading…
Cancel
Save