start an 'image settings' prefwindow-based dialog for settings several options on...
[mandelbrot.git] / xulapp / chrome / mandelbrot / content / mandelbrot.js
... / ...
CommitLineData
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
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}
50
51function drawImage() {
52 let canvas = document.getElementById("mbrotImage");
53 let context = canvas.getContext("2d");
54
55 document.getElementById("statusLabel").value =
56 document.getElementById("mbrotBundle").getString("statusDrawing");
57
58 let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
59 let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
60
61 context.fillStyle = "rgb(255, 255, 255)";
62 context.fillRect(0, 0, canvas.width, canvas.height);
63
64 gStartTime = new Date();
65
66 drawLine(0, canvas, context, iterMax, algorithm);
67}
68
69function drawLine(line, canvas, context, iterMax, algorithm) {
70 let Cr_min = -2.0;
71 let Cr_max = 1.0;
72 try {
73 Cr_min = gPref.getCharPref("mandelbrot.last_image.Cr_min");
74 Cr_max = gPref.getCharPref("mandelbrot.last_image.Cr_max");
75 }
76 catch (e) { }
77 let Cr_scale = Cr_max - Cr_min;
78
79 let Ci_min = -1.5;
80 let Ci_max = 1.5;
81 try {
82 Cr_min = gPref.getCharPref("mandelbrot.last_image.Ci_min");
83 Cr_max = gPref.getCharPref("mandelbrot.last_image.Ci_max");
84 }
85 catch (e) { }
86 let Ci_scale = Ci_max - Ci_min;
87
88 let pixels = [];
89 for (var img_y = line; img_y < canvas.height && img_y < line+8; img_y++)
90 for (let img_x = 0; img_x < canvas.width; img_x++) {
91 let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
92 Ci_min + (img_y / canvas.height) * Ci_scale);
93 pixels.push.apply(pixels, drawPoint(context, img_x, img_y, C, iterMax, algorithm));
94 }
95 context.putImageData({width: canvas.width, height: pixels.length/4/canvas.width, data: pixels}, 0, line);
96
97 if (img_y < canvas.height)
98 setTimeout(drawLine, 0, img_y, canvas, context, iterMax, algorithm);
99 else
100 EndCalc();
101}
102
103function EndCalc() {
104 let endTime = new Date();
105 let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
106 document.getElementById("statusLabel").value =
107 document.getElementById("mbrotBundle").getFormattedString("statusTime", [timeUsed.toFixed(3)]);
108}
109
110function complex(aReal, aImag) {
111 this.r = aReal;
112 this.i = aImag;
113}
114complex.prototype = {
115 square: function() {
116 return new complex(this.r * this.r - this.i * this.i,
117 2 * this.r * this.i);
118 },
119 dist: function() {
120 return Math.sqrt(this.r * this.r + this.i * this.i);
121 },
122 add: function(aComplex) {
123 return new complex(this.r + aComplex.r, this.i + aComplex.i);
124 }
125}
126
127function mandelbrotValueOO (aC, aIterMax) {
128 // this would be nice code in general but it looks like JS objects are too heavy for normal use.
129 let Z = new complex(0.0, 0.0);
130 for (var iter = 0; iter < aIterMax; iter++) {
131 Z = Z.square().add(aC);
132 if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
133 }
134 return iter;
135}
136
137function mandelbrotValueNumeric (aC, aIterMax) {
138 // optimized numeric code for fast calculation
139 let Cr = aC.r, Ci = aC.i;
140 let Zr = 0.0, Zi = 0.0;
141 let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
142 for (var iter = 0; iter < aIterMax; iter++) {
143 Zi = 2 * Zr * Zi + Ci;
144 Zr = Zr2 - Zi2 + Cr;
145
146 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
147 if (Zr2 + Zi2 > 256) { break; }
148 }
149 return iter;
150}
151
152function getColor(aIterValue, aIterMax) {
153 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
154 if (gColorPalette && gColorPalette.length)
155 return gColorPalette[standardizedValue];
156
157 // fallback to simple b/w if for some reason we don't have a palette
158 if (aIterValue == aIterMax)
159 return [0, 0, 0, 255];
160 else
161 return [255, 255, 255, 255];
162}
163
164function getColorPalette(palName) {
165 var palette = [];
166 switch (palName) {
167 case 'bw':
168 for (let i = 0; i < 1024; i++) {
169 palette[i] = [255, 255, 255, 255];
170 }
171 palette[1024] = [0, 0, 0, 255];
172 break;
173 case 'kairo':
174 // outer areas
175 for (let i = 0; i < 32; i++) {
176 let cc1 = Math.floor(i * 127 / 31);
177 let cc2 = 170 - Math.floor(i * 43 / 31);
178 palette[i] = [cc1, cc2, cc1, 255];
179 }
180 // inner areas
181 for (let i = 0; i < 51; i++) {
182 let cc = Math.floor(i * 170 / 50);
183 palette[32 + i] = [cc, 0, (170-cc), 255];
184 }
185 // corona
186 for (let i = 0; i < 101; i++) {
187 let cc = Math.floor(i * 200 / 100);
188 palette[83 + i] = [255, cc, 0, 255];
189 }
190 // inner corona
191 for (let i = 0; i < 201; i++) {
192 let cc1 = 255 - Math.floor(i * 85 / 200);
193 let cc2 = 200 - Math.floor(i * 30 / 200);
194 let cc3 = Math.floor(i * 170 / 200);
195 palette[184 + i] = [cc1, cc2, cc3, 255];
196 }
197 for (let i = 0; i < 301; i++) {
198 let cc1 = 170 - Math.floor(i * 43 / 300);
199 let cc2 = 170 + Math.floor(i * 85 / 300);
200 palette[385 + i] = [cc1, cc1, cc2, 255];
201 }
202 for (let i = 0; i < 338; i++) {
203 let cc = 127 + Math.floor(i * 128 / 337);
204 palette[686 + i] = [cc, cc, 255, 255];
205 }
206 palette[1024] = [0, 0, 0, 255];
207 break;
208 case 'rainbow-linear1':
209 for (let i = 0; i < 256; i++) {
210 palette[i] = [i, 0, 0, 255];
211 palette[256 + i] = [255, i, 0, 255];
212 palette[512 + i] = [255 - i, 255, i, 255];
213 palette[768 + i] = [i, 255-i, 255, 255];
214 }
215 palette[1024] = [0, 0, 0, 255];
216 break;
217 }
218/*
219Select Case palnr
220Case 1 'Standard-Palette (QB-Colors)
221 For i = 0 To 1024
222 xx = CInt(i * 500 / 1024 + 2)
223 If xx <= 15 Then clr = xx
224 If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485))
225 If xx >= 500 Then clr = 0
226 palette(i) = QBColor(clr)
227 Next
228Case 3 'Regenbogen-Palette 1 (qu.)
229 For i = 0 To 33
230 clr = CInt(i * 255 / 33)
231 palette(i) = RGB(clr, 0, 0)
232 Next
233 For i = 0 To 136
234 clr = CInt(i * 255 / 136)
235 palette(34 + i) = RGB(255, clr, 0)
236 Next
237 For i = 0 To 306
238 clr = CInt(i * 255 / 306)
239 palette(171 + i) = RGB(255 - clr, 255, clr)
240 Next
241 For i = 0 To 545
242 clr = CInt(i * 255 / 545)
243 palette(478 + i) = RGB(clr, 255 - clr, 255)
244 Next
245Case 4 'Regenbogen-Palette 2 (linear)
246 For i = 0 To 204
247 clr = CInt(i * 255 / 204)
248 palette(i) = RGB(255, clr, 0)
249 palette(204 + i) = RGB(255 - clr, 255, 0)
250 palette(409 + i) = RGB(0, 255, clr)
251 palette(614 + i) = RGB(0, 255 - clr, 255)
252 palette(819 + i) = RGB(clr, 0, 255)
253 Next
254Case 5 'Regenbogen-Palette 2 (qu.)
255 For i = 0 To 18
256 clr = CInt(i * 255 / 18)
257 palette(i) = RGB(255, clr, 0)
258 Next
259 For i = 0 To 73
260 clr = CInt(i * 255 / 73)
261 palette(20 + i) = RGB(255 - clr, 255, 0)
262 Next
263 For i = 0 To 167
264 clr = CInt(i * 255 / 167)
265 palette(93 + i) = RGB(0, 255, clr)
266 Next
267 For i = 0 To 297
268 clr = CInt(i * 255 / 297)
269 palette(261 + i) = RGB(0, 255 - clr, 255)
270 Next
271 For i = 0 To 464
272 clr = CInt(i * 255 / 464)
273 palette(559 + i) = RGB(clr, 0, 255)
274 Next
275*/
276 return palette;
277}
278
279function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
280 var itVal;
281 switch (algorithm) {
282 case 'oo':
283 itVal = mandelbrotValueOO(C, iterMax);
284 break;
285 case 'numeric':
286 default:
287 itVal = mandelbrotValueNumeric(C, iterMax);
288 break;
289 }
290 return getColor(itVal, iterMax);
291}
292
293/***** pure UI functions *****/
294
295function saveImage() {
296 const bundle = document.getElementById("mbrotBundle");
297 const nsIFilePicker = Components.interfaces.nsIFilePicker;
298 var fp = null;
299 try {
300 fp = Components.classes["@mozilla.org/filepicker;1"]
301 .createInstance(nsIFilePicker);
302 } catch (e) {}
303 if (!fp) return;
304 var promptString = bundle.getString("savePrompt");
305 fp.init(window, promptString, nsIFilePicker.modeSave);
306 fp.appendFilter(bundle.getString("pngFilterName"), "*.png");
307 fp.defaultString = "mandelbrot.png";
308
309 var fpResult = fp.show();
310 if (fpResult != nsIFilePicker.returnCancel) {
311 saveCanvas(document.getElementById("mbrotImage"), fp.file);
312 }
313}
314
315function updateIterMenu() {
316 try {
317 var currentIter = gPref.getIntPref("mandelbrot.iteration_max");
318 }
319 catch(e) {
320 var currentIter = 0;
321 }
322 if (currentIter < 10) {
323 currentIter = 500;
324 setIter(currentIter);
325 }
326
327 var popup = document.getElementById("menu_iterPopup");
328 var item = popup.firstChild;
329 while (item) {
330 if (item.getAttribute("name") == "iter") {
331 if (item.getAttribute("value") == currentIter)
332 item.setAttribute("checked","true");
333 else
334 item.removeAttribute("checked");
335 }
336 item = item.nextSibling;
337 }
338}
339
340function setIter(aIter) {
341 gPref.setIntPref("mandelbrot.iteration_max", aIter);
342}
343
344function updatePaletteMenu() {
345 try {
346 var currentPalette = gPref.getCharPref("mandelbrot.color_palette");
347 }
348 catch(e) {
349 var currentPalette = '';
350 }
351 if (!currentPalette.length) {
352 currentPalette = 'kairo';
353 setPalette(currentPalette);
354 }
355 if (!gColorPalette || !gColorPalette.length)
356 gColorPalette = getColorPalette(currentPalette);
357
358 var popup = document.getElementById("menu_palettePopup");
359 var item = popup.firstChild;
360 while (item) {
361 if (item.getAttribute("name") == "palette") {
362 if (item.getAttribute("value") == currentPalette)
363 item.setAttribute("checked", "true");
364 else
365 item.removeAttribute("checked");
366 }
367 item = item.nextSibling;
368 }
369}
370
371function setPalette(aPaletteID) {
372 gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
373 gColorPalette = getColorPalette(aPaletteID);
374}
375
376function imgSettings() {
377 window.openDialog("chrome://mandelbrot/content/image-settings.xul");
378}
379
380function updateDebugMenu() {
381 var jitMenuItem = document.getElementById("jitEnabled");
382 jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit.chrome"));
383}
384
385function toggleJITState(jitMenuItem) {
386 var jitEnabled = !gPref.getBoolPref("javascript.options.jit.chrome");
387 gPref.setBoolPref("javascript.options.jit.chrome", jitEnabled)
388 jitMenuItem.setAttribute("checked", jitEnabled? "true" : "false");
389}
390
391function updateAlgoMenu() {
392 try {
393 var currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
394 }
395 catch(e) {
396 var currentAlgo = '';
397 }
398 if (!currentAlgo.length) {
399 currentAlgo = 'numeric';
400 setAlgorithm(currentAlgo);
401 }
402
403 var popup = document.getElementById("menu_algoPopup");
404 var item = popup.firstChild;
405 while (item) {
406 if (item.getAttribute("name") == "algorithm") {
407 if (item.getAttribute("value") == currentAlgo)
408 item.setAttribute("checked", "true");
409 else
410 item.removeAttribute("checked");
411 }
412 item = item.nextSibling;
413 }
414}
415
416function setAlgorithm(algoID) {
417 gPref.setCharPref("mandelbrot.use_algorithm", algoID);
418}
419
420/***** helper functions from external sources *****/
421
422// function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
423// custom modifications:
424// - use "a"-prefix on function arguments
425// - take an nsILocalFile as aDestFile argument
426// - always do silent download
427function saveCanvas(aCanvas, aDestFile) {
428 // create a data url from the canvas and then create URIs of the source and targets
429 var io = Components.classes["@mozilla.org/network/io-service;1"]
430 .getService(Components.interfaces.nsIIOService);
431 var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
432
433 // prepare to save the canvas data
434 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
435 .createInstance(Components.interfaces.nsIWebBrowserPersist);
436
437 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
438 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
439
440 // save the canvas data to the file
441 persist.saveURI(source, null, null, null, null, aDestFile);
442}
443
444// function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application
445function quitApp(aForceQuit) {
446 var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
447 .getService(Components.interfaces.nsIAppStartup);
448
449 // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit
450 // process if there is unsaved data. eForceQuit will quit no matter what.
451 var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit :
452 Components.interfaces.nsIAppStartup.eAttemptQuit;
453 appStartup.quit(quitSeverity);
454}