Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Commit f284b74

Browse files
committed
Fixed alpha settings during save with original data.
Nuget release.
1 parent c7e2fda commit f284b74

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

CSharpImageLibrary/CSharpImageLibrary.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<projectUrl>https://github.com/KFreon/CSharpImageLibrary</projectUrl>
1010
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1111
<description>Loads and saves standard images including DDS (all sorts and weird ones like V8U8, G8, etc. See full list on github.)</description>
12-
<releaseNotes>Support for large component DDS' enabled.</releaseNotes>
12+
<releaseNotes>Minor fix for using original data during save not working as intended.</releaseNotes>
1313
<copyright>Copyright KFreon 2015</copyright>
1414
<tags>Image DDS Texture</tags>
1515
</metadata>

CSharpImageLibrary/ImageEngineImage.cs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ byte[] AttemptSaveUsingOriginalData(ImageFormats.ImageEngineFormatDetails destFo
265265
int newHeight = Height;
266266
DDS_Header tempHeader = null;
267267
byte[] data = null;
268+
byte[] tempOriginalData = OriginalData;
268269

269270
if (destFormatDetails.IsDDS)
270271
{
@@ -299,6 +300,86 @@ byte[] AttemptSaveUsingOriginalData(ImageFormats.ImageEngineFormatDetails destFo
299300
}
300301
else
301302
{
303+
if (alphaSetting == AlphaSettings.RemoveAlphaChannel)
304+
{
305+
// Can't edit alpha directly in premultiplied formats. Not easily anyway.
306+
if (destFormatDetails.IsPremultipliedFormat)
307+
return ImageEngine.Save(MipMaps, destFormatDetails, GenerateMips, alphaSetting, desiredMaxDimension, mipToSave);
308+
309+
310+
// DDS Formats only
311+
switch (destFormatDetails.Format)
312+
{
313+
// Excluded cos they have no true alpha
314+
case ImageEngineFormat.DDS_A8:
315+
case ImageEngineFormat.DDS_A8L8:
316+
case ImageEngineFormat.DDS_ATI1:
317+
case ImageEngineFormat.DDS_ATI2_3Dc:
318+
case ImageEngineFormat.DDS_V8U8:
319+
case ImageEngineFormat.DDS_G16_R16:
320+
case ImageEngineFormat.DDS_G8_L8:
321+
case ImageEngineFormat.DDS_R5G6B5:
322+
case ImageEngineFormat.DDS_RGB_8:
323+
case ImageEngineFormat.DDS_DXT1:
324+
break;
325+
326+
// Exluded cos they're alpha isn't easily edited
327+
case ImageEngineFormat.DDS_DXT2:
328+
case ImageEngineFormat.DDS_DXT4:
329+
break;
330+
331+
// Excluded cos they're currently unsupported
332+
case ImageEngineFormat.DDS_CUSTOM:
333+
case ImageEngineFormat.DDS_DX10:
334+
case ImageEngineFormat.DDS_ARGB_4:
335+
break;
336+
337+
case ImageEngineFormat.DDS_ABGR_8:
338+
case ImageEngineFormat.DDS_ARGB_32F:
339+
case ImageEngineFormat.DDS_ARGB_8:
340+
case ImageEngineFormat.DDS_DXT3:
341+
case ImageEngineFormat.DDS_DXT5:
342+
tempOriginalData = new byte[OriginalData.Length];
343+
Array.Copy(OriginalData, tempOriginalData, OriginalData.Length);
344+
345+
// Edit alpha values
346+
int alphaStart = 128;
347+
int alphaJump = 0;
348+
byte[] alphaBlock = null;
349+
if (destFormatDetails.IsBlockCompressed)
350+
{
351+
alphaJump = 16;
352+
alphaBlock = new byte[8];
353+
for (int i = 0; i < 8; i++)
354+
alphaBlock[i] = 255;
355+
}
356+
else
357+
{
358+
alphaJump = destFormatDetails.ComponentSize * 4;
359+
alphaBlock = new byte[destFormatDetails.ComponentSize];
360+
361+
switch (destFormatDetails.ComponentSize)
362+
{
363+
case 1:
364+
alphaBlock[0] = 255;
365+
break;
366+
case 2:
367+
alphaBlock = BitConverter.GetBytes(ushort.MaxValue);
368+
break;
369+
case 4:
370+
alphaBlock = BitConverter.GetBytes(1f);
371+
break;
372+
}
373+
}
374+
375+
for (int i = alphaStart; i < OriginalData.Length; i += alphaJump)
376+
Array.Copy(alphaBlock, 0, tempOriginalData, i, alphaBlock.Length);
377+
378+
break;
379+
}
380+
}
381+
382+
302383
switch (GenerateMips)
303384
{
304385
case MipHandling.KeepExisting:
@@ -344,7 +425,7 @@ byte[] AttemptSaveUsingOriginalData(ImageFormats.ImageEngineFormatDetails destFo
344425

345426
// Use existing array, otherwise create one.
346427
data = data ?? new byte[length];
347-
Array.Copy(OriginalData, start, data, destStart, length - destStart);
428+
Array.Copy(tempOriginalData, start, data, destStart, length - destStart);
348429

349430
// Write header if existing (DDS Only)
350431
if (tempHeader != null)

0 commit comments

Comments
 (0)