Skip to content

Commit 203c503

Browse files
committed
fix: wrong pfim image format
Signed-off-by: leo <[email protected]>
1 parent 47012e2 commit 203c503

File tree

1 file changed

+8
-36
lines changed

1 file changed

+8
-36
lines changed

src/ViewModels/ImageSource.cs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -100,59 +100,31 @@ private static ImageSource DecodeWithPfim(Stream stream, long size)
100100
var data = pfiImage.Data;
101101
var stride = pfiImage.Stride;
102102

103-
var pixelFormat = PixelFormats.Rgba8888;
103+
var pixelFormat = PixelFormats.Bgra8888;
104104
var alphaFormat = AlphaFormat.Opaque;
105105
switch (pfiImage.Format)
106106
{
107107
case ImageFormat.Rgb8:
108108
pixelFormat = PixelFormats.Gray8;
109109
break;
110-
case ImageFormat.R5g6b5:
111-
pixelFormat = PixelFormats.Rgb565;
112-
break;
113-
case ImageFormat.Rgba16:
114-
var pixels = pfiImage.DataLen / 2;
115-
var newSize = pfiImage.DataLen * 2;
116-
data = new byte[newSize];
117-
stride = 4 * pfiImage.Width;
118-
for (int i = 0; i < pixels; i++)
119-
{
120-
var rg = pfiImage.Data[i * 2];
121-
var ba = pfiImage.Data[i * 2 + 1];
122-
data[i * 4 + 0] = (byte)Math.Round((rg >> 4) / 15.0 * 255);
123-
data[i * 4 + 1] = (byte)Math.Round((rg & 0xF) / 15.0 * 255);
124-
data[i * 4 + 2] = (byte)Math.Round((ba >> 4) / 15.0 * 255);
125-
data[i * 4 + 3] = (byte)Math.Round((ba & 0xF) / 15.0 * 255);
126-
}
127-
alphaFormat = AlphaFormat.Premul;
128-
break;
110+
case ImageFormat.R5g5b5:
129111
case ImageFormat.R5g5b5a1:
130-
var pixels2 = pfiImage.DataLen / 2;
131-
var newSize2 = pfiImage.DataLen * 2;
132-
data = new byte[newSize2];
133-
stride = 4 * pfiImage.Width;
134-
for (int i = 0; i < pixels2; i++)
135-
{
136-
var v = (int)pfiImage.Data[i * 2] << 8 + pfiImage.Data[i * 2 + 1];
137-
data[i * 4 + 0] = (byte)Math.Round(((v & 0b1111100000000000) >> 11) / 31.0 * 255);
138-
data[i * 4 + 1] = (byte)Math.Round(((v & 0b11111000000) >> 6) / 31.0 * 255);
139-
data[i * 4 + 2] = (byte)Math.Round(((v & 0b111110) >> 1) / 31.0 * 255);
140-
data[i * 4 + 3] = (byte)((v & 1) == 1 ? 255 : 0);
141-
}
142-
alphaFormat = AlphaFormat.Premul;
112+
pixelFormat = PixelFormats.Bgr555;
113+
break;
114+
case ImageFormat.R5g6b5:
115+
pixelFormat = PixelFormats.Bgr565;
143116
break;
144117
case ImageFormat.Rgb24:
145-
pixelFormat = PixelFormats.Rgb24;
118+
pixelFormat = PixelFormats.Bgr24;
146119
break;
147120
case ImageFormat.Rgba32:
148-
pixelFormat = PixelFormat.Rgba8888;
149121
alphaFormat = AlphaFormat.Premul;
150122
break;
151123
default:
152124
return new ImageSource(null, 0);
153125
}
154126

155-
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(pfiImage.Data, 0);
127+
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0);
156128
var pixelSize = new PixelSize(pfiImage.Width, pfiImage.Height);
157129
var dpi = new Vector(96, 96);
158130
var bitmap = new Bitmap(pixelFormat, alphaFormat, ptr, pixelSize, dpi, stride);

0 commit comments

Comments
 (0)