1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is KaiRo.at Mandelbrot, XULRunner version.
16 * The Initial Developer of the Original Code is
17 * Robert Kaiser <kairo@kairo.at>.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
22 * Robert Kaiser <kairo@kairo.at>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 var gColorPalette = [];
39 var gPref = Components.classes["@mozilla.org/preferences-service;1"]
40 .getService(Components.interfaces.nsIPrefService)
47 document.getElementById("statusLabel").value =
48 document.getElementById("mbrotBundle").getString("statusEmpty");
51 function drawImage() {
52 let canvas = document.getElementById("mbrotImage");
53 let context = canvas.getContext("2d");
55 document.getElementById("drawButton").hidden = true;
57 document.getElementById("statusLabel").value =
58 document.getElementById("mbrotBundle").getString("statusDrawing");
63 Cr_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"));
64 Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
67 if ((Cr_min < -2) || (Cr_min > 2) ||
68 (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
69 Cr_min = -2.0; Cr_max = 1.0;
71 gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
72 gPref.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
77 Ci_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"));
78 Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
81 if ((Ci_min < -2) || (Ci_min > 2) ||
82 (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) {
83 Ci_min = -2.0; Ci_max = 1.0;
85 gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
86 gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
88 let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
89 let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
93 iWidth = gPref.getIntPref("mandelbrot.image.width");
96 if ((iWidth < 10) || (iWidth > 5000)) {
98 gPref.setIntPref("mandelbrot.image.width", iWidth);
102 iHeight = gPref.getIntPref("mandelbrot.image.height");
105 if ((iHeight < 10) || (iHeight > 5000)) {
107 gPref.setIntPref("mandelbrot.image.height", iHeight);
110 canvas.width = iWidth;
111 canvas.height = iHeight;
113 context.fillStyle = "rgba(255, 255, 255, 127)";
114 context.fillRect(0, 0, canvas.width, canvas.height);
116 gStartTime = new Date();
118 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
119 canvas, context, iterMax, algorithm);
122 function drawLine(line, dimensions, canvas, context, iterMax, algorithm) {
123 let Cr_min = dimensions[0];
124 let Cr_max = dimensions[1];
125 let Cr_scale = Cr_max - Cr_min;
127 let Ci_min = dimensions[2];
128 let Ci_max = dimensions[3];
129 let Ci_scale = Ci_max - Ci_min;
132 for (var img_y = line; img_y < canvas.height && img_y < line+8; img_y++)
133 for (let img_x = 0; img_x < canvas.width; img_x++) {
134 let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
135 Ci_min + (img_y / canvas.height) * Ci_scale);
136 pixels.push.apply(pixels, drawPoint(context, img_x, img_y, C, iterMax, algorithm));
138 context.putImageData({width: canvas.width, height: pixels.length/4/canvas.width, data: pixels}, 0, line);
140 if (img_y < canvas.height)
141 setTimeout(drawLine, 0, img_y, dimensions, canvas, context, iterMax, algorithm);
147 let endTime = new Date();
148 let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
149 document.getElementById("statusLabel").value =
150 document.getElementById("mbrotBundle").getFormattedString("statusTime", [timeUsed.toFixed(3)]);
153 function complex(aReal, aImag) {
157 complex.prototype = {
159 return new complex(this.r * this.r - this.i * this.i,
160 2 * this.r * this.i);
163 return Math.sqrt(this.r * this.r + this.i * this.i);
165 add: function(aComplex) {
166 return new complex(this.r + aComplex.r, this.i + aComplex.i);
170 function mandelbrotValueOO (aC, aIterMax) {
171 // this would be nice code in general but it looks like JS objects are too heavy for normal use.
172 let Z = new complex(0.0, 0.0);
173 for (var iter = 0; iter < aIterMax; iter++) {
174 Z = Z.square().add(aC);
175 if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
180 function mandelbrotValueNumeric (aC, aIterMax) {
181 // optimized numeric code for fast calculation
182 let Cr = aC.r, Ci = aC.i;
183 let Zr = 0.0, Zi = 0.0;
184 let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
185 for (var iter = 0; iter < aIterMax; iter++) {
186 Zi = 2 * Zr * Zi + Ci;
189 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
190 if (Zr2 + Zi2 > 256) { break; }
195 function getColor(aIterValue, aIterMax) {
196 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
197 if (gColorPalette && gColorPalette.length)
198 return gColorPalette[standardizedValue];
200 // fallback to simple b/w if for some reason we don't have a palette
201 if (aIterValue == aIterMax)
202 return [0, 0, 0, 255];
204 return [255, 255, 255, 255];
207 function getColorPalette(palName) {
211 for (let i = 0; i < 1024; i++) {
212 palette[i] = [255, 255, 255, 255];
214 palette[1024] = [0, 0, 0, 255];
218 for (let i = 0; i < 32; i++) {
219 let cc1 = Math.floor(i * 127 / 31);
220 let cc2 = 170 - Math.floor(i * 43 / 31);
221 palette[i] = [cc1, cc2, cc1, 255];
224 for (let i = 0; i < 51; i++) {
225 let cc = Math.floor(i * 170 / 50);
226 palette[32 + i] = [cc, 0, (170-cc), 255];
229 for (let i = 0; i < 101; i++) {
230 let cc = Math.floor(i * 200 / 100);
231 palette[83 + i] = [255, cc, 0, 255];
234 for (let i = 0; i < 201; i++) {
235 let cc1 = 255 - Math.floor(i * 85 / 200);
236 let cc2 = 200 - Math.floor(i * 30 / 200);
237 let cc3 = Math.floor(i * 170 / 200);
238 palette[184 + i] = [cc1, cc2, cc3, 255];
240 for (let i = 0; i < 301; i++) {
241 let cc1 = 170 - Math.floor(i * 43 / 300);
242 let cc2 = 170 + Math.floor(i * 85 / 300);
243 palette[385 + i] = [cc1, cc1, cc2, 255];
245 for (let i = 0; i < 338; i++) {
246 let cc = 127 + Math.floor(i * 128 / 337);
247 palette[686 + i] = [cc, cc, 255, 255];
249 palette[1024] = [0, 0, 0, 255];
251 case 'rainbow-linear1':
252 for (let i = 0; i < 256; i++) {
253 palette[i] = [i, 0, 0, 255];
254 palette[256 + i] = [255, i, 0, 255];
255 palette[512 + i] = [255 - i, 255, i, 255];
256 palette[768 + i] = [i, 255-i, 255, 255];
258 palette[1024] = [0, 0, 0, 255];
263 Case 1 'Standard-Palette (QB-Colors)
265 xx = CInt(i * 500 / 1024 + 2)
266 If xx <= 15 Then clr = xx
267 If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485))
268 If xx >= 500 Then clr = 0
269 palette(i) = QBColor(clr)
271 Case 3 'Regenbogen-Palette 1 (qu.)
273 clr = CInt(i * 255 / 33)
274 palette(i) = RGB(clr, 0, 0)
277 clr = CInt(i * 255 / 136)
278 palette(34 + i) = RGB(255, clr, 0)
281 clr = CInt(i * 255 / 306)
282 palette(171 + i) = RGB(255 - clr, 255, clr)
285 clr = CInt(i * 255 / 545)
286 palette(478 + i) = RGB(clr, 255 - clr, 255)
288 Case 4 'Regenbogen-Palette 2 (linear)
290 clr = CInt(i * 255 / 204)
291 palette(i) = RGB(255, clr, 0)
292 palette(204 + i) = RGB(255 - clr, 255, 0)
293 palette(409 + i) = RGB(0, 255, clr)
294 palette(614 + i) = RGB(0, 255 - clr, 255)
295 palette(819 + i) = RGB(clr, 0, 255)
297 Case 5 'Regenbogen-Palette 2 (qu.)
299 clr = CInt(i * 255 / 18)
300 palette(i) = RGB(255, clr, 0)
303 clr = CInt(i * 255 / 73)
304 palette(20 + i) = RGB(255 - clr, 255, 0)
307 clr = CInt(i * 255 / 167)
308 palette(93 + i) = RGB(0, 255, clr)
311 clr = CInt(i * 255 / 297)
312 palette(261 + i) = RGB(0, 255 - clr, 255)
315 clr = CInt(i * 255 / 464)
316 palette(559 + i) = RGB(clr, 0, 255)
322 function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
326 itVal = mandelbrotValueOO(C, iterMax);
330 itVal = mandelbrotValueNumeric(C, iterMax);
333 return getColor(itVal, iterMax);
336 /***** pure UI functions *****/
340 function mouseevent(etype, event) {
341 let canvas = document.getElementById("mbrotImage");
344 if (event.button == 0)
345 // left button - start dragzoom
346 zoomstart = {x: event.clientX - canvas.offsetLeft,
347 y: event.clientY - canvas.offsetTop};
350 if (event.button == 0)
351 alert(zoomstart.x + ',' + zoomstart.y + '-' +
352 (event.clientX - canvas.offsetLeft) + ',' +
353 (event.clientY - canvas.offsetTop));
354 zoomstart = undefined;
359 function saveImage() {
360 const bundle = document.getElementById("mbrotBundle");
361 const nsIFilePicker = Components.interfaces.nsIFilePicker;
364 fp = Components.classes["@mozilla.org/filepicker;1"]
365 .createInstance(nsIFilePicker);
368 var promptString = bundle.getString("savePrompt");
369 fp.init(window, promptString, nsIFilePicker.modeSave);
370 fp.appendFilter(bundle.getString("pngFilterName"), "*.png");
371 fp.defaultString = "mandelbrot.png";
373 var fpResult = fp.show();
374 if (fpResult != nsIFilePicker.returnCancel) {
375 saveCanvas(document.getElementById("mbrotImage"), fp.file);
379 function updateBookmarksMenu(aParent) {
380 /* from SM UBhistory, needs to be adapted
381 while (aParent.hasChildNodes())
382 aParent.removeChild(aParent.lastChild);
384 var file = Components.classes["@mozilla.org/file/directory_service;1"]
385 .getService(Components.interfaces.nsIProperties)
386 .get("ProfD", Components.interfaces.nsIFile);
387 file.append("urlbarhistory.sqlite");
389 var connection = Components.classes["@mozilla.org/storage/service;1"]
390 .getService(Components.interfaces.mozIStorageService)
393 if (connection.tableExists("urlbarhistory")) {
394 var statement = connection.createStatement(
395 "SELECT url FROM urlbarhistory ORDER BY ROWID DESC");
396 while (statement.executeStep())
397 aParent.appendChild(document.createElement("menuitem"))
398 .setAttribute("label", statement.getString(0));
400 statement.finalize();
407 //Create the "Nothing Available" Menu item and disable it.
408 var na = aParent.appendChild(document.createElement("menuitem"));
409 na.setAttribute("label", gNavigatorBundle.getString("nothingAvailable"));
410 na.setAttribute("disabled", "true");
414 function callBookmark(evtarget) {
417 function saveBookmark() {
418 /* from SM UBhistory, needs to be adapted
419 // Open or create the urlbar history database.
420 var file = Components.classes["@mozilla.org/file/directory_service;1"]
421 .getService(Components.interfaces.nsIProperties)
422 .get("ProfD", Components.interfaces.nsIFile);
423 file.append("urlbarhistory.sqlite");
424 var connection = Components.classes["@mozilla.org/storage/service;1"]
425 .getService(Components.interfaces.mozIStorageService)
427 connection.beginTransaction();
428 if (!connection.tableExists("urlbarhistory"))
429 connection.createTable("urlbarhistory", "url TEXT, foo INTEGER, bar REAL");
430 // NULL. The value is a NULL value.
431 // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
432 // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
433 // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
435 // Put the value as it was typed by the user in to urlbar history
436 statement = connection.createStatement(
437 "INSERT INTO urlbarhistory (url) VALUES (?1)");
438 statement.bindStringParameter(0, aUrlToAdd);
440 statement.finalize();
444 function updateIterMenu() {
446 var currentIter = gPref.getIntPref("mandelbrot.iteration_max");
451 if (currentIter < 10) {
453 setIter(currentIter);
456 var popup = document.getElementById("menu_iterPopup");
457 var item = popup.firstChild;
459 if (item.getAttribute("name") == "iter") {
460 if (item.getAttribute("value") == currentIter)
461 item.setAttribute("checked","true");
463 item.removeAttribute("checked");
465 item = item.nextSibling;
469 function setIter(aIter) {
470 gPref.setIntPref("mandelbrot.iteration_max", aIter);
473 function updatePaletteMenu() {
475 var currentPalette = gPref.getCharPref("mandelbrot.color_palette");
478 var currentPalette = '';
480 if (!currentPalette.length) {
481 currentPalette = 'kairo';
482 setPalette(currentPalette);
484 if (!gColorPalette || !gColorPalette.length)
485 gColorPalette = getColorPalette(currentPalette);
487 var popup = document.getElementById("menu_palettePopup");
488 var item = popup.firstChild;
490 if (item.getAttribute("name") == "palette") {
491 if (item.getAttribute("value") == currentPalette)
492 item.setAttribute("checked", "true");
494 item.removeAttribute("checked");
496 item = item.nextSibling;
500 function setPalette(aPaletteID) {
501 gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
502 gColorPalette = getColorPalette(aPaletteID);
505 function imgSettings() {
506 window.openDialog("chrome://mandelbrot/content/image-settings.xul");
509 function updateDebugMenu() {
510 var jitMenuItem = document.getElementById("jitEnabled");
511 jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit.chrome"));
514 function toggleJITState(jitMenuItem) {
515 var jitEnabled = !gPref.getBoolPref("javascript.options.jit.chrome");
516 gPref.setBoolPref("javascript.options.jit.chrome", jitEnabled)
517 jitMenuItem.setAttribute("checked", jitEnabled? "true" : "false");
520 function updateAlgoMenu() {
522 var currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
525 var currentAlgo = '';
527 if (!currentAlgo.length) {
528 currentAlgo = 'numeric';
529 setAlgorithm(currentAlgo);
532 var popup = document.getElementById("menu_algoPopup");
533 var item = popup.firstChild;
535 if (item.getAttribute("name") == "algorithm") {
536 if (item.getAttribute("value") == currentAlgo)
537 item.setAttribute("checked", "true");
539 item.removeAttribute("checked");
541 item = item.nextSibling;
545 function setAlgorithm(algoID) {
546 gPref.setCharPref("mandelbrot.use_algorithm", algoID);
549 function addonsManager(aPane) {
550 var theEM = Components.classes["@mozilla.org/appshell/window-mediator;1"]
551 .getService(Components.interfaces.nsIWindowMediator)
552 .getMostRecentWindow("Extension:Manager");
556 theEM.showView(aPane);
560 const EMURL = "chrome://mozapps/content/extensions/extensions.xul";
561 const EMFEATURES = "all,dialog=no";
563 window.openDialog(EMURL, "", EMFEATURES, aPane);
565 window.openDialog(EMURL, "", EMFEATURES);
568 function errorConsole() {
569 toOpenWindowByType("global:console", "chrome://global/content/console.xul");
572 /***** helper functions from external sources *****/
574 // function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
575 // custom modifications:
576 // - use "a"-prefix on function arguments
577 // - take an nsILocalFile as aDestFile argument
578 // - always do silent download
579 function saveCanvas(aCanvas, aDestFile) {
580 // create a data url from the canvas and then create URIs of the source and targets
581 var io = Components.classes["@mozilla.org/network/io-service;1"]
582 .getService(Components.interfaces.nsIIOService);
583 var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
585 // prepare to save the canvas data
586 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
587 .createInstance(Components.interfaces.nsIWebBrowserPersist);
589 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
590 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
592 // save the canvas data to the file
593 persist.saveURI(source, null, null, null, null, aDestFile);
596 // function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application
597 function quitApp(aForceQuit) {
598 var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
599 .getService(Components.interfaces.nsIAppStartup);
601 // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit
602 // process if there is unsaved data. eForceQuit will quit no matter what.
603 var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit :
604 Components.interfaces.nsIAppStartup.eAttemptQuit;
605 appStartup.quit(quitSeverity);
608 // functions below are from comm-central/suite/common/tasksOverlay.js
609 function toOpenWindow(aWindow) {
611 // Try to focus the previously focused window e.g. message compose body
612 aWindow.document.commandDispatcher.focusedWindow.focus();
614 // e.g. full-page plugin or non-XUL document; just raise the top window
619 function toOpenWindowByType(inType, uri, features) {
620 // don't do several loads in parallel
624 var topWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"]
625 .getService(Components.interfaces.nsIWindowMediator)
626 .getMostRecentWindow(inType);
628 toOpenWindow( topWindow );
630 // open the requested window, but block it until it's fully loaded
631 function newWindowLoaded(event) {
632 // make sure that this handler is called only once
633 window.removeEventListener("unload", newWindowLoaded, false);
634 window[uri].removeEventListener("load", newWindowLoaded, false);
637 // remember the newly loading window until it's fully loaded
638 // or until the current window passes away
639 window[uri] = window.openDialog(uri, "", features || "all,dialog=no");
640 window[uri].addEventListener("load", newWindowLoaded, false);
641 window.addEventListener("unload", newWindowLoaded, false);