port bug 1155776, bug 1155816, bug 870370 (moves from Makefile.in to moz.build)
[mandelbrot.git] / content / mandelbrot.js
CommitLineData
5b823560
RK
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is KaiRo.at Mandelbrot, XULRunner version.
15 *
16 * The Initial Developer of the Original Code is
17 * Robert Kaiser <kairo@kairo.at>.
8a2b6e17 18 * Portions created by the Initial Developer are Copyright (C) 2008-2011
5b823560
RK
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Robert Kaiser <kairo@kairo.at>
8a2b6e17
RK
23 * prefiks (patch for some speedups)
24 * Boris Zbarsky <bzbarsky@mit.edu> (use imageData for canvas interaction)
5b823560
RK
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
140e460f
RK
40Components.utils.import("resource://gre/modules/Services.jsm");
41
6e98af87 42var gColorPalette = [];
6e98af87 43var gStartTime = 0;
fa4ecb24 44var gMbrotBundle;
86e67c44 45var gCurrentImageData;
cd6af089 46var gLastImageData;
6e98af87
RK
47
48function Startup() {
fa4ecb24
RK
49 gMbrotBundle = document.getElementById("mbrotBundle");
50 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty");
3fd66836
RK
51
52 let img = document.getElementById("mbrotImage");
53 img.addEventListener("mouseup", imgEvHandler, false);
54 img.addEventListener("mousedown", imgEvHandler, false);
55 img.addEventListener("mousemove", imgEvHandler, false);
56 img.addEventListener("touchstart", imgEvHandler, false);
57 img.addEventListener("touchend", imgEvHandler, false);
58 img.addEventListener("touchcancel", imgEvHandler, false);
59 img.addEventListener("touchleave", imgEvHandler, false);
60 img.addEventListener("touchmove", imgEvHandler, false);
140e460f
RK
61
62 Services.obs.notifyObservers(window, "mandelbrot-loaded", null);
6e98af87 63}
37b05b56 64
a79ddf52
RK
65function getAdjustPref(prefname) {
66 let value;
67 switch (prefname) {
68 case "image.width":
69 case "image.height":
70 value = 0;
71 try {
140e460f 72 value = Services.prefs.getIntPref("mandelbrot." + prefname);
a79ddf52
RK
73 }
74 catch (e) { }
75 if ((value < 10) || (value > 5000)) {
76 value = 300;
140e460f 77 Services.prefs.setIntPref("mandelbrot." + prefname, value);
a79ddf52
RK
78 }
79 return value;
80 case "last_image.Cr_*":
81 let Cr_min = -2.0;
82 let Cr_max = 1.0;
83 try {
140e460f
RK
84 Cr_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_min"));
85 Cr_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_max"));
a79ddf52
RK
86 }
87 catch (e) { }
88 if ((Cr_min < -3) || (Cr_min > 2) ||
89 (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
90 Cr_min = -2.0; Cr_max = 1.0;
91 }
140e460f
RK
92 Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
93 Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
a79ddf52
RK
94 return {Cr_min: Cr_min, Cr_max: Cr_max};
95 case "last_image.Ci_*":
96 let Ci_min = -1.5;
97 let Ci_max = 1.5;
98 try {
140e460f
RK
99 Ci_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_min"));
100 Ci_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_max"));
a79ddf52
RK
101 }
102 catch (e) { }
103 if ((Ci_min < -2.5) || (Ci_min > 2.5) ||
104 (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) {
105 Ci_min = -1.5; Ci_max = 1.5;
106 }
140e460f
RK
107 Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
108 Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
a79ddf52
RK
109 return {Ci_min: Ci_min, Ci_max: Ci_max};
110 case "iteration_max":
111 value = 500;
112 try {
140e460f 113 value = Services.prefs.getIntPref("mandelbrot." + prefname);
a79ddf52
RK
114 }
115 catch (e) {
116 setIter(value);
117 }
118 if (value < 10 || value > 10000) {
119 value = 500;
120 setIter(value);
121 }
122 return value;
123 case "use_algorithm":
124 value = "numeric";
125 try {
140e460f 126 value = Services.prefs.getCharPref("mandelbrot." + prefname);
a79ddf52
RK
127 }
128 catch (e) {
129 setAlgorithm(value);
130 }
131 return value;
132 case "color_palette":
133 value = "kairo";
134 try {
140e460f 135 value = Services.prefs.getCharPref("mandelbrot." + prefname);
a79ddf52
RK
136 }
137 catch(e) {
138 setPalette(value);
139 }
140 return value;
141 case "syncProportions":
142 value = true;
143 try {
140e460f 144 value = Services.prefs.getBoolPref("mandelbrot." + prefname);
a79ddf52
RK
145 }
146 catch(e) {
140e460f 147 Services.prefs.setBoolPref("mandelbrot." + prefname, value);
a79ddf52
RK
148 }
149 return value;
150 default:
151 return false;
86e67c44 152 }
a79ddf52
RK
153}
154
155function adjustCoordsAndDraw(aC_min, aC_max) {
156 let iWidth = getAdjustPref("image.width");
157 let iHeight = getAdjustPref("image.height");
86e67c44
RK
158
159 // correct coordinates
160 if (aC_min.r < -2)
161 aC_min.r = -2;
162 if (aC_max.r > 2)
163 aC_max.r = 2;
164 if ((aC_min.r > 2) || (aC_max.r < -2) || (aC_min.r >= aC_max.r)) {
165 aC_min.r = -2.0; aC_max.r = 1.0;
166 }
167 if (aC_min.i < -2)
168 aC_min.i = -2;
169 if (aC_max.i > 2)
170 aC_max.i = 2;
171 if ((aC_min.i > 2) || (aC_max.i < -2) || (aC_min.i >= aC_max.i)) {
30586599 172 aC_min.i = -1.3; aC_max.i = 1.3;
86e67c44
RK
173 }
174
175 let CWidth = aC_max.r - aC_min.r;
176 let CHeight = aC_max.i - aC_min.i;
177 let C_mid = new complex(aC_min.r + CWidth / 2, aC_min.i + CHeight / 2);
178
179 let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight);
180
140e460f
RK
181 Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2);
182 Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2);
183 Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2);
184 Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2);
86e67c44
RK
185
186 drawImage();
187}
188
37b05b56 189function drawImage() {
8a9c8e3f 190 let canvas = document.getElementById("mbrotImage");
2cb9a6b5 191 let context = canvas.getContext("2d");
37b05b56 192
5366c7d6
RK
193 document.getElementById("drawButton").hidden = true;
194
fa4ecb24 195 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusDrawing");
2cb9a6b5 196
cd6af089
RK
197 if (gCurrentImageData) {
198 gLastImageData = gCurrentImageData;
199 }
200
a79ddf52
RK
201 let Cr_vals = getAdjustPref("last_image.Cr_*");
202 let Cr_min = Cr_vals.Cr_min;
203 let Cr_max = Cr_vals.Cr_max;
eceff1c9 204
a79ddf52
RK
205 let Ci_vals = getAdjustPref("last_image.Ci_*");
206 let Ci_min = Ci_vals.Ci_min;
207 let Ci_max = Ci_vals.Ci_max;
eceff1c9 208
a79ddf52
RK
209 let iterMax = getAdjustPref("iteration_max");
210 let algorithm = getAdjustPref("use_algorithm");
6e98af87 211
cd6af089
RK
212 let currentPalette = getAdjustPref("color_palette");
213 if (!gColorPalette || !gColorPalette.length)
214 gColorPalette = getColorPalette(currentPalette);
215
a79ddf52
RK
216 let iWidth = getAdjustPref("image.width");
217 let iHeight = getAdjustPref("image.height");
eceff1c9 218
86e67c44
RK
219 gCurrentImageData = {
220 C_min: new complex(Cr_min, Ci_min),
221 C_max: new complex(Cr_max, Ci_max),
222 iWidth: iWidth,
223 iHeight: iHeight,
224 iterMax: iterMax
225 };
226
eceff1c9
RK
227 canvas.width = iWidth;
228 canvas.height = iHeight;
229
230 context.fillStyle = "rgba(255, 255, 255, 127)";
2cb9a6b5 231 context.fillRect(0, 0, canvas.width, canvas.height);
37b05b56 232
2cb9a6b5
RK
233 gStartTime = new Date();
234
eceff1c9
RK
235 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
236 canvas, context, iterMax, algorithm);
2cb9a6b5
RK
237}
238
eceff1c9 239function drawLine(line, dimensions, canvas, context, iterMax, algorithm) {
aad35028
RK
240 let Cr_min = dimensions[0];
241 let Cr_max = dimensions[1];
242 let Cr_scale = Cr_max - Cr_min;
243
244 let Ci_min = dimensions[2];
245 let Ci_max = dimensions[3];
246 let Ci_scale = Ci_max - Ci_min;
247
8a2b6e17
RK
248 let lines = Math.min(canvas.height - line, 8);
249 let imageData = context.createImageData(canvas.width, lines);
250 let pixels = imageData.data;
251 let idx = 0;
aad35028
RK
252 for (var img_y = line; img_y < canvas.height && img_y < line+8; img_y++)
253 for (let img_x = 0; img_x < canvas.width; img_x++) {
254 let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
255 Ci_min + (img_y / canvas.height) * Ci_scale);
8a2b6e17
RK
256 let colors = drawPoint(context, img_x, img_y, C, iterMax, algorithm);
257 pixels[idx++] = colors[0];
258 pixels[idx++] = colors[1];
259 pixels[idx++] = colors[2];
260 pixels[idx++] = colors[3];
aad35028 261 }
8a2b6e17 262 context.putImageData(imageData, 0, line);
2cb9a6b5 263
aad35028
RK
264 if (img_y < canvas.height)
265 setTimeout(drawLine, 0, img_y, dimensions, canvas, context, iterMax, algorithm);
266 else if (gStartTime)
267 EndCalc();
37b05b56
RK
268}
269
6e98af87
RK
270function EndCalc() {
271 let endTime = new Date();
272 let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
273 document.getElementById("statusLabel").value =
fa4ecb24 274 gMbrotBundle.getFormattedString("statusTime", [timeUsed.toFixed(3)]);
287a980b 275 gStartTime = 0;
6e98af87
RK
276}
277
37b05b56
RK
278function complex(aReal, aImag) {
279 this.r = aReal;
280 this.i = aImag;
2cb9a6b5
RK
281}
282complex.prototype = {
283 square: function() {
37b05b56
RK
284 return new complex(this.r * this.r - this.i * this.i,
285 2 * this.r * this.i);
2cb9a6b5
RK
286 },
287 dist: function() {
37b05b56 288 return Math.sqrt(this.r * this.r + this.i * this.i);
2cb9a6b5
RK
289 },
290 add: function(aComplex) {
37b05b56
RK
291 return new complex(this.r + aComplex.r, this.i + aComplex.i);
292 }
293}
294
6e98af87
RK
295function mandelbrotValueOO (aC, aIterMax) {
296 // this would be nice code in general but it looks like JS objects are too heavy for normal use.
8444612a 297 let Z = new complex(0.0, 0.0);
37b05b56
RK
298 for (var iter = 0; iter < aIterMax; iter++) {
299 Z = Z.square().add(aC);
300 if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
301 }
6e98af87
RK
302 return iter;
303}
8444612a 304
6e98af87
RK
305function mandelbrotValueNumeric (aC, aIterMax) {
306 // optimized numeric code for fast calculation
8444612a
RK
307 let Cr = aC.r, Ci = aC.i;
308 let Zr = 0.0, Zi = 0.0;
309 let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
310 for (var iter = 0; iter < aIterMax; iter++) {
311 Zi = 2 * Zr * Zi + Ci;
312 Zr = Zr2 - Zi2 + Cr;
313
314 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
315 if (Zr2 + Zi2 > 256) { break; }
316 }
37b05b56
RK
317 return iter;
318}
319
320function getColor(aIterValue, aIterMax) {
8a9c8e3f 321 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
6e98af87
RK
322 if (gColorPalette && gColorPalette.length)
323 return gColorPalette[standardizedValue];
324
325 // fallback to simple b/w if for some reason we don't have a palette
326 if (aIterValue == aIterMax)
2cb9a6b5 327 return [0, 0, 0, 255];
6e98af87 328 else
2cb9a6b5 329 return [255, 255, 255, 255];
37b05b56
RK
330}
331
332function getColorPalette(palName) {
333 var palette = [];
334 switch (palName) {
335 case 'bw':
8a9c8e3f 336 for (let i = 0; i < 1024; i++) {
2cb9a6b5 337 palette[i] = [255, 255, 255, 255];
37b05b56 338 }
2cb9a6b5 339 palette[1024] = [0, 0, 0, 255];
37b05b56
RK
340 break;
341 case 'kairo':
342 // outer areas
8a9c8e3f
RK
343 for (let i = 0; i < 32; i++) {
344 let cc1 = Math.floor(i * 127 / 31);
345 let cc2 = 170 - Math.floor(i * 43 / 31);
2cb9a6b5 346 palette[i] = [cc1, cc2, cc1, 255];
37b05b56
RK
347 }
348 // inner areas
8a9c8e3f
RK
349 for (let i = 0; i < 51; i++) {
350 let cc = Math.floor(i * 170 / 50);
2cb9a6b5 351 palette[32 + i] = [cc, 0, (170-cc), 255];
37b05b56
RK
352 }
353 // corona
8a9c8e3f
RK
354 for (let i = 0; i < 101; i++) {
355 let cc = Math.floor(i * 200 / 100);
2cb9a6b5 356 palette[83 + i] = [255, cc, 0, 255];
37b05b56
RK
357 }
358 // inner corona
8a9c8e3f
RK
359 for (let i = 0; i < 201; i++) {
360 let cc1 = 255 - Math.floor(i * 85 / 200);
361 let cc2 = 200 - Math.floor(i * 30 / 200);
362 let cc3 = Math.floor(i * 170 / 200);
2cb9a6b5 363 palette[184 + i] = [cc1, cc2, cc3, 255];
37b05b56 364 }
8a9c8e3f
RK
365 for (let i = 0; i < 301; i++) {
366 let cc1 = 170 - Math.floor(i * 43 / 300);
367 let cc2 = 170 + Math.floor(i * 85 / 300);
2cb9a6b5 368 palette[385 + i] = [cc1, cc1, cc2, 255];
37b05b56 369 }
8a9c8e3f
RK
370 for (let i = 0; i < 338; i++) {
371 let cc = 127 + Math.floor(i * 128 / 337);
2cb9a6b5 372 palette[686 + i] = [cc, cc, 255, 255];
37b05b56 373 }
2cb9a6b5 374 palette[1024] = [0, 0, 0, 255];
37b05b56
RK
375 break;
376 case 'rainbow-linear1':
8a9c8e3f 377 for (let i = 0; i < 256; i++) {
2cb9a6b5
RK
378 palette[i] = [i, 0, 0, 255];
379 palette[256 + i] = [255, i, 0, 255];
380 palette[512 + i] = [255 - i, 255, i, 255];
381 palette[768 + i] = [i, 255-i, 255, 255];
37b05b56 382 }
2cb9a6b5 383 palette[1024] = [0, 0, 0, 255];
37b05b56 384 break;
72eff464
RK
385 case 'rainbow-squared1':
386 for (let i = 0; i < 34; i++) {
387 let cc = Math.floor(i * 255 / 33);
388 palette[i] = [cc, 0, 0, 255];
389 }
390 for (let i = 0; i < 137; i++) {
391 let cc = Math.floor(i * 255 / 136);
392 palette[34 + i] = [255, cc, 0, 255];
393 }
394 for (let i = 0; i < 307; i++) {
395 let cc = Math.floor(i * 255 / 306);
396 palette[171 + i] = [255 - cc, 255, cc, 255];
397 }
398 for (let i = 0; i < 546; i++) {
399 let cc = Math.floor(i * 255 / 545);
400 palette[478 + i] = [cc, 255 - cc, 255, 255];
401 }
402 palette[1024] = [0, 0, 0, 255];
403 break;
404 case 'rainbow-linear2':
405 for (let i = 0; i < 205; i++) {
406 let cc = Math.floor(i * 255 / 204);
407 palette[i] = [255, cc, 0, 255];
408 palette[204 + i] = [255 - cc, 255, 0, 255];
409 palette[409 + i] = [0, 255, cc, 255];
410 palette[614 + i] = [0, 255 - cc, 255, 255];
411 palette[819 + i] = [cc, 0, 255, 255];
412 }
413 palette[1024] = [0, 0, 0, 255];
414 break;
415 case 'rainbow-squared2':
416 for (let i = 0; i < 19; i++) {
417 let cc = Math.floor(i * 255 / 18);
418 palette[i] = [255, cc, 0, 255];
419 }
420 for (let i = 0; i < 74; i++) {
421 let cc = Math.floor(i * 255 / 73);
422 palette[19 + i] = [255 - cc, 255, 0, 255];
423 }
424 for (let i = 0; i < 168; i++) {
425 let cc = Math.floor(i * 255 / 167);
426 palette[93 + i] = [0, 255, cc, 255];
427 }
428 for (let i = 0; i < 298; i++) {
429 let cc = Math.floor(i * 255 / 297);
430 palette[261 + i] = [0, 255 - cc, 255, 255];
431 }
432 for (let i = 0; i < 465; i++) {
433 let cc = Math.floor(i * 255 / 464);
434 palette[559 + i] = [cc, 0, 255, 255];
435 }
436 palette[1024] = [0, 0, 0, 255];
437 break;
37b05b56 438 }
72eff464
RK
439 /*
440 'Standard-Palette (QB-Colors)
37b05b56
RK
441 For i = 0 To 1024
442 xx = CInt(i * 500 / 1024 + 2)
443 If xx <= 15 Then clr = xx
444 If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485))
445 If xx >= 500 Then clr = 0
446 palette(i) = QBColor(clr)
447 Next
72eff464 448 */
37b05b56
RK
449 return palette;
450}
451
6e98af87
RK
452function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
453 var itVal;
454 switch (algorithm) {
455 case 'oo':
456 itVal = mandelbrotValueOO(C, iterMax);
457 break;
458 case 'numeric':
459 default:
460 itVal = mandelbrotValueNumeric(C, iterMax);
461 break;
462 }
2cb9a6b5 463 return getColor(itVal, iterMax);
37b05b56
RK
464}
465
6e98af87
RK
466/***** pure UI functions *****/
467
4d8e7dcb 468var zoomstart;
7e4a9776 469var imgBackup;
b6d22cbb 470var zoomTouchID;
4d8e7dcb 471
3fd66836
RK
472let imgEvHandler = {
473 handleEvent: function(aEvent) {
474 let canvas = document.getElementById("mbrotImage");
475 let context = canvas.getContext("2d");
b6d22cbb
RK
476 let touchEvent = aEvent.type.indexOf('touch') != -1;
477
478 // Bail out if this is neither a touch nor left-click.
479 if (!touchEvent && aEvent.button != 0)
480 return;
481
482 // Bail out if the started touch can't be found.
483 if (touchEvent && zoomstart &&
484 !aEvent.changedTouches.identifiedTouch(zoomTouchID))
485 return;
486
487 let coordObj = touchEvent ?
488 aEvent.changedTouches.identifiedTouch(zoomTouchID) :
489 aEvent;
490
3fd66836
RK
491 switch (aEvent.type) {
492 case 'mousedown':
493 case 'touchstart':
b6d22cbb
RK
494 if (touchEvent) {
495 zoomTouchID = aEvent.changedTouches.item(0).identifier;
496 coordObj = aEvent.changedTouches.identifiedTouch(zoomTouchID);
3fd66836 497 }
b6d22cbb
RK
498 // left button - start dragzoom
499 zoomstart = {x: coordObj.clientX - canvas.offsetLeft,
500 y: coordObj.clientY - canvas.offsetTop};
501 imgBackup = context.getImageData(0, 0, canvas.width, canvas.height);
3fd66836
RK
502 break;
503 case 'mouseup':
504 case 'touchend':
b6d22cbb 505 if (zoomstart) {
3fd66836 506 context.putImageData(imgBackup, 0, 0);
b6d22cbb
RK
507 let zoomend = {x: coordObj.clientX - canvas.offsetLeft,
508 y: coordObj.clientY - canvas.offsetTop};
3fd66836
RK
509
510 // make sure zoomend is bigger than zoomstart
511 if ((zoomend.x == zoomstart.x) || (zoomend.y == zoomstart.y)) {
512 // cannot zoom what has no area, discard it
513 zoomstart = undefined;
514 return;
515 }
516 if (zoomend.x < zoomstart.x)
517 [zoomend.x, zoomstart.x] = [zoomstart.x, zoomend.x];
518 if (zoomend.y < zoomstart.y)
519 [zoomend.y, zoomstart.y] = [zoomstart.y, zoomend.y];
520
521 // determine new "coordinates"
522 let CWidth = gCurrentImageData.C_max.r - gCurrentImageData.C_min.r;
523 let CHeight = gCurrentImageData.C_max.i - gCurrentImageData.C_min.i;
524 let newC_min = new complex(
525 gCurrentImageData.C_min.r + zoomstart.x / gCurrentImageData.iWidth * CWidth,
526 gCurrentImageData.C_min.i + zoomstart.y / gCurrentImageData.iHeight * CHeight);
527 let newC_max = new complex(
528 gCurrentImageData.C_min.r + zoomend.x / gCurrentImageData.iWidth * CWidth,
529 gCurrentImageData.C_min.i + zoomend.y / gCurrentImageData.iHeight * CHeight);
530
531 adjustCoordsAndDraw(newC_min, newC_max);
532 }
533 zoomstart = undefined;
534 break;
535 case 'mousemove':
536 case 'touchmove':
b6d22cbb 537 if (zoomstart) {
3fd66836
RK
538 context.putImageData(imgBackup, 0, 0);
539 context.strokeStyle = "rgb(255,255,31)";
540 context.strokeRect(zoomstart.x, zoomstart.y,
b6d22cbb
RK
541 coordObj.clientX - canvas.offsetLeft - zoomstart.x,
542 coordObj.clientY - canvas.offsetTop - zoomstart.y);
86e67c44 543 }
7e4a9776 544 break;
3fd66836 545 }
4d8e7dcb 546 }
3fd66836 547};
4d8e7dcb 548
37b05b56 549function saveImage() {
740b86d1 550 const nsIFilePicker = Components.interfaces.nsIFilePicker;
aad35028 551 let fp = null;
740b86d1
RK
552 try {
553 fp = Components.classes["@mozilla.org/filepicker;1"]
554 .createInstance(nsIFilePicker);
555 } catch (e) {}
556 if (!fp) return;
aad35028 557 let promptString = gMbrotBundle.getString("savePrompt");
740b86d1 558 fp.init(window, promptString, nsIFilePicker.modeSave);
fa4ecb24 559 fp.appendFilter(gMbrotBundle.getString("pngFilterName"), "*.png");
740b86d1
RK
560 fp.defaultString = "mandelbrot.png";
561
aad35028 562 let fpResult = fp.show();
740b86d1
RK
563 if (fpResult != nsIFilePicker.returnCancel) {
564 saveCanvas(document.getElementById("mbrotImage"), fp.file);
565 }
37b05b56
RK
566}
567
287a980b
RK
568function updateBookmarkMenu(aParent) {
569 document.getElementById("bookmarkSave").disabled =
cd6af089
RK
570 (!document.getElementById("drawButton").hidden || (gStartTime > 0));
571
572 document.getElementById("bookmarkBack").disabled = !gLastImageData;
287a980b
RK
573
574 while (aParent.hasChildNodes() &&
86e67c44 575 aParent.lastChild.id != "bookmarkSeparator")
287a980b
RK
576 aParent.removeChild(aParent.lastChild);
577
aad35028 578 let file = Components.classes["@mozilla.org/file/directory_service;1"]
287a980b
RK
579 .getService(Components.interfaces.nsIProperties)
580 .get("ProfD", Components.interfaces.nsIFile);
581 file.append("mandelbookmarks.sqlite");
582 if (file.exists()) {
aad35028 583 let connection = Components.classes["@mozilla.org/storage/service;1"]
287a980b
RK
584 .getService(Components.interfaces.mozIStorageService)
585 .openDatabase(file);
586 try {
587 if (connection.tableExists("bookmarks")) {
aad35028 588 let statement = connection.createStatement(
86e67c44
RK
589 "SELECT name,ROWID FROM bookmarks ORDER BY ROWID ASC");
590 while (statement.executeStep()) {
591 let newItem = aParent.appendChild(document.createElement("menuitem"));
592 newItem.setAttribute("label", statement.getString(0));
593 newItem.setAttribute("bmRowID", statement.getString(1));
594 }
287a980b
RK
595 statement.reset();
596 statement.finalize();
597 return;
2eed6617 598 }
287a980b
RK
599 } finally {
600 connection.close();
2eed6617 601 }
287a980b
RK
602 }
603 // Create the "Nothing Available" Menu item and disable it.
aad35028 604 let na = aParent.appendChild(document.createElement("menuitem"));
fa4ecb24 605 na.setAttribute("label", gMbrotBundle.getString("noBookmarks"));
287a980b 606 na.setAttribute("disabled", "true");
2eed6617
RK
607}
608
b6d22cbb 609function goBack() {
cd6af089
RK
610 if (gLastImageData) {
611 Services.prefs.setIntPref("mandelbrot.iteration_max",
612 gLastImageData.iterMax);
613 // use gLastImageData.iWidth, gLastImageData.iHeight ???
614 adjustCoordsAndDraw(gLastImageData.C_min, gLastImageData.C_max);
615 gLastImageData = undefined;
616 }
617}
618
2eed6617 619function callBookmark(evtarget) {
86e67c44 620 if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator")
b8f9a76f
RK
621 return;
622 if (evtarget.id == "bookmarkOverview") {
623 adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
624 return;
625 }
f34775a6
RK
626
627 if (evtarget.getAttribute('bmRowID')) {
628 let iterMax = 0;
629 let C_min = null;
630 let C_max = null;
631
632 let file = Components.classes["@mozilla.org/file/directory_service;1"]
633 .getService(Components.interfaces.nsIProperties)
634 .get("ProfD", Components.interfaces.nsIFile);
635 file.append("mandelbookmarks.sqlite");
636 let connection = Components.classes["@mozilla.org/storage/service;1"]
637 .getService(Components.interfaces.mozIStorageService)
638 .openDatabase(file);
639 let statement = connection.createStatement(
640 "SELECT iteration_max,Cr_min,Cr_max,Ci_min,Ci_max FROM bookmarks WHERE ROWID=?1");
641 statement.bindStringParameter(0, evtarget.getAttribute('bmRowID'));
642 while (statement.executeStep()) {
643 iterMax = statement.getInt32(0);
644 C_min = new complex(statement.getDouble(1), statement.getDouble(3));
645 C_max = new complex(statement.getDouble(2), statement.getDouble(4));
646 }
647 statement.finalize();
648 connection.close();
649
650 if (iterMax && C_min && C_max) {
140e460f 651 Services.prefs.setIntPref("mandelbrot.iteration_max", iterMax);
f34775a6
RK
652 adjustCoordsAndDraw(C_min, C_max);
653 }
654 }
2eed6617
RK
655}
656
657function saveBookmark() {
9c2ca9fa 658 // retrieve wanted bookmark name with a prompt
aad35028 659 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
9c2ca9fa 660 .getService(Components.interfaces.nsIPromptService);
aad35028
RK
661 let input = {value: ""}; // empty default value
662 let ok = prompts.prompt(null, gMbrotBundle.getString("saveBookmarkTitle"), gMbrotBundle.getString("saveBookmarkLabel"), input, null, {});
9c2ca9fa
RK
663 // ok is true if OK is pressed, false if Cancel. input.value holds the value of the edit field if "OK" was pressed.
664 if (!ok || !input.value)
665 return
666
aad35028 667 let bmName = input.value;
287a980b
RK
668
669 // Open or create the bookmarks database.
aad35028 670 let file = Components.classes["@mozilla.org/file/directory_service;1"]
287a980b
RK
671 .getService(Components.interfaces.nsIProperties)
672 .get("ProfD", Components.interfaces.nsIFile);
673 file.append("mandelbookmarks.sqlite");
aad35028 674 let connection = Components.classes["@mozilla.org/storage/service;1"]
287a980b
RK
675 .getService(Components.interfaces.mozIStorageService)
676 .openDatabase(file);
677 connection.beginTransaction();
678 if (!connection.tableExists("bookmarks"))
679 connection.createTable("bookmarks", "name TEXT, iteration_max INTEGER, Cr_min REAL, Cr_max REAL, Ci_min REAL, Ci_max REAL");
680 // NULL. The value is a NULL value.
681 // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
682 // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
683 // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
684
685 // Put value of the current image into the bookmarks table
aad35028 686 let statement = connection.createStatement(
287a980b
RK
687 "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)");
688 statement.bindStringParameter(0, bmName);
86e67c44
RK
689 statement.bindStringParameter(1, gCurrentImageData.iterMax);
690 statement.bindStringParameter(2, gCurrentImageData.C_min.r);
691 statement.bindStringParameter(3, gCurrentImageData.C_max.r);
692 statement.bindStringParameter(4, gCurrentImageData.C_min.i);
693 statement.bindStringParameter(5, gCurrentImageData.C_max.i);
287a980b
RK
694 statement.execute();
695 statement.finalize();
696 connection.commitTransaction();
697 connection.close();
2eed6617
RK
698}
699
6403d662 700function imgSettings() {
60e048b2
RK
701 let anchor = null;
702 let position = "before_start";
5fb4a082 703 if (document.getElementById("mandelbrot-page").nodeName == "page") {
60e048b2
RK
704 anchor = document.getElementById("mandelbrotToolbar");
705 }
706 else {
707 anchor = document.getElementById("mandelbrotMenubar");
708 position = "after_start";
709 }
710 document.getElementById("imgSettingsPanel").showPopup(anchor, position);
6403d662
RK
711}
712
8a2b6e17
RK
713function initImgSettings() {
714 // Get values from prefs.
a79ddf52
RK
715 for each (let coord in ["Cr", "Ci"]) {
716 let coord_vals = getAdjustPref("last_image." + coord + "_*");
717 document.getElementById("is_" + coord + "_min").value = coord_vals[coord + "_min"];
718 document.getElementById("is_" + coord + "_max").value = coord_vals[coord + "_max"];
8a2b6e17
RK
719 }
720 for each (let dim in ["width", "height"]) {
a79ddf52 721 document.getElementById("is_img_" + dim).value = getAdjustPref("image." + dim);
8a2b6e17 722 }
a79ddf52 723 document.getElementById("is_syncProp").checked = getAdjustPref("syncProportions");
8a2b6e17
RK
724
725 // Calculate scales.
726 recalcCoord("Cr", "scale");
727 recalcCoord("Ci", "scale");
728
729 // Clear the preview.
730 let canvas = document.getElementById("is_mbrotPreview");
731 let context = canvas.getContext("2d");
732 context.fillStyle = "rgba(255, 255, 255, 127)";
733 context.fillRect(0, 0, canvas.width, canvas.height);
5fb4a082
RK
734
735 // Set lists to correct values.
736 updateIterList();
737 updatePaletteList();
738 updateAlgoList();
8a2b6e17
RK
739}
740
741function closeImgSettings() {
742 // Hide popup, which will automatically make a call to save values.
743 document.getElementById("imgSettingsPanel").hidePopup();
744}
745
746function saveImgSettings() {
747 // Get values to prefs.
748 for each (let coord in ["Cr_min", "Cr_max", "Ci_min", "Ci_max"]) {
140e460f
RK
749 Services.prefs.setCharPref("mandelbrot.last_image." + coord,
750 document.getElementById("is_" + coord).value);
8a2b6e17
RK
751 }
752 for each (let dim in ["width", "height"]) {
140e460f
RK
753 Services.prefs.setIntPref("mandelbrot.image." + dim,
754 document.getElementById("is_img_" + dim).value);
8a2b6e17 755 }
140e460f
RK
756 Services.prefs.setBoolPref("mandelbrot.syncProportions",
757 document.getElementById("is_syncProp").checked);
8a2b6e17
RK
758}
759
760function checkISValue(textbox, type) {
761 if (type == "coord") {
762 textbox.value = roundCoord(parseFloat(textbox.value));
763 }
764 else if (type == "dim") {
765 textbox.value = parseInt(textbox.value);
766 }
767}
768
769function drawPreview() {
770 let canvas = document.getElementById("is_mbrotPreview");
771 let context = canvas.getContext("2d");
772
773 if (document.getElementById("is_img_width").value /
774 document.getElementById("is_img_height").value
775 < 80 / 50) {
776 canvas.height = 50;
777 canvas.width = canvas.height *
778 document.getElementById("is_img_width").value /
779 document.getElementById("is_img_height").value;
780 }
781 else {
782 canvas.width = 80;
783 canvas.height = canvas.width *
784 document.getElementById("is_imgHeight").value /
785 document.getElementById("is_imgWidth").value;
786 }
787
788 let Cr_min = parseFloat(document.getElementById("is_Cr_min").value);
789 let Cr_max = parseFloat(document.getElementById("is_Cr_max").value);
790 if ((Cr_min < -2) || (Cr_min > 2) ||
791 (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
792 Cr_min = -2.0; Cr_max = 1.0;
793 }
794
795 let Ci_min = parseFloat(document.getElementById("is_Ci_min").value);
796 let Ci_max = parseFloat(document.getElementById("is_Ci_max").value);
797 if ((Ci_min < -2) || (Ci_min > 2) ||
798 (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) {
799 Ci_min = -2.0; Ci_max = 1.0;
800 }
801
a79ddf52
RK
802 let iterMax = getAdjustPref("iteration_max");
803 let algorithm = getAdjustPref("use_algorithm");
8a2b6e17
RK
804
805 context.fillStyle = "rgba(255, 255, 255, 127)";
806 context.fillRect(0, 0, canvas.width, canvas.height);
807
a79ddf52 808 let currentPalette = getAdjustPref("color_palette");
8a2b6e17
RK
809 gColorPalette = getColorPalette(currentPalette);
810
811 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
812 canvas, context, iterMax, algorithm);
813}
814
815function recalcCoord(coord, target) {
816 let othercoord = (coord == "Ci") ? "Cr" : "Ci";
817 let owndim = (coord == "Ci") ? "height" : "width";
818 let otherdim = (coord == "Ci") ? "width" : "height";
3fd66836 819 let myscale;
8a2b6e17 820 if (target == "scale") {
3fd66836 821 myscale =
8a2b6e17
RK
822 parseFloat(document.getElementById("is_" + coord + "_max").value) -
823 parseFloat(document.getElementById("is_" + coord + "_min").value);
824 document.getElementById("is_" + coord + "_scale").value = roundCoord(myscale);
825 }
826 else if (target == 'max') {
827 let mymax =
828 parseFloat(document.getElementById("is_" + coord + "_min").value) +
829 parseFloat(document.getElementById("is_" + coord + "_scale").value);
830 document.getElementById("is_" + coord + "_max").value = roundCoord(mymax);
3fd66836 831 myscale = document.getElementById("is_" + coord + "_scale").value;
8a2b6e17
RK
832 }
833 if (document.getElementById("is_syncProp").checked) {
834 let otherscale = myscale *
835 document.getElementById("is_img_" + otherdim).value /
836 document.getElementById("is_img_" + owndim).value;
837 document.getElementById("is_" + othercoord + "_scale").value = roundCoord(otherscale);
838 let othermax =
839 parseFloat(document.getElementById("is_" + othercoord + "_min").value) +
840 parseFloat(document.getElementById("is_" + othercoord + "_scale").value);
841 document.getElementById("is_" + othercoord + "_max").value = roundCoord(othermax);
842 }
843}
844
845function checkProportions() {
846 if (!document.getElementById("is_syncProp").checked) {
847 recalcCoord("Cr", "scale");
848 }
849}
850
851function roundCoord(floatval) {
852 // We should round to 10 decimals here or so
853 return parseFloat(floatval.toFixed(10));
854}
855
5fb4a082
RK
856function updateIterList() {
857 let currentIter = getAdjustPref("iteration_max");
858 document.getElementById("iterList").value = currentIter;
859}
860
861function updatePaletteList() {
862 let currentPalette = getAdjustPref("color_palette");
863 if (!gColorPalette || !gColorPalette.length)
864 gColorPalette = getColorPalette(currentPalette);
865 document.getElementById("colorList").value = currentPalette;
866}
867
868function updateAlgoList() {
869 let currentAlgo = getAdjustPref("use_algorithm");
870 document.getElementById("algoList").value = currentAlgo;
871}
872
873function setIter(aIter) {
874 Services.prefs.setIntPref("mandelbrot.iteration_max", aIter);
875}
876
877function setPalette(aPaletteID) {
878 Services.prefs.setCharPref("mandelbrot.color_palette", aPaletteID);
879 gColorPalette = getColorPalette(aPaletteID);
880}
881
882function setAlgorithm(algoID) {
883 Services.prefs.setCharPref("mandelbrot.use_algorithm", algoID);
884}
885
886
6e98af87
RK
887/***** helper functions from external sources *****/
888
740b86d1
RK
889// function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
890// custom modifications:
891// - use "a"-prefix on function arguments
892// - take an nsILocalFile as aDestFile argument
893// - always do silent download
894function saveCanvas(aCanvas, aDestFile) {
37b05b56
RK
895 // create a data url from the canvas and then create URIs of the source and targets
896 var io = Components.classes["@mozilla.org/network/io-service;1"]
897 .getService(Components.interfaces.nsIIOService);
740b86d1 898 var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
37b05b56
RK
899
900 // prepare to save the canvas data
901 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
902 .createInstance(Components.interfaces.nsIWebBrowserPersist);
903
904 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
905 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
906
37b05b56 907 // save the canvas data to the file
740b86d1 908 persist.saveURI(source, null, null, null, null, aDestFile);
37b05b56 909}