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