add MPL/GPL/LGPL tri-license to all files
[mandelbrot.git] / xulapp / chrome / mandelbrot / 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>.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Robert Kaiser <kairo@kairo.at>
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
6e98af87
RK
38var gColorPalette = [];
39var gPref = Components.classes["@mozilla.org/preferences-service;1"]
40 .getService(Components.interfaces.nsIPrefService)
41 .getBranch(null);
42var gStartTime = 0;
43
44function Startup() {
45 updateIterMenu();
46 updatePaletteMenu();
47 document.getElementById("statusLabel").value =
48 document.getElementById("mbrotBundle").getString("statusEmpty");
49}
37b05b56
RK
50
51function drawImage() {
8a9c8e3f 52 let canvas = document.getElementById("mbrotImage");
37b05b56 53 if (canvas.getContext) {
8a9c8e3f 54 let context = canvas.getContext("2d");
37b05b56 55
6e98af87
RK
56 document.getElementById("statusLabel").value =
57 document.getElementById("mbrotBundle").getString("statusDrawing");
58
37b05b56
RK
59 // example:
60 // context.fillStyle = "rgb(200,0,0)";
61 // context.fillRect (10, 10, 55, 50); // x, y, width, height
62 //
63 // context.fillStyle = "rgba(0, 0, 200, 0.5)";
64 // context.fillRect (30, 30, 55, 50);
65
8a9c8e3f
RK
66 let Cr_min = -2.0;
67 let Cr_max = 1.0;
68 let Cr_scale = Cr_max - Cr_min;
37b05b56 69
8a9c8e3f
RK
70 let Ci_min = -1.5;
71 let Ci_max = 1.5;
72 let Ci_scale = Ci_max - Ci_min;
37b05b56 73
6e98af87
RK
74 let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
75 let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
76
77 gStartTime = new Date();
37b05b56 78
8a9c8e3f
RK
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,
37b05b56 82 Ci_min + (img_y / canvas.height) * Ci_scale);
6e98af87 83 window.setTimeout(drawPoint, 0, context, img_x, img_y, C, iterMax, algorithm);
37b05b56
RK
84 }
85 }
6e98af87 86 window.setTimeout(EndCalc, 0);
37b05b56
RK
87 }
88}
89
6e98af87
RK
90function EndCalc() {
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)]);
95}
96
37b05b56
RK
97function complex(aReal, aImag) {
98 this.r = aReal;
99 this.i = aImag;
100 this.square = function() {
101 return new complex(this.r * this.r - this.i * this.i,
102 2 * this.r * this.i);
103 }
104 this.dist = function() {
105 return Math.sqrt(this.r * this.r + this.i * this.i);
106 }
107 this.add = function(aComplex) {
108 return new complex(this.r + aComplex.r, this.i + aComplex.i);
109 }
110}
111
6e98af87
RK
112function mandelbrotValueOO (aC, aIterMax) {
113 // this would be nice code in general but it looks like JS objects are too heavy for normal use.
8444612a 114 let Z = new complex(0.0, 0.0);
37b05b56
RK
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; }
118 }
6e98af87
RK
119 return iter;
120}
8444612a 121
6e98af87
RK
122function mandelbrotValueNumeric (aC, aIterMax) {
123 // optimized numeric code for fast calculation
8444612a
RK
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;
129 Zr = Zr2 - Zi2 + Cr;
130
131 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
132 if (Zr2 + Zi2 > 256) { break; }
133 }
37b05b56
RK
134 return iter;
135}
136
137function getColor(aIterValue, aIterMax) {
8a9c8e3f 138 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
6e98af87
RK
139 if (gColorPalette && gColorPalette.length)
140 return gColorPalette[standardizedValue];
141
142 // fallback to simple b/w if for some reason we don't have a palette
143 if (aIterValue == aIterMax)
37b05b56 144 return "rgb(0,0,0)";
6e98af87 145 else
37b05b56 146 return "rgb(255,255,255)";
37b05b56
RK
147}
148
149function getColorPalette(palName) {
150 var palette = [];
151 switch (palName) {
152 case 'bw':
8a9c8e3f 153 for (let i = 0; i < 1024; i++) {
37b05b56
RK
154 palette[i] = 'rgb(255,255,255)';
155 }
156 palette[1024] = 'rgb(0,0,0)';
157 break;
158 case 'kairo':
159 // outer areas
8a9c8e3f
RK
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);
37b05b56
RK
163 palette[i] = 'rgb(' + cc1 + ',' + cc2 + ',' + cc1 + ')';
164 }
165 // inner areas
8a9c8e3f
RK
166 for (let i = 0; i < 51; i++) {
167 let cc = Math.floor(i * 170 / 50);
37b05b56
RK
168 palette[32 + i] = 'rgb(' + cc + ',0,' + (170 + cc) + ')';
169 }
170 // corona
8a9c8e3f
RK
171 for (let i = 0; i < 101; i++) {
172 let cc = Math.floor(i * 200 / 100);
37b05b56
RK
173 palette[83 + i] = 'rgb(255,' + cc + ',0)';
174 }
175 // inner corona
8a9c8e3f
RK
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);
37b05b56
RK
180 palette[184 + i] = 'rgb(' + cc1 + ',' + cc2 + ',' + cc3 + ')';
181 }
8a9c8e3f
RK
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);
37b05b56
RK
185 palette[385 + i] = 'rgb(' + cc1 + ',' + cc1 + ',' + cc2 + ')';
186 }
8a9c8e3f
RK
187 for (let i = 0; i < 338; i++) {
188 let cc = 127 + Math.floor(i * 128 / 337);
37b05b56
RK
189 palette[686 + i] = 'rgb(' + cc + ',' + cc + ',255)';
190 }
191 palette[1024] = 'rgb(0,0,0)';
192 break;
193 case 'rainbow-linear1':
8a9c8e3f 194 for (let i = 0; i < 256; i++) {
37b05b56
RK
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)';
199 }
200 palette[1024] = 'rgb(0,0,0)';
201 break;
202 }
203/*
204Select Case palnr
205Case 1 'Standard-Palette (QB-Colors)
206 For i = 0 To 1024
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)
212 Next
213Case 3 'Regenbogen-Palette 1 (qu.)
214 For i = 0 To 33
215 clr = CInt(i * 255 / 33)
216 palette(i) = RGB(clr, 0, 0)
217 Next
218 For i = 0 To 136
219 clr = CInt(i * 255 / 136)
220 palette(34 + i) = RGB(255, clr, 0)
221 Next
222 For i = 0 To 306
223 clr = CInt(i * 255 / 306)
224 palette(171 + i) = RGB(255 - clr, 255, clr)
225 Next
226 For i = 0 To 545
227 clr = CInt(i * 255 / 545)
228 palette(478 + i) = RGB(clr, 255 - clr, 255)
229 Next
230Case 4 'Regenbogen-Palette 2 (linear)
231 For i = 0 To 204
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)
238 Next
239Case 5 'Regenbogen-Palette 2 (qu.)
240 For i = 0 To 18
241 clr = CInt(i * 255 / 18)
242 palette(i) = RGB(255, clr, 0)
243 Next
244 For i = 0 To 73
245 clr = CInt(i * 255 / 73)
246 palette(20 + i) = RGB(255 - clr, 255, 0)
247 Next
248 For i = 0 To 167
249 clr = CInt(i * 255 / 167)
250 palette(93 + i) = RGB(0, 255, clr)
251 Next
252 For i = 0 To 297
253 clr = CInt(i * 255 / 297)
254 palette(261 + i) = RGB(0, 255 - clr, 255)
255 Next
256 For i = 0 To 464
257 clr = CInt(i * 255 / 464)
258 palette(559 + i) = RGB(clr, 0, 255)
259 Next
260*/
261 return palette;
262}
263
6e98af87
RK
264function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
265 var itVal;
266 switch (algorithm) {
267 case 'oo':
268 itVal = mandelbrotValueOO(C, iterMax);
269 break;
270 case 'numeric':
271 default:
272 itVal = mandelbrotValueNumeric(C, iterMax);
273 break;
274 }
37b05b56
RK
275 context.fillStyle = getColor(itVal, iterMax);
276 context.fillRect (img_x, img_y, 1, 1); // x, y, width, height
277}
278
6e98af87
RK
279/***** pure UI functions *****/
280
37b05b56 281function saveImage() {
6e98af87 282 // XXX: should call filepicker!
37b05b56
RK
283 saveCanvas(document.getElementById("mbrotImage"), "/home/robert/temp/canvas-save.png")
284}
285
6e98af87
RK
286function updateIterMenu() {
287 try {
288 var currentIter = gPref.getIntPref("mandelbrot.iteration_max");
289 }
290 catch(e) {
291 var currentIter = 0;
292 }
293 if (currentIter < 10) {
294 currentIter = 500;
295 setIter(currentIter);
296 }
297
298 var popup = document.getElementById("menu_iterPopup");
299 var item = popup.firstChild;
300 while (item) {
301 if (item.getAttribute("name") == "iter") {
302 if (item.getAttribute("value") == currentIter)
303 item.setAttribute("checked","true");
304 else
305 item.removeAttribute("checked");
306 }
307 item = item.nextSibling;
308 }
309}
310
311function setIter(aIter) {
312 gPref.setIntPref("mandelbrot.iteration_max", aIter);
313}
314
315function updatePaletteMenu() {
316 try {
317 var currentPalette = gPref.getCharPref("mandelbrot.color_palette");
318 }
319 catch(e) {
320 var currentPalette = '';
321 }
322 if (!currentPalette.length) {
323 currentPalette = 'kairo';
324 setPalette(currentPalette);
325 }
326 if (!gColorPalette || !gColorPalette.length)
327 gColorPalette = getColorPalette(currentPalette);
328
329 var popup = document.getElementById("menu_palettePopup");
330 var item = popup.firstChild;
331 while (item) {
332 if (item.getAttribute("name") == "palette") {
333 if (item.getAttribute("value") == currentPalette)
334 item.setAttribute("checked", "true");
335 else
336 item.removeAttribute("checked");
337 }
338 item = item.nextSibling;
339 }
340}
341
342function setPalette(aPaletteID) {
343 gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
344 gColorPalette = getColorPalette(aPaletteID);
345}
346
347function updateDebugMenu() {
348 var jitMenuItem = document.getElementById("jitEnabled");
349 jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit.chrome"));
350}
351
352function 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");
356}
357
358function updateAlgoMenu() {
359 try {
360 var currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
361 }
362 catch(e) {
363 var currentAlgo = '';
364 }
365 if (!currentAlgo.length) {
366 currentAlgo = 'numeric';
367 setAlgorithm(currentAlgo);
368 }
369
370 var popup = document.getElementById("menu_algoPopup");
371 var item = popup.firstChild;
372 while (item) {
373 if (item.getAttribute("name") == "algorithm") {
374 if (item.getAttribute("value") == currentAlgo)
375 item.setAttribute("checked", "true");
376 else
377 item.removeAttribute("checked");
378 }
379 item = item.nextSibling;
380 }
381}
382
383function setAlgorithm(algoID) {
384 gPref.setCharPref("mandelbrot.use_algorithm", algoID);
385}
386
387
388/***** helper functions from external sources *****/
389
37b05b56
RK
390// function below is from from http://developer.mozilla.org/en/docs/Code_snippets:Canvas
391function 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);
396
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);
402
403 // prepare to save the canvas data
404 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
405 .createInstance(Components.interfaces.nsIWebBrowserPersist);
406
407 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
408 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
409
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;
415
416 // save the canvas data to the file
417 persist.saveURI(source, null, null, null, null, file);
418}
419
420// function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application
421function quitApp(aForceQuit) {
422 var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
423 .getService(Components.interfaces.nsIAppStartup);
424
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);
430}