Skip to content

Commit 2e89d3c

Browse files
committed
Fix deduce jpeg color type
1 parent 57335b8 commit 2e89d3c

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/ImageSharp/Formats/Jpeg/JpegColorType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public enum JpegColorType : byte
3030
/// <summary>
3131
/// The pixel data will be preserved as RGB without any sub sampling.
3232
/// </summary>
33-
Rgb,
33+
Rgb = 3,
3434
}
3535
}

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,9 @@ public void Dispose()
409409
/// <summary>
410410
/// Returns the correct colorspace based on the image component count and the jpeg frame component id's.
411411
/// </summary>
412+
/// <param name="componentCount">The number of components.</param>
412413
/// <returns>The <see cref="JpegColorSpace"/></returns>
413-
private JpegColorSpace DeduceJpegColorSpace(byte componentCount, JpegComponent[] components)
414+
private JpegColorSpace DeduceJpegColorSpace(byte componentCount)
414415
{
415416
if (componentCount == 1)
416417
{
@@ -425,7 +426,7 @@ private JpegColorSpace DeduceJpegColorSpace(byte componentCount, JpegComponent[]
425426
}
426427

427428
// If the component Id's are R, G, B in ASCII the colorspace is RGB and not YCbCr.
428-
if (components[2].Id == 66 && components[1].Id == 71 && components[0].Id == 82)
429+
if (this.Components[2].Id == 66 && this.Components[1].Id == 71 && this.Components[0].Id == 82)
429430
{
430431
return JpegColorSpace.RGB;
431432
}
@@ -446,6 +447,35 @@ private JpegColorSpace DeduceJpegColorSpace(byte componentCount, JpegComponent[]
446447
return default;
447448
}
448449

450+
/// <summary>
451+
/// Returns the jpeg color type based on the colorspace and subsampling used.
452+
/// </summary>
453+
/// <returns>Jpeg color type.</returns>
454+
private JpegColorType DeduceJpegColorType()
455+
{
456+
switch (this.ColorSpace)
457+
{
458+
case JpegColorSpace.Grayscale:
459+
return JpegColorType.Luminance;
460+
case JpegColorSpace.RGB:
461+
return JpegColorType.Rgb;
462+
case JpegColorSpace.YCbCr:
463+
if (this.Frame.Components[0].HorizontalSamplingFactor == 1 && this.Frame.Components[0].VerticalSamplingFactor == 1 &&
464+
this.Frame.Components[1].HorizontalSamplingFactor == 1 && this.Frame.Components[1].VerticalSamplingFactor == 1 &&
465+
this.Frame.Components[2].HorizontalSamplingFactor == 1 && this.Frame.Components[2].VerticalSamplingFactor == 1)
466+
{
467+
return JpegColorType.YCbCrRatio444;
468+
}
469+
else
470+
{
471+
return JpegColorType.YCbCrRatio420;
472+
}
473+
474+
default:
475+
return JpegColorType.YCbCrRatio420;
476+
}
477+
}
478+
449479
/// <summary>
450480
/// Initializes the EXIF profile.
451481
/// </summary>
@@ -859,7 +889,7 @@ private void ProcessDefineQuantizationTablesMarker(BufferedReadStream stream, in
859889
}
860890

861891
/// <summary>
862-
/// Processes the Start of Frame marker. Specified in section B.2.2.
892+
/// Processes the Start of Frame marker. Specified in section B.2.2.
863893
/// </summary>
864894
/// <param name="stream">The input stream.</param>
865895
/// <param name="remaining">The remaining bytes in the segment block.</param>
@@ -951,20 +981,8 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining,
951981
index += componentBytes;
952982
}
953983

954-
this.ColorSpace = this.DeduceJpegColorSpace(componentCount, this.Frame.Components);
955-
956-
switch (this.ColorSpace)
957-
{
958-
case JpegColorSpace.Grayscale:
959-
this.Metadata.GetJpegMetadata().ColorType = JpegColorType.Luminance;
960-
break;
961-
case JpegColorSpace.RGB:
962-
this.Metadata.GetJpegMetadata().ColorType = JpegColorType.Rgb;
963-
break;
964-
default:
965-
this.Metadata.GetJpegMetadata().ColorType = JpegColorType.YCbCrRatio420;
966-
break;
967-
}
984+
this.ColorSpace = this.DeduceJpegColorSpace(componentCount);
985+
this.Metadata.GetJpegMetadata().ColorType = this.DeduceJpegColorType();
968986

969987
if (!metadataOnly)
970988
{

0 commit comments

Comments
 (0)