@@ -19,16 +19,9 @@ public static class ImageTools {
1919 public static string OSPLABEL = $ "OpenSatelliteProject { LibInfo . Version } ";
2020
2121 public static Bitmap ReprojectLinear ( Bitmap bmp , GeoConverter gc , bool fixCrop = false ) {
22- Bitmap output = new Bitmap ( bmp . Width , bmp . Height , PixelFormat . Format8bppIndexed ) ;
23- ColorPalette pal = output . Palette ;
24- // Standard grayscale palette
25- for ( int i = 0 ; i <= 255 ; i ++ ) {
26- pal . Entries [ i ] = Color . FromArgb ( i , i , i ) ;
27- }
28- output . Palette = pal ;
29-
22+ Bitmap output = new Bitmap ( bmp . Width , bmp . Height , PixelFormat . Format32bppArgb ) ;
3023 var pdata = bmp . LockBits ( new Rectangle ( 0 , 0 , bmp . Width , bmp . Height ) , ImageLockMode . ReadOnly , PixelFormat . Format32bppArgb ) ;
31- var odata = output . LockBits ( new Rectangle ( 0 , 0 , output . Width , output . Height ) , ImageLockMode . ReadWrite , PixelFormat . Format8bppIndexed ) ;
24+ var odata = output . LockBits ( new Rectangle ( 0 , 0 , output . Width , output . Height ) , ImageLockMode . ReadWrite , PixelFormat . Format32bppArgb ) ;
3225 unsafe {
3326 byte * dPtr = ( byte * ) odata . Scan0 . ToPointer ( ) ;
3427 byte * pPtr = ( byte * ) pdata . Scan0 . ToPointer ( ) ;
@@ -46,7 +39,10 @@ public static Bitmap ReprojectLinear(Bitmap bmp, GeoConverter gc, bool fixCrop =
4639 if ( fixCrop ) {
4740 newx -= gc . CropLeft ;
4841 }
49- dPtr [ y * stride + x ] = bilinear ( pPtr , newx , newy , pdata . Stride ) ;
42+ dPtr [ y * stride + x * 4 + 0 ] = BilinearInterp ( pPtr , newx , newy , pdata . Stride , 0 ) ;
43+ dPtr [ y * stride + x * 4 + 1 ] = BilinearInterp ( pPtr , newx , newy , pdata . Stride , 1 ) ;
44+ dPtr [ y * stride + x * 4 + 2 ] = BilinearInterp ( pPtr , newx , newy , pdata . Stride , 2 ) ;
45+ dPtr [ y * stride + x * 4 + 3 ] = BilinearInterp ( pPtr , newx , newy , pdata . Stride , 3 ) ;
5046 }
5147 }
5248 }
@@ -61,19 +57,22 @@ private unsafe static byte val(byte *data, int x, int y, int mw) {
6157 return data [ y * mw + x * 4 + 1 ] ;
6258 }
6359
60+ private unsafe static byte ValueAtImage ( byte * data , int x , int y , int mw , int colorChannel ) {
61+ return data [ y * mw + x * 4 + colorChannel ] ;
62+ }
6463
65- private unsafe static byte bilinear ( byte * data , double x , double y , int mw ) {
64+ private unsafe static byte BilinearInterp ( byte * data , double x , double y , int mw , int colorChannel ) {
6665 int rx = ( int ) ( x ) ;
6766 int ry = ( int ) ( y ) ;
6867 float fracX = ( float ) ( x - rx ) ;
6968 float fracY = ( float ) ( y - ry ) ;
7069 float invfracX = 1f - fracX ;
7170 float invfracY = 1f - fracY ;
7271
73- byte a = val ( data , rx , ry , mw ) ;
74- byte b = val ( data , rx + 1 , ry , mw ) ;
75- byte c = val ( data , rx , ry + 1 , mw ) ;
76- byte d = val ( data , rx + 1 , ry + 1 , mw ) ;
72+ byte a = ValueAtImage ( data , rx , ry , mw , colorChannel ) ;
73+ byte b = ValueAtImage ( data , rx + 1 , ry , mw , colorChannel ) ;
74+ byte c = ValueAtImage ( data , rx , ry + 1 , mw , colorChannel ) ;
75+ byte d = ValueAtImage ( data , rx + 1 , ry + 1 , mw , colorChannel ) ;
7776
7877 return ( byte ) ( ( a * invfracX + b * fracX ) * invfracY + ( c * invfracX + d * fracX ) * fracY ) ;
7978 }
0 commit comments