Switch to compiler-tester
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ImageListStreamer.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2002-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper (jackson@ximian.com)
24 //
25 // Based on work done by:
26 //   Dennis Hayes (dennish@Raytek.com)
27 //   Aleksey Ryabchuk (ryabchuk@yahoo.com)
28
29 using System.IO;
30 using System.Drawing;
31 using System.Collections;
32 using System.Drawing.Imaging;
33 using System.Runtime.Serialization;
34 using System.Runtime.InteropServices;
35
36
37 namespace System.Windows.Forms {
38
39         [Serializable]
40         public sealed class ImageListStreamer : ISerializable {
41
42                 private static byte [] signature = new byte [] {77 , 83 , 70 , 116};
43                 
44                 private Image [] images;
45                 private Size image_size;
46                 private Color back_color;
47
48                 private ImageListStreamer (SerializationInfo info, StreamingContext context) {
49
50                         byte [] data = (byte [])info.GetValue ("Data", typeof (byte []));
51                         if (data == null || data.Length <= signature.Length)
52                                 return;
53                         // check the signature ( 'MSFt' )
54                         if (data [0] != signature [0] || data [1] != signature [1] ||
55                                         data [2] != signature [2] ||  data [3] != signature [3])
56                                 return;
57
58                         // calulate size of array needed for decomressed data
59                         int i = 0;
60                         int real_byte_count = 0;
61                         for (i = signature.Length; i < data.Length; i += 2)
62                                 real_byte_count += data [i];
63
64                         if (real_byte_count == 0)
65                                 return;
66                         
67                         int j = 0;
68                         byte [] decompressed = new byte [real_byte_count];
69
70                         for (i = signature.Length; i < data.Length; i += 2) {
71                                 for (int k = 0; k < data [i]; k++)
72                                         decompressed [j++] = data [i + 1];
73                         }
74
75                         MemoryStream stream = new MemoryStream (decompressed);
76                         BinaryReader reader = new BinaryReader (stream);
77
78                         try {
79                                 // read image list header
80                                 reader.ReadUInt16 ();   // usMagic
81                                 reader.ReadUInt16 ();   // usVersion
82                                 ushort cCurImage = reader.ReadUInt16 ();
83                                 reader.ReadUInt16 ();   // cMaxImage
84                                 reader.ReadUInt16 ();   // cGrow
85                                 ushort cx        = reader.ReadUInt16 ();
86                                 ushort cy        = reader.ReadUInt16 ();
87                                 uint   bkcolor   = reader.ReadUInt32 ();
88                                 reader.ReadUInt16 ();   // flags
89
90                                 short [] ovls = new short [4];
91                                 for (i = 0; i < ovls.Length; i++) {
92                                         ovls[i] = reader.ReadInt16 ();
93                                 }
94
95                                 image_size = new Size (cx, cy);
96                                 back_color = Color.FromArgb ((int) bkcolor);
97                                                 
98                                 MemoryStream start = new MemoryStream (decompressed,
99                                                 (int) stream.Position,
100                                                 (int) stream.Length - (int) stream.Position,
101                                                 false);
102
103                                 Image image = Image.FromStream (start);
104
105                                 // Holy calamity. This is what happens on MS
106                                 // if the background colour is 0xFFFFFFFF (CLR_NONE)
107                                 // the mask is set to the color at pixel 0, 0
108                                 Bitmap bmp = image as Bitmap;
109                                 if (bkcolor == 0xFFFFFFFF && bmp != null)
110                                         back_color = bmp.GetPixel (0, 0);
111
112                                 int step = image.Width / cx;
113                                 images = new Image [cCurImage];
114
115                                 Rectangle dest_rect = new Rectangle (0, 0, cx, cy);
116                                 for (int r = 0 ; r < cCurImage ; r++) {
117                                         Rectangle area = new Rectangle (
118                                                 (r % step) * cx,
119                                                 (r / step) * cy,
120                                                 cx, cy);
121                                         Bitmap b = new Bitmap (cx, cy);
122                                         using (Graphics g = Graphics.FromImage (b)) {
123                                                 g.DrawImage (image, dest_rect, area, 
124                                                                 GraphicsUnit.Pixel);
125                                         }
126                                         b.MakeTransparent (back_color);
127                                         images [r] = b;
128                                 }
129
130                         } catch (Exception e) {
131
132                         }
133                 }
134
135                 [MonoTODO ("RLE is broken")]
136                 public void GetObjectData (SerializationInfo info, StreamingContext context)
137                 {
138                         MemoryStream stream = new MemoryStream ();
139                         BinaryWriter writer = new BinaryWriter (stream);
140
141                         writer.Write (signature);
142                         writer.Write (GetStreamData ());
143                         
144                         info.AddValue ("Data", stream.ToArray (), typeof (byte []));
145                 }
146
147                 private byte [] GetStreamData ()
148                 {
149                         MemoryStream stream = new MemoryStream ();
150                         BinaryWriter writer = new BinaryWriter (stream);
151
152                         int cols = 4;
153                         int rows = images.Length / cols;
154                         if (images.Length % cols > 0)
155                                 ++rows;
156
157                         Bitmap main = new Bitmap (cols * ImageSize.Width, rows * ImageSize.Height);
158                         using (Graphics g = Graphics.FromImage (main)) {
159                                 g.FillRectangle (new SolidBrush (BackColor), 0, 0, cols * ImageSize.Width, rows * ImageSize.Height);
160                                 for (int i = 0; i < images.Length; i++) {
161                                         g.DrawImage (images [i], (i % cols) * ImageSize.Width,
162                                                         (i / cols) * ImageSize.Height);
163                                 }
164                         }
165
166                         writer.Write ((ushort) (('L' << 8) | 'I'));    // magic
167                         writer.Write ((ushort) 0x101);                 // version
168                         writer.Write ((ushort) images.Length);
169                         writer.Write ((ushort) images.Length);
170                         writer.Write ((ushort) (rows * cols));
171                         writer.Write ((ushort) 0x4);                    // grow....not sure this should be hard coded
172                         writer.Write ((ushort) image_size.Width);
173                         writer.Write ((ushort) image_size.Height);
174                         writer.Write (BackColor.ToArgb ());
175                         writer.Write ((ushort) 0x1009);                // flags
176
177                         for (int i = 0; i < 4; i++)
178                                 writer.Write ((short) -1);  // ovls
179
180                         return RLEncodeData (stream.ToArray ());
181                 }
182
183                 // TODO: This is broken
184                 private byte [] RLEncodeData (byte [] data)
185                 {
186                         MemoryStream stream = new MemoryStream ();
187                         BinaryWriter writer = new BinaryWriter (stream);
188
189                         for (int i = 0; i < data.Length; i += 2) {
190                                 int seq = 0;
191                                 byte item  = data [i];
192                                 while (data [i++] == item && i < data.Length)
193                                         seq++;
194                                 writer.Write ((byte) seq);
195                                 writer.Write (item);
196                         }
197
198                         return stream.ToArray ();
199
200                 }
201
202                 internal Image [] Images {
203                         get { return images; }
204                 }
205
206                 internal Size ImageSize {
207                         get { return image_size; }
208                 }
209
210                 internal ColorDepth ImageColorDepth {
211                         get { return ColorDepth.Depth32Bit; }
212                 }
213
214                 internal Color BackColor {
215                         get { return back_color; }
216                 }
217         }
218 }
219