Skip to content

Commit 07d50d0

Browse files
Merge pull request #1779 from SixLabors/js/jpeg-encoder-color-type
Ensure jpeg encoder is immutable.
2 parents 424d4f9 + d5cea15 commit 07d50d0

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

Diff for: src/ImageSharp/Formats/Jpeg/JpegEncoder.cs

-25
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
2929
where TPixel : unmanaged, IPixel<TPixel>
3030
{
3131
var encoder = new JpegEncoderCore(this);
32-
this.InitializeColorType(image);
3332
encoder.Encode(image, stream);
3433
}
3534

@@ -45,31 +44,7 @@ public Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, Cancellation
4544
where TPixel : unmanaged, IPixel<TPixel>
4645
{
4746
var encoder = new JpegEncoderCore(this);
48-
this.InitializeColorType(image);
4947
return encoder.EncodeAsync(image, stream, cancellationToken);
5048
}
51-
52-
/// <summary>
53-
/// If ColorType was not set, set it based on the given image.
54-
/// </summary>
55-
private void InitializeColorType<TPixel>(Image<TPixel> image)
56-
where TPixel : unmanaged, IPixel<TPixel>
57-
{
58-
// First inspect the image metadata.
59-
if (this.ColorType == null)
60-
{
61-
JpegMetadata metadata = image.Metadata.GetJpegMetadata();
62-
this.ColorType = metadata.ColorType;
63-
}
64-
65-
// Secondly, inspect the pixel type.
66-
if (this.ColorType == null)
67-
{
68-
bool isGrayscale =
69-
typeof(TPixel) == typeof(L8) || typeof(TPixel) == typeof(L16) ||
70-
typeof(TPixel) == typeof(La16) || typeof(TPixel) == typeof(La32);
71-
this.ColorType = isGrayscale ? JpegColorType.Luminance : JpegColorType.YCbCrRatio420;
72-
}
73-
}
7449
}
7550
}

Diff for: src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

+36-3
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
8686
ImageMetadata metadata = image.Metadata;
8787
JpegMetadata jpegMetadata = metadata.GetJpegMetadata();
8888

89-
// If the color type was not specified by the user, preserve the color type of the input image, if it's a supported color type.
90-
if (!this.colorType.HasValue && IsSupportedColorType(jpegMetadata.ColorType))
89+
// If the color type was not specified by the user, preserve the color type of the input image.
90+
if (!this.colorType.HasValue)
9191
{
92-
this.colorType = jpegMetadata.ColorType;
92+
this.colorType = GetFallbackColorType(image);
9393
}
9494

9595
// Compute number of components based on color type in options.
@@ -156,6 +156,39 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
156156
stream.Flush();
157157
}
158158

159+
/// <summary>
160+
/// If color type was not set, set it based on the given image.
161+
/// Note, if there is no metadata and the image has multiple components this method
162+
/// returns <see langword="null"/> defering the field assignment
163+
/// to <see cref="InitQuantizationTables(int, JpegMetadata, out Block8x8F, out Block8x8F)"/>.
164+
/// </summary>
165+
private static JpegColorType? GetFallbackColorType<TPixel>(Image<TPixel> image)
166+
where TPixel : unmanaged, IPixel<TPixel>
167+
{
168+
// First inspect the image metadata.
169+
JpegColorType? colorType = null;
170+
JpegMetadata metadata = image.Metadata.GetJpegMetadata();
171+
if (IsSupportedColorType(metadata.ColorType))
172+
{
173+
return metadata.ColorType;
174+
}
175+
176+
// Secondly, inspect the pixel type.
177+
// TODO: PixelTypeInfo should contain a component count!
178+
bool isGrayscale =
179+
typeof(TPixel) == typeof(L8) || typeof(TPixel) == typeof(L16) ||
180+
typeof(TPixel) == typeof(La16) || typeof(TPixel) == typeof(La32);
181+
182+
// We don't set multi-component color types here since we can set it based upon
183+
// the quality in InitQuantizationTables.
184+
if (isGrayscale)
185+
{
186+
colorType = JpegColorType.Luminance;
187+
}
188+
189+
return colorType;
190+
}
191+
159192
/// <summary>
160193
/// Returns true, if the color type is supported by the encoder.
161194
/// </summary>

0 commit comments

Comments
 (0)