-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.csx
134 lines (115 loc) · 4.23 KB
/
run.csx
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
#r "Newtonsoft.Json"
#r "System.Drawing"
#r "System.IO"
using System;
using System.Net;
using Newtonsoft.Json;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Diagnostics;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"Webhook was triggered!");
var message = await req.Content.ReadAsStringAsync();
var jsonContent = JsonConvert.DeserializeObject<PictureJson>(message);
string b64pic = jsonContent.base64Picture;
string b64picedit;
int glitchType = jsonContent.type;
Bitmap i;
//dynamic data = JsonConvert.DeserializeObject(jsonContent);
if (jsonContent.base64Picture == null) {
return req.CreateResponse(HttpStatusCode.BadRequest, new {
error = "Please pass glitch type and a base64 picture string in the input object"
});
}else{
// Convert base 64 string to byte[]
byte[] imageBytes = Convert.FromBase64String(b64pic);
// Convert byte[] to Image
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
i = (Bitmap) Bitmap.FromStream(ms, true);
}
//glitchtype 1 = colorglitch, 2 = colorswap, 3 = noise, 4 = bars
int roffset, goffset, boffset;
roffset = goffset = boffset = 0;
//1
if(glitchType==1) roffset = goffset = boffset = GetRandomNumber(5)+1;
//2
else if(glitchType == 2){
switch (GetRandomNumber(4))
{
case 0:
roffset = GetRandomNumber(3);
goffset = GetRandomNumber(3);
boffset = GetRandomNumber(7);
log.Info("case 0");
break;
case 1:
roffset = GetRandomNumber(7);
goffset = GetRandomNumber(3);
boffset = GetRandomNumber(3);
log.Info("case 1");
break;
case 2:
roffset = GetRandomNumber(3);
goffset = GetRandomNumber(7);
boffset = GetRandomNumber(3);
log.Info("case 2");
break;
default:
roffset = GetRandomNumber(7);
goffset = GetRandomNumber(7);
boffset = GetRandomNumber(7);
log.Info("case default");
break;
}
log.Info("r: "+ roffset + " g: " + goffset + " b: " + boffset);
}
for (int x = 0; x < i.Width; x++)
{
//4
if(glitchType==4) roffset = goffset = boffset = GetRandomNumber(32);
for (int y = 0; y < i.Height; y++)
{
//3
if(glitchType==3){
int rando = GetRandomNumber(32);
roffset = rando;
goffset = rando;
boffset = rando;
}
Color pixelColor = i.GetPixel(x, y);
Color newColor = Color.FromArgb((pixelColor.R<< roffset) & 0xff, (pixelColor.G<< goffset) & 0xff, (pixelColor.B<< boffset) & 0xff);
i.SetPixel(x, y, newColor);
}
}
//convert back to base 64 string
using (var sm = new MemoryStream())
{
// Convert Image to byte[]
i.Save(sm, ImageFormat.Jpeg);
byte[] byteImage = sm.ToArray();
// Convert byte[] to base 64 string
b64picedit = Convert.ToBase64String(byteImage).Insert(0,"data:image/jpeg;base64,");
}
}
return req.CreateResponse(HttpStatusCode.OK, new {
Type = glitchType,
base64Picture = b64picedit
});
}
class PictureJson
{
public int type { get; set; }
public string base64Picture { get; set; }
}
private static readonly Random getrandom = new Random();
private static readonly object syncLock = new object();
public static int GetRandomNumber(int n)
{
lock(syncLock)
{ // synchronize
return getrandom.Next(n);
}
}