-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxaos.js
1410 lines (1344 loc) · 52.5 KB
/
xaos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* XaoS.js
* https://github.com/jblang/XaoS.js
*
* Copyright (C)2011 John B. Langston III
* Copyright (C)2001, 2010 Andrea Medeghini
* Copyright (C)1996, 1997 Jan Hubicka and Thomas Marsh
*
* Based on code from XaoS by Jan Hubicka (http://xaos.sf.net)
* and from JAME by Andrea Medeghini (http://www.fractalwalk.net)
*
* This file is part of XaoS.js.
*
* XaoS.js is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* XaoS.js is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XaoS.js. If not, see <http://www.gnu.org/licenses/>.
*
*/
var xaos = xaos || {};
xaos.zoom = (function() {
"use strict";
const USE_XAOS = true; // Whether to use zooming or recalculate every frame
const USE_SYMMETRY = true; // Whether to use symmetry when possible
const USE_SOLIDGUESS = true; // Whether to use solid guessing to avoid calculations
const RANGES = 2; // Number of ranges to use for sizing approximation data
const RANGE = 4; // Maximum distance to use for approximation
const MASK = 0x7; // Mask value for maximum potential source lines
const DSIZE = (RANGES + 1); // Shift value for target lines
const FPMUL = 64; // Multiplication factor for fixed-point representation
const FPRANGE = FPMUL * RANGE; // Fixed point range of approximation
const MAX_PRICE = Number.MAX_VALUE; // Maximum price of uninitialized approximation
const NEW_PRICE = FPRANGE * FPRANGE; // Price of calculating a new line
const GUESS_RANGE = 4; // Range to use for solid guessing
/** A price entry in the approximation table
* @constructor
*/
function Price() {
this.previous = null; // Previous price calculated for the same line
this.index = 0; // Index of the source for this approximation (-1 means new calculation)
this.price = MAX_PRICE; // Price calculated for this line
}
/** A group of pixels to be moved
* @constructor
*/
function Move() {
this.length = 0; // number of pixels to move
this.from = 0; // starting offset of pixel source
this.to = 0; // starting offset of pixel destination
}
/** A single row or column of pixels in the image
* @constructor
*/
function Line() {
this.recalculate = false; // whether to recalculate this line
this.dirty = false; // whether this line needs to be redrawn
this.isRow = false; // whether this is a row (true) or column (false)
this.index = 0; // index of row or column within the image
this.symIndex = 0; // index of pixels to use for symmetry
this.symTo = 0; // position of pixels this is symmetrical to
this.symRef = 0; // position of pixels referring to this one
this.oldPosition = 0.0; // line's old position in the fractal's complex plane
this.newPosition = 0.0; // line's new position in the fractal's complex plane
this.priority = 0.0; // calculation priority for this row/column
}
/** An image derived from an HTML5 canvas
* @param canvas - the canvas used to display the image
* @constructor
*/
function CanvasImage(canvas) {
let width = canvas.clientWidth;
let height = canvas.clientHeight;
if (canvas.width !== width || canvas.height !== height) {
canvas.width = width;
canvas.height = height;
} else {
ctx.clearRect(0, 0, width, height);
}
this.canvas = canvas;
this.context = canvas.getContext("2d");
this.width = canvas.width;
this.height = canvas.height;
this.newImageData = this.context.createImageData(this.width, this.height);
this.oldImageData = this.context.createImageData(this.width, this.height);
this.newBuffer = new Uint32Array(this.newImageData.data.buffer);
this.oldBuffer = new Uint32Array(this.oldImageData.data.buffer);
}
/** Swap new and old buffers */
CanvasImage.prototype.swapBuffers = function() {
var tmp = this.oldBuffer;
this.oldBuffer = this.newBuffer;
this.newBuffer = tmp;
tmp = this.oldImageData;
this.newImageData = this.oldImageData;
this.oldImageData = tmp;
};
/** Draw the current image */
CanvasImage.prototype.paint = function() {
this.context.putImageData(this.newImageData, 0, 0);
};
/** Utility function to make an array of the specified size
* with the specified initial value. It will do the right thing
* to create unique items, whether you pass in a prototype, a
* constructor, or a primitive.
* @param {number} size - the size of the array.
* @param initial - the initial value for each entry.
*/
function makeArray(size, initial) {
var i, data = [];
for (i = 0; i < size; i++) {
if (typeof initial === "object") {
// prototype object
data[i] = Object.create(initial);
} else if (typeof initial === "function") {
// constructor
data[i] = new initial();
} else {
// primitive
data[i] = initial;
}
}
return data;
}
/** Container for all zoom context data for a particular canvas.
*
* @param image {CanvasImage} Image on which to draw the fractal.
* @param fractal {FractalContext} Fractal parameters.
* @constructor
*/
function ZoomContext(image, fractal) {
var size = Math.max(image.width, image.height);
this.image = image; // the image to draw the fractal on
this.fractal = fractal; // the fractal formula used for the image
this.columns = makeArray(image.width, Line); // columns in the fractal image
this.rows = makeArray(image.height, Line); // rows in the fractal image
this.sourcePos = makeArray(size + 1, 0); // fixed-point positions for source lines
this.oldBest = makeArray(size, null); // best prices for previous line
this.newBest = makeArray(size, null); // best prices for current line
this.calcPrices = makeArray(size, Price); // prices for calculating new lines
this.movePrices = makeArray(size << DSIZE, Price); // prices for approximating new lines from exsiting ones
this.moveTable = makeArray(image.width + 1, Move); // table of pixels to be moved
this.fillTable = makeArray(image.width + 1, Move); // table of pixels to be filled
this.queue = makeArray(image.width + image.height, null); // queue of lines to calculate
this.queueLength = 0; // length of the calculation queue
this.startTime = 0; // time that the current frame was started
this.minFPS = 60; // target FPS to maintain
this.fudgeFactor = 0; // fudge factor used to achieve target FPS
this.incomplete = false; // flag indicates incomplete calculation
this.zooming = false; // flag indicates image is currently zooming
}
/** Swaps the old and new best prices in the this container. */
ZoomContext.prototype.swapBest = function() {
var tmpBest = this.oldBest;
this.oldBest = this.newBest;
this.newBest = tmpBest;
};
/** Convert fractal viewport from radius and center to x and y start to end ranges */
ZoomContext.prototype.convertArea = function() {
var radius = this.fractal.region.radius;
var center = this.fractal.region.center;
var aspect = this.image.width / this.image.height;
var size = Math.max(radius.x, radius.y * aspect);
return {
begin: {
x: center.x - size / 2,
y: (center.y - size / 2) / aspect
},
end: {
x: center.x + size / 2,
y: (center.y + size / 2) / aspect
}
};
};
/** Resets line of pixels for fresh calculation
*
* @param line - row or column of pixels
* @param begin - starting fractal cooridnate
* @param end - ending coordinate
* @param isRow - whether this is a row or column
* @returns {number}
*/
ZoomContext.prototype.initialize = function(lines, begin, end, isRow) {
var i;
var p;
var step = (end - begin) / lines.length;
var line = null;
for (i = 0, p = begin; i < lines.length; i++, p += step) {
line = lines[i]
line.recalculate = true;
line.dirty = true;
line.isRow = isRow;
line.index = i;
line.oldPosition = p;
line.newPosition = p;
line.symIndex = i;
line.symTo = -1;
line.symRef = -1;
}
return step;
}
/** Calculate price of approximating one line from another
*
* @param p1 - position of first line
* @param p2 - position of second line
* @returns {number} - price of approximation
*/
function calcPrice(p1, p2) {
return (p1 - p2) * (p1 - p2);
}
/** Calculate fixed-point representation of each line's old position
* @param lines - lines to use for calculation
* @param begin - beginning of floating point range
* @param end - end of floating point range
*/
ZoomContext.prototype.calcFixedpoint = function(lines, begin, end) {
var tofix = (lines.length * FPMUL) / (end - begin);
var i;
this.sourcePos[lines.length] = Number.MAX_VALUE;
for (i = lines.length - 1; i >= 0; i--) {
this.sourcePos[i] = ((lines[i].oldPosition - begin) * tofix) | 0;
if (this.sourcePos[i] > this.sourcePos[i + 1]) {
this.sourcePos[i] = this.sourcePos[i + 1];
}
}
}
/** Choose the best approximation for lines based on previous frame
*
* @param lines - relocation table for rows or columns
* @param begin - beginning coordinate (x or y)
* @param end - ending coordinate (x or y)
* @param newPosition - array of newPosition coordinates on the complex plane
* @returns {number}
*/
ZoomContext.prototype.approximate = function(lines, begin, end) {
var previous = null; // pointer to previous approximation
var best = null; // pointer to best approximation
var line = null; // pointer to current line
var price = 0; // price of current approximation
var dest; // index of the current destination line
var idealPos = 0; // ideal position for the current destination
var maxPos = 0; // maximum valid source position of the current destination
var source = 0; // index of current source line
var prevBegin = 0; // index of first potential source for current destination
var prevEnd = 0; // index of last potential source for current destination
var currBegin = 0; // index of first potential source for next destination
var flag = 0;
var size = lines.length;
var step = (end - begin) / size;
var sourcePos = this.sourcePos;
// Calculate fixed-point positions of all source lines
this.calcFixedpoint(lines, begin, end);
for (dest = 0, idealPos = 0; dest < size; dest++, idealPos += FPMUL) {
this.swapBest();
maxPos = idealPos - FPRANGE;
if (maxPos < -FPMUL) {
maxPos = -FPMUL;
}
source = prevBegin;
while (sourcePos[source] < maxPos) {
source++;
}
currBegin = source;
maxPos = idealPos + FPRANGE;
// Find the previous approximation
if ((prevBegin !== prevEnd) && (source > prevBegin)) {
// Previous line had approximations; use them
if (source < prevEnd) {
previous = this.oldBest[source - 1];
} else {
previous = this.oldBest[prevEnd - 1];
}
price = previous.price;
} else if (dest > 0) {
// Previous line had no approximations
// Use the price of calculating the previous line
previous = this.calcPrices[dest - 1];
price = previous.price;
} else {
// We're on the first line; no previous prices exists
previous = null;
price = 0;
}
// Add the price for calculating this line
price += NEW_PRICE;
best = this.calcPrices[dest];
best.price = price;
best.index = -1;
best.previous = previous;
// Try all possible approximations for this line and calculate the best one
if (prevBegin !== prevEnd) {
if (source === prevBegin) {
// We're on the first line so there is no previous line
if (sourcePos[source] !== sourcePos[source + 1]) {
previous = this.calcPrices[dest - 1];
price = previous.price + calcPrice(sourcePos[source], idealPos);
if (price < best.price) {
best = this.movePrices[(source << DSIZE) + (dest & MASK)];
best.price = price;
best.index = source;
best.previous = previous;
}
}
this.newBest[source++] = best;
}
previous = null;
// Potential sources for the previous and current line overlap within
// this range, so we have to calculate every possibility and find the best
while (source < prevEnd) {
if (sourcePos[source] !== sourcePos[source + 1]) {
previous = this.oldBest[source - 1];
price = previous.price + NEW_PRICE;
if (price < best.price) {
best = this.movePrices[((source - 1) << DSIZE) + (dest & MASK)];
best.price = price;
best.index = -1;
best.previous = previous;
this.newBest[source - 1] = best;
}
price = previous.price + calcPrice(sourcePos[source], idealPos);
if (price < best.price) {
best = this.movePrices[(source << DSIZE) + (dest & MASK)];
best.price = price;
best.index = source;
best.previous = previous;
} else if (sourcePos[source] > idealPos) {
this.newBest[source++] = best;
break;
}
}
this.newBest[source++] = best;
}
// We are past the overlapping area
if (source > prevBegin) {
previous = this.oldBest[source - 1];
} else {
previous = this.calcPrices[dest - 1];
}
price = previous.price + NEW_PRICE;
if ((price < best.price) && (source > currBegin)) {
best = this.movePrices[((source - 1) << DSIZE) + (dest & MASK)];
best.price = price;
best.index = -1;
best.previous = previous;
this.newBest[source - 1] = best;
}
while (sourcePos[source] < maxPos) {
if (sourcePos[source] !== sourcePos[source + 1]) {
price = previous.price + calcPrice(sourcePos[source], idealPos);
if (price < best.price) {
best = this.movePrices[(source << DSIZE) + (dest & MASK)];
best.price = price;
best.index = source;
best.previous = previous;
} else if (sourcePos[source] > idealPos) {
break;
}
}
this.newBest[source++] = best;
}
while (sourcePos[source] < maxPos) {
this.newBest[source++] = best;
}
} else if (sourcePos[source] < maxPos) {
if (dest > 0) {
previous = this.calcPrices[dest - 1];
price = previous.price;
} else {
previous = null;
price = 0;
}
while (sourcePos[source] < maxPos) {
if (sourcePos[source] !== sourcePos[source + 1]) {
price += calcPrice(sourcePos[source], idealPos);
if (price < best.price) {
best = this.movePrices[(source << DSIZE) + (dest & MASK)];
best.price = price;
best.index = source;
best.previous = previous;
} else if (sourcePos[source] > idealPos) {
break;
}
}
this.newBest[source++] = best;
}
while (sourcePos[source] < maxPos) {
this.newBest[source++] = best;
}
}
prevBegin = currBegin;
currBegin = prevEnd;
prevEnd = source;
}
if ((begin > lines[0].oldPosition) && (end < lines[size - 1].oldPosition)) {
flag = 1;
}
if ((sourcePos[0] > 0) && (sourcePos[size - 1] < (size * FPMUL))) {
flag = 2;
}
for (dest = size - 1; dest >= 0; dest--) {
line = lines[dest]
line.symTo = -1;
line.symRef = -1;
if (best.index < 0) {
line.recalculate = true;
line.dirty = true;
line.symIndex = line.index;
} else {
line.symIndex = best.index;
line.newPosition = lines[best.index].oldPosition;
line.recalculate = false;
line.dirty = false;
}
best = best.previous;
}
newPositions(lines, begin, end, step, flag);
return step;
}
/** Choose new positions for lines based on calculated prices
*
* @param lines
* @param size
* @param begin1
* @param end1
* @param step
* @param newPosition
* @param flag
*/
function newPositions(lines, begin1, end1, step, flag) {
var delta = 0;
var size = lines.length;
var begin = 0;
var end = 0;
var s = -1;
var e = -1;
if (begin1 > end1) {
begin1 = end1;
}
while (s < (size - 1)) {
e = s + 1;
if (lines[e].recalculate) {
while (e < size) {
if (!lines[e].recalculate) {
break;
}
e++;
}
if (e < size) {
end = lines[e].newPosition;
} else {
end = end1;
}
if (s < 0) {
begin = begin1;
} else {
begin = lines[s].newPosition;
}
if ((e === size) && (begin > end)) {
end = begin;
}
if ((e - s) === 2) {
delta = (end - begin) * 0.5;
} else {
delta = (end - begin) / (e - s);
}
switch (flag) {
case 1:
for (s++; s < e; s++) {
begin += delta;
lines[s].newPosition = begin;
lines[s].priority = 1 / (1 + (Math.abs((lines[s].oldPosition - begin)) * step));
}
break;
case 2:
for (s++; s < e; s++) {
begin += delta;
lines[s].newPosition = begin;
lines[s].priority = Math.abs((lines[s].oldPosition - begin)) * step;
}
break;
default:
for (s++; s < e; s++) {
begin += delta;
lines[s].newPosition = begin;
lines[s].priority = 1.0;
}
break;
}
}
s = e;
}
}
/** Populate symmetry data into relocation table
*
* @param lines
* @param symi
* @param symPosition
* @param step
*/
function prepareSymmetry(lines, symi, symPosition, step) {
var i;
var j = 0;
var tmp;
var abs;
var distance;
var newPosition;
var size = lines.length;
var max = size - RANGE - 1;
var min = RANGE;
var istart = 0;
var line = null;
var otherLine = null;
var symj = (2 * symi) - size;
symPosition *= 2;
if (symj < 0) {
symj = 0;
}
distance = step * RANGE;
for (i = symj; i < symi; i++) {
line = lines[i];
if (line.symTo !== -1) {
continue;
}
newPosition = line.newPosition;
line.symTo = (2 * symi) - i;
if (line.symTo > max) {
line.symTo = max;
}
j = ((line.symTo - istart) > RANGE) ? (-RANGE) : (-line.symTo + istart);
if (line.recalculate) {
while ((j < RANGE) && ((line.symTo + j) < (size - 1))) {
tmp = symPosition - lines[line.symTo + j].newPosition;
abs = Math.abs(tmp - newPosition);
if (abs < distance) {
if (((i === 0) || (tmp > lines[i - 1].newPosition)) && (tmp < lines[i + 1].newPosition)) {
distance = abs;
min = j;
}
} else if (tmp < newPosition) {
break;
}
j++;
}
} else {
while ((j < RANGE) && ((line.symTo + j) < (size - 1))) {
if (line.recalculate) {
tmp = symPosition - lines[line.symTo + j].newPosition;
abs = Math.abs(tmp - newPosition);
if (abs < distance) {
if (((i === 0) || (tmp > lines[i - 1].newPosition)) && (tmp < lines[i + 1].newPosition)) {
distance = abs;
min = j;
}
} else if (tmp < newPosition) {
break;
}
}
j++;
}
}
line.symTo += min;
otherLine = lines[line.symTo];
if ((min === RANGE) || (line.symTo <= symi) || (otherLine.symTo !== -1) || (otherLine.symRef !== -1)) {
line.symTo = -1;
continue;
}
if (!line.recalculate) {
line.symTo = -1;
if ((otherLine.symTo !== -1) || !otherLine.recalculate) {
continue;
}
otherLine.symIndex = line.symIndex;
otherLine.symTo = i;
istart = line.symTo - 1;
otherLine.recalculate = false;
otherLine.dirty = true;
line.symRef = line.symTo;
otherLine.newPosition = symPosition - line.newPosition;
} else {
if (otherLine.symTo !== -1) {
line.symTo = -1;
continue;
}
line.symIndex = otherLine.symIndex;
istart = line.symTo - 1;
line.recalculate = false;
line.dirty = true;
otherLine.symRef = i;
line.newPosition = symPosition - otherLine.newPosition;
}
}
}
/** Optimized array copy using Duff's Device.
*
* @param from {Array} source array
* @param fromOffset {number} offset into source array
* @param to {Array} idealPos array
* @param toOffset {number} offset into idealPos array
* @param length {number} elements to copy
*/
function arrayCopy(from, fromOffset, to, toOffset, length) {
var n = length % 8;
while (n--) {
to[toOffset++] = from[fromOffset++];
}
n = (length / 8) | 0;
while (n--) {
to[toOffset++] = from[fromOffset++];
to[toOffset++] = from[fromOffset++];
to[toOffset++] = from[fromOffset++];
to[toOffset++] = from[fromOffset++];
to[toOffset++] = from[fromOffset++];
to[toOffset++] = from[fromOffset++];
to[toOffset++] = from[fromOffset++];
to[toOffset++] = from[fromOffset++];
}
}
/** Apply previously calculated symmetry to image */
ZoomContext.prototype.doSymmetry = function() {
var from_offset = 0;
var to_offset = 0;
var i;
var j = 0;
var buffer = this.image.newBuffer;
var bufferWidth = this.image.width;
for (i = 0; i < this.rows.length; i++) {
if ((this.rows[i].symTo >= 0) && (!this.rows[this.rows[i].symTo].dirty)) {
from_offset = this.rows[i].symTo * bufferWidth;
arrayCopy(buffer, from_offset, buffer, to_offset, bufferWidth);
this.rows[i].dirty = false;
}
to_offset += bufferWidth;
}
for (i = 0; i < this.columns.length; i++) {
if ((this.columns[i].symTo >= 0) && (!this.columns[this.columns[i].symTo].dirty)) {
to_offset = i;
from_offset = this.columns[i].symTo;
for (j = 0; j < this.rows.length; j++) {
buffer[to_offset] = buffer[from_offset];
to_offset += bufferWidth;
from_offset += bufferWidth;
}
this.columns[i].dirty = false;
}
}
}
/** Build an optimized move table based on relocation table */
ZoomContext.prototype.prepareMove = function() {
var move = null;
var i = 0;
var j = 0;
var s = 0;
while (i < this.columns.length) {
if (!this.columns[i].dirty) {
move = this.moveTable[s];
move.to = i;
move.length = 1;
move.from = this.columns[i].symIndex;
for (j = i + 1; j < this.columns.length; j++) {
if (this.columns[j].dirty || ((j - this.columns[j].symIndex) !== (move.to - move.from))) {
break;
}
move.length++;
}
i = j;
s++;
} else {
i++;
}
}
move = this.moveTable[s];
move.length = 0;
}
/** Execute moves defined in move table */
ZoomContext.prototype.doMove = function() {
var move = null;
var newOffset = 0;
var oldOffset = 0;
var from = 0;
var to = 0;
var i;
var s = 0;
var length = 0;
var newBuffer = this.image.newBuffer;
var oldBuffer = this.image.oldBuffer;
var bufferWidth = this.image.width;
for (i = 0; i < this.rows.length; i++) {
if (!this.rows[i].dirty) {
s = 0;
oldOffset = this.rows[i].symIndex * bufferWidth;
while ((move = this.moveTable[s]).length > 0) {
from = oldOffset + move.from;
to = newOffset + move.to;
length = move.length;
arrayCopy(oldBuffer, from, newBuffer, to, length);
s++;
}
}
newOffset += bufferWidth;
}
}
/** Shortcut for prepare and execute move */
ZoomContext.prototype.movePixels = function() {
this.prepareMove();
this.doMove();
}
/** Prepare fill table based on relocation table */
ZoomContext.prototype.prepareFill = function() {
var fill = null;
var i;
var j = 0;
var k = 0;
var s = 0;
var n = 0;
for (i = 0; i < this.columns.length; i++) {
if (this.columns[i].dirty) {
j = i - 1;
for (k = i + 1; (k < this.columns.length) && this.columns[k].dirty; k++) {}
while ((i < this.columns.length) && this.columns[i].dirty) {
if ((k < this.columns.length) && ((j < i) || ((this.columns[i].newPosition - this.columns[j].newPosition) > (this.columns[k].newPosition - this.columns[i].newPosition)))) {
j = k;
} else if (j < 0) {
break;
}
n = k - i;
fill = this.fillTable[s];
fill.length = n;
fill.from = j;
fill.to = i;
while (n > 0) {
this.columns[i].newPosition = this.columns[j].newPosition;
this.columns[i].dirty = false;
n--;
i++;
}
s++;
}
}
}
fill = this.fillTable[s];
fill.length = 0;
}
/** Apply fill table */
ZoomContext.prototype.doFill = function() {
var fill = null;
var from_offset = 0;
var to_offset = 0;
var from = 0;
var to = 0;
var i;
var j = 0;
var k = 0;
var t = 0;
var s = 0;
var d = 0;
var buffer = this.image.newBuffer;
var bufferWidth = this.image.width;
for (i = 0; i < this.rows.length; i++) {
if (this.rows[i].dirty) {
j = i - 1;
for (k = i + 1; (k < this.rows.length) && this.rows[k].dirty; k++) {}
while ((i < this.rows.length) && this.rows[i].dirty) {
if ((k < this.rows.length) && ((j < i) || ((this.rows[i].newPosition - this.rows[j].newPosition) > (this.rows[k].newPosition - this.rows[i].newPosition)))) {
j = k;
} else if (j < 0) {
break;
}
to_offset = i * bufferWidth;
from_offset = j * bufferWidth;
if (!this.rows[j].dirty) {
s = 0;
while ((fill = this.fillTable[s]).length > 0) {
from = from_offset + fill.from;
to = from_offset + fill.to;
for (t = 0; t < fill.length; t++) {
d = to + t;
buffer[d] = buffer[from];
}
s++;
}
}
arrayCopy(buffer, from_offset, buffer, to_offset, bufferWidth);
this.rows[i].newPosition = this.rows[j].newPosition;
this.rows[i].dirty = true;
i++;
}
} else {
s = 0;
from_offset = i * bufferWidth;
while ((fill = this.fillTable[s]).length > 0) {
from = from_offset + fill.from;
to = from_offset + fill.to;
for (t = 0; t < fill.length; t++) {
d = to + t;
buffer[d] = buffer[from];
}
s++;
}
this.rows[i].dirty = true;
}
}
}
/** Shortcut to prepare and apply fill table */
ZoomContext.prototype.fill = function() {
this.prepareFill();
this.doFill();
}
/** Render line using solid guessing
*
* @param row
*/
ZoomContext.prototype.renderRow = function(row) {
var buffer = this.image.newBuffer;
var bufferWidth = this.image.width;
var newPosition = row.newPosition;
var r = row.index;
var offset = r * bufferWidth;
var i;
var j;
var k;
var n;
var distl;
var distr;
var distu;
var distd;
var offsetu;
var offsetd;
var offsetl;
var offsetul;
var offsetur;
var offsetdl;
var offsetdr;
var rend = r - GUESS_RANGE;
var length;
var current;
if (rend < 0) {
rend = 0;
}
for (i = r - 1; (i >= rend) && this.rows[i].dirty; i--) {}
distu = r - i;
rend = r + GUESS_RANGE;
if (rend >= this.rows.length) {
rend = this.rows.length - 1;
}
for (j = r + 1; (j < rend) && this.rows[j].dirty; j++) {}
distd = j - r;
if (!USE_SOLIDGUESS || (i < 0) || (j >= this.rows.length) || this.rows[i].dirty || this.rows[j].dirty) {
for (k = 0, length = this.columns.length; k < length; k++) {
current = this.columns[k];
if (!this.columns[k].dirty) {
buffer[offset] = this.fractal.formula(current.newPosition, newPosition);
}
offset++;
}
} else {
distr = 0;
distl = Number.MAX_VALUE / 2;
offsetu = offset - (distu * bufferWidth);
offsetd = offset + (distd * bufferWidth);
for (k = 0, length = this.columns.length; k < length; k++) {
current = this.columns[k];
if (!this.columns[k].dirty) {
if (distr <= 0) {
rend = k + GUESS_RANGE;
if (rend >= this.columns.length) {
rend = this.columns.length - 1;
}
for (j = k + 1; (j < rend) && this.columns[j].dirty; j++) {
distr = j - k;
}
if (j >= rend) {
distr = Number.MAX_VALUE / 2;
}
}
if ((distr < (Number.MAX_VALUE / 4)) && (distl < (Number.MAX_VALUE / 4))) {
offsetl = offset - distl;
offsetul = offsetu - distl;
offsetdl = offsetd - distl;
offsetur = offsetu + distr;
offsetdr = offsetd + distr;
n = buffer[offsetl];
if ((n == buffer[offsetu]) && (n == buffer[offsetd]) && (n == buffer[offsetul]) && (n == buffer[offsetur]) && (n == buffer[offsetdl]) && (n == buffer[offsetdr])) {
buffer[offset] = n;
} else {
buffer[offset] = this.fractal.formula(current.newPosition, newPosition);
}
} else {
buffer[offset] = this.fractal.formula(current.newPosition, newPosition);
}
distl = 0;
}
offset++;
offsetu++;
offsetd++;
distr--;
distl++;
}
}
row.recalculate = false;
row.dirty = false;
}
/** Render column using solid guessing
*
* @param column
*/
ZoomContext.prototype.renderColumn = function(column) {
var buffer = this.image.newBuffer;
var bufferWidth = this.image.width;
var newPosition = column.newPosition;
var r = column.index;
var offset = r;
var rend = r - GUESS_RANGE;
var i;
var j;
var k;
var n;
var distl;
var distr;
var distu;
var distd;
var offsetl;
var offsetr;
var offsetu;
var offsetlu;
var offsetru;
var offsetld;
var offsetrd;
var sumu;
var sumd;
var length;
var current;
if (rend < 0) {
rend = 0;
}
for (i = r - 1; (i >= rend) && this.columns[i].dirty; i--) {}
distl = r - i;
rend = r + GUESS_RANGE;
if (rend >= this.columns.length) {
rend = this.columns.length - 1;
}
for (j = r + 1; (j < rend) && this.columns[j].dirty; j++) {}
distr = j - r;
if (!USE_SOLIDGUESS || (i < 0) || (j >= this.columns.length) || this.columns[i].dirty || this.columns[j].dirty) {
for (k = 0, length = this.rows.length; k < length; k++) {
current = this.rows[k];
if (!this.rows[k].dirty) {
buffer[offset] = this.fractal.formula(newPosition, current.newPosition);
}
offset += bufferWidth;
}
} else {
distd = 0;
distu = Number.MAX_VALUE / 2;
offsetl = offset - distl;
offsetr = offset + distr;