@@ -241,14 +241,18 @@ ref Unsafe.Add(ref blockRef, k),
241
241
/// Encodes the DC coefficients for a given component's blocks in a scan.
242
242
/// </summary>
243
243
/// <param name="component">The component whose DC coefficients need to be encoded.</param>
244
+ /// <param name="restartInterval">Numbers of MCUs between restart markers.</param>
244
245
/// <param name="cancellationToken">The token to request cancellation.</param>
245
- public void EncodeDcScan ( Component component , CancellationToken cancellationToken )
246
+ public void EncodeDcScan ( Component component , int restartInterval , CancellationToken cancellationToken )
246
247
{
247
248
int h = component . HeightInBlocks ;
248
249
int w = component . WidthInBlocks ;
249
250
250
251
ref HuffmanLut dcHuffmanTable = ref this . dcHuffmanTables [ component . DcTableId ] ;
251
252
253
+ int restarts = 0 ;
254
+ int restartsToGo = restartInterval ;
255
+
252
256
for ( int i = 0 ; i < h ; i ++ )
253
257
{
254
258
cancellationToken . ThrowIfCancellationRequested ( ) ;
@@ -258,6 +262,13 @@ public void EncodeDcScan(Component component, CancellationToken cancellationToke
258
262
259
263
for ( nuint k = 0 ; k < ( uint ) w ; k ++ )
260
264
{
265
+ if ( restartInterval > 0 && restartsToGo == 0 )
266
+ {
267
+ this . FlushRemainingBytes ( ) ;
268
+ this . WriteRestart ( restarts % 8 ) ;
269
+ component . DcPredictor = 0 ;
270
+ }
271
+
261
272
this . WriteDc (
262
273
component ,
263
274
ref Unsafe . Add ( ref blockRef , k ) ,
@@ -267,6 +278,18 @@ ref Unsafe.Add(ref blockRef, k),
267
278
{
268
279
this . FlushToStream ( ) ;
269
280
}
281
+
282
+ if ( restartInterval > 0 )
283
+ {
284
+ if ( restartsToGo == 0 )
285
+ {
286
+ restartsToGo = restartInterval ;
287
+ restarts ++ ;
288
+ restarts &= 7 ;
289
+ }
290
+
291
+ restartsToGo -- ;
292
+ }
270
293
}
271
294
}
272
295
@@ -279,12 +302,16 @@ ref Unsafe.Add(ref blockRef, k),
279
302
/// <param name="component">The component whose AC coefficients need to be encoded.</param>
280
303
/// <param name="start">The starting index of the AC coefficient range to encode.</param>
281
304
/// <param name="end">The ending index of the AC coefficient range to encode.</param>
305
+ /// <param name="restartInterval">Numbers of MCUs between restart markers.</param>
282
306
/// <param name="cancellationToken">The token to request cancellation.</param>
283
- public void EncodeAcScan ( Component component , nint start , nint end , CancellationToken cancellationToken )
307
+ public void EncodeAcScan ( Component component , nint start , nint end , int restartInterval , CancellationToken cancellationToken )
284
308
{
285
309
int h = component . HeightInBlocks ;
286
310
int w = component . WidthInBlocks ;
287
311
312
+ int restarts = 0 ;
313
+ int restartsToGo = restartInterval ;
314
+
288
315
ref HuffmanLut acHuffmanTable = ref this . acHuffmanTables [ component . AcTableId ] ;
289
316
290
317
for ( int i = 0 ; i < h ; i ++ )
@@ -296,6 +323,12 @@ public void EncodeAcScan(Component component, nint start, nint end, Cancellation
296
323
297
324
for ( nuint k = 0 ; k < ( uint ) w ; k ++ )
298
325
{
326
+ if ( restartInterval > 0 && restartsToGo == 0 )
327
+ {
328
+ this . FlushRemainingBytes ( ) ;
329
+ this . WriteRestart ( restarts % 8 ) ;
330
+ }
331
+
299
332
this . WriteAcBlock (
300
333
ref Unsafe . Add ( ref blockRef , k ) ,
301
334
start ,
@@ -306,6 +339,18 @@ ref Unsafe.Add(ref blockRef, k),
306
339
{
307
340
this . FlushToStream ( ) ;
308
341
}
342
+
343
+ if ( restartInterval > 0 )
344
+ {
345
+ if ( restartsToGo == 0 )
346
+ {
347
+ restartsToGo = restartInterval ;
348
+ restarts ++ ;
349
+ restarts &= 7 ;
350
+ }
351
+
352
+ restartsToGo -- ;
353
+ }
309
354
}
310
355
}
311
356
@@ -508,6 +553,9 @@ private void WriteBlock(
508
553
this . WriteAcBlock ( ref block , 1 , 64 , ref acTable ) ;
509
554
}
510
555
556
+ private void WriteRestart ( int restart ) =>
557
+ this . target . Write ( [ 0xff , ( byte ) ( JpegConstants . Markers . RST0 + restart ) ] ) ;
558
+
511
559
/// <summary>
512
560
/// Emits the most significant count of bits to the buffer.
513
561
/// </summary>
0 commit comments