Skip to content

Commit a06511f

Browse files
Initialize pixel type info from decoder.
1 parent 9a0e289 commit a06511f

37 files changed

Lines changed: 306 additions & 160 deletions

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
208208
public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
209209
{
210210
this.ReadImageHeaders(stream, out _, out _);
211-
return new ImageInfo(new PixelTypeInfo(this.infoHeader.BitsPerPixel), new(this.infoHeader.Width, this.infoHeader.Height), this.metadata);
211+
return new ImageInfo(new(this.infoHeader.Width, this.infoHeader.Height), this.metadata);
212212
}
213213

214214
/// <summary>

src/ImageSharp/Formats/Gif/GifDecoderCore.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
249249
}
250250

251251
return new ImageInfo(
252-
new PixelTypeInfo(this.logicalScreenDescriptor.BitsPerPixel),
253252
new(this.logicalScreenDescriptor.Width, this.logicalScreenDescriptor.Height),
254253
this.metadata,
255254
framesMetadata);

src/ImageSharp/Formats/ImageDecoder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ internal void SetDecoderFormat(Configuration configuration, ImageInfo info)
307307
if (configuration.ImageFormatsManager.TryFindFormatByDecoder(this, out IImageFormat? format))
308308
{
309309
info.Metadata.DecodedImageFormat = format;
310+
info.PixelType = info.Metadata.GetDecodedPixelTypeInfo();
310311

311312
foreach (ImageFrameMetadata frame in info.FrameMetadataCollection)
312313
{

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
226226
this.InitDerivedMetadataProperties();
227227

228228
Size pixelSize = this.Frame.PixelSize;
229-
return new ImageInfo(new PixelTypeInfo(this.Frame.BitsPerPixel), new(pixelSize.Width, pixelSize.Height), this.Metadata);
229+
return new ImageInfo(new(pixelSize.Width, pixelSize.Height), this.Metadata);
230230
}
231231

232232
/// <summary>

src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
6969
{
7070
this.ProcessHeader(stream);
7171

72-
var image = new Image<TPixel>(this.configuration, this.pixelSize.Width, this.pixelSize.Height, this.metadata);
72+
Image<TPixel> image = new(this.configuration, this.pixelSize.Width, this.pixelSize.Height, this.metadata);
7373

7474
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
7575

@@ -86,17 +86,17 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
8686
public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
8787
{
8888
this.ProcessHeader(stream);
89-
90-
// BlackAndWhite pixels are encoded into a byte.
91-
int bitsPerPixel = this.componentType == PbmComponentType.Short ? 16 : 8;
92-
return new ImageInfo(new PixelTypeInfo(bitsPerPixel), new(this.pixelSize.Width, this.pixelSize.Height), this.metadata);
89+
return new ImageInfo(
90+
new(this.pixelSize.Width, this.pixelSize.Height),
91+
this.metadata);
9392
}
9493

9594
/// <summary>
9695
/// Processes the ppm header.
9796
/// </summary>
9897
/// <param name="stream">The input stream.</param>
9998
/// <exception cref="InvalidImageContentException">An EOF marker has been read before the image has been decoded.</exception>
99+
[MemberNotNull(nameof(metadata))]
100100
private void ProcessHeader(BufferedReadStream stream)
101101
{
102102
Span<byte> buffer = stackalloc byte[2];

src/ImageSharp/Formats/Pbm/PbmMetadata.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,20 @@ public PixelTypeInfo GetPixelTypeInfo()
9797
colorType = PixelColorType.Binary;
9898
info = PixelComponentInfo.Create(1, bpp, 1);
9999
break;
100-
case PbmColorType.Grayscale:
101-
bpp = 8;
102-
colorType = PixelColorType.Luminance;
103-
info = PixelComponentInfo.Create(1, bpp, 8);
104-
break;
105100
case PbmColorType.Rgb:
106-
bpp = 24;
101+
bpp = this.ComponentType == PbmComponentType.Short ? 48 : 24;
107102
colorType = PixelColorType.RGB;
108-
info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
103+
info = this.ComponentType == PbmComponentType.Short
104+
? PixelComponentInfo.Create(3, bpp, 16, 16, 16)
105+
: PixelComponentInfo.Create(3, bpp, 8, 8, 8);
109106
break;
107+
case PbmColorType.Grayscale:
110108
default:
111-
bpp = 8;
109+
bpp = this.ComponentType == PbmComponentType.Short ? 16 : 8;
112110
colorType = PixelColorType.Luminance;
113-
info = PixelComponentInfo.Create(1, bpp, 8);
111+
info = this.ComponentType == PbmComponentType.Short
112+
? PixelComponentInfo.Create(1, bpp, bpp)
113+
: PixelComponentInfo.Create(1, bpp, bpp);
114114
break;
115115
}
116116

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
515515
// Both PLTE and tRNS chunks, if present, have been read at this point as per spec.
516516
AssignColorPalette(this.palette, this.paletteAlpha, pngMetadata);
517517

518-
return new ImageInfo(new PixelTypeInfo(this.CalculateBitsPerPixel()), new(this.header.Width, this.header.Height), metadata);
518+
return new ImageInfo(new(this.header.Width, this.header.Height), metadata);
519519
}
520520
finally
521521
{
@@ -680,29 +680,6 @@ private void InitializeFrame<TPixel>(
680680
this.scanline = this.configuration.MemoryAllocator.Allocate<byte>(this.bytesPerScanline, AllocationOptions.Clean);
681681
}
682682

683-
/// <summary>
684-
/// Calculates the correct number of bits per pixel for the given color type.
685-
/// </summary>
686-
/// <returns>The <see cref="int"/></returns>
687-
private int CalculateBitsPerPixel()
688-
{
689-
switch (this.pngColorType)
690-
{
691-
case PngColorType.Grayscale:
692-
case PngColorType.Palette:
693-
return this.header.BitDepth;
694-
case PngColorType.GrayscaleWithAlpha:
695-
return this.header.BitDepth * 2;
696-
case PngColorType.Rgb:
697-
return this.header.BitDepth * 3;
698-
case PngColorType.RgbWithAlpha:
699-
return this.header.BitDepth * 4;
700-
default:
701-
PngThrowHelper.ThrowNotSupportedColor();
702-
return -1;
703-
}
704-
}
705-
706683
/// <summary>
707684
/// Calculates the correct number of bytes per pixel for the given color type.
708685
/// </summary>

src/ImageSharp/Formats/Png/PngMetadata.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ public PixelTypeInfo GetPixelTypeInfo()
176176
break;
177177

178178
case PngColorType.Grayscale:
179-
bpp = 8;
179+
bpp = (int)this.BitDepth;
180180
colorType = PixelColorType.Luminance;
181-
info = PixelComponentInfo.Create(1, bpp, 8);
181+
info = PixelComponentInfo.Create(1, bpp, bpp);
182182
break;
183183

184184
case PngColorType.GrayscaleWithAlpha:
@@ -200,15 +200,15 @@ public PixelTypeInfo GetPixelTypeInfo()
200200
case PngColorType.Rgb:
201201
if (this.BitDepth == PngBitDepth.Bit16)
202202
{
203-
bpp = 24;
203+
bpp = 48;
204204
colorType = PixelColorType.RGB;
205-
info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
205+
info = PixelComponentInfo.Create(3, bpp, 16, 16, 16);
206206
break;
207207
}
208208

209-
bpp = 48;
209+
bpp = 24;
210210
colorType = PixelColorType.RGB;
211-
info = PixelComponentInfo.Create(3, bpp, 16, 16, 16);
211+
info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
212212
break;
213213

214214
case PngColorType.RgbWithAlpha:

src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
7373
qoiMetadata.Channels = this.header.Channels;
7474
qoiMetadata.ColorSpace = this.header.ColorSpace;
7575

76-
return new ImageInfo(pixelType, size, metadata);
76+
return new ImageInfo(size, metadata);
7777
}
7878

7979
/// <summary>

src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,6 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
646646
{
647647
this.ReadFileHeader(stream);
648648
return new ImageInfo(
649-
new PixelTypeInfo(this.fileHeader.PixelDepth),
650649
new(this.fileHeader.Width, this.fileHeader.Height),
651650
this.metadata);
652651
}

0 commit comments

Comments
 (0)