Skip to content

Commit a42e30f

Browse files
Merge pull request #1062 from SixLabors/js/smart-non-generic-api
Add La16 and La32 IPixel formats.
2 parents 9275555 + 8395e9c commit a42e30f

File tree

269 files changed

+3907
-918
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

269 files changed

+3907
-918
lines changed

src/ImageSharp/Advanced/AotCompilerTools.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ static AotCompilerTools()
4040
/// </summary>
4141
private static void SeedEverything()
4242
{
43-
Seed<Alpha8>();
43+
Seed<A8>();
4444
Seed<Argb32>();
4545
Seed<Bgr24>();
4646
Seed<Bgr565>();
4747
Seed<Bgra32>();
4848
Seed<Bgra4444>();
4949
Seed<Bgra5551>();
5050
Seed<Byte4>();
51-
Seed<Gray16>();
52-
Seed<Gray8>();
51+
Seed<L16>();
52+
Seed<L8>();
53+
Seed<La16>();
54+
Seed<La32>();
5355
Seed<HalfSingle>();
5456
Seed<HalfVector2>();
5557
Seed<HalfVector4>();

src/ImageSharp/Common/Helpers/ImageMaths.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static int GetBT709Luminance(ref Vector4 vector, int luminanceLevels)
3838
/// <returns>The <see cref="byte"/>.</returns>
3939
[MethodImpl(InliningOptions.ShortMethod)]
4040
public static byte Get8BitBT709Luminance(byte r, byte g, byte b) =>
41-
(byte)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5f);
41+
(byte)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F);
4242

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

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

6565
/// <summary>
6666
/// Scales a value from a 16 bit <see cref="ushort"/> to it's 8 bit <see cref="byte"/> equivalent.

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ private void ReadInfoHeader()
13211321
this.metadata = meta;
13221322

13231323
short bitsPerPixel = this.infoHeader.BitsPerPixel;
1324-
this.bmpMetadata = this.metadata.GetFormatMetadata(BmpFormat.Instance);
1324+
this.bmpMetadata = this.metadata.GetBmpMetadata();
13251325
this.bmpMetadata.InfoHeaderType = infoHeaderType;
13261326

13271327
// We can only encode at these bit rates so far (1 bit and 4 bit are still missing).

src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
105105

106106
this.configuration = image.GetConfiguration();
107107
ImageMetadata metadata = image.Metadata;
108-
BmpMetadata bmpMetadata = metadata.GetFormatMetadata(BmpFormat.Instance);
108+
BmpMetadata bmpMetadata = metadata.GetBmpMetadata();
109109
this.bitsPerPixel = this.bitsPerPixel ?? bmpMetadata.BitsPerPixel;
110110

111111
short bpp = (short)this.bitsPerPixel;
@@ -315,11 +315,11 @@ private void Write16Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
315315
private void Write8Bit<TPixel>(Stream stream, ImageFrame<TPixel> image)
316316
where TPixel : struct, IPixel<TPixel>
317317
{
318-
bool isGray8 = typeof(TPixel) == typeof(Gray8);
318+
bool isL8 = typeof(TPixel) == typeof(L8);
319319
using (IMemoryOwner<byte> colorPaletteBuffer = this.memoryAllocator.AllocateManagedByteBuffer(ColorPaletteSize8Bit, AllocationOptions.Clean))
320320
{
321321
Span<byte> colorPalette = colorPaletteBuffer.GetSpan();
322-
if (isGray8)
322+
if (isL8)
323323
{
324324
this.Write8BitGray(stream, image, colorPalette);
325325
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using SixLabors.ImageSharp.Formats.Bmp;
5+
using SixLabors.ImageSharp.Metadata;
6+
7+
namespace SixLabors.ImageSharp
8+
{
9+
/// <summary>
10+
/// Extension methods for the <see cref="ImageMetadata"/> type.
11+
/// </summary>
12+
public static partial class MetadataExtensions
13+
{
14+
/// <summary>
15+
/// Gets the bmp format specific metadata for the image.
16+
/// </summary>
17+
/// <param name="metadata">The metadata this method extends.</param>
18+
/// <returns>The <see cref="BmpMetadata"/>.</returns>
19+
public static BmpMetadata GetBmpMetadata(this ImageMetadata metadata) => metadata.GetFormatMetadata(BmpFormat.Instance);
20+
}
21+
}

src/ImageSharp/Formats/Gif/GifDecoderCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ private void RestoreToBackground<TPixel>(ImageFrame<TPixel> frame)
553553
[MethodImpl(MethodImplOptions.AggressiveInlining)]
554554
private void SetFrameMetadata(ImageFrameMetadata meta)
555555
{
556-
GifFrameMetadata gifMeta = meta.GetFormatMetadata(GifFormat.Instance);
556+
GifFrameMetadata gifMeta = meta.GetGifMetadata();
557557
if (this.graphicsControlExtension.DelayTime > 0)
558558
{
559559
gifMeta.FrameDelay = this.graphicsControlExtension.DelayTime;
@@ -615,7 +615,7 @@ private void ReadLogicalScreenDescriptorAndGlobalColorTable(Stream stream)
615615
}
616616

617617
this.metadata = meta;
618-
this.gifMetadata = meta.GetFormatMetadata(GifFormat.Instance);
618+
this.gifMetadata = meta.GetGifMetadata();
619619
this.gifMetadata.ColorTableMode = this.logicalScreenDescriptor.GlobalColorTableFlag
620620
? GifColorTableMode.Global
621621
: GifColorTableMode.Local;

src/ImageSharp/Formats/Gif/GifEncoderCore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
7878
this.configuration = image.GetConfiguration();
7979

8080
ImageMetadata metadata = image.Metadata;
81-
GifMetadata gifMetadata = metadata.GetFormatMetadata(GifFormat.Instance);
81+
GifMetadata gifMetadata = metadata.GetGifMetadata();
8282
this.colorTableMode = this.colorTableMode ?? gifMetadata.ColorTableMode;
8383
bool useGlobalTable = this.colorTableMode == GifColorTableMode.Global;
8484

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

@@ -166,7 +166,7 @@ private void EncodeLocal<TPixel>(Image<TPixel> image, IQuantizedFrame<TPixel> qu
166166
foreach (ImageFrame<TPixel> frame in image.Frames)
167167
{
168168
ImageFrameMetadata metadata = frame.Metadata;
169-
GifFrameMetadata frameMetadata = metadata.GetFormatMetadata(GifFormat.Instance);
169+
GifFrameMetadata frameMetadata = metadata.GetGifMetadata();
170170
if (quantized is null)
171171
{
172172
// Allow each frame to be encoded at whatever color depth the frame designates if set.

0 commit comments

Comments
 (0)