From 71bf9802efd609ffff3fd9c208187097d2d3245e Mon Sep 17 00:00:00 2001 From: jugstalt Date: Mon, 17 Oct 2022 16:21:36 +0200 Subject: [PATCH] GDAL Images: handle rasterband with ColorInterp.Undefined --- .../DataSources/GDAL/RasterClassV3.cs | 83 ++++++++++++++++--- 1 file changed, 71 insertions(+), 12 deletions(-) diff --git a/gView.DataSources.OSGeo/DataSources/GDAL/RasterClassV3.cs b/gView.DataSources.OSGeo/DataSources/GDAL/RasterClassV3.cs index 2b7314f7b..267a55afb 100644 --- a/gView.DataSources.OSGeo/DataSources/GDAL/RasterClassV3.cs +++ b/gView.DataSources.OSGeo/DataSources/GDAL/RasterClassV3.cs @@ -5,6 +5,7 @@ using gView.Framework.LinAlg; using gView.Framework.system; using gView.GraphicsEngine; +using Microsoft.SqlServer.Management.SqlParser.SqlCodeDom; using System; using System.Collections.Generic; using System.IO; @@ -73,9 +74,12 @@ public RasterClassV3(IRasterDataset dataset, string filename, IPolygon polygon) case ".gsd": _type = RasterType.grid; break; - //case ".jp2": - // _type = RasterType.wavelet; - // break; + case ".jp2": + if (gdalDataset.RasterCount == 4) + { + _type = RasterType.wavelet; + } + break; } using (OSGeo_v3.GDAL.Band band = gdalDataset.GetRasterBand(1)) @@ -371,13 +375,22 @@ private IRasterPaintContext PaintImage(int x, int y, int wWidth, int wHeight, in { List colors = new List(); - for (int i = 1; i <= (gdalDataset.RasterCount > 3 ? 3 : gdalDataset.RasterCount); ++i) + int rasterBands = /*gdalDataset.RasterCount; */(gdalDataset.RasterCount > 3 ? 3 : gdalDataset.RasterCount); + bool hasDefinedBands = false; + + for (int i = 1; i <= rasterBands; ++i) { using (OSGeo_v3.GDAL.Band band = gdalDataset.GetRasterBand(i)) { - int ch = 0; - switch ((ColorInterp)band.GetRasterColorInterpretation()) + var colorInterp = (ColorInterp)band.GetRasterColorInterpretation(); + + if (i == 1 && colorInterp != ColorInterp.Undefined) + { + hasDefinedBands = true; + } + + switch (colorInterp) { case ColorInterp.BlueBand: ch = 0; @@ -409,11 +422,25 @@ private IRasterPaintContext PaintImage(int x, int y, int wWidth, int wHeight, in colEntry.c4, colEntry.c1, colEntry.c2, colEntry.c3)); } + break; + case ColorInterp.Undefined: + if (hasDefinedBands) + { + continue; + } + + // If the bands are undefined, + // imply at that first three bands are red, green, blue...!? + ch = 3 - i; break; } - band.ReadRaster(x, y, wWidth, wHeight, - new IntPtr(buf.ToInt64() + ch), - iWidth, iHeight, OSGeo_v3.GDAL.DataType.GDT_Byte, pixelSpace, stride); + + if (ch >= 0 && ch <= 2) + { + band.ReadRaster(x, y, wWidth, wHeight, + new IntPtr(buf.ToInt64() + ch), + iWidth, iHeight, OSGeo_v3.GDAL.DataType.GDT_Byte, pixelSpace, stride); + } band.Dispose(); } @@ -487,13 +514,21 @@ private IRasterPaintContext PaintWavelet(int x, int y, int wWidth, int wHeight, using (var gdalDataset = OpenGdalDataset()) { - for (int i = 1; i <= (gdalDataset.RasterCount > 3 ? 3 : gdalDataset.RasterCount); ++i) + int rasterBands = /*gdalDataset.RasterCount; */(gdalDataset.RasterCount > 4 ? 4 : gdalDataset.RasterCount); + bool hasDefinedBands = false; + + for (int i = 1; i <= rasterBands; ++i) { using (OSGeo_v3.GDAL.Band band = gdalDataset.GetRasterBand(i)) { + var colorInterp = (ColorInterp)band.GetRasterColorInterpretation(); + if (i == 1 && colorInterp != ColorInterp.Undefined) + { + hasDefinedBands = true; + } int ch = 0; - switch ((ColorInterp)band.GetRasterColorInterpretation()) + switch (colorInterp) { case ColorInterp.BlueBand: ch = 0; @@ -503,11 +538,35 @@ private IRasterPaintContext PaintWavelet(int x, int y, int wWidth, int wHeight, break; case ColorInterp.RedBand: ch = 2; + break; + case ColorInterp.AlphaBand: + ch = 3; + break; + case ColorInterp.Undefined: + if(hasDefinedBands) + { + continue; + } + + // If the bands are undefined, + // imply at that first three bands are red, green, blue, alpha...!? + + ch = ((3 - i) + 4) % 4; + + // i=1: 2 + // i=2: 1 + // i=3: 0 + // i=4: 3 (3-4) + 4 = 3 % 4 = 3 + break; } - band.ReadRaster(x, y, wWidth, wHeight, + + if (ch >= 0 && ch <= 3) + { + band.ReadRaster(x, y, wWidth, wHeight, new IntPtr(buf.ToInt64() + ch), iWidth, iHeight, OSGeo_v3.GDAL.DataType.GDT_Byte, pixelSpace, stride); + } } }