File: Map.cs

package info (click to toggle)
quickroute-gps 2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,576 kB
  • sloc: cs: 74,488; makefile: 72; sh: 43
file content (246 lines) | stat: -rw-r--r-- 7,484 bytes parent folder | download | duplicates (3)
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
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Reflection;
using System.Runtime.Serialization;
using System.Net;
using System.IO;
using System.Drawing;

namespace QuickRoute.BusinessEntities
{
  [Serializable]
  public class Map : ISerializable 
  {
    private MapStorageType storageType;
    private MapSourceType sourceType;
    private Bitmap image;
    private string source;
    [NonSerialized] private byte[] rawData;

    public Map(string source, MapSourceType sourceType, MapStorageType storageType)
    {
      this.source = source;
      this.sourceType = sourceType;
      this.storageType = storageType;
      this.image = GetImage(source, sourceType);
    }

    private Bitmap GetImage(string source, MapSourceType sourceType)
    {
      Stream stream = null;
      switch (sourceType)
      {
        case MapSourceType.FileSystem:
          stream = new FileStream(source, FileMode.Open);
          break;

        case MapSourceType.Url:
          stream = GetImageStreamFromUrl(source);
          break;
      }

      var result = StripQuickRouteHeader(stream);
      if(stream != null)
      {
        stream.Close();
        stream.Dispose();
      }
      return result;
    }

    private Bitmap StripQuickRouteHeader(Stream stream)
    {
      Bitmap targetImage = null;
      if(stream != null)
      {
        // check if the image is an exported QuickRoute image; if yes, remove the border
        var ed = QuickRouteJpegExtensionData.FromStream(stream);
        var sourceImage = System.Drawing.Image.FromStream(stream);
        rawData = new byte[stream.Length];
        stream.Position = 0;
        stream.Read(rawData, 0, (int)stream.Length);
        if (ed != null)
        {
          targetImage = new Bitmap(ed.MapLocationAndSizeInPixels.Width, ed.MapLocationAndSizeInPixels.Height);
          var g = Graphics.FromImage(targetImage);
          g.DrawImage(sourceImage, new Rectangle(new Point(0, 0), ed.MapLocationAndSizeInPixels.Size), ed.MapLocationAndSizeInPixels, GraphicsUnit.Pixel);
          g.Dispose();
        }
        else
        {
          targetImage = new Bitmap(sourceImage);
        }
      }
      return targetImage;
    }

    public Map(Bitmap image)
    {
      this.source = null;
      this.sourceType = MapSourceType.FileSystem;
      this.storageType = MapStorageType.Inline;
      this.image = image;
      this.rawData = null;
    }

    public Map(Stream stream)
    {
      this.source = null;
      this.sourceType = MapSourceType.FileSystem;
      this.storageType = MapStorageType.Inline;
      stream.Position = 0;
      this.image = StripQuickRouteHeader(stream);
    }

    protected Map(SerializationInfo info, StreamingContext context)
    {
      source = info.GetString("source");
      sourceType = (MapSourceType)(info.GetValue("sourceType", typeof(MapSourceType)));
      storageType = (MapStorageType)(info.GetValue("storageType", typeof(MapStorageType)));
      switch (storageType)
      {
        case MapStorageType.Inline:
          //rawData = (byte[])(info.GetValueNo("rawData", typeof(byte[])));
          var getValueNoThrowMethod = info.GetType().GetMethod("GetValueNoThrow", BindingFlags.Instance | BindingFlags.NonPublic);
          if(getValueNoThrowMethod == null)
            rawData = (byte[])(info.GetValue("rawData", typeof(byte[])));
          else
            rawData = (byte[])getValueNoThrowMethod.Invoke(info, new object[] { "rawData", typeof(byte[]) });


          if (rawData != null)
          {
            // version 2.3 file format contains rawData field, create image from it
            using (var ms = new MemoryStream(rawData))
            {
              image = (Bitmap)System.Drawing.Image.FromStream(ms);
            }
          }
          else
          {
            // version 2.2 file format
            image = (Bitmap)(info.GetValue("image", typeof(Bitmap)));
          }
          break;

        case MapStorageType.Reference:
          switch (sourceType)
          {
            case MapSourceType.FileSystem:
              image = (Bitmap)System.Drawing.Image.FromFile(source);
              break;
            case MapSourceType.Url:
              image = GetImageFromUrl(source);
              break;
          }
          break;
      }
    }

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
      info.AddValue("source", source);
      info.AddValue("sourceType", sourceType, typeof(MapSourceType));
      info.AddValue("storageType", storageType, typeof(MapStorageType));
      if(storageType == MapStorageType.Inline)
      {
        // saving bitmap object in version 2.2 and earlier
        if (rawData == null)
        {
          info.AddValue("image", image);
        }
        else
        {
          // from version 2.3, save byte array instead to decrease file size when map image is in jpeg format
          // this breaks backward compatibility, but the advantage of a small file size is more important in my opinion
          info.AddValue("rawData", rawData);
        }
      }
    }

    public MapStorageType StorageType
    {
      get { return storageType; }
      set { storageType = value; }
    }

    public MapSourceType SourceType
    {
      get { return sourceType; }
    }

    public Bitmap Image
    {
      get { return image; }
    }

    public string Source
    {
      get { return source; }
    }

    /// <summary>
    /// Rotates the map image
    /// </summary>
    /// <param name="angle">Rotation angle, radians counterclockwise</param>
    public void RotateImage(double angle)
    {
      Graphics g = Graphics.FromImage(image);
      Matrix t = new Matrix();
      t.RotateAt((float)(angle * Math.PI / 180), new PointF((float)image.Width / 2, (float)image.Height / 2));
      g.Transform = t;
      g.DrawImage(image, 0, 0); 
    }

    private static Bitmap GetImageFromUrl(string url)
    {
      var request = WebRequest.Create(url);
      var response = (HttpWebResponse)request.GetResponse();
      var dataStream = response.GetResponseStream();
      var imageFromUrl = (Bitmap)System.Drawing.Image.FromStream(dataStream);
      dataStream.Close();
      response.Close();
      return imageFromUrl;
    }

    private static Stream GetImageStreamFromUrl(string url)
    {
      var request = WebRequest.Create(url);
      var response = (HttpWebResponse)request.GetResponse();
      return response.GetResponseStream();
    }

  }

  /// <summary>
  /// The type of storage of the map image when the map object is serialized.
  /// </summary>
  public enum MapStorageType
  {
    /// <summary>
    /// The map image is stored as a byte array in the object itself
    /// </summary>
    Inline,
    /// <summary>
    /// The map image is stored as a reference to a file or url
    /// </summary>
    Reference
  }

  /// <summary>
  /// The location type of the map image file.
  /// </summary>
  public enum MapSourceType
  {
    /// <summary>
    /// The source is a file in the file system
    /// </summary>
    FileSystem,
    /// <summary>
    /// The source is a file on an internet url 
    /// </summary>
    Url
  }
}