@@ -346,72 +346,60 @@ static uint GetNextNonDuplicateOffset(List<G1Element32> elements, int currentEle
346
346
347
347
public static byte [ ] DecodeRLEImageData ( G1Element32 img )
348
348
{
349
- // not sure why this happens, but this seems 'legit'; airport files have these
350
- if ( img . ImageData . Length == 0 )
351
- {
352
- return [ ] ;
353
- }
354
-
355
349
var width = img . Width ;
356
350
var height = img . Height ;
357
351
358
352
var dstLineWidth = img . Width ;
359
- var dst0Index = 0 ; // dstLineWidth * img.yOffset + img.xOffset;
353
+ var dst0Index = 0 ;
360
354
361
355
var srcBuf = img . ImageData ;
362
356
var dstBuf = new byte [ img . Width * img . Height ] ; // Assuming a single byte per pixel
363
357
364
358
var srcY = 0 ;
365
359
360
+ // Move up to the first line of the image if source_y_start is negative. Why does this even occur?
366
361
if ( srcY < 0 )
367
362
{
368
363
srcY ++ ;
369
364
height -- ;
370
365
dst0Index += dstLineWidth ;
371
366
}
372
367
368
+ // For every line in the image
373
369
for ( var i = 0 ; i < height ; i ++ )
374
370
{
375
371
var y = srcY + i ;
376
372
377
- var lineOffset = srcBuf [ y * 2 ] | ( srcBuf [ ( y * 2 ) + 1 ] << 8 ) ;
378
-
379
- var nextRunIndex = lineOffset ;
373
+ // The first part of the source pointer is a list of offsets to different lines
374
+ // This will move the pointer to the correct source line.
375
+ var nextRunIndex = srcBuf [ y * 2 ] | ( srcBuf [ ( y * 2 ) + 1 ] << 8 ) ;
380
376
var dstLineStartIndex = dst0Index + ( dstLineWidth * i ) ;
381
377
382
- while ( true )
378
+ // For every data chunk in the line
379
+ var isEndOfLine = false ;
380
+ while ( ! isEndOfLine )
383
381
{
384
382
var srcIndex = nextRunIndex ;
385
-
386
- var rleInfoByte = srcBuf [ srcIndex ++ ] ;
387
- var dataSize = rleInfoByte & 0x7F ;
388
- var isEndOfLine = ( rleInfoByte & 0x80 ) != 0 ;
389
-
383
+ var dataSize = srcBuf [ srcIndex ++ ] ;
390
384
var firstPixelX = srcBuf [ srcIndex ++ ] ;
391
- nextRunIndex = srcIndex + dataSize ;
385
+ isEndOfLine = ( dataSize & 0x80 ) != 0 ;
386
+ dataSize &= 0x7F ;
392
387
393
- var x = firstPixelX - 0 ; // img.xOffset;
394
- var numPixels = dataSize ;
388
+ // Have our next source pointer point to the next data section
389
+ nextRunIndex = srcIndex + dataSize ;
395
390
396
- if ( x > 0 )
397
- {
398
- x ++ ;
399
- srcIndex ++ ;
400
- numPixels -- ;
401
- }
402
- else if ( x < 0 )
403
- {
404
- srcIndex += - x ;
405
- numPixels += x ;
406
- x = 0 ;
407
- }
391
+ var x = firstPixelX ;
392
+ int numPixels = dataSize ;
408
393
394
+ // If the end position is further out than the whole image
395
+ // end position then we need to shorten the line again
409
396
numPixels = Math . Min ( numPixels , width - x ) ;
410
397
411
398
var dstIndex = dstLineStartIndex + x ;
412
399
413
400
if ( numPixels > 0 )
414
401
{
402
+ // Since we're sampling each pixel at this zoom level, just do a straight std::memcpy
415
403
Array . Copy ( srcBuf , srcIndex , dstBuf , dstIndex , numPixels ) ;
416
404
}
417
405
0 commit comments