프로젝트

일반

사용자정보

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

markus / KCOM / Common / ImageSourceHelper.cs @ 1ed8da93

이력 | 보기 | 이력해설 | 다운로드 (10 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
                            System.Diagnostics.Debug.WriteLine("이미지 다운로드");
29
30
                            await responseStream.CopyToAsync(memoryStream);
31
32
                            image = new BitmapImage();
33
                            image.BeginInit();
34
                            image.CacheOption = BitmapCacheOption.OnLoad;
35
                            image.StreamSource = memoryStream;
36
                            image.DecodePixelWidth = (int)pageWidth;
37
                            image.DecodePixelHeight = (int)pageHeight;
38
                            image.EndInit();
39
                            image.Freeze();
40
                            return image;
41
                        }
42
                    }
43
                }
44
            }
45
            catch (Exception ex)
46
            {
47
                throw new Exception("Image Load Error ", ex);
48
            }
49
            finally
50
            {
51
                httpClient?.Dispose();
52
                responseStream?.Dispose();
53
                memoryStream?.Dispose();
54
                image = null;
55
            }
56
        }
57 566f0526 taeseongkim
58
        public static byte[] CopyPixels(BitmapSource image)
59
        {
60
            byte[] result = new byte[image.PixelWidth * image.PixelHeight * 4];
61
            image.CopyPixels(result, image.PixelWidth * 4, 0);
62
63
            return result;
64
        }
65
66
        public static byte[] SetBrightness(int brightness, byte[] pixels)
67
        {
68
            for (int i = 0; i < pixels.Length / 4; ++i)
69
            {
70
                byte b = pixels[i * 4];
71
                byte g = pixels[i * 4 + 1];
72
                byte r = pixels[i * 4 + 2];
73
                byte a = pixels[i * 4 + 3];
74
75
                if (r != 255 || g != 255 || b != 255 || a == 255)
76
                {
77
                    b = (byte)(b + brightness);
78
                    g = (byte)(g + brightness);
79
                    r = (byte)(r + brightness);
80
                    ///a = (byte)(a + brightness);
81
82
                    if (r < 0) r = 1;
83
                    if (r > 255) r = 255;
84
85
                    if (g < 0) g = 1;
86
                    if (g > 255) g = 255;
87
88
                    if (b < 0) b = 1;
89
                    if (b > 255) b = 255;
90
91
                    pixels[i * 4] = b;
92
                    pixels[i * 4 + 1] = g;
93
                    pixels[i * 4 + 2] = r;
94
                }
95
            }
96
97
            return pixels;
98
        }
99
100
        public static byte[] SetBrightnessAndContrast(int brightness,double contrast, byte[] pixels)
101
        {
102 997071b8 taeseongkim
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
103
            stopwatch.Start();
104
105 566f0526 taeseongkim
            if (contrast < -100) contrast = -100;
106
            if (contrast > 100) contrast = 100;
107
            contrast = (100.0 + contrast) / 100.0;
108
            contrast *= contrast;
109
110
            for (int i = 0; i < pixels.Length / 4; ++i)
111
            {
112
                byte b = pixels[i * 4];
113
                byte g = pixels[i * 4 + 1];
114
                byte r = pixels[i * 4 + 2];
115
                byte a = pixels[i * 4 + 3];
116
117 997071b8 taeseongkim
                //System.Diagnostics.Debug.WriteLineIf(i< 4, $"B :{b} G : {g} r : {r}");
118
119
                if (r != 255 || g != 255 || b != 255)
120 566f0526 taeseongkim
                {
121
                    b = (byte)(b + brightness);
122
                    g = (byte)(g + brightness);
123
                    r = (byte)(r + brightness);
124
                    ///a = (byte)(a + brightness);
125
126
                    if (r < 0) r = 1;
127
                    if (r > 255) r = 255;
128
129
                    if (g < 0) g = 1;
130
                    if (g > 255) g = 255;
131
132
                    if (b < 0) b = 1;
133
                    if (b > 255) b = 255;
134
135
                    double pR = r / 255.0;
136
                    pR -= 0.5;
137
                    pR *= contrast;
138
                    pR += 0.5;
139
                    pR *= 255;
140
                    if (pR < 0) pR = 0;
141
                    if (pR > 255) pR = 255;
142
143
                    double pG = g / 255.0;
144
                    pG -= 0.5;
145
                    pG *= contrast;
146
                    pG += 0.5;
147
                    pG *= 255;
148
                    if (pG < 0) pG = 0;
149
                    if (pG > 255) pG = 255;
150
151
                    double pB = b / 255.0;
152
                    pB -= 0.5;
153
                    pB *= contrast;
154
                    pB += 0.5;
155
                    pB *= 255;
156
                    if (pB < 0) pB = 0;
157
                    if (pB > 255) pB = 255;
158
159
                    pixels[i * 4] = (byte)pB;
160
                    pixels[i * 4 + 1] = (byte)pG;
161
                    pixels[i * 4 + 2] = (byte)pR;
162 fc9da8df taeseongkim
              }
163 566f0526 taeseongkim
            }
164
165 997071b8 taeseongkim
            System.Diagnostics.Debug.WriteLine($"일반 : {stopwatch.Elapsed.ToString()}");
166
167
            return pixels;
168
        }
169
170
        public static byte[] SetBrightnessAndContrastLinp(int brightness, double contrast, byte[] pixels)
171
        {
172
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
173
            stopwatch.Start();
174
175
            if (contrast < -100) contrast = -100;
176
177
            if (contrast > 100) contrast = 100;
178
179
            contrast = (100.0 + contrast) / 100.0;
180
181
            contrast *= contrast;
182
183
            Parallel.ForEach(SteppedIntegerList(pixels,0, pixels.Length, 4,4),new ParallelOptions {MaxDegreeOfParallelism = 5 }, item=>
184
            {
185
                var pixel = item.ToList();
186
187
                byte b = pixel[0].Value;
188
                byte g = pixel[1].Value;
189
                byte r = pixel[2].Value;
190
                byte a = pixel[3].Value;
191
192
                //System.Diagnostics.Debug.WriteLineIf(pixel[0].Key < 4, $"B :{b} G : {g} r : {r}");
193
194
                if (r != 255 || g != 255 || b != 255)
195
                {
196
                    b = (byte)(b + brightness);
197
                    g = (byte)(g + brightness);
198
                    r = (byte)(r + brightness);
199
                    ///a = (byte)(a + brightness);
200
201
                    if (r < 0) r = 1;
202
                    if (r > 255) r = 255;
203
204
                    if (g < 0) g = 1;
205
                    if (g > 255) g = 255;
206
207
                    if (b < 0) b = 1;
208
                    if (b > 255) b = 255;
209
210
                    double pR = r / 255.0;
211
                    pR -= 0.5;
212
                    pR *= contrast;
213
                    pR += 0.5;
214
                    pR *= 255;
215
                    if (pR < 0) pR = 0;
216
                    if (pR > 255) pR = 255;
217
218
                    double pG = g / 255.0;
219
                    pG -= 0.5;
220
                    pG *= contrast;
221
                    pG += 0.5;
222
                    pG *= 255;
223
                    if (pG < 0) pG = 0;
224
                    if (pG > 255) pG = 255;
225
226
                    double pB = b / 255.0;
227
                    pB -= 0.5;
228
                    pB *= contrast;
229
                    pB += 0.5;
230
                    pB *= 255;
231
                    if (pB < 0) pB = 0;
232
                    if (pB > 255) pB = 255;
233
234
                    pixels[pixel[0].Key] = (byte)pB;
235
                    pixels[pixel[1].Key] = (byte)pG;
236
                    pixels[pixel[2].Key] = (byte)pR;
237
                }
238
            });
239
240
            System.Diagnostics.Debug.WriteLine($"Linq : {stopwatch.Elapsed.ToString()}");
241
242 566f0526 taeseongkim
            return pixels;
243
        }
244
245 997071b8 taeseongkim
        public static IEnumerable<Dictionary<int,T>> SteppedIntegerList<T>(T[] array, int startIndex,int endEndex, int stepSize,int TakeSize)
246
        {
247
            for (int i = startIndex; i < endEndex; i += stepSize)
248
            {
249
                Dictionary<int, T> result = new Dictionary<int, T>();
250
251
                for (int j = 0; j < TakeSize; j++)
252
                {
253
                    result.Add(j, array[i + j]);
254
                }
255
                
256
                yield return result;
257
            }
258
        }
259
260 566f0526 taeseongkim
        public static byte[] SetContrast(double contrast, byte[] pixels)
261
        {
262
            if (contrast < -100) contrast = -100;
263
            if (contrast > 100) contrast = 100;
264
            contrast = (100.0 + contrast) / 100.0;
265
            contrast *= contrast;
266
267
            for (int i = 0; i < pixels.Length / 4; ++i)
268
            {
269
                byte b = pixels[i * 4];
270
                byte g = pixels[i * 4 + 1];
271
                byte r = pixels[i * 4 + 2];
272
                byte a = pixels[i * 4 + 3];
273
274
                if (r != 255 || g != 255 || b != 255 || a == 255)
275
                {
276
                    double pR = r / 255.0;
277
                    pR -= 0.5;
278
                    pR *= contrast;
279
                    pR += 0.5;
280
                    pR *= 255;
281
                    if (pR < 0) pR = 0;
282
                    if (pR > 255) pR = 255;
283
284
                    double pG = g / 255.0;
285
                    pG -= 0.5;
286
                    pG *= contrast;
287
                    pG += 0.5;
288
                    pG *= 255;
289
                    if (pG < 0) pG = 0;
290
                    if (pG > 255) pG = 255;
291
292
                    double pB = b / 255.0;
293
                    pB -= 0.5;
294
                    pB *= contrast;
295
                    pB += 0.5;
296
                    pB *= 255;
297
                    if (pB < 0) pB = 0;
298
                    if (pB > 255) pB = 255;
299
300
                    pixels[i * 4] = (byte)pB;
301
                    pixels[i * 4 + 1] = (byte)pG;
302
                    pixels[i * 4 + 2] = (byte)pR;
303
                }
304
            }
305
306
            return pixels;
307
        }
308 cdfb57ff taeseongkim
    }
309
}
클립보드 이미지 추가 (최대 크기: 500 MB)