* ImageList.cs: When the image stream is set pull all the images
[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                         
51                         
52                         byte [] data = (byte [])info.GetValue ("Data", typeof (byte []));
53                         if (data == null || data.Length <= signature.Length)
54                                 return;
55                         // check the signature ( 'MSFt' )
56                         if (data [0] != signature [0] || data [1] != signature [1] ||
57                                         data [2] != signature [2] ||  data [3] != signature [3])
58                                 return;
59
60                         // calulate size of array needed for decomressed data
61                         int i = 0;
62                         int real_byte_count = 0;
63                         for (i = signature.Length; i < data.Length; i += 2)
64                                 real_byte_count += data [i];
65
66                         if (real_byte_count == 0)
67                                 return;
68                         
69                         int j = 0;
70                         byte [] decompressed = new byte [real_byte_count];
71
72                         for (i = signature.Length; i < data.Length; i += 2) {
73                                 for (int k = 0; k < data [i]; k++)
74                                         decompressed [j++] = data [i + 1];
75                         }
76
77                         MemoryStream stream = new MemoryStream (decompressed);
78                         BinaryReader reader = new BinaryReader (stream);
79
80                         IntPtr hbmMask = IntPtr.Zero;
81                         IntPtr hbmColor= IntPtr.Zero;
82
83                         try {
84                                 // read image list header
85                                 ushort usMagic   = reader.ReadUInt16 ();
86                                 ushort usVersion = reader.ReadUInt16 ();
87                                 ushort cCurImage = reader.ReadUInt16 ();
88                                 ushort cMaxImage = reader.ReadUInt16 ();
89                                 ushort cGrow     = reader.ReadUInt16 ();
90                                 ushort cx        = reader.ReadUInt16 ();
91                                 ushort cy        = reader.ReadUInt16 ();
92                                 uint   bkcolor   = reader.ReadUInt32 ();
93                                 ushort flags     = reader.ReadUInt16 ();
94
95                                 short [] ovls = new short [4];
96                                 for (i = 0; i < ovls.Length; i++)
97                                         ovls[i] = reader.ReadInt16 ();
98
99                                 image_size = new Size (cx, cy);
100                                 back_color = Color.FromArgb ((int) bkcolor);
101                                                 
102                                 MemoryStream start = new MemoryStream (decompressed,
103                                                 (int) stream.Position,
104                                                 (int) stream.Length - (int) stream.Position,
105                                                 false);
106
107                                 Image image = Image.FromStream (start);
108
109                                 // Holy calamity. This is what happens on MS
110                                 // if the background colour is 0xFFFFFFFF (CLR_NONE)
111                                 // the mask is set to the color at pixel 0, 0
112                                 Bitmap bmp = image as Bitmap;
113                                 if (bkcolor == 0xFFFFFFFF && bmp != null)
114                                         back_color = bmp.GetPixel (0, 0);
115
116                                 int step = image.Width / cx;
117                                 images = new Image [cCurImage];
118
119                                 Rectangle dest_rect = new Rectangle (0, 0, cx, cy);
120                                 for (int r = 0 ; r < cCurImage ; r++) {
121                                         Rectangle area = new Rectangle (
122                                                 (r % step) / cx,
123                                                 (r / step) * cy, 
124                                                 cx, cy);
125                                         Bitmap b = new Bitmap (cx, cy);
126                                         using (Graphics g = Graphics.FromImage (b)) {
127                                                 g.DrawImage (image, dest_rect, area, 
128                                                                 GraphicsUnit.Pixel);
129                                         }
130                                         b.MakeTransparent (back_color);
131                                         images [r] = b;
132                                 }
133
134                         } catch (Exception e) {
135
136                         }
137                 }
138
139                 [MonoTODO]
140                 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
141                 {
142
143                 }
144
145                 internal Image [] Images {
146                         get { return images; }
147                 }
148
149                 internal Size ImageSize {
150                         get { return image_size; }
151                 }
152
153                 internal ColorDepth ImageColorDepth {
154                         get { return ColorDepth.Depth32Bit; }
155                 }
156
157                 internal Color BackColor {
158                         get { return back_color; }
159                 }
160         }
161 }
162