-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefault.aspx.cs
More file actions
632 lines (538 loc) · 20.4 KB
/
Default.aspx.cs
File metadata and controls
632 lines (538 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/*=========================================
Based off Color2Gray: Salience-Preserving Color Removal. Amy A. Gooch, Sven C. Olsen, Jack Tumblin, and Bruce Gooch. SIGGRAPH 05
http://www.cs.northwestern.edu/~ago820/color2gray/color2gray.pdf
* Supports .jpg and .png images in RGB ColorSpace
* To Do's:
* - Upload Images*
* - Convert Images From RGB to CIE LaB*
* - RGB -> XYZ //Reduced to one step
* - XYZ -> LMS //Reduced to one step
* - RGB -> LMS*
* - LMS -> LaB*
*
* - Compute Target Differences using luminance and chrominance differences
*
* - Use a Least Square Optimization to selectively modulate the source luminance differences (VIA iterations)
*
* - Convert Images from CIE LaB to RGB to be displayed*
* - LaB -> LMS*
* - LMS -> RGB*
*
* Extras:
* - Multiple Gray Scale algorithms
* - Recoloring for individuals with color blindness :)
* - Based off the work of Dalton J(http://www.daltonize.org)
*
* ========== Bug fixes: ==================
* - NaN or out of range values in images* (Fixed)
* ========================================= */
namespace Color2Gray {
public partial class Default : System.Web.UI.Page {
//Used to manipulate the JPEG bitmaps
protected struct col {
public float R; //L L
public float G; //M a
public float B; //S b
}
protected void Page_Load(object sender, EventArgs e) {
debug.Text = "";
}
//Used to toggle the extra menu options
protected void showOptions(object sender, EventArgs e) {
switch (taskDDL.SelectedValue) {
case "color2Gray":
case "color2Color":
C2GPanel.Visible = true;
CBPanel.Visible = false;
break;
case "color2CC":
C2GPanel.Visible = true;
CBPanel.Visible = true;
break;
default:
C2GPanel.Visible = false;
CBPanel.Visible = false;;
break;
}
}
protected void run(object sender, EventArgs e) {
if (sourceFU.HasFile) {
//source(RGB) -> sourceEdit(Lab)
Bitmap source = new Bitmap(sourceFU.PostedFile.InputStream);
//RGB
col[,] sourceEdit = BitmapToCol(source);
Bitmap result = colToBitmap(sourceEdit);
showImage(result); //Show original image
switch (taskDDL.SelectedValue) { //img in RGB format at end
case "grayscaleAverage": {
RGBtoGrayscaleAvg(ref sourceEdit);
break;
}
case "grayscaleBT601": {
RGBtoGrayscaleBT601(ref sourceEdit);
break;
}
case "grayscaleDesaturation": {
RGBtoGrayscaleDesat(ref sourceEdit);
break;
}
case "grayscaleLab": {
RGBtoLMS(ref sourceEdit);
LMStoLab(ref sourceEdit);
LabtoGray(ref sourceEdit);
LabtoLMS(ref sourceEdit);
LMStoRGB(ref sourceEdit);
break;
}
case "color2Gray": {
RGBtoLMS(ref sourceEdit);
LMStoLab(ref sourceEdit);
int r;
if (!int.TryParse(RadiusTB.Text, out r)) r = 5;
float alpha;
if (!float.TryParse(AlphaTB.Text, out alpha)) alpha = 10;
float theta;
if (!float.TryParse(ThetaTB.Text, out theta)) theta = 45;
theta *= (float)(3.1459 / 180);
int iters;
if (!int.TryParse(ItersTB.Text, out iters)) iters = 10;
col[,] orig = (col[,])sourceEdit.Clone();
float[,] distance = calcDistance(sourceEdit, r, theta, alpha);
solve(ref sourceEdit, ref distance, r, iters);
postSolve(ref sourceEdit, ref orig);
LabtoGray(ref sourceEdit); //0 out the a,b channels
LabtoLMS(ref sourceEdit);
LMStoRGB(ref sourceEdit);
break;
}
case "color2Color": {
RGBtoLMS(ref sourceEdit);
LMStoLab(ref sourceEdit);
int r;
if (!int.TryParse(RadiusTB.Text, out r)) r = 5;
float alpha;
if (!float.TryParse(AlphaTB.Text, out alpha)) alpha = 10;
float theta;
if (!float.TryParse(ThetaTB.Text, out theta)) theta = 45;
theta *= (float)(3.1459 / 180);
int iters;
if (!int.TryParse(ItersTB.Text, out iters)) iters = 10;
col[,] orig = (col[,])sourceEdit.Clone();
float[,] distance = calcDistance(sourceEdit, r, theta, alpha);
solve(ref sourceEdit, ref distance, r, iters);
postSolve(ref sourceEdit, ref orig);
LabtoLMS(ref sourceEdit);
LMStoRGB(ref sourceEdit);
break;
}
case "color2CC": {
RGBtoLMS(ref sourceEdit);
LMStoLab(ref sourceEdit);
int r;
if (!int.TryParse(RadiusTB.Text, out r)) r = 5;
float alpha;
if (!float.TryParse(AlphaTB.Text, out alpha)) alpha = 10;
float theta;
if (!float.TryParse(ThetaTB.Text, out theta)) theta = 45;
theta *= (float)(3.1459 / 180);
int iters;
if (!int.TryParse(ItersTB.Text, out iters)) iters = 10;
col[,] orig = (col[,])sourceEdit.Clone();
int cb;
if (!int.TryParse(CBDDL.SelectedValue, out cb)) cb = 1;
float[,] distance = calcDistance(sourceEdit, r, theta, alpha);
solve(ref sourceEdit, ref distance, r, iters);
postSolve(ref sourceEdit, ref orig);
LabtoLMS(ref sourceEdit);
col[,] err = (col[,])sourceEdit.Clone();
LMStoRGB(ref sourceEdit);
LMStoCB(ref err, cb); //calculate the CB pixels
LMStoRGB(ref err);
result = colToBitmap(err);
showImage(result);
for (int i = 0; i < err.GetLength(0); i++) { //remove them from the image
for (int j = 0; j < err.GetLength(1); j++) {
err[i, j].R -= sourceEdit[i, j].R;
err[i, j].G -= sourceEdit[i, j].B;
err[i, j].B -= sourceEdit[i, j].G;
}
}
result = colToBitmap(err);
showImage(result);
CBCorrect(ref err); //correct them
result = colToBitmap(err);
showImage(result);
for (int i = 0; i < err.GetLength(0); i++) { //add them back in
for (int j = 0; j < err.GetLength(1); j++) {
sourceEdit[i, j].R += err[i, j].R;
sourceEdit[i, j].G += err[i, j].B;
sourceEdit[i, j].B += err[i, j].G;
}
}
break;
}
}
result = colToBitmap(sourceEdit);
showImage(result);
}
else {
debug.Text = "You forgot to select an image";
}
return;
}
protected float[,] calcDistance(col[,] img, int r, float theta, float alpha) {
int w = img.GetLength(0);
int h = img.GetLength(1);
float[,] ret = new float[w, h];
Array.Clear(ret, 0, ret.Length); //0 it out
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
for (int x = i - r; x <= i + r; x++) {
if (x < 0 || x >= w) continue;
for (int y = j - r; y <= j + r; y++) {
if (y < 0 || y >= h) continue;
float delta = calcDelta(img[i, j], img[x, y], theta, alpha);
ret[i, j] += delta;
ret[x, y] -= delta;
}
}
}
}
return ret;
}
protected float calcDelta(col a, col b, float theta, float alpha) {
float dL = a.R - b.R;
float v = (float)Math.Sqrt((a.G - b.G) * (a.G - b.G) + (a.B - b.B) * (a.B - b.B));
float dC = crunch(v, alpha);
if (Math.Abs(dL) > dC) return dL;
return dC * ((Math.Cos(theta) * (a.G - b.G) + Math.Sin(theta) * (a.B - b.B)) > 0 ? 1 : -1);
}
protected float crunch(float chrom_dist, float alphaL) {
return alphaL == 0 ? (float)0 : (float)(alphaL * Math.Tanh(chrom_dist / alphaL));
}
protected void solve(ref col[,] img, ref float[,] d, int r, int iters) {
int w = img.GetLength(0);
int h = img.GetLength(1);
for (int k = 0; k < iters; k++) {
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
float sum = 0;
int count = 0;
for (int x = i - r; x <= i + r; x++) {
if (x < 0 || x >= w) continue;
for (int y = j - r; y <= j + r; y++) {
if (y < 0 || y >= h) continue;
sum += img[x, y].R;
count++;
}
}
img[i, j].R = (float)(d[i, j] + sum) / count;
}
}
}
}
protected void postSolve(ref col[,] img, ref col[,] source) {
float error = 0;
int w = img.GetLength(0);
int h = img.GetLength(1);
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
error += img[i,j].R - source[i,j].R;
error /= w*h;
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
img[i,j].R -= error;
}
protected void LabtoGray(ref col[,] img) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
//Keep L
img[i, j].G = 0; //a
img[i, j].B = 0; //b
}
}
}
protected void LabRecolor(ref col[,] img, ref float[,] d) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
//Keep L
img[i, j].G -= (float)Math.Sqrt(d[i,j]); //a
img[i, j].B += (float)Math.Sqrt(d[i, j]); //b
}
}
}
protected void LMStoCB(ref col[,] img, int type) {
float[,] toCB;
switch (type) {
case 1: //Deuteranopia: green weakness
toCB = new float[3, 3]
{ {(float)1.0, (float)0.0, (float)0.0},
{(float)0.494207, (float)0.0, (float)1.24827},
{(float)0.0, (float)0.0, (float)1.0}
};
break;
case 2: //Protanopia: red weakness
toCB = new float[3, 3]
{ {(float)0.0, (float)2.02344, (float)-2.52581},
{(float)0.0, (float)1.0, (float)0.0},
{(float)0.0, (float)0.0, (float)1.0}
};
break;
case 3: //Tritanopia: blue weakness
toCB = new float[3, 3]
{ {(float)1.0, (float)0.0, (float)0.0},
{(float)0.0, (float)1.0, (float)0.0},
{(float)-0.395913, (float)0.801109, (float)0.0}
};
break;
default:
return;//do nothing
}
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
float[] LMS = new float[3] { (img[i, j]).R, (img[i, j]).G, (img[i, j]).B };
float[] CB = matMult(LMS, toCB);
img[i, j].R = CB[0];
img[i, j].G = CB[1];
img[i, j].B = CB[2];
}
}
}
protected void CBCorrect(ref col[,] img) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
float[] RGB = new float[3] { img[i, j].R, img[i, j].G, img[i, j].B };
float[,] toRGB = new float[3, 3]
{ {(float)0.0, (float)0.0, (float)0.0},
{(float)0.7, (float)1.0, (float)0.0},
{(float)0.7, (float)0.0, (float)1.0}
};
float[] Corrected = matMult(RGB, toRGB);
img[i, j].R = Corrected[0];
img[i, j].G = Corrected[1];
img[i, j].B = Corrected[2];
}
}
}
protected void RGBtoGrayscaleBT601(ref col[,] img) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
float gray = (float)(img[i, j].R * 0.299 + img[i, j].G * 0.587 + img[i, j].B* 0.114);
img[i, j].R = gray;
img[i, j].G = gray;
img[i, j].B = gray;
}
}
}
protected void RGBtoGrayscaleAvg(ref col[,] img) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
float gray = (float)((img[i, j].R + img[i, j].G + img[i, j].B) / 3);
img[i, j].R = gray;
img[i, j].G = gray;
img[i, j].B = gray;
}
}
}
protected void RGBtoGrayscaleDesat(ref col[,] img) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
float gray = (float)((Math.Max(img[i, j].R, Math.Max(img[i, j].G, img[i, j].B))
+ Math.Min(img[i, j].R, Math.Min(img[i, j].G, img[i, j].B))) / 2.0);
img[i, j].R = gray;
img[i, j].G = gray;
img[i, j].B = gray;
}
}
}
protected col[,] BitmapToCol(Bitmap img) {
col[,] ret = new col[img.Width, img.Height];
for (int i = 0; i < img.Width; i++) {
for (int j = 0; j < img.Height; j++) {
Color pixel = img.GetPixel(i, j);
(ret[i, j]).R = Convert.ToInt16(pixel.R);
(ret[i, j]).G = Convert.ToInt16(pixel.G);
(ret[i, j]).B = Convert.ToInt16(pixel.B);
}
}
return ret;
}
protected Bitmap colToBitmap(col[,] img) {
Bitmap ret = new Bitmap(img.GetLength(0), img.GetLength(1));
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
img[i, j].R = img[i, j].R != img[i, j].R ? 0 : img[i, j].R > 255 ? 255 : img[i, j].R < 0 ? 0 : img[i, j].R;
img[i, j].G = img[i, j].G != img[i, j].G ? 0 : img[i, j].G > 255 ? 255 : img[i, j].G < 0 ? 0 : img[i, j].G;
img[i, j].B = img[i, j].B != img[i, j].B ? 0 : img[i, j].B > 255 ? 255 : img[i, j].B < 0 ? 0 : img[i, j].B;
int red = Convert.ToInt32(img[i, j].R);
int green = Convert.ToInt32(img[i, j].G);
int blue = Convert.ToInt32(img[i, j].B);
Color px = Color.FromArgb(red, green, blue);
ret.SetPixel(i, j, px);
}
}
return ret;
}
protected void RGBtoLMS(ref col[,] img) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
float[] RGB = new float[3] { (img[i, j]).R, (img[i, j]).G, (img[i, j]).B };
float[,] toLMS = new float[3, 3]
{ {(float)0.3811, (float)0.5783, (float)0.0402},
{(float)0.1967, (float)0.7244, (float)0.0782},
{(float)0.0241, (float)0.1288, (float)0.8444}
};
float[] LMS = matMult(RGB, toLMS);
img[i, j].R = LMS[0];
img[i, j].G = LMS[1];
img[i, j].B = LMS[2];
}
}
}
protected void LMStoLab(ref col[,] img) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
float[] LMS = new float[3] {img[i, j].R == 0 ? 0 : (float)(Math.Log10((img[i, j]).R)),
img[i, j].G == 0 ? 0 : (float)Math.Log10((img[i, j]).G),
img[i, j].B == 0 ? 0 : (float)Math.Log10((img[i, j]).B) };
float[,] toLab1 = new float[3, 3]
{ {(float)1, (float)1, (float)1},
{(float)1, (float)1, (float)-2},
{(float)1, (float)-1, (float)0.0}
};
float[,] toLab2 = new float[3, 3]
{ {(float)(1/Math.Sqrt(3)), (float)0.0, (float)0.0},
{(float)0.0, (float)(1/Math.Sqrt(6)), (float)0.0},
{(float)0.0, (float)0.0, (float)(1/Math.Sqrt(2))}
};
float[] Lab = matMult(LMS, toLab1);
Lab = matMult(Lab, toLab2);
img[i, j].R = Lab[0];
img[i, j].G = Lab[1];
img[i, j].B = Lab[2];
}
}
}
protected void LabtoLMS(ref col[,] img) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
float[] Lab = new float[3] { (img[i, j]).R, (img[i, j]).G, (img[i, j]).B };
float[,] toLMS1 = new float[3, 3]
{ {(float)(Math.Sqrt(3)/3), (float)0.0, (float)0.0},
{(float)0.0, (float)(Math.Sqrt(6)/6), (float)0.0},
{(float)0.0, (float)0.0, (float)(Math.Sqrt(2)/2)}
};
float[,] toLMS2 = new float[3, 3]
{ {(float)1, (float)1, (float)1},
{(float)1, (float)1, (float)-1},
{(float)1, (float)-2, (float)0.0}
};
float[] LMS = matMult(Lab, toLMS1);
LMS = matMult(LMS, toLMS2);
img[i, j].R = (float)Math.Pow(10, LMS[0]);
img[i, j].G = (float)Math.Pow(10, LMS[1]);
img[i, j].B = (float)Math.Pow(10, LMS[2]);
}
}
}
protected void LMStoRGB(ref col[,] img) {
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
float[] LMS = new float[3] { img[i, j].R, img[i, j].G, img[i, j].B };
float[,] toRGB = new float[3, 3]
{ {(float)4.4679, (float)-3.5873, (float)0.1193},
{(float)-1.2186, (float)2.3809, (float)-0.1624},
{(float)0.0497, (float)-0.2439, (float)1.2045}
};
float[] RGB = matMult(LMS, toRGB);
img[i, j].R = RGB[0];
img[i, j].G = RGB[1];
img[i, j].B = RGB[2];
}
}
}
protected col calcMean(ref col[,] img) {
col ret = new col();
ret.R = 0;
ret.G = 0;
ret.B = 0;
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
if (img[i, j].R == img[i, j].R)
ret.R += img[i, j].R;
if (img[i, j].G == img[i, j].G)
ret.G += img[i, j].G;
if (img[i, j].B == img[i, j].B)
ret.B += img[i, j].B;
}
}
ret.R /= (img.GetLength(0) * img.GetLength(1));
ret.G /= (img.GetLength(0) * img.GetLength(1));
ret.B /= (img.GetLength(0) * img.GetLength(1));
return ret;
}
protected col calcStdDiv(ref col[,] img) {
col ret = new col();
ret.R = 0;
ret.G = 0;
ret.B = 0;
//1. Calc Mean
col mean = calcMean(ref img);
//2. subtract the Mean and square the result.
//3. Then work out the mean of those squared differences.
for (int i = 0; i < img.GetLength(0); i++) {
for (int j = 0; j < img.GetLength(1); j++) {
if (img[i, j].R == img[i, j].R)
ret.R += (img[i, j].R - mean.R) * (img[i, j].R - mean.R);
if (img[i, j].G == img[i, j].G)
ret.G += (img[i, j].G - mean.G) * (img[i, j].G - mean.G);
if (img[i, j].B == img[i, j].B)
ret.B += (img[i, j].B - mean.B) * (img[i, j].B - mean.B);
}
}
ret.R /= (img.GetLength(0) * img.GetLength(1));
ret.G /= (img.GetLength(0) * img.GetLength(1));
ret.B /= (img.GetLength(0) * img.GetLength(1));
//4. Take the square root of that and we are done!
ret.R = (float)Math.Sqrt(ret.R);
ret.G = (float)Math.Sqrt(ret.G);
ret.B = (float)Math.Sqrt(ret.B);
return ret;
}
protected float[] matMult(float[] a, float[,] b) {
float[] ret = new float[3] { 0, 0, 0 };
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
ret[i] += a[j] * b[i, j];
}
}
return ret;
}
//Addes the image to ImageUP
protected void showImage(Bitmap img) {
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
var base64Data = Convert.ToBase64String(ms.ToArray());
System.Web.UI.WebControls.Image newImg = new System.Web.UI.WebControls.Image();
newImg.ImageUrl = "data:image/jpg;base64," + base64Data;
newImg.Visible = true;
imageUP.ContentTemplateContainer.Controls.Add(newImg);
}
//Clears the form and the Image Panel
protected void clear(object sender, EventArgs e) {
Response.Redirect(Request.RawUrl); //Just refresh
//sourceFU.Attributes.Clear();
//imageUP.ContentTemplateContainer.Controls.Clear();
}
}
}