Browse Source

Fix To() Method to allow vector scaling.

af/merge-core
James Jackson-South 9 years ago
parent
commit
07595c3d57
  1. 16
      src/ImageSharp/Image/Image.cs
  2. 17
      src/ImageSharp/Image/ImageFrame.cs
  3. 5
      tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

16
src/ImageSharp/Image/Image.cs

@ -10,6 +10,7 @@ namespace ImageSharp
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
@ -260,14 +261,25 @@ namespace ImageSharp
/// <summary>
/// Returns a copy of the image in the given pixel format.
/// <remarks>
/// Most color formats when converted to vectors have a range of <value>0</value> to <value>1</value>. Some however, <see cref="NormalizedByte4"/>, for example scale between
/// <value>-1</value> to <value>1</value>. This requires additional computation to convert between the formats.
/// For example, if I wanted to convert from <see cref="Color"/> to <see cref="NormalizedByte4"/> the following function would be required. <example>v => (2F * v) - Vector4.One</example>
/// </remarks>
/// </summary>
/// <param name="scaleFunc">A function that allows for the correction of vector scaling between color formats.</param>
/// <typeparam name="TColor2">The pixel format.</typeparam>
/// <typeparam name="TPacked2">The packed format. <example>uint, long, float.</example></typeparam>
/// <returns>The <see cref="Image{TColor2, TPacked2}"/></returns>
public Image<TColor2, TPacked2> To<TColor2, TPacked2>()
public Image<TColor2, TPacked2> To<TColor2, TPacked2>(Func<Vector4, Vector4> scaleFunc = null)
where TColor2 : struct, IPackedPixel<TPacked2>
where TPacked2 : struct
{
if (scaleFunc == null)
{
scaleFunc = v => v;
}
Image<TColor2, TPacked2> target = new Image<TColor2, TPacked2>(this.Width, this.Height)
{
Quality = this.Quality,
@ -290,7 +302,7 @@ namespace ImageSharp
for (int x = 0; x < target.Width; x++)
{
TColor2 color = default(TColor2);
color.PackFromVector4(pixels[x, y].ToVector4());
color.PackFromVector4(scaleFunc(pixels[x, y].ToVector4()));
targetPixels[x, y] = color;
}
});

17
src/ImageSharp/Image/ImageFrame.cs

@ -5,6 +5,8 @@
namespace ImageSharp
{
using System;
using System.Numerics;
using System.Threading.Tasks;
/// <summary>
@ -40,14 +42,25 @@ namespace ImageSharp
/// <summary>
/// Returns a copy of the image frame in the given pixel format.
/// <remarks>
/// Most color formats when converted to vectors have a range of <value>0</value> to <value>1</value>. Some however, <see cref="NormalizedByte4"/>, for example scale between
/// <value>-1</value> to <value>1</value>. This requires additional computation to convert between the formats.
/// For example, if I wanted to convert from <see cref="Color"/> to <see cref="NormalizedByte4"/> the following function would be required. <example>v => (2F * v) - Vector4.One</example>
/// </remarks>
/// </summary>
/// <param name="scaleFunc">A function that allows for the correction of vector scaling between color formats.</param>
/// <typeparam name="TColor2">The pixel format.</typeparam>
/// <typeparam name="TPacked2">The packed format. <example>uint, long, float.</example></typeparam>
/// <returns>The <see cref="ImageFrame{TColor2, TPacked2}"/></returns>
public ImageFrame<TColor2, TPacked2> To<TColor2, TPacked2>()
public ImageFrame<TColor2, TPacked2> To<TColor2, TPacked2>(Func<Vector4, Vector4> scaleFunc = null)
where TColor2 : struct, IPackedPixel<TPacked2>
where TPacked2 : struct
{
if (scaleFunc == null)
{
scaleFunc = v => v;
}
ImageFrame<TColor2, TPacked2> target = new ImageFrame<TColor2, TPacked2>
{
Quality = this.Quality,
@ -68,7 +81,7 @@ namespace ImageSharp
for (int x = 0; x < target.Width; x++)
{
TColor2 color = default(TColor2);
color.PackFromVector4(pixels[x, y].ToVector4());
color.PackFromVector4(scaleFunc(pixels[x, y].ToVector4()));
targetPixels[x, y] = color;
}
});

5
tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

@ -7,6 +7,7 @@ namespace ImageSharp.Tests
{
using System;
using System.IO;
using System.Numerics;
using Xunit;
@ -62,8 +63,8 @@ namespace ImageSharp.Tests
// Image<HalfVector4, ulong> image = file.CreateImage().To<HalfVector4, ulong>();
// Image<NormalizedByte2, ushort> image = file.CreateImage().To<NormalizedByte2, ushort>();
// TODO: Conversion between types who's vector ranges are different are not possible.
// Image<NormalizedByte4, uint> image = file.CreateImage().To<NormalizedByte4, uint>();
// TODO: Conversion between types who's vector ranges are different are not possible without scaling function, Make static version of known ones.
// Image<NormalizedByte4, uint> image = file.CreateImage().To<NormalizedByte4, uint>(v => (2F * v) - Vector4.One);
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
{

Loading…
Cancel
Save