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 if (canvas.getContext) {
54 let context = canvas.getContext("2d");
56 document.getElementById("statusLabel").value =
57 document.getElementById("mbrotBundle").getString("statusDrawing");
60 // context.fillStyle = "rgb(200,0,0)";
61 // context.fillRect (10, 10, 55, 50); // x, y, width, height
63 // context.fillStyle = "rgba(0, 0, 200, 0.5)";
64 // context.fillRect (30, 30, 55, 50);
68 let Cr_scale = Cr_max - Cr_min;
72 let Ci_scale = Ci_max - Ci_min;
74 let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
75 let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
77 gStartTime = new Date();
79 for (let img_x = 0; img_x < canvas.width; img_x++) {
80 for (let img_y = 0; img_y < canvas.height; img_y++) {
81 let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
82 Ci_min + (img_y / canvas.height) * Ci_scale);
83 window.setTimeout(drawPoint, 0, context, img_x, img_y, C, iterMax, algorithm);
86 window.setTimeout(EndCalc, 0);
91 let endTime = new Date();
92 let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
93 document.getElementById("statusLabel").value =
94 document.getElementById("mbrotBundle").getFormattedString("statusTime", [timeUsed.toFixed(3)]);
97 function complex(aReal, aImag) {
100 this.square = function() {
101 return new complex(this.r * this.r - this.i * this.i,
102 2 * this.r * this.i);
104 this.dist = function() {
105 return Math.sqrt(this.r * this.r + this.i * this.i);
107 this.add = function(aComplex) {
108 return new complex(this.r + aComplex.r, this.i + aComplex.i);
112 function mandelbrotValueOO (aC, aIterMax) {
113 // this would be nice code in general but it looks like JS objects are too heavy for normal use.
114 let Z = new complex(0.0, 0.0);
115 for (var iter = 0; iter < aIterMax; iter++) {
116 Z = Z.square().add(aC);
117 if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
122 function mandelbrotValueNumeric (aC, aIterMax) {
123 // optimized numeric code for fast calculation
124 let Cr = aC.r, Ci = aC.i;
125 let Zr = 0.0, Zi = 0.0;
126 let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
127 for (var iter = 0; iter < aIterMax; iter++) {
128 Zi = 2 * Zr * Zi + Ci;
131 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
132 if (Zr2 + Zi2 > 256) { break; }
137 function getColor(aIterValue, aIterMax) {
138 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
139 if (gColorPalette && gColorPalette.length)
140 return gColorPalette[standardizedValue];
142 // fallback to simple b/w if for some reason we don't have a palette
143 if (aIterValue == aIterMax)
146 return "rgb(255,255,255)";
149 function getColorPalette(palName) {
153 for (let i = 0; i < 1024; i++) {
154 palette[i] = 'rgb(255,255,255)';
156 palette[1024] = 'rgb(0,0,0)';
160 for (let i = 0; i < 32; i++) {
161 let cc1 = Math.floor(i * 127 / 31);
162 let cc2 = 170 - Math.floor(i * 43 / 31);
163 palette[i] = 'rgb(' + cc1 + ',' + cc2 + ',' + cc1 + ')';
166 for (let i = 0; i < 51; i++) {
167 let cc = Math.floor(i * 170 / 50);
168 palette[32 + i] = 'rgb(' + cc + ',0,' + (170 + cc) + ')';
171 for (let i = 0; i < 101; i++) {
172 let cc = Math.floor(i * 200 / 100);
173 palette[83 + i] = 'rgb(255,' + cc + ',0)';
176 for (let i = 0; i < 201; i++) {
177 let cc1 = 255 - Math.floor(i * 85 / 200);
178 let cc2 = 200 - Math.floor(i * 30 / 200);
179 let cc3 = Math.floor(i * 170 / 200);
180 palette[184 + i] = 'rgb(' + cc1 + ',' + cc2 + ',' + cc3 + ')';
182 for (let i = 0; i < 301; i++) {
183 let cc1 = 170 - Math.floor(i * 43 / 300);
184 let cc2 = 170 + Math.floor(i * 85 / 300);
185 palette[385 + i] = 'rgb(' + cc1 + ',' + cc1 + ',' + cc2 + ')';
187 for (let i = 0; i < 338; i++) {
188 let cc = 127 + Math.floor(i * 128 / 337);
189 palette[686 + i] = 'rgb(' + cc + ',' + cc + ',255)';
191 palette[1024] = 'rgb(0,0,0)';
193 case 'rainbow-linear1':
194 for (let i = 0; i < 256; i++) {
195 palette[i] = 'rgb(' + i + ',0,0)';
196 palette[256 + i] = 'rgb(255,' + i + ',0)';
197 palette[512 + i] = 'rgb(' + (255 - i) + ',255,' + i + ')';
198 palette[768 + i] = 'rgb(' + i + ',' + (255 - i) + ',255)';
200 palette[1024] = 'rgb(0,0,0)';
205 Case 1 'Standard-Palette (QB-Colors)
207 xx = CInt(i * 500 / 1024 + 2)
208 If xx <= 15 Then clr = xx
209 If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485))
210 If xx >= 500 Then clr = 0
211 palette(i) = QBColor(clr)
213 Case 3 'Regenbogen-Palette 1 (qu.)
215 clr = CInt(i * 255 / 33)
216 palette(i) = RGB(clr, 0, 0)
219 clr = CInt(i * 255 / 136)
220 palette(34 + i) = RGB(255, clr, 0)
223 clr = CInt(i * 255 / 306)
224 palette(171 + i) = RGB(255 - clr, 255, clr)
227 clr = CInt(i * 255 / 545)
228 palette(478 + i) = RGB(clr, 255 - clr, 255)
230 Case 4 'Regenbogen-Palette 2 (linear)
232 clr = CInt(i * 255 / 204)
233 palette(i) = RGB(255, clr, 0)
234 palette(204 + i) = RGB(255 - clr, 255, 0)
235 palette(409 + i) = RGB(0, 255, clr)
236 palette(614 + i) = RGB(0, 255 - clr, 255)
237 palette(819 + i) = RGB(clr, 0, 255)
239 Case 5 'Regenbogen-Palette 2 (qu.)
241 clr = CInt(i * 255 / 18)
242 palette(i) = RGB(255, clr, 0)
245 clr = CInt(i * 255 / 73)
246 palette(20 + i) = RGB(255 - clr, 255, 0)
249 clr = CInt(i * 255 / 167)
250 palette(93 + i) = RGB(0, 255, clr)
253 clr = CInt(i * 255 / 297)
254 palette(261 + i) = RGB(0, 255 - clr, 255)
257 clr = CInt(i * 255 / 464)
258 palette(559 + i) = RGB(clr, 0, 255)
264 function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
268 itVal = mandelbrotValueOO(C, iterMax);
272 itVal = mandelbrotValueNumeric(C, iterMax);
275 context.fillStyle = getColor(itVal, iterMax);
276 context.fillRect (img_x, img_y, 1, 1); // x, y, width, height
279 /***** pure UI functions *****/
281 function saveImage() {
282 // XXX: should call filepicker!
283 saveCanvas(document.getElementById("mbrotImage"), "/home/robert/temp/canvas-save.png")
286 function updateIterMenu() {
288 var currentIter = gPref.getIntPref("mandelbrot.iteration_max");
293 if (currentIter < 10) {
295 setIter(currentIter);
298 var popup = document.getElementById("menu_iterPopup");
299 var item = popup.firstChild;
301 if (item.getAttribute("name") == "iter") {
302 if (item.getAttribute("value") == currentIter)
303 item.setAttribute("checked","true");
305 item.removeAttribute("checked");
307 item = item.nextSibling;
311 function setIter(aIter) {
312 gPref.setIntPref("mandelbrot.iteration_max", aIter);
315 function updatePaletteMenu() {
317 var currentPalette = gPref.getCharPref("mandelbrot.color_palette");
320 var currentPalette = '';
322 if (!currentPalette.length) {
323 currentPalette = 'kairo';
324 setPalette(currentPalette);
326 if (!gColorPalette || !gColorPalette.length)
327 gColorPalette = getColorPalette(currentPalette);
329 var popup = document.getElementById("menu_palettePopup");
330 var item = popup.firstChild;
332 if (item.getAttribute("name") == "palette") {
333 if (item.getAttribute("value") == currentPalette)
334 item.setAttribute("checked", "true");
336 item.removeAttribute("checked");
338 item = item.nextSibling;
342 function setPalette(aPaletteID) {
343 gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
344 gColorPalette = getColorPalette(aPaletteID);
347 function updateDebugMenu() {
348 var jitMenuItem = document.getElementById("jitEnabled");
349 jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit.chrome"));
352 function toggleJITState(jitMenuItem) {
353 var jitEnabled = !gPref.getBoolPref("javascript.options.jit.chrome");
354 gPref.setBoolPref("javascript.options.jit.chrome", jitEnabled)
355 jitMenuItem.setAttribute("checked", jitEnabled? "true" : "false");
358 function updateAlgoMenu() {
360 var currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
363 var currentAlgo = '';
365 if (!currentAlgo.length) {
366 currentAlgo = 'numeric';
367 setAlgorithm(currentAlgo);
370 var popup = document.getElementById("menu_algoPopup");
371 var item = popup.firstChild;
373 if (item.getAttribute("name") == "algorithm") {
374 if (item.getAttribute("value") == currentAlgo)
375 item.setAttribute("checked", "true");
377 item.removeAttribute("checked");
379 item = item.nextSibling;
383 function setAlgorithm(algoID) {
384 gPref.setCharPref("mandelbrot.use_algorithm", algoID);
388 /***** helper functions from external sources *****/
390 // function below is from from http://developer.mozilla.org/en/docs/Code_snippets:Canvas
391 function saveCanvas(canvas, destFile) {
392 // convert string filepath to an nsIFile
393 var file = Components.classes["@mozilla.org/file/local;1"]
394 .createInstance(Components.interfaces.nsILocalFile);
395 file.initWithPath(destFile);
397 // create a data url from the canvas and then create URIs of the source and targets
398 var io = Components.classes["@mozilla.org/network/io-service;1"]
399 .getService(Components.interfaces.nsIIOService);
400 var source = io.newURI(canvas.toDataURL("image/png", ""), "UTF8", null);
401 var target = io.newFileURI(file);
403 // prepare to save the canvas data
404 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
405 .createInstance(Components.interfaces.nsIWebBrowserPersist);
407 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
408 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
410 // displays a download dialog (remove these 3 lines for silent download)
411 var xfer = Components.classes["@mozilla.org/transfer;1"]
412 .createInstance(Components.interfaces.nsITransfer);
413 xfer.init(source, target, "", null, null, null, persist);
414 persist.progressListener = xfer;
416 // save the canvas data to the file
417 persist.saveURI(source, null, null, null, null, file);
420 // function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application
421 function quitApp(aForceQuit) {
422 var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
423 .getService(Components.interfaces.nsIAppStartup);
425 // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit
426 // process if there is unsaved data. eForceQuit will quit no matter what.
427 var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit :
428 Components.interfaces.nsIAppStartup.eAttemptQuit;
429 appStartup.quit(quitSeverity);