| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Ivan Komissarov. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the DDS plugin in the Qt ImageFormats module. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #include "qddshandler.h" |
| 42 | |
| 43 | #include <QtCore/qdebug.h> |
| 44 | #include <QtGui/qimage.h> |
| 45 | |
| 46 | #include <cmath> |
| 47 | |
| 48 | #include "ddsheader.h" |
| 49 | |
| 50 | #include <cmath> |
| 51 | |
| 52 | #ifndef QT_NO_DATASTREAM |
| 53 | |
| 54 | QT_BEGIN_NAMESPACE |
| 55 | |
| 56 | enum Colors { |
| 57 | Red = 0, |
| 58 | Green, |
| 59 | Blue, |
| 60 | Alpha, |
| 61 | ColorCount |
| 62 | }; |
| 63 | |
| 64 | enum DXTVersions { |
| 65 | One = 1, |
| 66 | Two = 2, |
| 67 | Three = 3, |
| 68 | Four = 4, |
| 69 | Five = 5, |
| 70 | RXGB = 6 |
| 71 | }; |
| 72 | |
| 73 | // All magic numbers are little-endian as long as dds format has little |
| 74 | // endian byte order |
| 75 | static const quint32 ddsMagic = 0x20534444; // "DDS " |
| 76 | static const quint32 dx10Magic = 0x30315844; // "DX10" |
| 77 | |
| 78 | static const qint64 headerSize = 128; |
| 79 | static const quint32 ddsSize = 124; // headerSize without magic |
| 80 | static const quint32 pixelFormatSize = 32; |
| 81 | |
| 82 | struct FaceOffset |
| 83 | { |
| 84 | int x, y; |
| 85 | }; |
| 86 | |
| 87 | static const FaceOffset faceOffsets[6] = { {2, 1}, {0, 1}, {1, 0}, {1, 2}, {1, 1}, {3, 1} }; |
| 88 | |
| 89 | static int faceFlags[6] = { |
| 90 | DDSHeader::Caps2CubeMapPositiveX, |
| 91 | DDSHeader::Caps2CubeMapNegativeX, |
| 92 | DDSHeader::Caps2CubeMapPositiveY, |
| 93 | DDSHeader::Caps2CubeMapNegativeY, |
| 94 | DDSHeader::Caps2CubeMapPositiveZ, |
| 95 | DDSHeader::Caps2CubeMapNegativeZ |
| 96 | }; |
| 97 | |
| 98 | struct FormatInfo |
| 99 | { |
| 100 | Format format; |
| 101 | quint32 flags; |
| 102 | quint32 bitCount; |
| 103 | quint32 rBitMask; |
| 104 | quint32 gBitMask; |
| 105 | quint32 bBitMask; |
| 106 | quint32 aBitMask; |
| 107 | }; |
| 108 | |
| 109 | static const FormatInfo formatInfos[] = { |
| 110 | { FormatA8R8G8B8, DDSPixelFormat::FlagRGBA, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, |
| 111 | { FormatX8R8G8B8, DDSPixelFormat::FlagRGB, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, |
| 112 | { FormatA2B10G10R10, DDSPixelFormat::FlagRGBA, 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }, |
| 113 | { FormatA8B8G8R8, DDSPixelFormat::FlagRGBA, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, |
| 114 | { FormatX8B8G8R8, DDSPixelFormat::FlagRGB, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, |
| 115 | { FormatG16R16, DDSPixelFormat::FlagRGBA, 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, |
| 116 | { FormatG16R16, DDSPixelFormat::FlagRGB, 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, |
| 117 | { FormatA2R10G10B10, DDSPixelFormat::FlagRGBA, 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 }, |
| 118 | |
| 119 | { FormatR8G8B8, DDSPixelFormat::FlagRGB, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, |
| 120 | |
| 121 | { FormatR5G6B5, DDSPixelFormat::FlagRGB, 16, 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, |
| 122 | { FormatX1R5G5B5, DDSPixelFormat::FlagRGB, 16, 0x00007c00, 0x000003e0, 0x0000001f, 0x00000000 }, |
| 123 | { FormatA1R5G5B5, DDSPixelFormat::FlagRGBA, 16, 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, |
| 124 | { FormatA4R4G4B4, DDSPixelFormat::FlagRGBA, 16, 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, |
| 125 | { FormatA8R3G3B2, DDSPixelFormat::FlagRGBA, 16, 0x000000e0, 0x0000001c, 0x00000003, 0x0000ff00 }, |
| 126 | { FormatX4R4G4B4, DDSPixelFormat::FlagRGB, 16, 0x00000f00, 0x000000f0, 0x0000000f, 0x00000000 }, |
| 127 | { FormatA8L8, DDSPixelFormat::FlagLA, 16, 0x000000ff, 0x00000000, 0x00000000, 0x0000ff00 }, |
| 128 | { FormatL16, DDSPixelFormat::FlagLuminance, 16, 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, |
| 129 | |
| 130 | { FormatR3G3B2, DDSPixelFormat::FlagRGB, 8, 0x000000e0, 0x0000001c, 0x00000003, 0x00000000 }, |
| 131 | { FormatA8, DDSPixelFormat::FlagAlpha, 8, 0x00000000, 0x00000000, 0x00000000, 0x000000ff }, |
| 132 | { FormatL8, DDSPixelFormat::FlagLuminance, 8, 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, |
| 133 | { FormatA4L4, DDSPixelFormat::FlagLA, 8, 0x0000000f, 0x00000000, 0x00000000, 0x000000f0 }, |
| 134 | |
| 135 | { FormatV8U8, DDSPixelFormat::FlagNormal, 16, 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000 }, |
| 136 | { FormatL6V5U5, 0, 16, 0x0000001f, 0x000003e0, 0x0000fc00, 0x00000000 }, |
| 137 | { FormatX8L8V8U8, 0, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, |
| 138 | { FormatQ8W8V8U8, DDSPixelFormat::FlagNormal, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, |
| 139 | { FormatV16U16, DDSPixelFormat::FlagNormal, 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, |
| 140 | { FormatA2W10V10U10, DDSPixelFormat::FlagNormal, 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 } |
| 141 | }; |
| 142 | static const size_t formatInfosSize = sizeof(formatInfos)/sizeof(FormatInfo); |
| 143 | |
| 144 | static const Format knownFourCCs[] = { |
| 145 | FormatA16B16G16R16, |
| 146 | FormatV8U8, |
| 147 | FormatUYVY, |
| 148 | FormatR8G8B8G8, |
| 149 | FormatYUY2, |
| 150 | FormatG8R8G8B8, |
| 151 | FormatDXT1, |
| 152 | FormatDXT2, |
| 153 | FormatDXT3, |
| 154 | FormatDXT4, |
| 155 | FormatDXT5, |
| 156 | FormatRXGB, |
| 157 | FormatATI2, |
| 158 | FormatQ16W16V16U16, |
| 159 | FormatR16F, |
| 160 | FormatG16R16F, |
| 161 | FormatA16B16G16R16F, |
| 162 | FormatR32F, |
| 163 | FormatG32R32F, |
| 164 | FormatA32B32G32R32F, |
| 165 | FormatCxV8U8 |
| 166 | }; |
| 167 | static const size_t knownFourCCsSize = sizeof(knownFourCCs)/sizeof(Format); |
| 168 | |
| 169 | struct FormatName |
| 170 | { |
| 171 | Format format; |
| 172 | const char *const name; |
| 173 | }; |
| 174 | static const FormatName formatNames[] = { |
| 175 | { FormatUnknown, "unknown" }, |
| 176 | |
| 177 | { FormatR8G8B8, "R8G8B8" }, |
| 178 | { FormatA8R8G8B8, "A8R8G8B8" }, |
| 179 | { FormatX8R8G8B8, "X8R8G8B8" }, |
| 180 | { FormatR5G6B5, "R5G6B5" }, |
| 181 | { FormatX1R5G5B5, "X1R5G5B5" }, |
| 182 | { FormatA1R5G5B5, "A1R5G5B5" }, |
| 183 | { FormatA4R4G4B4, "A4R4G4B4" }, |
| 184 | { FormatR3G3B2, "R3G3B2" }, |
| 185 | { FormatA8, "A8" }, |
| 186 | { FormatA8R3G3B2, "A8R3G3B2" }, |
| 187 | { FormatX4R4G4B4, "X4R4G4B4" }, |
| 188 | { FormatA2B10G10R10, "A2B10G10R10" }, |
| 189 | { FormatA8B8G8R8, "A8B8G8R8" }, |
| 190 | { FormatX8B8G8R8, "X8B8G8R8" }, |
| 191 | { FormatG16R16, "G16R16" }, |
| 192 | { FormatA2R10G10B10, "A2R10G10B10" }, |
| 193 | { FormatA16B16G16R16, "A16B16G16R16" }, |
| 194 | |
| 195 | { FormatA8P8, "A8P8" }, |
| 196 | { FormatP8, "P8" }, |
| 197 | |
| 198 | { FormatL8, "L8" }, |
| 199 | { FormatA8L8, "A8L8" }, |
| 200 | { FormatA4L4, "A4L4" }, |
| 201 | |
| 202 | { FormatV8U8, "V8U8" }, |
| 203 | { FormatL6V5U5, "L6V5U5" }, |
| 204 | { FormatX8L8V8U8, "X8L8V8U8" }, |
| 205 | { FormatQ8W8V8U8, "Q8W8V8U8" }, |
| 206 | { FormatV16U16, "V16U16" }, |
| 207 | { FormatA2W10V10U10, "A2W10V10U10" }, |
| 208 | |
| 209 | { FormatUYVY, "UYVY" }, |
| 210 | { FormatR8G8B8G8, "R8G8_B8G8" }, |
| 211 | { FormatYUY2, "YUY2" }, |
| 212 | { FormatG8R8G8B8, "G8R8_G8B8" }, |
| 213 | { FormatDXT1, "DXT1" }, |
| 214 | { FormatDXT2, "DXT2" }, |
| 215 | { FormatDXT3, "DXT3" }, |
| 216 | { FormatDXT4, "DXT4" }, |
| 217 | { FormatDXT5, "DXT5" }, |
| 218 | { FormatRXGB, "RXGB" }, |
| 219 | { FormatATI2, "ATI2" }, |
| 220 | |
| 221 | { FormatD16Lockable, "D16Lockable" }, |
| 222 | { FormatD32, "D32" }, |
| 223 | { FormatD15S1, "D15S1" }, |
| 224 | { FormatD24S8, "D24S8" }, |
| 225 | { FormatD24X8, "D24X8" }, |
| 226 | { FormatD24X4S4, "D24X4S4" }, |
| 227 | { FormatD16, "D16" }, |
| 228 | |
| 229 | { FormatD32FLockable, "D32FLockable" }, |
| 230 | { FormatD24FS8, "D24FS8" }, |
| 231 | |
| 232 | { FormatD32Lockable, "D32Lockable" }, |
| 233 | { FormatS8Lockable, "S8Lockable" }, |
| 234 | |
| 235 | { FormatL16, "L16" }, |
| 236 | |
| 237 | { FormatVertexData, "VertexData" }, |
| 238 | { FormatIndex32, "Index32" }, |
| 239 | { FormatIndex32, "Index32" }, |
| 240 | |
| 241 | { FormatQ16W16V16U16, "Q16W16V16U16" }, |
| 242 | |
| 243 | { FormatMulti2ARGB8, "Multi2ARGB8" }, |
| 244 | |
| 245 | { FormatR16F, "R16F" }, |
| 246 | { FormatG16R16F, "G16R16F" }, |
| 247 | { FormatA16B16G16R16F, "A16B16G16R16F" }, |
| 248 | |
| 249 | { FormatR32F, "R32F" }, |
| 250 | { FormatG32R32F, "G32R32F" }, |
| 251 | { FormatA32B32G32R32F, "A32B32G32R32F" }, |
| 252 | |
| 253 | { FormatCxV8U8, "CxV8U8" }, |
| 254 | |
| 255 | { FormatA1, "A1" }, |
| 256 | { FormatA2B10G10R10_XR_BIAS, "A2B10G10R10_XR_BIAS" }, |
| 257 | { FormatBinaryBuffer, "BinaryBuffer" }, |
| 258 | |
| 259 | { FormatP4, "P4" }, |
| 260 | { FormatA4P4, "A4P4" } |
| 261 | }; |
| 262 | static const size_t formatNamesSize = sizeof(formatNames)/sizeof(FormatName); |
| 263 | |
| 264 | static inline int maskToShift(quint32 mask) |
| 265 | { |
| 266 | if (mask == 0) |
| 267 | return 0; |
| 268 | |
| 269 | int result = 0; |
| 270 | while (!((mask >> result) & 1)) |
| 271 | result++; |
| 272 | return result; |
| 273 | } |
| 274 | |
| 275 | static inline int maskLength(quint32 mask) |
| 276 | { |
| 277 | int result = 0; |
| 278 | while (mask) { |
| 279 | if (mask & 1) |
| 280 | result++; |
| 281 | mask >>= 1; |
| 282 | } |
| 283 | return result; |
| 284 | } |
| 285 | |
| 286 | static inline quint32 readValue(QDataStream &s, quint32 size) |
| 287 | { |
| 288 | Q_ASSERT(size == 8 || size == 16 || size == 24 || size == 32); |
| 289 | |
| 290 | quint32 value = 0; |
| 291 | quint8 tmp; |
| 292 | for (unsigned bit = 0; bit < size; bit += 8) { |
| 293 | s >> tmp; |
| 294 | value += (quint32(tmp) << bit); |
| 295 | } |
| 296 | return value; |
| 297 | } |
| 298 | |
| 299 | static inline bool hasAlpha(const DDSHeader &dds) |
| 300 | { |
| 301 | return (dds.pixelFormat.flags & (DDSPixelFormat::FlagAlphaPixels | DDSPixelFormat::FlagAlpha)) != 0; |
| 302 | } |
| 303 | |
| 304 | static inline bool isCubeMap(const DDSHeader &dds) |
| 305 | { |
| 306 | return (dds.caps2 & DDSHeader::Caps2CubeMap) != 0; |
| 307 | } |
| 308 | |
| 309 | static inline QRgb yuv2rgb(quint8 Y, quint8 U, quint8 V) |
| 310 | { |
| 311 | return qRgb(quint8(Y + 1.13983 * (V - 128)), |
| 312 | quint8(Y - 0.39465 * (U - 128) - 0.58060 * (V - 128)), |
| 313 | quint8(Y + 2.03211 * (U - 128))); |
| 314 | } |
| 315 | |
| 316 | static Format getFormat(const DDSHeader &dds) |
| 317 | { |
| 318 | const DDSPixelFormat &format = dds.pixelFormat; |
| 319 | if (format.flags & DDSPixelFormat::FlagPaletteIndexed4) { |
| 320 | return FormatP4; |
| 321 | } else if (format.flags & DDSPixelFormat::FlagPaletteIndexed8) { |
| 322 | return FormatP8; |
| 323 | } else if (format.flags & DDSPixelFormat::FlagFourCC) { |
| 324 | for (size_t i = 0; i < knownFourCCsSize; ++i) { |
| 325 | if (dds.pixelFormat.fourCC == knownFourCCs[i]) |
| 326 | return knownFourCCs[i]; |
| 327 | } |
| 328 | } else { |
| 329 | for (size_t i = 0; i < formatInfosSize; ++i) { |
| 330 | const FormatInfo &info = formatInfos[i]; |
| 331 | if ((format.flags & info.flags) == info.flags && |
| 332 | format.rgbBitCount == info.bitCount && |
| 333 | format.rBitMask == info.rBitMask && |
| 334 | format.gBitMask == info.gBitMask && |
| 335 | format.bBitMask == info.bBitMask && |
| 336 | format.aBitMask == info.aBitMask) { |
| 337 | return info.format; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return FormatUnknown; |
| 343 | } |
| 344 | |
| 345 | static inline quint8 getNormalZ(quint8 nx, quint8 ny) |
| 346 | { |
| 347 | const double fx = nx / 127.5 - 1.0; |
| 348 | const double fy = ny / 127.5 - 1.0; |
| 349 | const double fxfy = 1.0 - fx * fx - fy * fy; |
| 350 | return fxfy > 0 ? 255 * std::sqrt(fxfy) : 0; |
| 351 | } |
| 352 | |
| 353 | static inline void decodeColor(quint16 color, quint8 &red, quint8 &green, quint8 &blue) |
| 354 | { |
| 355 | red = ((color >> 11) & 0x1f) << 3; |
| 356 | green = ((color >> 5) & 0x3f) << 2; |
| 357 | blue = (color & 0x1f) << 3; |
| 358 | } |
| 359 | |
| 360 | static inline quint8 calcC2(quint8 c0, quint8 c1) |
| 361 | { |
| 362 | return 2.0 * c0 / 3.0 + c1 / 3.0; |
| 363 | } |
| 364 | |
| 365 | static inline quint8 calcC2a(quint8 c0, quint8 c1) |
| 366 | { |
| 367 | return c0 / 2.0 + c1 / 2.0; |
| 368 | } |
| 369 | |
| 370 | static inline quint8 calcC3(quint8 c0, quint8 c1) |
| 371 | { |
| 372 | return c0 / 3.0 + 2.0 * c1 / 3.0; |
| 373 | } |
| 374 | |
| 375 | static void DXTFillColors(QRgb *result, quint16 c0, quint16 c1, quint32 table, bool dxt1a = false) |
| 376 | { |
| 377 | quint8 r[4]; |
| 378 | quint8 g[4]; |
| 379 | quint8 b[4]; |
| 380 | quint8 a[4]; |
| 381 | |
| 382 | a[0] = a[1] = a[2] = a[3] = 255; |
| 383 | |
| 384 | decodeColor(c0, r[0], g[0], b[0]); |
| 385 | decodeColor(c1, r[1], g[1], b[1]); |
| 386 | if (!dxt1a) { |
| 387 | r[2] = calcC2(r[0], r[1]); |
| 388 | g[2] = calcC2(g[0], g[1]); |
| 389 | b[2] = calcC2(b[0], b[1]); |
| 390 | r[3] = calcC3(r[0], r[1]); |
| 391 | g[3] = calcC3(g[0], g[1]); |
| 392 | b[3] = calcC3(b[0], b[1]); |
| 393 | } else { |
| 394 | r[2] = calcC2a(r[0], r[1]); |
| 395 | g[2] = calcC2a(g[0], g[1]); |
| 396 | b[2] = calcC2a(b[0], b[1]); |
| 397 | r[3] = g[3] = b[3] = a[3] = 0; |
| 398 | } |
| 399 | |
| 400 | for (int k = 0; k < 4; k++) |
| 401 | for (int l = 0; l < 4; l++) { |
| 402 | unsigned index = table & 0x0003; |
| 403 | table >>= 2; |
| 404 | |
| 405 | result[k * 4 + l] = qRgba(r[index], g[index], b[index], a[index]); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | template <DXTVersions version> |
| 410 | inline void setAlphaDXT32Helper(QRgb *rgbArr, quint64 alphas) |
| 411 | { |
| 412 | Q_STATIC_ASSERT(version == Two || version == Three); |
| 413 | for (int i = 0; i < 16; i++) { |
| 414 | quint8 alpha = 16 * (alphas & 0x0f); |
| 415 | QRgb rgb = rgbArr[i]; |
| 416 | if (version == Two) // DXT2 |
| 417 | rgbArr[i] = qRgba(qRed(rgb) * alpha / 0xff, qGreen(rgb) * alpha / 0xff, qBlue(rgb) * alpha / 0xff, alpha); |
| 418 | else if (version == Three) // DXT3 |
| 419 | rgbArr[i] = qRgba(qRed(rgb), qGreen(rgb), qBlue(rgb), alpha); |
| 420 | alphas = alphas >> 4; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | template <DXTVersions version> |
| 425 | inline void setAlphaDXT45Helper(QRgb *rgbArr, quint64 alphas) |
| 426 | { |
| 427 | Q_STATIC_ASSERT(version == Four || version == Five); |
| 428 | quint8 a[8]; |
| 429 | a[0] = alphas & 0xff; |
| 430 | a[1] = (alphas >> 8) & 0xff; |
| 431 | if (a[0] > a[1]) { |
| 432 | a[2] = (6*a[0] + 1*a[1]) / 7; |
| 433 | a[3] = (5*a[0] + 2*a[1]) / 7; |
| 434 | a[4] = (4*a[0] + 3*a[1]) / 7; |
| 435 | a[5] = (3*a[0] + 4*a[1]) / 7; |
| 436 | a[6] = (2*a[0] + 5*a[1]) / 7; |
| 437 | a[7] = (1*a[0] + 6*a[1]) / 7; |
| 438 | } else { |
| 439 | a[2] = (4*a[0] + 1*a[1]) / 5; |
| 440 | a[3] = (3*a[0] + 2*a[1]) / 5; |
| 441 | a[4] = (2*a[0] + 3*a[1]) / 5; |
| 442 | a[5] = (1*a[0] + 4*a[1]) / 5; |
| 443 | a[6] = 0; |
| 444 | a[7] = 255; |
| 445 | } |
| 446 | alphas >>= 16; |
| 447 | for (int i = 0; i < 16; i++) { |
| 448 | quint8 index = alphas & 0x07; |
| 449 | quint8 alpha = a[index]; |
| 450 | QRgb rgb = rgbArr[i]; |
| 451 | if (version == Four) // DXT4 |
| 452 | rgbArr[i] = qRgba(qRed(rgb) * alpha / 0xff, qGreen(rgb) * alpha / 0xff, qBlue(rgb) * alpha / 0xff, alpha); |
| 453 | else if (version == Five) // DXT5 |
| 454 | rgbArr[i] = qRgba(qRed(rgb), qGreen(rgb), qBlue(rgb), alpha); |
| 455 | alphas = alphas >> 3; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | template <DXTVersions version> |
| 460 | inline void setAlphaDXT(QRgb *rgbArr, quint64 alphas) |
| 461 | { |
| 462 | Q_UNUSED(rgbArr); |
| 463 | Q_UNUSED(alphas); |
| 464 | } |
| 465 | |
| 466 | template <> |
| 467 | inline void setAlphaDXT<Two>(QRgb *rgbArr, quint64 alphas) |
| 468 | { |
| 469 | setAlphaDXT32Helper<Two>(rgbArr, alphas); |
| 470 | } |
| 471 | |
| 472 | template <> |
| 473 | inline void setAlphaDXT<Three>(QRgb *rgbArr, quint64 alphas) |
| 474 | { |
| 475 | setAlphaDXT32Helper<Three>(rgbArr, alphas); |
| 476 | } |
| 477 | |
| 478 | template <> |
| 479 | inline void setAlphaDXT<Four>(QRgb *rgbArr, quint64 alphas) |
| 480 | { |
| 481 | setAlphaDXT45Helper<Four>(rgbArr, alphas); |
| 482 | } |
| 483 | |
| 484 | template <> |
| 485 | inline void setAlphaDXT<Five>(QRgb *rgbArr, quint64 alphas) |
| 486 | { |
| 487 | setAlphaDXT45Helper<Five>(rgbArr, alphas); |
| 488 | } |
| 489 | |
| 490 | template <> |
| 491 | inline void setAlphaDXT<RXGB>(QRgb *rgbArr, quint64 alphas) |
| 492 | { |
| 493 | setAlphaDXT45Helper<Five>(rgbArr, alphas); |
| 494 | } |
| 495 | |
| 496 | static inline QRgb invertRXGBColors(QRgb pixel) |
| 497 | { |
| 498 | return qRgb(qAlpha(pixel), qGreen(pixel), qBlue(pixel)); |
| 499 | } |
| 500 | |
| 501 | template <DXTVersions version> |
| 502 | static QImage readDXT(QDataStream &s, quint32 width, quint32 height) |
| 503 | { |
| 504 | QImage::Format format = (version == Two || version == Four) ? |
| 505 | QImage::Format_ARGB32_Premultiplied : QImage::Format_ARGB32; |
| 506 | |
| 507 | QImage image(width, height, format); |
| 508 | |
| 509 | for (quint32 i = 0; i < height; i += 4) { |
| 510 | for (quint32 j = 0; j < width; j += 4) { |
| 511 | quint64 alpha = 0; |
| 512 | quint16 c0, c1; |
| 513 | quint32 table; |
| 514 | if (version != One) |
| 515 | s >> alpha; |
| 516 | s >> c0; |
| 517 | s >> c1; |
| 518 | s >> table; |
| 519 | |
| 520 | QRgb arr[16]; |
| 521 | |
| 522 | DXTFillColors(arr, c0, c1, table, version == One && c0 <= c1); |
| 523 | setAlphaDXT<version>(arr, alpha); |
| 524 | |
| 525 | const quint32 kMax = qMin<quint32>(4, height - i); |
| 526 | const quint32 lMax = qMin<quint32>(4, width - j); |
| 527 | for (quint32 k = 0; k < kMax; k++) { |
| 528 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(i + k)); |
| 529 | for (quint32 l = 0; l < lMax; l++) { |
| 530 | QRgb pixel = arr[k * 4 + l]; |
| 531 | if (version == RXGB) |
| 532 | pixel = invertRXGBColors(pixel); |
| 533 | |
| 534 | line[j + l] = pixel; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | return image; |
| 540 | } |
| 541 | |
| 542 | static inline QImage readDXT1(QDataStream &s, quint32 width, quint32 height) |
| 543 | { |
| 544 | return readDXT<One>(s, width, height); |
| 545 | } |
| 546 | |
| 547 | static inline QImage readDXT2(QDataStream &s, quint32 width, quint32 height) |
| 548 | { |
| 549 | return readDXT<Two>(s, width, height); |
| 550 | } |
| 551 | |
| 552 | static inline QImage readDXT3(QDataStream &s, quint32 width, quint32 height) |
| 553 | { |
| 554 | return readDXT<Three>(s, width, height); |
| 555 | } |
| 556 | |
| 557 | static inline QImage readDXT4(QDataStream &s, quint32 width, quint32 height) |
| 558 | { |
| 559 | return readDXT<Four>(s, width, height); |
| 560 | } |
| 561 | |
| 562 | static inline QImage readDXT5(QDataStream &s, quint32 width, quint32 height) |
| 563 | { |
| 564 | return readDXT<Five>(s, width, height); |
| 565 | } |
| 566 | |
| 567 | static inline QImage readRXGB(QDataStream &s, quint32 width, quint32 height) |
| 568 | { |
| 569 | return readDXT<RXGB>(s, width, height); |
| 570 | } |
| 571 | |
| 572 | static QImage readATI2(QDataStream &s, quint32 width, quint32 height) |
| 573 | { |
| 574 | QImage image(width, height, QImage::Format_RGB32); |
| 575 | |
| 576 | for (quint32 i = 0; i < height; i += 4) { |
| 577 | for (quint32 j = 0; j < width; j += 4) { |
| 578 | quint64 alpha1; |
| 579 | quint64 alpha2; |
| 580 | s >> alpha1; |
| 581 | s >> alpha2; |
| 582 | |
| 583 | QRgb arr[16]; |
| 584 | memset(arr, 0, sizeof(QRgb) * 16); |
| 585 | setAlphaDXT<Five>(arr, alpha1); |
| 586 | for (int k = 0; k < 16; ++k) { |
| 587 | quint8 a = qAlpha(arr[k]); |
| 588 | arr[k] = qRgba(0, 0, a, 0); |
| 589 | } |
| 590 | setAlphaDXT<Five>(arr, alpha2); |
| 591 | |
| 592 | const quint32 kMax = qMin<quint32>(4, height - i); |
| 593 | const quint32 lMax = qMin<quint32>(4, width - j); |
| 594 | for (quint32 k = 0; k < kMax; k++) { |
| 595 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(i + k)); |
| 596 | for (quint32 l = 0; l < lMax; l++) { |
| 597 | QRgb pixel = arr[k * 4 + l]; |
| 598 | const quint8 nx = qAlpha(pixel); |
| 599 | const quint8 ny = qBlue(pixel); |
| 600 | const quint8 nz = getNormalZ(nx, ny); |
| 601 | line[j + l] = qRgb(nx, ny, nz); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | return image; |
| 607 | } |
| 608 | |
| 609 | static QImage readUnsignedImage(QDataStream &s, const DDSHeader &dds, quint32 width, quint32 height, bool hasAlpha) |
| 610 | { |
| 611 | quint32 flags = dds.pixelFormat.flags; |
| 612 | |
| 613 | quint32 masks[ColorCount]; |
| 614 | quint8 shifts[ColorCount]; |
| 615 | quint8 bits[ColorCount]; |
| 616 | masks[Red] = dds.pixelFormat.rBitMask; |
| 617 | masks[Green] = dds.pixelFormat.gBitMask; |
| 618 | masks[Blue] = dds.pixelFormat.bBitMask; |
| 619 | masks[Alpha] = hasAlpha ? dds.pixelFormat.aBitMask : 0; |
| 620 | for (int i = 0; i < ColorCount; ++i) { |
| 621 | shifts[i] = maskToShift(masks[i]); |
| 622 | bits[i] = maskLength(masks[i]); |
| 623 | |
| 624 | // move mask to the left |
| 625 | if (bits[i] <= 8) |
| 626 | masks[i] = (masks[i] >> shifts[i]) << (8 - bits[i]); |
| 627 | } |
| 628 | |
| 629 | const QImage::Format format = hasAlpha ? QImage::Format_ARGB32 : QImage::Format_RGB32; |
| 630 | |
| 631 | QImage image(width, height, format); |
| 632 | |
| 633 | for (quint32 y = 0; y < height; y++) { |
| 634 | for (quint32 x = 0; x < width; x++) { |
| 635 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 636 | |
| 637 | quint32 value = readValue(s, dds.pixelFormat.rgbBitCount); |
| 638 | quint8 colors[ColorCount]; |
| 639 | |
| 640 | for (int c = 0; c < ColorCount; ++c) { |
| 641 | if (bits[c] > 8) { |
| 642 | // truncate unneseccary bits |
| 643 | colors[c] = (value & masks[c]) >> shifts[c] >> (bits[c] - 8); |
| 644 | } else { |
| 645 | // move color to the left |
| 646 | quint8 color = value >> shifts[c] << (8 - bits[c]) & masks[c]; |
| 647 | if (masks[c]) |
| 648 | colors[c] = color * 0xff / masks[c]; |
| 649 | else |
| 650 | colors[c] = 0; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (flags & DDSPixelFormat::FlagLuminance) |
| 655 | line[x] = qRgba(colors[Red], colors[Red], colors[Red], colors[Alpha]); |
| 656 | else if (flags & DDSPixelFormat::FlagYUV) |
| 657 | line[x] = yuv2rgb(colors[Red], colors[Green], colors[Blue]); |
| 658 | else |
| 659 | line[x] = qRgba(colors[Red], colors[Green], colors[Blue], colors[Alpha]); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | return image; |
| 664 | } |
| 665 | |
| 666 | static double readFloat16(QDataStream &s) |
| 667 | { |
| 668 | quint16 value; |
| 669 | s >> value; |
| 670 | |
| 671 | double sign = (value & 0x8000) == 0x8000 ? -1.0 : 1.0; |
| 672 | qint8 exp = (value & 0x7C00) >> 10; |
| 673 | quint16 fraction = value & 0x3FF; |
| 674 | |
| 675 | if (exp == 0) |
| 676 | return sign * std::pow(2.0, -14.0) * fraction / 1024.0; |
| 677 | else |
| 678 | return sign * std::pow(2.0, exp - 15) * (1 + fraction / 1024.0); |
| 679 | } |
| 680 | |
| 681 | static inline float readFloat32(QDataStream &s) |
| 682 | { |
| 683 | Q_ASSERT(sizeof(float) == 4); |
| 684 | float value; |
| 685 | // TODO: find better way to avoid setting precision each time |
| 686 | QDataStream::FloatingPointPrecision precision = s.floatingPointPrecision(); |
| 687 | s.setFloatingPointPrecision(QDataStream::SinglePrecision); |
| 688 | s >> value; |
| 689 | s.setFloatingPointPrecision(precision); |
| 690 | return value; |
| 691 | } |
| 692 | |
| 693 | static QImage readR16F(QDataStream &s, const quint32 width, const quint32 height) |
| 694 | { |
| 695 | QImage image(width, height, QImage::Format_RGB32); |
| 696 | |
| 697 | for (quint32 y = 0; y < height; y++) { |
| 698 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 699 | for (quint32 x = 0; x < width; x++) { |
| 700 | quint8 r = readFloat16(s) * 255; |
| 701 | line[x] = qRgba(r, 0, 0, 0); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return image; |
| 706 | } |
| 707 | |
| 708 | static QImage readRG16F(QDataStream &s, const quint32 width, const quint32 height) |
| 709 | { |
| 710 | QImage image(width, height, QImage::Format_RGB32); |
| 711 | |
| 712 | for (quint32 y = 0; y < height; y++) { |
| 713 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 714 | for (quint32 x = 0; x < width; x++) { |
| 715 | quint8 r = readFloat16(s) * 255; |
| 716 | quint8 g = readFloat16(s) * 255; |
| 717 | line[x] = qRgba(r, g, 0, 0); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | return image; |
| 722 | } |
| 723 | |
| 724 | static QImage readARGB16F(QDataStream &s, const quint32 width, const quint32 height) |
| 725 | { |
| 726 | QImage image(width, height, QImage::Format_ARGB32); |
| 727 | |
| 728 | for (quint32 y = 0; y < height; y++) { |
| 729 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 730 | for (quint32 x = 0; x < width; x++) { |
| 731 | quint8 colors[ColorCount]; |
| 732 | for (int c = 0; c < ColorCount; ++c) |
| 733 | colors[c] = readFloat16(s) * 255; |
| 734 | |
| 735 | line[x] = qRgba(colors[Red], colors[Green], colors[Blue], colors[Alpha]); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | return image; |
| 740 | } |
| 741 | |
| 742 | static QImage readR32F(QDataStream &s, const quint32 width, const quint32 height) |
| 743 | { |
| 744 | QImage image(width, height, QImage::Format_RGB32); |
| 745 | |
| 746 | for (quint32 y = 0; y < height; y++) { |
| 747 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 748 | for (quint32 x = 0; x < width; x++) { |
| 749 | quint8 r = readFloat32(s) * 255; |
| 750 | line[x] = qRgba(r, 0, 0, 0); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | return image; |
| 755 | } |
| 756 | |
| 757 | static QImage readRG32F(QDataStream &s, const quint32 width, const quint32 height) |
| 758 | { |
| 759 | QImage image(width, height, QImage::Format_RGB32); |
| 760 | |
| 761 | for (quint32 y = 0; y < height; y++) { |
| 762 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 763 | for (quint32 x = 0; x < width; x++) { |
| 764 | quint8 r = readFloat32(s) * 255; |
| 765 | quint8 g = readFloat32(s) * 255; |
| 766 | line[x] = qRgba(r, g, 0, 0); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | return image; |
| 771 | } |
| 772 | |
| 773 | static QImage readARGB32F(QDataStream &s, const quint32 width, const quint32 height) |
| 774 | { |
| 775 | QImage image(width, height, QImage::Format_ARGB32); |
| 776 | |
| 777 | for (quint32 y = 0; y < height; y++) { |
| 778 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 779 | for (quint32 x = 0; x < width; x++) { |
| 780 | quint8 colors[ColorCount]; |
| 781 | for (int c = 0; c < ColorCount; ++c) |
| 782 | colors[c] = readFloat32(s) * 255; |
| 783 | line[x] = qRgba(colors[Red], colors[Green], colors[Blue], colors[Alpha]); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | return image; |
| 788 | } |
| 789 | |
| 790 | static QImage readQ16W16V16U16(QDataStream &s, const quint32 width, const quint32 height) |
| 791 | { |
| 792 | QImage image(width, height, QImage::Format_ARGB32); |
| 793 | |
| 794 | quint8 colors[ColorCount]; |
| 795 | qint16 tmp; |
| 796 | for (quint32 y = 0; y < height; y++) { |
| 797 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 798 | for (quint32 x = 0; x < width; x++) { |
| 799 | for (int i = 0; i < ColorCount; i++) { |
| 800 | s >> tmp; |
| 801 | colors[i] = (tmp + 0x7FFF) >> 8; |
| 802 | } |
| 803 | line[x] = qRgba(colors[Red], colors[Green], colors[Blue], colors[Alpha]); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | return image; |
| 808 | } |
| 809 | |
| 810 | static QImage readCxV8U8(QDataStream &s, const quint32 width, const quint32 height) |
| 811 | { |
| 812 | QImage image(width, height, QImage::Format_RGB32); |
| 813 | |
| 814 | for (quint32 y = 0; y < height; y++) { |
| 815 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 816 | for (quint32 x = 0; x < width; x++) { |
| 817 | qint8 v, u; |
| 818 | s >> v >> u; |
| 819 | |
| 820 | const quint8 vn = v + 128; |
| 821 | const quint8 un = u + 128; |
| 822 | const quint8 c = getNormalZ(vn, un); |
| 823 | |
| 824 | line[x] = qRgb(vn, un, c); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | return image; |
| 829 | } |
| 830 | |
| 831 | static QImage readPalette8Image(QDataStream &s, quint32 width, quint32 height) |
| 832 | { |
| 833 | QImage image(width, height, QImage::Format_Indexed8); |
| 834 | for (int i = 0; i < 256; ++i) { |
| 835 | quint8 r, g, b, a; |
| 836 | s >> r >> g >> b >> a; |
| 837 | image.setColor(i, qRgba(r, g, b, a)); |
| 838 | } |
| 839 | |
| 840 | for (quint32 y = 0; y < height; y++) { |
| 841 | for (quint32 x = 0; x < width; x++) { |
| 842 | quint8 index; |
| 843 | s >> index; |
| 844 | image.setPixel(x, y, index); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | return image; |
| 849 | } |
| 850 | |
| 851 | static QImage readPalette4Image(QDataStream &s, quint32 width, quint32 height) |
| 852 | { |
| 853 | QImage image(width, height, QImage::Format_Indexed8); |
| 854 | for (int i = 0; i < 16; ++i) { |
| 855 | quint8 r, g, b, a; |
| 856 | s >> r >> g >> b >> a; |
| 857 | image.setColor(i, qRgba(r, g, b, a)); |
| 858 | } |
| 859 | |
| 860 | for (quint32 y = 0; y < height; y++) { |
| 861 | quint8 index; |
| 862 | for (quint32 x = 0; x < width - 1; ) { |
| 863 | s >> index; |
| 864 | image.setPixel(x++, y, (index & 0x0f) >> 0); |
| 865 | image.setPixel(x++, y, (index & 0xf0) >> 4); |
| 866 | } |
| 867 | if (width % 2 == 1) { |
| 868 | s >> index; |
| 869 | image.setPixel(width - 1, y, (index & 0x0f) >> 0); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | return image; |
| 874 | } |
| 875 | |
| 876 | static QImage readARGB16(QDataStream &s, quint32 width, quint32 height) |
| 877 | { |
| 878 | QImage image(width, height, QImage::Format_ARGB32); |
| 879 | |
| 880 | for (quint32 y = 0; y < height; y++) { |
| 881 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 882 | for (quint32 x = 0; x < width; x++) { |
| 883 | quint8 colors[ColorCount]; |
| 884 | for (int i = 0; i < ColorCount; ++i) { |
| 885 | quint16 color; |
| 886 | s >> color; |
| 887 | colors[i] = quint8(color >> 8); |
| 888 | } |
| 889 | line[x] = qRgba(colors[Red], colors[Green], colors[Blue], colors[Alpha]); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | return image; |
| 894 | } |
| 895 | |
| 896 | static QImage readV8U8(QDataStream &s, quint32 width, quint32 height) |
| 897 | { |
| 898 | QImage image(width, height, QImage::Format_RGB32); |
| 899 | |
| 900 | for (quint32 y = 0; y < height; y++) { |
| 901 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 902 | for (quint32 x = 0; x < width; x++) { |
| 903 | qint8 v, u; |
| 904 | s >> v >> u; |
| 905 | line[x] = qRgb(v + 128, u + 128, 255); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | return image; |
| 910 | } |
| 911 | |
| 912 | static QImage readL6V5U5(QDataStream &s, quint32 width, quint32 height) |
| 913 | { |
| 914 | QImage image(width, height, QImage::Format_ARGB32); |
| 915 | |
| 916 | quint16 tmp; |
| 917 | for (quint32 y = 0; y < height; y++) { |
| 918 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 919 | for (quint32 x = 0; x < width; x++) { |
| 920 | s >> tmp; |
| 921 | quint8 r = qint8((tmp & 0x001f) >> 0) * 0xff/0x1f + 128; |
| 922 | quint8 g = qint8((tmp & 0x03e0) >> 5) * 0xff/0x1f + 128; |
| 923 | quint8 b = quint8((tmp & 0xfc00) >> 10) * 0xff/0x3f; |
| 924 | line[x] = qRgba(r, g, 0xff, b); |
| 925 | } |
| 926 | } |
| 927 | return image; |
| 928 | } |
| 929 | |
| 930 | static QImage readX8L8V8U8(QDataStream &s, quint32 width, quint32 height) |
| 931 | { |
| 932 | QImage image(width, height, QImage::Format_ARGB32); |
| 933 | |
| 934 | quint8 a, l; |
| 935 | qint8 v, u; |
| 936 | for (quint32 y = 0; y < height; y++) { |
| 937 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 938 | for (quint32 x = 0; x < width; x++) { |
| 939 | s >> v >> u >> a >> l; |
| 940 | line[x] = qRgba(v + 128, u + 128, 255, a); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | return image; |
| 945 | } |
| 946 | |
| 947 | static QImage readQ8W8V8U8(QDataStream &s, quint32 width, quint32 height) |
| 948 | { |
| 949 | QImage image(width, height, QImage::Format_ARGB32); |
| 950 | |
| 951 | quint8 colors[ColorCount]; |
| 952 | qint8 tmp; |
| 953 | for (quint32 y = 0; y < height; y++) { |
| 954 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 955 | for (quint32 x = 0; x < width; x++) { |
| 956 | for (int i = 0; i < ColorCount; i++) { |
| 957 | s >> tmp; |
| 958 | colors[i] = tmp + 128; |
| 959 | } |
| 960 | line[x] = qRgba(colors[Red], colors[Green], colors[Blue], colors[Alpha]); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | return image; |
| 965 | } |
| 966 | |
| 967 | static QImage readV16U16(QDataStream &s, quint32 width, quint32 height) |
| 968 | { |
| 969 | QImage image(width, height, QImage::Format_RGB32); |
| 970 | |
| 971 | for (quint32 y = 0; y < height; y++) { |
| 972 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 973 | for (quint32 x = 0; x < width; x++) { |
| 974 | qint16 v, u; |
| 975 | s >> v >> u; |
| 976 | v = (v + 0x8000) >> 8; |
| 977 | u = (u + 0x8000) >> 8; |
| 978 | line[x] = qRgb(v, u, 255); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | return image; |
| 983 | } |
| 984 | |
| 985 | static QImage readA2W10V10U10(QDataStream &s, quint32 width, quint32 height) |
| 986 | { |
| 987 | QImage image(width, height, QImage::Format_ARGB32); |
| 988 | |
| 989 | quint32 tmp; |
| 990 | for (quint32 y = 0; y < height; y++) { |
| 991 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 992 | for (quint32 x = 0; x < width; x++) { |
| 993 | s >> tmp; |
| 994 | quint8 r = qint8((tmp & 0x3ff00000) >> 20 >> 2) + 128; |
| 995 | quint8 g = qint8((tmp & 0x000ffc00) >> 10 >> 2) + 128; |
| 996 | quint8 b = qint8((tmp & 0x000003ff) >> 0 >> 2) + 128; |
| 997 | quint8 a = 0xff * ((tmp & 0xc0000000) >> 30) / 3; |
| 998 | // dunno why we should swap b and r here |
| 999 | std::swap(b, r); |
| 1000 | line[x] = qRgba(r, g, b, a); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | return image; |
| 1005 | } |
| 1006 | |
| 1007 | static QImage readUYVY(QDataStream &s, quint32 width, quint32 height) |
| 1008 | { |
| 1009 | QImage image(width, height, QImage::Format_RGB32); |
| 1010 | |
| 1011 | quint8 uyvy[4]; |
| 1012 | for (quint32 y = 0; y < height; y++) { |
| 1013 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 1014 | for (quint32 x = 0; x < width - 1; ) { |
| 1015 | s >> uyvy[0] >> uyvy[1] >> uyvy[2] >> uyvy[3]; |
| 1016 | line[x++] = yuv2rgb(uyvy[1], uyvy[0], uyvy[2]); |
| 1017 | line[x++] = yuv2rgb(uyvy[3], uyvy[0], uyvy[2]); |
| 1018 | } |
| 1019 | if (width % 2 == 1) { |
| 1020 | s >> uyvy[0] >> uyvy[1] >> uyvy[2] >> uyvy[3]; |
| 1021 | line[width - 1] = yuv2rgb(uyvy[1], uyvy[0], uyvy[2]); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | return image; |
| 1026 | } |
| 1027 | |
| 1028 | static QImage readR8G8B8G8(QDataStream &s, quint32 width, quint32 height) |
| 1029 | { |
| 1030 | QImage image(width, height, QImage::Format_RGB32); |
| 1031 | quint8 rgbg[4]; |
| 1032 | for (quint32 y = 0; y < height; y++) { |
| 1033 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 1034 | for (quint32 x = 0; x < width - 1; ) { |
| 1035 | s >> rgbg[1] >> rgbg[0] >> rgbg[3] >> rgbg[2]; |
| 1036 | line[x++] = qRgb(rgbg[0], rgbg[1], rgbg[2]); |
| 1037 | line[x++] = qRgb(rgbg[0], rgbg[3], rgbg[2]); |
| 1038 | } |
| 1039 | if (width % 2 == 1) { |
| 1040 | s >> rgbg[1] >> rgbg[0] >> rgbg[3] >> rgbg[2]; |
| 1041 | line[width - 1] = qRgb(rgbg[0], rgbg[1], rgbg[2]); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | return image; |
| 1046 | } |
| 1047 | |
| 1048 | static QImage readYUY2(QDataStream &s, quint32 width, quint32 height) |
| 1049 | { |
| 1050 | QImage image(width, height, QImage::Format_RGB32); |
| 1051 | |
| 1052 | quint8 yuyv[4]; |
| 1053 | for (quint32 y = 0; y < height; y++) { |
| 1054 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 1055 | for (quint32 x = 0; x < width - 1; ) { |
| 1056 | s >> yuyv[0] >> yuyv[1] >> yuyv[2] >> yuyv[3]; |
| 1057 | line[x++] = yuv2rgb(yuyv[0], yuyv[1], yuyv[3]); |
| 1058 | line[x++] = yuv2rgb(yuyv[2], yuyv[1], yuyv[3]); |
| 1059 | } |
| 1060 | if (width % 2 == 1) { |
| 1061 | s >> yuyv[0] >> yuyv[1] >> yuyv[2] >> yuyv[3]; |
| 1062 | line[width - 1] = yuv2rgb(yuyv[2], yuyv[1], yuyv[3]); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | return image; |
| 1067 | } |
| 1068 | |
| 1069 | static QImage readG8R8G8B8(QDataStream &s, quint32 width, quint32 height) |
| 1070 | { |
| 1071 | QImage image(width, height, QImage::Format_RGB32); |
| 1072 | quint8 grgb[4]; |
| 1073 | for (quint32 y = 0; y < height; y++) { |
| 1074 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 1075 | for (quint32 x = 0; x < width - 1; ) { |
| 1076 | s >> grgb[1] >> grgb[0] >> grgb[3] >> grgb[2]; |
| 1077 | line[x++] = qRgb(grgb[1], grgb[0], grgb[3]); |
| 1078 | line[x++] = qRgb(grgb[1], grgb[2], grgb[3]); |
| 1079 | } |
| 1080 | if (width % 2 == 1) { |
| 1081 | s >> grgb[1] >> grgb[0] >> grgb[3] >> grgb[2]; |
| 1082 | line[width - 1] = qRgb(grgb[1], grgb[0], grgb[3]); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | return image; |
| 1087 | } |
| 1088 | |
| 1089 | static QImage readA2R10G10B10(QDataStream &s, const DDSHeader &dds, quint32 width, quint32 height) |
| 1090 | { |
| 1091 | QImage image = readUnsignedImage(s, dds, width, height, true); |
| 1092 | for (quint32 y = 0; y < height; y++) { |
| 1093 | QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)); |
| 1094 | for (quint32 x = 0; x < width; x++) { |
| 1095 | QRgb pixel = image.pixel(x, y); |
| 1096 | line[x] = qRgba(qBlue(pixel), qGreen(pixel), qRed(pixel), qAlpha(pixel)); |
| 1097 | } |
| 1098 | } |
| 1099 | return image; |
| 1100 | } |
| 1101 | |
| 1102 | static QImage readLayer(QDataStream &s, const DDSHeader &dds, const int format, quint32 width, quint32 height) |
| 1103 | { |
| 1104 | if (width * height == 0) |
| 1105 | return QImage(); |
| 1106 | |
| 1107 | switch (format) { |
| 1108 | case FormatR8G8B8: |
| 1109 | case FormatX8R8G8B8: |
| 1110 | case FormatR5G6B5: |
| 1111 | case FormatR3G3B2: |
| 1112 | case FormatX1R5G5B5: |
| 1113 | case FormatX4R4G4B4: |
| 1114 | case FormatX8B8G8R8: |
| 1115 | case FormatG16R16: |
| 1116 | case FormatL8: |
| 1117 | case FormatL16: |
| 1118 | return readUnsignedImage(s, dds, width, height, false); |
| 1119 | case FormatA8R8G8B8: |
| 1120 | case FormatA1R5G5B5: |
| 1121 | case FormatA4R4G4B4: |
| 1122 | case FormatA8: |
| 1123 | case FormatA8R3G3B2: |
| 1124 | case FormatA8B8G8R8: |
| 1125 | case FormatA8L8: |
| 1126 | case FormatA4L4: |
| 1127 | return readUnsignedImage(s, dds, width, height, true); |
| 1128 | case FormatA2R10G10B10: |
| 1129 | case FormatA2B10G10R10: |
| 1130 | return readA2R10G10B10(s, dds, width, height); |
| 1131 | case FormatP8: |
| 1132 | case FormatA8P8: |
| 1133 | return readPalette8Image(s, width, height); |
| 1134 | case FormatP4: |
| 1135 | case FormatA4P4: |
| 1136 | return readPalette4Image(s, width, height); |
| 1137 | case FormatA16B16G16R16: |
| 1138 | return readARGB16(s, width, height); |
| 1139 | case FormatV8U8: |
| 1140 | return readV8U8(s, width, height); |
| 1141 | case FormatL6V5U5: |
| 1142 | return readL6V5U5(s, width, height); |
| 1143 | case FormatX8L8V8U8: |
| 1144 | return readX8L8V8U8(s, width, height); |
| 1145 | case FormatQ8W8V8U8: |
| 1146 | return readQ8W8V8U8(s, width, height); |
| 1147 | case FormatV16U16: |
| 1148 | return readV16U16(s, width, height); |
| 1149 | case FormatA2W10V10U10: |
| 1150 | return readA2W10V10U10(s, width, height); |
| 1151 | case FormatUYVY: |
| 1152 | return readUYVY(s, width, height); |
| 1153 | case FormatR8G8B8G8: |
| 1154 | return readR8G8B8G8(s, width, height); |
| 1155 | case FormatYUY2: |
| 1156 | return readYUY2(s, width, height); |
| 1157 | case FormatG8R8G8B8: |
| 1158 | return readG8R8G8B8(s, width, height); |
| 1159 | case FormatDXT1: |
| 1160 | return readDXT1(s, width, height); |
| 1161 | case FormatDXT2: |
| 1162 | return readDXT2(s, width, height); |
| 1163 | case FormatDXT3: |
| 1164 | return readDXT3(s, width, height); |
| 1165 | case FormatDXT4: |
| 1166 | return readDXT4(s, width, height); |
| 1167 | case FormatDXT5: |
| 1168 | return readDXT5(s, width, height); |
| 1169 | case FormatRXGB: |
| 1170 | return readRXGB(s, width, height); |
| 1171 | case FormatATI2: |
| 1172 | return readATI2(s, width, height); |
| 1173 | case FormatR16F: |
| 1174 | return readR16F(s, width, height); |
| 1175 | case FormatG16R16F: |
| 1176 | return readRG16F(s, width, height); |
| 1177 | case FormatA16B16G16R16F: |
| 1178 | return readARGB16F(s, width, height); |
| 1179 | case FormatR32F: |
| 1180 | return readR32F(s, width, height); |
| 1181 | case FormatG32R32F: |
| 1182 | return readRG32F(s, width, height); |
| 1183 | case FormatA32B32G32R32F: |
| 1184 | return readARGB32F(s, width, height); |
| 1185 | case FormatD16Lockable: |
| 1186 | case FormatD32: |
| 1187 | case FormatD15S1: |
| 1188 | case FormatD24S8: |
| 1189 | case FormatD24X8: |
| 1190 | case FormatD24X4S4: |
| 1191 | case FormatD16: |
| 1192 | case FormatD32FLockable: |
| 1193 | case FormatD24FS8: |
| 1194 | case FormatD32Lockable: |
| 1195 | case FormatS8Lockable: |
| 1196 | case FormatVertexData: |
| 1197 | case FormatIndex16: |
| 1198 | case FormatIndex32: |
| 1199 | break; |
| 1200 | case FormatQ16W16V16U16: |
| 1201 | return readQ16W16V16U16(s, width, height); |
| 1202 | case FormatMulti2ARGB8: |
| 1203 | break; |
| 1204 | case FormatCxV8U8: |
| 1205 | return readCxV8U8(s, width, height); |
| 1206 | case FormatA1: |
| 1207 | case FormatA2B10G10R10_XR_BIAS: |
| 1208 | case FormatBinaryBuffer: |
| 1209 | case FormatLast: |
| 1210 | break; |
| 1211 | } |
| 1212 | |
| 1213 | return QImage(); |
| 1214 | } |
| 1215 | |
| 1216 | static inline QImage readTexture(QDataStream &s, const DDSHeader &dds, const int format, const int mipmapLevel) |
| 1217 | { |
| 1218 | quint32 width = dds.width / (1 << mipmapLevel); |
| 1219 | quint32 height = dds.height / (1 << mipmapLevel); |
| 1220 | return readLayer(s, dds, format, width, height); |
| 1221 | } |
| 1222 | |
| 1223 | static qint64 mipmapSize(const DDSHeader &dds, const int format, const int level) |
| 1224 | { |
| 1225 | quint32 w = dds.width/(1 << level); |
| 1226 | quint32 h = dds.height/(1 << level); |
| 1227 | |
| 1228 | switch (format) { |
| 1229 | case FormatR8G8B8: |
| 1230 | case FormatX8R8G8B8: |
| 1231 | case FormatR5G6B5: |
| 1232 | case FormatX1R5G5B5: |
| 1233 | case FormatX4R4G4B4: |
| 1234 | case FormatX8B8G8R8: |
| 1235 | case FormatG16R16: |
| 1236 | case FormatL8: |
| 1237 | case FormatL16: |
| 1238 | return w * h * dds.pixelFormat.rgbBitCount / 8; |
| 1239 | case FormatA8R8G8B8: |
| 1240 | case FormatA1R5G5B5: |
| 1241 | case FormatA4R4G4B4: |
| 1242 | case FormatA8: |
| 1243 | case FormatA8R3G3B2: |
| 1244 | case FormatA2B10G10R10: |
| 1245 | case FormatA8B8G8R8: |
| 1246 | case FormatA2R10G10B10: |
| 1247 | case FormatA8L8: |
| 1248 | case FormatA4L4: |
| 1249 | return w * h * dds.pixelFormat.rgbBitCount / 8; |
| 1250 | case FormatP8: |
| 1251 | return 256 + w * h * 8; |
| 1252 | case FormatA16B16G16R16: |
| 1253 | return w * h * 4 * 2; |
| 1254 | case FormatA8P8: |
| 1255 | break; |
| 1256 | case FormatV8U8: |
| 1257 | case FormatL6V5U5: |
| 1258 | return w * h * 2; |
| 1259 | case FormatX8L8V8U8: |
| 1260 | case FormatQ8W8V8U8: |
| 1261 | case FormatV16U16: |
| 1262 | case FormatA2W10V10U10: |
| 1263 | return w * h * 4; |
| 1264 | case FormatUYVY: |
| 1265 | case FormatR8G8B8G8: |
| 1266 | case FormatYUY2: |
| 1267 | case FormatG8R8G8B8: |
| 1268 | return w * h * 2; |
| 1269 | case FormatDXT1: |
| 1270 | return ((w + 3)/4) * ((h + 3)/4) * 8; |
| 1271 | case FormatDXT2: |
| 1272 | case FormatDXT3: |
| 1273 | case FormatDXT4: |
| 1274 | case FormatDXT5: |
| 1275 | return ((w + 3)/4) * ((h + 3)/4) * 16; |
| 1276 | case FormatD16Lockable: |
| 1277 | case FormatD32: |
| 1278 | case FormatD15S1: |
| 1279 | case FormatD24S8: |
| 1280 | case FormatD24X8: |
| 1281 | case FormatD24X4S4: |
| 1282 | case FormatD16: |
| 1283 | case FormatD32FLockable: |
| 1284 | case FormatD24FS8: |
| 1285 | case FormatD32Lockable: |
| 1286 | case FormatS8Lockable: |
| 1287 | case FormatVertexData: |
| 1288 | case FormatIndex16: |
| 1289 | case FormatIndex32: |
| 1290 | break; |
| 1291 | case FormatQ16W16V16U16: |
| 1292 | return w * h * 4 * 2; |
| 1293 | case FormatMulti2ARGB8: |
| 1294 | break; |
| 1295 | case FormatR16F: |
| 1296 | return w * h * 1 * 2; |
| 1297 | case FormatG16R16F: |
| 1298 | return w * h * 2 * 2; |
| 1299 | case FormatA16B16G16R16F: |
| 1300 | return w * h * 4 * 2; |
| 1301 | case FormatR32F: |
| 1302 | return w * h * 1 * 4; |
| 1303 | case FormatG32R32F: |
| 1304 | return w * h * 2 * 4; |
| 1305 | case FormatA32B32G32R32F: |
| 1306 | return w * h * 4 * 4; |
| 1307 | case FormatCxV8U8: |
| 1308 | return w * h * 2; |
| 1309 | case FormatA1: |
| 1310 | case FormatA2B10G10R10_XR_BIAS: |
| 1311 | case FormatBinaryBuffer: |
| 1312 | case FormatLast: |
| 1313 | break; |
| 1314 | } |
| 1315 | |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | static qint64 mipmapOffset(const DDSHeader &dds, const int format, const int level) |
| 1320 | { |
| 1321 | qint64 result = 0; |
| 1322 | for (int i = 0; i < level; ++i) |
| 1323 | result += mipmapSize(dds, format, i); |
| 1324 | return result; |
| 1325 | } |
| 1326 | |
| 1327 | static QImage readCubeMap(QDataStream &s, const DDSHeader &dds, const int fmt) |
| 1328 | { |
| 1329 | QImage::Format format = hasAlpha(dds) ? QImage::Format_ARGB32 : QImage::Format_RGB32; |
| 1330 | QImage image(4 * dds.width, 3 * dds.height, format); |
| 1331 | |
| 1332 | image.fill(0); |
| 1333 | |
| 1334 | for (int i = 0; i < 6; i++) { |
| 1335 | if (!(dds.caps2 & faceFlags[i])) |
| 1336 | continue; // Skip face. |
| 1337 | |
| 1338 | const QImage face = readLayer(s, dds, fmt, dds.width, dds.height); |
| 1339 | |
| 1340 | // Compute face offsets. |
| 1341 | int offset_x = faceOffsets[i].x * dds.width; |
| 1342 | int offset_y = faceOffsets[i].y * dds.height; |
| 1343 | |
| 1344 | // Copy face on the image. |
| 1345 | for (quint32 y = 0; y < dds.height; y++) { |
| 1346 | const QRgb *src = reinterpret_cast<const QRgb *>(face.scanLine(y)); |
| 1347 | QRgb *dst = reinterpret_cast<QRgb *>(image.scanLine(y + offset_y)) + offset_x; |
| 1348 | memcpy(dst, src, sizeof(QRgb) * dds.width); |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | return image; |
| 1353 | } |
| 1354 | |
| 1355 | static QByteArray formatName(int format) |
| 1356 | { |
| 1357 | for (size_t i = 0; i < formatNamesSize; ++i) { |
| 1358 | if (formatNames[i].format == format) |
| 1359 | return formatNames[i].name; |
| 1360 | } |
| 1361 | |
| 1362 | return formatNames[0].name; |
| 1363 | } |
| 1364 | |
| 1365 | static int formatByName(const QByteArray &name) |
| 1366 | { |
| 1367 | const QByteArray loweredName = name.toLower(); |
| 1368 | for (size_t i = 0; i < formatNamesSize; ++i) { |
| 1369 | if (QByteArray(formatNames[i].name).toLower() == loweredName) |
| 1370 | return formatNames[i].format; |
| 1371 | } |
| 1372 | |
| 1373 | return FormatUnknown; |
| 1374 | } |
| 1375 | |
| 1376 | QDDSHandler::QDDSHandler() : |
| 1377 | m_header(), |
| 1378 | m_format(FormatA8R8G8B8), |
| 1379 | m_header10(), |
| 1380 | m_currentImage(0), |
| 1381 | m_scanState(ScanNotScanned) |
| 1382 | { |
| 1383 | } |
| 1384 | |
| 1385 | bool QDDSHandler::canRead() const |
| 1386 | { |
| 1387 | if (m_scanState == ScanNotScanned && !canRead(device())) |
| 1388 | return false; |
| 1389 | |
| 1390 | if (m_scanState != ScanError) { |
| 1391 | setFormat(QByteArrayLiteral("dds")); |
| 1392 | return true; |
| 1393 | } |
| 1394 | |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
| 1398 | bool QDDSHandler::read(QImage *outImage) |
| 1399 | { |
| 1400 | if (!ensureScanned() || device()->isSequential()) |
| 1401 | return false; |
| 1402 | |
| 1403 | qint64 pos = headerSize + mipmapOffset(m_header, m_format, m_currentImage); |
| 1404 | if (!device()->seek(pos)) |
| 1405 | return false; |
| 1406 | QDataStream s(device()); |
| 1407 | s.setByteOrder(QDataStream::LittleEndian); |
| 1408 | |
| 1409 | QImage image = isCubeMap(m_header) ? |
| 1410 | readCubeMap(s, m_header, m_format) : |
| 1411 | readTexture(s, m_header, m_format, m_currentImage); |
| 1412 | |
| 1413 | bool ok = s.status() == QDataStream::Ok && !image.isNull(); |
| 1414 | if (ok) |
| 1415 | *outImage = image; |
| 1416 | return ok; |
| 1417 | } |
| 1418 | |
| 1419 | bool QDDSHandler::write(const QImage &outImage) |
| 1420 | { |
| 1421 | if (m_format != FormatA8R8G8B8) { |
| 1422 | qWarning() << "Format" << formatName(m_format) << "is not supported"; |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
| 1426 | const QImage image = outImage.convertToFormat(QImage::Format_ARGB32); |
| 1427 | |
| 1428 | QDataStream s(device()); |
| 1429 | s.setByteOrder(QDataStream::LittleEndian); |
| 1430 | |
| 1431 | DDSHeader dds; |
| 1432 | // Filling header |
| 1433 | dds.magic = ddsMagic; |
| 1434 | dds.size = 124; |
| 1435 | dds.flags = DDSHeader::FlagCaps | DDSHeader::FlagHeight | |
| 1436 | DDSHeader::FlagWidth | DDSHeader::FlagPixelFormat; |
| 1437 | dds.height = image.height(); |
| 1438 | dds.width = image.width(); |
| 1439 | dds.pitchOrLinearSize = 128; |
| 1440 | dds.depth = 0; |
| 1441 | dds.mipMapCount = 0; |
| 1442 | for (int i = 0; i < DDSHeader::ReservedCount; i++) |
| 1443 | dds.reserved1[i] = 0; |
| 1444 | dds.caps = DDSHeader::CapsTexture; |
| 1445 | dds.caps2 = 0; |
| 1446 | dds.caps3 = 0; |
| 1447 | dds.caps4 = 0; |
| 1448 | dds.reserved2 = 0; |
| 1449 | |
| 1450 | // Filling pixelformat |
| 1451 | dds.pixelFormat.size = 32; |
| 1452 | dds.pixelFormat.flags = DDSPixelFormat::FlagAlphaPixels | DDSPixelFormat::FlagRGB; |
| 1453 | dds.pixelFormat.fourCC = 0; |
| 1454 | dds.pixelFormat.rgbBitCount = 32; |
| 1455 | dds.pixelFormat.aBitMask = 0xff000000; |
| 1456 | dds.pixelFormat.rBitMask = 0x00ff0000; |
| 1457 | dds.pixelFormat.gBitMask = 0x0000ff00; |
| 1458 | dds.pixelFormat.bBitMask = 0x000000ff; |
| 1459 | |
| 1460 | s << dds; |
| 1461 | for (int height = 0; height < image.height(); height++) { |
| 1462 | for (int width = 0; width < image.width(); width++) { |
| 1463 | QRgb pixel = image.pixel(width, height); |
| 1464 | quint32 color; |
| 1465 | quint8 alpha = qAlpha(pixel); |
| 1466 | quint8 red = qRed(pixel); |
| 1467 | quint8 green = qGreen(pixel); |
| 1468 | quint8 blue = qBlue(pixel); |
| 1469 | color = (alpha << 24) + (red << 16) + (green << 8) + blue; |
| 1470 | s << color; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | return true; |
| 1475 | } |
| 1476 | |
| 1477 | QVariant QDDSHandler::option(QImageIOHandler::ImageOption option) const |
| 1478 | { |
| 1479 | if (!supportsOption(option) || !ensureScanned()) |
| 1480 | return QVariant(); |
| 1481 | |
| 1482 | switch (option) { |
| 1483 | case QImageIOHandler::Size: |
| 1484 | return QSize(m_header.width, m_header.height); |
| 1485 | case QImageIOHandler::SubType: |
| 1486 | return formatName(m_format); |
| 1487 | case QImageIOHandler::SupportedSubTypes: |
| 1488 | return QVariant::fromValue(QList<QByteArray>() << formatName(FormatA8R8G8B8)); |
| 1489 | default: |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | return QVariant(); |
| 1494 | } |
| 1495 | |
| 1496 | void QDDSHandler::setOption(QImageIOHandler::ImageOption option, const QVariant &value) |
| 1497 | { |
| 1498 | if (option == QImageIOHandler::SubType) { |
| 1499 | const QByteArray subType = value.toByteArray(); |
| 1500 | m_format = formatByName(subType.toUpper()); |
| 1501 | if (m_format == FormatUnknown) |
| 1502 | qWarning() << "unknown format" << subType; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | bool QDDSHandler::supportsOption(QImageIOHandler::ImageOption option) const |
| 1507 | { |
| 1508 | return (option == QImageIOHandler::Size) |
| 1509 | || (option == QImageIOHandler::SubType) |
| 1510 | || (option == QImageIOHandler::SupportedSubTypes); |
| 1511 | } |
| 1512 | |
| 1513 | int QDDSHandler::imageCount() const |
| 1514 | { |
| 1515 | if (!ensureScanned()) |
| 1516 | return 0; |
| 1517 | |
| 1518 | return qMax<quint32>(1, m_header.mipMapCount); |
| 1519 | } |
| 1520 | |
| 1521 | bool QDDSHandler::jumpToImage(int imageNumber) |
| 1522 | { |
| 1523 | if (imageNumber >= imageCount()) |
| 1524 | return false; |
| 1525 | |
| 1526 | m_currentImage = imageNumber; |
| 1527 | return true; |
| 1528 | } |
| 1529 | |
| 1530 | bool QDDSHandler::canRead(QIODevice *device) |
| 1531 | { |
| 1532 | if (!device) { |
| 1533 | qWarning() << "DDSHandler::canRead() called with no device"; |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
| 1537 | if (device->isSequential()) |
| 1538 | return false; |
| 1539 | |
| 1540 | return device->peek(4) == QByteArrayLiteral("DDS "); |
| 1541 | } |
| 1542 | |
| 1543 | bool QDDSHandler::ensureScanned() const |
| 1544 | { |
| 1545 | if (m_scanState != ScanNotScanned) |
| 1546 | return m_scanState == ScanSuccess; |
| 1547 | |
| 1548 | m_scanState = ScanError; |
| 1549 | |
| 1550 | QDDSHandler *that = const_cast<QDDSHandler *>(this); |
| 1551 | that->m_format = FormatUnknown; |
| 1552 | |
| 1553 | if (device()->isSequential()) { |
| 1554 | qWarning() << "Sequential devices are not supported"; |
| 1555 | return false; |
| 1556 | } |
| 1557 | |
| 1558 | qint64 oldPos = device()->pos(); |
| 1559 | device()->seek(0); |
| 1560 | |
| 1561 | QDataStream s(device()); |
| 1562 | s.setByteOrder(QDataStream::LittleEndian); |
| 1563 | s >> that->m_header; |
| 1564 | if (m_header.pixelFormat.fourCC == dx10Magic) |
| 1565 | s >> that->m_header10; |
| 1566 | |
| 1567 | device()->seek(oldPos); |
| 1568 | |
| 1569 | if (s.status() != QDataStream::Ok) |
| 1570 | return false; |
| 1571 | |
| 1572 | if (!verifyHeader(m_header)) |
| 1573 | return false; |
| 1574 | |
| 1575 | that->m_format = getFormat(m_header); |
| 1576 | if (that->m_format == FormatUnknown) |
| 1577 | return false; |
| 1578 | |
| 1579 | m_scanState = ScanSuccess; |
| 1580 | return true; |
| 1581 | } |
| 1582 | |
| 1583 | bool QDDSHandler::verifyHeader(const DDSHeader &dds) const |
| 1584 | { |
| 1585 | quint32 flags = dds.flags; |
| 1586 | quint32 requiredFlags = DDSHeader::FlagCaps | DDSHeader::FlagHeight |
| 1587 | | DDSHeader::FlagWidth | DDSHeader::FlagPixelFormat; |
| 1588 | if ((flags & requiredFlags) != requiredFlags) { |
| 1589 | qWarning() << "Wrong dds.flags - not all required flags present. " |
| 1590 | "Actual flags :" << flags; |
| 1591 | return false; |
| 1592 | } |
| 1593 | |
| 1594 | if (dds.size != ddsSize) { |
| 1595 | qWarning() << "Wrong dds.size: actual =" << dds.size |
| 1596 | << "expected =" << ddsSize; |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
| 1600 | if (dds.pixelFormat.size != pixelFormatSize) { |
| 1601 | qWarning() << "Wrong dds.pixelFormat.size: actual =" << dds.pixelFormat.size |
| 1602 | << "expected =" << pixelFormatSize; |
| 1603 | return false; |
| 1604 | } |
| 1605 | |
| 1606 | if (dds.width > INT_MAX || dds.height > INT_MAX) { |
| 1607 | qWarning() << "Can't read image with w/h bigger than INT_MAX"; |
| 1608 | return false; |
| 1609 | } |
| 1610 | |
| 1611 | return true; |
| 1612 | } |
| 1613 | |
| 1614 | QT_END_NAMESPACE |
| 1615 | |
| 1616 | #endif // QT_NO_DATASTREAM |