forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGdiImageLayerProxy.cs
More file actions
259 lines (231 loc) · 8.84 KB
/
GdiImageLayerProxy.cs
File metadata and controls
259 lines (231 loc) · 8.84 KB
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
// Copyright 2014 - Robert Smart (www.cnl-software), Felix Obermaier (www.ivv-aachen.de)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Common.Logging;
using GeoAPI.Geometries;
using SharpMap.Data;
using SharpMap.Styles;
namespace SharpMap.Layers
{
/// <summary>
/// Image manipulation proxy layer
/// </summary>
/// <remarks>Note: This layer is not for layers implementing <see cref="ITileAsyncLayer"/>.</remarks>
/// <typeparam name="T">The type of the proxy layer. <see cref="ITileAsyncLayer"/> are not excluded, but are not handled in any way.</typeparam>
// ToDo: think of a better name:
// - ImageManipulationProxyLayer
// - What to do with large images
[Serializable]
public class GdiImageLayerProxy<T> : ICanQueryLayer, IDisposable
where T: class, ILayer
{
/// <summary>
/// Creates a proxy class that transforms all colors to grey scale
/// </summary>
/// <param name="baseLayer">The layer to be proxied</param>
/// <returns>A proxy layer</returns>
public static GdiImageLayerProxy<T> CreateGreyScale(T baseLayer)
{
var colorMatrix = new ColorMatrix(
new[]
{
new[] {.3f, .3f, .3f, 0, 0},
new[] {.59f, .59f, .59f, 0, 0},
new[] {.11f, .11f, .11f, 0, 0},
new[] {0f, 0, 0, 1, 0},
new[] {0f, 0, 0, 0, 1}
});
return new GdiImageLayerProxy<T>(baseLayer, colorMatrix);
}
private static readonly ILog Logger = LogManager.GetLogger(typeof(GdiImageLayerProxy<T>));
private readonly ColorMatrix _colorMatrix;
private readonly ColorMap[] _colorMap;
private readonly T _baseLayer;
/// <summary>
/// Creates an instance of this class using the provided <paramref name="opacity"/>
/// </summary>
/// <param name="layer">The layer to be proxied</param>
/// <param name="opacity">An opacity value in the range of [0f, 1f]. Values outside of that range will be clipped.</param>
public GdiImageLayerProxy(T layer, float opacity)
: this(layer, new ColorMatrix {Matrix33 = Math.Max(Math.Min(1f, opacity), 0f)})
{
}
/// <summary>
/// Creates an instance of this class using the provided <paramref name="colorMatrix"/>
/// </summary>
/// <param name="layer">The layer to be proxied</param>
/// <param name="colorMatrix">A color matrix that is to be applied upon drawing</param>
public GdiImageLayerProxy(T layer, ColorMatrix colorMatrix)
{
if (layer is ITileAsyncLayer)
{
Logger.Warn("ITileAsyncLayer is not a valid layer for GdiImageLayerProxy<T>");
}
_baseLayer = layer;
_colorMatrix = colorMatrix;
VisibilityUnits = VisibilityUnits.ZoomLevel;
}
/// <summary>
/// Creates an instance of this class using the provided <paramref name="colorMap"/>
/// </summary>
/// <param name="layer">The layer to be proxied</param>
/// <param name="colorMap">The color map</param>
public GdiImageLayerProxy(T layer, params ColorMap[] colorMap)
{
if (layer is ITileAsyncLayer)
{
Logger.Warn("ITileAsyncLayer is not a valid layer for GdiImageLayerProxy<T>");
}
_baseLayer = layer;
_colorMap = colorMap;
VisibilityUnits = VisibilityUnits.ZoomLevel;
}
/// <summary>
/// Gets a value indicating the proxied base layer
/// </summary>
public T BaseLayer{ get { return _baseLayer; }}
double ILayer.MinVisible
{
get { return _baseLayer.MinVisible; }
set { _baseLayer.MinVisible = value; }
}
double ILayer.MaxVisible
{
get { return _baseLayer.MaxVisible; }
set { _baseLayer.MaxVisible = value; }
}
/// <summary>
/// Gets or Sets what level-reference the Min/Max values are defined in
/// </summary>
public VisibilityUnits VisibilityUnits
{
get => _baseLayer.VisibilityUnits;
set => _baseLayer.VisibilityUnits = value;
}
bool ILayer.Enabled
{
get { return _baseLayer.Enabled; }
set { _baseLayer.Enabled = value; }
}
/// <summary>
/// Optional title of layer. It will be used for services like WMS where both Name and Title are supported.
/// </summary>
public string LayerTitle
{
get { return _baseLayer.LayerTitle; }
set { _baseLayer.LayerTitle = value; }
}
string ILayer.LayerName
{
get { return _baseLayer.LayerName; }
set { _baseLayer.LayerName = value; }
}
Envelope ILayer.Envelope
{
get { return _baseLayer.Envelope; }
}
int ILayer.SRID
{
get { return _baseLayer.SRID; }
set { _baseLayer.SRID = value; }
}
int ILayer.TargetSRID
{
get { return _baseLayer.TargetSRID; }
}
string ILayer.Proj4Projection
{
get { return _baseLayer.Proj4Projection; }
set { _baseLayer.Proj4Projection = value; }
}
void ILayer.Render(Graphics g, Map map)
{
((ILayer)this).Render(g, (MapViewport)map);
}
void ILayer.Render(Graphics g, MapViewport map)
{
if (_baseLayer is ITileAsyncLayer)
{
Logger.Warn("ITileAsyncLayer is not a valid layer for GdiImageLayerProxy<T>. -> Skipping");
_baseLayer.Render(g, map);
return;
}
var s = map.Size;
using (var img = new Bitmap(s.Width, s.Height, PixelFormat.Format32bppArgb))
{
using (var gg = Graphics.FromImage(img))
{
_baseLayer.Render(gg, map);
}
using (var ia = CreateImageAttributes())
{
g.DrawImage(img, new Rectangle(0, 0, s.Width, s.Height),
0, 0, s.Width, s.Height, GraphicsUnit.Pixel, ia);
}
}
}
private ImageAttributes CreateImageAttributes()
{
var ia = new ImageAttributes();
if (_colorMatrix != null)
{
ia.SetColorMatrix(_colorMatrix);
}
else if (_colorMap != null)
{
ia.SetRemapTable(_colorMap);
}
return ia;
}
/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
if (_baseLayer is IDisposable disposable)
disposable.Dispose();
}
void ICanQueryLayer.ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds)
{
if (_baseLayer is ICanQueryLayer cqLayer)
{
cqLayer.ExecuteIntersectionQuery(box, ds);
}
}
void ICanQueryLayer.ExecuteIntersectionQuery(IGeometry geometry, FeatureDataSet ds)
{
if (_baseLayer is ICanQueryLayer cqLayer)
{
cqLayer.ExecuteIntersectionQuery(geometry, ds);
}
}
bool ICanQueryLayer.IsQueryEnabled
{
get
{
if (_baseLayer is ICanQueryLayer cqLayer)
return cqLayer.IsQueryEnabled;
return false;
}
set
{
if (_baseLayer is ICanQueryLayer cqLayer)
cqLayer.IsQueryEnabled = value;
}
}
}
}