markus / KCOM / Common / ImageSourceHelper.cs @ 3212270d
이력 | 보기 | 이력해설 | 다운로드 (11.7 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Net.Http; |
5 |
using System.Text; |
6 |
using System.Threading.Tasks; |
7 |
using System.Windows.Media; |
8 |
using System.Windows.Media.Imaging; |
9 |
|
10 |
namespace KCOM.Common |
11 |
{ |
12 |
public static class ImageSourceHelper |
13 |
{ |
14 |
public static async System.Threading.Tasks.Task<BitmapImage> GetDownloadImageAsync(Uri uri, double pageWidth, double pageHeight) |
15 |
{ |
16 |
System.Net.Http.HttpClient httpClient = null; |
17 |
System.IO.Stream responseStream = null; |
18 |
System.IO.MemoryStream memoryStream = null; |
19 |
BitmapImage image = null; |
20 |
|
21 |
try |
22 |
{ |
23 |
using (httpClient = new System.Net.Http.HttpClient()) |
24 |
{ |
25 |
using (responseStream = await httpClient.GetStreamAsync(uri)) |
26 |
{ |
27 |
using (memoryStream = new System.IO.MemoryStream()) |
28 |
{ |
29 |
System.Diagnostics.Debug.WriteLine(uri); |
30 |
|
31 |
await responseStream.CopyToAsync(memoryStream); |
32 |
|
33 |
memoryStream.Position = 0; |
34 |
|
35 |
image = new BitmapImage(); |
36 |
image.BeginInit(); |
37 |
|
38 |
//image.CreateOptions = BitmapCreateOptions.None; |
39 |
image.CacheOption = BitmapCacheOption.OnLoad; |
40 |
image.UriSource = uri; |
41 |
image.StreamSource = memoryStream; |
42 |
image.DecodePixelWidth = (int)pageWidth; |
43 |
image.DecodePixelHeight = (int)pageHeight; |
44 |
image.EndInit(); |
45 |
image.Freeze(); |
46 |
System.Diagnostics.Debug.WriteLine("Freeze : " + uri); |
47 |
} |
48 |
} |
49 |
} |
50 |
} |
51 |
catch (Exception ex) |
52 |
{ |
53 |
throw new Exception("Image Load Error ", ex); |
54 |
} |
55 |
finally |
56 |
{ |
57 |
httpClient?.Dispose(); |
58 |
responseStream?.Dispose(); |
59 |
memoryStream?.Dispose(); |
60 |
} |
61 |
|
62 |
return image; |
63 |
} |
64 |
|
65 |
public static BitmapImage GetDownloadImage(string uri) |
66 |
{ |
67 |
return GetDownloadImage(new Uri(uri),null); |
68 |
} |
69 |
public static BitmapImage GetDownloadImage(string uri,System.Windows.Size DecodeSize) |
70 |
{ |
71 |
return GetDownloadImage(new Uri(uri), DecodeSize); |
72 |
} |
73 |
|
74 |
public static BitmapImage GetDownloadImage(Uri uri, System.Windows.Size? DecodeSize) |
75 |
{ |
76 |
BitmapImage resultImage = new BitmapImage(); |
77 |
|
78 |
resultImage.BeginInit(); |
79 |
|
80 |
if(DecodeSize != null) |
81 |
{ |
82 |
resultImage.DecodePixelWidth = (int)DecodeSize.Value.Width; |
83 |
resultImage.DecodePixelHeight = (int)DecodeSize.Value.Height; |
84 |
} |
85 |
|
86 |
resultImage.CacheOption = BitmapCacheOption.OnLoad; |
87 |
resultImage.UriCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.Default); |
88 |
resultImage.UriSource = uri; |
89 |
resultImage.EndInit(); |
90 |
|
91 |
resultImage.DownloadProgress += (ex, arg) => |
92 |
{ |
93 |
System.Diagnostics.Debug.WriteLine($"download Image : {arg.Progress}%"); |
94 |
}; |
95 |
|
96 |
while (resultImage.IsDownloading) |
97 |
{ |
98 |
System.Windows.Forms.Application.DoEvents(); //Method from thread linked above |
99 |
System.Diagnostics.Debug.WriteLine("doEvent"); |
100 |
} |
101 |
|
102 |
resultImage.Freeze(); |
103 |
return resultImage; |
104 |
} |
105 |
|
106 |
public static byte[] CopyPixels(BitmapSource image) |
107 |
{ |
108 |
byte[] result = new byte[image.PixelWidth * image.PixelHeight * 4]; |
109 |
image.CopyPixels(result, image.PixelWidth * 4, 0); |
110 |
|
111 |
return result; |
112 |
} |
113 |
|
114 |
public static byte[] SetBrightness(int brightness, byte[] pixels) |
115 |
{ |
116 |
for (int i = 0; i < pixels.Length / 4; ++i) |
117 |
{ |
118 |
byte b = pixels[i * 4]; |
119 |
byte g = pixels[i * 4 + 1]; |
120 |
byte r = pixels[i * 4 + 2]; |
121 |
byte a = pixels[i * 4 + 3]; |
122 |
|
123 |
if (r != 255 || g != 255 || b != 255 || a == 255) |
124 |
{ |
125 |
b = (byte)(b + brightness); |
126 |
g = (byte)(g + brightness); |
127 |
r = (byte)(r + brightness); |
128 |
///a = (byte)(a + brightness); |
129 |
|
130 |
if (r < 0) r = 1; |
131 |
if (r > 255) r = 255; |
132 |
|
133 |
if (g < 0) g = 1; |
134 |
if (g > 255) g = 255; |
135 |
|
136 |
if (b < 0) b = 1; |
137 |
if (b > 255) b = 255; |
138 |
|
139 |
pixels[i * 4] = b; |
140 |
pixels[i * 4 + 1] = g; |
141 |
pixels[i * 4 + 2] = r; |
142 |
} |
143 |
} |
144 |
|
145 |
return pixels; |
146 |
} |
147 |
|
148 |
public static byte[] SetBrightnessAndContrast(int brightness,double contrast, byte[] pixels) |
149 |
{ |
150 |
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); |
151 |
stopwatch.Start(); |
152 |
|
153 |
if (contrast < -100) contrast = -100; |
154 |
if (contrast > 100) contrast = 100; |
155 |
contrast = (100.0 + contrast) / 100.0; |
156 |
contrast *= contrast; |
157 |
|
158 |
for (int i = 0; i < pixels.Length / 4; ++i) |
159 |
{ |
160 |
byte b = pixels[i * 4]; |
161 |
byte g = pixels[i * 4 + 1]; |
162 |
byte r = pixels[i * 4 + 2]; |
163 |
byte a = pixels[i * 4 + 3]; |
164 |
|
165 |
//System.Diagnostics.Debug.WriteLineIf(i< 4, $"B :{b} G : {g} r : {r}"); |
166 |
|
167 |
if (r != 255 || g != 255 || b != 255) |
168 |
{ |
169 |
b = (byte)(b + brightness); |
170 |
g = (byte)(g + brightness); |
171 |
r = (byte)(r + brightness); |
172 |
///a = (byte)(a + brightness); |
173 |
|
174 |
if (r < 0) r = 1; |
175 |
if (r > 255) r = 255; |
176 |
|
177 |
if (g < 0) g = 1; |
178 |
if (g > 255) g = 255; |
179 |
|
180 |
if (b < 0) b = 1; |
181 |
if (b > 255) b = 255; |
182 |
|
183 |
double pR = r / 255.0; |
184 |
pR -= 0.5; |
185 |
pR *= contrast; |
186 |
pR += 0.5; |
187 |
pR *= 255; |
188 |
if (pR < 0) pR = 0; |
189 |
if (pR > 255) pR = 255; |
190 |
|
191 |
double pG = g / 255.0; |
192 |
pG -= 0.5; |
193 |
pG *= contrast; |
194 |
pG += 0.5; |
195 |
pG *= 255; |
196 |
if (pG < 0) pG = 0; |
197 |
if (pG > 255) pG = 255; |
198 |
|
199 |
double pB = b / 255.0; |
200 |
pB -= 0.5; |
201 |
pB *= contrast; |
202 |
pB += 0.5; |
203 |
pB *= 255; |
204 |
if (pB < 0) pB = 0; |
205 |
if (pB > 255) pB = 255; |
206 |
|
207 |
pixels[i * 4] = (byte)pB; |
208 |
pixels[i * 4 + 1] = (byte)pG; |
209 |
pixels[i * 4 + 2] = (byte)pR; |
210 |
} |
211 |
} |
212 |
|
213 |
System.Diagnostics.Debug.WriteLine($"일반 : {stopwatch.Elapsed.ToString()}"); |
214 |
|
215 |
return pixels; |
216 |
} |
217 |
|
218 |
public static byte[] SetBrightnessAndContrastLinp(int brightness, double contrast, byte[] pixels) |
219 |
{ |
220 |
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); |
221 |
stopwatch.Start(); |
222 |
|
223 |
if (contrast < -100) contrast = -100; |
224 |
|
225 |
if (contrast > 100) contrast = 100; |
226 |
|
227 |
contrast = (100.0 + contrast) / 100.0; |
228 |
|
229 |
contrast *= contrast; |
230 |
|
231 |
Parallel.ForEach(SteppedIntegerList(pixels,0, pixels.Length, 4,4),new ParallelOptions {MaxDegreeOfParallelism = 5 }, item=> |
232 |
{ |
233 |
var pixel = item.ToList(); |
234 |
|
235 |
byte b = pixel[0].Value; |
236 |
byte g = pixel[1].Value; |
237 |
byte r = pixel[2].Value; |
238 |
byte a = pixel[3].Value; |
239 |
|
240 |
//System.Diagnostics.Debug.WriteLineIf(pixel[0].Key < 4, $"B :{b} G : {g} r : {r}"); |
241 |
|
242 |
if (r != 255 || g != 255 || b != 255) |
243 |
{ |
244 |
b = (byte)(b + brightness); |
245 |
g = (byte)(g + brightness); |
246 |
r = (byte)(r + brightness); |
247 |
///a = (byte)(a + brightness); |
248 |
|
249 |
if (r < 0) r = 1; |
250 |
if (r > 255) r = 255; |
251 |
|
252 |
if (g < 0) g = 1; |
253 |
if (g > 255) g = 255; |
254 |
|
255 |
if (b < 0) b = 1; |
256 |
if (b > 255) b = 255; |
257 |
|
258 |
double pR = r / 255.0; |
259 |
pR -= 0.5; |
260 |
pR *= contrast; |
261 |
pR += 0.5; |
262 |
pR *= 255; |
263 |
if (pR < 0) pR = 0; |
264 |
if (pR > 255) pR = 255; |
265 |
|
266 |
double pG = g / 255.0; |
267 |
pG -= 0.5; |
268 |
pG *= contrast; |
269 |
pG += 0.5; |
270 |
pG *= 255; |
271 |
if (pG < 0) pG = 0; |
272 |
if (pG > 255) pG = 255; |
273 |
|
274 |
double pB = b / 255.0; |
275 |
pB -= 0.5; |
276 |
pB *= contrast; |
277 |
pB += 0.5; |
278 |
pB *= 255; |
279 |
if (pB < 0) pB = 0; |
280 |
if (pB > 255) pB = 255; |
281 |
|
282 |
pixels[pixel[0].Key] = (byte)pB; |
283 |
pixels[pixel[1].Key] = (byte)pG; |
284 |
pixels[pixel[2].Key] = (byte)pR; |
285 |
} |
286 |
}); |
287 |
|
288 |
System.Diagnostics.Debug.WriteLine($"Linq : {stopwatch.Elapsed.ToString()}"); |
289 |
|
290 |
return pixels; |
291 |
} |
292 |
|
293 |
public static IEnumerable<Dictionary<int,T>> SteppedIntegerList<T>(T[] array, int startIndex,int endEndex, int stepSize,int TakeSize) |
294 |
{ |
295 |
for (int i = startIndex; i < endEndex; i += stepSize) |
296 |
{ |
297 |
Dictionary<int, T> result = new Dictionary<int, T>(); |
298 |
|
299 |
for (int j = 0; j < TakeSize; j++) |
300 |
{ |
301 |
result.Add(j, array[i + j]); |
302 |
} |
303 |
|
304 |
yield return result; |
305 |
} |
306 |
} |
307 |
|
308 |
public static byte[] SetContrast(double contrast, byte[] pixels) |
309 |
{ |
310 |
if (contrast < -100) contrast = -100; |
311 |
if (contrast > 100) contrast = 100; |
312 |
contrast = (100.0 + contrast) / 100.0; |
313 |
contrast *= contrast; |
314 |
|
315 |
for (int i = 0; i < pixels.Length / 4; ++i) |
316 |
{ |
317 |
byte b = pixels[i * 4]; |
318 |
byte g = pixels[i * 4 + 1]; |
319 |
byte r = pixels[i * 4 + 2]; |
320 |
byte a = pixels[i * 4 + 3]; |
321 |
|
322 |
if (r != 255 || g != 255 || b != 255 || a == 255) |
323 |
{ |
324 |
double pR = r / 255.0; |
325 |
pR -= 0.5; |
326 |
pR *= contrast; |
327 |
pR += 0.5; |
328 |
pR *= 255; |
329 |
if (pR < 0) pR = 0; |
330 |
if (pR > 255) pR = 255; |
331 |
|
332 |
double pG = g / 255.0; |
333 |
pG -= 0.5; |
334 |
pG *= contrast; |
335 |
pG += 0.5; |
336 |
pG *= 255; |
337 |
if (pG < 0) pG = 0; |
338 |
if (pG > 255) pG = 255; |
339 |
|
340 |
double pB = b / 255.0; |
341 |
pB -= 0.5; |
342 |
pB *= contrast; |
343 |
pB += 0.5; |
344 |
pB *= 255; |
345 |
if (pB < 0) pB = 0; |
346 |
if (pB > 255) pB = 255; |
347 |
|
348 |
pixels[i * 4] = (byte)pB; |
349 |
pixels[i * 4 + 1] = (byte)pG; |
350 |
pixels[i * 4 + 2] = (byte)pR; |
351 |
} |
352 |
} |
353 |
|
354 |
return pixels; |
355 |
} |
356 |
} |
357 |
} |