Browse Source

CheckDimensions

af/merge-core
Anton Firszov 9 years ago
parent
commit
f8d5742450
  1. 45
      src/ImageSharp/Formats/Jpg/Utils/JpegUtils.cs
  2. 4
      src/ImageSharp/Image/PixelAccessor{TColor}.cs

45
src/ImageSharp/Formats/Jpg/Utils/JpegUtils.cs

@ -13,12 +13,9 @@ namespace ImageSharp.Formats.Jpg
internal static unsafe class JpegUtils
{
/// <summary>
/// Copy a region of an image into dest. De "outlier" area will be stretched out with pixels on the right and bottom of
/// the image.
/// Copy a region of an image into dest. De "outlier" area will be stretched out with pixels on the right and bottom of the image.
/// </summary>
/// <typeparam name="TColor">
/// The pixel type
/// </typeparam>
/// <typeparam name="TColor">The pixel type</typeparam>
/// <param name="pixels">The input pixel acessor</param>
/// <param name="dest">The destination <see cref="PixelArea{TColor}"/></param>
/// <param name="sourceY">Starting Y coord</param>
@ -30,27 +27,57 @@ namespace ImageSharp.Formats.Jpg
int sourceX)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
pixels.CopyTo(dest, sourceY, sourceX);
pixels.UncheckedCopyTo(dest, sourceY, sourceX);
int stretchFromX = pixels.Width - sourceX;
int stretchFromY = pixels.Height - sourceY;
StretchPixels(dest, stretchFromX, stretchFromY);
}
/// <summary>
/// Copy a region of image into the image destination area. Does not throw when requesting a 0-size copy.
/// </summary>
/// <typeparam name="TColor">The pixel type</typeparam>
/// <param name="sourcePixels">The source <see cref="PixelAccessor{TColor}"/> </param>
/// <param name="destinationArea">The destination area.</param>
/// <param name="sourceY">The source row index.</param>
/// <param name="sourceX">The source column index.</param>
/// <exception cref="NotSupportedException">
/// Thrown when an unsupported component order value is passed.
/// </exception>
public static void UncheckedCopyTo<TColor>(
this PixelAccessor<TColor> sourcePixels,
PixelArea<TColor> destinationArea,
int sourceY,
int sourceX)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
// TODO: Code smell! This is exactly the same code PixelArea<TColor>.CopyTo() starts with!
int width = Math.Min(destinationArea.Width, sourcePixels.Width - sourceX);
int height = Math.Min(destinationArea.Height, sourcePixels.Height - sourceY);
if (width < 1 || height < 1)
{
return;
}
sourcePixels.CopyTo(destinationArea, sourceY, sourceX);
}
/// <summary>
/// Copy an RGB value
/// </summary>
/// <param name="source">Source pointer</param>
/// <param name="dest">Destination pointer</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void CopyRgb(byte* source, byte* dest)
public static void CopyRgb(byte* source, byte* dest)
{
*dest++ = *source++; // R
*dest++ = *source++; // G
*dest = *source; // B
}
// Nothing to stretch if (fromX, fromY) is outside the area, or is at (0,0)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsInvalidStretchArea<TColor>(PixelArea<TColor> area, int fromX, int fromY)
private static bool IsInvalidStretchStartingPosition<TColor>(PixelArea<TColor> area, int fromX, int fromY)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
return fromX <= 0 || fromY <= 0 || fromX >= area.Width || fromY >= area.Height;
@ -59,7 +86,7 @@ namespace ImageSharp.Formats.Jpg
private static void StretchPixels<TColor>(PixelArea<TColor> area, int fromX, int fromY)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
if (IsInvalidStretchArea(area, fromX, fromY))
if (IsInvalidStretchStartingPosition(area, fromX, fromY))
{
return;
}

4
src/ImageSharp/Image/PixelAccessor{TColor}.cs

@ -186,7 +186,7 @@ namespace ImageSharp
int width = Math.Min(area.Width, this.Width - targetX);
int height = Math.Min(area.Height, this.Height - targetY);
// this.CheckDimensions(width, height); TODO: Why was width == 0 or height == 0 considered a problem? Copy implementations do not fail on this (just do nothing)!
this.CheckDimensions(width, height);
switch (area.ComponentOrder)
{
case ComponentOrder.ZYX:
@ -220,7 +220,7 @@ namespace ImageSharp
int width = Math.Min(area.Width, this.Width - sourceX);
int height = Math.Min(area.Height, this.Height - sourceY);
// this.CheckDimensions(width, height); TODO: Why was width == 0 or height == 0 considered a problem? Copy implementations do not fail on this (just do nothing)!
this.CheckDimensions(width, height);
switch (area.ComponentOrder)
{
case ComponentOrder.ZYX:

Loading…
Cancel
Save