@@ -19,15 +19,14 @@ public PixedModel Deserialize(Stream stream, ApplicationData applicationData)
19
19
20
20
public void Serialize ( Stream stream , PixedModel model , bool close )
21
21
{
22
- BluwolfIcons . Icon icon = new ( ) ;
23
-
22
+ IList < SkiaIconImage > images = [ ] ;
24
23
if ( model . Frames . Count == 1 )
25
24
{
26
25
var bitmap = model . CurrentFrame . Render ( ) ;
27
26
28
27
if ( IconFormats . Count == 0 )
29
28
{
30
- icon . Images . Add ( new SkiaIconImage ( bitmap ) ) ;
29
+ images . Add ( new SkiaIconImage ( bitmap ) ) ;
31
30
}
32
31
else
33
32
{
@@ -37,7 +36,7 @@ public void Serialize(Stream stream, PixedModel model, bool close)
37
36
{
38
37
if ( format . X == bitmap . Width && format . Y == bitmap . Height )
39
38
{
40
- icon . Images . Add ( new SkiaIconImage ( bitmap ) ) ;
39
+ images . Add ( new SkiaIconImage ( bitmap ) ) ;
41
40
}
42
41
else
43
42
{
@@ -46,7 +45,7 @@ public void Serialize(Stream stream, PixedModel model, bool close)
46
45
canvas . Clear ( SKColors . Transparent ) ;
47
46
canvas . DrawBitmap ( bitmap , new SKRect ( 0 , 0 , scaledBitmap . Width , scaledBitmap . Height ) ) ;
48
47
canvas . Dispose ( ) ;
49
- icon . Images . Add ( new SkiaIconImage ( scaledBitmap ) ) ;
48
+ images . Add ( new SkiaIconImage ( scaledBitmap ) ) ;
50
49
}
51
50
}
52
51
}
@@ -55,15 +54,58 @@ public void Serialize(Stream stream, PixedModel model, bool close)
55
54
{
56
55
foreach ( var frame in model . Frames )
57
56
{
58
- icon . Images . Add ( new SkiaIconImage ( frame . Render ( ) ) ) ;
57
+ images . Add ( new SkiaIconImage ( frame . Render ( ) ) ) ;
59
58
}
60
59
}
61
60
62
- icon . Save ( stream ) ;
61
+ Save ( stream , images ) ;
63
62
64
63
if ( close )
65
64
{
66
65
stream . Dispose ( ) ;
67
66
}
68
67
}
68
+
69
+ private static void Save ( Stream stream , IList < SkiaIconImage > images )
70
+ {
71
+ ArgumentNullException . ThrowIfNull ( stream ) ;
72
+
73
+ if ( ! stream . CanWrite )
74
+ throw new ArgumentException ( "Stream must support writing." , nameof ( stream ) ) ;
75
+
76
+ using var writer = new BinaryWriter ( stream ) ;
77
+ // Reserved, always 0.
78
+ writer . Write ( ( ushort ) 0 ) ;
79
+ // 1 for ICO, 2 for CUR
80
+ writer . Write ( ( ushort ) 1 ) ;
81
+ writer . Write ( ( ushort ) images . Count ) ;
82
+
83
+ var pendingImages = new Queue < byte [ ] > ( ) ;
84
+ var offset = 6 + 16 * images . Count ; // Header: 6; Each Image: 16
85
+
86
+ foreach ( var image in images )
87
+ {
88
+ writer . Write ( ( byte ) image . Width ) ;
89
+ writer . Write ( ( byte ) image . Height ) ;
90
+
91
+ // Number of colors in the palette. Since we always save the image ourselves (with no palette), hardcode this to 0 (No palette).
92
+ writer . Write ( ( byte ) 0 ) ;
93
+ // Reserved, always 0.
94
+ writer . Write ( ( byte ) 0 ) ;
95
+ // Color planes. Since we save the images ourselves, this is 1.
96
+ writer . Write ( ( ushort ) 1 ) ;
97
+ writer . Write ( ( ushort ) image . BitsPerPixel ) ;
98
+
99
+ var data = image . GetData ( ) ;
100
+
101
+ writer . Write ( ( uint ) data . Length ) ;
102
+ writer . Write ( ( uint ) offset ) ;
103
+
104
+ offset += data . Length ;
105
+ pendingImages . Enqueue ( data ) ;
106
+ }
107
+
108
+ while ( pendingImages . Count > 0 )
109
+ writer . Write ( pendingImages . Dequeue ( ) ) ;
110
+ }
69
111
}
0 commit comments