Skip to content

Commit 612f791

Browse files
committed
Use luminance quant table for all channel with RGB
1 parent ed9bd16 commit 612f791

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,16 @@ public void EncodeGrayscale<TPixel>(Image<TPixel> pixels, ref Block8x8F luminanc
237237
/// <typeparam name="TPixel">The pixel format.</typeparam>
238238
/// <param name="pixels">The pixel accessor providing access to the image pixels.</param>
239239
/// <param name="luminanceQuantTable">Luminance quantization table provided by the callee.</param>
240-
/// <param name="chrominanceQuantTable">Chrominance quantization table provided by the callee.</param>
241240
/// <param name="cancellationToken">The token to monitor for cancellation.</param>
242-
public void EncodeRgb<TPixel>(Image<TPixel> pixels, ref Block8x8F luminanceQuantTable, ref Block8x8F chrominanceQuantTable, CancellationToken cancellationToken)
241+
public void EncodeRgb<TPixel>(Image<TPixel> pixels, ref Block8x8F luminanceQuantTable, CancellationToken cancellationToken)
243242
where TPixel : unmanaged, IPixel<TPixel>
244243
{
245244
this.huffmanTables = HuffmanLut.TheHuffmanLut;
246245

247246
var unzig = ZigZag.CreateUnzigTable();
248247

249248
// ReSharper disable once InconsistentNaming
250-
int prevDCY = 0, prevDCCb = 0, prevDCCr = 0;
249+
int prevDCR = 0, prevDCG = 0, prevDCB = 0;
251250

252251
ImageFrame<TPixel> frame = pixels.Frames.RootFrame;
253252
Buffer2D<TPixel> pixelBuffer = frame.PixelBuffer;
@@ -264,25 +263,25 @@ public void EncodeRgb<TPixel>(Image<TPixel> pixels, ref Block8x8F luminanceQuant
264263
{
265264
pixelConverter.Convert(x, y, ref currentRows);
266265

267-
prevDCY = this.WriteBlock(
266+
prevDCR = this.WriteBlock(
268267
QuantIndex.Luminance,
269-
prevDCY,
268+
prevDCR,
270269
ref pixelConverter.R,
271270
ref luminanceQuantTable,
272271
ref unzig);
273272

274-
prevDCCb = this.WriteBlock(
275-
QuantIndex.Chrominance,
276-
prevDCCb,
273+
prevDCG = this.WriteBlock(
274+
QuantIndex.Luminance,
275+
prevDCG,
277276
ref pixelConverter.G,
278-
ref chrominanceQuantTable,
277+
ref luminanceQuantTable,
279278
ref unzig);
280279

281-
prevDCCr = this.WriteBlock(
282-
QuantIndex.Chrominance,
283-
prevDCCr,
280+
prevDCB = this.WriteBlock(
281+
QuantIndex.Luminance,
282+
prevDCB,
284283
ref pixelConverter.B,
285-
ref chrominanceQuantTable,
284+
ref luminanceQuantTable,
286285
ref unzig);
287286
}
288287
}

src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
160160
scanEncoder.Encode420(image, ref luminanceQuantTable, ref chrominanceQuantTable, cancellationToken);
161161
break;
162162
case JpegColorType.Rgb:
163-
scanEncoder.EncodeRgb(image, ref luminanceQuantTable, ref chrominanceQuantTable, cancellationToken);
163+
scanEncoder.EncodeRgb(image, ref luminanceQuantTable, cancellationToken);
164164
break;
165165
}
166166
}
@@ -620,6 +620,17 @@ private void WriteStartOfFrame(int width, int height, int componentCount, ReadOn
620620
0x11,
621621
0x11
622622
};
623+
624+
if (this.colorType == JpegColorType.Rgb)
625+
{
626+
chroma = stackalloc byte[]
627+
{
628+
0x00,
629+
0x00,
630+
0x00
631+
};
632+
}
633+
623634
break;
624635
case JpegColorType.YCbCrRatio420:
625636
subsamples = stackalloc byte[]
@@ -669,6 +680,17 @@ private void WriteStartOfScan(int componentCount, ReadOnlySpan<byte> componentId
669680
0x11
670681
};
671682

683+
// Use the same DC/AC tables for all channels for RGB.
684+
if (this.colorType == JpegColorType.Rgb)
685+
{
686+
huffmanId = stackalloc byte[]
687+
{
688+
0x00,
689+
0x00,
690+
0x00
691+
};
692+
}
693+
672694
// Write the SOS (Start Of Scan) marker "\xff\xda" followed by 12 bytes:
673695
// - the marker length "\x00\x0c",
674696
// - the number of components "\x03",

0 commit comments

Comments
 (0)