프로젝트

일반

사용자정보

개정판 566f0526

ID566f052670e186a3d794e2c1d1ff66350ff9fd17
상위 eb9a8cb6
하위 484bd949, 81173d69

김태성이(가) 5년 이상 전에 추가함

Image 밝기 & 색 농도 조절

Change-Id: I80feb7c7a64fbaf8d1d85a32bdf8baa813be2d4c

차이점 보기:

KCOM/Common/ImageSourceHelper.cs
54 54
                image = null;
55 55
            }
56 56
        }
57

  
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
            if (contrast < -100) contrast = -100;
103
            if (contrast > 100) contrast = 100;
104
            contrast = (100.0 + contrast) / 100.0;
105
            contrast *= contrast;
106

  
107
            for (int i = 0; i < pixels.Length / 4; ++i)
108
            {
109
                byte b = pixels[i * 4];
110
                byte g = pixels[i * 4 + 1];
111
                byte r = pixels[i * 4 + 2];
112
                byte a = pixels[i * 4 + 3];
113

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

  
121
                    if (r < 0) r = 1;
122
                    if (r > 255) r = 255;
123

  
124
                    if (g < 0) g = 1;
125
                    if (g > 255) g = 255;
126

  
127
                    if (b < 0) b = 1;
128
                    if (b > 255) b = 255;
129

  
130
                    double pR = r / 255.0;
131
                    pR -= 0.5;
132
                    pR *= contrast;
133
                    pR += 0.5;
134
                    pR *= 255;
135
                    if (pR < 0) pR = 0;
136
                    if (pR > 255) pR = 255;
137

  
138
                    double pG = g / 255.0;
139
                    pG -= 0.5;
140
                    pG *= contrast;
141
                    pG += 0.5;
142
                    pG *= 255;
143
                    if (pG < 0) pG = 0;
144
                    if (pG > 255) pG = 255;
145

  
146
                    double pB = b / 255.0;
147
                    pB -= 0.5;
148
                    pB *= contrast;
149
                    pB += 0.5;
150
                    pB *= 255;
151
                    if (pB < 0) pB = 0;
152
                    if (pB > 255) pB = 255;
153

  
154
                    pixels[i * 4] = (byte)pB;
155
                    pixels[i * 4 + 1] = (byte)pG;
156
                    pixels[i * 4 + 2] = (byte)pR;
157
                }
158
            }
159

  
160
            return pixels;
161
        }
162

  
163
        public static byte[] SetContrast(double contrast, byte[] pixels)
164
        {
165
            if (contrast < -100) contrast = -100;
166
            if (contrast > 100) contrast = 100;
167
            contrast = (100.0 + contrast) / 100.0;
168
            contrast *= contrast;
169

  
170
            for (int i = 0; i < pixels.Length / 4; ++i)
171
            {
172
                byte b = pixels[i * 4];
173
                byte g = pixels[i * 4 + 1];
174
                byte r = pixels[i * 4 + 2];
175
                byte a = pixels[i * 4 + 3];
176

  
177
                if (r != 255 || g != 255 || b != 255 || a == 255)
178
                {
179
                    double pR = r / 255.0;
180
                    pR -= 0.5;
181
                    pR *= contrast;
182
                    pR += 0.5;
183
                    pR *= 255;
184
                    if (pR < 0) pR = 0;
185
                    if (pR > 255) pR = 255;
186

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

  
195
                    double pB = b / 255.0;
196
                    pB -= 0.5;
197
                    pB *= contrast;
198
                    pB += 0.5;
199
                    pB *= 255;
200
                    if (pB < 0) pB = 0;
201
                    if (pB > 255) pB = 255;
202

  
203
                    pixels[i * 4] = (byte)pB;
204
                    pixels[i * 4 + 1] = (byte)pG;
205
                    pixels[i * 4 + 2] = (byte)pR;
206
                }
207
            }
208

  
209
            return pixels;
210
        }
57 211
    }
58 212
}

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)