ddsformat.c 71.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2020 Ziqing Hui
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18 19 20 21 22 23 24 25 26
 *
 *
 * Note:
 *
 * Uncompressed image:
 *     For uncompressed formats, a block is equivalent to a pixel.
 *
 * Cube map:
 *     A cube map is equivalent to a 2D texture array which has 6 textures.
 *     A cube map array is equivalent to a 2D texture array which has cubeCount*6 textures.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 */

#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "objbase.h"

#include "wincodecs_private.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);

43
#define DDS_MAGIC 0x20534444
44 45 46 47 48 49
#ifndef MAKEFOURCC
#define MAKEFOURCC(ch0, ch1, ch2, ch3)  \
    ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) |  \
    ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))
#endif

50 51 52 53 54 55
#define GET_RGB565_R(color)   ((BYTE)(((color) >> 11) & 0x1F))
#define GET_RGB565_G(color)   ((BYTE)(((color) >> 5)  & 0x3F))
#define GET_RGB565_B(color)   ((BYTE)(((color) >> 0)  & 0x1F))
#define MAKE_RGB565(r, g, b)  ((WORD)(((BYTE)(r) << 11) | ((BYTE)(g) << 5) | (BYTE)(b)))
#define MAKE_ARGB(a, r, g, b) (((DWORD)(a) << 24) | ((DWORD)(r) << 16) | ((DWORD)(g) << 8) | (DWORD)(b))

56 57 58 59 60 61 62
#define DDPF_ALPHAPIXELS     0x00000001
#define DDPF_ALPHA           0x00000002
#define DDPF_FOURCC          0x00000004
#define DDPF_PALETTEINDEXED8 0x00000020
#define DDPF_RGB             0x00000040
#define DDPF_LUMINANCE       0x00020000
#define DDPF_BUMPDUDV        0x00080000
63

64 65 66 67 68 69 70 71 72
#define DDSCAPS2_CUBEMAP 0x00000200
#define DDSCAPS2_VOLUME  0x00200000

#define DDS_DIMENSION_TEXTURE1D 2
#define DDS_DIMENSION_TEXTURE2D 3
#define DDS_DIMENSION_TEXTURE3D 4

#define DDS_RESOURCE_MISC_TEXTURECUBE 0x00000004

73 74 75
#define DDS_BLOCK_WIDTH  4
#define DDS_BLOCK_HEIGHT 4

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
typedef struct {
    DWORD size;
    DWORD flags;
    DWORD fourCC;
    DWORD rgbBitCount;
    DWORD rBitMask;
    DWORD gBitMask;
    DWORD bBitMask;
    DWORD aBitMask;
} DDS_PIXELFORMAT;

typedef struct {
    DWORD size;
    DWORD flags;
    DWORD height;
    DWORD width;
    DWORD pitchOrLinearSize;
    DWORD depth;
    DWORD mipMapCount;
    DWORD reserved1[11];
    DDS_PIXELFORMAT ddspf;
    DWORD caps;
    DWORD caps2;
    DWORD caps3;
    DWORD caps4;
    DWORD reserved2;
} DDS_HEADER;

104 105 106 107 108 109 110 111
typedef struct {
    DWORD dxgiFormat;
    DWORD resourceDimension;
    DWORD miscFlag;
    DWORD arraySize;
    DWORD miscFlags2;
} DDS_HEADER_DXT10;

112 113 114 115 116 117 118
typedef struct dds_info {
    UINT width;
    UINT height;
    UINT depth;
    UINT mip_levels;
    UINT array_size;
    UINT frame_count;
119
    UINT data_offset;
120
    UINT bytes_per_block; /* for uncompressed format, this means bytes per pixel*/
121 122 123
    DXGI_FORMAT format;
    WICDdsDimension dimension;
    WICDdsAlphaMode alpha_mode;
124
    const GUID *pixel_format;
125
    UINT pixel_format_bpp;
126 127
} dds_info;

128 129 130 131
typedef struct dds_frame_info {
    UINT width;
    UINT height;
    DXGI_FORMAT format;
132
    UINT bytes_per_block; /* for uncompressed format, this means bytes per pixel*/
133 134
    UINT block_width;
    UINT block_height;
135 136
    UINT width_in_blocks;
    UINT height_in_blocks;
137
    const GUID *pixel_format;
138
    UINT pixel_format_bpp;
139 140
} dds_frame_info;

141 142
typedef struct DdsDecoder {
    IWICBitmapDecoder IWICBitmapDecoder_iface;
143
    IWICDdsDecoder IWICDdsDecoder_iface;
144
    IWICWineDecoder IWICWineDecoder_iface;
145 146 147 148
    LONG ref;
    BOOL initialized;
    IStream *stream;
    CRITICAL_SECTION lock;
149
    dds_info info;
150 151
} DdsDecoder;

152 153
typedef struct DdsFrameDecode {
    IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
154
    IWICDdsFrameDecode IWICDdsFrameDecode_iface;
155
    LONG ref;
156 157
    BYTE *block_data;
    BYTE *pixel_data;
158
    CRITICAL_SECTION lock;
159
    dds_frame_info info;
160 161
} DdsFrameDecode;

162 163
typedef struct DdsEncoder {
    IWICBitmapEncoder IWICBitmapEncoder_iface;
164
    IWICDdsEncoder IWICDdsEncoder_iface;
165 166 167 168
    LONG ref;
    CRITICAL_SECTION lock;
    IStream *stream;
    UINT frame_count;
169
    UINT frame_index;
170 171
    BOOL uncommitted_frame;
    BOOL committed;
172
    dds_info info;
173 174
} DdsEncoder;

175 176 177 178 179 180 181 182 183 184 185 186
typedef struct DdsFrameEncode {
    IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
    LONG ref;
    DdsEncoder *parent;
    BOOL initialized;
    BOOL frame_created;
    UINT width;
    UINT height;
    double dpi_x;
    double dpi_y;
} DdsFrameEncode;

187 188
static struct dds_format {
    DDS_PIXELFORMAT pixel_format;
189 190
    const GUID *wic_format;
    UINT wic_format_bpp;
191
    DXGI_FORMAT dxgi_format;
192
} dds_format_table[] = {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppPBGRA, 32,       DXGI_FORMAT_BC1_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '2'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppPBGRA, 32,       DXGI_FORMAT_BC2_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '3'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppBGRA,  32,       DXGI_FORMAT_BC2_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '4'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppPBGRA, 32,       DXGI_FORMAT_BC3_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '5'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppBGRA,  32,       DXGI_FORMAT_BC3_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('B', 'C', '4', 'U'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppBGRA,  32,       DXGI_FORMAT_BC4_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('B', 'C', '4', 'S'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppBGRA,  32,       DXGI_FORMAT_BC4_SNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('B', 'C', '5', 'U'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppBGRA,  32,       DXGI_FORMAT_BC5_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('B', 'C', '5', 'S'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppBGRA,  32,       DXGI_FORMAT_BC5_SNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('A', 'T', 'I', '1'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppBGRA,  32,       DXGI_FORMAT_BC4_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('A', 'T', 'I', '2'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppBGRA,  32,       DXGI_FORMAT_BC5_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('R', 'G', 'B', 'G'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bpp4Channels, 32,   DXGI_FORMAT_R8G8_B8G8_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('G', 'R', 'G', 'B'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bpp4Channels, 32,   DXGI_FORMAT_G8R8_G8B8_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', '1', '0'), 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormatUndefined,       0,   DXGI_FORMAT_UNKNOWN },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, 0x24, 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat64bppRGBA,       64,  DXGI_FORMAT_R16G16B16A16_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, 0x6E, 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat64bppRGBA,       64,  DXGI_FORMAT_R16G16B16A16_SNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, 0x6F, 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat16bppGrayHalf,   16,  DXGI_FORMAT_R16_FLOAT },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, 0x70, 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormatUndefined,       0,   DXGI_FORMAT_R16G16_FLOAT },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, 0x71, 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat64bppRGBAHalf,   64,  DXGI_FORMAT_R16G16B16A16_FLOAT },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, 0x72, 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat32bppGrayFloat,  32,  DXGI_FORMAT_R32_FLOAT },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, 0x73, 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormatUndefined,       32,  DXGI_FORMAT_R32G32_FLOAT },
    { { sizeof(DDS_PIXELFORMAT), DDPF_FOURCC, 0x74, 0, 0, 0, 0, 0 },
      &GUID_WICPixelFormat128bppRGBAFloat, 128, DXGI_FORMAT_R32G32B32A32_FLOAT },
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 32, 0xFF,0xFF00,0xFF0000,0xFF000000 },
      &GUID_WICPixelFormat32bppRGBA,        32, DXGI_FORMAT_R8G8B8A8_UNORM },
239 240
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 32, 0xFF,0xFF00,0xFF0000,0 },
      &GUID_WICPixelFormat32bppRGB,         32, DXGI_FORMAT_UNKNOWN },
241 242 243 244
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 32, 0xFF0000,0xFF00,0xFF,0xFF000000 },
      &GUID_WICPixelFormat32bppBGRA,        32, DXGI_FORMAT_B8G8R8A8_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 32, 0xFF0000,0xFF00,0xFF,0 },
      &GUID_WICPixelFormat32bppBGR,         32, DXGI_FORMAT_B8G8R8X8_UNORM },
245
    /* The red and blue masks are swapped for DXGI_FORMAT_R10G10B10A2_UNORM.
246
     * For "correct" one, the RGB masks should be 0x3FF,0xFFC00,0x3FF00000.
247
     * see: https://walbourn.github.io/dds-update-and-1010102-problems */
248 249
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 32, 0x3FF00000,0xFFC00,0x3FF,0xC0000000 },
      &GUID_WICPixelFormat32bppR10G10B10A2, 32, DXGI_FORMAT_R10G10B10A2_UNORM },
250
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 32, 0x3FF,0xFFC00,0x3FF00000,0xC0000000 },
251
      &GUID_WICPixelFormat32bppRGBA1010102, 32, DXGI_FORMAT_R10G10B10A2_UNORM },
252 253 254 255
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB ,      0, 32, 0xFFFF,0xFFFF0000,0,0 },
      &GUID_WICPixelFormatUndefined,        0,  DXGI_FORMAT_R16G16_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB ,      0, 32, 0xFFFFFFFF,0,0,0 },
      &GUID_WICPixelFormat32bppGrayFloat,   32, DXGI_FORMAT_R32_FLOAT },
256 257 258 259
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB ,      0, 24, 0xFF0000,0x00FF00,0x0000FF,0 },
      &GUID_WICPixelFormat24bppBGR,         24, DXGI_FORMAT_UNKNOWN },
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB ,      0, 24, 0x0000FF,0x00FF00,0xFF0000,0 },
      &GUID_WICPixelFormat24bppRGB,         24, DXGI_FORMAT_UNKNOWN },
260 261
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 16, 0xF800,0x7E0,0x1F,0 },
      &GUID_WICPixelFormat16bppBGR565,      16, DXGI_FORMAT_B5G6R5_UNORM },
262 263
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 16, 0x7C00,0x3E0,0x1F,0 },
      &GUID_WICPixelFormat16bppBGR555,      16, DXGI_FORMAT_UNKNOWN },
264 265 266 267 268 269 270 271 272 273 274
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 16, 0x7C00,0x3E0,0x1F,0x8000 },
      &GUID_WICPixelFormat16bppBGRA5551,    16, DXGI_FORMAT_B5G5R5A1_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_RGB,       0, 16, 0xF00,0xF0,0xF,0xF000 },
      &GUID_WICPixelFormatUndefined,        0,  DXGI_FORMAT_B4G4R4A4_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_ALPHA,     0, 8,  0,0,0,0xFF },
      &GUID_WICPixelFormat8bppAlpha,        8,  DXGI_FORMAT_A8_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_LUMINANCE, 0, 16, 0xFFFF,0,0,0 },
      &GUID_WICPixelFormat16bppGray,        16, DXGI_FORMAT_R16_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_LUMINANCE, 0, 16, 0xFF,0,0,0xFF00 },
      &GUID_WICPixelFormatUndefined,        0,  DXGI_FORMAT_R8G8_UNORM },
    { { sizeof(DDS_PIXELFORMAT), DDPF_LUMINANCE, 0, 8,  0xFF,0,0,0 },
275
      &GUID_WICPixelFormat8bppGray,         8,  DXGI_FORMAT_R8_UNORM },
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    { { 0 }, &GUID_WICPixelFormat8bppAlpha,          8,   DXGI_FORMAT_A8_UNORM },
    { { 0 }, &GUID_WICPixelFormat8bppGray,           8,   DXGI_FORMAT_R8_UNORM },
    { { 0 }, &GUID_WICPixelFormat16bppGray,          16,  DXGI_FORMAT_R16_UNORM },
    { { 0 }, &GUID_WICPixelFormat16bppGrayHalf,      16,  DXGI_FORMAT_R16_FLOAT },
    { { 0 }, &GUID_WICPixelFormat16bppBGR565,        16,  DXGI_FORMAT_B5G6R5_UNORM },
    { { 0 }, &GUID_WICPixelFormat16bppBGRA5551,      16,  DXGI_FORMAT_B5G5R5A1_UNORM },
    { { 0 }, &GUID_WICPixelFormat32bppGrayFloat,     32,  DXGI_FORMAT_R32_FLOAT },
    { { 0 }, &GUID_WICPixelFormat32bppRGBA,          32,  DXGI_FORMAT_R8G8B8A8_UNORM },
    { { 0 }, &GUID_WICPixelFormat32bppBGRA,          32,  DXGI_FORMAT_B8G8R8A8_UNORM },
    { { 0 }, &GUID_WICPixelFormat32bppBGR,           32,  DXGI_FORMAT_B8G8R8X8_UNORM },
    { { 0 }, &GUID_WICPixelFormat32bppR10G10B10A2,   32,  DXGI_FORMAT_R10G10B10A2_UNORM },
    { { 0 }, &GUID_WICPixelFormat32bppRGBE,          32,  DXGI_FORMAT_R9G9B9E5_SHAREDEXP },
    { { 0 }, &GUID_WICPixelFormat32bppRGBA1010102XR, 32,  DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM },
    { { 0 }, &GUID_WICPixelFormat64bppRGBA,          64,  DXGI_FORMAT_R16G16B16A16_UNORM },
    { { 0 }, &GUID_WICPixelFormat64bppRGBAHalf,      64,  DXGI_FORMAT_R16G16B16A16_FLOAT },
    { { 0 }, &GUID_WICPixelFormat96bppRGBFloat,      96,  DXGI_FORMAT_R32G32B32_FLOAT },
    { { 0 }, &GUID_WICPixelFormat128bppRGBAFloat,    128, DXGI_FORMAT_R32G32B32A32_FLOAT },
    { { 0 }, &GUID_WICPixelFormatUndefined,          0,   DXGI_FORMAT_UNKNOWN }
294 295
};

296 297 298 299 300 301 302 303 304 305
static DXGI_FORMAT compressed_formats[] = {
    DXGI_FORMAT_BC1_TYPELESS,  DXGI_FORMAT_BC1_UNORM, DXGI_FORMAT_BC1_UNORM_SRGB,
    DXGI_FORMAT_BC2_TYPELESS,  DXGI_FORMAT_BC2_UNORM, DXGI_FORMAT_BC2_UNORM_SRGB,
    DXGI_FORMAT_BC3_TYPELESS,  DXGI_FORMAT_BC3_UNORM, DXGI_FORMAT_BC3_UNORM_SRGB,
    DXGI_FORMAT_BC4_TYPELESS,  DXGI_FORMAT_BC4_UNORM, DXGI_FORMAT_BC4_SNORM,
    DXGI_FORMAT_BC5_TYPELESS,  DXGI_FORMAT_BC5_UNORM, DXGI_FORMAT_BC5_SNORM,
    DXGI_FORMAT_BC6H_TYPELESS, DXGI_FORMAT_BC6H_UF16, DXGI_FORMAT_BC6H_SF16,
    DXGI_FORMAT_BC7_TYPELESS,  DXGI_FORMAT_BC7_UNORM, DXGI_FORMAT_BC7_UNORM_SRGB
};

306 307
static HRESULT WINAPI DdsDecoder_Dds_GetFrame(IWICDdsDecoder *, UINT, UINT, UINT, IWICBitmapFrameDecode **);

308 309 310 311 312 313 314
static DWORD rgb565_to_argb(WORD color, BYTE alpha)
{
    return MAKE_ARGB(alpha, (GET_RGB565_R(color) * 0xFF + 0x0F) / 0x1F,
                            (GET_RGB565_G(color) * 0xFF + 0x1F) / 0x3F,
                            (GET_RGB565_B(color) * 0xFF + 0x0F) / 0x1F);
}

315 316 317 318 319 320
static inline BOOL has_extended_header(DDS_HEADER *header)
{
    return (header->ddspf.flags & DDPF_FOURCC) &&
           (header->ddspf.fourCC == MAKEFOURCC('D', 'X', '1', '0'));
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
static WICDdsDimension get_dimension(DDS_HEADER *header, DDS_HEADER_DXT10 *header_dxt10)
{
    if (header_dxt10) {
        if (header_dxt10->miscFlag & DDS_RESOURCE_MISC_TEXTURECUBE) return WICDdsTextureCube;
        switch (header_dxt10->resourceDimension)
        {
        case DDS_DIMENSION_TEXTURE1D: return WICDdsTexture1D;
        case DDS_DIMENSION_TEXTURE2D: return WICDdsTexture2D;
        case DDS_DIMENSION_TEXTURE3D: return WICDdsTexture3D;
        default: return WICDdsTexture2D;
        }
    } else {
        if (header->caps2 & DDSCAPS2_CUBEMAP) {
            return WICDdsTextureCube;
        } else if (header->caps2 & DDSCAPS2_VOLUME) {
            return WICDdsTexture3D;
        } else {
            return WICDdsTexture2D;
        }
    }
}

343
static struct dds_format *get_dds_format(DDS_PIXELFORMAT *pixel_format)
344
{
345 346
    UINT i;

347
    for (i = 0; i < ARRAY_SIZE(dds_format_table); i++)
348
    {
349 350 351 352 353 354 355 356
        if ((pixel_format->flags & dds_format_table[i].pixel_format.flags) &&
            (pixel_format->fourCC == dds_format_table[i].pixel_format.fourCC) &&
            (pixel_format->rgbBitCount == dds_format_table[i].pixel_format.rgbBitCount) &&
            (pixel_format->rBitMask == dds_format_table[i].pixel_format.rBitMask) &&
            (pixel_format->gBitMask == dds_format_table[i].pixel_format.gBitMask) &&
            (pixel_format->bBitMask == dds_format_table[i].pixel_format.bBitMask) &&
            (pixel_format->aBitMask == dds_format_table[i].pixel_format.aBitMask))
            return dds_format_table + i;
357
    }
358

359
    return dds_format_table + ARRAY_SIZE(dds_format_table) - 1;
360 361 362 363 364 365
}

static WICDdsAlphaMode get_alpha_mode_from_fourcc(DWORD fourcc)
{
    switch (fourcc)
    {
366 367 368 369 370 371
        case MAKEFOURCC('D', 'X', 'T', '1'):
        case MAKEFOURCC('D', 'X', 'T', '2'):
        case MAKEFOURCC('D', 'X', 'T', '4'):
            return WICDdsAlphaModePremultiplied;
        default:
            return WICDdsAlphaModeUnknown;
372 373 374
    }
}

375 376
static UINT get_bytes_per_block_from_format(DXGI_FORMAT format)
{
377
    /* for uncompressed format, return bytes per pixel*/
378 379
    switch (format)
    {
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        case DXGI_FORMAT_R8_TYPELESS:
        case DXGI_FORMAT_R8_UNORM:
        case DXGI_FORMAT_R8_UINT:
        case DXGI_FORMAT_R8_SNORM:
        case DXGI_FORMAT_R8_SINT:
        case DXGI_FORMAT_A8_UNORM:
            return 1;
        case DXGI_FORMAT_R8G8_TYPELESS:
        case DXGI_FORMAT_R8G8_UNORM:
        case DXGI_FORMAT_R8G8_UINT:
        case DXGI_FORMAT_R8G8_SNORM:
        case DXGI_FORMAT_R8G8_SINT:
        case DXGI_FORMAT_R16_TYPELESS:
        case DXGI_FORMAT_R16_FLOAT:
        case DXGI_FORMAT_D16_UNORM:
        case DXGI_FORMAT_R16_UNORM:
        case DXGI_FORMAT_R16_UINT:
        case DXGI_FORMAT_R16_SNORM:
        case DXGI_FORMAT_R16_SINT:
        case DXGI_FORMAT_B5G6R5_UNORM:
        case DXGI_FORMAT_B5G5R5A1_UNORM:
        case DXGI_FORMAT_B4G4R4A4_UNORM:
            return 2;
        case DXGI_FORMAT_R10G10B10A2_TYPELESS:
        case DXGI_FORMAT_R10G10B10A2_UNORM:
        case DXGI_FORMAT_R10G10B10A2_UINT:
        case DXGI_FORMAT_R11G11B10_FLOAT:
        case DXGI_FORMAT_R8G8B8A8_TYPELESS:
        case DXGI_FORMAT_R8G8B8A8_UNORM:
        case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
        case DXGI_FORMAT_R8G8B8A8_UINT:
        case DXGI_FORMAT_R8G8B8A8_SNORM:
        case DXGI_FORMAT_R8G8B8A8_SINT:
        case DXGI_FORMAT_R16G16_TYPELESS:
        case DXGI_FORMAT_R16G16_FLOAT:
        case DXGI_FORMAT_R16G16_UNORM:
        case DXGI_FORMAT_R16G16_UINT:
        case DXGI_FORMAT_R16G16_SNORM:
        case DXGI_FORMAT_R16G16_SINT:
        case DXGI_FORMAT_R32_TYPELESS:
        case DXGI_FORMAT_D32_FLOAT:
        case DXGI_FORMAT_R32_FLOAT:
        case DXGI_FORMAT_R32_UINT:
        case DXGI_FORMAT_R32_SINT:
        case DXGI_FORMAT_R24G8_TYPELESS:
        case DXGI_FORMAT_D24_UNORM_S8_UINT:
        case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
        case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
        case DXGI_FORMAT_R9G9B9E5_SHAREDEXP:
        case DXGI_FORMAT_R8G8_B8G8_UNORM:
        case DXGI_FORMAT_G8R8_G8B8_UNORM:
        case DXGI_FORMAT_B8G8R8A8_UNORM:
        case DXGI_FORMAT_B8G8R8X8_UNORM:
        case DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM:
        case DXGI_FORMAT_B8G8R8A8_TYPELESS:
        case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
        case DXGI_FORMAT_B8G8R8X8_TYPELESS:
        case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
            return 4;
439 440 441
        case DXGI_FORMAT_BC1_UNORM:
        case DXGI_FORMAT_BC1_TYPELESS:
        case DXGI_FORMAT_BC1_UNORM_SRGB:
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
        case DXGI_FORMAT_BC4_TYPELESS:
        case DXGI_FORMAT_BC4_UNORM:
        case DXGI_FORMAT_BC4_SNORM:
        case DXGI_FORMAT_R16G16B16A16_TYPELESS:
        case DXGI_FORMAT_R16G16B16A16_FLOAT:
        case DXGI_FORMAT_R16G16B16A16_UNORM:
        case DXGI_FORMAT_R16G16B16A16_UINT:
        case DXGI_FORMAT_R16G16B16A16_SNORM:
        case DXGI_FORMAT_R16G16B16A16_SINT:
        case DXGI_FORMAT_R32G32_TYPELESS:
        case DXGI_FORMAT_R32G32_FLOAT:
        case DXGI_FORMAT_R32G32_UINT:
        case DXGI_FORMAT_R32G32_SINT:
        case DXGI_FORMAT_R32G8X24_TYPELESS:
        case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
        case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
        case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
459
            return 8;
460 461 462 463 464
        case DXGI_FORMAT_R32G32B32_TYPELESS:
        case DXGI_FORMAT_R32G32B32_FLOAT:
        case DXGI_FORMAT_R32G32B32_UINT:
        case DXGI_FORMAT_R32G32B32_SINT:
            return 12;
465 466 467 468 469 470
        case DXGI_FORMAT_BC2_UNORM:
        case DXGI_FORMAT_BC2_TYPELESS:
        case DXGI_FORMAT_BC2_UNORM_SRGB:
        case DXGI_FORMAT_BC3_UNORM:
        case DXGI_FORMAT_BC3_TYPELESS:
        case DXGI_FORMAT_BC3_UNORM_SRGB:
471 472 473 474 475 476 477 478 479 480 481 482 483
        case DXGI_FORMAT_BC5_TYPELESS:
        case DXGI_FORMAT_BC5_UNORM:
        case DXGI_FORMAT_BC5_SNORM:
        case DXGI_FORMAT_BC6H_TYPELESS:
        case DXGI_FORMAT_BC6H_UF16:
        case DXGI_FORMAT_BC6H_SF16:
        case DXGI_FORMAT_BC7_TYPELESS:
        case DXGI_FORMAT_BC7_UNORM:
        case DXGI_FORMAT_BC7_UNORM_SRGB:
        case DXGI_FORMAT_R32G32B32A32_TYPELESS:
        case DXGI_FORMAT_R32G32B32A32_FLOAT:
        case DXGI_FORMAT_R32G32B32A32_UINT:
        case DXGI_FORMAT_R32G32B32A32_SINT:
484 485 486 487 488 489 490
            return 16;
        default:
            WARN("DXGI format 0x%x is not supported in DDS decoder\n", format);
            return 0;
    }
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
static UINT get_frame_count(UINT depth, UINT mip_levels, UINT array_size, WICDdsDimension dimension)
{
    UINT frame_count, i;

    if (depth == 1)
    {
        frame_count = mip_levels;
    }
    else
    {
        frame_count = 0;
        for (i = 0; i < mip_levels; i++)
        {
            frame_count += depth;
            if (depth > 1) depth /= 2;
        }
    }

    frame_count *= array_size;
    if (dimension == WICDdsTextureCube) frame_count *= 6;

    return frame_count;
}

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
static void get_frame_dds_index(UINT index, dds_info *info, UINT *array_index, UINT *mip_level, UINT *slice_index)
{
    UINT frame_per_texture, depth;

    if (info->dimension == WICDdsTextureCube)
        frame_per_texture = info->mip_levels;
    else
        frame_per_texture = info->frame_count / info->array_size;

    *array_index = index / frame_per_texture;
    *slice_index = index % frame_per_texture;
    depth = info->depth;
    *mip_level = 0;
    while (*slice_index >= depth)
    {
        *slice_index -= depth;
        (*mip_level)++;
        if (depth > 1) depth /= 2;
    }
}

536 537 538 539 540 541 542 543 544 545 546 547
static const GUID *dxgi_format_to_wic_format(DXGI_FORMAT dxgi_format)
{
    UINT i;
    for (i = 0; i < ARRAY_SIZE(dds_format_table); i++)
    {
        if (dds_format_table[i].pixel_format.size == 0 &&
            dds_format_table[i].dxgi_format == dxgi_format)
            return dds_format_table[i].wic_format;
    }
    return &GUID_WICPixelFormatUndefined;
}

548 549 550 551 552 553 554 555 556 557 558
static BOOL is_compressed(DXGI_FORMAT format)
{
    UINT i;

    for (i = 0; i < ARRAY_SIZE(compressed_formats); i++)
    {
        if (format == compressed_formats[i]) return TRUE;
    }
    return FALSE;
}

559 560
static void get_dds_info(dds_info* info, DDS_HEADER *header, DDS_HEADER_DXT10 *header_dxt10)
{
561
    struct dds_format *format_info;
562 563 564 565 566 567 568 569 570 571 572 573 574 575

    info->width = header->width;
    info->height = header->height;
    info->depth = 1;
    info->mip_levels = 1;
    info->array_size = 1;
    if (header->depth) info->depth = header->depth;
    if (header->mipMapCount) info->mip_levels = header->mipMapCount;

    if (has_extended_header(header)) {
        if (header_dxt10->arraySize) info->array_size = header_dxt10->arraySize;
        info->format = header_dxt10->dxgiFormat;
        info->dimension = get_dimension(NULL, header_dxt10);
        info->alpha_mode = header_dxt10->miscFlags2 & 0x00000008;
576
        info->data_offset = sizeof(DWORD) + sizeof(*header) + sizeof(*header_dxt10);
577
        if (is_compressed(info->format)) {
578 579
            info->pixel_format = (info->alpha_mode == WICDdsAlphaModePremultiplied) ?
                                 &GUID_WICPixelFormat32bppPBGRA : &GUID_WICPixelFormat32bppBGRA;
580 581
            info->pixel_format_bpp = 32;
        } else {
582 583
            info->pixel_format = dxgi_format_to_wic_format(info->format);
            info->pixel_format_bpp = get_bytes_per_block_from_format(info->format) * 8;
584
        }
585
    } else {
586
        format_info = get_dds_format(&header->ddspf);
587
        info->format = format_info->dxgi_format;
588 589
        info->dimension = get_dimension(header, NULL);
        info->alpha_mode = get_alpha_mode_from_fourcc(header->ddspf.fourCC);
590
        info->data_offset = sizeof(DWORD) + sizeof(*header);
591
        info->pixel_format = format_info->wic_format;
592
        info->pixel_format_bpp = format_info->wic_format_bpp;
593 594
    }

595
    if (header->ddspf.flags & (DDPF_RGB | DDPF_ALPHA | DDPF_LUMINANCE)) {
596
        info->bytes_per_block = header->ddspf.rgbBitCount / 8;
597 598
    } else {
        info->bytes_per_block = get_bytes_per_block_from_format(info->format);
599 600
    }

601
    info->frame_count = get_frame_count(info->depth, info->mip_levels, info->array_size, info->dimension);
602 603
}

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
static void decode_block(const BYTE *block_data, UINT block_count, DXGI_FORMAT format,
                         UINT width, UINT height, DWORD *buffer)
{
    const BYTE *block, *color_indices, *alpha_indices, *alpha_table;
    int i, j, x, y, block_x, block_y, color_index, alpha_index;
    int block_size, color_offset, color_indices_offset;
    WORD color[4], color_value = 0;
    BYTE alpha[8], alpha_value = 0;

    if (format == DXGI_FORMAT_BC1_UNORM) {
        block_size = 8;
        color_offset = 0;
        color_indices_offset = 4;
    } else {
        block_size = 16;
        color_offset = 8;
        color_indices_offset = 12;
    }
    block_x = 0;
    block_y = 0;

    for (i = 0; i < block_count; i++)
    {
        block = block_data + i * block_size;

        color[0] = *((WORD *)(block + color_offset));
        color[1] = *((WORD *)(block + color_offset + 2));
        color[2] = MAKE_RGB565(((GET_RGB565_R(color[0]) * 2 + GET_RGB565_R(color[1]) + 1) / 3),
                               ((GET_RGB565_G(color[0]) * 2 + GET_RGB565_G(color[1]) + 1) / 3),
                               ((GET_RGB565_B(color[0]) * 2 + GET_RGB565_B(color[1]) + 1) / 3));
        color[3] = MAKE_RGB565(((GET_RGB565_R(color[0]) + GET_RGB565_R(color[1]) * 2 + 1) / 3),
                               ((GET_RGB565_G(color[0]) + GET_RGB565_G(color[1]) * 2 + 1) / 3),
                               ((GET_RGB565_B(color[0]) + GET_RGB565_B(color[1]) * 2 + 1) / 3));

        switch (format)
        {
            case DXGI_FORMAT_BC1_UNORM:
                if (color[0] <= color[1]) {
                    color[2] = MAKE_RGB565(((GET_RGB565_R(color[0]) + GET_RGB565_R(color[1]) + 1) / 2),
                                           ((GET_RGB565_G(color[0]) + GET_RGB565_G(color[1]) + 1) / 2),
                                           ((GET_RGB565_B(color[0]) + GET_RGB565_B(color[1]) + 1) / 2));
                    color[3] = 0;
                }
                break;
            case DXGI_FORMAT_BC2_UNORM:
                alpha_table = block;
                break;
            case DXGI_FORMAT_BC3_UNORM:
                alpha[0] = *block;
                alpha[1] = *(block + 1);
                if (alpha[0] > alpha[1]) {
                    for (j = 2; j < 8; j++)
                    {
                        alpha[j] = (BYTE)((alpha[0] * (8 - j) + alpha[1] * (j - 1) + 3) / 7);
                    }
                } else {
                    for (j = 2; j < 6; j++)
                    {
                        alpha[j] = (BYTE)((alpha[0] * (6 - j) + alpha[1] * (j - 1) + 2) / 5);
                    }
                    alpha[6] = 0;
                    alpha[7] = 0xFF;
                }
                alpha_indices = block + 2;
                break;
            default:
                break;
        }

        color_indices = block + color_indices_offset;
        for (j = 0; j < 16; j++)
        {
            x = block_x + j % 4;
            y = block_y + j / 4;
            if (x >= width || y >= height) continue;

            color_index = (color_indices[j / 4] >> ((j % 4) * 2)) & 0x3;
            color_value = color[color_index];

            switch (format)
            {
                case DXGI_FORMAT_BC1_UNORM:
                    if ((color[0] <= color[1]) && !color_value) {
                        color_value = 0;
                        alpha_value = 0;
                    } else {
                        alpha_value = 0xFF;
                    }
                    break;
                case DXGI_FORMAT_BC2_UNORM:
                    alpha_value = (alpha_table[j / 2] >> (j % 2) * 4) & 0xF;
                    alpha_value = (BYTE)((alpha_value * 0xFF + 0x7)/ 0xF);
                    break;
                case DXGI_FORMAT_BC3_UNORM:
                    alpha_index = (*((DWORD *)(alpha_indices + (j / 8) * 3)) >> ((j % 8) * 3)) & 0x7;
                    alpha_value = alpha[alpha_index];
                    break;
                default:
                    break;
            }
            buffer[x + y * width] = rgb565_to_argb(color_value, alpha_value);
        }

        block_x += DDS_BLOCK_WIDTH;
        if (block_x >= width) {
            block_x = 0;
            block_y += DDS_BLOCK_HEIGHT;
        }
    }
}

715 716 717 718 719
static inline DdsDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
{
    return CONTAINING_RECORD(iface, DdsDecoder, IWICBitmapDecoder_iface);
}

720 721 722 723 724
static inline DdsDecoder *impl_from_IWICDdsDecoder(IWICDdsDecoder *iface)
{
    return CONTAINING_RECORD(iface, DdsDecoder, IWICDdsDecoder_iface);
}

725 726 727 728 729
static inline DdsDecoder *impl_from_IWICWineDecoder(IWICWineDecoder *iface)
{
    return CONTAINING_RECORD(iface, DdsDecoder, IWICWineDecoder_iface);
}

730 731 732 733 734
static inline DdsFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
{
    return CONTAINING_RECORD(iface, DdsFrameDecode, IWICBitmapFrameDecode_iface);
}

735 736 737 738 739
static inline DdsFrameDecode *impl_from_IWICDdsFrameDecode(IWICDdsFrameDecode *iface)
{
    return CONTAINING_RECORD(iface, DdsFrameDecode, IWICDdsFrameDecode_iface);
}

740 741 742 743 744
static inline DdsEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
{
    return CONTAINING_RECORD(iface, DdsEncoder, IWICBitmapEncoder_iface);
}

745 746 747 748 749
static inline DdsEncoder *impl_from_IWICDdsEncoder(IWICDdsEncoder *iface)
{
    return CONTAINING_RECORD(iface, DdsEncoder, IWICDdsEncoder_iface);
}

750 751 752 753 754
static inline DdsFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
{
    return CONTAINING_RECORD(iface, DdsFrameEncode, IWICBitmapFrameEncode_iface);
}

755 756 757 758 759 760 761 762 763 764 765 766
static HRESULT WINAPI DdsFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
                                                    void **ppv)
{
    DdsFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
    TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);

    if (!ppv) return E_INVALIDARG;

    if (IsEqualIID(&IID_IUnknown, iid) ||
        IsEqualIID(&IID_IWICBitmapSource, iid) ||
        IsEqualIID(&IID_IWICBitmapFrameDecode, iid)) {
        *ppv = &This->IWICBitmapFrameDecode_iface;
767 768
    } else if (IsEqualGUID(&IID_IWICDdsFrameDecode, iid)) {
        *ppv = &This->IWICDdsFrameDecode_iface;
769 770 771 772 773 774 775 776 777 778 779 780 781 782
    } else {
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI DdsFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
{
    DdsFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
    ULONG ref = InterlockedIncrement(&This->ref);

783
    TRACE("(%p) refcount=%lu\n", iface, ref);
784 785 786 787 788 789 790 791 792

    return ref;
}

static ULONG WINAPI DdsFrameDecode_Release(IWICBitmapFrameDecode *iface)
{
    DdsFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
    ULONG ref = InterlockedDecrement(&This->ref);

793
    TRACE("(%p) refcount=%lu\n", iface, ref);
794

795
    if (ref == 0) {
796 797 798
        if (This->pixel_data != This->block_data) free(This->pixel_data);
        free(This->block_data);
        free(This);
799
    }
800 801 802 803 804 805 806

    return ref;
}

static HRESULT WINAPI DdsFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
                                             UINT *puiWidth, UINT *puiHeight)
{
807
    DdsFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
808

809 810
    if (!puiWidth || !puiHeight) return E_INVALIDARG;

811 812
    *puiWidth = This->info.width;
    *puiHeight = This->info.height;
813 814 815 816

    TRACE("(%p) -> (%d,%d)\n", iface, *puiWidth, *puiHeight);

    return S_OK;
817 818 819 820 821
}

static HRESULT WINAPI DdsFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
                                                    WICPixelFormatGUID *pPixelFormat)
{
822
    DdsFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
823

824 825 826 827 828 829 830
    if (!pPixelFormat) return E_INVALIDARG;

    *pPixelFormat = *This->info.pixel_format;

    TRACE("(%p) -> %s\n", iface, debugstr_guid(pPixelFormat));

    return S_OK;
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
}

static HRESULT WINAPI DdsFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
                                                   double *pDpiX, double *pDpiY)
{
    FIXME("(%p,%p,%p): stub.\n", iface, pDpiX, pDpiY);

    return E_NOTIMPL;
}

static HRESULT WINAPI DdsFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
                                                 IWICPalette *pIPalette)
{
    FIXME("(%p,%p): stub.\n", iface, pIPalette);

    return E_NOTIMPL;
}

static HRESULT WINAPI DdsFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
                                                const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
{
852 853 854
    DdsFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
    UINT bpp, frame_stride, frame_size;
    INT x, y, width, height;
855
    HRESULT hr;
856

857 858 859 860 861
    TRACE("(%p,%s,%u,%u,%p)\n", iface, debug_wic_rect(prc), cbStride, cbBufferSize, pbBuffer);

    if (!pbBuffer) return E_INVALIDARG;

    bpp = This->info.pixel_format_bpp;
862 863
    if (!bpp) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;

864 865 866 867 868
    frame_stride = This->info.width * bpp / 8;
    frame_size = frame_stride * This->info.height;
    if (!prc) {
        if (cbStride < frame_stride) return E_INVALIDARG;
        if (cbBufferSize < frame_size) return WINCODEC_ERR_INSUFFICIENTBUFFER;
869 870 871 872 873 874 875 876 877 878 879 880
    } else {
        x = prc->X;
        y = prc->Y;
        width = prc->Width;
        height = prc->Height;
        if (x < 0 || y < 0 || width <= 0 || height <= 0 ||
            x + width > This->info.width ||
            y + height > This->info.height) {
            return E_INVALIDARG;
        }
        if (cbStride < width * bpp / 8) return E_INVALIDARG;
        if (cbBufferSize < cbStride * height) return WINCODEC_ERR_INSUFFICIENTBUFFER;
881 882
    }

883 884 885
    EnterCriticalSection(&This->lock);

    if (!This->pixel_data) {
886
        if (is_compressed(This->info.format)) {
887
            This->pixel_data = malloc(frame_size);
888 889 890 891 892 893 894 895
            if (!This->pixel_data) {
                hr = E_OUTOFMEMORY;
                goto end;
            }
            decode_block(This->block_data, This->info.width_in_blocks * This->info.height_in_blocks, This->info.format,
                         This->info.width, This->info.height, (DWORD *)This->pixel_data);
        } else {
            This->pixel_data = This->block_data;
896 897 898
        }
    }

899 900 901
    hr = copy_pixels(bpp, This->pixel_data, This->info.width, This->info.height, frame_stride,
                     prc, cbStride, cbBufferSize, pbBuffer);

902 903 904 905
end:
    LeaveCriticalSection(&This->lock);

    return hr;
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
}

static HRESULT WINAPI DdsFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
                                                            IWICMetadataQueryReader **ppIMetadataQueryReader)
{
    FIXME("(%p,%p): stub.\n", iface, ppIMetadataQueryReader);

    return E_NOTIMPL;
}

static HRESULT WINAPI DdsFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
                                                      UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
{
    FIXME("(%p,%u,%p,%p): stub.\n", iface, cCount, ppIColorContexts, pcActualCount);

    return E_NOTIMPL;
}

static HRESULT WINAPI DdsFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
                                                  IWICBitmapSource **ppIThumbnail)
{
    FIXME("(%p,%p): stub.\n", iface, ppIThumbnail);

    return E_NOTIMPL;
}

static const IWICBitmapFrameDecodeVtbl DdsFrameDecode_Vtbl = {
    DdsFrameDecode_QueryInterface,
    DdsFrameDecode_AddRef,
    DdsFrameDecode_Release,
    DdsFrameDecode_GetSize,
    DdsFrameDecode_GetPixelFormat,
    DdsFrameDecode_GetResolution,
    DdsFrameDecode_CopyPalette,
    DdsFrameDecode_CopyPixels,
    DdsFrameDecode_GetMetadataQueryReader,
    DdsFrameDecode_GetColorContexts,
    DdsFrameDecode_GetThumbnail
};

946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
static HRESULT WINAPI DdsFrameDecode_Dds_QueryInterface(IWICDdsFrameDecode *iface,
                                                        REFIID iid, void **ppv)
{
    DdsFrameDecode *This = impl_from_IWICDdsFrameDecode(iface);
    return DdsFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
}

static ULONG WINAPI DdsFrameDecode_Dds_AddRef(IWICDdsFrameDecode *iface)
{
    DdsFrameDecode *This = impl_from_IWICDdsFrameDecode(iface);
    return DdsFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
}

static ULONG WINAPI DdsFrameDecode_Dds_Release(IWICDdsFrameDecode *iface)
{
    DdsFrameDecode *This = impl_from_IWICDdsFrameDecode(iface);
    return DdsFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
}

static HRESULT WINAPI DdsFrameDecode_Dds_GetSizeInBlocks(IWICDdsFrameDecode *iface,
                                                         UINT *widthInBlocks, UINT *heightInBlocks)
{
968
    DdsFrameDecode *This = impl_from_IWICDdsFrameDecode(iface);
969

970 971 972 973 974 975 976 977
    if (!widthInBlocks || !heightInBlocks) return E_INVALIDARG;

    *widthInBlocks = This->info.width_in_blocks;
    *heightInBlocks = This->info.height_in_blocks;

    TRACE("(%p,%p,%p) -> (%d,%d)\n", iface, widthInBlocks, heightInBlocks, *widthInBlocks, *heightInBlocks);

    return S_OK;
978 979 980 981 982
}

static HRESULT WINAPI DdsFrameDecode_Dds_GetFormatInfo(IWICDdsFrameDecode *iface,
                                                       WICDdsFormatInfo *formatInfo)
{
983
    DdsFrameDecode *This = impl_from_IWICDdsFrameDecode(iface);
984

985 986 987 988 989 990 991 992 993 994 995
    if (!formatInfo) return E_INVALIDARG;

    formatInfo->DxgiFormat = This->info.format;
    formatInfo->BytesPerBlock = This->info.bytes_per_block;
    formatInfo->BlockWidth = This->info.block_width;
    formatInfo->BlockHeight = This->info.block_height;

    TRACE("(%p,%p) -> (0x%x,%d,%d,%d)\n", iface, formatInfo,
          formatInfo->DxgiFormat, formatInfo->BytesPerBlock, formatInfo->BlockWidth, formatInfo->BlockHeight);

    return S_OK;
996 997 998 999 1000 1001
}

static HRESULT WINAPI DdsFrameDecode_Dds_CopyBlocks(IWICDdsFrameDecode *iface,
                                                    const WICRect *boundsInBlocks, UINT stride, UINT bufferSize,
                                                    BYTE *buffer)
{
1002 1003
    DdsFrameDecode *This = impl_from_IWICDdsFrameDecode(iface);
    int x, y, width, height;
1004
    UINT bytes_per_block, frame_stride, frame_size;
1005 1006 1007 1008 1009 1010 1011 1012

    TRACE("(%p,%p,%u,%u,%p)\n", iface, boundsInBlocks, stride, bufferSize, buffer);

    if (!buffer) return E_INVALIDARG;

    bytes_per_block = This->info.bytes_per_block;
    frame_stride = This->info.width_in_blocks * bytes_per_block;
    frame_size = frame_stride * This->info.height_in_blocks;
1013

1014 1015 1016
    if (!boundsInBlocks) {
        if (stride < frame_stride) return E_INVALIDARG;
        if (bufferSize < frame_size) return E_INVALIDARG;
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
    } else {
        x = boundsInBlocks->X;
        y = boundsInBlocks->Y;
        width = boundsInBlocks->Width;
        height = boundsInBlocks->Height;
        if (x < 0 || y < 0 || width <= 0 || height <= 0 ||
            x + width > This->info.width_in_blocks ||
            y + height > This->info.height_in_blocks) {
            return E_INVALIDARG;
        }
        if (stride < width * bytes_per_block) return E_INVALIDARG;
        if (bufferSize < stride * height) return E_INVALIDARG;
1029 1030
    }

1031 1032
    return copy_pixels(This->info.bytes_per_block * 8, This->block_data, This->info.width_in_blocks,
                       This->info.height_in_blocks, frame_stride, boundsInBlocks, stride, bufferSize, buffer);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
}

static const IWICDdsFrameDecodeVtbl DdsFrameDecode_Dds_Vtbl = {
    DdsFrameDecode_Dds_QueryInterface,
    DdsFrameDecode_Dds_AddRef,
    DdsFrameDecode_Dds_Release,
    DdsFrameDecode_Dds_GetSizeInBlocks,
    DdsFrameDecode_Dds_GetFormatInfo,
    DdsFrameDecode_Dds_CopyBlocks
};

1044 1045 1046 1047
static HRESULT DdsFrameDecode_CreateInstance(DdsFrameDecode **frame_decode)
{
    DdsFrameDecode *result;

1048
    result = malloc(sizeof(*result));
1049 1050 1051
    if (!result) return E_OUTOFMEMORY;

    result->IWICBitmapFrameDecode_iface.lpVtbl = &DdsFrameDecode_Vtbl;
1052
    result->IWICDdsFrameDecode_iface.lpVtbl = &DdsFrameDecode_Dds_Vtbl;
1053
    result->ref = 1;
1054 1055
    InitializeCriticalSection(&result->lock);
    result->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DdsFrameDecode.lock");
1056 1057 1058 1059 1060

    *frame_decode = result;
    return S_OK;
}

1061 1062 1063 1064 1065 1066 1067 1068 1069
static HRESULT WINAPI DdsDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
                                                void **ppv)
{
    DdsDecoder *This = impl_from_IWICBitmapDecoder(iface);
    TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);

    if (!ppv) return E_INVALIDARG;

    if (IsEqualIID(&IID_IUnknown, iid) ||
1070
        IsEqualIID(&IID_IWICBitmapDecoder, iid)) {
1071
        *ppv = &This->IWICBitmapDecoder_iface;
1072 1073
    } else if (IsEqualIID(&IID_IWICDdsDecoder, iid)) {
        *ppv = &This->IWICDdsDecoder_iface;
1074 1075
    } else if (IsEqualIID(&IID_IWICWineDecoder, iid)) {
        *ppv = &This->IWICWineDecoder_iface;
1076
    } else {
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI DdsDecoder_AddRef(IWICBitmapDecoder *iface)
{
    DdsDecoder *This = impl_from_IWICBitmapDecoder(iface);
    ULONG ref = InterlockedIncrement(&This->ref);

1090
    TRACE("(%p) refcount=%lu\n", iface, ref);
1091 1092 1093 1094 1095 1096 1097 1098 1099

    return ref;
}

static ULONG WINAPI DdsDecoder_Release(IWICBitmapDecoder *iface)
{
    DdsDecoder *This = impl_from_IWICBitmapDecoder(iface);
    ULONG ref = InterlockedDecrement(&This->ref);

1100
    TRACE("(%p) refcount=%lu\n", iface, ref);
1101 1102 1103 1104 1105 1106

    if (ref == 0)
    {
        This->lock.DebugInfo->Spare[0] = 0;
        DeleteCriticalSection(&This->lock);
        if (This->stream) IStream_Release(This->stream);
1107
        free(This);
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
    }

    return ref;
}

static HRESULT WINAPI DdsDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
                                                 DWORD *capability)
{
    FIXME("(%p,%p,%p): stub.\n", iface, stream, capability);

    return E_NOTIMPL;
}

static HRESULT WINAPI DdsDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
                                            WICDecodeOptions cacheOptions)
{
1124
    DdsDecoder *This = impl_from_IWICBitmapDecoder(iface);
1125
    HRESULT hr;
1126

1127 1128
    TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);

1129 1130 1131 1132 1133
    EnterCriticalSection(&This->lock);

    hr = IWICWineDecoder_Initialize(&This->IWICWineDecoder_iface, pIStream, cacheOptions);
    if (FAILED(hr)) goto end;

1134 1135 1136 1137
    if (This->info.dimension == WICDdsTextureCube ||
        (This->info.format != DXGI_FORMAT_BC1_UNORM &&
         This->info.format != DXGI_FORMAT_BC2_UNORM &&
         This->info.format != DXGI_FORMAT_BC3_UNORM)) {
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
        IStream_Release(pIStream);
        This->stream = NULL;
        This->initialized = FALSE;
        hr = WINCODEC_ERR_BADHEADER;
    }

end:
    LeaveCriticalSection(&This->lock);

    return hr;
1148 1149 1150 1151 1152
}

static HRESULT WINAPI DdsDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
                                                    GUID *pguidContainerFormat)
{
1153 1154
    TRACE("(%p,%p)\n", iface, pguidContainerFormat);

1155
    memcpy(pguidContainerFormat, &GUID_ContainerFormatDds, sizeof(GUID));
1156

1157 1158 1159 1160 1161 1162
    return S_OK;
}

static HRESULT WINAPI DdsDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
                                                IWICBitmapDecoderInfo **ppIDecoderInfo)
{
1163
    TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
1164

1165
    return get_decoder_info(&CLSID_WICDdsDecoder, ppIDecoderInfo);
1166 1167 1168 1169 1170
}

static HRESULT WINAPI DdsDecoder_CopyPalette(IWICBitmapDecoder *iface,
                                             IWICPalette *pIPalette)
{
1171
    TRACE("(%p,%p)\n", iface, pIPalette);
1172

1173
    return WINCODEC_ERR_PALETTEUNAVAILABLE;
1174 1175 1176 1177 1178
}

static HRESULT WINAPI DdsDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
                                                        IWICMetadataQueryReader **ppIMetadataQueryReader)
{
1179 1180 1181
    if (!ppIMetadataQueryReader) return E_INVALIDARG;

    FIXME("(%p,%p)\n", iface, ppIMetadataQueryReader);
1182 1183 1184 1185 1186 1187 1188

    return E_NOTIMPL;
}

static HRESULT WINAPI DdsDecoder_GetPreview(IWICBitmapDecoder *iface,
                                            IWICBitmapSource **ppIBitmapSource)
{
1189
    TRACE("(%p,%p)\n", iface, ppIBitmapSource);
1190

1191
    return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1192 1193 1194 1195 1196
}

static HRESULT WINAPI DdsDecoder_GetColorContexts(IWICBitmapDecoder *iface,
                                                  UINT cCount, IWICColorContext **ppDdslorContexts, UINT *pcActualCount)
{
1197
    TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppDdslorContexts, pcActualCount);
1198

1199
    return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1200 1201 1202 1203 1204
}

static HRESULT WINAPI DdsDecoder_GetThumbnail(IWICBitmapDecoder *iface,
                                              IWICBitmapSource **ppIThumbnail)
{
1205
    TRACE("(%p,%p)\n", iface, ppIThumbnail);
1206

1207
    return WINCODEC_ERR_CODECNOTHUMBNAIL;
1208 1209 1210 1211 1212
}

static HRESULT WINAPI DdsDecoder_GetFrameCount(IWICBitmapDecoder *iface,
                                               UINT *pCount)
{
1213
    DdsDecoder *This = impl_from_IWICBitmapDecoder(iface);
1214

1215 1216 1217 1218 1219
    if (!pCount) return E_INVALIDARG;
    if (!This->initialized) return WINCODEC_ERR_WRONGSTATE;

    EnterCriticalSection(&This->lock);

1220
    *pCount = This->info.frame_count;
1221 1222 1223

    LeaveCriticalSection(&This->lock);

1224
    TRACE("(%p) -> %d\n", iface, *pCount);
1225 1226

    return S_OK;
1227 1228 1229 1230 1231
}

static HRESULT WINAPI DdsDecoder_GetFrame(IWICBitmapDecoder *iface,
                                          UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
{
1232
    DdsDecoder *This = impl_from_IWICBitmapDecoder(iface);
1233
    UINT array_index, mip_level, slice_index;
1234

1235
    TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
1236

1237
    if (!ppIBitmapFrame) return E_INVALIDARG;
1238

1239 1240 1241 1242 1243 1244 1245
    EnterCriticalSection(&This->lock);

    if (!This->initialized) {
        LeaveCriticalSection(&This->lock);
        return WINCODEC_ERR_WRONGSTATE;
    }

1246
    get_frame_dds_index(index, &This->info, &array_index, &mip_level, &slice_index);
1247 1248 1249 1250

    LeaveCriticalSection(&This->lock);

    return DdsDecoder_Dds_GetFrame(&This->IWICDdsDecoder_iface, array_index, mip_level, slice_index, ppIBitmapFrame);
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
}

static const IWICBitmapDecoderVtbl DdsDecoder_Vtbl = {
        DdsDecoder_QueryInterface,
        DdsDecoder_AddRef,
        DdsDecoder_Release,
        DdsDecoder_QueryCapability,
        DdsDecoder_Initialize,
        DdsDecoder_GetContainerFormat,
        DdsDecoder_GetDecoderInfo,
        DdsDecoder_CopyPalette,
        DdsDecoder_GetMetadataQueryReader,
        DdsDecoder_GetPreview,
        DdsDecoder_GetColorContexts,
        DdsDecoder_GetThumbnail,
        DdsDecoder_GetFrameCount,
        DdsDecoder_GetFrame
};

1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
static HRESULT WINAPI DdsDecoder_Dds_QueryInterface(IWICDdsDecoder *iface,
                                                    REFIID iid, void **ppv)
{
    DdsDecoder *This = impl_from_IWICDdsDecoder(iface);
    return DdsDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
}

static ULONG WINAPI DdsDecoder_Dds_AddRef(IWICDdsDecoder *iface)
{
    DdsDecoder *This = impl_from_IWICDdsDecoder(iface);
    return DdsDecoder_AddRef(&This->IWICBitmapDecoder_iface);
}

static ULONG WINAPI DdsDecoder_Dds_Release(IWICDdsDecoder *iface)
{
    DdsDecoder *This = impl_from_IWICDdsDecoder(iface);
    return DdsDecoder_Release(&This->IWICBitmapDecoder_iface);
}

static HRESULT WINAPI DdsDecoder_Dds_GetParameters(IWICDdsDecoder *iface,
                                                   WICDdsParameters *parameters)
{
1292 1293
    DdsDecoder *This = impl_from_IWICDdsDecoder(iface);
    HRESULT hr;
1294

1295 1296 1297 1298 1299 1300 1301 1302 1303
    if (!parameters) return E_INVALIDARG;

    EnterCriticalSection(&This->lock);

    if (!This->initialized) {
        hr = WINCODEC_ERR_WRONGSTATE;
        goto end;
    }

1304 1305 1306 1307 1308 1309 1310 1311
    parameters->Width = This->info.width;
    parameters->Height = This->info.height;
    parameters->Depth = This->info.depth;
    parameters->MipLevels = This->info.mip_levels;
    parameters->ArraySize = This->info.array_size;
    parameters->DxgiFormat = This->info.format;
    parameters->Dimension = This->info.dimension;
    parameters->AlphaMode = This->info.alpha_mode;
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322

    TRACE("(%p) -> (%dx%d depth=%d mipLevels=%d arraySize=%d dxgiFormat=0x%x dimension=0x%x alphaMode=0x%x)\n",
          iface, parameters->Width, parameters->Height, parameters->Depth, parameters->MipLevels,
          parameters->ArraySize, parameters->DxgiFormat, parameters->Dimension, parameters->AlphaMode);

    hr = S_OK;

end:
    LeaveCriticalSection(&This->lock);

    return hr;
1323 1324 1325 1326 1327 1328
}

static HRESULT WINAPI DdsDecoder_Dds_GetFrame(IWICDdsDecoder *iface,
                                              UINT arrayIndex, UINT mipLevel, UINT sliceIndex,
                                              IWICBitmapFrameDecode **bitmapFrame)
{
1329 1330
    DdsDecoder *This = impl_from_IWICDdsDecoder(iface);
    HRESULT hr;
1331
    LARGE_INTEGER seek;
1332
    UINT width, height, depth, block_width, block_height, width_in_blocks, height_in_blocks, size;
1333
    UINT frame_width = 0, frame_height = 0, frame_width_in_blocks = 0, frame_height_in_blocks = 0, frame_size = 0;
1334 1335
    UINT bytes_per_block, i;
    DWORD bytesread;
1336
    DdsFrameDecode *frame_decode = NULL;
1337

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
    TRACE("(%p,%u,%u,%u,%p)\n", iface, arrayIndex, mipLevel, sliceIndex, bitmapFrame);

    if (!bitmapFrame) return E_INVALIDARG;

    EnterCriticalSection(&This->lock);

    if (!This->initialized) {
        hr = WINCODEC_ERR_WRONGSTATE;
        goto end;
    }
1348 1349 1350 1351 1352

    if ((arrayIndex >= This->info.array_size && This->info.dimension != WICDdsTextureCube) ||
        (arrayIndex >= This->info.array_size * 6) ||
        (mipLevel   >= This->info.mip_levels) ||
        (sliceIndex >= This->info.depth)) {
1353 1354 1355 1356
        hr = E_INVALIDARG;
        goto end;
    }

1357
    if (is_compressed(This->info.format)) {
1358 1359 1360 1361 1362 1363
        block_width = DDS_BLOCK_WIDTH;
        block_height = DDS_BLOCK_HEIGHT;
    } else {
        block_width = 1;
        block_height = 1;
    }
1364
    bytes_per_block = This->info.bytes_per_block;
1365
    seek.QuadPart = This->info.data_offset;
1366

1367 1368
    width = This->info.width;
    height = This->info.height;
1369 1370
    depth = This->info.depth;
    for (i = 0; i < This->info.mip_levels; i++)
1371
    {
1372 1373
        width_in_blocks = (width + block_width - 1) / block_width;
        height_in_blocks = (height + block_height - 1) / block_height;
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
        size = width_in_blocks * height_in_blocks * bytes_per_block;

        if (i < mipLevel)  {
            seek.QuadPart += size * depth;
        } else if (i == mipLevel){
            seek.QuadPart += size * sliceIndex;
            frame_width = width;
            frame_height = height;
            frame_width_in_blocks = width_in_blocks;
            frame_height_in_blocks = height_in_blocks;
            frame_size = frame_width_in_blocks * frame_height_in_blocks * bytes_per_block;
            if (arrayIndex == 0) break;
        }
        seek.QuadPart += arrayIndex * size * depth;

1389 1390
        if (width > 1) width /= 2;
        if (height > 1) height /= 2;
1391
        if (depth > 1) depth /= 2;
1392 1393 1394 1395
    }

    hr = DdsFrameDecode_CreateInstance(&frame_decode);
    if (hr != S_OK) goto end;
1396 1397
    frame_decode->info.width = frame_width;
    frame_decode->info.height = frame_height;
1398
    frame_decode->info.format = This->info.format;
1399
    frame_decode->info.bytes_per_block = bytes_per_block;
1400 1401
    frame_decode->info.block_width = block_width;
    frame_decode->info.block_height = block_height;
1402 1403
    frame_decode->info.width_in_blocks = frame_width_in_blocks;
    frame_decode->info.height_in_blocks = frame_height_in_blocks;
1404
    frame_decode->info.pixel_format = This->info.pixel_format;
1405
    frame_decode->info.pixel_format_bpp = This->info.pixel_format_bpp;
1406
    frame_decode->block_data = malloc(frame_size);
1407
    frame_decode->pixel_data = NULL;
1408 1409
    hr = IStream_Seek(This->stream, seek, SEEK_SET, NULL);
    if (hr != S_OK) goto end;
1410
    hr = IStream_Read(This->stream, frame_decode->block_data, frame_size, &bytesread);
1411 1412 1413 1414
    if (hr != S_OK || bytesread != frame_size) {
        hr = WINCODEC_ERR_STREAMREAD;
        goto end;
    }
1415 1416 1417 1418 1419 1420 1421
    *bitmapFrame = &frame_decode->IWICBitmapFrameDecode_iface;

    hr = S_OK;

end:
    LeaveCriticalSection(&This->lock);

1422 1423
    if (hr != S_OK && frame_decode) DdsFrameDecode_Release(&frame_decode->IWICBitmapFrameDecode_iface);

1424
    return hr;
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
}

static const IWICDdsDecoderVtbl DdsDecoder_Dds_Vtbl = {
    DdsDecoder_Dds_QueryInterface,
    DdsDecoder_Dds_AddRef,
    DdsDecoder_Dds_Release,
    DdsDecoder_Dds_GetParameters,
    DdsDecoder_Dds_GetFrame
};

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
static HRESULT WINAPI DdsDecoder_Wine_QueryInterface(IWICWineDecoder *iface, REFIID iid, void **ppv)
{
    DdsDecoder *This = impl_from_IWICWineDecoder(iface);
    return DdsDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
}

static ULONG WINAPI DdsDecoder_Wine_AddRef(IWICWineDecoder *iface)
{
    DdsDecoder *This = impl_from_IWICWineDecoder(iface);
    return DdsDecoder_AddRef(&This->IWICBitmapDecoder_iface);
}

static ULONG WINAPI DdsDecoder_Wine_Release(IWICWineDecoder *iface)
{
    DdsDecoder *This = impl_from_IWICWineDecoder(iface);
    return DdsDecoder_Release(&This->IWICBitmapDecoder_iface);
}

static HRESULT WINAPI DdsDecoder_Wine_Initialize(IWICWineDecoder *iface, IStream *stream, WICDecodeOptions options)
{
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
    DdsDecoder *This = impl_from_IWICWineDecoder(iface);
    DDS_HEADER_DXT10 header_dxt10;
    LARGE_INTEGER seek;
    DDS_HEADER header;
    ULONG bytesread;
    DWORD magic;
    HRESULT hr;

    TRACE("(This %p, stream %p, options %#x)\n", iface, stream, options);

    EnterCriticalSection(&This->lock);

    if (This->initialized) {
        hr = WINCODEC_ERR_WRONGSTATE;
        goto end;
    }

    seek.QuadPart = 0;
    hr = IStream_Seek(stream, seek, SEEK_SET, NULL);
    if (FAILED(hr)) goto end;

    hr = IStream_Read(stream, &magic, sizeof(magic), &bytesread);
    if (FAILED(hr)) goto end;
    if (bytesread != sizeof(magic)) {
        hr = WINCODEC_ERR_STREAMREAD;
        goto end;
    }
    if (magic != DDS_MAGIC) {
        hr = WINCODEC_ERR_UNKNOWNIMAGEFORMAT;
        goto end;
    }

    hr = IStream_Read(stream, &header, sizeof(header), &bytesread);
    if (FAILED(hr)) goto end;
    if (bytesread != sizeof(header)) {
        hr = WINCODEC_ERR_STREAMREAD;
        goto end;
    }
    if (header.size != sizeof(header)) {
        hr = WINCODEC_ERR_BADHEADER;
        goto end;
    }

    if (has_extended_header(&header)) {
        hr = IStream_Read(stream, &header_dxt10, sizeof(header_dxt10), &bytesread);
        if (FAILED(hr)) goto end;
        if (bytesread != sizeof(header_dxt10)) {
            hr = WINCODEC_ERR_STREAMREAD;
            goto end;
        }
    }

    get_dds_info(&This->info, &header, &header_dxt10);

    This->initialized = TRUE;
    This->stream = stream;
    IStream_AddRef(stream);

end:
    LeaveCriticalSection(&This->lock);

    return hr;
1517 1518 1519 1520 1521 1522 1523 1524 1525
}

static const IWICWineDecoderVtbl DdsDecoder_Wine_Vtbl = {
    DdsDecoder_Wine_QueryInterface,
    DdsDecoder_Wine_AddRef,
    DdsDecoder_Wine_Release,
    DdsDecoder_Wine_Initialize
};

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
static HRESULT WINAPI DdsFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid, void **ppv)
{
    DdsFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
    TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);

    if (!ppv) return E_INVALIDARG;

    if (IsEqualIID(&IID_IUnknown, iid) ||
        IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
    {
        *ppv = &This->IWICBitmapFrameEncode_iface;
    }
    else
    {
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI DdsFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
{
    DdsFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
    ULONG ref = InterlockedIncrement(&This->ref);

1553
    TRACE("(%p) refcount=%lu\n", iface, ref);
1554 1555 1556 1557 1558 1559 1560 1561 1562

    return ref;
}

static ULONG WINAPI DdsFrameEncode_Release(IWICBitmapFrameEncode *iface)
{
    DdsFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
    ULONG ref = InterlockedDecrement(&This->ref);

1563
    TRACE("(%p) refcount=%lu\n", iface, ref);
1564 1565 1566 1567

    if (ref == 0)
    {
        IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1568
        free(This);
1569 1570 1571 1572 1573 1574 1575 1576
    }

    return ref;
}

static HRESULT WINAPI DdsFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
                                                IPropertyBag2 *encoderOptions)
{
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
    DdsFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
    HRESULT hr;

    TRACE("(%p,%p)\n", iface, encoderOptions);
    if (encoderOptions) FIXME("encoder options are not supported for DDS.\n");

    EnterCriticalSection(&This->parent->lock);

    if (This->initialized)
    {
        hr = WINCODEC_ERR_WRONGSTATE;
    }
    else
    {
        This->initialized = TRUE;
        hr = S_OK;
    }

    LeaveCriticalSection(&This->parent->lock);

    return hr;
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
}

static HRESULT WINAPI DdsFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
                                             UINT width, UINT height)
{
    DdsFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
    HRESULT hr;

    TRACE("(%p,%u,%u)\n", iface, width, height);

    EnterCriticalSection(&This->parent->lock);

    if (!This->initialized || This->frame_created)
    {
        hr = WINCODEC_ERR_WRONGSTATE;
    }
    else
    {
        This->width = width;
        This->height = height;
        hr = S_OK;
    }

    LeaveCriticalSection(&This->parent->lock);

    return hr;
}

static HRESULT WINAPI DdsFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
                                                   double dpiX, double dpiY)
{
    DdsFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
    HRESULT hr;

    TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);

    EnterCriticalSection(&This->parent->lock);

    if (!This->initialized || This->frame_created)
    {
        hr = WINCODEC_ERR_WRONGSTATE;
    }
    else
    {
        This->dpi_x = dpiX;
        This->dpi_y = dpiY;
        hr = S_OK;
    }

    LeaveCriticalSection(&This->parent->lock);

    return hr;
}

static HRESULT WINAPI DdsFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
                                                    WICPixelFormatGUID *pixelFormat)
{
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
    DdsFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
    HRESULT hr;

    TRACE("(%p,%s)\n", iface, debugstr_guid(pixelFormat));

    EnterCriticalSection(&This->parent->lock);

    if (!This->initialized)
    {
        hr = WINCODEC_ERR_NOTINITIALIZED;
    }
    else if (This->frame_created)
    {
        hr = WINCODEC_ERR_WRONGSTATE;
    }
    else
    {
        *pixelFormat = GUID_WICPixelFormat32bppBGRA;
        hr = S_OK;
    }

    LeaveCriticalSection(&This->parent->lock);

    return hr;
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
}

static HRESULT WINAPI DdsFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
                                                      UINT count, IWICColorContext **colorContext)
{
    FIXME("(%p,%u,%p): stub\n", iface, count, colorContext);
    return E_NOTIMPL;
}

static HRESULT WINAPI DdsFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
                                                IWICPalette *palette)
{
    FIXME("(%p,%p): stub\n", iface, palette);
    return E_NOTIMPL;
}

static HRESULT WINAPI DdsFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
                                                  IWICBitmapSource *thumbnail)
{
    TRACE("(%p,%p)\n", iface, thumbnail);
    return WINCODEC_ERR_UNSUPPORTEDOPERATION;
}

static HRESULT WINAPI DdsFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
                                                 UINT lineCount, UINT stride, UINT bufferSize, BYTE *pixels)
{
    FIXME("(%p,%u,%u,%u,%p): stub\n", iface, lineCount, stride, bufferSize, pixels);
    return E_NOTIMPL;
}

static HRESULT WINAPI DdsFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
                                                 IWICBitmapSource *bitmapSource, WICRect *rc)
{
    FIXME("(%p,%p,%s): stub\n", iface, bitmapSource, debug_wic_rect(rc));
    return E_NOTIMPL;
}

static HRESULT WINAPI DdsFrameEncode_Commit(IWICBitmapFrameEncode *iface)
{
    FIXME("(%p): stub\n", iface);
    return E_NOTIMPL;
}

static HRESULT WINAPI DdsFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
                                                            IWICMetadataQueryWriter **metadataQueryWriter)
{
    FIXME("(%p,%p): stub\n", iface, metadataQueryWriter);
    return E_NOTIMPL;
}

static const IWICBitmapFrameEncodeVtbl DdsFrameEncode_Vtbl = {
    DdsFrameEncode_QueryInterface,
    DdsFrameEncode_AddRef,
    DdsFrameEncode_Release,
    DdsFrameEncode_Initialize,
    DdsFrameEncode_SetSize,
    DdsFrameEncode_SetResolution,
    DdsFrameEncode_SetPixelFormat,
    DdsFrameEncode_SetColorContexts,
    DdsFrameEncode_SetPalette,
    DdsFrameEncode_SetThumbnail,
    DdsFrameEncode_WritePixels,
    DdsFrameEncode_WriteSource,
    DdsFrameEncode_Commit,
    DdsFrameEncode_GetMetadataQueryWriter
};

1746 1747 1748 1749 1750 1751 1752 1753 1754
HRESULT DdsDecoder_CreateInstance(REFIID iid, void** ppv)
{
    DdsDecoder *This;
    HRESULT ret;

    TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);

    *ppv = NULL;

1755
    This = malloc(sizeof(DdsDecoder));
1756 1757 1758
    if (!This) return E_OUTOFMEMORY;

    This->IWICBitmapDecoder_iface.lpVtbl = &DdsDecoder_Vtbl;
1759
    This->IWICDdsDecoder_iface.lpVtbl = &DdsDecoder_Dds_Vtbl;
1760
    This->IWICWineDecoder_iface.lpVtbl = &DdsDecoder_Wine_Vtbl;
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
    This->ref = 1;
    This->initialized = FALSE;
    This->stream = NULL;
    InitializeCriticalSection(&This->lock);
    This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DdsDecoder.lock");

    ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
    IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);

    return ret;
}
1772

1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
static HRESULT WINAPI DdsEncoder_Dds_QueryInterface(IWICDdsEncoder *iface, REFIID iid,
                                                    void **ppv)
{
    DdsEncoder *This = impl_from_IWICDdsEncoder(iface);
    return IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
}

static ULONG WINAPI DdsEncoder_Dds_AddRef(IWICDdsEncoder *iface)
{
    DdsEncoder *This = impl_from_IWICDdsEncoder(iface);
    return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
}

static ULONG WINAPI DdsEncoder_Dds_Release(IWICDdsEncoder *iface)
{
    DdsEncoder *This = impl_from_IWICDdsEncoder(iface);
    return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
}

static HRESULT WINAPI DdsEncoder_Dds_SetParameters(IWICDdsEncoder *iface,
                                                   WICDdsParameters *parameters)
{
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
    DdsEncoder *This = impl_from_IWICDdsEncoder(iface);
    HRESULT hr;

    TRACE("(%p,%p)\n", iface, parameters);

    if (!parameters) return E_INVALIDARG;

    EnterCriticalSection(&This->lock);

    if (!This->stream)
    {
        hr = WINCODEC_ERR_WRONGSTATE;
        goto end;
    }

    This->info.width      = parameters->Width;
    This->info.height     = parameters->Height;
    This->info.depth      = parameters->Depth;
    This->info.mip_levels = parameters->MipLevels;
    This->info.array_size = parameters->ArraySize;
    This->info.format     = parameters->DxgiFormat;
    This->info.dimension  = parameters->Dimension;
    This->info.alpha_mode = parameters->AlphaMode;

    This->info.bytes_per_block = get_bytes_per_block_from_format(This->info.format);
    This->info.frame_count = get_frame_count(This->info.depth, This->info.mip_levels,
                                             This->info.array_size, This->info.dimension);

    hr = S_OK;

end:
    LeaveCriticalSection(&This->lock);
    return hr;
1828 1829 1830 1831 1832
}

static HRESULT WINAPI DdsEncoder_Dds_GetParameters(IWICDdsEncoder *iface,
                                                   WICDdsParameters *parameters)
{
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
    DdsEncoder *This = impl_from_IWICDdsEncoder(iface);
    HRESULT hr;

    TRACE("(%p,%p)\n", iface, parameters);

    if (!parameters) return E_INVALIDARG;

    EnterCriticalSection(&This->lock);

    if (!This->stream)
    {
        hr = WINCODEC_ERR_WRONGSTATE;
        goto end;
    }

    parameters->Width      = This->info.width;
    parameters->Height     = This->info.height;
    parameters->Depth      = This->info.depth;
    parameters->MipLevels  = This->info.mip_levels;
    parameters->ArraySize  = This->info.array_size;
    parameters->DxgiFormat = This->info.format;
    parameters->Dimension  = This->info.dimension;
    parameters->AlphaMode  = This->info.alpha_mode;

    TRACE("(%p,%p) -> (%dx%d depth=%u mipLevels=%u arraySize=%u dxgiFormat=%#x dimension=%#x alphaMode=%#x)\n",
          iface, parameters, parameters->Width, parameters->Height, parameters->Depth, parameters->MipLevels,
          parameters->ArraySize, parameters->DxgiFormat, parameters->Dimension, parameters->AlphaMode);

    hr = S_OK;

end:
    LeaveCriticalSection(&This->lock);
    return hr;
1866 1867 1868 1869 1870 1871
}

static HRESULT WINAPI DdsEncoder_Dds_CreateNewFrame(IWICDdsEncoder *iface,
                                                    IWICBitmapFrameEncode **frameEncode,
                                                    UINT *arrayIndex, UINT *mipLevel, UINT *sliceIndex)
{
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
    DdsEncoder *This = impl_from_IWICDdsEncoder(iface);
    UINT array_index, mip_level, slice_index;
    DdsFrameEncode *result;
    HRESULT hr;

    TRACE("(%p,%p,%p,%p,%p)\n", iface, frameEncode, arrayIndex, mipLevel, sliceIndex);

    EnterCriticalSection(&This->lock);

    if (!This->stream || This->committed || This->uncommitted_frame)
    {
        hr = WINCODEC_ERR_WRONGSTATE;
        goto end;
    }

1887
    result = malloc(sizeof(*result));
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
    if (!result)
    {
        hr = E_OUTOFMEMORY;
        goto end;
    }

    get_frame_dds_index(This->frame_index, &This->info, &array_index, &mip_level, &slice_index);
    if (arrayIndex) *arrayIndex = array_index;
    if (mipLevel)   *mipLevel   = mip_level;
    if (sliceIndex) *sliceIndex = slice_index;

    This->frame_index++;
    result->IWICBitmapFrameEncode_iface.lpVtbl = &DdsFrameEncode_Vtbl;
    result->ref = 1;
    result->parent = This;
    result->parent->uncommitted_frame = TRUE;
    result->initialized = FALSE;
    result->frame_created = FALSE;
    IWICDdsEncoder_AddRef(iface);

    *frameEncode = &result->IWICBitmapFrameEncode_iface;
    hr = S_OK;

end:
    LeaveCriticalSection(&This->lock);
    return hr;
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
}

static const IWICDdsEncoderVtbl DdsEncoder_Dds_Vtbl =
{
    DdsEncoder_Dds_QueryInterface,
    DdsEncoder_Dds_AddRef,
    DdsEncoder_Dds_Release,
    DdsEncoder_Dds_SetParameters,
    DdsEncoder_Dds_GetParameters,
    DdsEncoder_Dds_CreateNewFrame
};

1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
static HRESULT WINAPI DdsEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
                                                   void **ppv)
{
    DdsEncoder *This = impl_from_IWICBitmapEncoder(iface);
    FIXME("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);

    if (!ppv) return E_INVALIDARG;

    if (IsEqualIID(&IID_IUnknown, iid) ||
        IsEqualIID(&IID_IWICBitmapEncoder, iid)) {
        *ppv = &This->IWICBitmapEncoder_iface;
1937 1938 1939
    } else if (IsEqualIID(&IID_IWICDdsEncoder, iid)) {
        *ppv = &This->IWICDdsEncoder_iface;
    } else {
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI DdsEncoder_AddRef(IWICBitmapEncoder *iface)
{
    DdsEncoder *This = impl_from_IWICBitmapEncoder(iface);
    ULONG ref = InterlockedIncrement(&This->ref);

1953
    TRACE("(%p) refcount=%lu\n", iface, ref);
1954 1955 1956 1957 1958 1959 1960 1961 1962

    return ref;
}

static ULONG WINAPI DdsEncoder_Release(IWICBitmapEncoder *iface)
{
    DdsEncoder *This = impl_from_IWICBitmapEncoder(iface);
    ULONG ref = InterlockedDecrement(&This->ref);

1963
    TRACE("(%p) refcount=%lu\n", iface, ref);
1964 1965 1966 1967 1968

    if (ref == 0) {
        This->lock.DebugInfo->Spare[0] = 0;
        DeleteCriticalSection(&This->lock);
        if (This->stream) IStream_Release(This->stream);
1969
        free(This);
1970 1971 1972 1973 1974 1975
    }

    return ref;
}

static HRESULT WINAPI DdsEncoder_Initialize(IWICBitmapEncoder *iface,
1976
                                            IStream *stream, WICBitmapEncoderCacheOption cacheOption)
1977
{
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
    DdsEncoder *This = impl_from_IWICBitmapEncoder(iface);
    HRESULT hr;

    TRACE("(%p,%p,%u)\n", iface, stream, cacheOption);

    if (cacheOption != WICBitmapEncoderNoCache)
        FIXME("Cache option %#x is not supported.\n", cacheOption);

    if (!stream) return E_INVALIDARG;

    EnterCriticalSection(&This->lock);

    if (This->stream)
    {
        hr = WINCODEC_ERR_WRONGSTATE;
        goto end;
    }

    This->stream = stream;
    IStream_AddRef(stream);

1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
    This->info.width = 1;
    This->info.height = 1;
    This->info.depth = 1;
    This->info.mip_levels = 1;
    This->info.array_size = 1;
    This->info.frame_count = 1;
    This->info.data_offset = 0;
    This->info.bytes_per_block = get_bytes_per_block_from_format(DXGI_FORMAT_BC3_UNORM);
    This->info.format = DXGI_FORMAT_BC3_UNORM;
    This->info.dimension = WICDdsTexture2D;
    This->info.alpha_mode = WICDdsAlphaModeUnknown;
    This->info.pixel_format = &GUID_WICPixelFormatUndefined;
    This->info.pixel_format_bpp = 0;

2013 2014 2015 2016 2017 2018
    hr = S_OK;

end:
    LeaveCriticalSection(&This->lock);

    return hr;
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
}

static HRESULT WINAPI DdsEncoder_GetContainerFormat(IWICBitmapEncoder *iface, GUID *format)
{
    TRACE("(%p,%p)\n", iface, format);

    if (!format)
        return E_INVALIDARG;

    memcpy(format, &GUID_ContainerFormatDds, sizeof(*format));
    return S_OK;
}

static HRESULT WINAPI DdsEncoder_GetEncoderInfo(IWICBitmapEncoder *iface, IWICBitmapEncoderInfo **info)
{
    IWICComponentInfo *comp_info;
    HRESULT hr;

    TRACE("%p,%p\n", iface, info);

    if (!info) return E_INVALIDARG;

    hr = CreateComponentInfo(&CLSID_WICDdsEncoder, &comp_info);
    if (hr == S_OK) {
        hr = IWICComponentInfo_QueryInterface(comp_info, &IID_IWICBitmapEncoderInfo, (void **)info);
        IWICComponentInfo_Release(comp_info);
    }
    return hr;
}

static HRESULT WINAPI DdsEncoder_SetColorContexts(IWICBitmapEncoder *iface,
                                                     UINT cCount, IWICColorContext **ppIColorContext)
{
    FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
    return E_NOTIMPL;
}

static HRESULT WINAPI DdsEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *palette)
{
    DdsEncoder *This = impl_from_IWICBitmapEncoder(iface);
    HRESULT hr;

    TRACE("(%p,%p)\n", iface, palette);

    EnterCriticalSection(&This->lock);

    hr = This->stream ? WINCODEC_ERR_UNSUPPORTEDOPERATION : WINCODEC_ERR_NOTINITIALIZED;

    LeaveCriticalSection(&This->lock);

    return hr;
}

static HRESULT WINAPI DdsEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
{
    TRACE("(%p,%p)\n", iface, pIThumbnail);
    return WINCODEC_ERR_UNSUPPORTEDOPERATION;
}

static HRESULT WINAPI DdsEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
{
    TRACE("(%p,%p)\n", iface, pIPreview);
    return WINCODEC_ERR_UNSUPPORTEDOPERATION;
}

static HRESULT WINAPI DdsEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
2085
                                                IWICBitmapFrameEncode **frameEncode, IPropertyBag2 **encoderOptions)
2086
{
2087 2088 2089 2090
    DdsEncoder *This = impl_from_IWICBitmapEncoder(iface);

    TRACE("(%p,%p,%p)\n", iface, frameEncode, encoderOptions);

2091
    return IWICDdsEncoder_CreateNewFrame(&This->IWICDdsEncoder_iface, frameEncode, NULL, NULL, NULL);
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
}

static HRESULT WINAPI DdsEncoder_Commit(IWICBitmapEncoder *iface)
{
    FIXME("(%p): stub\n", iface);
    return E_NOTIMPL;
}

static HRESULT WINAPI DdsEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
                                                           IWICMetadataQueryWriter **ppIMetadataQueryWriter)
{
    FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
    return E_NOTIMPL;
}

static const IWICBitmapEncoderVtbl DdsEncoder_Vtbl = {
    DdsEncoder_QueryInterface,
    DdsEncoder_AddRef,
    DdsEncoder_Release,
    DdsEncoder_Initialize,
    DdsEncoder_GetContainerFormat,
    DdsEncoder_GetEncoderInfo,
    DdsEncoder_SetColorContexts,
    DdsEncoder_SetPalette,
    DdsEncoder_SetThumbnail,
    DdsEncoder_SetPreview,
    DdsEncoder_CreateNewFrame,
    DdsEncoder_Commit,
    DdsEncoder_GetMetadataQueryWriter
};

HRESULT DdsEncoder_CreateInstance( REFIID iid, void **ppv)
{
    DdsEncoder *This;
    HRESULT ret;

    TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);

    *ppv = NULL;

2132
    This = malloc(sizeof(DdsEncoder));
2133 2134 2135
    if (!This) return E_OUTOFMEMORY;

    This->IWICBitmapEncoder_iface.lpVtbl = &DdsEncoder_Vtbl;
2136
    This->IWICDdsEncoder_iface.lpVtbl = &DdsEncoder_Dds_Vtbl;
2137 2138 2139
    This->ref = 1;
    This->stream = NULL;
    This->frame_count = 0;
2140
    This->frame_index = 0;
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
    This->uncommitted_frame = FALSE;
    This->committed = FALSE;
    InitializeCriticalSection(&This->lock);
    This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DdsEncoder.lock");

    ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
    IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);

    return ret;
}