@@ -340,43 +340,42 @@ public static void Apply2DLut(Func<byte, byte, int> lookup, ref Bitmap visible,
340340 infrared = ToFormat ( infrared , visible . PixelFormat ) ;
341341 }
342342
343+ if ( visible . Height != infrared . Height || visible . Width != infrared . Width ) {
344+ UIConsole . Warn ( "The Infrared and Visible channels size doesn't match, the false color might look weird.\n " +
345+ $ "Visible({ visible . Width } , { visible . Height } ) vs Infrared({ infrared . Width , infrared . Height } )") ;
346+ }
347+
343348 var vdata = visible . LockBits ( new Rectangle ( 0 , 0 , visible . Width , visible . Height ) , ImageLockMode . ReadWrite , visible . PixelFormat ) ;
344349 var idata = infrared . LockBits ( new Rectangle ( 0 , 0 , infrared . Width , infrared . Height ) , ImageLockMode . ReadOnly , visible . PixelFormat ) ;
345- int totalPoints = vdata . Stride * visible . Height ;
350+ int totalPoints = Math . Min ( vdata . Stride * visible . Height , idata . Stride * infrared . Height ) ; // Avoids crash on corrupted images
346351
347352 if ( visible . PixelFormat == PixelFormat . Format24bppRgb ) {
348353 unsafe {
349354 byte * vPtr = ( byte * ) vdata . Scan0 . ToPointer ( ) ;
350355 byte * iPtr = ( byte * ) idata . Scan0 . ToPointer ( ) ;
351- for ( int y = 0 ; y < visible . Height ; y ++ ) {
352- for ( int x = 0 ; x < visible . Width ; x ++ ) {
353- int stridePos = y * vdata . Stride + x * 3 ; // Packed RGB
354- // Assume Grayscale in RGB
355- byte visVal = vPtr [ stridePos ] ;
356- byte irVal = iPtr [ stridePos ] ;
357- int color = lookup ( irVal , visVal ) ;
358- vPtr [ stridePos + 0 ] = ( byte ) ( ( color >> 0 ) & 0xFF ) ;
359- vPtr [ stridePos + 1 ] = ( byte ) ( ( color >> 8 ) & 0xFF ) ;
360- vPtr [ stridePos + 2 ] = ( byte ) ( ( color >> 16 ) & 0xFF ) ;
361- }
356+ for ( int stridePos = 0 ; stridePos < totalPoints ; stridePos += 3 ) {
357+ // Assume Grayscale in RGB
358+ byte visVal = vPtr [ stridePos ] ;
359+ byte irVal = iPtr [ stridePos ] ;
360+ int color = lookup ( irVal , visVal ) ;
361+ vPtr [ stridePos + 0 ] = ( byte ) ( ( color >> 0 ) & 0xFF ) ;
362+ vPtr [ stridePos + 1 ] = ( byte ) ( ( color >> 8 ) & 0xFF ) ;
363+ vPtr [ stridePos + 2 ] = ( byte ) ( ( color >> 16 ) & 0xFF ) ;
362364 }
363365 }
364366 } else if ( visible . PixelFormat == PixelFormat . Format32bppArgb ) {
365367 unsafe {
366368 byte * vPtr = ( byte * ) vdata . Scan0 . ToPointer ( ) ;
367369 byte * iPtr = ( byte * ) idata . Scan0 . ToPointer ( ) ;
368- for ( int y = 0 ; y < visible . Height ; y ++ ) {
369- for ( int x = 0 ; x < visible . Width ; x ++ ) {
370- int stridePos = y * vdata . Stride + x * 4 ; // Packed ARGB
371- // Assume Grayscale in ARGB
372- byte visVal = vPtr [ stridePos ] ;
373- byte irVal = iPtr [ stridePos ] ;
374- int color = lookup ( irVal , visVal ) ;
375- vPtr [ stridePos + 0 ] = ( byte ) ( ( color >> 0 ) & 0xFF ) ;
376- vPtr [ stridePos + 1 ] = ( byte ) ( ( color >> 8 ) & 0xFF ) ;
377- vPtr [ stridePos + 2 ] = ( byte ) ( ( color >> 16 ) & 0xFF ) ;
378- vPtr [ stridePos + 3 ] = ( byte ) ( ( color >> 24 ) & 0xFF ) ;
379- }
370+ for ( int stridePos = 0 ; stridePos < totalPoints ; stridePos += 4 ) {
371+ // Assume Grayscale in ARGB
372+ byte visVal = vPtr [ stridePos ] ;
373+ byte irVal = iPtr [ stridePos ] ;
374+ int color = lookup ( irVal , visVal ) ;
375+ vPtr [ stridePos + 0 ] = ( byte ) ( ( color >> 0 ) & 0xFF ) ;
376+ vPtr [ stridePos + 1 ] = ( byte ) ( ( color >> 8 ) & 0xFF ) ;
377+ vPtr [ stridePos + 2 ] = ( byte ) ( ( color >> 16 ) & 0xFF ) ;
378+ vPtr [ stridePos + 3 ] = ( byte ) ( ( color >> 24 ) & 0xFF ) ;
380379 }
381380 }
382381 }
@@ -619,15 +618,15 @@ public static void DrawLatLonLines(ref Bitmap bmp, GeoConverter gc, Color color,
619618 float lastY ;
620619 using ( var graphics = Graphics . FromImage ( bmp ) ) {
621620 for ( float lat = gc . MinLatitude ; lat < gc . MaxLatitude ; lat += 10f ) {
622- lastX = - 1f ;
623- lastY = - 1f ;
621+ lastX = float . NaN ;
622+ lastY = float . NaN ;
624623 for ( float lon = gc . MinLongitude ; lon < gc . MaxLongitude ; lon += 0.1f ) {
625624 var xy = gc . latlon2xy ( lat , lon ) ;
626625
627626 if ( fixCrop ) {
628627 xy = new Tuple < int , int > ( xy . Item1 - gc . CropLeft , xy . Item2 ) ;
629628 }
630- if ( lastX != - 1 && lastY != - 1 ) {
629+ if ( ! float . IsNaN ( lastX ) && ! float . IsNaN ( lastY ) ) {
631630 graphics . DrawLine ( pen , lastX , lastY , xy . Item1 , xy . Item2 ) ;
632631 }
633632 lastX = xy . Item1 ;
@@ -636,15 +635,15 @@ public static void DrawLatLonLines(ref Bitmap bmp, GeoConverter gc, Color color,
636635
637636 }
638637 for ( float lon = gc . MinLongitude ; lon < gc . MaxLongitude ; lon += 10f ) {
639- lastX = - 1f ;
640- lastY = - 1f ;
638+ lastX = float . NaN ;
639+ lastY = float . NaN ;
641640 for ( float lat = gc . MinLatitude ; lat < gc . MaxLatitude ; lat += 0.1f ) {
642641 var xy = gc . latlon2xy ( lat , lon ) ;
643642
644643 if ( fixCrop ) {
645644 xy = new Tuple < int , int > ( xy . Item1 - gc . CropLeft , xy . Item2 ) ;
646645 }
647- if ( lastX != - 1 && lastY != - 1 ) {
646+ if ( ! float . IsNaN ( lastX ) && ! float . IsNaN ( lastY ) ) {
648647 graphics . DrawLine ( pen , lastX , lastY , xy . Item1 , xy . Item2 ) ;
649648 }
650649 lastX = xy . Item1 ;
0 commit comments