diff --git a/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs b/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs index 2502ef76..4524e3dc 100644 --- a/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs +++ b/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs @@ -185,20 +185,53 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender public Image DecodeToImage(byte[] j2kData) { - if (m_useCSJ2K) - return J2kImage.FromBytes(j2kData); - else + if (j2kData == null || j2kData.Length == 0) + return null; + + if (!m_useCSJ2K) + return DecodeToImageWithOpenJPEG(j2kData); + + try { - ManagedImage mimage; - Image image; - if (OpenJPEG.DecodeToImage(j2kData, out mimage, out image)) - { - mimage = null; - return image; - } - else - return null; + return J2kImage.FromBytes(j2kData); } + catch (Exception csj2kException) + { + // NexVerse compatibility fallback: + // Some legacy OpenSim/OAR texture assets are valid enough for + // OpenJPEG but use a JP2/J2K layout that CSJ2K rejects (for + // example "EOF reached before finding Contiguous Codestream + // Box"). Fall back transparently before declaring the asset + // unreadable. + try + { + Image fallback = DecodeToImageWithOpenJPEG(j2kData); + if (fallback != null) + return fallback; + } + catch (Exception openJpegException) + { + throw new InvalidDataException( + "CSJ2K and OpenJPEG both failed to decode JPEG2000 texture data.", + new AggregateException(csj2kException, openJpegException)); + } + + throw new InvalidDataException( + "CSJ2K failed and OpenJPEG could not decode JPEG2000 texture data.", + csj2kException); + } + } + + private static Image DecodeToImageWithOpenJPEG(byte[] j2kData) + { + ManagedImage mimage; + Image image; + + if (!OpenJPEG.DecodeToImage(j2kData, out mimage, out image)) + return null; + + mimage = null; + return image; } diff --git a/OpenSim/Region/CoreModules/World/Warp3DMap/Warp3DImageModule.cs b/OpenSim/Region/CoreModules/World/Warp3DMap/Warp3DImageModule.cs index 299fd148..02271970 100644 --- a/OpenSim/Region/CoreModules/World/Warp3DMap/Warp3DImageModule.cs +++ b/OpenSim/Region/CoreModules/World/Warp3DMap/Warp3DImageModule.cs @@ -73,6 +73,13 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap private Dictionary m_warpTextures; private Dictionary m_colors; + // Keep map generation useful when an old/corrupt texture cannot be + // decoded. Report a specific texture UUID only once per region module + // lifetime instead of flooding the simulator console on every tile + // render. + private readonly HashSet m_textureDecodeWarnings = new HashSet(); + private readonly object m_textureDecodeWarningsLock = new object(); + private bool m_drawPrimVolume = true; // true if should render the prims on the tile private bool m_textureTerrain = true; // true if to create terrain splatting texture private bool m_textureAverageTerrain = false; // replace terrain textures by their average color @@ -750,12 +757,29 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap { try { - using (Bitmap img = (Bitmap)m_imgDecoder.DecodeToImage(asset.Data)) - ret = new warp_Texture(img, 8); // reduce textures size to 256 * 256 + Image decoded = m_imgDecoder?.DecodeToImage(asset.Data); + if (decoded != null) + { + using (decoded) + { + if (decoded is Bitmap img) + ret = new warp_Texture(img, 8); // reduce textures size to 256 * 256 + else + using (Bitmap bitmap = new Bitmap(decoded)) + ret = new warp_Texture(bitmap, 8); + } + } + else + { + LogTextureDecodeFailureOnce( + id, + sop, + "JPEG2000 decoder returned no image"); + } } catch (Exception e) { - m_log.WarnFormat("[Warp3D]: Failed to decode texture {0} for prim {1} at {2}, exception {3}", id.ToString(), sop.Name, sop.GetWorldPosition().ToString(), e.Message); + LogTextureDecodeFailureOnce(id, sop, e.Message); } } else @@ -766,6 +790,27 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap return ret; } + private void LogTextureDecodeFailureOnce( + UUID id, + SceneObjectPart sop, + string reason) + { + lock (m_textureDecodeWarningsLock) + { + if (!m_textureDecodeWarnings.Add(id)) + return; + } + + m_log.WarnFormat( + "[Warp3D]: Failed to decode texture {0} for prim {1} at {2}; " + + "CSJ2K/OpenJPEG fallback exhausted: {3}. Further warnings for " + + "this texture UUID are suppressed.", + id, + sop?.Name ?? "", + sop?.GetWorldPosition().ToString() ?? "", + reason); + } + #endregion Rendering Methods #region Static Helpers diff --git a/tools/ci/verify_simulator_runtime_profile.py b/tools/ci/verify_simulator_runtime_profile.py index 59921877..8d01be3a 100644 --- a/tools/ci/verify_simulator_runtime_profile.py +++ b/tools/ci/verify_simulator_runtime_profile.py @@ -79,6 +79,14 @@ else: if "m_NoColide = true;" in create_geom: errors.append("ubODE CreateGeom still disables collision on fallback geometry") +j2k_decoder = (ROOT / "OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs").read_text(encoding="utf-8") +if "DecodeToImageWithOpenJPEG" not in j2k_decoder or "CSJ2K and OpenJPEG" not in j2k_decoder: + errors.append("J2K decoder must retain the CSJ2K -> OpenJPEG compatibility fallback") + +warp3d = (ROOT / "OpenSim/Region/CoreModules/World/Warp3DMap/Warp3DImageModule.cs").read_text(encoding="utf-8") +if "m_textureDecodeWarnings" not in warp3d or "Further warnings for this texture UUID are suppressed" not in warp3d: + errors.append("Warp3D must suppress repeated decode warnings for the same texture UUID") + launcher = (ROOT / "bin/opensim.sh").read_text(encoding="utf-8") match = re.search(r"^\s*ulimit\s+-s\s+(\d+)\s*$", launcher, re.MULTILINE) if match is None or int(match.group(1)) < 262144: @@ -89,4 +97,4 @@ if errors: print("::error::" + error) sys.exit(1) -print("NexVerse simulator runtime profile verified: ubODE + solid mesh fallback + ubODEMeshmerizer + Warp3D + LSL XML-RPC RemoteData.") +print("NexVerse simulator runtime profile verified: ubODE + solid mesh fallback + ubODEMeshmerizer + Warp3D JPEG2000 fallback + LSL XML-RPC RemoteData.")