Skip to content

Commit 0f1cdb3

Browse files
committed
Fix a crash when corrupted images get into false color generator
1 parent 75e0b73 commit 0f1cdb3

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

XRIT/Tools/ImageTools.cs

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

goesdump.userprefs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<Properties StartupItem="LibraryTest/LibraryTest.csproj" RefactoringSettings.EnableRefactorings="True">
22
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" />
3-
<MonoDevelop.Ide.Workbench ActiveDocument="XRIT/Properties/AssemblyInfo.cs">
3+
<MonoDevelop.Ide.Workbench ActiveDocument="XRIT/Tools/ImageTools.cs">
44
<Files>
5-
<File FileName="LibraryTest/Program.cs" Line="94" Column="15" />
5+
<File FileName="LibraryTest/Program.cs" Line="1" Column="1" />
66
<File FileName="XRIT/Geo/MapDrawer.cs" Line="1" Column="1" />
77
<File FileName="XRIT/ShapeFiles/ShapeFiles.cs" Line="1" Column="1" />
8-
<File FileName="XRIT/Tools/Presets.cs" Line="15" Column="40" />
8+
<File FileName="XRIT/Tools/Presets.cs" Line="1" Column="1" />
99
<File FileName="XRIT/GOES/ImageManager.cs" Line="553" Column="76" />
10-
<File FileName="XRIT/Tools/ImageTools.cs" Line="98" Column="63" />
10+
<File FileName="XRIT/Tools/ImageTools.cs" Line="565" Column="50" />
1111
<File FileName="XRIT/Tools/LLTools.cs" Line="1" Column="1" />
12-
<File FileName="XRIT/Properties/LibInfo.cs" Line="59" Column="1" />
13-
<File FileName="XRIT/Properties/AssemblyInfo.cs" Line="14" Column="32" />
12+
<File FileName="XRIT/Properties/LibInfo.cs" Line="1" Column="1" />
13+
<File FileName="XRIT/Properties/AssemblyInfo.cs" Line="18" Column="56" />
1414
</Files>
1515
</MonoDevelop.Ide.Workbench>
1616
<MonoDevelop.Ide.DebuggingService.Breakpoints>

0 commit comments

Comments
 (0)