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="ImagePrep.fs" />
<Compile Include="ImageServices.fs" /> <Compile Include="ImageServices.fs" />
<Compile Include="GridPainter.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> <Pack>True</Pack>
<PackagePath></PackagePath> <PackagePath></PackagePath>
</None> </None>
@ -46,7 +46,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.7.0" /> <PackageReference Update="FSharp.Core" Version="4.7.2" />
</ItemGroup> </ItemGroup>
</Project> </Project>

4
DeathSocket/GridPainter.fs

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

73
DeathSocket/ImageServices.fs

@ -191,21 +191,68 @@
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) = let drawBrushSpecGrid (spec: BrushSpec) (greyScale: bool) =
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 =
temp.Clone(new Rectangle(0, 0, temp.Width, temp.Height), PixelFormat.Format32bppArgb) // 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) // The orignial is above...
graphics.DrawImage(original,new Rectangle(0, 0, clone.Width, clone.Height))
let horizontalLines = if greyScale = true then
createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows)
let verticalLines = use temp1 = new Bitmap(original.Width, original.Height)
createVerticalLines (clone.Size.Width) (clone.Size.Height) (spec.columns) use g = Graphics.FromImage(temp1)
for line in horizontalLines do graphics.DrawLines (pen, line) let aM: float32[][] = [|
for line in verticalLines do graphics.DrawLines (pen, line) [| (float32 0.3); (float32 0.3); (float32 0.3); (float32 0.0); (float32 0.0) |];
clone.Save (spec.savePath) [| (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) = let drawRGBAGrid (spec: RGBASpec) =
use original = Bitmap.FromFile spec.originalPath use original = Bitmap.FromFile spec.originalPath

20
DeathSocketCLI/Commands.fs

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

8
TestCentre/LibraryTests.fs

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

Loading…
Cancel
Save