1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2002-2006 Novell, Inc.
//
// Authors:
// Jackson Harper (jackson@ximian.com)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// Based on work done by:
// Dennis Hayes (dennish@Raytek.com)
// Aleksey Ryabchuk (ryabchuk@yahoo.com)
using System.IO;
using System.Drawing;
using System.Collections;
using System.Drawing.Imaging;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
namespace System.Windows.Forms {
[Serializable]
public sealed class ImageListStreamer : ISerializable {
readonly ImageList.ImageCollection imageCollection;
Image [] images;
Size image_size;
Color back_color;
internal ImageListStreamer (ImageList.ImageCollection imageCollection)
{
this.imageCollection = imageCollection;
}
private ImageListStreamer (SerializationInfo info, StreamingContext context)
{
byte [] data = (byte []) info.GetValue ("Data", typeof (byte []));
if (data == null || data.Length <= 4) { // 4 is the signature
return;
}
// check the signature ( 'MSFt' )
if (data [0] != 77 || data [1] != 83 || data [2] != 70 || data [3] != 116) {
return;
}
MemoryStream decoded = GetDecodedStream (data, 4, data.Length - 4);
decoded.Position = 4; // jumps over 'magic' and 'version', which are 16-bits each
BinaryReader reader = new BinaryReader (decoded);
ushort nimages = reader.ReadUInt16 ();
reader.ReadUInt16 (); // cMaxImage
ushort grow = reader.ReadUInt16 (); // cGrow
ushort cx = reader.ReadUInt16 ();
ushort cy = reader.ReadUInt16 ();
uint bkcolor = reader.ReadUInt32 ();
back_color = Color.FromArgb ((int) bkcolor);
reader.ReadUInt16 (); // flags
short [] ovls = new short [4];
for (int i = 0; i < 4; i++) {
ovls[i] = reader.ReadInt16 ();
}
byte [] decoded_buffer = decoded.GetBuffer ();
int bmp_offset = 28;
// FileSize field from the bitmap file header
int filesize = decoded_buffer [bmp_offset + 2] + (decoded_buffer [bmp_offset + 3] << 8) +
(decoded_buffer [bmp_offset + 4] << 16) + (decoded_buffer [bmp_offset + 5] << 24);
// ImageSize field from the info header (can be 0)
int imagesize = decoded_buffer [bmp_offset + 34] + (decoded_buffer [bmp_offset + 35] << 8) +
(decoded_buffer [bmp_offset + 36] << 16) + (decoded_buffer [bmp_offset + 37] << 24);
int bmp_length = imagesize + filesize;
MemoryStream bmpms = new MemoryStream (decoded_buffer, bmp_offset, bmp_length);
Bitmap bmp = null;
Bitmap mask = null;
bmp = new Bitmap (bmpms);
MemoryStream mask_stream = new MemoryStream (decoded_buffer,
bmp_offset + bmp_length,
(int) (decoded.Length - bmp_offset - bmp_length));
if (mask_stream.Length > 0)
mask = new Bitmap (mask_stream);
if (bkcolor == 0xFFFFFFFF)
back_color = bmp.GetPixel (0, 0);
if (mask != null) {
int width = bmp.Width;
int height = bmp.Height;
Bitmap newbmp = new Bitmap (bmp);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Color mcolor = mask.GetPixel (x, y);
if (mcolor.B != 0) {
newbmp.SetPixel (x, y, Color.Transparent);
}
}
}
bmp.Dispose ();
bmp = newbmp;
mask.Dispose ();
mask = null;
}
images = new Image [nimages];
image_size = new Size (cx, cy);
Rectangle dest_rect = new Rectangle (0, 0, cx, cy);
if (grow * bmp.Width > cx) // Some images store a wrong 'grow' factor
grow = (ushort) (bmp.Width / cx);
for (int r = 0 ; r < nimages ; r++) {
int col = r % grow;
int row = r / grow;
Rectangle area = new Rectangle (col * cx, row * cy, cx, cy);
Bitmap b = new Bitmap (cx, cy);
using (Graphics g = Graphics.FromImage (b)) {
g.DrawImage (bmp, dest_rect, area, GraphicsUnit.Pixel);
}
images [r] = b;
}
bmp.Dispose ();
}
/*
static void WriteToFile (MemoryStream st)
{
st.Position = 0;
FileStream fs = File.OpenWrite (Path.GetTempFileName ());
Console.WriteLine ("Writing to {0}", fs.Name);
st.WriteTo (fs);
fs.Close ();
}
*/
static byte [] header = new byte []{ 77, 83, 70, 116, 73, 76, 1, 1 };
public void GetObjectData (SerializationInfo si, StreamingContext context)
{
MemoryStream stream = new MemoryStream ();
BinaryWriter writer = new BinaryWriter (stream);
writer.Write (header);
Image [] images = (imageCollection != null) ? imageCollection.ToArray () : this.images;
int cols = 4;
int rows = images.Length / cols;
if (images.Length % cols > 0)
++rows;
writer.Write ((ushort) images.Length);
writer.Write ((ushort) images.Length);
writer.Write ((ushort) 0x4);
writer.Write ((ushort) (images [0].Width));
writer.Write ((ushort) (images [0].Height));
writer.Write (0xFFFFFFFF); //BackColor.ToArgb ()); //FIXME: should set the right one here.
writer.Write ((ushort) 0x21);
for (int i = 0; i < 4; i++)
writer.Write ((short) -1);
Bitmap main = new Bitmap (cols * ImageSize.Width, rows * ImageSize.Height);
using (Graphics g = Graphics.FromImage (main)) {
g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), 0, 0,
main.Width, main.Height);
for (int i = 0; i < images.Length; i++) {
g.DrawImage (images [i], (i % cols) * ImageSize.Width,
(i / cols) * ImageSize.Height);
}
}
MemoryStream tmp = new MemoryStream ();
main.Save (tmp, ImageFormat.Bmp);
tmp.WriteTo (stream);
Bitmap mask = Get1bppMask (main);
main.Dispose ();
main = null;
tmp = new MemoryStream ();
mask.Save (tmp, ImageFormat.Bmp);
tmp.WriteTo (stream);
mask.Dispose ();
stream = GetRLEStream (stream, 4);
si.AddValue ("Data", stream.ToArray (), typeof (byte []));
}
unsafe Bitmap Get1bppMask (Bitmap main)
{
Rectangle rect = new Rectangle (0, 0, main.Width, main.Height);
Bitmap result = new Bitmap (main.Width, main.Height, PixelFormat.Format1bppIndexed);
BitmapData dresult = result.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
int w = images [0].Width;
int h = images [0].Height;
byte *scan = (byte *) dresult.Scan0.ToPointer ();
int stride = dresult.Stride;
Bitmap current = null;
for (int idx = 0; idx < images.Length; idx++) {
current = (Bitmap) images [idx];
// Hack for newly added images.
// Probably has to be done somewhere else.
Color c1 = current.GetPixel (0, 0);
if (c1.A != 0 && c1 == back_color)
current.MakeTransparent (back_color);
//
}
int yidx = 0;
int imgidx = 0;
int localy = 0;
int localx = 0;
int factor_y = 0;
int factor_x = 0;
for (int y = 0; y < main.Height; y++) {
if (localy == h) {
localy = 0;
factor_y += 4;
}
factor_x = 0;
localx = 0;
for (int x = 0; x < main.Width; x++) {
if (localx == w) {
localx = 0;
factor_x++;
}
imgidx = factor_y + factor_x;
if (imgidx >= images.Length)
break;
current = (Bitmap) images [imgidx];
Color color = current.GetPixel (localx, localy);
if (color.A == 0) {
int ptridx = yidx + (x >> 3);
scan [ptridx] |= (byte) (0x80 >> (x & 7));
}
localx++;
}
if (imgidx >= images.Length)
break;
yidx += stride;
localy++;
}
result.UnlockBits (dresult);
return result;
}
static MemoryStream GetDecodedStream (byte [] bytes, int offset, int size)
{
byte [] buffer = new byte [512];
int position = 0;
int count, data;
MemoryStream result = new MemoryStream ();
while (size > 0) {
count = (int) bytes [offset++];
data = (int) bytes [offset++];
if ((512 - count) < position) {
result.Write (buffer, 0, position);
position = 0;
}
for (int i = 0; i < count; i++)
buffer [position++] = (byte) data;
size -= 2;
}
if (position > 0)
result.Write (buffer, 0, position);
result.Position = 0;
return result;
}
//TODO: OptimizeMe
static MemoryStream GetRLEStream (MemoryStream input, int start)
{
MemoryStream result = new MemoryStream ();
byte [] ibuffer = input.GetBuffer ();
result.Write (ibuffer, 0, start);
input.Position = start;
int prev = -1;
int count = 0;
int current;
while ((current = input.ReadByte ()) != -1) {
if (prev != current || count == 255) {
if (prev != -1) {
result.WriteByte ((byte) count);
result.WriteByte ((byte) prev);
}
prev = current;
count = 0;
}
count++;
}
if (count > 0) {
result.WriteByte ((byte) count);
result.WriteByte ((byte) current);
}
return result;
}
internal Image [] Images {
get { return images; }
}
internal Size ImageSize {
get { return image_size; }
}
internal ColorDepth ColorDepth {
get { return ColorDepth.Depth32Bit; }
}
internal Color BackColor {
get { return back_color; }
}
}
}
|