Skip to content

Commit fbb9f78

Browse files
committed
fix: wrong alpha while reading .dds and .tga
Signed-off-by: leo <[email protected]>
1 parent 6ccb5af commit fbb9f78

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/ViewModels/ImageSource.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,23 @@ private static ImageSource DecodeWithPfim(Stream stream, long size)
109109
pixelFormat = PixelFormats.Gray8;
110110
break;
111111
case ImageFormat.R5g5b5:
112-
case ImageFormat.R5g5b5a1:
113112
pixelFormat = PixelFormats.Bgr555;
114113
break;
114+
case ImageFormat.R5g5b5a1:
115+
var pixels1 = pfiImage.DataLen / 2;
116+
data = new byte[pixels1 * 4];
117+
stride = pfiImage.Width * 4;
118+
for (var i = 0; i < pixels1; i++)
119+
{
120+
var src = BitConverter.ToUInt16(pfiImage.Data, i * 2);
121+
data[i * 4 + 0] = (byte)Math.Round((src & 0x1F) / 31F * 255); // B
122+
data[i * 4 + 1] = (byte)Math.Round(((src >> 5) & 0x1F) / 31F * 255); // G
123+
data[i * 4 + 2] = (byte)Math.Round(((src >> 10) & 0x1F) / 31F * 255); // R
124+
data[i * 4 + 3] = (byte)((src >> 15) * 255F); // A
125+
}
126+
127+
alphaFormat = AlphaFormat.Unpremul;
128+
break;
115129
case ImageFormat.R5g6b5:
116130
pixelFormat = PixelFormats.Bgr565;
117131
break;
@@ -131,10 +145,10 @@ private static ImageSource DecodeWithPfim(Stream stream, long size)
131145
data[i * 4 + 3] = (byte)Math.Round(((src >> 12) & 0x0F) / 15F * 255); // A
132146
}
133147

134-
alphaFormat = AlphaFormat.Premul;
148+
alphaFormat = AlphaFormat.Unpremul;
135149
break;
136150
case ImageFormat.Rgba32:
137-
alphaFormat = AlphaFormat.Premul;
151+
alphaFormat = AlphaFormat.Unpremul;
138152
break;
139153
default:
140154
return new ImageSource(null, 0);
@@ -165,7 +179,7 @@ private static ImageSource DecodeWithTiff(Stream stream, long size)
165179
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(pixels, 0);
166180
var pixelSize = new PixelSize(width, height);
167181
var dpi = new Vector(96, 96);
168-
var bitmap = new Bitmap(PixelFormats.Rgba8888, AlphaFormat.Premul, ptr, pixelSize, dpi, width * 4);
182+
var bitmap = new Bitmap(PixelFormats.Rgba8888, AlphaFormat.Unpremul, ptr, pixelSize, dpi, width * 4);
169183
return new ImageSource(bitmap, size);
170184
}
171185
}

0 commit comments

Comments
 (0)