Skip to content

Commit

Permalink
Introduce AlphaCompositing.darken
Browse files Browse the repository at this point in the history
  • Loading branch information
breki committed May 23, 2024
1 parent 7323f9e commit 3443c96
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 57 deletions.
3 changes: 2 additions & 1 deletion Demeton.Tests/Aw3d/AW3D experiments.fs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ let fetchAw3dHeightsArray _ =
[<Fact(Skip = "downloads the tile so it takes too long")>]
let ``Generate hillshading from AW3D`` () =
let pixelShader =
HighwireHillshader.shadePixel
IgorHillshader.shadePixel
{ SunAzimuth = IgorHillshader.DefaultSunAzimuth
ShadingColor = 0u
HeightsArrayIndex = 0 }

let createShaderFunction _ =
Expand Down
4 changes: 2 additions & 2 deletions Demeton.Tests/WorldCover/WorldCover experiments.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ let StepNameXcTracerWaterBodies = "XCTracer-water-bodies"
let StepNameXcTracerWaterBodiesOutline = "XCTracer-water-bodies-outline"

let hillshadingStep =
Demeton.Shaders.Pipeline.Common.ShadingStep.HighwireHillshading
Demeton.Shaders.HighwireHillshader.defaultParameters
Demeton.Shaders.Pipeline.Common.ShadingStep.IgorHillshading
IgorHillshader.defaultParameters

let waterBodiesStep = Pipeline.Common.CustomShading StepNameXcTracerWaterBodies

Expand Down
8 changes: 4 additions & 4 deletions Demeton/Commands/TileShadeCommand.fs
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,17 @@ let run (options: Options) : Result<unit, string> =
let igorHillshadingStep =
ShadingStep.IgorHillshading IgorHillshader.defaultParameters

let highwireHillshadingStep =
ShadingStep.HighwireHillshading HighwireHillshader.defaultParameters
let igorHillshadingStep =
ShadingStep.IgorHillshading IgorHillshader.defaultParameters

let slopeShadingStep =
ShadingStep.SlopeShading SlopeShader.defaultParameters

let hillshadingStep =
Compositing(
highwireHillshadingStep,
igorHillshadingStep,
slopeShadingStep,
CompositingFuncIdOver
CompositingFuncIdAlphaDarken
)

let rootShadingStep =
Expand Down
1 change: 0 additions & 1 deletion Demeton/Demeton.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
<Compile Include="Shaders\SlopeShader.fs"/>
<Compile Include="Shaders\AspectShader.fs"/>
<Compile Include="Shaders\IgorHillshader.fs"/>
<Compile Include="Shaders\HighwireHillshader.fs"/>
<Compile Include="Shaders\Pipeline\Common.fs"/>
<Compile Include="Shaders\Pipeline\Parsing.fs"/>
<Compile Include="Shaders\Pipeline\Building.fs"/>
Expand Down
36 changes: 36 additions & 0 deletions Demeton/Png/AlphaCompositing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,39 @@ let imageOver: CompositingFunc =
|> ignore

dest

let darken: CompositingFunc =
fun
(imageWidth: int)
(imageHeight: int)
(source: RawImageData)
(dest: RawImageData) ->
Parallel.For(
0,
imageHeight,
fun y ->
for x in 0 .. imageWidth - 1 do
let sourceDarknessRatio =
Rgba8Bit.pixelAt source imageWidth x y
|> Rgba8Bit.a
|> byteToRatio

let destDarknessRatio =
Rgba8Bit.pixelAt dest imageWidth x y
|> Rgba8Bit.a
|> byteToRatio

let outDarknessRatio =
1.
- ((1. - sourceDarknessRatio) * (1. - destDarknessRatio))

let outAlpha = ratioToByte outDarknessRatio

// todo 5: use source RGB value instead of always black
let outPixel = Rgba8Bit.rgbaColor 0uy 0uy 0uy outAlpha

Rgba8Bit.setPixelAt dest imageWidth x y outPixel
)
|> ignore

dest
42 changes: 0 additions & 42 deletions Demeton/Shaders/HighwireHillshader.fs

This file was deleted.

11 changes: 4 additions & 7 deletions Demeton/Shaders/Pipeline/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ type ShadingStep =
| IgorHillshading of IgorHillshader.ShaderParameters
| SlopeShading of SlopeShader.ShaderParameters
| AspectShading of AspectShader.ShaderParameters
| HighwireHillshading of HighwireHillshader.ShaderParameters
| CustomShading of ShadingFuncId
| Compositing of (ShadingStep * ShadingStep * CompositingFuncId)

[<Literal>]
let CompositingFuncIdOver = "over"

[<Literal>]
let CompositingFuncIdAlphaDarken = "alpha-darken"

let createShadingFuncById shadingFuncId =
invalidOp "we currently do not support custom shading functions"

let createCompositingFuncById compositingFuncId =
match compositingFuncId with
| CompositingFuncIdOver -> AlphaCompositing.imageOver
| CompositingFuncIdAlphaDarken -> AlphaCompositing.darken
| _ -> invalidOp "Unknown compositing function."


Expand Down Expand Up @@ -130,12 +133,6 @@ let rec executeShadingStep
Hillshading.shadeRaster
parameters.HeightsArrayIndex
(SlopeShader.shadePixel parameters)
| HighwireHillshading parameters ->
Log.info "Running highwire hillshading step..."

Hillshading.shadeRaster
parameters.HeightsArrayIndex
(HighwireHillshader.shadePixel parameters)
| CustomShading shadingFuncId -> shadingFuncFactory shadingFuncId
| _ -> invalidOp "Unsupported shading step"

Expand Down

0 comments on commit 3443c96

Please sign in to comment.