Skip to content

Commit

Permalink
add: 图片导出的进度条
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxuuu committed Dec 6, 2024
1 parent 420223f commit 7b10b82
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
58 changes: 31 additions & 27 deletions Image2Display/Image2Display/Helpers/ColorData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ColorData
/// <param name="rotate">遍历顺序</param>
/// <param name="action">回调函数</param>
/// <exception cref="NotImplementedException"></exception>
private static void IterateImg(Image<Rgba32> img, int rotate, Action<Rgba32> action)
private static void IterateImg(Image<Rgba32> img, int rotate, Action<Rgba32> action, Action<int> PgsCb)
{
//主要开始1 主要结束1 次要开始2 次要结束2 是否先x后y
var (m1,m2,p1,p2,xFirst) = rotate switch
Expand All @@ -36,13 +36,16 @@ private static void IterateImg(Image<Rgba32> img, int rotate, Action<Rgba32> act

var mStep = m1 < m2 ? 1 : -1;
var pStep = p1 < p2 ? 1 : -1;
var steps = img.Width * img.Height;
var stepNow = 0;
if (xFirst)
{
for (var y = p1; y != p2 + pStep; y += pStep)
{
for (var x = m1; x != m2 + mStep; x += mStep)
{
Debug.WriteLine($"x:{x} y:{y}");
stepNow++;
PgsCb(stepNow * 100 / steps);
action(img[x, y]);
}
}
Expand All @@ -53,7 +56,8 @@ private static void IterateImg(Image<Rgba32> img, int rotate, Action<Rgba32> act
{
for (var y = m1; y != m2 + mStep; y += mStep)
{
Debug.WriteLine($"x:{x} y:{y}");
stepNow++;
PgsCb(stepNow * 100 / steps);
action(img[x, y]);
}
}
Expand All @@ -77,7 +81,7 @@ private static int ReverseBits(int n, int bits)
return result;
}

public static List<byte> Get1_2_4_8BitsImage(Image<Rgba32> img, int rotate, bool reverseBits, int bitsPerPixel, List<Rgba32> colors)
public static List<byte> Get1_2_4_8BitsImage(Image<Rgba32> img, int rotate, bool reverseBits, int bitsPerPixel, List<Rgba32> colors, Action<int> PgsCb)
{
var l = new List<byte>();
var count = 0;
Expand All @@ -103,7 +107,7 @@ public static List<byte> Get1_2_4_8BitsImage(Image<Rgba32> img, int rotate, bool
count = 0;
temp = 0;
}
});
}, PgsCb);
// 如果最后一个字节不满8位,补0
if (count != 0)
{
Expand All @@ -114,7 +118,7 @@ public static List<byte> Get1_2_4_8BitsImage(Image<Rgba32> img, int rotate, bool
return l;
}

public static List<byte> Get16BitsImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, List<Rgba32> colors)
public static List<byte> Get16BitsImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, List<Rgba32> colors, Action<int> PgsCb)
{
var l = new List<byte>();
ushort temp = 0;
Expand All @@ -141,12 +145,12 @@ public static List<byte> Get16BitsImage(Image<Rgba32> img, int rotate, bool reve
l.Add((byte)((temp >> 8) & 0xFF));
l.Add((byte)(temp & 0xFF));
}
});
}, PgsCb);

return l;
}

public static List<byte> GetRGB444Image(Image<Rgba32> img, int rotate, bool reverseBits)
public static List<byte> GetRGB444Image(Image<Rgba32> img, int rotate, bool reverseBits, Action<int> PgsCb)
{
var l = new List<byte>();
byte halfTemp = 0;
Expand Down Expand Up @@ -181,15 +185,15 @@ public static List<byte> GetRGB444Image(Image<Rgba32> img, int rotate, bool reve
halfTemp = (byte)(b << 4);
isLastHalfByte = true;
}
});
}, PgsCb);

if (isLastHalfByte)
l.Add(halfTemp);

return l;
}

public static List<byte> GetRGB444HighEmptyImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian)
public static List<byte> GetRGB444HighEmptyImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, Action<int> PgsCb)
{
var l = new List<byte>();

Expand Down Expand Up @@ -221,12 +225,12 @@ public static List<byte> GetRGB444HighEmptyImage(Image<Rgba32> img, int rotate,
temp |= b;
l.Add(temp);
}
});
}, PgsCb);

return l;
}

public static List<byte> GetRGB565Image(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian)
public static List<byte> GetRGB565Image(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, Action<int> PgsCb)
{
var l = new List<byte>();

Expand Down Expand Up @@ -256,12 +260,12 @@ public static List<byte> GetRGB565Image(Image<Rgba32> img, int rotate, bool reve
l.Add((byte)((temp >> 8) & 0xFF));
l.Add((byte)(temp & 0xFF));
}
});
}, PgsCb);

return l;
}

public static List<byte> GetRGB555HighEmptyImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian)
public static List<byte> GetRGB555HighEmptyImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, Action<int> PgsCb)
{
var l = new List<byte>();

Expand Down Expand Up @@ -291,12 +295,12 @@ public static List<byte> GetRGB555HighEmptyImage(Image<Rgba32> img, int rotate,
l.Add((byte)((temp >> 8) & 0xFF));
l.Add((byte)(temp & 0xFF));
}
});
}, PgsCb);

return l;
}

public static List<byte> GetRGB666HighEmptyImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian)
public static List<byte> GetRGB666HighEmptyImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, Action<int> PgsCb)
{
var l = new List<byte>();

Expand Down Expand Up @@ -324,12 +328,12 @@ public static List<byte> GetRGB666HighEmptyImage(Image<Rgba32> img, int rotate,
l.Add(g);
l.Add(b);
}
});
}, PgsCb);

return l;
}

public static List<byte> GetRGB666LowEmptyImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian)
public static List<byte> GetRGB666LowEmptyImage(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, Action<int> PgsCb)
{
var l = new List<byte>();

Expand Down Expand Up @@ -360,12 +364,12 @@ public static List<byte> GetRGB666LowEmptyImage(Image<Rgba32> img, int rotate, b
l.Add(temp2);
l.Add(temp3);
}
});
}, PgsCb);

return l;
}

public static List<byte> GetRGB888Image(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian)
public static List<byte> GetRGB888Image(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, Action<int> PgsCb)
{
var l = new List<byte>();

Expand Down Expand Up @@ -393,12 +397,12 @@ public static List<byte> GetRGB888Image(Image<Rgba32> img, int rotate, bool reve
l.Add(g);
l.Add(b);
}
});
}, PgsCb);

return l;
}

public static List<byte> GetARGB8888Image(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian)
public static List<byte> GetARGB8888Image(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, Action<int> PgsCb)
{
var l = new List<byte>();

Expand Down Expand Up @@ -430,12 +434,12 @@ public static List<byte> GetARGB8888Image(Image<Rgba32> img, int rotate, bool re
l.Add(g);
l.Add(b);
}
});
}, PgsCb);

return l;
}

public static List<byte> GetRGBA8888Image(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian)
public static List<byte> GetRGBA8888Image(Image<Rgba32> img, int rotate, bool reverseBits, bool isSmallEndian, Action<int> PgsCb)
{
var l = new List<byte>();

Expand Down Expand Up @@ -467,12 +471,12 @@ public static List<byte> GetRGBA8888Image(Image<Rgba32> img, int rotate, bool re
l.Add(b);
l.Add(a);
}
});
}, PgsCb);

return l;
}

public static List<byte> GetGray8Image(Image<Rgba32> img, int rotate, bool reverseBits)
public static List<byte> GetGray8Image(Image<Rgba32> img, int rotate, bool reverseBits, Action<int> PgsCb)
{
var l = new List<byte>();

Expand All @@ -485,7 +489,7 @@ public static List<byte> GetGray8Image(Image<Rgba32> img, int rotate, bool rever
}

l.Add(r);
});
}, PgsCb);

return l;
}
Expand Down
33 changes: 21 additions & 12 deletions Image2Display/Image2Display/ViewModels/ImageConvertViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public ImageConvertViewModel()
[ObservableProperty]
private bool _isProcessing = false;

//处理进度
[ObservableProperty]
private int _ProgressValue = 0;

private ImageData? Image = null;
private List<Rgba32> ImageColors = new List<Rgba32>();
Expand Down Expand Up @@ -186,6 +189,11 @@ private bool CheckColorVilid()
{
IsProcessing = true;
List<byte>? r = null;
ProgressValue = 0;
void cb(int value)
{
ProgressValue = value;
}
try
{
await Task.Run(() =>
Expand All @@ -196,10 +204,10 @@ await Task.Run(() =>
{
<= 3 => ColorData.Get1_2_4_8BitsImage(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
(int)double.Pow(2, ColorDepth), ImageColors),
(int)double.Pow(2, ColorDepth), ImageColors,cb),
4 => ColorData.Get16BitsImage(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
ByteOrder == 1, ImageColors),
ByteOrder == 1, ImageColors, cb),
_ => throw new NotImplementedException(),
};
}
Expand All @@ -208,33 +216,33 @@ await Task.Run(() =>
r = FullColorStorage switch
{
0 => ColorData.GetRGB444Image(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1),
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1, cb),
1 => ColorData.GetRGB444HighEmptyImage(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
ByteOrder == 1),
ByteOrder == 1, cb),
2 => ColorData.GetRGB565Image(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
ByteOrder == 1),
ByteOrder == 1, cb),
3 => ColorData.GetRGB555HighEmptyImage(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
ByteOrder == 1),
ByteOrder == 1, cb),
4 => ColorData.GetRGB666HighEmptyImage(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
ByteOrder == 1),
ByteOrder == 1, cb),
5 => ColorData.GetRGB666LowEmptyImage(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
ByteOrder == 1),
ByteOrder == 1, cb),
6 => ColorData.GetRGB888Image(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
ByteOrder == 1),
ByteOrder == 1, cb),
7 => ColorData.GetARGB8888Image(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
ByteOrder == 1),
ByteOrder == 1, cb),
8 => ColorData.GetRGBA8888Image(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1,
ByteOrder == 1),
ByteOrder == 1, cb),
9 => ColorData.GetGray8Image(
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1),
Image!.Raw, PixelTraversalOrder, ColorInternalOrder == 1, cb),
_ => throw new NotImplementedException(),
};
}
Expand All @@ -244,6 +252,7 @@ await Task.Run(() =>
{
ShowError("导出失败" + e.Message, "Export failed" + e.Message);
}
ProgressValue = 0;
IsProcessing = false;
return r;
}
Expand Down
8 changes: 8 additions & 0 deletions Image2Display/Image2Display/Views/ImageConvertView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
x:DataType="vm:ImageConvertViewModel"
mc:Ignorable="d">
<Grid ColumnDefinitions="*,200">
<ProgressBar
Grid.ColumnSpan="2"
Height="5"
VerticalAlignment="Bottom"
Background="Transparent"
Maximum="100"
Minimum="0"
Value="{Binding ProgressValue}" />
<Grid Grid.Column="1" RowDefinitions="*,auto,2*,auto">
<Border
Margin="10"
Expand Down

0 comments on commit 7b10b82

Please sign in to comment.