1/**
2 * Simple DDS data parser for compressed 2D textures.
3 *
4 * Copyright (c) 2013-2023 Sasha Szpakowski
5 *
6 * This software is provided 'as-is', without any express or implied
7 * warranty. In no event will the authors be held liable for any damages
8 * arising from the use of this software.
9 *
10 * Permission is granted to anyone to use this software for any purpose,
11 * including commercial applications, and to alter it and redistribute it
12 * freely, subject to the following restrictions:
13 *
14 * 1. The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software
16 * in a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 * 3. This notice may not be removed or altered from any source distribution.
21 **/
22
23#ifndef DDS_PARSE_H
24#define DDS_PARSE_H
25
26#include <stddef.h>
27#include <stdint.h>
28
29#include <vector>
30
31#include "ddsinfo.h"
32
33namespace dds
34{
35
36// Represents a single mipmap level of a texture.
37struct Image
38{
39 int width = 0;
40 int height = 0;
41 size_t dataSize = 0;
42 const uint8_t *data = nullptr;
43};
44
45/**
46 * Determines whether the input byte data represents a valid DDS file.
47 * Does not take into account whether the texture format is supported.
48 *
49 * @param data The byte data to parse.
50 * @param dataSize The size in bytes of the data.
51 **/
52bool isDDS(const void *data, size_t dataSize);
53
54/**
55 * Determines whether the input byte data represents a valid compressed DDS
56 * file. Takes into account texture format, but not type (3D textures, etc.)
57 *
58 * @param data The byte data to parse.
59 * @param dataSize The size in bytes of the data.
60 **/
61bool isCompressedDDS(const void *data, size_t dataSize);
62
63dxinfo::DXGIFormat getDDSPixelFormat(const void *data, size_t dataSize);
64
65class Parser
66{
67public:
68
69 /**
70 * Constructor.
71 * Attempts to parse byte data as a compressed DDS file.
72 *
73 * @param data The byte data to parse.
74 * @param dataSize The size in bytes of the data.
75 **/
76 Parser(const void *data, size_t dataSize);
77 Parser(const Parser &other);
78 Parser();
79
80 Parser &operator = (const Parser &other);
81
82 ~Parser();
83
84 /**
85 * Gets the format of this texture.
86 **/
87 dxinfo::DXGIFormat getFormat() const;
88
89 /**
90 * Gets the data of this texture at a mipmap level. Mipmap level 0
91 * represents the base image.
92 *
93 * @param miplevel The mipmap level to get the data of.
94 * @return Pointer to the image data, or NULL if miplevel is not within the
95 * range of [0, numMipmaps).
96 **/
97 const Image *getImageData(size_t miplevel = 0) const;
98
99 /**
100 * Gets the number of mipmap levels in this texture.
101 * Includes the base mip level.
102 **/
103 size_t getMipmapCount() const;
104
105private:
106
107 size_t parseImageSize(dxinfo::DXGIFormat fmt, int width, int height) const;
108 bool parseTexData(const uint8_t *data, size_t dataSize, dxinfo::DXGIFormat fmt, int w, int h, int nb_mips);
109 bool parseData(const void *data, size_t dataSize);
110
111 std::vector<Image> texData;
112 dxinfo::DXGIFormat format;
113
114}; // Parser
115
116} // dds
117
118#endif // DDS_PARSE_H
119