summaryrefslogtreecommitdiff
path: root/gif.js
blob: a2c22faaf5f2a2e641656ab4b89584a65560b892 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
// gif.js by @timb :)
;(function(e,t,n,r){function i(r){if(!n[r]){if(!t[r]){if(e)return e(r);throw new Error("Cannot find module '"+r+"'")}var s=n[r]={exports:{}};t[r][0](function(e){var n=t[r][1][e];return i(n?n:e)},s,s.exports)}return n[r].exports}for(var s=0;s<r.length;s++)i(r[s]);return i})(typeof require!=="undefined"&&require,{1:[function(require,module,exports){var Benchmark = require('../../benchmark');
var Tube = require('../../tube');
var BufferLoader = require('../../bufferloader');

var decode = require('./decode');
var render = require('./render-canvas');

var GIF = function(src, opts){

  var gif = Object.create(GIF.proto);

  gif.buf = {}; // buffers used to store binary data
  gif.tube = Tube();
  gif.benchmark = Benchmark();

  if (src instanceof File){
    gif.src = src;
  }
  else if (typeof src === 'string'){ // url
    gif.src = src;
  }
  
  return gif;

};


/*
states:
empty
buffering: fetching a resource (File, url) into an ArrayBuffer
decoding: decode binary data into different data chunks
rendering: render data into frames as canvas contexts, or into a texture for webgl, etc
*/

GIF.proto = {};

GIF.setupBuffers = function(gif, abuf){
  gif.buf.cursor = 0;
  gif.buf.abuf = abuf;
  gif.buf.u8 = new Uint8Array(abuf);
  gif.buf.dv = new DataView(abuf);
};

GIF.proto.on = function(){ var gif = this;
  gif.tube.on.apply(gif.tube, arguments)
};
GIF.proto.off = function(){ var gif = this;
  gif.tube.off.apply(gif.tube, arguments)
};

GIF.proto.load = function(){ var gif = this;
  // if (!gif.src) throw new Error('gif needs a src');

  gif.loader = BufferLoader(gif.src, {benchmark: gif.benchmark});
  gif.loader.on("load", function(abuf){ 
    GIF.setupBuffers(gif, abuf)
    gif.loaded = true;
    gif.tube("loaded")
  });
  gif.loader.on("error", function(e, xhr){ gif.tube("error", e, xhr)} );
  gif.loader.load();

};

GIF.proto.decode = function(){ var gif = this;

  if (!gif.loaded) {
    gif.on("loaded", gif.decode.bind(gif)) // ugh
    gif.load();
    return gif
  }

  // TODO: deal with
  gif.extensions = [];
  gif.frames = [];
  //gif.cursor = 0;

  decode(gif);
  
};

GIF.proto.render = function(){ var gif = this;
  if (!gif.decoded){
    gif.on("decoded", gif.render.bind(gif)) // ugh
    gif.decode();
    return gif
  }

  render(gif);

};


if (typeof modules !== "undefined") modules.exports = GIF;
if (typeof window !== "undefined") window.GIF = GIF;
},{"../../benchmark":2,"../../tube":3,"../../bufferloader":4,"./decode":5,"./render-canvas":6}],2:[function(require,module,exports){// var b = Benchmark();
// b.start("something") // start a timer named "something"
// b.stop("something") // stop it
// b.something <-- time it took between start and stop calls in ms
// b.total <--- total ms of all benchmarks

var Benchmark = function(){
  var b = Object.create(Benchmark.proto);
  b.total = 0;
  return b;
};
Benchmark.proto = {};
Benchmark.proto.start = function(name){
  this[name+"_start_timestamp"] = Date.now();
};
Benchmark.proto.stop = function(name){
  if (!this[name+"_start_timestamp"]) return;
  var val = Date.now() - this[name+"_start_timestamp"];
  delete this[name+"_start_timestamp"];
  this[name] = val + (this[name] || 0);
  this.total += val;
};

module.exports = Benchmark;

// export Benchmark;
},{}],3:[function(require,module,exports){var setproto = require('./object/setproto');
var tokenize = require('./string/tokenize');
var globber = require('./string/globber');
var Uid = require('./uid');
var nextTick = require('./nexttick');

  // import queueOrNextTick from 'lib/nexttick4';

var globcache = {};

var Tube = function(opts){

  opts = opts || {};

  if (opts.queue){
    var c = function(){
      var args = arguments;
      // queueOrNextTick (function(){ c.send.apply(c, args) });
      nextTick (function(){ c.send.apply(c, args) });
      return c;
    };
  } else {
    var c = function(){
      c.send.apply(c, arguments);
      return c;
    };
  }

  setproto(c, Tube.proto);
  
  c.listeners = {};
  c.globListeners = {};

  return c;
};

Tube.total = {};

Tube.proto = {};


/*
adds fns as listeners to a channel

on("msg", fn, {opts})
on("msg", [fn, fn2], {opts})
on("msg msg2 msg3", fn, {opts})
on({"msg": fn, "msg2": fn2}, {opts})
*/
Tube.proto.on = function(){ var chan = this;

  if (typeof arguments[0] === "string") { 
  //if (arguments.length > 1) {           // on("msg", f)
    var msgMap = {};
    msgMap[arguments[0]] = arguments[1];
    var opts = arguments[2] || {};
  } else {                              // on({"msg": f, ...})
    var msgMap = arguments[0];
    var opts = arguments[1] || {};
  }

  for (var string in msgMap){
    
    var msgs = string.split(" ");

    var fs = msgMap[string];
    if (!Array.isArray(fs)) fs = [fs];

    for(var i=0, f; f=fs[i]; i++){
      if (!f.uid) f.uid = Uid();
    }

    for(var i=0, msg; msg=msgs[i]; i++){

      var listeners = (msg.indexOf("*") === -1) ?
                        chan.listeners :
                        chan.globListeners;

      // todo: this probably wastes a lot of memory?
      // make a copy of the listener, add to it, and replace the listener
      // why not just push directly?
      // send might be iterating over it... and that will fuck up the iteration
      listeners[msg] = (msg in listeners) ?
                         listeners[msg].concat(fs) :
                         fs.concat();
    }
  }

  return chan;

};



/*
off()
off("a:b:c")
off(f)
off("a:b:c", f)
off("a:b:c d:e:f")
off([f, f2])
off({"a": f, "b": f2})
*/
Tube.proto.off = function(){ var chan = this;

  var listeners, i, msgs, msg;

  // off() : delete all listeners. but replace, instead of delete
  if (arguments.length === 0) { 
    chan.listeners = {};
    chan.globListeners = {};
    return chan;
  }

  // off("a:b:c d:e:f")
  // remove all matching listeners
  if (arguments.length === 1 && typeof arguments[0] === "string"){
    // question... will this fuck up send if we delete in the middle of it dispatching?
    msgs = arguments[0].split(" ");

    for (i=0; msg=msgs[i]; i++){
      delete chan.listeners[msg];
      delete chan.globListeners[msg];
    }
    return chan;
  }

  // off(f) or off([f, f2])
  // remove all matching functions
  if (typeof arguments[0] === "function" || Array.isArray(arguments[0])) {
    var fs = (typeof arguments[0] === "function") ? 
               [arguments[0]] :
               arguments[0];
    // TODO
    return chan;
  }

  // off("a:b:c", f) or off({"a": f, "b": f2})
  if (arguments.length > 1) {           // off("msg", f)
    var msgMap = {};
    msgMap[arguments[0]] = arguments[1];
  } else {                              // off({"msg": f, ...})
    var msgMap = arguments[0];
  }
  
  for (var string in msgMap){
    msgs = string.split(" ");

    var fs = msgMap[string];
    if (typeof fs === "function") fs = [fs];

    for(var i=0; msg=msgs[i]; i++){

      if (msg in chan.listeners)
        listeners = chan.listeners;
      else if (msg in chan.globListeners)
        listeners = chan.globListeners;
      else
        continue;

      // gotta do this carefully in case we are still iterating through the listener in send
      // build a new array and assign it to the property, instead of mutating it.

      // console.log(" length of listeners[" + msg + "]: " + listeners[msg].length)
      // console.log(listeners[msg].join(","));
      // console.log(fs.join(","));

      listeners[msg] = listeners[msg].filter(
                         function(f){ return fs.indexOf(f) === -1 }
                       );
      // console.log(" length of listeners[" + msg + "]: " + listeners[msg].length)

    }
  }

  return chan;

};


/*

c = Tube()
c.on("foo", fn)
c("foo", "bar", [])

will call fn("bar", [], "foo")

*/
Tube.proto.send = function(msgString /*, data... */){

  // todo: don't do this?
  if (!Tube.total[msgString]) Tube.total[msgString] = 0
  Tube.total[msgString]+=1;

  var listener,
      listeners = this.listeners,
      globListeners = this.globListeners,
      //args = Array.prototype.splice.call(arguments, 1),
      msgs = tokenize(msgString),
      msg, f;

  if (arguments.length) {
    var args = Array.prototype.splice.call(arguments, 1);
    args.push(msgString);

  } else {
    var args = [];
  }

  for (var m=0; msg=msgs[m]; m++){

    var fsToRun = [];
    var uidKeyFnValue = {};
    var uidKeyMsgStringValue = {};

    // note this will die on errors
    // todo: implement http://dean.edwards.name/weblog/2009/03/callbacks-vs-events/
    // exact matches
    if (listener = listeners[msg]) {
      for (var i=0; f=listener[i]; i++){
          // fsToRun.push([f, msg]);
        uidKeyFnValue[f.uid] = f;
        uidKeyMsgStringValue[f.uid] = msg;
      }
    }

    // glob matches
    var msgSplit = msg.split(":");

    for (var pattern in globListeners){

      if (pattern !== "*") { // * always matches
        var patternSplit = globcache[pattern] || (globcache[pattern] = pattern.split(":"));
        if (!globber(patternSplit, msgSplit)) continue;
      }

      listener = globListeners[pattern];

      for (var i=0; f=listener[i]; i++){
        //f.apply(window, args); // hm possibly pass the actual message to the func
        // fsToRun.push([f, msg]);
        uidKeyFnValue[f.uid] = f;
        uidKeyMsgStringValue[f.uid] = msg;
      }
    }

    var fns = [];
    for (var f in uidKeyFnValue) fns.push(uidKeyFnValue[f]);

    for (var i=0, f; f=fns[i]; i++)
      f.apply(f, args);

  }

  return this;

};

module.exports = Tube;
},{"./object/setproto":7,"./string/tokenize":8,"./string/globber":9,"./uid":10,"./nexttick":11}],7:[function(require,module,exports){// TODO: replace all uses of
// setproto(foo, proto)
// with
// foo.__proto__ = proto
// when IE is no longer shit

var setproto = function(obj, proto){
  if (obj.__proto__)
    obj.__proto__ = proto;
  else
    for (var key in proto)
      obj[key] = proto[key];
};
module.exports = setproto;
},{}],8:[function(require,module,exports){// trimmed string into array of strings
var tokenize = function(str, splitOn){
  return str
           .trim()
           .split(splitOn || tokenize.default);
};
tokenize.default = /\s+/g;

module.exports = tokenize;
},{}],9:[function(require,module,exports){// can use * to match 0 or more : separated msgs
// globber("*".split(":"), "a:b:c".split(":")) => true
// globber("*:c".split(":"), "a:b:c".split(":")) => true
// globber("a:*".split(":"), "a:b:c".split(":")) => true
// globber("a:*:c".split(":"), "a:b:c".split(":")) => true

// based on codegolf.stackexchange.com/questions/467/implement-glob-matcher
var globber = function(patterns, strings) {
  // console.log("globber called with: " + patterns.join(":"), strings.join(":"))
  var first = patterns[0],
      rest = patterns.slice(1),
      len = strings.length,
      matchFound;

  if(first === '*') { 
    for(var i = 0; i <= len; ++i) {
      // console.log("* " + i + " trying " + rest.join(":") + " with " + strings.slice(i).join(":"))
      if(globber(rest, strings.slice(i))) return true;
    }
    return false;
  } else { 
    matchFound = (first === strings[0]);
    // console.log ("literal matching " + first + " " + strings[0] + " " + !!matched)
  }

  return matchFound && ((!rest.length && !len) || globber(rest, strings.slice(1)));
};
module.exports = globber;
},{}],10:[function(require,module,exports){var Uid = function(){
  return (Uid.counter++ + "");
}

Uid.counter = 1;

module.exports = Uid;
},{}],11:[function(require,module,exports){// based on https://github.com/timoxley/next-tick/blob/master/index.js

// postMessage behaves badly on IE8
if (window.ActiveXObject || !window.postMessage) {

  var nextTick = function(fn) {
    setTimeout(fn, 0);
  }

} else {

  // based on setZeroTimeout by David Baron
  // - http://dbaron.org/log/20100309-faster-timeouts
  var timeouts = []
    , name = 'next-tick-zero-timeout'

  window.addEventListener('message', function(e){
    if (e.source == window && e.data == name) {
      if (e.stopPropagation) e.stopPropagation();
      if (timeouts.length) timeouts.shift()();
    }
  }, true);

  var nextTick = function(fn){
    timeouts.push(fn);
    window.postMessage(name, '*');
  }

}

module.exports = nextTick;
},{}],4:[function(require,module,exports){var Benchmark = require('./benchmark');
var setproto = require('./object/setproto');
var extend = require('./object/extend');
var Tube = require('./tube');

// usage: Buffer(url || File || ArrayBuffer)
var BufferLoader = function(src, opts){

  var loader = Tube();
  setproto(loader, BufferLoader.proto);

  if (opts && opts.benchmark) loader.benchmark = opts.benchmark;

  //buf.remember("load error")
  // loader.on("load", function(abuf){ setupBuffer(loader, abuf) }); // <-- hm, needs ref to buf

  loader.src = src;

  return loader;
}

BufferLoader.proto = {};
extend(BufferLoader.proto, Tube.proto);

BufferLoader.proto.load = function(){ var loader = this;
  var src = loader.src;
  if (typeof src === "string") loader.loadFromUrl(src)
  else if (src instanceof File) loader.loadFromFile(src);
  else if (src instanceof ArrayBuffer) loader("load", src);
}

BufferLoader.proto.loadFromFile = function(file){ var loader = this;

  var r = new FileReader();
  
  r.addEventListener('load', function(e){
    if (loader.benchmark) loader.benchmark.stop("fetch-from-disk");
    loader('load', r.result, e);
    // console.log(r.file.name + " 100%: " + r.result.byteLength + " bytes")
  });

  r.addEventListener('error', function(e){
    // console.log(arguments);
    loader('error', e, r)
  });

  r.addEventListener('progress', function(e){ 
    //console.log(r.file.name + " " + (e.loaded / e.total * 100) + "%: " + e.loaded + " bytes") 
    loader('progress', e)
  });

  if (loader.benchmark) loader.benchmark.start("fetch-from-disk");
  r.readAsArrayBuffer(file);

};


BufferLoader.proto.loadFromUrl = function(url){ var loader=this;

  var xhr = new XMLHttpRequest();
  xhr.open("GET", url);
  xhr.responseType = "arraybuffer";

  xhr.addEventListener('load', function(e){
    if (loader.benchmark) loader.benchmark.stop("fetch-from-network");
    // console.log(r.file.name + " 100%: " + r.result.byteLength + " bytes")
    loader('load', xhr.response, e);
  });

  xhr.addEventListener('error', function(e){
    // console.log(arguments);
    loader('error', e, xhr);
  });

  xhr.addEventListener('progress', function(e){ 
    //console.log(r.file.name + " " + (e.loaded / e.total * 100) + "%: " + e.loaded + " bytes") 
    loader('progress', e)
  });

  if (loader.benchmark) loader.benchmark.start("fetch-from-network");
  // xhr.open("GET", url); 
  
  xhr.send();

};

module.exports = BufferLoader;
},{"./benchmark":2,"./object/setproto":7,"./object/extend":12,"./tube":3}],12:[function(require,module,exports){module.exports = function(destination, source) {
  for (var property in source)
    destination[property] = source[property];
  return destination;
};
},{}],5:[function(require,module,exports){(function(){var blockSigs = require('./spec').blockSigs;
var extSigs = require('./spec').extSigs;
var palette = require('./palette');
var makeCurrentFrame =  require('./animate').makeCurrentFrame;

var BinarySpecReader = require('../../binaryspec');
var spec = require('./spec').spec;
var specReader = BinarySpecReader(spec);

var decode = function(gif, opts){
  decodeNextPart(gif);
};


// this gets called when more binary data is ready to process
// it gets set as BufferedReader's onload function
/*
var moreDataInBufferEvent = function(gif){
  if (gif.waitingForFileBuffer) decodeNextPart(gif);
};
*/

/*
loads the next part of the gif, eventually calling the gif's onload function when every part has been loaded

this function gets called when a previous part has finished loading, 
and when there's more data in the buffered reader

if the next part has a fixed size according to spec, we know whether we have to wait on the buffer to fill or not
if the next part has a variable size, call the howToDecode function which will try to load it
*/
var decodeNextPart = function(gif, nextPart){
  //gif.waitingForFileBuffer = false;

  nextPart = nextPart || "header";
  var buf = gif.buf;

  while (nextPart !== "done" && nextPart !== "error"){

    if (nextPart in howToDecode && typeof howToDecode[nextPart] === "function") { // dont know size
      nextPart = howToDecode[nextPart](gif);
    } else { // we know exact size of next part

      var partSize = specReader.parts[nextPart].byteSize;

      if (buf.abuf.byteLength < partSize + buf.cursor){
        gif.waitingForFileBuffer = true;
        return;
      } else {
        var fields = specReader.decodeBinaryFieldsToJSON(nextPart, buf.cursor, buf);
        buf.cursor += specReader.parts[nextPart].byteSize;
        nextPart = handleLoadedPart[nextPart](gif, fields);
      }
    }
    // todo: maybe do this when and return unknown from dataBlock read
    // if (nextPart === "unknown") nextPart = "dataBlock"

  }

  if (nextPart === "done"){
    // create palette from all frames' palettes

    if (gif.benchmark) gif.benchmark.start("palette");
    gif.paletteTotal = palette.create(gif);
    if (gif.benchmark) gif.benchmark.stop("palette");

    // todo: move elsewhere
    makeCurrentFrame.bind(gif)();
//    GIF.decode.doneDecodingCleanup(gif);
    //gif.tube("progress", "done");
    gif.decoded = true;
    gif.tube("decoded");
    return;
  }
  
};


var howToDecode = {
  "globalPalette": function(gif){ var buf = gif.buf;
    var paletteSize = gif.paletteSize * 3; // r,g,b bytes

    if (buf.abuf.byteLength < buf.cursor + paletteSize) {
      gif.waitingForFileBuffer = true;
      return;
    }

    //gif.palette = GIF.palette.binary2rgba(gif.reader.buffer.slice(gif.cursor, gif.cursor + paletteSize));
    gif.palette = palette.binary2rgba(new Uint8Array(buf.abuf, buf.cursor, paletteSize));
    buf.cursor += paletteSize;

    return "dataBlock";
  },
  "localPalette": function(gif){ var buf = gif.buf;
    var paletteSize = gif.frames[gif.frames.length - 1].paletteSize * 3; // r,g,b bytes

    if (buf.abuf.byteLength < buf.cursor + paletteSize) {
      gif.waitingForFileBuffer = true;
      return;
    }

    gif.frames[gif.frames.length - 1].palette = palette.binary2rgba(new Uint8Array(buf.abuf, buf.cursor, paletteSize));
    buf.cursor += paletteSize;
    return "imageData";
  },
  "dataBlock": function(gif){ var buf = gif.buf;

    if (buf.abuf.byteLength < buf.cursor + 1) {
      gif.waitingForFileBuffer = true;
      return;
    }

    var blockType = "unknown",
        extType = "unknown",
        nextPart;

    var blockSig = buf.u8[buf.cursor];
    
    if (blockSig in blockSigs) {
      blockType = blockSigs[blockSig];
    } else {
      //console.log("unknown data block type ("+ Number(blockSig).toString(16) +")!");
      nextPart = "done";
    }

    if (blockType === "extension"){ // we need to determine what kind of extension the block is
      // need next two bytes to find out what kind of extension the data block is
      if (buf.abuf.byteLength < buf.cursor + 2) {
        gif.waitingForFileBuffer = true;
        return;
        console.log("ran out of buffer")
      }
      var extSig = buf.u8[buf.cursor+1];
      if (extSig in extSigs) {
        extType = extSigs[extSig];
        nextPart = extType;
      } else { 
        //console.log("unknown extension type ("+ Number(extSig).toString(16) +")")
        nextPart = "done";
      }
    } else {
      if (blockType === "unknown") { blockType = "dataBlock"; buf.cursor += 1 }
      nextPart = blockType;
    }

    return nextPart;

  },
  "trailer": function(gif){
    return "done";
  }
};

var handleLoadedPart = {
  "header": function(gif, fields){

    if (fields.signature != "GIF"){
      gif.tube("error", "file doesn't seem to be a gif (file signature: '"+fields.signature+"')");
      return "error";
    }

    // gif.version = fields.version
      
    return "screenDesc";
  },
  "screenDesc": function(gif, fields){
    for (var field in fields) gif[field] = fields[field];
    // todo: make nicer
    // this should be a u3 not this bit[3] bit-shifted horseshit 
    gif.paletteSize = (gif.paletteSize[0] << 2) + (gif.paletteSize[1] << 1) + (gif.paletteSize[2]);
    gif.paletteSize = Math.pow(2, gif.paletteSize + 1)

    if (gif.paletteExists) return "globalPalette";
    else return "dataBlock";
  },
  "imageDesc": function(gif, fields){
    // make a blank image frame if none exists or the last frame already has image data
    // we don't know if a blank frame has already been created because a graphic control block might
    // have come before this block and made one
    if (!gif.frames.length || ("w" in gif.frames[gif.frames.length - 1]))
      gif.frames.push({})
    
    var frame = gif.frames[gif.frames.length - 1]

    for (var field in fields) frame[field] = fields[field];
    frame.paletteSize = (frame.paletteSize[0] << 2) + (frame.paletteSize[1] << 1) + (frame.paletteSize[2]);
    frame.paletteSize = Math.pow(2, frame.paletteSize + 1)

    if (frame.paletteExists) return "localPalette";
    else return "imageData";
  },
    // if sublocks was able to read:
    //   increase cursor from subblocks
    //   increase cursor from fields
    // else
    //   set waiting
    //   subtract 2 from cursor (for the 2 that was read to get the data block)
    //   subtract part.bytesize from cursor (not needed in this case?)
  "applicationExtension": function(gif, fields){
    var blockinfo = readSubBlocks(gif);
    if (blockinfo === false) {
      gif.waitingForFileBuffer = true
      gif.buf.cursor -= specReader.parts.applicationExtension.byteSize
      return;
    } else {
      var extension = {"data": blockinfo};
      for(var field in fields)
        extension[field] = fields[field]
      gif.extensions.push(extension)
      return "dataBlock";
    }
  },
  "comment": function(gif, fields){
    var blockinfo = readSubBlocks(gif);
    if (blockinfo === false) {
      gif.waitingForFileBuffer = true;
      gif.buf.cursor -= specReader.parts.comment.byteSize;
      return;
    } else {
      var extension = {"comment": blockinfo};
      for(var field in fields)
        extension[field] = fields[field];
      gif.extensions.push(extension);
      return "dataBlock";
    }
  },
  "plainText": function(gif, fields){
    var blockinfo = readSubBlocks(gif);
    if (blockinfo === false) {
      gif.waitingForFileBuffer = true;
      gif.buf.cursor -= specReader.parts.plainText.byteSize;
      return;
    } else {
      var extension = {"plainText": blockinfo};
      for(var field in fields)
        extension[field] = fields[field];
      gif.extensions.push(extension);
      return "dataBlock";
    }
  },
  "graphicControl": function(gif, fields){
    var dm = (fields.disposalMethod[0] << 2) + (fields.disposalMethod[1] << 1) + (fields.disposalMethod[2]);
    gif.frames.push({"delay": fields.delay,
                     "transparentIndex": fields.transparentColor ? fields.transparentIndex : -1,
                     "disposalMethod": dm})
    return "dataBlock";
  },
  "imageData": function(gif, fields){
    var blockinfo = readSubBlocks(gif);
    if (blockinfo === false) {
      gif.waitingForFileBuffer = true
      gif.buf.cursor -= specReader.parts.imageData.byteSize
      console.log("fucked")
      return;
    } else {

// TODO: ENABLE THIS!
//      gif.tube("progress", "found " + gif.frames.length + " frames");

      var frame = gif.frames[gif.frames.length - 1];
      //var palette = ("palette" in frame) ? frame.palette : gif.palette // local palette otherwise global palette
      frame.lzwCodeSize = fields.lzwCodeSize;
      frame.blockinfo = blockinfo;
//      var transparentIndex = ("transparentIndex" in frame) ? frame.transparentIndex : -1
      return "dataBlock";
    }    

  }
};

// read subblocks out of a gif's buffer...
// reads block sizes and returns an object with a start cursor and an array of block ends
// returns false if there's not enough data 
var readSubBlocks = function(gif){

  if (gif.benchmark) gif.benchmark.start("read-subblocks");

  var blockEnds = [];

  var buf = gif.buf,
      u8 = buf.u8,
      byteLength = u8.byteLength;
  
  // gif.cursor is for whole file... pos is a cursor for just this blob
  var startBlockCursor = buf.cursor;
  var pos = buf.cursor;
  startBlockCursor += 1;
  var byteSize = 0;
  var outOfData = false

  // only actually advance cursor if we can read in all the sub blocks from the buffer
  var cursorTemp = 0;

  while(!outOfData){
    // get block size
    if (byteLength < pos + 1) { outOfData = true; break;}
    byteSize = u8[pos];
    pos += 1;

    // a sub block with size 0 indicates end of sub blocks
    if (byteSize === 0) { cursorTemp += 1; break;}
    
    // read block
    if (byteLength < pos + byteSize) { outOfData = true; break;}
    blockEnds.push(pos + byteSize);
    
    pos += byteSize
    cursorTemp += byteSize + 1
    // gif.subBlocksRead += 1
  }

  // TODO? CLEAN UP!
  if (outOfData) { 
    // gif.bufferMisses += 1
    // gif.benchmark.wasted += (Date.now() - start) / 1000
    console.log("out of data")
    return false

  } else { // end of sub blocks happened
    buf.cursor += cursorTemp
    //if ("onprogress" in gif) gif.onprogress(gif, Math.floor(gif.cursor / gif.file.size * 100))
//    gif.benchmark.subblocks += (Date.now() - start) / 1000;
    if (gif.benchmark) gif.benchmark.stop("read-subblocks");
    return {start: startBlockCursor, blockEnds: blockEnds};

  }
};

module.exports = decode;
})()
},{"./spec":13,"./palette":14,"./animate":15,"../../binaryspec":16}],13:[function(require,module,exports){var blockSigs = {
 0x21: "extension",
 0x2c: "imageDesc",
 0x3b: "trailer"
};

exports.blockSigs = blockSigs;

var extSigs = {
 0xf9: "graphicControl",
 0xfe: "comment",
 0x01: "plainText",
 0xff: "applicationExtension"
};

exports.extSigs = extSigs;

/*
GIF.getGeneralBlockType = function(sig){
  if (sig == 0x3b)
    return "trailer"
  else if (sig < 0x7F)
    return "graphic rendering block"
  else if (sig < 0xF9)
    return "control block"
  else return "special purpose block"
}

*/

var spec = {
  "header": [
    "str[3] signature",
    "str[3] version"
  ],
  "screenDesc": [
    "u16    w",
    "u16    h",
    "bit    paletteExists",
    "bit[3] resolution ignore",
    "bit    sortFlag ignore",
    "bit[3] paletteSize",
    "u8     bgColorIndex",
    "u8     aspectRatio ignore"
  ],
  "imageDesc": [
    "u8     sig ignore",
    "u16    x",
    "u16    y",
    "u16    w",
    "u16    h",
    "bit    paletteExists",
    "bit    interlaced",
    "bit    sortFlag",
    "bit[2] reserved ignore",
    "bit[3] paletteSize"   
  ],
  "applicationExtension": [
    "u8     sig ignore",
    "u8     extSig ignore",
    "u8     blockSize ignore",
    "str[8] identifier",
    "str[3] authCode ignore"
  ],
  "graphicControl": [
    "u8     sig ignore",
    "u8     extSig ignore",
    "u8     blockSize ignore",
    "bit[3] reserved ignore",
    "bit[3] disposalMethod",
    "bit    userInput ignore",
    "bit    transparentColor",
    "u16    delay",
    "u8     transparentIndex",
    "u8     blockTerminator ignore"
  ],
  "comment": [
    "u8     sig ignore",
    "u8     extSig ignore"
  ],
  "plainText": [
    "u8    sig ignore",
    "u8    extSig ignore",
    "u8    blockSize",
    "u16   textGridLeft",
    "u16   textGridTop",
    "u16   textGridWidth",
    "u16   textGridHeight",
    "u8    charCellWidth",
    "u8    charCellHeight",
    "u8    fgColorIndex",
    "u8    bgColorIndex"
  ],
  "imageData": [
    "u8     lzwCodeSize"
  ]
};

exports.spec = spec;
},{}],14:[function(require,module,exports){(function(){var rgba2css = require('../../color/rgba2css');
var create2d = require('../../create/2d');
var createImageData = require('../../create/imagedata');

var palette = {};

// flat typed array of r g b a values from binary GIF palette
palette.binary2rgba = function(abuf /*, transparentIndex */){
  var table = new Uint8Array(abuf.byteLength/3*4);
  var counter = 0
  for(var i = 0, length = abuf.byteLength/3*4; i<length; i+=4){
    table[i] = abuf[counter];
    table[i+1] = abuf[counter+1];
    table[i+2] = abuf[counter+2];
    table[i+3] = 255;
    counter += 3;
  }
/*
  if (transparentIndex !== undefined)
    table[transparentIndex*4 + 3] = 0;
*/
  return table;
};


// TODO. break this up, make less confusing, etc.
// horrible monolithic palette mappings constructor
// builds a hash lookup to map rgba value to palette index
// builds an array to map indexes to rgba values
// builds an array to map indexes to css values
// builds an imagedata to map indexes to colors
//
// gif palette index numbers are not preserved... 
// this makes a palette from all local palettes (frames) plus global palette
// only ONE transparent value is included... 0,0,0,0, others are ignored
palette.create = function(gif){

  // boot up palette with a transparent color to start with
  var rgba2Index = {"0":0},
      index2Css = ["rgba(0,0,0,0)"],
      index2Rgba = [[0,0,0,0]];

  var addPalette = function(palette){
    for(var i=0, size=palette.length; i<size; i+=4){
      var a = palette[i+3]
      if (a === 0) continue; // skip transparent values... just use first palette index for them
      var r = palette[i],
          g = palette[i+1],
          b = palette[i+2],
          index = (r | (g << 8) | (b << 16) | (a << 24)).toString();
          // http://jsperf.com/rgba-hash-lookup
          // also... this overflows and flips sign... is that safe over diff. js implementations?
          if (index in rgba2Index) continue;
          rgba2Index[index] = index2Rgba.length
          var rgba = [r,g,b,a]
          index2Rgba.push(rgba)
          index2Css.push(rgba2css(rgba))
    }
  };

  if ("palette" in gif)
    addPalette(gif.palette)

  for(var f=0; f<gif.frames.length; f++){
    var frame = gif.frames[f];
    if (!("palette" in frame)) continue;
    addPalette(frame.palette)
  }

  // create this last, as we don't know palette size until it is built
  var imagedata = createImageData(index2Rgba.length, 1)
  for(var i=0, size=index2Rgba.length*4, data=imagedata.data; i<size; i+=4){
    var rgba = index2Rgba[i/4]
    data[i] = rgba[0]
    data[i+1] = rgba[1]
    data[i+2] = rgba[2]
    data[i+3] = rgba[3]
  }

  return {"rgba2Index": rgba2Index,
          "index2Rgba": index2Rgba,
          "index2Css": index2Css,
          "imagedata": imagedata,
          "length": index2Rgba.length}
};

module.exports = palette;
})()
},{"../../color/rgba2css":17,"../../create/2d":18,"../../create/imagedata":19}],17:[function(require,module,exports){var rgba2css = function(rgba){
  //return "rgba(" + rgba.join(",") + ")"
  // arraybuffers have no join...
  return "rgba(" + rgba[0] + "," + rgba[1] + "," + rgba[2] + "," + rgba[3] + ")"
}
module.exports = rgba2css;
},{}],18:[function(require,module,exports){var create2d = function(w, h){
  var canvas = document.createElement("canvas");
  canvas.width = w || 0;
  canvas.height = h || 0;
  return canvas.getContext("2d");
};

if (typeof module !== "undefined") module.exports = create2d;
},{}],19:[function(require,module,exports){var createImageData = function createImageData(w, h){
  return createImageData.ctx.createImageData(w, h);
};
createImageData.ctx = document.createElement("canvas").getContext("2d");

if (typeof module !== "undefined") module.exports = createImageData;
},{}],15:[function(require,module,exports){//make a function on a gif that determines which frame to show given a timestamp.
// this allows all gifs to be synced no matter when they are loaded
// however, this is undesirable if you want to start an animation on frame 0 when it is displayed.
var makeCurrentFrame = function(){
  var defaultDelay = 100; // msecs

  if (this.frames.length === 1){ // shortcut for static gifs
    this.currentFrame = function(){ return 0 };
  }

  var totalDelay = 0;
  var delays = [];
  for(var i=0; i<this.frames.length; i++){
    var frame = this.frames[i];
    var delay = ("delay" in frame && frame.delay > 0) ? frame.delay * 10 : defaultDelay;
    totalDelay += delay;
    delays.push(totalDelay);
  }

  this.currentFrame = makeCurrentFrameFunction(delays);
};

var makeCurrentFrameFunction = function(delays){ 
  var totalTime = delays[delays.length - 1];
  return function(timestamp){
    var r = (timestamp || Date.now()) % totalTime;
    for(var i=0; i<delays.length; i++)
      if (r < delays[i])
        return i;
    return i;
  }
};

exports.makeCurrentFrame = makeCurrentFrame;
},{}],16:[function(require,module,exports){var tokenize = require('./string/tokenize');
var BitView = require('../deps/bitview/bitview.timb.js');

var BinarySpecReader = function(spec){
  var reader = Object.create(BinarySpecReader.proto);
  var parts = reader.parts = {};

  for (var partName in spec)
    parts[partName] = decodeSpecPart(spec[partName]);

  return reader;
};

BinarySpecReader.proto = {};

// decodes GIF parsing info from the specification
var decodeSpecPart = function(part){

  var bitSizes = {
    "bit": 1,
    "str": 8,
    "i8": 8,
    "u8": 8,
    "i16": 16,
    "u16": 16,
    "i32": 32,
    "u32": 32
  };

  var parsedSpec = {"fields": []};
  var size = 0;

  for (var i=0; i<part.length; i++){
    
    var instruction = tokenize(part[i]);
    var ignore = /(^|\s)ignore($|\s)/.test(part[i]);
    var typeInfo = instruction[0].split("["); // eg "uint(16)"
    var fieldType = typeInfo[0];
    var fieldLength = parseInt(typeInfo[1] || 1);
    var isArray = (typeInfo.length > 1);
                                                // char(2) is 16 bits. uint(16) is 16 bits
    var bitSize = bitSizes[fieldType] * fieldLength;

    parsedSpec.fields.push({"name": instruction[1],
                      "type": fieldType,
                      "ignore": ignore,
                      "bitSize": bitSize,
                      "isArray": isArray});
    size += bitSize;
  }
  
  parsedSpec.bitSize = size;
  parsedSpec.byteSize = size / 8;

  return parsedSpec;
};

// decodes a chunk according to data types in gif.spec.js
// todo: rewrite the binary decoding stuff to not be so shit
BinarySpecReader.proto.decodeBinaryFieldsToJSON = function(partName, cursor, buf){ var reader = this;

  var part = reader.parts[partName];

  var fields = {}, numFields = part.fields.length, bitPos = 0;

  for(var i = 0; i < numFields; i++){

    var field = part.fields[i];
    if (!field.ignore) {
      var bitOffset = bitPos % 8;
      var decodeByteStart = Math.floor((bitPos - bitOffset) / 8);
      var decodeByteEnd = decodeByteStart + Math.ceil(field.bitSize / 8);

      switch(field.type){
        case "u8":
          fields[field.name] = buf.u8[cursor + decodeByteStart]; break;
        case "i8":
          fields[field.name] = buf.dv.getInt8(cursor + decodeByteStart); break;
        case "u16":
          fields[field.name] = buf.dv.getUint16(cursor + decodeByteStart, true); break;
        case "i16":
          fields[field.name] = buf.dv.getInt16(cursor + decodeByteStart, true); break;
        case "u32":
          fields[field.name] = buf.dv.getUint32(cursor + decodeByteStart, true); break;
        case "i32":
          fields[field.name] = buf.dv.getInt32(cursor + decodeByteStart, true); break;
        case "str":
          fields[field.name] = abuf2str(buf.abuf, cursor + decodeByteStart, field.bitSize >> 3); break;
        case "bit":
          if (!field.isArray) {
            fields[field.name] = new BitView(buf.abuf, cursor + decodeByteStart).getBit(bitOffset);
          } else {
            var bv = new BitView(buf.abuf, cursor + decodeByteStart);
            var bits = [];
            for (var bb=bitOffset; bb<bitOffset+field.bitSize; bb++){
              bits.push(bv.getBit(bb))
            }
            fields[field.name] = bits;
          }
          break;
         // fields[field.name] = abuf2str(buf.abuf, cursor + decodeByteStart, field.bitSize / 8); break;
        default:
          console.log("please implement: " + field.type + ", " + partName);
      }
    }
    bitPos += field.bitSize;
  }
  //gif.cursor += part.byteSize;
  //GIF.decode.handleLoadedPart[gif.nextPart](gif, fields);
  // console.log(fields);
  return fields
}

// todo: wasteful of space?
var abuf2str = function(abuf, offset, len) {
  return String.fromCharCode.apply(null, new Uint8Array(abuf, offset, len));
}


module.exports = BinarySpecReader;
},{"./string/tokenize":8,"../deps/bitview/bitview.timb.js":20}],20:[function(require,module,exports){// @timb: edit of https://github.com/kig/bitview.js by Ilmari Heikkinen
var BitView = function(buf, offset) {
  this.buffer = buf;
  this.u8 = new Uint8Array(buf, offset);
};

BitView.prototype.getBit = function(idx) {
  var v = this.u8[idx >> 3];
  var off = idx & 0x7;
  return (v >> (7-off)) & 1;
};

module.exports = BitView;
},{}],6:[function(require,module,exports){var create2d = require('../../create/2d');
var createImageData = require('../../create/imagedata');
//import queueOrNextTick from 'lib/nexttick4';
var nextTick = require('../../nexttick');

var lzwImageData = require('./decode-lzw');

/*
  this has two rendering types... canvas and webgl
  both build gif image frames from binary data that was decoded when the gif loaded

  canvas method will build full-frame canvas objects and place them into each frame of the gif.
  eg, render.canvas(gif) will create gif.frames[0].ctx and so on

  webgl method builds imagedata textures and tries to pack multiple frames efficiently into channels if it can.

  in gifs, each frame stored might just be a rectangle that changed from the previous frame.
  these are referred to as "raw frames" in this code
*/

var render = function(gif, config){

  config = config || {};

  var bench = gif.benchmark || false;

  var frameNum = config.frameNum || 0;

  if (frameNum === 0){ // preallocate all frames
    for (var i=0; i<gif.frames.length; i++){
      gif.frames[i].ctx = create2d(gif.w, gif.h);
      gif.buf.pixeldata = new Uint8Array(gif.w * gif.h);
    }
  }

  if (frameNum >= gif.frames.length) { // done making frames
    //gif.tube("progress", "done")
    gif.rendered = true;
    gif.tube("rendered");
    return
  }

  var frame = gif.frames[frameNum];
  var pixeldata = gif.buf.pixeldata;

  // gif.percentLoaded = frameNum / gif.frames.length
  //gif.tube("decompressing frame " + (frameNum+1))



  // lzw
  if (bench) bench.start("decompress-lzw");
  // var pixeldata = new Uint8Array(frame.w * frame.h);
  //pixeldata.area = frame.w * frame.h;
  lzwImageData(frame.blockinfo, gif.buf.u8, frame.lzwCodeSize, frame.w, frame.h, pixeldata);
  if (bench) bench.stop("decompress-lzw");

  // deinterlace
  if (frame.interlaced) {
    if (bench) bench.start("deinterlace");
    pixeldata = deinterlacePixels(pixeldata, frame.w, frame.h)
    if (bench) bench.stop("deinterlace");
  }

  // canvas-ize
  if (bench) bench.start("pixeldata-to-canvas");
  makeFullFrame(pixeldata, gif, frameNum);
  if (bench) bench.stop("pixeldata-to-canvas");

  // todo: queue this better
  var func = render.bind(undefined, gif, {"frameNum": frameNum+1});
  // queueOrNextTick(func);
  nextTick(func);
  // setZeroTimeout(func);
  //setTimeout(func, 1) // otherwise progress won't show in chrome

};

var makeFullFrame = function(pixeldata, gif, frameNum){

  var frame = gif.frames[frameNum],
      ctx = frame.ctx;

  if (frameNum === 0){ // don't need previous frame info to do disposal if it's the first frame
    ctx.putImageData(pixelDataToImageData(pixeldata, gif, frame), frame.x, frame.y,
                        0,0,frame.w,frame.h);
    return;
  }

  var prevFrameNum = frameNum-1,
      prevFrame = gif.frames[prevFrameNum],
      prevCanvas = prevFrame.ctx.canvas,
      rawCtx;

  // disposal method is 0 (unspecified) or 1 (do not dispose)
  // do nothing, paste new frame image over old one
  if (prevFrame.disposalMethod === 0 || prevFrame.disposalMethod === 1){
    rawCtx = makeRawFrameAsContext(gif, frameNum, pixeldata);
    ctx.drawImage(prevCanvas, 0, 0)
    ctx.drawImage(rawCtx.canvas, 0,0,frame.w,frame.h,   frame.x,frame.y,frame.w,frame.h)
  }

  // disposal method is 2 (restore to background color)
  // but everyone just restores to transparency
  // see notes on http://www.imagemagick.org/Usage/anim_basics/#background
  if (prevFrame.disposalMethod === 2){
    // fast path... whole frame cleared
    if (prevFrame.x === 0 && prevFrame.y === 0 && prevFrame.w === gif.w && prevFrame.h === gif.h) {
      // var rawContext = makeRawFrameAsContext(gif, frameNum)
      // frame.context.drawImage(rawContext.canvas, frame.x, frame.y);
      ctx.putImageData(makeRawFrameAsImageData(gif, frameNum, pixeldata), frame.x, frame.y,
                            0,0,frame.w,frame.h);
    } else { // draw the edges of the previous frame and then draw the current frame overtop
      /*
      .__________.
      |__________|
      | |      | |
      | |      | |
      |_|______|_|
      |__________|
      */
      //top
      if (prevFrame.y > 0)
        ctx.drawImage(prevCanvas, 0,0, gif.w,prevFrame.y,   
                                    0,0, gif.w,prevFrame.y);
      //left
      if (prevFrame.x > 0)
        ctx.drawImage(prevCanvas, 0,prevFrame.y, prevFrame.x,prevFrame.h, 
                                    0,prevFrame.y, prevFrame.x,prevFrame.h);
      // right
      if (prevFrame.x+prevFrame.w < gif.w)
        ctx.drawImage(prevCanvas, prevFrame.x+prevFrame.w, prevFrame.y,   (gif.w-prevFrame.x-prevFrame.w),prevFrame.h,
                                    prevFrame.x+prevFrame.w, prevFrame.y,   (gif.w-prevFrame.x-prevFrame.w),prevFrame.h);
      // bottom
      if (prevFrame.y+prevFrame.h < gif.h)
        ctx.drawImage(prevCanvas, 0,prevFrame.y+prevFrame.h,  gif.w,(gif.h-prevFrame.y-prevFrame.h),
                                    0,prevFrame.y+prevFrame.h,  gif.w,(gif.h-prevFrame.y-prevFrame.h))

      rawCtx = makeRawFrameAsContext(gif, frameNum, pixeldata);
      ctx.drawImage(rawCtx.canvas, 0,0,frame.w,frame.h,   frame.x,frame.y,frame.w,frame.h);

    }
  }

  // disposal method is 3 (restore to previous)
  if (prevFrame.disposalMethod === 3){
    // look for last previous frame that doesn't have "previous" disposal method
    while(prevFrameNum > 0 && gif.frames[prevFrameNum].disposalMethod === 3) prevFrameNum -= 1;
    prevFrame = gif.frames[prevFrameNum]
    // console.log(prevFrameNum)
    if (prevFrame.disposalMethod != 3) ctx.drawImage(prevFrame.ctx.canvas, 0, 0)
    rawCtx = makeRawFrameAsContext(gif, frameNum, pixeldata);
    ctx.drawImage(rawCtx.canvas, 0,0,frame.w,frame.h,   frame.x, frame.y,frame.w,frame.h)
  }

}

var makeRawFrameAsContext = function(gif, frameNum, pixeldata){
  // cache a context to reuse
  if (makeRawFrameAsContext.ctx && 
      makeRawFrameAsContext.ctx.canvas.width === gif.w &&
      makeRawFrameAsContext.ctx.canvas.height === gif.h) {
    var ctx = makeRawFrameAsContext.ctx;
  } else {
    var ctx = makeRawFrameAsContext.ctx = create2d(gif.w, gif.h);
  }

  var frame = gif.frames[frameNum];
  pixeldata = pixeldata || frame.pixelData;
  var palette = ("palette" in frame) ? frame.palette : gif.palette;
  var transparentIndex = ("transparentIndex" in frame) ? frame.transparentIndex : -1;

  if (transparentIndex > -1) palette[(transparentIndex*4)+3] = 0;

  var rawImageData = pixelData2imageData(gif, palette, pixeldata, frame.w, frame.h, transparentIndex);

  ctx.putImageData(rawImageData, 0, 0, 0,0,frame.w,frame.h)

  return ctx

  // return imageData2contextDirty(rawImageData, 0,0,frame.w,frame.h);
};

var makeRawFrameAsImageData = function(gif, frameNum, pixeldata){
  var frame = gif.frames[frameNum];
  pixeldata = pixeldata || frame.pixelData;
  var palette = ("palette" in frame) ? frame.palette : gif.palette;
  var transparentIndex = ("transparentIndex" in frame) ? frame.transparentIndex : -1;

  if (transparentIndex > -1) palette[(transparentIndex*4)+3] = 0;

  return pixelData2imageData(gif, palette, pixeldata, frame.w, frame.h, transparentIndex)
};

var pixelDataToImageData = function(pixeldata, gif, frame){
  // var frame = gif.frames[frameNum];
  var palette = ("palette" in frame) ? frame.palette : gif.palette;
  var transparentIndex = ("transparentIndex" in frame) ? frame.transparentIndex : -1;

  if (transparentIndex > -1) palette[(transparentIndex*4)+3] = 0;

  return pixelData2imageData(gif, palette, pixeldata, frame.w, frame.h, transparentIndex)
};


var imageData2context = function(imageData){
  var ctx = create2d(imageData.width, imageData.height)
  ctx.putImageData(imageData, 0, 0)
  return ctx
};
var imageData2contextDirty = function(imageData, dx,dy,dw,dh){
  var ctx = create2d(imageData.width, imageData.height)
  ctx.putImageData(imageData, 0, 0, dx,dy,dw,dh)
  return ctx
};

var pixelData2imageData = function(gif, palette, pixeldata, w, h, transparentIndex){
  if (pixelData2imageData.imagedata && 
      pixelData2imageData.imagedata.width === gif.w &&
      pixelData2imageData.imagedata.height === gif.h) {
    var imagedata = pixelData2imageData.imagedata;
  } else {
    var imagedata = pixelData2imageData.imagedata = createImageData(gif.w, gif.h);
  }

  var data = imagedata.data
  // var i = pixeldata.length;
  var i = 0;

  for (var y=0; y<h; y++){ var yinc = y*gif.w;
    for (var x=0; x<w; x++){ //var i = y*gif.w+x;
      // var cp = i*4
      var cp = (x + yinc)*4
      var ct = pixeldata[i]*4 //palette[pixeldata[i]]
      data[cp] = palette[ct]
      data[cp+1] = palette[ct+1]
      data[cp+2] = palette[ct+2]
      data[cp+3] = palette[ct+3]
      i += 1;
    }
  }

  return imagedata;
};

// deinterlace the 4 chunks from one imageData blob in one pass
// see appendix e of gif spec
//
// ideas: canvasize first, then deinterlace... 
// and do canvas drawImage calls or putImageData calls to move the rows around?
var deinterlacePixels = function(oldpixels, w, h){

  var pixels = new Uint8Array(oldpixels.length)
  var stripes2 = Math.ceil(h/8) // the row where the 2nd interlaced chunk starts
  var stripes3 = Math.ceil(h/4)
  var stripes4 = Math.ceil(h/2)

  // pixels[(w*y) +x] = oldpixels[(w*y) +x] <- base calculation

  for(var y=0; y<h; y++){
    var interlacedRowOffset
    var rowOffset = w * y
    if (y % 8 === 0)
      interlacedRowOffset = w* (y/8)
    else if ((y + 4) % 8 === 0)
      interlacedRowOffset = w* ((y-4)/8+stripes2)
    else if (y % 2 === 0)
      interlacedRowOffset = w* ((y-2)/4+stripes3)
    else
      interlacedRowOffset = w* ((y-1)/2+stripes4)
    for(var x=0; x<w; x++)
      pixels[rowOffset+x] = oldpixels[interlacedRowOffset+x]
  }

  return pixels;
};

module.exports = render;
},{"../../create/2d":18,"../../create/imagedata":19,"../../nexttick":11,"./decode-lzw":21}],21:[function(require,module,exports){// looked at imagemagick implementation instead of trying to figure out my buggy one

/*
    decodes an lzw compressed gif frame 
    mostly based on imagemagick's implementation

    data, binary string
    data_size, lzw compression code size
    w, h, width and height of image
    paletteRemap is an optional array of different palette indexes to use

    returns typed array of palette index values

    this assumes we have the entire data for the frame buffered...  
    its possible to modify this so that a resumable state could be passed in, but prob not necessary
*/ 
// TODO: cache stacks?
var lzwImageData = function(blockinfo, u8buf, data_size, w, h, pixels, paletteRemap){
  var MaxStack = 4096;
  var NullCode = -1;
  var cursor = 0
  var npix = w * h;
  var code, in_code, old_code;
  var first = 0;
  var top = 0;
  var pi = 0
  var pixels = pixels || new Uint8Array(npix);
  var prefix = new Uint16Array(MaxStack*2) //Uint16Array(MaxStack*1.5) <-- this makes a nice glitch
  var suffix = new Uint8Array(MaxStack)
  // var prefix = lzwImageData.prefix;
  // var suffix = lzwImageData.suffix;
  var pixelStack = new Uint8Array(MaxStack+1)

  //  Initialize GIF data stream decoder.
  var clear = 1 << data_size;
  var end_of_information = clear + 1;
  var available = clear + 2;
  old_code = NullCode;
  var code_size = data_size + 1;
  var code_mask = (1 << code_size) - 1;
  for (code = 0; code < clear; code++){
    prefix[code] = 0;
    suffix[code] = code;
  }

  var bits = 0; var datum = 0;
  var dvcursor = blockinfo.start;
  var blockEnds = blockinfo.blockEnds;
  var blockEnd = blockEnds.shift();

  //  Decode GIF pixel stream.
  for (var i = 0; i < npix;){
    if (top === 0){

      if (bits < code_size){

        datum += u8buf[dvcursor] << bits;

        bits += 8;

        dvcursor++;
        if (dvcursor === blockEnd){
          dvcursor += 1;
          blockEnd = blockEnds.shift();
        }

        continue;

      }
      code = datum & code_mask;
      datum >>= code_size;
      bits -= code_size;

      if (code > available) {
        console.log(":(");
      //  console.log(num2bin(code));
      //  break;
      } // if we get here something bad happened ;(
      if (code === end_of_information) { console.log("fuck"); break};
      if (code === clear) {
        code_size = data_size + 1;
        code_mask = (1 << code_size) - 1;
        available = clear + 2;
        old_code = NullCode;
        continue;
      }
      if (old_code === NullCode){
        pixelStack[top++] = suffix[code];
        old_code = code;
        first = code;
        continue;
      }
      in_code = code;
      if (code === available){
        pixelStack[top++] = first;
        code = old_code;
      }
      while (code > clear){
        pixelStack[top++] = suffix[code];
        code = prefix[code];
      }
      first = (suffix[code]) // & 0xff;
                              // ^^ timb: not needed?
      //  Add a new string to the string table,
      if (available >= MaxStack) { /*console.log("maxstack!");*/ /*break;*/ }
      pixelStack[top++] = first;
      prefix[available] = old_code;
      suffix[available] = first;
      available++;
      /* why does == have higher precedence than & ?
         i just looked it up and i think it is because javascript
         has basically the same precedence as C and 
         "once upon a time, C didn't have the logical operators && and ||, 
         and the bitwise operators & and | did double duty."
      */
      if ((available & code_mask) === 0 && available < MaxStack){
        code_size++;
        code_mask += available;
      }
      old_code = in_code;
    }

    top--;
    if (paletteRemap)
      pixels[pi++] = paletteRemap[pixelStack[top]];
    else
      pixels[pi++] = pixelStack[top];
    i++;
  }

  // not needed: typed arrays init'd to 0 already
  // for (i = pi, len=pixels.length; i < len; i++) 
        // pixels[i] = 0; // clear missing pixels


  return pixels
};

var MaxStack = 4096;
// lzwImageData.prefix = new Uint16Array(MaxStack*2)
// lzwImageData.suffix = new Uint8Array(MaxStack)

module.exports = lzwImageData;
},{}]},{},[1]);