Skip to content

Commit 61bda0f

Browse files
committed
Reproject now works on ARGB (very very slow)
1 parent e13f7b3 commit 61bda0f

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

LibraryTest/Program.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ public static void Main (string[] args) {
9292
//bmp.Save(filename + "-orig.png", ImageFormat.Png);
9393
//vbmp.Save(visFilename + "-orig.png", ImageFormat.Png);
9494

95-
Bitmap reproj = ImageTools.ReprojectLinear (vbmp, gc);
95+
//Bitmap reproj = ImageTools.ReprojectLinear (vbmp, gc);
9696

97-
reproj.Save("test.png", ImageFormat.Png);
97+
//reproj.Save("test.png", ImageFormat.Png);
9898

99-
/*
100-
var mapDrawer = new MapDrawer(shapeFile);
99+
///*
100+
var mapDrawer = new MapDrawer("/home/lucas/Works/OpenSatelliteProject/split/borders/ne_50m_admin_1_states_provinces_lakes.shp");
101+
//var mapDrawer = new MapDrawer(shapeFile);
101102
//ImageTools.DrawLatLonLines(ref bmp, gc, Color.Brown);
102103
ImageTools.ApplyCurve (OpenSatelliteProject.Presets.NEW_VIS_FALSE_CURVE, ref vbmp);
103104

@@ -119,6 +120,11 @@ public static void Main (string[] args) {
119120
landMap.Dispose ();
120121
bmp.Dispose();
121122

123+
//Console.WriteLine ("Starting Reprojection");
124+
//Bitmap reproj = ImageTools.ReprojectLinear (vbmp, gc);
125+
//Console.WriteLine ("Reprojection end");
126+
//reproj.Save("test-falsecolor-reproject.jpg", ImageFormat.Jpeg);
127+
122128

123129
//return;
124130
// */

XRIT/Tools/ImageTools.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)