1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #include "ddsHandler.h" |
22 | #include "common/Exception.h" |
23 | #include "image/ImageData.h" |
24 | |
25 | // dds parser |
26 | #include "ddsparse/ddsparse.h" |
27 | |
28 | namespace love |
29 | { |
30 | namespace image |
31 | { |
32 | namespace magpie |
33 | { |
34 | |
35 | static PixelFormat convertFormat(dds::dxinfo::DXGIFormat dxformat, bool &sRGB, bool &bgra) |
36 | { |
37 | using namespace dds::dxinfo; |
38 | |
39 | sRGB = false; |
40 | bgra = false; |
41 | |
42 | switch (dxformat) |
43 | { |
44 | case DXGI_FORMAT_R32G32B32A32_TYPELESS: |
45 | case DXGI_FORMAT_R32G32B32A32_FLOAT: |
46 | return PIXELFORMAT_RGBA32F; |
47 | |
48 | case DXGI_FORMAT_R16G16B16A16_TYPELESS: |
49 | case DXGI_FORMAT_R16G16B16A16_FLOAT: |
50 | return PIXELFORMAT_RGBA16F; |
51 | |
52 | case DXGI_FORMAT_R16G16B16A16_UNORM: |
53 | return PIXELFORMAT_RGBA16; |
54 | |
55 | case DXGI_FORMAT_R32G32_TYPELESS: |
56 | case DXGI_FORMAT_R32G32_FLOAT: |
57 | return PIXELFORMAT_RG32F; |
58 | |
59 | case DXGI_FORMAT_R10G10B10A2_TYPELESS: |
60 | case DXGI_FORMAT_R10G10B10A2_UNORM: |
61 | return PIXELFORMAT_RGB10A2; |
62 | |
63 | case DXGI_FORMAT_R11G11B10_FLOAT: |
64 | return PIXELFORMAT_RG11B10F; |
65 | |
66 | case DXGI_FORMAT_R8G8B8A8_TYPELESS: |
67 | case DXGI_FORMAT_R8G8B8A8_UNORM: |
68 | case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: |
69 | sRGB = (dxformat == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); |
70 | return PIXELFORMAT_RGBA8; |
71 | |
72 | case DXGI_FORMAT_R16G16_TYPELESS: |
73 | case DXGI_FORMAT_R16G16_FLOAT: |
74 | return PIXELFORMAT_RG16F; |
75 | |
76 | case DXGI_FORMAT_R16G16_UNORM: |
77 | return PIXELFORMAT_RG16; |
78 | |
79 | case DXGI_FORMAT_R32_TYPELESS: |
80 | case DXGI_FORMAT_R32_FLOAT: |
81 | return PIXELFORMAT_R32F; |
82 | |
83 | case DXGI_FORMAT_R8G8_TYPELESS: |
84 | case DXGI_FORMAT_R8G8_UNORM: |
85 | return PIXELFORMAT_RG8; |
86 | |
87 | case DXGI_FORMAT_R16_TYPELESS: |
88 | case DXGI_FORMAT_R16_FLOAT: |
89 | return PIXELFORMAT_R16F; |
90 | |
91 | case DXGI_FORMAT_R16_UNORM: |
92 | return PIXELFORMAT_R16; |
93 | |
94 | case DXGI_FORMAT_R8_TYPELESS: |
95 | case DXGI_FORMAT_R8_UNORM: |
96 | case DXGI_FORMAT_A8_UNORM: |
97 | return PIXELFORMAT_R8; |
98 | |
99 | case DXGI_FORMAT_BC1_TYPELESS: |
100 | case DXGI_FORMAT_BC1_UNORM: |
101 | case DXGI_FORMAT_BC1_UNORM_SRGB: |
102 | sRGB = (dxformat == DXGI_FORMAT_BC1_UNORM_SRGB); |
103 | return PIXELFORMAT_DXT1; |
104 | |
105 | case DXGI_FORMAT_BC2_TYPELESS: |
106 | case DXGI_FORMAT_BC2_UNORM: |
107 | case DXGI_FORMAT_BC2_UNORM_SRGB: |
108 | sRGB = (dxformat == DXGI_FORMAT_BC2_UNORM_SRGB); |
109 | return PIXELFORMAT_DXT3; |
110 | |
111 | case DXGI_FORMAT_BC3_TYPELESS: |
112 | case DXGI_FORMAT_BC3_UNORM: |
113 | case DXGI_FORMAT_BC3_UNORM_SRGB: |
114 | sRGB = (dxformat == DXGI_FORMAT_BC3_UNORM_SRGB); |
115 | return PIXELFORMAT_DXT5; |
116 | |
117 | case DXGI_FORMAT_BC4_TYPELESS: |
118 | case DXGI_FORMAT_BC4_UNORM: |
119 | return PIXELFORMAT_BC4; |
120 | |
121 | case DXGI_FORMAT_BC4_SNORM: |
122 | return PIXELFORMAT_BC4s; |
123 | |
124 | case DXGI_FORMAT_BC5_TYPELESS: |
125 | case DXGI_FORMAT_BC5_UNORM: |
126 | return PIXELFORMAT_BC5; |
127 | |
128 | case DXGI_FORMAT_BC5_SNORM: |
129 | return PIXELFORMAT_BC5s; |
130 | |
131 | case DXGI_FORMAT_B5G6R5_UNORM: |
132 | return PIXELFORMAT_RGB565; |
133 | |
134 | case DXGI_FORMAT_B5G5R5A1_UNORM: |
135 | return PIXELFORMAT_RGB5A1; |
136 | |
137 | case DXGI_FORMAT_B8G8R8A8_UNORM: |
138 | case DXGI_FORMAT_B8G8R8A8_TYPELESS: |
139 | case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: |
140 | sRGB = (dxformat == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB); |
141 | bgra = true; |
142 | return PIXELFORMAT_RGBA8; |
143 | |
144 | case DXGI_FORMAT_BC6H_TYPELESS: |
145 | case DXGI_FORMAT_BC6H_UF16: |
146 | return PIXELFORMAT_BC6H; |
147 | |
148 | case DXGI_FORMAT_BC6H_SF16: |
149 | return PIXELFORMAT_BC6Hs; |
150 | |
151 | case DXGI_FORMAT_BC7_TYPELESS: |
152 | case DXGI_FORMAT_BC7_UNORM: |
153 | case DXGI_FORMAT_BC7_UNORM_SRGB: |
154 | sRGB = (dxformat == DXGI_FORMAT_BC7_UNORM_SRGB); |
155 | return PIXELFORMAT_BC7; |
156 | |
157 | default: |
158 | return PIXELFORMAT_UNKNOWN; |
159 | } |
160 | } |
161 | |
162 | bool DDSHandler::canDecode(Data *data) |
163 | { |
164 | using namespace dds::dxinfo; |
165 | |
166 | DXGIFormat dxformat = dds::getDDSPixelFormat(data->getData(), data->getSize()); |
167 | bool isSRGB = false; |
168 | bool bgra = false; |
169 | PixelFormat format = convertFormat(dxformat, isSRGB, bgra); |
170 | |
171 | return ImageData::validPixelFormat(format); |
172 | } |
173 | |
174 | FormatHandler::DecodedImage DDSHandler::decode(Data *data) |
175 | { |
176 | DecodedImage img; |
177 | |
178 | dds::Parser parser(data->getData(), data->getSize()); |
179 | |
180 | bool isSRGB = false; |
181 | bool bgra = false; |
182 | img.format = convertFormat(parser.getFormat(), isSRGB, bgra); |
183 | |
184 | if (!ImageData::validPixelFormat(img.format)) |
185 | throw love::Exception("Could not parse DDS pixel data: Unsupported format." ); |
186 | |
187 | if (parser.getMipmapCount() == 0) |
188 | throw love::Exception("Could not parse DDS pixel data: No readable texture data." ); |
189 | |
190 | // We only support the top mip level through this codepath. |
191 | const dds::Image *ddsimg = parser.getImageData(0); |
192 | |
193 | try |
194 | { |
195 | img.data = new uint8[ddsimg->dataSize]; |
196 | } |
197 | catch (std::exception &) |
198 | { |
199 | throw love::Exception("Out of memory." ); |
200 | } |
201 | |
202 | memcpy(img.data, ddsimg->data, ddsimg->dataSize); |
203 | |
204 | img.size = ddsimg->dataSize; |
205 | img.width = ddsimg->width; |
206 | img.height = ddsimg->height; |
207 | |
208 | // Swap red and blue channels for incoming BGRA data. |
209 | if (bgra) |
210 | { |
211 | for (int y = 0; y < img.height; y++) |
212 | { |
213 | for (int x = 0; x < img.width; x++) |
214 | { |
215 | size_t offset = (y * img.width + x) * 4; |
216 | uint8 b = img.data[offset + 0]; |
217 | uint8 r = img.data[offset + 2]; |
218 | img.data[offset + 0] = r; |
219 | img.data[offset + 2] = b; |
220 | } |
221 | } |
222 | } |
223 | |
224 | return img; |
225 | } |
226 | |
227 | bool DDSHandler::canParseCompressed(Data *data) |
228 | { |
229 | return dds::isCompressedDDS(data->getData(), data->getSize()); |
230 | } |
231 | |
232 | StrongRef<CompressedMemory> DDSHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB) |
233 | { |
234 | if (!dds::isCompressedDDS(filedata->getData(), filedata->getSize())) |
235 | throw love::Exception("Could not decode compressed data (not a DDS file?)" ); |
236 | |
237 | PixelFormat texformat = PIXELFORMAT_UNKNOWN; |
238 | bool isSRGB = false; |
239 | bool bgra = false; |
240 | |
241 | StrongRef<CompressedMemory> memory; |
242 | size_t dataSize = 0; |
243 | |
244 | images.clear(); |
245 | |
246 | // Attempt to parse the dds file. |
247 | dds::Parser parser(filedata->getData(), filedata->getSize()); |
248 | |
249 | texformat = convertFormat(parser.getFormat(), isSRGB, bgra); |
250 | |
251 | if (texformat == PIXELFORMAT_UNKNOWN) |
252 | throw love::Exception("Could not parse compressed data: Unsupported format." ); |
253 | |
254 | if (parser.getMipmapCount() == 0) |
255 | throw love::Exception("Could not parse compressed data: No readable texture data." ); |
256 | |
257 | // Calculate the size of the block of memory we're returning. |
258 | for (size_t i = 0; i < parser.getMipmapCount(); i++) |
259 | { |
260 | const dds::Image *img = parser.getImageData(i); |
261 | dataSize += img->dataSize; |
262 | } |
263 | |
264 | memory.set(new CompressedMemory(dataSize), Acquire::NORETAIN); |
265 | |
266 | size_t dataOffset = 0; |
267 | |
268 | // Copy the parsed mipmap levels from the FileData to our CompressedImageData. |
269 | for (size_t i = 0; i < parser.getMipmapCount(); i++) |
270 | { |
271 | // Fetch the data for this mipmap level. |
272 | const dds::Image *img = parser.getImageData(i); |
273 | |
274 | // Copy the mipmap image from the FileData to our block of memory. |
275 | memcpy(memory->data + dataOffset, img->data, img->dataSize); |
276 | |
277 | auto slice = new CompressedSlice(texformat, img->width, img->height, memory, dataOffset, img->dataSize); |
278 | images.emplace_back(slice, Acquire::NORETAIN); |
279 | |
280 | dataOffset += img->dataSize; |
281 | } |
282 | |
283 | format = texformat; |
284 | sRGB = isSRGB; |
285 | return memory; |
286 | } |
287 | |
288 | } // magpie |
289 | } // image |
290 | } // love |
291 | |