Browse Source

Add new To<TColor,TPacked> method

af/merge-core
James Jackson-South 9 years ago
parent
commit
d90351341f
  1. 58
      src/ImageSharp/Image/Image.cs
  2. 51
      src/ImageSharp/Image/ImageFrame.cs
  3. 1
      tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

58
src/ImageSharp/Image/Image.cs

@ -11,6 +11,7 @@ namespace ImageSharp
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Formats;
@ -245,7 +246,7 @@ namespace ImageSharp
/// <summary>
/// Returns a Base64 encoded string from the given image.
/// </summary>
/// <example>data:image/gif;base64,R0lGODlhAQABAIABAEdJRgAAACwAAAAAAQABAAACAkQBAA==</example>
/// <example><see href="data:image/gif;base64,R0lGODlhAQABAIABAEdJRgAAACwAAAAAAQABAAACAkQBAA=="/></example>
/// <returns>The <see cref="string"/></returns>
public string ToBase64String()
{
@ -257,6 +258,57 @@ namespace ImageSharp
}
}
/// <summary>
/// Returns a copy of the image in the given pixel format.
/// </summary>
/// <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>()
where TColor2 : struct, IPackedPixel<TPacked2>
where TPacked2 : struct
{
Image<TColor2, TPacked2> target = new Image<TColor2, TPacked2>(this.Width, this.Height)
{
Quality = this.Quality,
FrameDelay = this.FrameDelay,
HorizontalResolution = this.HorizontalResolution,
VerticalResolution = this.VerticalResolution,
CurrentImageFormat = this.CurrentImageFormat,
RepeatCount = this.RepeatCount
};
using (PixelAccessor<TColor, TPacked> pixels = this.Lock())
using (PixelAccessor<TColor2, TPacked2> targetPixels = target.Lock())
{
Parallel.For(
0,
target.Height,
Bootstrapper.Instance.ParallelOptions,
y =>
{
for (int x = 0; x < target.Width; x++)
{
TColor2 color = default(TColor2);
color.PackFromVector4(pixels[x, y].ToVector4());
targetPixels[x, y] = color;
}
});
}
if (this.ExifProfile != null)
{
target.ExifProfile = new ExifProfile(this.ExifProfile);
}
foreach (ImageFrame<TColor, TPacked> frame in this.Frames)
{
target.Frames.Add(frame.To<TColor2, TPacked2>());
}
return target;
}
/// <summary>
/// Copies the properties from the other <see cref="Image{TColor, TPacked}"/>.
/// </summary>
@ -278,6 +330,10 @@ namespace ImageSharp
}
}
/// <summary>
/// Creates a new <see cref="ImageFrame{TColor,TPacked}"/> from this instance
/// </summary>
/// <returns>The <see cref="ImageFrame{TColor,TPacked}"/></returns>
internal virtual ImageFrame<TColor, TPacked> ToFrame()
{
return new ImageFrame<TColor, TPacked>(this);

51
src/ImageSharp/Image/ImageFrame.cs

@ -5,6 +5,8 @@
namespace ImageSharp
{
using System.Threading.Tasks;
/// <summary>
/// Represents a single frame in a animation.
/// </summary>
@ -24,9 +26,7 @@ namespace ImageSharp
/// <summary>
/// Initializes a new instance of the <see cref="ImageFrame{TColor, TPacked}"/> class.
/// </summary>
/// <param name="image">
/// The image to create the frame from.
/// </param>
/// <param name="image">The image to create the frame from.</param>
public ImageFrame(ImageBase<TColor, TPacked> image)
: base(image)
{
@ -38,9 +38,52 @@ namespace ImageSharp
return $"ImageFrame: {this.Width}x{this.Height}";
}
/// <summary>
/// Returns a copy of the image frame in the given pixel format.
/// </summary>
/// <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>()
where TColor2 : struct, IPackedPixel<TPacked2>
where TPacked2 : struct
{
ImageFrame<TColor2, TPacked2> target = new ImageFrame<TColor2, TPacked2>
{
Quality = this.Quality,
FrameDelay = this.FrameDelay
};
target.InitPixels(this.Width, this.Height);
using (PixelAccessor<TColor, TPacked> pixels = this.Lock())
using (PixelAccessor<TColor2, TPacked2> targetPixels = target.Lock())
{
Parallel.For(
0,
target.Height,
Bootstrapper.Instance.ParallelOptions,
y =>
{
for (int x = 0; x < target.Width; x++)
{
TColor2 color = default(TColor2);
color.PackFromVector4(pixels[x, y].ToVector4());
targetPixels[x, y] = color;
}
});
}
return target;
}
/// <summary>
/// Clones the current instance.
/// </summary>
/// <returns>The <see cref="ImageFrame{TColor, TPacked}"/></returns>
internal virtual ImageFrame<TColor, TPacked> Clone()
{
return new ImageFrame<TColor, TPacked>(this);
}
}
}
}

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

@ -52,6 +52,7 @@ namespace ImageSharp.Tests
foreach (TestFile file in Files)
{
Image image = file.CreateImage();
// Image<Bgr565, ushort> image = file.CreateImage().To<Bgr565, ushort>();
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
{

Loading…
Cancel
Save