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.
/// 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 ->

102
DeathSocket/ImageServices.fs

@ -191,68 +191,52 @@
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)
// 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) |];
[| (float32 0.11); (float32 0.11); (float32 0.11); (float32 0.0); (float32 0.0) |];
[| (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0); (float32 0.0) |];
[| (float32 0.0); (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0) |];
|]
let colourMatrix = new ColorMatrix(aM)
use attributes = new ImageAttributes()
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 pen = new Pen (spec.colour, width = spec.penWidth)
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 clone = new Bitmap(temp.Width, temp.Height)
use g = Graphics.FromImage(clone)
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) |];
[| (float32 0.11); (float32 0.11); (float32 0.11); (float32 0.0); (float32 0.0) |];
[| (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0); (float32 0.0) |];
[| (float32 0.0); (float32 0.0); (float32 0.0); (float32 0.0); (float32 1.0) |];
|]
let colourMatrix = new ColorMatrix(aM)
use attributes = new ImageAttributes()
attributes.SetColorMatrix(colourMatrix)
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)
use pen = new Pen (spec.colour, width = spec.penWidth)
graphics.DrawImage(clone ,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)
//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 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(clone ,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

Loading…
Cancel
Save