From ad309d3d0f6c1b95b07949b7cd510df6ceb6654c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 20 Jan 2025 21:59:53 +0100 Subject: [PATCH] Trade: fix moving between compressed and uncompressed ImageData. I mean, yeah, it's all bad, but at least it works now. The upcoming internal representation will not be this silly CompressedPixelStorage anymore and then it will become a bit less bad. For a proper language-lawyer-safe implementation I'd explicitly call destructors and then in-place-new the other instance and such, but that's two more branches and thus twice as many chances to mess up. --- src/Magnum/Trade/ImageData.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Magnum/Trade/ImageData.cpp b/src/Magnum/Trade/ImageData.cpp index 706b09257..46f75245a 100644 --- a/src/Magnum/Trade/ImageData.cpp +++ b/src/Magnum/Trade/ImageData.cpp @@ -119,11 +119,15 @@ template ImageData& ImageData::o swap(_dataFlags, other._dataFlags); swap(_compressed, other._compressed); swap(_flags, other._flags); - if(_compressed) { + /* Because the CompressedPixelStorage is larger than the + uncompressed, copy it if either of the sides is compressed to ensure no + compressed properties are lost. The _storage / _compressedStorage and + _format / _compressedFormat are unions of trivially copyable contents so + copying a type that's not there should be fine. */ + if(_compressed || other._compressed) { swap(_compressedStorage, other._compressedStorage); swap(_compressedFormat, other._compressedFormat); - } - else { + } else { swap(_storage, other._storage); swap(_format, other._format); }