/
ImageCrooper.cs
387 lines (345 loc) · 17.9 KB
/
ImageCrooper.cs
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Diagnostics;
namespace Migracja
{
abstract class Crooper
{
public Size panelSize;
private int fCenterX;
private int fCenterY;
public Bitmap srcBmp;
protected int srcBmpWidth;
protected int srcBmpHeight;
public MapFactory mapFactory;
public int centerX
{
get { return fCenterX; }
set
{
fCenterX = (int)Math.Max(0, value);
fCenterX = (int)Math.Min(fCenterX, srcBmpWidth);
}
}
public int centerY
{
get { return fCenterY; }
set
{
fCenterY = (int)Math.Max(0, value);
fCenterY = (int)Math.Min(fCenterY, srcBmpHeight);
}
}
public int left;
public int top;
public Crooper(Size aPanelSize, int aSrcBmpHeight, int aSrcBmpWidth)
{
panelSize = aPanelSize;
centerX = (int)Math.Round(aPanelSize.Width / (float)2);
centerY = (int)Math.Round(aPanelSize.Height / (float)2);
srcBmpHeight = aSrcBmpHeight;
srcBmpWidth = aSrcBmpWidth;
}
public int GetLeftAbsoluteOryg(float aScale)
{
return centerX - (int)Math.Ceiling(1.5 * panelSize.Width / aScale);
}
public int GetTopAbsoluteOryg(float aScale)
{
return centerY - (int)Math.Ceiling(1.5 * panelSize.Height / aScale);
}
public int GetRightAbsoluteOryg(float aScale)
{
return GetLeftAbsoluteOryg(aScale) + (int)Math.Ceiling(3 * panelSize.Width / aScale);
}
public int GetBottomAbsoluteOryg(float aScale)
{
return GetTopAbsoluteOryg(aScale) + (int)Math.Ceiling(3 * panelSize.Height / aScale);
}
//ustala jaki wycinek oryginalnego obrazu ma zostać pokazany
public Rectangle GetSourceRectangle(float aScale)
{
//aby x,y nie były mniejsze niż punkt (0,0) - ograniczenie o lewej strony.
//Od prawej strony nie wyjdą poza ramy obrazu, bo x zawsze będzie < centerX,
//które to ma założony warunek
int x = Math.Max(0, GetLeftAbsoluteOryg(aScale));
int y = Math.Max(0, GetTopAbsoluteOryg(aScale));
//ustalanie rozmiaru, tak aby dla mocnego przesuniącia wycinka w lewo pobrał tylko część wycinka
int rectWidth = Math.Min(srcBmpWidth, GetRightAbsoluteOryg(aScale)) - Math.Max(GetLeftAbsoluteOryg(aScale), x);
int rectHeight = Math.Min(srcBmpHeight, GetBottomAbsoluteOryg(aScale)) - Math.Max(GetTopAbsoluteOryg(aScale), y);
return new Rectangle(x, y, rectWidth, rectHeight);
}
//ustala jak ma być położony oryginalny wycinek w nowopokazywanym fragmencie. Możliwe jest, że wycinek będzie
//przesunięty pokazując wolną przestrzeń na jednym z brzegów fragmentu.
public Rectangle GetDestinationRectangle(float aScale, Rectangle srcRectangle)
{
int resultRectX;
int resultRectY;
//jeśli wycinek jest mniejszy i ma być odsunięty od boku obrazu (sam wycinek był przyciśnięty do boku, więc
//prawdopodobnie postła wolna przestrzeń po lewej stronie do wypełnienia. Prawa soraona "wypełni"
//się automatycznie, bo mając przerwę z lewej strony i wklejony obrazek, przerwa po prawej stworzy się samoczynnie)
if ((srcRectangle.Width * aScale < 3 * panelSize.Width) && (srcRectangle.X == 0))
resultRectX = (int)Math.Ceiling(-GetLeftAbsoluteOryg(aScale) * aScale);
//GetAbsolute... oddaje wartości dla oryginalnej wielkości/skala, a my
//obliczamy teraz przesunięcie na rozciągniętym obrazie, więc trzeba
//pomnożyć to przez skalę
else
resultRectX = 0;
if ((srcRectangle.Height * aScale < 3 * panelSize.Height) && (srcRectangle.Y == 0))
resultRectY = (int)Math.Ceiling(-GetTopAbsoluteOryg(aScale) * aScale);
else
resultRectY = 0;
return new Rectangle(resultRectX,
resultRectY,
(int)Math.Ceiling(srcRectangle.Width * aScale),
(int)Math.Ceiling(srcRectangle.Height * aScale)
);
}
public abstract Bitmap GetCroppedImage(float aScale, UpdateInfoBoxTimeDelegate aFunct = null);
}
class RaserImageCrooper: Crooper
{
public RaserImageCrooper(Size aPanelSize, int aCenterX, int aCenterY, Bitmap aSrcBmp)
: base(aPanelSize, aSrcBmp.Height, aSrcBmp.Width)
{
srcBmp = aSrcBmp;
centerX = aCenterX;
centerY = aCenterY;
}
public override Bitmap GetCroppedImage(float aScale, UpdateInfoBoxTimeDelegate aFunct = null)
{
Rectangle rect = GetSourceRectangle(aScale);
Bitmap result;
Rectangle resultRect = GetDestinationRectangle(aScale, rect);
//finalna bitmapa o odpowiednim rozmiarze
result = new Bitmap(3 * panelSize.Width, 3 * panelSize.Height);
//ustawia, że result będzie płótnem graphics
Graphics graphics = Graphics.FromImage(result);
//ustawia sposób zmiękczania przy powiększaniu - NN da brak zmiękczania
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
//przepisze źródło na cel odpowiednio skalująć. W tym przypadku źódło i cel to result
//nowy obszar jest większy od startego więc nastąpi rozciągnięcie pixeli zgodnie z InterpolationMode
graphics.DrawImage(srcBmp,
resultRect, //nowy obszar
rect, //originalny obszar
System.Drawing.GraphicsUnit.Pixel);
return result;
}
}
/*class RaserImageCrooper : Crooper
{
public RaserImageCrooper(Size aPanelSize, Bitmap aSrcBmp)
: base(aPanelSize, aSrcBmp.Height, aSrcBmp.Width)
{
srcBmp = aSrcBmp;
}
public override Bitmap GetCroppedImage(float aScale, UpdateInfoBoxTimeDelegate aFunct = null)
{
Rectangle rect = GetSourceRectangle(aScale);
Bitmap result;
Rectangle resultRect = GetDestinationRectangle(aScale, rect);
//finalna bitmapa o odpowiednim rozmiarze
result = new Bitmap(3 * panelSize.Width, 3 * panelSize.Height);
//ustawia, że result będzie płótnem graphics
Graphics graphics = Graphics.FromImage(result);
//ustawia sposób zmiękczania przy powiększaniu - NN da brak zmiękczania
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
//przepisze źródło na cel odpowiednio skalująć. W tym przypadku źódło i cel to result
//nowy obszar jest większy od startego więc nastąpi rozciągnięcie pixeli zgodnie z InterpolationMode
graphics.DrawImage(srcBmp,
resultRect, //nowy obszar
rect, //originalny obszar
System.Drawing.GraphicsUnit.Pixel);
return result;
}
}*/
class VectorImageCrooper : Crooper
{
MainWindowSettings settings;
public VectorImageCrooper(Size aPanelSize, MapFactory aMapFactory, int aCenterX, int aCenterY, MainWindowSettings aSettings, Bitmap aSrcBmp)
: base(aPanelSize, aSrcBmp.Height, aSrcBmp.Width)
{
mapFactory = aMapFactory;
settings = aSettings;
if (aMapFactory != null)
{
srcBmpWidth = aMapFactory.vectArr.Length;
Debug.Assert(aMapFactory.vectArr[0] != null, "aMapFactory.vectArr[0] jest null");
srcBmpHeight = aMapFactory.vectArr[0].Length;
}
centerX = aCenterX;
centerY = aCenterY;
}
public override Bitmap GetCroppedImage(float aScale, UpdateInfoBoxTimeDelegate aFunct = null)
{
DateTime dtStart = DateTime.Now;
Rectangle rect = GetSourceRectangle(aScale);
Bitmap result;
Rectangle resultRect = GetDestinationRectangle(aScale, rect);
//Rectangle resultRect = new Rectangle(0, 0, (int)Math.Round(srcBmpWidth * aScale), (int)Math.Round(srcBmpHeight * aScale));
//finalna bitmapa o odpowiednim rozmiarze
result = new Bitmap(3 * panelSize.Width, 3 * panelSize.Height);
Graphics graphics = Graphics.FromImage(result);
//Brush h = new Brush();
//Pen localPen = new Pen(new Brush() );
Vector_Rectangle[][] vectArr = mapFactory.vectArr;
aFunct(" Faza1", false, dtStart);
//rysowanie rectangli odzwierciedlających pixele wzorcowego obrazka
if (!settings.Polygons() || aScale == 1)
{
//będziemy poruszać się po ustalonym wycinku wzorcowego obrazu
for (int x = 0; x < rect.Width; x++)
{
for (int y = 0; y < rect.Height; y++)
{
int sourceX = x + rect.X;
int sourceY = y + rect.Y;
graphics.FillRectangle(new SolidBrush(vectArr[sourceX][sourceY].color), (x) * aScale + resultRect.X, (y) * aScale + resultRect.Y, aScale, aScale);
if (settings.Edges() && aScale >= 4)
graphics.DrawRectangle(new Pen(Color.Red), (x) * aScale + resultRect.X, (y) * aScale + resultRect.Y, aScale, aScale);
//graphics.DrawRectangle(localPen, x/**aScale*/, y/**aScale*/, /*aScale, aScale*/ 100, 100);
//result.SetPixel(x + resultRect.X, y + resultRect.Y, vectArr[x][y].color);
/*for (int xScale = 0; xScale < aScale; xScale++)
{
for (int yScale = 0; yScale < aScale; yScale++)
{
result.SetPixel(x + resultRect.X, y + resultRect.Y, vectArr[x][y].color);
}
} */
}
}
}
//rysowanie polygonów
else
{
Color tmpColor = new Color();
int tmpLineR;
int tmpLineG;
int tmpLineB;
int tmpPointR;
int tmpPointG;
int tmpPointB;
int tmpPointBorderR;
int tmpPointBorderG;
int tmpPointBorderB;
Boolean[][] usedArray = new Boolean[rect.Width + rect.X][];
for (int i = 0; i < rect.Width + rect.X; i++)
{
usedArray[i] = new Boolean[rect.Height + rect.Y];
for (int j = 0; j < rect.Height; j++)
{
usedArray[i][j] = false;
}
}
//będziemy poruszać się po ustalonym wycinku wzorcowego obrazu
for (int x = 0; x < rect.Width; x++)
{
for (int y = 0; y < rect.Height; y++)
{
int sourceX = x + rect.X;
int sourceY = y + rect.Y;
VectoredRectangleGroup group = mapFactory.GetGroupByXY(sourceX, sourceY, usedArray);
//foreach (VectoredRectangleGroup group in mapFactory.Values)
if(group != null)
{
PointAdv[] granica;
if (settings.ShowSimplifiedEdge())
{
Debug.Assert(group.pointAdvMapFromSimplifiedEdge != null,
"Tablica pointArrFromSimplifiedEdge nie została zainicjalizowana.");
Debug.Assert(group.pointAdvMapFromSimplifiedEdge.Length != 0,
"Tablica pointArrFromSimplifiedEdge jest pusta.");
granica = group.GetScaledPointArrFromSimplifiedEdge(aScale,
resultRect.X - rect.X * aScale,
resultRect.Y - rect.Y * aScale);
}
else
{
Debug.Assert(group.pointAdvMapFromFullEdge != null,
"Tablica pointArrFromFullEdge nie została zainicjalizowana.");
Debug.Assert(group.pointAdvMapFromFullEdge.Length != 0,
"Tablica pointArrFromFullEdge jest pusta.");
granica = group.GetScaledPointArrFromFullEdge(aScale,
resultRect.X - rect.X * aScale,
resultRect.Y - rect.Y * aScale);
}
Point[] pointArr = GetPointArr(granica);
if (pointArr.Count() < 2)
continue;
graphics.FillPolygon(new SolidBrush(group.sourceColor), pointArr);
if (settings.Edges())
{
if (!settings.TestColor())
{
Math.DivRem(group.sourceColor.R + 50, 256, out tmpLineR);
Math.DivRem(group.sourceColor.G + 50, 256, out tmpLineG);
Math.DivRem(group.sourceColor.B + 50, 256, out tmpLineB);
Math.DivRem(2 * group.sourceColor.R, 256, out tmpPointR);
Math.DivRem(2 * group.sourceColor.G, 256, out tmpPointG);
Math.DivRem(2 * group.sourceColor.B, 256, out tmpPointB);
Math.DivRem(2 * group.sourceColor.R + 150, 256, out tmpPointBorderR);
Math.DivRem(2 * group.sourceColor.G + 150, 256, out tmpPointBorderG);
Math.DivRem(2 * group.sourceColor.B + 150, 256, out tmpPointBorderB);
}
else
{
tmpLineR = 255;
tmpLineG = 0;
tmpLineB = 0;
tmpPointR = 100;
tmpPointG = 255;
tmpPointB = 255;
tmpPointBorderR = 100;
tmpPointBorderG = 255;
tmpPointBorderB = 255;
}
graphics.DrawPolygon(new Pen(Color.FromArgb(tmpLineR, tmpLineG, tmpLineB)), pointArr);
foreach (PointAdv p in granica)
{
//graphics.DrawLine(new Pen(Color.Blue), p, p);
if (p.X < result.Width && p.Y < result.Height && p.X > 0 && p.Y > 0)
if (p.GetKdPointType() == Cst.c_geoPxSimple)
result.SetPixel(p.X, p.Y, Color.FromArgb(tmpPointR, tmpPointG, tmpPointB));
else
result.SetPixel(p.X, p.Y, Color.FromArgb(tmpPointBorderR, tmpPointBorderG, tmpPointBorderB));
}
}
//group.edgeList
}
}
}
}
/*
//ustawia, że result będzie płótnem graphics
Graphics graphics = Graphics.FromImage(result);
//ustawia sposób zmiękczania przy powiększaniu - NN da brak zmiękczania
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
//przepisze źródło na cel odpowiednio skalująć. W tym przypadku źódło i cel to result
//nowy obszar jest większy od startego więc nastąpi rozciągnięcie pixeli zgodnie z InterpolationMode
graphics.DrawImage(srcBmp,
resultRect, //nowy obszar
rect, //originalny obszar
System.Drawing.GraphicsUnit.Pixel);*/
return result;
}
private Point[] GetPointArr(PointAdv[] aArr)
{
Point[] result = new Point[aArr.Length];
int i=0;
foreach (PointAdv pointAdv in aArr)
{
if (pointAdv is ScaledPointAdv)
result[i] = ((ScaledPointAdv)pointAdv).GetPoint();
else
result[i] = pointAdv.GetPoint();
i++;
}
return result;
}
}
}