Skip to content

Commit

Permalink
Merge pull request #1062 from SixLabors/js/smart-non-generic-api
Browse files Browse the repository at this point in the history
Add La16 and La32 IPixel formats.
  • Loading branch information
JimBobSquarePants authored Dec 15, 2019
2 parents 9275555 + 8395e9c commit a42e30f
Show file tree
Hide file tree
Showing 269 changed files with 3,907 additions and 918 deletions.
8 changes: 5 additions & 3 deletions src/ImageSharp/Advanced/AotCompilerTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ static AotCompilerTools()
/// </summary>
private static void SeedEverything()
{
Seed<Alpha8>();
Seed<A8>();
Seed<Argb32>();
Seed<Bgr24>();
Seed<Bgr565>();
Seed<Bgra32>();
Seed<Bgra4444>();
Seed<Bgra5551>();
Seed<Byte4>();
Seed<Gray16>();
Seed<Gray8>();
Seed<L16>();
Seed<L8>();
Seed<La16>();
Seed<La32>();
Seed<HalfSingle>();
Seed<HalfVector2>();
Seed<HalfVector4>();
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/Common/Helpers/ImageMaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static int GetBT709Luminance(ref Vector4 vector, int luminanceLevels)
/// <returns>The <see cref="byte"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static byte Get8BitBT709Luminance(byte r, byte g, byte b) =>
(byte)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5f);
(byte)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F);

/// <summary>
/// Gets the luminance from the rgb components using the formula as specified by ITU-R Recommendation BT.709.
Expand All @@ -49,7 +49,7 @@ public static byte Get8BitBT709Luminance(byte r, byte g, byte b) =>
/// <returns>The <see cref="ushort"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static ushort Get16BitBT709Luminance(ushort r, ushort g, ushort b) =>
(ushort)((r * .2126F) + (g * .7152F) + (b * .0722F));
(ushort)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F);

/// <summary>
/// Gets the luminance from the rgb components using the formula as specified by ITU-R Recommendation BT.709.
Expand All @@ -60,7 +60,7 @@ public static ushort Get16BitBT709Luminance(ushort r, ushort g, ushort b) =>
/// <returns>The <see cref="ushort"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static ushort Get16BitBT709Luminance(float r, float g, float b) =>
(ushort)((r * .2126F) + (g * .7152F) + (b * .0722F));
(ushort)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F);

/// <summary>
/// Scales a value from a 16 bit <see cref="ushort"/> to it's 8 bit <see cref="byte"/> equivalent.
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ private void ReadInfoHeader()
this.metadata = meta;

short bitsPerPixel = this.infoHeader.BitsPerPixel;
this.bmpMetadata = this.metadata.GetFormatMetadata(BmpFormat.Instance);
this.bmpMetadata = this.metadata.GetBmpMetadata();
this.bmpMetadata.InfoHeaderType = infoHeaderType;

// We can only encode at these bit rates so far (1 bit and 4 bit are still missing).
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)

this.configuration = image.GetConfiguration();
ImageMetadata metadata = image.Metadata;
BmpMetadata bmpMetadata = metadata.GetFormatMetadata(BmpFormat.Instance);
BmpMetadata bmpMetadata = metadata.GetBmpMetadata();
this.bitsPerPixel = this.bitsPerPixel ?? bmpMetadata.BitsPerPixel;

short bpp = (short)this.bitsPerPixel;
Expand Down Expand Up @@ -315,11 +315,11 @@ private void Write16Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
private void Write8Bit<TPixel>(Stream stream, ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
bool isGray8 = typeof(TPixel) == typeof(Gray8);
bool isL8 = typeof(TPixel) == typeof(L8);
using (IMemoryOwner<byte> colorPaletteBuffer = this.memoryAllocator.AllocateManagedByteBuffer(ColorPaletteSize8Bit, AllocationOptions.Clean))
{
Span<byte> colorPalette = colorPaletteBuffer.GetSpan();
if (isGray8)
if (isL8)
{
this.Write8BitGray(stream, image, colorPalette);
}
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions src/ImageSharp/Formats/Bmp/MetadataExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Metadata;

namespace SixLabors.ImageSharp
{
/// <summary>
/// Extension methods for the <see cref="ImageMetadata"/> type.
/// </summary>
public static partial class MetadataExtensions
{
/// <summary>
/// Gets the bmp format specific metadata for the image.
/// </summary>
/// <param name="metadata">The metadata this method extends.</param>
/// <returns>The <see cref="BmpMetadata"/>.</returns>
public static BmpMetadata GetBmpMetadata(this ImageMetadata metadata) => metadata.GetFormatMetadata(BmpFormat.Instance);
}
}
4 changes: 2 additions & 2 deletions src/ImageSharp/Formats/Gif/GifDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ private void RestoreToBackground<TPixel>(ImageFrame<TPixel> frame)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetFrameMetadata(ImageFrameMetadata meta)
{
GifFrameMetadata gifMeta = meta.GetFormatMetadata(GifFormat.Instance);
GifFrameMetadata gifMeta = meta.GetGifMetadata();
if (this.graphicsControlExtension.DelayTime > 0)
{
gifMeta.FrameDelay = this.graphicsControlExtension.DelayTime;
Expand Down Expand Up @@ -615,7 +615,7 @@ private void ReadLogicalScreenDescriptorAndGlobalColorTable(Stream stream)
}

this.metadata = meta;
this.gifMetadata = meta.GetFormatMetadata(GifFormat.Instance);
this.gifMetadata = meta.GetGifMetadata();
this.gifMetadata.ColorTableMode = this.logicalScreenDescriptor.GlobalColorTableFlag
? GifColorTableMode.Global
: GifColorTableMode.Local;
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/Formats/Gif/GifEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
this.configuration = image.GetConfiguration();

ImageMetadata metadata = image.Metadata;
GifMetadata gifMetadata = metadata.GetFormatMetadata(GifFormat.Instance);
GifMetadata gifMetadata = metadata.GetGifMetadata();
this.colorTableMode = this.colorTableMode ?? gifMetadata.ColorTableMode;
bool useGlobalTable = this.colorTableMode == GifColorTableMode.Global;

Expand Down Expand Up @@ -136,7 +136,7 @@ private void EncodeGlobal<TPixel>(Image<TPixel> image, IQuantizedFrame<TPixel> q
{
ImageFrame<TPixel> frame = image.Frames[i];
ImageFrameMetadata metadata = frame.Metadata;
GifFrameMetadata frameMetadata = metadata.GetFormatMetadata(GifFormat.Instance);
GifFrameMetadata frameMetadata = metadata.GetGifMetadata();
this.WriteGraphicalControlExtension(frameMetadata, transparencyIndex, stream);
this.WriteImageDescriptor(frame, false, stream);

Expand Down Expand Up @@ -166,7 +166,7 @@ private void EncodeLocal<TPixel>(Image<TPixel> image, IQuantizedFrame<TPixel> qu
foreach (ImageFrame<TPixel> frame in image.Frames)
{
ImageFrameMetadata metadata = frame.Metadata;
GifFrameMetadata frameMetadata = metadata.GetFormatMetadata(GifFormat.Instance);
GifFrameMetadata frameMetadata = metadata.GetGifMetadata();
if (quantized is null)
{
// Allow each frame to be encoded at whatever color depth the frame designates if set.
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions src/ImageSharp/Formats/Gif/MetadataExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Metadata;

namespace SixLabors.ImageSharp
{
/// <summary>
/// Extension methods for the <see cref="ImageMetadata"/> type.
/// </summary>
public static partial class MetadataExtensions
{
/// <summary>
/// Gets the gif format specific metadata for the image.
/// </summary>
/// <param name="metadata">The metadata this method extends.</param>
/// <returns>The <see cref="GifMetadata"/>.</returns>
public static GifMetadata GetGifMetadata(this ImageMetadata metadata) => metadata.GetFormatMetadata(GifFormat.Instance);

/// <summary>
/// Gets the gif format specific metadata for the image frame.
/// </summary>
/// <param name="metadata">The metadata this method extends.</param>
/// <returns>The <see cref="GifFrameMetadata"/>.</returns>
public static GifFrameMetadata GetGifMetadata(this ImageFrameMetadata metadata) => metadata.GetFormatMetadata(GifFormat.Instance);
}
}
7 changes: 3 additions & 4 deletions src/ImageSharp/Formats/IImageDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System.IO;
Expand All @@ -17,17 +17,16 @@ public interface IImageDecoder
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The decoded image of a given pixel type.</returns>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel>;

/// <summary>
/// Decodes the image from the specified stream to an <see cref="Image"/>.
/// The decoder is free to choose the pixel type.
/// </summary>
/// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The decoded image of a pixel type chosen by the decoder.</returns>
/// <returns>The <see cref="Image"/>.</returns>
Image Decode(Configuration configuration, Stream stream);
}
}
11 changes: 6 additions & 5 deletions src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System.IO;
Expand Down Expand Up @@ -28,6 +28,10 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
}
}

/// <inheritdoc />
public Image Decode(Configuration configuration, Stream stream)
=> this.Decode<Rgba32>(configuration, stream);

/// <inheritdoc/>
public IImageInfo Identify(Configuration configuration, Stream stream)
{
Expand All @@ -38,8 +42,5 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
return decoder.Identify(stream);
}
}

/// <inheritdoc />
public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream);
}
}
}
59 changes: 31 additions & 28 deletions src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,48 +649,51 @@ private void ProcessDefineQuantizationTablesMarker(int remaining)
switch (quantizationTableSpec >> 4)
{
case 0:
{
// 8 bit values
if (remaining < 64)
{
// 8 bit values
if (remaining < 64)
{
done = true;
break;
}
done = true;
break;
}

this.InputStream.Read(this.temp, 0, 64);
remaining -= 64;
this.InputStream.Read(this.temp, 0, 64);
remaining -= 64;

ref Block8x8F table = ref this.QuantizationTables[tableIndex];
for (int j = 0; j < 64; j++)
{
table[j] = this.temp[j];
}
ref Block8x8F table = ref this.QuantizationTables[tableIndex];
for (int j = 0; j < 64; j++)
{
table[j] = this.temp[j];
}
}

break;
break;
case 1:
{
// 16 bit values
if (remaining < 128)
{
// 16 bit values
if (remaining < 128)
{
done = true;
break;
}
done = true;
break;
}

this.InputStream.Read(this.temp, 0, 128);
remaining -= 128;
this.InputStream.Read(this.temp, 0, 128);
remaining -= 128;

ref Block8x8F table = ref this.QuantizationTables[tableIndex];
for (int j = 0; j < 64; j++)
{
table[j] = (this.temp[2 * j] << 8) | this.temp[(2 * j) + 1];
}
ref Block8x8F table = ref this.QuantizationTables[tableIndex];
for (int j = 0; j < 64; j++)
{
table[j] = (this.temp[2 * j] << 8) | this.temp[(2 * j) + 1];
}
}

break;

break;
default:
{
JpegThrowHelper.ThrowBadQuantizationTable();
break;
}
}

if (done)
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
ImageMetadata metadata = image.Metadata;

// System.Drawing produces identical output for jpegs with a quality parameter of 0 and 1.
int qlty = (this.quality ?? metadata.GetFormatMetadata(JpegFormat.Instance).Quality).Clamp(1, 100);
int qlty = (this.quality ?? metadata.GetJpegMetadata().Quality).Clamp(1, 100);
this.subsample = this.subsample ?? (qlty >= 91 ? JpegSubsample.Ratio444 : JpegSubsample.Ratio420);

// Convert from a quality rating to a scaling factor.
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions src/ImageSharp/Formats/Jpeg/MetadataExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Metadata;

namespace SixLabors.ImageSharp
{
/// <summary>
/// Extension methods for the <see cref="ImageMetadata"/> type.
/// </summary>
public static partial class MetadataExtensions
{
/// <summary>
/// Gets the jpeg format specific metadata for the image.
/// </summary>
/// <param name="metadata">The metadata this method extends.</param>
/// <returns>The <see cref="JpegMetadata"/>.</returns>
public static JpegMetadata GetJpegMetadata(this ImageMetadata metadata) => metadata.GetFormatMetadata(JpegFormat.Instance);
}
}
21 changes: 21 additions & 0 deletions src/ImageSharp/Formats/Png/MetadataExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Metadata;

namespace SixLabors.ImageSharp
{
/// <summary>
/// Extension methods for the <see cref="ImageMetadata"/> type.
/// </summary>
public static partial class MetadataExtensions
{
/// <summary>
/// Gets the png format specific metadata for the image.
/// </summary>
/// <param name="metadata">The metadata this method extends.</param>
/// <returns>The <see cref="PngMetadata"/>.</returns>
public static PngMetadata GetPngMetadata(this ImageMetadata metadata) => metadata.GetFormatMetadata(PngFormat.Instance);
}
}
Loading

0 comments on commit a42e30f

Please sign in to comment.