Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2287 | - | 1 | |
2 | namespace GMap.NET.WindowsPresentation |
||
3 | { |
||
4 | using System.Collections.Generic; |
||
5 | using System.Collections.ObjectModel; |
||
6 | using System.Diagnostics; |
||
7 | using System.Windows; |
||
8 | using System.Windows.Media; |
||
9 | using System.Windows.Media.Imaging; |
||
10 | using GMap.NET.Internals; |
||
11 | using GMap.NET.MapProviders; |
||
12 | |||
13 | /// <summary> |
||
14 | /// image abstraction |
||
15 | /// </summary> |
||
16 | public class GMapImage : PureImage |
||
17 | { |
||
18 | public ImageSource Img; |
||
19 | |||
20 | public override void Dispose() |
||
21 | { |
||
22 | if(Img != null) |
||
23 | { |
||
24 | Img = null; |
||
25 | } |
||
26 | |||
27 | if(Data != null) |
||
28 | { |
||
29 | Data.Dispose(); |
||
30 | Data = null; |
||
31 | } |
||
32 | } |
||
33 | } |
||
34 | |||
35 | /// <summary> |
||
36 | /// image abstraction proxy |
||
37 | /// </summary> |
||
38 | public class GMapImageProxy : PureImageProxy |
||
39 | { |
||
40 | GMapImageProxy() |
||
41 | { |
||
42 | |||
43 | } |
||
44 | |||
45 | public static void Enable() |
||
46 | { |
||
47 | GMapProvider.TileImageProxy = Instance; |
||
48 | } |
||
49 | |||
50 | public static readonly GMapImageProxy Instance = new GMapImageProxy(); |
||
51 | |||
52 | //static readonly byte[] pngHeader = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; |
||
53 | //static readonly byte[] jpgHeader = { 0xFF, 0xD8, 0xFF }; |
||
54 | //static readonly byte[] gifHeader = { 0x47, 0x49, 0x46 }; |
||
55 | //static readonly byte[] bmpHeader = { 0x42, 0x4D }; |
||
56 | |||
57 | public override PureImage FromStream(System.IO.Stream stream) |
||
58 | { |
||
59 | GMapImage ret = null; |
||
60 | if(stream != null) |
||
61 | { |
||
62 | var type = stream.ReadByte(); |
||
63 | stream.Position = 0; |
||
64 | |||
65 | ImageSource m = null; |
||
66 | |||
67 | switch(type) |
||
68 | { |
||
69 | // PNG: 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A |
||
70 | case 0x89: |
||
71 | { |
||
72 | var bitmapDecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.OnLoad); |
||
73 | m = bitmapDecoder.Frames[0]; |
||
74 | bitmapDecoder = null; |
||
75 | } |
||
76 | break; |
||
77 | |||
78 | // JPG: 0xFF, 0xD8, 0xFF |
||
79 | case 0xFF: |
||
80 | { |
||
81 | var bitmapDecoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.OnLoad); |
||
82 | m = bitmapDecoder.Frames[0]; |
||
83 | bitmapDecoder = null; |
||
84 | } |
||
85 | break; |
||
86 | |||
87 | // GIF: 0x47, 0x49, 0x46 |
||
88 | case 0x47: |
||
89 | { |
||
90 | var bitmapDecoder = new GifBitmapDecoder(stream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.OnLoad); |
||
91 | m = bitmapDecoder.Frames[0]; |
||
92 | bitmapDecoder = null; |
||
93 | } |
||
94 | break; |
||
95 | |||
96 | // BMP: 0x42, 0x4D |
||
97 | case 0x42: |
||
98 | { |
||
99 | var bitmapDecoder = new BmpBitmapDecoder(stream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.OnLoad); |
||
100 | m = bitmapDecoder.Frames[0]; |
||
101 | bitmapDecoder = null; |
||
102 | } |
||
103 | break; |
||
104 | |||
105 | // TIFF: 0x49, 0x49 || 0x4D, 0x4D |
||
106 | case 0x49: |
||
107 | case 0x4D: |
||
108 | { |
||
109 | var bitmapDecoder = new TiffBitmapDecoder(stream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.OnLoad); |
||
110 | m = bitmapDecoder.Frames[0]; |
||
111 | bitmapDecoder = null; |
||
112 | } |
||
113 | break; |
||
114 | |||
115 | default: |
||
116 | { |
||
117 | Debug.WriteLine("WindowsPresentationImageProxy: unknown image format: " + type); |
||
118 | } |
||
119 | break; |
||
120 | } |
||
121 | |||
122 | if(m != null) |
||
123 | { |
||
124 | ret = new GMapImage(); |
||
125 | ret.Img = m; |
||
126 | if(ret.Img.CanFreeze) |
||
127 | { |
||
128 | ret.Img.Freeze(); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | return ret; |
||
133 | } |
||
134 | |||
135 | public override bool Save(System.IO.Stream stream, PureImage image) |
||
136 | { |
||
137 | GMapImage ret = (GMapImage)image; |
||
138 | if(ret.Img != null) |
||
139 | { |
||
140 | try |
||
141 | { |
||
142 | PngBitmapEncoder e = new PngBitmapEncoder(); |
||
143 | e.Frames.Add(BitmapFrame.Create(ret.Img as BitmapSource)); |
||
144 | e.Save(stream); |
||
145 | e = null; |
||
146 | } |
||
147 | catch |
||
148 | { |
||
149 | return false; |
||
150 | } |
||
151 | } |
||
152 | else |
||
153 | { |
||
154 | return false; |
||
155 | } |
||
156 | |||
157 | return true; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | //internal class TileVisual : FrameworkElement |
||
162 | //{ |
||
163 | // public readonly ObservableCollection<ImageSource> Source; |
||
164 | // public readonly RawTile Tile; |
||
165 | |||
166 | // public TileVisual(IEnumerable<ImageSource> src, RawTile tile) |
||
167 | // { |
||
168 | // Opacity = 0; |
||
169 | // Tile = tile; |
||
170 | |||
171 | // Source = new ObservableCollection<ImageSource>(src); |
||
172 | // Source.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Source_CollectionChanged); |
||
173 | |||
174 | // this.Loaded += new RoutedEventHandler(ImageVisual_Loaded); |
||
175 | // this.Unloaded += new RoutedEventHandler(ImageVisual_Unloaded); |
||
176 | // } |
||
177 | |||
178 | // void Source_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
||
179 | // { |
||
180 | // if (IsLoaded) |
||
181 | // { |
||
182 | // switch (e.Action) |
||
183 | // { |
||
184 | // case System.Collections.Specialized.NotifyCollectionChangedAction.Add: |
||
185 | // case System.Collections.Specialized.NotifyCollectionChangedAction.Move: |
||
186 | // case System.Collections.Specialized.NotifyCollectionChangedAction.Remove: |
||
187 | // case System.Collections.Specialized.NotifyCollectionChangedAction.Replace: |
||
188 | // { |
||
189 | // UpdateVisual(); |
||
190 | // } |
||
191 | // break; |
||
192 | |||
193 | // case System.Collections.Specialized.NotifyCollectionChangedAction.Reset: |
||
194 | // { |
||
195 | // Child = null; |
||
196 | // } |
||
197 | // break; |
||
198 | // } |
||
199 | // } |
||
200 | // } |
||
201 | |||
202 | // void ImageVisual_Unloaded(object sender, RoutedEventArgs e) |
||
203 | // { |
||
204 | // Child = null; |
||
205 | // } |
||
206 | |||
207 | // void ImageVisual_Loaded(object sender, RoutedEventArgs e) |
||
208 | // { |
||
209 | // UpdateVisual(); |
||
210 | // } |
||
211 | |||
212 | // Visual _child; |
||
213 | // public virtual Visual Child |
||
214 | // { |
||
215 | // get |
||
216 | // { |
||
217 | // return _child; |
||
218 | // } |
||
219 | // set |
||
220 | // { |
||
221 | // if (_child != value) |
||
222 | // { |
||
223 | // if (_child != null) |
||
224 | // { |
||
225 | // RemoveLogicalChild(_child); |
||
226 | // RemoveVisualChild(_child); |
||
227 | // } |
||
228 | |||
229 | // if (value != null) |
||
230 | // { |
||
231 | // AddVisualChild(value); |
||
232 | // AddLogicalChild(value); |
||
233 | // } |
||
234 | |||
235 | // // cache the new child |
||
236 | // _child = value; |
||
237 | |||
238 | // InvalidateVisual(); |
||
239 | // } |
||
240 | // } |
||
241 | // } |
||
242 | |||
243 | // public void UpdateVisual() |
||
244 | // { |
||
245 | // Child = Create(); |
||
246 | // } |
||
247 | |||
248 | // static readonly Pen gridPen = new Pen(Brushes.White, 2.0); |
||
249 | |||
250 | // private DrawingVisual Create() |
||
251 | // { |
||
252 | // var dv = new DrawingVisual(); |
||
253 | |||
254 | // using (DrawingContext dc = dv.RenderOpen()) |
||
255 | // { |
||
256 | // foreach (var img in Source) |
||
257 | // { |
||
258 | // var rect = new Rect(0, 0, img.Width + 0.6, img.Height + 0.6); |
||
259 | |||
260 | // dc.DrawImage(img, rect); |
||
261 | // dc.DrawRectangle(null, gridPen, rect); |
||
262 | // } |
||
263 | // } |
||
264 | |||
265 | // return dv; |
||
266 | // } |
||
267 | |||
268 | // #region Necessary Overrides -- Needed by WPF to maintain bookkeeping of our hosted visuals |
||
269 | // protected override int VisualChildrenCount |
||
270 | // { |
||
271 | // get |
||
272 | // { |
||
273 | // return (Child == null ? 0 : 1); |
||
274 | // } |
||
275 | // } |
||
276 | |||
277 | // protected override Visual GetVisualChild(int index) |
||
278 | // { |
||
279 | // Debug.Assert(index == 0); |
||
280 | // return Child; |
||
281 | // } |
||
282 | // #endregion |
||
283 | //} |
||
284 | } |