Skip to content

Commit 1b68402

Browse files
committed
Add DecodeCommands support
1 parent ee8e357 commit 1b68402

File tree

4 files changed

+91
-7
lines changed

4 files changed

+91
-7
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Drawing;
4+
using System.Linq;
5+
6+
namespace Imageflow.Fluent
7+
{
8+
[Flags]
9+
public enum DecderDownscalingMode
10+
{
11+
/// <summary>
12+
/// Use the Imageflow default (usually highest quality)
13+
/// </summary>
14+
Unspecified = 0,
15+
/// <summary>
16+
/// Use the fastest method
17+
/// </summary>
18+
Fastest = 1,
19+
20+
/// <summary>
21+
/// A slower (but more accurate) scaling method is employed; the DCT blocks are fully decoded, then a true resampling kernel is applied.
22+
/// </summary>
23+
SpatialLumaScaling = 2,
24+
/// <summary>
25+
/// Like SpatialLumaScaling, but gamma correction is applied before the resampling kernel, then removed afterwards.
26+
/// Has the effect of linear-light scaling
27+
/// </summary>
28+
GammaCorrectSpatialLumaScaling = 6,
29+
Best = 6,
30+
}
31+
32+
public class DecodeCommands
33+
{
34+
public Size? DownscaleHint { get; set; } = Size.Empty;
35+
36+
public DecderDownscalingMode DownscalingMode { get; set; } = DecderDownscalingMode.Unspecified;
37+
38+
public bool DiscardColorProfile { get; set; } = false;
39+
40+
public object[] ToImageflowDynamic()
41+
{
42+
object downscale = DownscaleHint.HasValue ? new {
43+
jpeg_downscale_hints = new {
44+
width = DownscaleHint.Value.Width,
45+
height = DownscaleHint.Value.Height,
46+
scale_luma_spatially = (DownscalingMode | DecderDownscalingMode.SpatialLumaScaling) > 0,
47+
gamma_correct_for_srgb_during_spatial_luma_scaling = DownscalingMode == DecderDownscalingMode.GammaCorrectSpatialLumaScaling
48+
}
49+
}: null;
50+
object ignore = DiscardColorProfile ? new {discard_color_profile = (string) null} : null;
51+
return new object[] {downscale, ignore}.Where(obj => obj != null).ToArray();
52+
}
53+
}
54+
}

src/Imageflow/Fluent/FluentBuildJob.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ internal void AddOutput(int ioId, IOutputDestination destination)
2929
throw new ArgumentException("ioId", $"ioId {ioId} has already been assigned");
3030
_outputs.Add(ioId, destination);
3131
}
32+
[Obsolete("Use Decode(source, ioId, commands) instead")]
3233
public BuildNode DownscalingDecode(IBytesSource source, int ioId, int widthHint, int heightHint, bool scaleLumaSpatially = false, bool gammaCorrectForSrgbDuringSpatialLumaScaling = false)
3334
{
3435
AddInput(ioId, source);
@@ -46,12 +47,34 @@ public BuildNode DownscalingDecode(IBytesSource source, int ioId, int widthHint,
4647
}
4748
});
4849
}
49-
public BuildNode Decode(IBytesSource source, int ioId)
50+
51+
52+
public BuildNode Decode(IBytesSource source, int ioId, DecodeCommands commands)
5053
{
5154
AddInput(ioId, source);
52-
return BuildNode.StartNode(this, new {decode = new {io_id = ioId}});
55+
if (commands == null)
56+
{
57+
return BuildNode.StartNode(this,
58+
new
59+
{
60+
decode = new
61+
{
62+
io_id = ioId
63+
}
64+
});
65+
}
66+
return BuildNode.StartNode(this,
67+
new
68+
{
69+
decode = new
70+
{
71+
io_id = ioId,
72+
commands = commands.ToImageflowDynamic()
73+
}
74+
});
5375
}
54-
76+
77+
public BuildNode Decode(IBytesSource source, int ioId) => Decode(source, ioId, null);
5578
public BuildNode Decode(IBytesSource source) => Decode( source, GenerateIoId());
5679
public BuildNode Decode(ArraySegment<byte> source) => Decode( new BytesSource(source), GenerateIoId());
5780
public BuildNode Decode(byte[] source) => Decode( new BytesSource(source), GenerateIoId());

tests/Imageflow.Test/Imageflow.Test.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1212
</PropertyGroup>
1313
<ItemGroup>
14-
<PackageReference Include="Imageflow.NativeRuntime.ubuntu_14_04-x86_64" Version="1.0.0-rc3a" />
15-
<PackageReference Include="Imageflow.NativeRuntime.win-x86" Version="1.0.0-rc3a" />
16-
<PackageReference Include="Imageflow.NativeRuntime.win-x86_64-sandybridge" Version="1.0.0-rc3a" />
14+
<PackageReference Include="Imageflow.NativeRuntime.ubuntu_14_04-x86_64" Version="1.0.0-rc5a" />
15+
<PackageReference Include="Imageflow.NativeRuntime.win-x86" Version="1.0.0-rc5a" />
16+
<PackageReference Include="Imageflow.NativeRuntime.win-x86_64-sandybridge" Version="1.0.0-rc5a" />
1717
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
1818
<PackageReference Include="xunit" Version="2.2.0" />
1919
<PackageReference Include="xunit.extensibility.execution" Version="2.2.0" />

tests/Imageflow.Test/TestApi.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Drawing;
34
using Xunit;
45
using Imageflow;
56
using System.Dynamic;
@@ -44,7 +45,13 @@ public async Task TestCustomDownscaling()
4445
BuildJobResult r;
4546
using (var b = new FluentBuildJob())
4647
{
47-
r = await b.DownscalingDecode(new BytesSource(imageBytes), 0, 20,20, false, false)
48+
var cmd = new DecodeCommands
49+
{
50+
DownscaleHint = new Size(20, 20),
51+
DownscalingMode = DecderDownscalingMode.Fastest,
52+
DiscardColorProfile = true
53+
};
54+
r = await b.Decode(new BytesSource(imageBytes), 0, cmd)
4855
.Distort(30, 20, 50.0f, InterpolationFilter.RobidouxFast, InterpolationFilter.Cubic)
4956
.ConstrainWithin(5, 5)
5057
.EncodeToBytes(new LibPngEncoder()).FinishAsync();

0 commit comments

Comments
 (0)