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
| using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Null.PhantomTank.Library;
namespace Null.PhantomTank
{
public static class PhantomTank
{
/// 像素运算方式 变量 : 源色: Xc Yc, 输出: Za Zc
/// 其中: c表示Color, 即颜色亮度, a代表Alpha通道, 即不透明度
/// 备注: 值全部为0~1的比值
///
/// Expr: | Xc = Za * Zc + 1 - Za
/// | Yc = Za * Zc
///
/// Calc: | Xc = Yc + 1 - Za
/// | Yc = Xc + Za - 1
/// | Za = Yc - Xc + 1
///
/// Need: | 1 >= Yc + 1 - Za >= 0, 0 >= Yc - Za >= -1; Then: | Za >= Yc
/// | 1 >= Xc + Za - 1 >= 0, 2 >= Xc + Za >= 1; Then: | Xc + Za >= 1
/// | 1 >= Yc - Xc + 1 >= 0, 0 >= Yc - Xc >= -1; Then: | Xc >= Yc
///
/// Root: | Zc = Yc / Za
/// | Za = Yc - Xc + 1
///
/// Basic Root: | Zc = Yc / (Za / 255) = Yc * 255 / Za
/// | Za = Yc - Xc + 255
private static Color CalcPixel(Color src1, Color src2, float src1ColorRatio = 0.5f)
{
float src2ColorRatio = 1 - src1ColorRatio;
int
xc = (int)((src1.R + src1.G + src1.B) * src1ColorRatio / 3 + src2ColorRatio * 255),
yc = (int)((src2.R + src2.G + src2.B) * src2ColorRatio / 3);
int
za = yc - xc + 255,
zc = za == 0 ? 0 : (yc * 255 / za);
return Color.FromArgb(za, zc, zc, zc);
}
private static float ConvertRatio(float ratio)
{
return ratio / (ratio + 1);
}
public const float DefaultRatio = 1;
public static Bitmap ResizeBitmap(Bitmap src, Color bgColor, ResizeMode resize, int newWidth, int newHeight)
{
Size srcSize = src.Size;
int srcWidth = srcSize.Width,
srcHeight = srcSize.Height;
Bitmap result = new Bitmap(newWidth, newHeight, src.PixelFormat);
Graphics rstG = Graphics.FromImage(result);
rstG.Clear(bgColor);
Size destSize;
Rectangle srcRect, destRect;
switch(resize)
{
case ResizeMode.NoResize:
destRect = new Rectangle((newWidth - srcWidth) / 2, (newHeight - srcHeight) / 2, srcWidth, srcHeight);
rstG.DrawImageUnscaled(src, destRect);
break;
case ResizeMode.Stretch:
srcRect = new Rectangle(0, 0, srcWidth, srcHeight);
destRect = new Rectangle(0, 0, newWidth, newHeight);
rstG.DrawImage(src, destRect, srcRect, GraphicsUnit.Pixel);
break;
case ResizeMode.Uniform:
srcRect = new Rectangle(0, 0, srcWidth, srcHeight);
destSize = new Size(newWidth, srcHeight * newWidth / srcWidth);
if (destSize.Height > newHeight)
destSize = new Size(srcWidth * newHeight / srcHeight, newHeight);
destRect = new Rectangle(new Point((newWidth - destSize.Width) / 2, (newHeight - destSize.Height) / 2), destSize);
rstG.DrawImage(src, destRect, srcRect, GraphicsUnit.Pixel);
break;
case ResizeMode.UniformToFill:
srcRect = new Rectangle(0, 0, srcWidth, srcHeight);
destSize = new Size(newWidth, srcHeight * newWidth / srcWidth);
if (destSize.Height < newHeight)
destSize = new Size(srcWidth * newHeight / srcHeight, newHeight);
destRect = new Rectangle(new Point((newWidth - destSize.Width) / 2, (newHeight - destSize.Height) / 2), destSize);
rstG.DrawImage(src, destRect, srcRect, GraphicsUnit.Pixel);
break;
}
return result;
}
/// <summary>
/// 最基本的合成方法, 请保证图片尺寸是一致的
/// </summary>
/// <param name="src1">在白底下可以看到的图片</param>
/// <param name="src2">在黑底下可以看到的图片</param>
/// <returns>合并后的黑白图像</returns>
public static Bitmap BasicCombineBitmap(Bitmap src1, Bitmap src2, float colorRatio)
{
if (src1 == null || src2 == null)
throw new ArgumentNullException();
if (src1.Size != src2.Size)
throw new ArgumentOutOfRangeException();
Size srcSize = src1.Size;
int width = srcSize.Width, height = srcSize.Height;
Bitmap result = new Bitmap(width, height, src1.PixelFormat);
result.SetResolution(src1.HorizontalResolution, src1.VerticalResolution);
LockBitmap lbmp1 = new LockBitmap(src1);
LockBitmap lbmp2 = new LockBitmap(src2);
LockBitmap lrst = new LockBitmap(result);
float whiteRatio = ConvertRatio(colorRatio);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Color
srcPixel1 = lbmp1.GetPixel(j, i),
srcPixel2 = lbmp2.GetPixel(j, i),
outPixel = CalcPixel(srcPixel1, srcPixel2, whiteRatio);
lrst.SetPixel(j, i, outPixel);
}
}
lbmp1.UnlockBits();
lbmp2.UnlockBits();
lrst.UnlockBits();
return result;
}
/// <summary>
/// 转换图片
/// </summary>
/// <param name="src">源图</param>
/// <param name="tankType">坦克类型</param>
/// <returns>转换结果</returns>
public static Bitmap ConvertBitmap(Bitmap src, TankType tankType)
{
Size srcSize = src.Size;
int
srcWidth = srcSize.Width,
srcHeight = srcSize.Height;
Bitmap result = new Bitmap(srcWidth, srcHeight, src.PixelFormat);
result.SetResolution(src.HorizontalResolution, src.VerticalResolution);
LockBitmap lsrc = new LockBitmap(src);
LockBitmap lrst = new LockBitmap(result);
Func<int, Color> pixelCalcFunc;
switch (tankType)
{
case TankType.AppearOnBlack:
pixelCalcFunc = (srcPixel) => Color.FromArgb(srcPixel, 255, 255, 255);
break;
case TankType.AppearOnWhite:
pixelCalcFunc = (srcPixel) => Color.FromArgb(255 - srcPixel, 0, 0, 0);
break;
default:
throw new InvalidOperationException("Not supported.");
}
for (int i = 0; i < srcHeight; i++)
{
for (int j = 0; j < srcWidth; j++)
{
Color pixel = lsrc.GetPixel(j, i);
int light = (pixel.R + pixel.G + pixel.B) / 3;
lrst.SetPixel(j, i, pixelCalcFunc.Invoke(light));
}
}
lsrc.UnlockBits();
lrst.UnlockBits();
return result;
}
public static Bitmap ConvertImage(Image src, TankType tankType)
{
Bitmap newSrc = new Bitmap(src);
Bitmap result = ConvertBitmap(newSrc, tankType);
newSrc.Dispose();
return result;
}
public static Bitmap CombineBitmap(Bitmap src1, Bitmap src2, Color bgColor1, Color bgColor2, ResizeMode resize, float colorRatio)
{
Size
src1Size = src1.Size,
src2Size = src2.Size;
int src1Width = src1Size.Width,
src1Height = src1Size.Height,
src2Width = src2Size.Width,
src2Height = src2Size.Height;
int
maxWidth = src1Width > src2Width ? src1Width : src2Width,
maxHeight = src1Height > src2Height ? src1Height : src2Height;
Bitmap
newSrc1 = new Bitmap(maxWidth, maxHeight, src1.PixelFormat),
newSrc2 = new Bitmap(maxWidth, maxHeight, src2.PixelFormat);
Graphics
srcG1 = Graphics.FromImage(newSrc1),
srcG2 = Graphics.FromImage(newSrc2);
srcG1.Clear(bgColor1);
srcG2.Clear(bgColor2);
// 这里进行的是对图片的重新调整尺寸操作
switch(resize)
{
case ResizeMode.NoResize:
srcG1.DrawImageUnscaled(src1, (maxWidth - src1Width) / 2, (maxHeight - src1Height) / 2);
srcG2.DrawImageUnscaled(src2, (maxWidth - src2Width) / 2, (maxHeight - src2Height) / 2);
break;
case ResizeMode.Stretch:
srcG1.DrawImage(src1, new Rectangle(0, 0, maxWidth, maxHeight), new Rectangle(0, 0, src1Width, src1Height), GraphicsUnit.Pixel);
srcG2.DrawImage(src2, new Rectangle(0, 0, maxWidth, maxHeight), new Rectangle(0, 0, src2Width, src2Height), GraphicsUnit.Pixel);
break;
case ResizeMode.Uniform:
Size
scaleSize1 = new Size(maxWidth, (int)(src1Height * ((float)maxWidth / src1Width))),
scaleSize2 = new Size(maxWidth, (int)(src2Height * ((float)maxWidth / src2Width)));
if (scaleSize1.Height > maxHeight)
scaleSize1 = new Size((int)(src1Width * ((float)maxHeight / src1Height)), maxHeight);
if (scaleSize2.Height > maxHeight)
scaleSize2 = new Size((int)(src2Width * ((float)maxHeight / src2Height)), maxHeight);
srcG1.DrawImage(src1, new Rectangle(new Point((maxWidth - scaleSize1.Width) / 2, (maxHeight - scaleSize1.Height) / 2), scaleSize1), new Rectangle(0, 0, src1Width, src1Height), GraphicsUnit.Pixel);
srcG2.DrawImage(src2, new Rectangle(new Point((maxWidth - scaleSize2.Width) / 2, (maxHeight - scaleSize2.Height) / 2), scaleSize2), new Rectangle(0, 0, src2Width, src2Height), GraphicsUnit.Pixel);
break;
case ResizeMode.UniformToFill:
Size
scaleFillSize1 = new Size(maxWidth, (int)(src1Height * ((float)maxWidth / src1Width))),
scaleFillSize2 = new Size(maxWidth, (int)(src2Height * ((float)maxWidth / src2Width)));
if (scaleFillSize1.Height < maxHeight)
scaleFillSize1 = new Size((int)(src1Width * ((float)maxHeight / src1Height)), maxHeight);
if (scaleFillSize2.Height < maxHeight)
scaleFillSize2 = new Size((int)(src2Width * ((float)maxHeight / src2Height)), maxHeight);
srcG1.DrawImage(src1, new Rectangle(new Point((maxWidth - scaleFillSize1.Width) / 2, (maxHeight - scaleFillSize1.Height) / 2), scaleFillSize1), new Rectangle(0, 0, src1Width, src1Height), GraphicsUnit.Pixel);
srcG2.DrawImage(src2, new Rectangle(new Point((maxWidth - scaleFillSize2.Width) / 2, (maxHeight - scaleFillSize2.Height) / 2), scaleFillSize2), new Rectangle(0, 0, src2Width, src2Height), GraphicsUnit.Pixel);
break;
}
srcG1.Dispose();
srcG2.Dispose();
Bitmap result = BasicCombineBitmap(newSrc1, newSrc2, colorRatio); // 调用最终合成方法
newSrc1.Dispose();
newSrc2.Dispose();
return result;
}
public static Bitmap CombineBitmap(Bitmap src1, Bitmap src2, Color bgColor1, Color bgColor2, ResizeMode resize)
{
return CombineBitmap(src1, src2, Color.White, Color.Black, resize, DefaultRatio);
}
public static Bitmap CombineBitmap(Bitmap src1, Bitmap src2, Color bgColor1, Color bgColor2, float colorRatio)
{
return CombineBitmap(src1, src2, Color.White, Color.Black, ResizeMode.NoResize, DefaultRatio);
}
public static Bitmap CombineBitmap(Bitmap src1, Bitmap src2, ResizeMode resize, float colorRatio)
{
return CombineBitmap(src1, src2, Color.White, Color.Black, resize, colorRatio);
}
public static Bitmap CombineBitmap(Bitmap src1, Bitmap src2, ResizeMode resize)
{
return CombineBitmap(src1, src2, Color.White, Color.Black, resize, 0);
}
public static Bitmap CombineBitmap(Bitmap src1, Bitmap src2, float colorRatio)
{
return CombineBitmap(src1, src2, Color.White, Color.Black, ResizeMode.NoResize, colorRatio);
}
public static Bitmap CombineBitmap(Bitmap src1, Bitmap src2)
{
return CombineBitmap(src1, src2, Color.White, Color.Black, ResizeMode.NoResize, DefaultRatio);
}
public static Bitmap CombineImage(Image src1, Image src2, Color bgColor1, Color bgColor2, ResizeMode resize, float colorRatio)
{
Bitmap
newSrc1 = new Bitmap(src1),
newSrc2 = new Bitmap(src2);
Bitmap result = CombineBitmap(newSrc1, newSrc2, bgColor1, bgColor2, resize, colorRatio);
newSrc1.Dispose();
newSrc2.Dispose();
return result;
}
public static Bitmap CombineImage(Image src1, Image src2, Color bgColor1, Color bgColor2, ResizeMode resize)
{
return CombineImage(src1, src2, Color.White, Color.Black, resize, DefaultRatio);
}
public static Bitmap CombineImage(Image src1, Image src2, Color bgColor1, Color bgColor2, float colorRatio)
{
return CombineImage(src1, src2, Color.White, Color.Black, ResizeMode.NoResize, colorRatio);
}
public static Bitmap CombineImage(Image src1, Image src2, ResizeMode resize, float colorRatio)
{
return CombineImage(src1, src2, Color.White, Color.Black, resize, colorRatio);
}
public static Bitmap CombineImage(Image src1, Image src2, ResizeMode resize)
{
return CombineImage(src1, src2, Color.White, Color.Black, resize, DefaultRatio);
}
public static Bitmap CombineImage(Image src1, Image src2, float colorRatio)
{
return CombineImage(src1, src2, Color.White, Color.Black, ResizeMode.NoResize, colorRatio);
}
public static Bitmap CombineImage(Image src1, Image src2)
{
return CombineImage(src1, src2, Color.White, Color.Black, ResizeMode.NoResize, DefaultRatio);
}
}
public enum ResizeMode
{
NoResize,
Stretch,
Uniform,
UniformToFill,
}
public enum TankType
{
AppearOnBlack,
AppearOnWhite,
}
}
|