make more constants controllable
[Dynartic.git] / DynImage.js
1 /*
2 * DynImage.js
3 * node.js module to perform the bulk of the calculations used in Dynartic
4 * main.js is a light frontend to this data structure
5 */
6
7 function DynImage(width, height, buffer) {
8 this.width = width;
9 this.height = height;
10
11 this.lesserDimension = this.width > this.height ?
12 this.height :
13 this.width;
14
15 this.buffer = buffer;
16
17 // prefill with black as opposed to nonsense from memory
18 this.fillColor(0, 0, 0, 0);
19 }
20
21 // gets or sets the color; RGBA<->XY
22
23 DynImage.prototype.coordIndex = function(x, y) {
24 return 4 * ((y * this.width) + x);
25 }
26
27 DynImage.prototype.getColor = function(x, y) {
28 x = Math.floor(x);
29 y = Math.floor(y);
30
31 if(
32 (x < 0) ||
33 (y < 0) ||
34 (x >= this.width) ||
35 (y >= this.height)
36 ) {
37 return [0,0,0,0]; // out of bounds
38 }
39
40 var index = this.coordIndex(x, y);
41
42 return [
43 this.buffer[index],
44 this.buffer[index+1],
45 this.buffer[index+2],
46 this.buffer[index+3]
47 ];
48 }
49
50 DynImage.prototype.setColor = function(x, y, r, g, b, a) {
51 if(typeof a === 'undefined') a = 0; // A is optional; opaque if unspecified
52
53 x = Math.floor(x);
54 y = Math.floor(y);
55
56 if(
57 (x < 0) ||
58 (y < 0) ||
59 (x >= this.width) ||
60 (y >= this.height)
61 ) {
62 return; // out of bounds
63 }
64
65 var index = this.coordIndex(x, y);
66
67
68 this.buffer[index+0] = Math.floor(r);
69 this.buffer[index+1] = Math.floor(g);
70 this.buffer[index+2] = Math.floor(b);
71 this.buffer[index+3] = Math.floor(a);
72 }
73
74 // blindly fills the entirety of the buffer with a given color
75 // used for initialization, etc.
76 DynImage.prototype.fillColor = function(r, g, b, a) {
77 /*for(var i = 0; i < (this.width * this.height * 4); i += 4) {
78 this.buffer[i + 0] = r;
79 this.buffer[i + 1] = g;
80 this.buffer[i + 2] = b;
81 this.buffer[i + 3] = a;
82 }*/
83 for(var x = 0; x < this.width; ++x) {
84 for(var y = 0; y < this.height; ++y) {
85 this.setColor(x, y, r, g, b, a);
86 }
87 }
88 }
89
90 // "pulls" the image up
91 // starting with a point, that point is raised a certain, specified amount (this is of course an analogy)
92 // this causes the alpha value at that point to be raised to this value,
93 // and ripples the effect the nearby pixels as well
94 DynImage.prototype.pointRing = function(sx, sy, height, layerIntensity, intensityMultiplier) {
95 var rippleAmount = (255 / this.lesserDimension) * height;
96
97 var usedPoints = {};
98
99 for(var r = 0; r < height; r += 0.1) {
100 c: for(var theta = 0; theta < (2 * Math.PI); theta += 0.01) {
101 // tracing points around many concentric circles
102
103 var x = (r * Math.cos(theta)) + sx,
104 y = (r * Math.sin(theta)) + sy;
105
106 if(usedPoints[x+";"+y]) continue c;
107
108 usedPoints[x+";"+y] = 1;
109
110 var distance = Math.sqrt( ( (sx - x) * (sx - x) ) + ( (sy - y) * (sy - y) ) );
111 var intensity = Math.log(height - distance) * intensityMultiplier; // linear function.
112 // this outputs a value 0-1, where 1 is the full effect of pulling and 0 is no effect
113 // in the future, we may want a smoother curve
114 // TODO: find alternative that works better
115
116 //var intensity = 0.5;
117
118 var currentColor = this.getColor(x, y);
119 this.setColor(x, y,
120 currentColor[0] - (intensity * layerIntensity),
121 currentColor[1] - (intensity * layerIntensity),
122 currentColor[2] - (intensity * layerIntensity)
123 );
124 }
125 }
126 }
127
128 DynImage.prototype.punchImage = function(sx, sy, sw, sh, force, spread) {
129 for(var i = -(sw / 2); i < (sw/2); ++i) {
130 for(var j = (-(sh/2)); j < (sh/2); ++j) {
131 var x = sx + i,
132 y = sy + j;
133
134 var intensity = Math.sqrt( (i*i) + (j*j) );
135 intensity = Math.pow(2, -intensity / spread);
136 if( Math.sqrt( ((sx - x) * (sx - x)) + ((sy - y) * (sy - y))) < (sw/2))
137 this.punchPoint(x, y, intensity * force);
138 }
139 }
140 }
141
142 DynImage.prototype.punchWave = function(sx, sy, sw, sh, force, spread, period) {
143 for(var i = -(sw / 2); i < (sw/2); ++i) {
144 for(var j = (-(sh/2)); j < (sh/2); ++j) {
145 var x = sx + i,
146 y = sy + j;
147
148 var intensity = Math.sqrt( (i*i) + (j*j) );
149 intensity = Math.pow(2, -intensity / spread);
150
151 this.punchPoint(x, y, intensity * force * Math.sin(intensity / period));
152 }
153 }
154 }
155
156 // opposite of point pulling; works on a single point only
157
158 DynImage.prototype.punchPoint = function(x, y, force) {
159 var color = this.getColor(x, y);
160 color[0] -= force;
161 color[1] -= force;
162 color[2] -= force;
163
164 color[0] = color[0] > 0 ? color[0] < 256 ? color[0] : 255 : 0;
165 color[1] = color[1] > 0 ? color[0] < 256 ? color[1] : 255 : 0;
166 color[2] = color[2] > 0 ? color[0] < 256 ? color[2] : 255 : 0;
167
168 this.setColor(x, y, color[0], color[1], color[2], color[3]);
169 }
170
171 // contrast changer
172 // any pixel below a threshold becomes darker,
173 // and any pixel above the threshold becomes lighter
174
175 DynImage.prototype.recontrast = function(threshold, amount) {
176 for(var i = 0; i < this.width; ++i) {
177 for(var j = 0; j < this.height; ++j) {
178 var pix = this.getColor(i, j);
179
180 // TODO: use HSL for proper value testing
181
182 var avg = (pix[0] + pix[1] + pix[2]) / 3;
183 if(avg > threshold) {
184 this.setColor(i, j, pix[0] + amount, pix[1] + amount, pix[2] + amount);
185 } else {
186 this.setColor(i, j, pix[0] - amount, pix[1] - amount, pix[2] - amount);
187 }
188 }
189 }
190 }
191
192 DynImage.prototype.borderPoint = function(x, y, bred, bgreen, bblue, percent) {
193 var color = this.getColor(x, y);
194
195 // weighted average
196
197 this.setColor( ((bred * percent) + (color[0] * (100 - percent))) / 200,
198 ((bgreen * percent) + (color[1] * (100 - percent))) / 200,
199 ((bblue * percent) + (color[2] * (100 - percent))) / 200);
200 }
201
202 DynImage.prototype.border = function(width, height, bred, bgreen, bblue, mul) {
203 var borderIterations = mul;
204
205 for(var i = 0; i < borderIterations; ++i) {
206 for(var x = i; x < (width - i); ++x) {
207 // top
208
209 var y = i;
210 this.borderPoint(x, y, bred, bgreen, bblue, 100 - (i * mul));
211
212 y = height - i - 1;
213 this.borderPoint(x, y, bred, bgreen, bblue, 100 - (i * mul));
214 }
215 }
216 }
217
218
219 module.exports = DynImage;
This page took 0.113106 seconds and 5 git commands to generate.