Browse Source

implement rough initial version of convert to greyscale.

The code which used the Brush spec type can now convert the image to greyscale and save the image with the grid still in the colour the end-user selected. This code is very rough and will need cleaning up.
unstable
Craig Oates 4 years ago
parent
commit
b7cd421323
  1. 4
      DeathSocket/DeathSocket.fsproj
  2. 4
      DeathSocket/GridPainter.fs
  3. 73
      DeathSocket/ImageServices.fs
  4. 20
      DeathSocketCLI/Commands.fs
  5. 8
      TestCentre/LibraryTests.fs

4
DeathSocket/DeathSocket.fsproj

@ -32,7 +32,7 @@
<Compile Include="ImagePrep.fs" />
<Compile Include="ImageServices.fs" />
<Compile Include="GridPainter.fs" />
<None Include="..\..\..\_temp\death-socket-repo-logo.png">
<None Include="..\..\..\_projects\death-socket\death-socket-repo-logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
@ -46,7 +46,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.7.0" />
<PackageReference Update="FSharp.Core" Version="4.7.2" />
</ItemGroup>
</Project>

4
DeathSocket/GridPainter.fs

@ -34,13 +34,13 @@ 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 (spec: ImageSpec) =
let applyGridToImageAsync (greyScale: bool) (spec: ImageSpec) =
async {
try
match spec with
| Brush b ->
validateIO b.originalPath b.savePath |> ignore
drawBrushSpecGrid b
drawBrushSpecGrid b greyScale
| RGBA r ->
validateIO r.originalPath r.savePath |> ignore
drawRGBAGrid r

73
DeathSocket/ImageServices.fs

@ -191,21 +191,68 @@
assume any function with a "*Spec" type as a parameter will use this "temp"
file. *)
let drawBrushSpecGrid (spec: BrushSpec) =
let drawBrushSpecGrid (spec: BrushSpec) (greyScale: bool) =
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)
//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 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

20
DeathSocketCLI/Commands.fs

@ -32,15 +32,15 @@
let exit () = Environment.Exit (Environment.ExitCode)
[<ListCommand>]
[<Parameters "(image-path: string) (new-path: string)">]
[<Parameters "(image-path: string) (new-path: string) (greyScale: bool)">]
[<Description
"Takes the image at 'image-path' applies a 10x10 grid (in white) to it and saves the result at 'new-path'.">]
[<Usage "add-default C:/base-image.png C:/final-image.png">]
let ``add-default`` imgPath newPath =
[<Usage "add-default C:/base-image.png C:/final-image.png true">]
let ``add-default`` imgPath newPath greyScale=
try
printfn "[INFO.] Adding default grid to image..."
Brush (buildDefaultSpec imgPath newPath)
|> applyGridToImageAsync
|> applyGridToImageAsync greyScale
|> Async.Start
showEndOfCommandMessage
with
@ -55,11 +55,11 @@
[<Description "Adds a grid to an image, using the specified parameters, and saves it.">]
[<Usage
"add-grid C:/orignal-image.png 10 5 2 red C:/new-image.png">]
let ``add-grid`` imgPath numRows numColumns pWidth colour newPath =
let ``add-grid`` imgPath numRows numColumns pWidth colour newPath greyScale=
try
printfn "[INFO.] Adding grid to image..."
Brush (buildSpec imgPath numRows numColumns pWidth colour newPath)
|> applyGridToImageAsync
|> applyGridToImageAsync greyScale
|> Async.Start
showEndOfCommandMessage
with
@ -109,16 +109,16 @@
printfn "%s" item.Key
showEndOfCommandMessage
let``add-skia-grid`` imgPath numRows numColumns pWidth colour newPath =
let``add-skia-grid`` imgPath numRows numColumns pWidth colour newPath greyScale=
printfn "[INFO.] Adding SkiaSharp grid to image..."
Skia (buildSkiaSpec imgPath numRows numColumns pWidth colour newPath)
|> applyGridToImageAsync
|> applyGridToImageAsync greyScale
|> Async.Start
showEndOfCommandMessage
let``add-skia-rgb-grid`` imgPath numRows numColumns pWidth r g b newPath =
let``add-skia-rgb-grid`` imgPath numRows numColumns pWidth r g b newPath greyScale =
printfn "[INFO.] Adding SkiaSharp grid to image..."
SkiaRGB (buildSkiaRGBSpec imgPath numRows numColumns pWidth r g b newPath)
|> applyGridToImageAsync
|> applyGridToImageAsync greyScale
|> Async.Start
showEndOfCommandMessage

8
TestCentre/LibraryTests.fs

@ -134,7 +134,7 @@
penWidth = float32 (newPenWidth())
rows = 10
columns = 10 })
|> applyGridToImageAsync
|> applyGridToImageAsync true // true is temp.
|> Async.RunSynchronously
(File.Exists sPath) = true
@ -152,7 +152,7 @@
penWidth = float32 (newPenWidth())
rows = 10
columns = 10 })
|> applyGridToImageAsync
|> applyGridToImageAsync true // true is temp.
|> Async.RunSynchronously
(File.Exists sPath) = true
@ -199,7 +199,7 @@
penWidth = float32 (newPenWidth())
rows = newNum ()
columns = newNum () })
|> applyGridToImageAsync
|> applyGridToImageAsync true // true is temp.
|> Async.RunSynchronously
(File.Exists sPath) = true
@ -217,7 +217,7 @@
penWidth = float32 (newPenWidth())
rows = newNum ()
columns = newNum () })
|> applyGridToImageAsync
|> applyGridToImageAsync true // true is temp.
|> Async.RunSynchronously
(File.Exists sPath) = true

Loading…
Cancel
Save