Browse Source

Sanitize ColorMatrix filters

Former-commit-id: b26dae4a4bbe620476ad334e7e4b6eb9eb2d4625
Former-commit-id: 3a71a08ef4665c7b677153c7df2e9c41926e0b10
Former-commit-id: 1a1eec21f2fe5e15cac7afd8f9c441d7d207ce01
af/merge-core
James Jackson-South 10 years ago
parent
commit
48fb3880b9
  1. 41
      src/ImageProcessorCore/Filters/Processors/ColorMatrix/ColorMatrixFilter.cs
  2. 2
      src/ImageProcessorCore/Filters/Processors/ColorMatrix/LomographProcessor.cs
  3. 4
      src/ImageProcessorCore/Filters/Processors/ColorMatrix/PolaroidProcessor.cs
  4. 24
      tests/ImageProcessorCore.Tests/Processors/Filters/LomographTest.cs

41
src/ImageProcessorCore/Filters/Processors/ColorMatrix/ColorMatrixFilter.cs

@ -5,6 +5,7 @@
namespace ImageProcessorCore.Processors
{
using System;
using System.Numerics;
using System.Threading.Tasks;
@ -28,6 +29,24 @@ namespace ImageProcessorCore.Processors
{
int startX = sourceRectangle.X;
int endX = sourceRectangle.Right;
// Align start/end positions.
int minX = Math.Max(0, startX);
int maxX = Math.Min(source.Width, endX);
int minY = Math.Max(0, startY);
int maxY = Math.Min(source.Height, endY);
// Reset offset if necessary.
if (minX > 0)
{
startX = 0;
}
if (minY > 0)
{
startY = 0;
}
Matrix4x4 matrix = this.Matrix;
bool compand = this.Compand;
@ -35,18 +54,20 @@ namespace ImageProcessorCore.Processors
using (IPixelAccessor<T, TP> targetPixels = target.Lock())
{
Parallel.For(
startY,
endY,
this.ParallelOptions,
y =>
{
for (int x = startX; x < endX; x++)
minY,
maxY,
this.ParallelOptions,
y =>
{
targetPixels[x, y] = this.ApplyMatrix(sourcePixels[x, y], matrix, compand);
}
int offsetY = y - startY;
for (int x = minX; x < maxX; x++)
{
int offsetX = x - startX;
targetPixels[offsetX, offsetY] = this.ApplyMatrix(sourcePixels[offsetX, offsetY], matrix, compand);
}
this.OnRowProcessed();
});
this.OnRowProcessed();
});
}
}

2
src/ImageProcessorCore/Filters/Processors/ColorMatrix/LomographProcessor.cs

@ -32,7 +32,7 @@ namespace ImageProcessorCore.Processors
{
T packed = default(T);
packed.PackFromBytes(0, 10, 0, 255); // Very dark (mostly black) lime green.
new VignetteProcessor<T, TP> { VignetteColor = packed }.Apply(target, target, targetRectangle);
new VignetteProcessor<T, TP> { VignetteColor = packed }.Apply(target, target, sourceRectangle);
}
}
}

4
src/ImageProcessorCore/Filters/Processors/ColorMatrix/PolaroidProcessor.cs

@ -38,11 +38,11 @@ namespace ImageProcessorCore.Processors
{
T packedV = default(T);
packedV.PackFromBytes(102, 34, 0, 255); // Very dark orange [Brown tone]
new VignetteProcessor<T, TP> { VignetteColor = packedV }.Apply(target, target, targetRectangle);
new VignetteProcessor<T, TP> { VignetteColor = packedV }.Apply(target, target, sourceRectangle);
T packedG = default(T);
packedG.PackFromBytes(255, 153, 102, 178); // Light orange
new GlowProcessor<T, TP> { GlowColor = packedG, Radius = target.Width / 4F }.Apply(target, target, targetRectangle);
new GlowProcessor<T, TP> { GlowColor = packedG, Radius = target.Width / 4F }.Apply(target, target, sourceRectangle);
}
}
}

24
tests/ImageProcessorCore.Tests/Processors/Filters/LomographTest.cs

@ -34,5 +34,29 @@ namespace ImageProcessorCore.Tests
}
}
}
[Fact]
public void ImageShouldApplyLomographFilterInBox()
{
const string path = "TestOutput/Lomograph";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
foreach (string file in Files)
{
using (FileStream stream = File.OpenRead(file))
{
string filename = Path.GetFileNameWithoutExtension(file) + "-InBox" + Path.GetExtension(file);
Image image = new Image(stream);
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Lomograph(new Rectangle(image.Width / 4, image.Width / 4, image.Width / 2, image.Height / 2))
.Save(output);
}
}
}
}
}
}
Loading…
Cancel
Save