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. 46
      DeathSocket/ImageServices.fs

12
DeathSocket/GridPainter.fs

@ -34,22 +34,24 @@ namespace DeathSocket
/// is not in use or needed by another program/process.
/// This is because it is locked whilst in this function.
/// </remarks>
let applyGridToImageAsync (greyScale: bool) (spec: ImageSpec) =
let applyGridToImageAsync (makeGreyScale: bool) (spec: ImageSpec) =
async {
try
match spec with
| Brush b ->
validateIO b.originalPath b.savePath |> ignore
drawBrushSpecGrid b greyScale
match makeGreyScale with
| true -> drawBrushSpecGridGrey b
| false -> drawBrushSpecGrid b
| RGBA r ->
validateIO r.originalPath r.savePath |> ignore
drawRGBAGrid r
drawRGBAGrid r // greyScale
| Skia s ->
validateIO s.originalPath s.savePath |> ignore
drawSkiaGrid s
drawSkiaGrid s // greyScale
| SkiaRGB sR ->
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."
with
| :? FileNotFoundException as ex ->

46
DeathSocket/ImageServices.fs

@ -191,18 +191,14 @@
assume any function with a "*Spec" type as a parameter will use this "temp"
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 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)
// The orignial is above...
if greyScale = true then
use temp1 = new Bitmap(original.Width, original.Height)
use g = Graphics.FromImage(temp1)
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) |];
@ -216,23 +212,24 @@
attributes.SetColorMatrix(colourMatrix)
g.DrawImage(temp, new Rectangle(0, 0, temp.Width, temp.Height),
0, 0, temp.Width, temp.Height, GraphicsUnit.Pixel, attributes)
// use clone = temp1
use graphics = Graphics.FromImage(temp1)
use graphics = Graphics.FromImage(clone)
use pen = new Pen (spec.colour, width = spec.penWidth)
graphics.DrawImage(temp1 ,new Rectangle(0, 0, temp1.Width, temp1.Height))
graphics.DrawImage(clone ,new Rectangle(0, 0, clone.Width, clone.Height))
let horizontalLines =
createHorizontalLines (temp1.Size.Width) (temp1.Size.Height) (spec.rows)
createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows)
let verticalLines =
createVerticalLines (temp1.Size.Width) (temp1.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)
temp1.Save (spec.savePath)
else
clone.Save (spec.savePath)
let drawBrushSpecGrid (spec: BrushSpec) =
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))
graphics.DrawImage(clone ,new Rectangle(0, 0, clone.Width, clone.Height))
let horizontalLines =
createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows)
let verticalLines =
@ -241,19 +238,6 @@
for line in verticalLines do graphics.DrawLines (pen, line)
clone.Save (spec.savePath)
// The original is below...
//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)
let drawRGBAGrid (spec: RGBASpec) =
use original = Bitmap.FromFile spec.originalPath
use temp = new Bitmap (original)

Loading…
Cancel
Save