프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Common / ImageSourceHelper.cs @ b60e844d

이력 | 보기 | 이력해설 | 다운로드 (11.3 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));
68
        }
69

    
70
        public static BitmapImage GetDownloadImage(Uri uri)
71
        {
72
            BitmapImage resultImage = new BitmapImage();
73

    
74
            resultImage.BeginInit();
75
            resultImage.CacheOption = BitmapCacheOption.OnLoad;
76
            resultImage.UriCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.Default);
77
            resultImage.UriSource = uri;
78
            resultImage.EndInit();
79

    
80
            resultImage.DownloadProgress += (ex, arg) =>
81
            {
82
                System.Diagnostics.Debug.WriteLine($"download Image : {arg.Progress}%");
83
            };
84

    
85
            while (resultImage.IsDownloading)
86
            {
87
                System.Windows.Forms.Application.DoEvents(); //Method from thread linked above
88
                System.Diagnostics.Debug.WriteLine("doEvent");
89
            }
90

    
91
            resultImage.Freeze();
92
            return resultImage;
93
        }
94

    
95
        public static byte[] CopyPixels(BitmapSource image)
96
        {
97
            byte[] result = new byte[image.PixelWidth * image.PixelHeight * 4];
98
            image.CopyPixels(result, image.PixelWidth * 4, 0);
99

    
100
            return result;
101
        }
102

    
103
        public static byte[] SetBrightness(int brightness, byte[] pixels)
104
        {
105
            for (int i = 0; i < pixels.Length / 4; ++i)
106
            {
107
                byte b = pixels[i * 4];
108
                byte g = pixels[i * 4 + 1];
109
                byte r = pixels[i * 4 + 2];
110
                byte a = pixels[i * 4 + 3];
111

    
112
                if (r != 255 || g != 255 || b != 255 || a == 255)
113
                {
114
                    b = (byte)(b + brightness);
115
                    g = (byte)(g + brightness);
116
                    r = (byte)(r + brightness);
117
                    ///a = (byte)(a + brightness);
118

    
119
                    if (r < 0) r = 1;
120
                    if (r > 255) r = 255;
121

    
122
                    if (g < 0) g = 1;
123
                    if (g > 255) g = 255;
124

    
125
                    if (b < 0) b = 1;
126
                    if (b > 255) b = 255;
127

    
128
                    pixels[i * 4] = b;
129
                    pixels[i * 4 + 1] = g;
130
                    pixels[i * 4 + 2] = r;
131
                }
132
            }
133

    
134
            return pixels;
135
        }
136

    
137
        public static byte[] SetBrightnessAndContrast(int brightness,double contrast, byte[] pixels)
138
        {
139
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
140
            stopwatch.Start();
141

    
142
            if (contrast < -100) contrast = -100;
143
            if (contrast > 100) contrast = 100;
144
            contrast = (100.0 + contrast) / 100.0;
145
            contrast *= contrast;
146

    
147
            for (int i = 0; i < pixels.Length / 4; ++i)
148
            {
149
                byte b = pixels[i * 4];
150
                byte g = pixels[i * 4 + 1];
151
                byte r = pixels[i * 4 + 2];
152
                byte a = pixels[i * 4 + 3];
153

    
154
                //System.Diagnostics.Debug.WriteLineIf(i< 4, $"B :{b} G : {g} r : {r}");
155

    
156
                if (r != 255 || g != 255 || b != 255)
157
                {
158
                    b = (byte)(b + brightness);
159
                    g = (byte)(g + brightness);
160
                    r = (byte)(r + brightness);
161
                    ///a = (byte)(a + brightness);
162

    
163
                    if (r < 0) r = 1;
164
                    if (r > 255) r = 255;
165

    
166
                    if (g < 0) g = 1;
167
                    if (g > 255) g = 255;
168

    
169
                    if (b < 0) b = 1;
170
                    if (b > 255) b = 255;
171

    
172
                    double pR = r / 255.0;
173
                    pR -= 0.5;
174
                    pR *= contrast;
175
                    pR += 0.5;
176
                    pR *= 255;
177
                    if (pR < 0) pR = 0;
178
                    if (pR > 255) pR = 255;
179

    
180
                    double pG = g / 255.0;
181
                    pG -= 0.5;
182
                    pG *= contrast;
183
                    pG += 0.5;
184
                    pG *= 255;
185
                    if (pG < 0) pG = 0;
186
                    if (pG > 255) pG = 255;
187

    
188
                    double pB = b / 255.0;
189
                    pB -= 0.5;
190
                    pB *= contrast;
191
                    pB += 0.5;
192
                    pB *= 255;
193
                    if (pB < 0) pB = 0;
194
                    if (pB > 255) pB = 255;
195

    
196
                    pixels[i * 4] = (byte)pB;
197
                    pixels[i * 4 + 1] = (byte)pG;
198
                    pixels[i * 4 + 2] = (byte)pR;
199
              }
200
            }
201

    
202
            System.Diagnostics.Debug.WriteLine($"일반 : {stopwatch.Elapsed.ToString()}");
203

    
204
            return pixels;
205
        }
206

    
207
        public static byte[] SetBrightnessAndContrastLinp(int brightness, double contrast, byte[] pixels)
208
        {
209
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
210
            stopwatch.Start();
211

    
212
            if (contrast < -100) contrast = -100;
213

    
214
            if (contrast > 100) contrast = 100;
215

    
216
            contrast = (100.0 + contrast) / 100.0;
217

    
218
            contrast *= contrast;
219

    
220
            Parallel.ForEach(SteppedIntegerList(pixels,0, pixels.Length, 4,4),new ParallelOptions {MaxDegreeOfParallelism = 5 }, item=>
221
            {
222
                var pixel = item.ToList();
223

    
224
                byte b = pixel[0].Value;
225
                byte g = pixel[1].Value;
226
                byte r = pixel[2].Value;
227
                byte a = pixel[3].Value;
228

    
229
                //System.Diagnostics.Debug.WriteLineIf(pixel[0].Key < 4, $"B :{b} G : {g} r : {r}");
230

    
231
                if (r != 255 || g != 255 || b != 255)
232
                {
233
                    b = (byte)(b + brightness);
234
                    g = (byte)(g + brightness);
235
                    r = (byte)(r + brightness);
236
                    ///a = (byte)(a + brightness);
237

    
238
                    if (r < 0) r = 1;
239
                    if (r > 255) r = 255;
240

    
241
                    if (g < 0) g = 1;
242
                    if (g > 255) g = 255;
243

    
244
                    if (b < 0) b = 1;
245
                    if (b > 255) b = 255;
246

    
247
                    double pR = r / 255.0;
248
                    pR -= 0.5;
249
                    pR *= contrast;
250
                    pR += 0.5;
251
                    pR *= 255;
252
                    if (pR < 0) pR = 0;
253
                    if (pR > 255) pR = 255;
254

    
255
                    double pG = g / 255.0;
256
                    pG -= 0.5;
257
                    pG *= contrast;
258
                    pG += 0.5;
259
                    pG *= 255;
260
                    if (pG < 0) pG = 0;
261
                    if (pG > 255) pG = 255;
262

    
263
                    double pB = b / 255.0;
264
                    pB -= 0.5;
265
                    pB *= contrast;
266
                    pB += 0.5;
267
                    pB *= 255;
268
                    if (pB < 0) pB = 0;
269
                    if (pB > 255) pB = 255;
270

    
271
                    pixels[pixel[0].Key] = (byte)pB;
272
                    pixels[pixel[1].Key] = (byte)pG;
273
                    pixels[pixel[2].Key] = (byte)pR;
274
                }
275
            });
276

    
277
            System.Diagnostics.Debug.WriteLine($"Linq : {stopwatch.Elapsed.ToString()}");
278

    
279
            return pixels;
280
        }
281

    
282
        public static IEnumerable<Dictionary<int,T>> SteppedIntegerList<T>(T[] array, int startIndex,int endEndex, int stepSize,int TakeSize)
283
        {
284
            for (int i = startIndex; i < endEndex; i += stepSize)
285
            {
286
                Dictionary<int, T> result = new Dictionary<int, T>();
287

    
288
                for (int j = 0; j < TakeSize; j++)
289
                {
290
                    result.Add(j, array[i + j]);
291
                }
292
                
293
                yield return result;
294
            }
295
        }
296

    
297
        public static byte[] SetContrast(double contrast, byte[] pixels)
298
        {
299
            if (contrast < -100) contrast = -100;
300
            if (contrast > 100) contrast = 100;
301
            contrast = (100.0 + contrast) / 100.0;
302
            contrast *= contrast;
303

    
304
            for (int i = 0; i < pixels.Length / 4; ++i)
305
            {
306
                byte b = pixels[i * 4];
307
                byte g = pixels[i * 4 + 1];
308
                byte r = pixels[i * 4 + 2];
309
                byte a = pixels[i * 4 + 3];
310

    
311
                if (r != 255 || g != 255 || b != 255 || a == 255)
312
                {
313
                    double pR = r / 255.0;
314
                    pR -= 0.5;
315
                    pR *= contrast;
316
                    pR += 0.5;
317
                    pR *= 255;
318
                    if (pR < 0) pR = 0;
319
                    if (pR > 255) pR = 255;
320

    
321
                    double pG = g / 255.0;
322
                    pG -= 0.5;
323
                    pG *= contrast;
324
                    pG += 0.5;
325
                    pG *= 255;
326
                    if (pG < 0) pG = 0;
327
                    if (pG > 255) pG = 255;
328

    
329
                    double pB = b / 255.0;
330
                    pB -= 0.5;
331
                    pB *= contrast;
332
                    pB += 0.5;
333
                    pB *= 255;
334
                    if (pB < 0) pB = 0;
335
                    if (pB > 255) pB = 255;
336

    
337
                    pixels[i * 4] = (byte)pB;
338
                    pixels[i * 4 + 1] = (byte)pG;
339
                    pixels[i * 4 + 2] = (byte)pR;
340
                }
341
            }
342

    
343
            return pixels;
344
        }
345
    }
346
}
클립보드 이미지 추가 (최대 크기: 500 MB)