forked from 0xC0000054/pdn-ddsfiletype-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DdsFile.cs
367 lines (315 loc) · 13.5 KB
/
DdsFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
////////////////////////////////////////////////////////////////////////
//
// This file is part of pdn-ddsfiletype-plus, a DDS FileType plugin
// for Paint.NET that adds support for the DX10 and later formats.
//
// Copyright (c) 2017-2019 Nicholas Hayes
//
// This file is licensed under the MIT License.
// See LICENSE.txt for complete licensing and attribution information.
//
////////////////////////////////////////////////////////////////////////
using PaintDotNet;
using PaintDotNet.AppModel;
using System;
using System.Drawing;
using System.IO;
namespace DdsFileTypePlus
{
internal static class DdsFile
{
public static unsafe Document Load(Stream input)
{
Document doc = null;
using (DdsNative.DdsImage image = DdsNative.Load(input))
{
doc = new Document(image.Width, image.Height);
BitmapLayer layer = Layer.CreateBackgroundLayer(image.Width, image.Height);
Surface surface = layer.Surface;
for (int y = 0; y < surface.Height; ++y)
{
byte* src = image.GetRowAddressUnchecked(y);
ColorBgra* dst = surface.GetRowAddressUnchecked(y);
for (int x = 0; x < surface.Width; ++x)
{
dst->R = src[0];
dst->G = src[1];
dst->B = src[2];
dst->A = src[3];
src += 4;
++dst;
}
}
doc.Layers.Add(layer);
}
return doc;
}
public static void Save(
IServiceProvider services,
Document input,
Stream output,
DdsFileFormat format,
DdsErrorMetric errorMetric,
BC7CompressionMode compressionMode,
bool cubeMap,
bool generateMipmaps,
ResamplingAlgorithm sampling,
Surface scratchSurface,
ProgressEventHandler progressCallback)
{
using (RenderArgs args = new RenderArgs(scratchSurface))
{
input.Render(args, true);
}
DdsNative.DdsProgressCallback ddsProgress = null;
if (progressCallback != null)
{
ddsProgress = (UIntPtr done, UIntPtr total) =>
{
double progress = (double)done.ToUInt64() / (double)total.ToUInt64();
try
{
progressCallback(null, new ProgressEventArgs(progress * 100.0, true));
return true;
}
catch (OperationCanceledException)
{
return false;
}
};
}
int width = scratchSurface.Width;
int height = scratchSurface.Height;
int arraySize = 1;
Size? cubeMapFaceSize = null;
if (cubeMap && IsCrossedCubeMapSize(scratchSurface))
{
if (width > height)
{
width /= 4;
height /= 3;
}
else
{
width /= 3;
height /= 4;
}
arraySize = 6;
cubeMapFaceSize = new Size(width, height);
}
int mipLevels = generateMipmaps ? GetMipCount(width, height) : 1;
bool enableHardwareAcceleration = (bool)services.GetService<ISettingsService>().GetSetting(AppSettingPaths.UI.EnableHardwareAcceleration).Value;
DdsNative.DDSSaveInfo info = new DdsNative.DDSSaveInfo
{
width = width,
height = height,
arraySize = arraySize,
mipLevels = mipLevels,
format = format,
errorMetric = errorMetric,
compressionMode = compressionMode,
cubeMap = cubeMapFaceSize.HasValue,
enableHardwareAcceleration = enableHardwareAcceleration
};
using (TextureCollection textures = GetTextures(scratchSurface, cubeMapFaceSize, mipLevels, sampling))
{
DdsNative.Save(info, textures, output, ddsProgress);
}
}
private static bool IsCrossedCubeMapSize(Surface surface)
{
// A crossed image cube map must have a 4:3 aspect ratio for horizontal cube maps
// or a 3:4 aspect ratio for vertical cube maps, with the cube map images being square.
//
// For example, a horizontal crossed image with 256 x 256 pixel cube maps
// would have a width of 1024 and a height of 768.
if (surface.Width > surface.Height)
{
return (surface.Width / 4) == (surface.Height / 3);
}
else if (surface.Height > surface.Width)
{
return (surface.Width / 3) == (surface.Height / 4);
}
return false;
}
private static int GetMipCount(int width, int height)
{
int mipCount = 1;
while (width > 1 || height > 1)
{
++mipCount;
if (width > 1)
{
width /= 2;
}
if (height > 1)
{
height /= 2;
}
}
return mipCount;
}
private static TextureCollection GetTextures(Surface scratchSurface, Size? cubeMapFaceSize, int mipLevels, ResamplingAlgorithm algorithm)
{
TextureCollection textures = null;
TextureCollection tempTextures = null;
try
{
tempTextures = new TextureCollection(mipLevels);
if (cubeMapFaceSize.HasValue)
{
// DirectX 10+ requires DDS cube maps to have all 6 faces.
tempTextures.Capacity *= 6;
Size faceSize = cubeMapFaceSize.Value;
Point[] cubeMapOffsets = new Point[6];
// Split the crossed image into the individual cube map faces.
//
// The crossed image uses the same layout as the Intel® Texture Works DDS plug-in for Adobe Photoshop®
// (https://github.com/GameTechDev/Intel-Texture-Works-Plugin)
//
// The DirectXTex texassemble utility and Unity® both use different layouts, so there does not appear
// to be any common standard for a crossed image.
//
// The cube map faces in a DDS file are always ordered: +X, -X, +Y, -Y, +Z, -Z.
if (scratchSurface.Width > scratchSurface.Height)
{
// A horizontal crossed image uses the following layout:
//
// [ +Y ]
// [ -X ][ +Z ][ +X ][ -Z ]
// [ -Y ]
//
cubeMapOffsets[0] = new Point(faceSize.Width * 2, faceSize.Height); // +X
cubeMapOffsets[1] = new Point(0, faceSize.Height); // -X
cubeMapOffsets[2] = new Point(faceSize.Width, 0); // +Y
cubeMapOffsets[3] = new Point(faceSize.Width, faceSize.Height * 2); // -Y
cubeMapOffsets[4] = new Point(faceSize.Width, faceSize.Height); // +Z
cubeMapOffsets[5] = new Point(faceSize.Width * 3, faceSize.Height); // -Z
}
else
{
// A vertical crossed image uses the following layout:
//
// [ +Y ]
// [ -X ][ +Z ][ +X ]
// [ -Y ]
// [ -Z ]
//
cubeMapOffsets[0] = new Point(faceSize.Width * 2, faceSize.Height); // +X
cubeMapOffsets[1] = new Point(0, faceSize.Height); // -X
cubeMapOffsets[2] = new Point(faceSize.Width, 0); // +Y
cubeMapOffsets[3] = new Point(faceSize.Width, faceSize.Height * 2); // -Y
cubeMapOffsets[4] = new Point(faceSize.Width, faceSize.Height); // +Z
cubeMapOffsets[5] = new Point(faceSize.Width, faceSize.Height * 3); // -Z
}
for (int i = 0; i < 6; ++i)
{
Point srcStartOffset = cubeMapOffsets[i];
tempTextures.Add(new Texture(scratchSurface.CreateWindow(srcStartOffset.X, srcStartOffset.Y, faceSize.Width, faceSize.Height), true));
if (mipLevels > 1)
{
Surface cubeMapSurface = tempTextures[tempTextures.Count - 1].Surface;
for (int j = 1; j < mipLevels; ++j)
{
int mipWidth = Math.Max(1, cubeMapSurface.Width >> j);
int mipHeight = Math.Max(1, cubeMapSurface.Height >> j);
tempTextures.Add(CreateMipTexture(cubeMapSurface, mipWidth, mipHeight, algorithm));
}
}
}
}
else
{
tempTextures.Add(new Texture(scratchSurface, false));
if (mipLevels > 1)
{
for (int j = 1; j < mipLevels; ++j)
{
int mipWidth = Math.Max(1, scratchSurface.Width >> j);
int mipHeight = Math.Max(1, scratchSurface.Height >> j);
tempTextures.Add(CreateMipTexture(scratchSurface, mipWidth, mipHeight, algorithm));
}
}
}
textures = tempTextures;
tempTextures = null;
}
finally
{
if (tempTextures != null)
{
tempTextures.Dispose();
}
}
return textures;
}
private static unsafe Texture CreateMipTexture(Surface fullSize, int mipWidth, int mipHeight, ResamplingAlgorithm algorithm)
{
Texture mipTexture = null;
Surface mipSurface = null;
try
{
mipSurface = new Surface(mipWidth, mipHeight);
mipSurface.FitSurface(algorithm, fullSize);
if (HasTransparency(fullSize))
{
// Downscaling images with transparency is done in a way that allows the completely transparent areas
// to retain their RGB color values, this behavior is required by some programs that use DDS files.
using (Surface color = new Surface(mipWidth, mipHeight))
{
using (Surface opaqueClone = fullSize.Clone())
{
// Set the alpha channel to fully opaque to prevent Windows Imaging Component
// from discarding the color information of completely transparent pixels.
new UnaryPixelOps.SetAlphaChannelTo255().Apply(opaqueClone, opaqueClone.Bounds);
color.FitSurface(algorithm, opaqueClone);
}
for (int y = 0; y < mipHeight; ++y)
{
ColorBgra* colorPtr = color.GetRowAddressUnchecked(y);
ColorBgra* destPtr = mipSurface.GetRowAddressUnchecked(y);
for (int x = 0; x < mipWidth; ++x)
{
// Copy the color data from the opaque image to create a merged
// image with the transparent pixels retaining their original values.
destPtr->B = colorPtr->B;
destPtr->G = colorPtr->G;
destPtr->R = colorPtr->R;
++colorPtr;
++destPtr;
}
}
}
}
mipTexture = new Texture(mipSurface, true);
mipSurface = null;
}
finally
{
if (mipSurface != null)
{
mipSurface.Dispose();
}
}
return mipTexture;
}
private static unsafe bool HasTransparency(Surface surface)
{
for (int y = 0; y < surface.Height; ++y)
{
ColorBgra* ptr = surface.GetRowAddressUnchecked(y);
ColorBgra* ptrEnd = ptr + surface.Width;
while (ptr < ptrEnd)
{
if (ptr->A < 255)
{
return true;
}
++ptr;
}
}
return false;
}
}
}