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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2002-2005 Novell, Inc.
23 // Jackson Harper (jackson@ximian.com)
25 // Based on work done by:
26 // Dennis Hayes (dennish@Raytek.com)
27 // Aleksey Ryabchuk (ryabchuk@yahoo.com)
31 using System.Collections;
32 using System.Drawing.Imaging;
33 using System.Runtime.Serialization;
34 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms {
40 public sealed class ImageListStreamer : ISerializable {
42 private static byte [] signature = new byte [] {77 , 83 , 70 , 116};
44 private Image [] images;
45 private Size image_size;
46 private Color back_color;
48 private ImageListStreamer (SerializationInfo info, StreamingContext context) {
50 byte [] data = (byte [])info.GetValue ("Data", typeof (byte []));
51 if (data == null || data.Length <= signature.Length)
53 // check the signature ( 'MSFt' )
54 if (data [0] != signature [0] || data [1] != signature [1] ||
55 data [2] != signature [2] || data [3] != signature [3])
58 // calulate size of array needed for decomressed data
60 int real_byte_count = 0;
61 for (i = signature.Length; i < data.Length; i += 2)
62 real_byte_count += data [i];
64 if (real_byte_count == 0)
68 byte [] decompressed = new byte [real_byte_count];
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];
75 MemoryStream stream = new MemoryStream (decompressed);
76 BinaryReader reader = new BinaryReader (stream);
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
90 short [] ovls = new short [4];
91 for (i = 0; i < ovls.Length; i++) {
92 ovls[i] = reader.ReadInt16 ();
95 image_size = new Size (cx, cy);
96 back_color = Color.FromArgb ((int) bkcolor);
98 MemoryStream start = new MemoryStream (decompressed,
99 (int) stream.Position,
100 (int) stream.Length - (int) stream.Position,
103 Image image = Image.FromStream (start);
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);
112 int step = image.Width / cx;
113 images = new Image [cCurImage];
115 Rectangle dest_rect = new Rectangle (0, 0, cx, cy);
116 for (int r = 0 ; r < cCurImage ; r++) {
117 Rectangle area = new Rectangle (
121 Bitmap b = new Bitmap (cx, cy);
122 using (Graphics g = Graphics.FromImage (b)) {
123 g.DrawImage (image, dest_rect, area,
126 b.MakeTransparent (back_color);
130 } catch (Exception e) {
135 [MonoTODO ("RLE is broken")]
136 public void GetObjectData (SerializationInfo info, StreamingContext context)
138 MemoryStream stream = new MemoryStream ();
139 BinaryWriter writer = new BinaryWriter (stream);
141 writer.Write (signature);
142 writer.Write (GetStreamData ());
144 info.AddValue ("Data", stream.ToArray (), typeof (byte []));
147 private byte [] GetStreamData ()
149 MemoryStream stream = new MemoryStream ();
150 BinaryWriter writer = new BinaryWriter (stream);
153 int rows = images.Length / cols;
154 if (images.Length % cols > 0)
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);
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
177 for (int i = 0; i < 4; i++)
178 writer.Write ((short) -1); // ovls
180 return RLEncodeData (stream.ToArray ());
183 // TODO: This is broken
184 private byte [] RLEncodeData (byte [] data)
186 MemoryStream stream = new MemoryStream ();
187 BinaryWriter writer = new BinaryWriter (stream);
189 for (int i = 0; i < data.Length; i += 2) {
191 byte item = data [i];
192 while (data [i++] == item && i < data.Length)
194 writer.Write ((byte) seq);
198 return stream.ToArray ();
202 internal Image [] Images {
203 get { return images; }
206 internal Size ImageSize {
207 get { return image_size; }
210 internal ColorDepth ImageColorDepth {
211 get { return ColorDepth.Depth32Bit; }
214 internal Color BackColor {
215 get { return back_color; }