-
Notifications
You must be signed in to change notification settings - Fork 0
/
greypixellist.js-e
173 lines (159 loc) · 5.56 KB
/
greypixellist.js-e
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
## Copyright (c) 2015, Empirical Modelling Group
## All rights reserved.
##
## See LICENSE.txt
/** Returns a GreyPixelList drawable. The ptrToData argument should be a pointer to a list of
* integers. The first element represents the grey shade of the top-left pixel (0-255) and the
* subsequent elements represent successive pixels, first along the top row going from left to right
* and then along the second row, etc. The data list should contain data for an exact number of rows
* of the specified width. The image can be tinted in a particular colour using the optional
* redMultiply, greenMultiply, blueMultiply, redAdd, greenAdd and blueAdd parameters.
* redOfResultingPixel = redMultiply * grayscaleValue + redAdd
* greenOfResultingPixel = greenMultiply * grayscaleValue + redAdd
* blueOfResultingPixel = blueMultiply * grayscaleValue + blueAdd
*
* The resulting red, green and blue values from the above calculations are clipped so that the final
* values rendered all lie within the range 0 to 255. This can be used to create effects, for
* example, multipliers greater than 1 increase the contrast of the image. Usage of the undefined
* value in the list of grayscale values causes the relevant pixel to be white and uneffected by the
* tinting process.
*
* The multipliers all default to 1 and the additions all default to adding nothing, i.e. a pure
* grayscale image is created without a tint.
*
* Example:
* ````
* func greyMap {
* auto data, i, j, v;
* data = [];
* for (j = 0; j <= 255; j++) {
* for (i = 0; i <= 255; i++) {
* v = sqrt((i - 128) * (i - 128) + (j - 128) * (j - 128));
* if (v > 115) {
* append data, @;
* } else {
* append data, ((i - 128) * (i - 128) + (j - 128) * (j - 128)) % 256;
* }
* }
* }
return data;
* }
* pixelData is greyMap();
* pixels is GreyPixelList(&pixelData, mousePosition.x - 128, mousePosition.y - 128, 256);
* picture is [pixels];
* ````
* @param [name]
* @param ptrToData Pointer to the pixel data
* @param x x-position
* @param y y-position
* @param width
* @param [redMultiply]
* @param [blueMultiply]
* @param [greenMultiply]
* @param [redAdd]
* @param [blueAdd]
* @param [greenAdd]
* @return GreyPixelList drawable
*/
func GreyPixelList {
${{
var name;
var argsProcessed;
if (typeof(arguments[0]) == "string") {
name = arguments[0];
argsProcessed = 1;
} else {
argsProcessed = 0;
}
var ptrToData = arguments[argsProcessed];
argsProcessed++;
var x = arguments[argsProcessed];
argsProcessed++;
var y = arguments[argsProcessed];
argsProcessed++;
var width = arguments[argsProcessed];
argsProcessed++;
var redMultiply = arguments[argsProcessed];
if (redMultiply === undefined) redMultiply = 1;
argsProcessed++;
var greenMultiply = arguments[argsProcessed];
if (greenMultiply === undefined) greenMultiply = 1;
argsProcessed++;
var blueMultiply = arguments[argsProcessed];
if (blueMultiply === undefined) blueMultiply = 1;
argsProcessed++;
var redAdd = arguments[argsProcessed];
if (redAdd === undefined) redAdd = 0;
argsProcessed++;
var greenAdd = arguments[argsProcessed];
if (greenAdd === undefined) greenAdd = 0;
argsProcessed++;
var blueAdd = arguments[argsProcessed];
if (blueAdd === undefined) blueAdd = 0;
argsProcessed++;
return new GreyPixelList(name, ptrToData, x, y, width, redMultiply, greenMultiply, blueMultiply, redAdd, greenAdd, blueAdd);
}}$;
}
${{
GreyPixelList = function(name, ptrToData, x, y, width, redMultiply, greenMultiply, blueMultiply, redAdd, greenAdd, blueAdd) {
this.name = edenUI.plugins.Canvas2D.initZoneFromName(name, "GreyPixelList");
this.obsName = root.currentObservableName();
this.ptr = ptrToData;
this.data = ptrToData instanceof Symbol? ptrToData.value() : [];
this.x = x;
this.y = y;
this.width = width;
this.height = this.data.length / width;
this.redMultiply = redMultiply;
this.greenMultiply = greenMultiply;
this.blueMultiply = blueMultiply;
this.redAdd = redAdd;
this.greenAdd = greenAdd;
this.blueAdd = blueAdd;
}
GreyPixelList.prototype.draw = function(context) {
var imageData = context.createImageData(this.width, this.height);
var imageArr = imageData.data;
var dataArr = this.data;
var dataArrayLen = dataArr.length;
var imageArrayLen = dataArrayLen * 4;
var offset;
offset = 0;
for (var i = 0; i < dataArrayLen; i++) {
imageArr[offset] = this.redMultiply * dataArr[i] + this.redAdd;
offset = offset + 4;
}
offset = 1;
for (var i = 0; i < dataArrayLen; i++) {
imageArr[offset] = this.greenMultiply * dataArr[i] + this.greenAdd;
offset = offset + 4;
}
offset = 2;
for (var i = 0; i < dataArrayLen; i++) {
imageArr[offset] = this.blueMultiply * dataArr[i] + this.blueAdd;
offset = offset + 4;
}
offset = 3;
for (var i = 0; i < dataArrayLen; i++) {
if (dataArr[i] !== undefined) {
imageArr[offset] = 255;
}
offset = offset + 4;
}
context.putImageData(imageData, this.x, this.y);
}
GreyPixelList.prototype.isHit = function (context, scale, x, y) {
return x >= this.x && x < this.x + this.width && y >= this.y && y < this.y + this.height &&
this.data[(y - this.y) * this.width + x - this.x] !== undefined;
}
GreyPixelList.prototype.toString = function() {
var s = "GreyPixelList(";
if (this.name !== undefined) {
s = s + Eden.edenCodeForValue(this.name) + ", ";
}
s = s + Eden.edenCodeForValues(this.ptr, this.x, this.y, this.width, this.redMultiply,
this.redAdd, this.greenMultiply, this.greenAdd, this.blueMultiply, this.blueAdd) + ")";
return s;
}
GreyPixelList.prototype.getEdenCode = GreyPixelList.prototype.toString;
}}$;