bump versions to include new trunk
[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;
4d8e7dcb 470
3fd66836
RK
471let imgEvHandler = {
472 handleEvent: function(aEvent) {
473 let canvas = document.getElementById("mbrotImage");
474 let context = canvas.getContext("2d");
475 switch (aEvent.type) {
476 case 'mousedown':
477 case 'touchstart':
478 if (aEvent.button == 0) {
479 // left button - start dragzoom
480 zoomstart = {x: aEvent.clientX - canvas.offsetLeft,
481 y: aEvent.clientY - canvas.offsetTop};
482 imgBackup = context.getImageData(0, 0, canvas.width, canvas.height);
483 }
484 break;
485 case 'mouseup':
486 case 'touchend':
487 if (aEvent.button == 0 && zoomstart) {
488 context.putImageData(imgBackup, 0, 0);
489 let zoomend = {x: aEvent.clientX - canvas.offsetLeft,
490 y: aEvent.clientY - canvas.offsetTop};
491
492 // make sure zoomend is bigger than zoomstart
493 if ((zoomend.x == zoomstart.x) || (zoomend.y == zoomstart.y)) {
494 // cannot zoom what has no area, discard it
495 zoomstart = undefined;
496 return;
497 }
498 if (zoomend.x < zoomstart.x)
499 [zoomend.x, zoomstart.x] = [zoomstart.x, zoomend.x];
500 if (zoomend.y < zoomstart.y)
501 [zoomend.y, zoomstart.y] = [zoomstart.y, zoomend.y];
502
503 // determine new "coordinates"
504 let CWidth = gCurrentImageData.C_max.r - gCurrentImageData.C_min.r;
505 let CHeight = gCurrentImageData.C_max.i - gCurrentImageData.C_min.i;
506 let newC_min = new complex(
507 gCurrentImageData.C_min.r + zoomstart.x / gCurrentImageData.iWidth * CWidth,
508 gCurrentImageData.C_min.i + zoomstart.y / gCurrentImageData.iHeight * CHeight);
509 let newC_max = new complex(
510 gCurrentImageData.C_min.r + zoomend.x / gCurrentImageData.iWidth * CWidth,
511 gCurrentImageData.C_min.i + zoomend.y / gCurrentImageData.iHeight * CHeight);
512
513 adjustCoordsAndDraw(newC_min, newC_max);
514 }
515 zoomstart = undefined;
516 break;
517 case 'mousemove':
518 case 'touchmove':
519 if (aEvent.button == 0 && zoomstart) {
520 context.putImageData(imgBackup, 0, 0);
521 context.strokeStyle = "rgb(255,255,31)";
522 context.strokeRect(zoomstart.x, zoomstart.y,
523 aEvent.clientX - canvas.offsetLeft - zoomstart.x,
524 aEvent.clientY - canvas.offsetTop - zoomstart.y);
86e67c44 525 }
7e4a9776 526 break;
3fd66836 527 }
4d8e7dcb 528 }
3fd66836 529};
4d8e7dcb 530
37b05b56 531function saveImage() {
740b86d1 532 const nsIFilePicker = Components.interfaces.nsIFilePicker;
aad35028 533 let fp = null;
740b86d1
RK
534 try {
535 fp = Components.classes["@mozilla.org/filepicker;1"]
536 .createInstance(nsIFilePicker);
537 } catch (e) {}
538 if (!fp) return;
aad35028 539 let promptString = gMbrotBundle.getString("savePrompt");
740b86d1 540 fp.init(window, promptString, nsIFilePicker.modeSave);
fa4ecb24 541 fp.appendFilter(gMbrotBundle.getString("pngFilterName"), "*.png");
740b86d1
RK
542 fp.defaultString = "mandelbrot.png";
543
aad35028 544 let fpResult = fp.show();
740b86d1
RK
545 if (fpResult != nsIFilePicker.returnCancel) {
546 saveCanvas(document.getElementById("mbrotImage"), fp.file);
547 }
37b05b56
RK
548}
549
287a980b
RK
550function updateBookmarkMenu(aParent) {
551 document.getElementById("bookmarkSave").disabled =
cd6af089
RK
552 (!document.getElementById("drawButton").hidden || (gStartTime > 0));
553
554 document.getElementById("bookmarkBack").disabled = !gLastImageData;
287a980b
RK
555
556 while (aParent.hasChildNodes() &&
86e67c44 557 aParent.lastChild.id != "bookmarkSeparator")
287a980b
RK
558 aParent.removeChild(aParent.lastChild);
559
aad35028 560 let file = Components.classes["@mozilla.org/file/directory_service;1"]
287a980b
RK
561 .getService(Components.interfaces.nsIProperties)
562 .get("ProfD", Components.interfaces.nsIFile);
563 file.append("mandelbookmarks.sqlite");
564 if (file.exists()) {
aad35028 565 let connection = Components.classes["@mozilla.org/storage/service;1"]
287a980b
RK
566 .getService(Components.interfaces.mozIStorageService)
567 .openDatabase(file);
568 try {
569 if (connection.tableExists("bookmarks")) {
aad35028 570 let statement = connection.createStatement(
86e67c44
RK
571 "SELECT name,ROWID FROM bookmarks ORDER BY ROWID ASC");
572 while (statement.executeStep()) {
573 let newItem = aParent.appendChild(document.createElement("menuitem"));
574 newItem.setAttribute("label", statement.getString(0));
575 newItem.setAttribute("bmRowID", statement.getString(1));
576 }
287a980b
RK
577 statement.reset();
578 statement.finalize();
579 return;
2eed6617 580 }
287a980b
RK
581 } finally {
582 connection.close();
2eed6617 583 }
287a980b
RK
584 }
585 // Create the "Nothing Available" Menu item and disable it.
aad35028 586 let na = aParent.appendChild(document.createElement("menuitem"));
fa4ecb24 587 na.setAttribute("label", gMbrotBundle.getString("noBookmarks"));
287a980b 588 na.setAttribute("disabled", "true");
2eed6617
RK
589}
590
cd6af089
RK
591function goBack () {
592 if (gLastImageData) {
593 Services.prefs.setIntPref("mandelbrot.iteration_max",
594 gLastImageData.iterMax);
595 // use gLastImageData.iWidth, gLastImageData.iHeight ???
596 adjustCoordsAndDraw(gLastImageData.C_min, gLastImageData.C_max);
597 gLastImageData = undefined;
598 }
599}
600
2eed6617 601function callBookmark(evtarget) {
86e67c44 602 if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator")
b8f9a76f
RK
603 return;
604 if (evtarget.id == "bookmarkOverview") {
605 adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
606 return;
607 }
f34775a6
RK
608
609 if (evtarget.getAttribute('bmRowID')) {
610 let iterMax = 0;
611 let C_min = null;
612 let C_max = null;
613
614 let file = Components.classes["@mozilla.org/file/directory_service;1"]
615 .getService(Components.interfaces.nsIProperties)
616 .get("ProfD", Components.interfaces.nsIFile);
617 file.append("mandelbookmarks.sqlite");
618 let connection = Components.classes["@mozilla.org/storage/service;1"]
619 .getService(Components.interfaces.mozIStorageService)
620 .openDatabase(file);
621 let statement = connection.createStatement(
622 "SELECT iteration_max,Cr_min,Cr_max,Ci_min,Ci_max FROM bookmarks WHERE ROWID=?1");
623 statement.bindStringParameter(0, evtarget.getAttribute('bmRowID'));
624 while (statement.executeStep()) {
625 iterMax = statement.getInt32(0);
626 C_min = new complex(statement.getDouble(1), statement.getDouble(3));
627 C_max = new complex(statement.getDouble(2), statement.getDouble(4));
628 }
629 statement.finalize();
630 connection.close();
631
632 if (iterMax && C_min && C_max) {
140e460f 633 Services.prefs.setIntPref("mandelbrot.iteration_max", iterMax);
f34775a6
RK
634 adjustCoordsAndDraw(C_min, C_max);
635 }
636 }
2eed6617
RK
637}
638
639function saveBookmark() {
9c2ca9fa 640 // retrieve wanted bookmark name with a prompt
aad35028 641 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
9c2ca9fa 642 .getService(Components.interfaces.nsIPromptService);
aad35028
RK
643 let input = {value: ""}; // empty default value
644 let ok = prompts.prompt(null, gMbrotBundle.getString("saveBookmarkTitle"), gMbrotBundle.getString("saveBookmarkLabel"), input, null, {});
9c2ca9fa
RK
645 // ok is true if OK is pressed, false if Cancel. input.value holds the value of the edit field if "OK" was pressed.
646 if (!ok || !input.value)
647 return
648
aad35028 649 let bmName = input.value;
287a980b
RK
650
651 // Open or create the bookmarks database.
aad35028 652 let file = Components.classes["@mozilla.org/file/directory_service;1"]
287a980b
RK
653 .getService(Components.interfaces.nsIProperties)
654 .get("ProfD", Components.interfaces.nsIFile);
655 file.append("mandelbookmarks.sqlite");
aad35028 656 let connection = Components.classes["@mozilla.org/storage/service;1"]
287a980b
RK
657 .getService(Components.interfaces.mozIStorageService)
658 .openDatabase(file);
659 connection.beginTransaction();
660 if (!connection.tableExists("bookmarks"))
661 connection.createTable("bookmarks", "name TEXT, iteration_max INTEGER, Cr_min REAL, Cr_max REAL, Ci_min REAL, Ci_max REAL");
662 // NULL. The value is a NULL value.
663 // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
664 // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
665 // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
666
667 // Put value of the current image into the bookmarks table
aad35028 668 let statement = connection.createStatement(
287a980b
RK
669 "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)");
670 statement.bindStringParameter(0, bmName);
86e67c44
RK
671 statement.bindStringParameter(1, gCurrentImageData.iterMax);
672 statement.bindStringParameter(2, gCurrentImageData.C_min.r);
673 statement.bindStringParameter(3, gCurrentImageData.C_max.r);
674 statement.bindStringParameter(4, gCurrentImageData.C_min.i);
675 statement.bindStringParameter(5, gCurrentImageData.C_max.i);
287a980b
RK
676 statement.execute();
677 statement.finalize();
678 connection.commitTransaction();
679 connection.close();
2eed6617
RK
680}
681
6403d662 682function imgSettings() {
60e048b2
RK
683 let anchor = null;
684 let position = "before_start";
5fb4a082 685 if (document.getElementById("mandelbrot-page").nodeName == "page") {
60e048b2
RK
686 anchor = document.getElementById("mandelbrotToolbar");
687 }
688 else {
689 anchor = document.getElementById("mandelbrotMenubar");
690 position = "after_start";
691 }
692 document.getElementById("imgSettingsPanel").showPopup(anchor, position);
6403d662
RK
693}
694
8a2b6e17
RK
695function initImgSettings() {
696 // Get values from prefs.
a79ddf52
RK
697 for each (let coord in ["Cr", "Ci"]) {
698 let coord_vals = getAdjustPref("last_image." + coord + "_*");
699 document.getElementById("is_" + coord + "_min").value = coord_vals[coord + "_min"];
700 document.getElementById("is_" + coord + "_max").value = coord_vals[coord + "_max"];
8a2b6e17
RK
701 }
702 for each (let dim in ["width", "height"]) {
a79ddf52 703 document.getElementById("is_img_" + dim).value = getAdjustPref("image." + dim);
8a2b6e17 704 }
a79ddf52 705 document.getElementById("is_syncProp").checked = getAdjustPref("syncProportions");
8a2b6e17
RK
706
707 // Calculate scales.
708 recalcCoord("Cr", "scale");
709 recalcCoord("Ci", "scale");
710
711 // Clear the preview.
712 let canvas = document.getElementById("is_mbrotPreview");
713 let context = canvas.getContext("2d");
714 context.fillStyle = "rgba(255, 255, 255, 127)";
715 context.fillRect(0, 0, canvas.width, canvas.height);
5fb4a082
RK
716
717 // Set lists to correct values.
718 updateIterList();
719 updatePaletteList();
720 updateAlgoList();
8a2b6e17
RK
721}
722
723function closeImgSettings() {
724 // Hide popup, which will automatically make a call to save values.
725 document.getElementById("imgSettingsPanel").hidePopup();
726}
727
728function saveImgSettings() {
729 // Get values to prefs.
730 for each (let coord in ["Cr_min", "Cr_max", "Ci_min", "Ci_max"]) {
140e460f
RK
731 Services.prefs.setCharPref("mandelbrot.last_image." + coord,
732 document.getElementById("is_" + coord).value);
8a2b6e17
RK
733 }
734 for each (let dim in ["width", "height"]) {
140e460f
RK
735 Services.prefs.setIntPref("mandelbrot.image." + dim,
736 document.getElementById("is_img_" + dim).value);
8a2b6e17 737 }
140e460f
RK
738 Services.prefs.setBoolPref("mandelbrot.syncProportions",
739 document.getElementById("is_syncProp").checked);
8a2b6e17
RK
740}
741
742function checkISValue(textbox, type) {
743 if (type == "coord") {
744 textbox.value = roundCoord(parseFloat(textbox.value));
745 }
746 else if (type == "dim") {
747 textbox.value = parseInt(textbox.value);
748 }
749}
750
751function drawPreview() {
752 let canvas = document.getElementById("is_mbrotPreview");
753 let context = canvas.getContext("2d");
754
755 if (document.getElementById("is_img_width").value /
756 document.getElementById("is_img_height").value
757 < 80 / 50) {
758 canvas.height = 50;
759 canvas.width = canvas.height *
760 document.getElementById("is_img_width").value /
761 document.getElementById("is_img_height").value;
762 }
763 else {
764 canvas.width = 80;
765 canvas.height = canvas.width *
766 document.getElementById("is_imgHeight").value /
767 document.getElementById("is_imgWidth").value;
768 }
769
770 let Cr_min = parseFloat(document.getElementById("is_Cr_min").value);
771 let Cr_max = parseFloat(document.getElementById("is_Cr_max").value);
772 if ((Cr_min < -2) || (Cr_min > 2) ||
773 (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
774 Cr_min = -2.0; Cr_max = 1.0;
775 }
776
777 let Ci_min = parseFloat(document.getElementById("is_Ci_min").value);
778 let Ci_max = parseFloat(document.getElementById("is_Ci_max").value);
779 if ((Ci_min < -2) || (Ci_min > 2) ||
780 (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) {
781 Ci_min = -2.0; Ci_max = 1.0;
782 }
783
a79ddf52
RK
784 let iterMax = getAdjustPref("iteration_max");
785 let algorithm = getAdjustPref("use_algorithm");
8a2b6e17
RK
786
787 context.fillStyle = "rgba(255, 255, 255, 127)";
788 context.fillRect(0, 0, canvas.width, canvas.height);
789
a79ddf52 790 let currentPalette = getAdjustPref("color_palette");
8a2b6e17
RK
791 gColorPalette = getColorPalette(currentPalette);
792
793 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
794 canvas, context, iterMax, algorithm);
795}
796
797function recalcCoord(coord, target) {
798 let othercoord = (coord == "Ci") ? "Cr" : "Ci";
799 let owndim = (coord == "Ci") ? "height" : "width";
800 let otherdim = (coord == "Ci") ? "width" : "height";
3fd66836 801 let myscale;
8a2b6e17 802 if (target == "scale") {
3fd66836 803 myscale =
8a2b6e17
RK
804 parseFloat(document.getElementById("is_" + coord + "_max").value) -
805 parseFloat(document.getElementById("is_" + coord + "_min").value);
806 document.getElementById("is_" + coord + "_scale").value = roundCoord(myscale);
807 }
808 else if (target == 'max') {
809 let mymax =
810 parseFloat(document.getElementById("is_" + coord + "_min").value) +
811 parseFloat(document.getElementById("is_" + coord + "_scale").value);
812 document.getElementById("is_" + coord + "_max").value = roundCoord(mymax);
3fd66836 813 myscale = document.getElementById("is_" + coord + "_scale").value;
8a2b6e17
RK
814 }
815 if (document.getElementById("is_syncProp").checked) {
816 let otherscale = myscale *
817 document.getElementById("is_img_" + otherdim).value /
818 document.getElementById("is_img_" + owndim).value;
819 document.getElementById("is_" + othercoord + "_scale").value = roundCoord(otherscale);
820 let othermax =
821 parseFloat(document.getElementById("is_" + othercoord + "_min").value) +
822 parseFloat(document.getElementById("is_" + othercoord + "_scale").value);
823 document.getElementById("is_" + othercoord + "_max").value = roundCoord(othermax);
824 }
825}
826
827function checkProportions() {
828 if (!document.getElementById("is_syncProp").checked) {
829 recalcCoord("Cr", "scale");
830 }
831}
832
833function roundCoord(floatval) {
834 // We should round to 10 decimals here or so
835 return parseFloat(floatval.toFixed(10));
836}
837
5fb4a082
RK
838function updateIterList() {
839 let currentIter = getAdjustPref("iteration_max");
840 document.getElementById("iterList").value = currentIter;
841}
842
843function updatePaletteList() {
844 let currentPalette = getAdjustPref("color_palette");
845 if (!gColorPalette || !gColorPalette.length)
846 gColorPalette = getColorPalette(currentPalette);
847 document.getElementById("colorList").value = currentPalette;
848}
849
850function updateAlgoList() {
851 let currentAlgo = getAdjustPref("use_algorithm");
852 document.getElementById("algoList").value = currentAlgo;
853}
854
855function setIter(aIter) {
856 Services.prefs.setIntPref("mandelbrot.iteration_max", aIter);
857}
858
859function setPalette(aPaletteID) {
860 Services.prefs.setCharPref("mandelbrot.color_palette", aPaletteID);
861 gColorPalette = getColorPalette(aPaletteID);
862}
863
864function setAlgorithm(algoID) {
865 Services.prefs.setCharPref("mandelbrot.use_algorithm", algoID);
866}
867
868
6e98af87
RK
869/***** helper functions from external sources *****/
870
740b86d1
RK
871// function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
872// custom modifications:
873// - use "a"-prefix on function arguments
874// - take an nsILocalFile as aDestFile argument
875// - always do silent download
876function saveCanvas(aCanvas, aDestFile) {
37b05b56
RK
877 // create a data url from the canvas and then create URIs of the source and targets
878 var io = Components.classes["@mozilla.org/network/io-service;1"]
879 .getService(Components.interfaces.nsIIOService);
740b86d1 880 var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
37b05b56
RK
881
882 // prepare to save the canvas data
883 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
884 .createInstance(Components.interfaces.nsIWebBrowserPersist);
885
886 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
887 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
888
37b05b56 889 // save the canvas data to the file
740b86d1 890 persist.saveURI(source, null, null, null, null, aDestFile);
37b05b56 891}