@socketsecurity/cli-with-sentry 0.14.93 → 0.14.95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/constants.js +3 -3
  2. package/dist/constants.js.map +1 -1
  3. package/dist/instrument-with-sentry.js +2 -2
  4. package/dist/instrument-with-sentry.js.map +1 -1
  5. package/dist/module-sync/cli.js +177 -157
  6. package/dist/module-sync/cli.js.map +1 -1
  7. package/dist/module-sync/shadow-bin.js +3 -14
  8. package/dist/module-sync/shadow-bin.js.map +1 -1
  9. package/dist/module-sync/shadow-npm-inject.js +68 -59
  10. package/dist/module-sync/shadow-npm-inject.js.map +1 -1
  11. package/dist/module-sync/shadow-npm-paths.js +16 -29
  12. package/dist/module-sync/shadow-npm-paths.js.map +1 -1
  13. package/dist/module-sync/vendor.d.ts +0 -0
  14. package/dist/module-sync/vendor.js +85829 -12598
  15. package/dist/module-sync/vendor.js.map +1 -1
  16. package/dist/require/cli.js +159 -138
  17. package/dist/require/cli.js.map +1 -1
  18. package/dist/require/shadow-bin.d.ts +5 -0
  19. package/dist/require/shadow-bin.js +108 -1
  20. package/dist/require/shadow-bin.js.map +1 -0
  21. package/dist/require/shadow-npm-inject.d.ts +1 -0
  22. package/dist/require/shadow-npm-inject.js +2335 -1
  23. package/dist/require/shadow-npm-inject.js.map +1 -0
  24. package/dist/require/shadow-npm-paths.d.ts +29 -0
  25. package/dist/require/shadow-npm-paths.js +454 -1
  26. package/dist/require/shadow-npm-paths.js.map +1 -0
  27. package/package.json +29 -29
  28. package/dist/blessed/lib/alias.js +0 -521
  29. package/dist/blessed/lib/blessed.js +0 -34
  30. package/dist/blessed/lib/colors.js +0 -492
  31. package/dist/blessed/lib/events.js +0 -197
  32. package/dist/blessed/lib/gpmclient.js +0 -247
  33. package/dist/blessed/lib/helpers.js +0 -172
  34. package/dist/blessed/lib/keys.js +0 -514
  35. package/dist/blessed/lib/program.js +0 -4532
  36. package/dist/blessed/lib/tput.js +0 -3113
  37. package/dist/blessed/lib/unicode.js +0 -914
  38. package/dist/blessed/lib/widget.js +0 -62
  39. package/dist/blessed/lib/widgets/ansiimage.js +0 -175
  40. package/dist/blessed/lib/widgets/bigtext.js +0 -172
  41. package/dist/blessed/lib/widgets/box.js +0 -36
  42. package/dist/blessed/lib/widgets/button.js +0 -64
  43. package/dist/blessed/lib/widgets/checkbox.js +0 -97
  44. package/dist/blessed/lib/widgets/element.js +0 -2873
  45. package/dist/blessed/lib/widgets/filemanager.js +0 -225
  46. package/dist/blessed/lib/widgets/form.js +0 -303
  47. package/dist/blessed/lib/widgets/image.js +0 -73
  48. package/dist/blessed/lib/widgets/input.js +0 -36
  49. package/dist/blessed/lib/widgets/layout.js +0 -251
  50. package/dist/blessed/lib/widgets/line.js +0 -61
  51. package/dist/blessed/lib/widgets/list.js +0 -654
  52. package/dist/blessed/lib/widgets/listbar.js +0 -454
  53. package/dist/blessed/lib/widgets/listtable.js +0 -267
  54. package/dist/blessed/lib/widgets/loading.js +0 -90
  55. package/dist/blessed/lib/widgets/log.js +0 -84
  56. package/dist/blessed/lib/widgets/message.js +0 -147
  57. package/dist/blessed/lib/widgets/node.js +0 -315
  58. package/dist/blessed/lib/widgets/overlayimage.js +0 -796
  59. package/dist/blessed/lib/widgets/progressbar.js +0 -168
  60. package/dist/blessed/lib/widgets/prompt.js +0 -129
  61. package/dist/blessed/lib/widgets/question.js +0 -131
  62. package/dist/blessed/lib/widgets/radiobutton.js +0 -64
  63. package/dist/blessed/lib/widgets/radioset.js +0 -38
  64. package/dist/blessed/lib/widgets/screen.js +0 -2487
  65. package/dist/blessed/lib/widgets/scrollablebox.js +0 -417
  66. package/dist/blessed/lib/widgets/scrollabletext.js +0 -37
  67. package/dist/blessed/lib/widgets/table.js +0 -385
  68. package/dist/blessed/lib/widgets/terminal.js +0 -454
  69. package/dist/blessed/lib/widgets/text.js +0 -37
  70. package/dist/blessed/lib/widgets/textarea.js +0 -378
  71. package/dist/blessed/lib/widgets/textbox.js +0 -81
  72. package/dist/blessed/lib/widgets/video.js +0 -132
  73. package/dist/blessed/usr/fonts/AUTHORS +0 -1
  74. package/dist/blessed/usr/fonts/LICENSE +0 -94
  75. package/dist/blessed/usr/fonts/README +0 -340
  76. package/dist/blessed/usr/fonts/ter-u14b.json +0 -17826
  77. package/dist/blessed/usr/fonts/ter-u14n.json +0 -17826
  78. package/dist/blessed/usr/linux +0 -0
  79. package/dist/blessed/usr/windows-ansi +0 -0
  80. package/dist/blessed/usr/xterm +0 -0
  81. package/dist/blessed/usr/xterm-256color +0 -0
  82. package/dist/blessed/usr/xterm.termcap +0 -243
  83. package/dist/blessed/usr/xterm.terminfo +0 -1977
  84. package/dist/blessed/vendor/tng.js +0 -1878
@@ -1,1878 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * tng.js - png reader
5
- * Copyright (c) 2015, Christopher Jeffrey (MIT License).
6
- * https://github.com/chjj/tng
7
- */
8
-
9
- const fs = require('node:fs'),
10
- util = require('node:util'),
11
- path = require('node:path'),
12
- zlib = require('node:zlib'),
13
- assert = require('node:assert'),
14
- cp = require('node:child_process'),
15
- exec = cp.execFileSync
16
-
17
- /**
18
- * PNG
19
- */
20
-
21
- function PNG(file, options) {
22
- let buf, chunks, idat, pixels
23
-
24
- if (!(this instanceof PNG)) {
25
- return new PNG(file, options)
26
- }
27
-
28
- if (!file) {
29
- throw new Error('no file')
30
- }
31
-
32
- this.options = options || {}
33
- this.colors = options.colors || require('blessed/lib/colors')
34
- this.optimization = this.options.optimization || 'mem'
35
- this.speed = this.options.speed || 1
36
-
37
- if (Buffer.isBuffer(file)) {
38
- this.file = this.options.filename || null
39
- buf = file
40
- } else {
41
- this.options.filename = file
42
- this.file = path.resolve(process.cwd(), file)
43
- buf = fs.readFileSync(this.file)
44
- }
45
-
46
- this.format =
47
- buf.readUInt32BE(0) === 0x89504e47
48
- ? 'png'
49
- : buf.slice(0, 3).toString('ascii') === 'GIF'
50
- ? 'gif'
51
- : buf.readUInt16BE(0) === 0xffd8
52
- ? 'jpg'
53
- : path.extname(this.file).slice(1).toLowerCase() || 'png'
54
-
55
- if (this.format !== 'png') {
56
- return this.toPNG(buf)
57
- }
58
-
59
- chunks = this.parseRaw(buf)
60
- idat = this.parseChunks(chunks)
61
- pixels = this.parseLines(idat)
62
-
63
- this.bmp = this.createBitmap(pixels)
64
- this.cellmap = this.createCellmap(this.bmp)
65
- this.frames = this.compileFrames(this.frames)
66
- }
67
-
68
- PNG.prototype.parseRaw = function (buf) {
69
- let chunks = [],
70
- index = 0,
71
- i = 0,
72
- len,
73
- type,
74
- name,
75
- data,
76
- crc,
77
- check,
78
- critical,
79
- public_,
80
- conforming,
81
- copysafe,
82
- pos
83
-
84
- this._debug(this.file)
85
-
86
- if (
87
- buf.readUInt32BE(0) !== 0x89504e47 ||
88
- buf.readUInt32BE(4) !== 0x0d0a1a0a
89
- ) {
90
- throw new Error('bad header')
91
- }
92
-
93
- i += 8
94
-
95
- while (i < buf.length) {
96
- try {
97
- len = buf.readUInt32BE(i)
98
- i += 4
99
- pos = i
100
- type = buf.slice(i, i + 4)
101
- name = type.toString('ascii')
102
- i += 4
103
- data = buf.slice(i, i + len)
104
- i += len
105
- check = this.crc32(buf.slice(pos, i))
106
- crc = buf.readInt32BE(i)
107
- i += 4
108
- critical = !!(~type[0] & 32)
109
- public_ = !!(~type[1] & 32)
110
- conforming = !!(~type[2] & 32)
111
- copysafe = !!(~type[3] & 32)
112
- if (crc !== check) {
113
- throw new Error(name + ': bad crc')
114
- }
115
- } catch (e) {
116
- if (this.options.debug) {
117
- throw e
118
- }
119
- break
120
- }
121
- chunks.push({
122
- index: index++,
123
- id: name.toLowerCase(),
124
- len: len,
125
- pos: pos,
126
- end: i,
127
- type: type,
128
- name: name,
129
- data: data,
130
- crc: crc,
131
- check: check,
132
- raw: buf.slice(pos, i),
133
- flags: {
134
- critical: critical,
135
- public_: public_,
136
- conforming: conforming,
137
- copysafe: copysafe
138
- }
139
- })
140
- }
141
-
142
- return chunks
143
- }
144
-
145
- PNG.prototype.parseChunks = function (chunks) {
146
- let i, chunk, name, data, p, idat, info
147
-
148
- for (i = 0; i < chunks.length; i++) {
149
- chunk = chunks[i]
150
- name = chunk.id
151
- data = chunk.data
152
- info = {}
153
- switch (name) {
154
- case 'ihdr': {
155
- this.width = info.width = data.readUInt32BE(0)
156
- this.height = info.height = data.readUInt32BE(4)
157
- this.bitDepth = info.bitDepth = data.readUInt8(8)
158
- this.colorType = info.colorType = data.readUInt8(9)
159
- this.compression = info.compression = data.readUInt8(10)
160
- this.filter = info.filter = data.readUInt8(11)
161
- this.interlace = info.interlace = data.readUInt8(12)
162
- switch (this.bitDepth) {
163
- case 1:
164
- case 2:
165
- case 4:
166
- case 8:
167
- case 16:
168
- case 24:
169
- case 32:
170
- break
171
- default:
172
- throw new Error('bad bit depth: ' + this.bitDepth)
173
- }
174
- switch (this.colorType) {
175
- case 0:
176
- case 2:
177
- case 3:
178
- case 4:
179
- case 6:
180
- break
181
- default:
182
- throw new Error('bad color: ' + this.colorType)
183
- }
184
- switch (this.compression) {
185
- case 0:
186
- break
187
- default:
188
- throw new Error('bad compression: ' + this.compression)
189
- }
190
- switch (this.filter) {
191
- case 0:
192
- case 1:
193
- case 2:
194
- case 3:
195
- case 4:
196
- break
197
- default:
198
- throw new Error('bad filter: ' + this.filter)
199
- }
200
- switch (this.interlace) {
201
- case 0:
202
- case 1:
203
- break
204
- default:
205
- throw new Error('bad interlace: ' + this.interlace)
206
- }
207
- break
208
- }
209
- case 'plte': {
210
- this.palette = info.palette = []
211
- for (p = 0; p < data.length; p += 3) {
212
- this.palette.push({
213
- r: data[p + 0],
214
- g: data[p + 1],
215
- b: data[p + 2],
216
- a: 255
217
- })
218
- }
219
- break
220
- }
221
- case 'idat': {
222
- this.size = this.size || 0
223
- this.size += data.length
224
- this.idat = this.idat || []
225
- this.idat.push(data)
226
- info.size = data.length
227
- break
228
- }
229
- case 'iend': {
230
- this.end = true
231
- break
232
- }
233
- case 'trns': {
234
- this.alpha = info.alpha = Array.prototype.slice.call(data)
235
- if (this.palette) {
236
- for (p = 0; p < data.length; p++) {
237
- if (!this.palette[p]) {
238
- break
239
- }
240
- this.palette[p].a = data[p]
241
- }
242
- }
243
- break
244
- }
245
- // https://wiki.mozilla.org/APNG_Specification
246
- case 'actl': {
247
- this.actl = info = {}
248
- this.frames = []
249
- this.actl.numFrames = data.readUInt32BE(0)
250
- this.actl.numPlays = data.readUInt32BE(4)
251
- break
252
- }
253
- case 'fctl': {
254
- // IDAT is the first frame depending on the order:
255
- // IDAT is a frame: acTL->fcTL->IDAT->[fcTL]->fdAT
256
- // IDAT is not a frame: acTL->IDAT->[fcTL]->fdAT
257
- if (!this.idat) {
258
- this.idat = []
259
- this.frames.push({
260
- idat: true,
261
- fctl: info,
262
- fdat: this.idat
263
- })
264
- } else {
265
- this.frames.push({
266
- fctl: info,
267
- fdat: []
268
- })
269
- }
270
- info.sequenceNumber = data.readUInt32BE(0)
271
- info.width = data.readUInt32BE(4)
272
- info.height = data.readUInt32BE(8)
273
- info.xOffset = data.readUInt32BE(12)
274
- info.yOffset = data.readUInt32BE(16)
275
- info.delayNum = data.readUInt16BE(20)
276
- info.delayDen = data.readUInt16BE(22)
277
- info.disposeOp = data.readUInt8(24)
278
- info.blendOp = data.readUInt8(25)
279
- break
280
- }
281
- case 'fdat': {
282
- info.sequenceNumber = data.readUInt32BE(0)
283
- info.data = data.slice(4)
284
- this.frames[this.frames.length - 1].fdat.push(info.data)
285
- break
286
- }
287
- }
288
- chunk.info = info
289
- }
290
-
291
- this._debug(chunks)
292
-
293
- if (this.frames) {
294
- this.frames = this.frames.map(function (frame, i) {
295
- frame.fdat = this.decompress(frame.fdat)
296
- if (!frame.fdat.length) {
297
- throw new Error('no data')
298
- }
299
- return frame
300
- }, this)
301
- }
302
-
303
- idat = this.decompress(this.idat)
304
- if (!idat.length) {
305
- throw new Error('no data')
306
- }
307
-
308
- return idat
309
- }
310
-
311
- PNG.prototype.parseLines = function (data) {
312
- let pixels = [],
313
- x,
314
- p,
315
- prior,
316
- line,
317
- filter,
318
- samples,
319
- pendingSamples,
320
- ch,
321
- shiftStart,
322
- i,
323
- toShift,
324
- sample
325
-
326
- this.sampleDepth =
327
- this.colorType === 0
328
- ? 1
329
- : this.colorType === 2
330
- ? 3
331
- : this.colorType === 3
332
- ? 1
333
- : this.colorType === 4
334
- ? 2
335
- : this.colorType === 6
336
- ? 4
337
- : 1
338
- this.bitsPerPixel = this.bitDepth * this.sampleDepth
339
- this.bytesPerPixel = Math.ceil(this.bitsPerPixel / 8)
340
- this.wastedBits =
341
- (this.width * this.bitsPerPixel) / 8 -
342
- (((this.width * this.bitsPerPixel) / 8) | 0)
343
- this.byteWidth = Math.ceil(this.width * (this.bitsPerPixel / 8))
344
-
345
- this.shiftStart =
346
- (this.bitDepth + (8 / this.bitDepth - this.bitDepth) - 1) | 0
347
- this.shiftMult = this.bitDepth >= 8 ? 0 : this.bitDepth
348
- this.mask = this.bitDepth === 32 ? 0xffffffff : (1 << this.bitDepth) - 1
349
-
350
- if (this.interlace === 1) {
351
- samples = this.sampleInterlacedLines(data)
352
- for (i = 0; i < samples.length; i += this.sampleDepth) {
353
- pixels.push(samples.slice(i, i + this.sampleDepth))
354
- }
355
- return pixels
356
- }
357
-
358
- for (p = 0; p < data.length; p += this.byteWidth) {
359
- prior = line || []
360
- filter = data[p++]
361
- line = data.slice(p, p + this.byteWidth)
362
- line = this.unfilterLine(filter, line, prior)
363
- samples = this.sampleLine(line)
364
- for (i = 0; i < samples.length; i += this.sampleDepth) {
365
- pixels.push(samples.slice(i, i + this.sampleDepth))
366
- }
367
- }
368
-
369
- return pixels
370
- }
371
-
372
- PNG.prototype.unfilterLine = function (filter, line, prior) {
373
- for (let x = 0; x < line.length; x++) {
374
- if (filter === 0) {
375
- break
376
- } else if (filter === 1) {
377
- line[x] = this.filters.sub(x, line, prior, this.bytesPerPixel)
378
- } else if (filter === 2) {
379
- line[x] = this.filters.up(x, line, prior, this.bytesPerPixel)
380
- } else if (filter === 3) {
381
- line[x] = this.filters.average(x, line, prior, this.bytesPerPixel)
382
- } else if (filter === 4) {
383
- line[x] = this.filters.paeth(x, line, prior, this.bytesPerPixel)
384
- }
385
- }
386
- return line
387
- }
388
-
389
- PNG.prototype.sampleLine = function (line, width) {
390
- let samples = [],
391
- x = 0,
392
- pendingSamples,
393
- ch,
394
- i,
395
- sample,
396
- shiftStart,
397
- toShift
398
-
399
- while (x < line.length) {
400
- pendingSamples = this.sampleDepth
401
- while (pendingSamples--) {
402
- ch = line[x]
403
- if (this.bitDepth === 16) {
404
- ch = (ch << 8) | line[++x]
405
- } else if (this.bitDepth === 24) {
406
- ch = (ch << 16) | (line[++x] << 8) | line[++x]
407
- } else if (this.bitDepth === 32) {
408
- ch = (ch << 24) | (line[++x] << 16) | (line[++x] << 8) | line[++x]
409
- } else if (this.bitDepth > 32) {
410
- throw new Error('bitDepth ' + this.bitDepth + ' unsupported.')
411
- }
412
- shiftStart = this.shiftStart
413
- toShift = shiftStart - (x === line.length - 1 ? this.wastedBits : 0)
414
- for (i = 0; i <= toShift; i++) {
415
- sample = (ch >> (this.shiftMult * shiftStart)) & this.mask
416
- if (this.colorType !== 3) {
417
- if (this.bitDepth < 8) {
418
- // <= 8 would work too, doesn't matter
419
- // sample = sample * (0xff / this.mask) | 0; // would work too
420
- sample *= 0xff / this.mask
421
- sample |= 0
422
- } else if (this.bitDepth > 8) {
423
- sample = ((sample / this.mask) * 255) | 0
424
- }
425
- }
426
- samples.push(sample)
427
- shiftStart--
428
- }
429
- x++
430
- }
431
- }
432
-
433
- // Needed for deinterlacing?
434
- if (width != null) {
435
- samples = samples.slice(0, width * this.sampleDepth)
436
- }
437
-
438
- return samples
439
- }
440
-
441
- // http://www.w3.org/TR/PNG-Filters.html
442
- PNG.prototype.filters = {
443
- sub: function Sub(x, line, prior, bpp) {
444
- if (x < bpp) {
445
- return line[x]
446
- }
447
- return (line[x] + line[x - bpp]) % 256
448
- },
449
- up: function Up(x, line, prior, bpp) {
450
- return (line[x] + (prior[x] || 0)) % 256
451
- },
452
- average: function Average(x, line, prior, bpp) {
453
- if (x < bpp) {
454
- return Math.floor((prior[x] || 0) / 2)
455
- }
456
- // if (x < bpp) return (prior[x] || 0) >> 1;
457
- return (
458
- (line[x] + Math.floor((line[x - bpp] + prior[x]) / 2)) %
459
- // + ((line[x - bpp] + prior[x]) >> 1)
460
- 256
461
- )
462
- },
463
- paeth: function Paeth(x, line, prior, bpp) {
464
- if (x < bpp) {
465
- return prior[x] || 0
466
- }
467
- return (
468
- (line[x] +
469
- this._predictor(line[x - bpp], prior[x] || 0, prior[x - bpp] || 0)) %
470
- 256
471
- )
472
- },
473
- _predictor: function PaethPredictor(a, b, c) {
474
- // a = left, b = above, c = upper left
475
- const p = a + b - c,
476
- pa = Math.abs(p - a),
477
- pb = Math.abs(p - b),
478
- pc = Math.abs(p - c)
479
- if (pa <= pb && pa <= pc) {
480
- return a
481
- }
482
- if (pb <= pc) {
483
- return b
484
- }
485
- return c
486
- }
487
- }
488
-
489
- /**
490
- * Adam7 deinterlacing ported to javascript from PyPNG:
491
- * pypng - Pure Python library for PNG image encoding/decoding
492
- * Copyright (c) 2009-2015, David Jones (MIT License).
493
- * https://github.com/drj11/pypng
494
- *
495
- * Permission is hereby granted, free of charge, to any person
496
- * obtaining a copy of this software and associated documentation files
497
- * (the "Software"), to deal in the Software without restriction,
498
- * including without limitation the rights to use, copy, modify, merge,
499
- * publish, distribute, sublicense, and/or sell copies of the Software,
500
- * and to permit persons to whom the Software is furnished to do so,
501
- * subject to the following conditions:
502
- *
503
- * The above copyright notice and this permission notice shall be
504
- * included in all copies or substantial portions of the Software.
505
- *
506
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
507
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
508
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
509
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
510
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
511
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
512
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
513
- * SOFTWARE.
514
- */
515
-
516
- PNG.prototype.sampleInterlacedLines = function (raw) {
517
- let psize,
518
- vpr,
519
- samples,
520
- source_offset,
521
- i,
522
- pass,
523
- xstart,
524
- ystart,
525
- xstep,
526
- ystep,
527
- recon,
528
- ppr,
529
- row_size,
530
- y,
531
- filter_type,
532
- scanline,
533
- flat,
534
- offset,
535
- end_offset,
536
- skip,
537
- j,
538
- k,
539
- f
540
-
541
- const adam7 = [
542
- [0, 0, 8, 8],
543
- [4, 0, 8, 8],
544
- [0, 4, 4, 8],
545
- [2, 0, 4, 4],
546
- [0, 2, 2, 4],
547
- [1, 0, 2, 2],
548
- [0, 1, 1, 2]
549
- ]
550
-
551
- // Fractional bytes per pixel
552
- psize = (this.bitDepth / 8) * this.sampleDepth
553
-
554
- // Values per row (of the target image)
555
- vpr = this.width * this.sampleDepth
556
-
557
- // Make a result array, and make it big enough. Interleaving
558
- // writes to the output array randomly (well, not quite), so the
559
- // entire output array must be in memory.
560
- samples = new Buffer(vpr * this.height)
561
- samples.fill(0)
562
-
563
- source_offset = 0
564
-
565
- for (i = 0; i < adam7.length; i++) {
566
- pass = adam7[i]
567
- xstart = pass[0]
568
- ystart = pass[1]
569
- xstep = pass[2]
570
- ystep = pass[3]
571
- if (xstart >= this.width) {
572
- continue
573
- }
574
- // The previous (reconstructed) scanline. Empty array at the
575
- // beginning of a pass to indicate that there is no previous
576
- // line.
577
- recon = []
578
- // Pixels per row (reduced pass image)
579
- ppr = Math.ceil((this.width - xstart) / xstep)
580
- // Row size in bytes for this pass.
581
- row_size = Math.ceil(psize * ppr)
582
- for (y = ystart; y < this.height; y += ystep) {
583
- filter_type = raw[source_offset]
584
- source_offset += 1
585
- scanline = raw.slice(source_offset, source_offset + row_size)
586
- source_offset += row_size
587
- recon = this.unfilterLine(filter_type, scanline, recon)
588
- // Convert so that there is one element per pixel value
589
- flat = this.sampleLine(recon, ppr)
590
- if (xstep === 1) {
591
- assert.equal(xstart, 0)
592
- offset = y * vpr
593
- for (k = offset, f = 0; k < offset + vpr; k++, f++) {
594
- samples[k] = flat[f]
595
- }
596
- } else {
597
- offset = y * vpr + xstart * this.sampleDepth
598
- end_offset = (y + 1) * vpr
599
- skip = this.sampleDepth * xstep
600
- for (j = 0; j < this.sampleDepth; j++) {
601
- for (
602
- k = offset + j, f = j;
603
- k < end_offset;
604
- k += skip, f += this.sampleDepth
605
- ) {
606
- samples[k] = flat[f]
607
- }
608
- }
609
- }
610
- }
611
- }
612
-
613
- return samples
614
- }
615
-
616
- PNG.prototype.createBitmap = function (pixels) {
617
- let bmp = [],
618
- i
619
-
620
- if (this.colorType === 0) {
621
- pixels = pixels.map(function (sample) {
622
- return { r: sample[0], g: sample[0], b: sample[0], a: 255 }
623
- })
624
- } else if (this.colorType === 2) {
625
- pixels = pixels.map(function (sample) {
626
- return { r: sample[0], g: sample[1], b: sample[2], a: 255 }
627
- })
628
- } else if (this.colorType === 3) {
629
- pixels = pixels.map(function (sample) {
630
- if (!this.palette[sample[0]]) {
631
- throw new Error('bad palette index')
632
- }
633
- return this.palette[sample[0]]
634
- }, this)
635
- } else if (this.colorType === 4) {
636
- pixels = pixels.map(function (sample) {
637
- return { r: sample[0], g: sample[0], b: sample[0], a: sample[1] }
638
- })
639
- } else if (this.colorType === 6) {
640
- pixels = pixels.map(function (sample) {
641
- return { r: sample[0], g: sample[1], b: sample[2], a: sample[3] }
642
- })
643
- }
644
-
645
- for (i = 0; i < pixels.length; i += this.width) {
646
- bmp.push(pixels.slice(i, i + this.width))
647
- }
648
-
649
- return bmp
650
- }
651
-
652
- PNG.prototype.createCellmap = function (bmp, options) {
653
- bmp = bmp || this.bmp
654
- options = options || this.options
655
- let cellmap = [],
656
- scale = options.scale || 0.2,
657
- height = bmp.length,
658
- width = bmp[0].length,
659
- cmwidth = options.width,
660
- cmheight = options.height,
661
- line,
662
- x,
663
- y,
664
- xx,
665
- yy,
666
- xs,
667
- ys
668
-
669
- if (cmwidth) {
670
- scale = cmwidth / width
671
- } else if (cmheight) {
672
- scale = cmheight / height
673
- }
674
-
675
- if (!cmheight) {
676
- cmheight = Math.round(height * scale)
677
- }
678
-
679
- if (!cmwidth) {
680
- cmwidth = Math.round(width * scale)
681
- }
682
-
683
- ys = height / cmheight
684
- xs = width / cmwidth
685
-
686
- for (y = 0; y < bmp.length; y += ys) {
687
- line = []
688
- yy = Math.round(y)
689
- if (!bmp[yy]) {
690
- break
691
- }
692
- for (x = 0; x < bmp[yy].length; x += xs) {
693
- xx = Math.round(x)
694
- if (!bmp[yy][xx]) {
695
- break
696
- }
697
- line.push(bmp[yy][xx])
698
- }
699
- cellmap.push(line)
700
- }
701
-
702
- return cellmap
703
- }
704
-
705
- PNG.prototype.renderANSI = function (bmp) {
706
- let self = this,
707
- out = ''
708
-
709
- bmp.forEach(function (line, y) {
710
- line.forEach(function (pixel, x) {
711
- const outch = self.getOutch(x, y, line, pixel)
712
- out += self.pixelToSGR(pixel, outch)
713
- })
714
- out += '\n'
715
- })
716
-
717
- return out
718
- }
719
-
720
- PNG.prototype.renderContent = function (bmp, el) {
721
- let self = this,
722
- out = ''
723
-
724
- bmp.forEach(function (line, y) {
725
- line.forEach(function (pixel, x) {
726
- const outch = self.getOutch(x, y, line, pixel)
727
- out += self.pixelToTags(pixel, outch)
728
- })
729
- out += '\n'
730
- })
731
-
732
- el.setContent(out)
733
-
734
- return out
735
- }
736
-
737
- PNG.prototype.renderScreen = function (bmp, screen, xi, xl, yi, yl) {
738
- let self = this,
739
- lines = screen.lines,
740
- cellLines,
741
- y,
742
- yy,
743
- x,
744
- xx,
745
- alpha,
746
- attr,
747
- ch
748
-
749
- cellLines = bmp.reduce(function (cellLines, line, y) {
750
- const cellLine = []
751
- line.forEach(function (pixel, x) {
752
- const outch = self.getOutch(x, y, line, pixel),
753
- cell = self.pixelToCell(pixel, outch)
754
- cellLine.push(cell)
755
- })
756
- cellLines.push(cellLine)
757
- return cellLines
758
- }, [])
759
-
760
- for (y = yi; y < yl; y++) {
761
- yy = y - yi
762
- for (x = xi; x < xl; x++) {
763
- xx = x - xi
764
- if (lines[y] && lines[y][x] && cellLines[yy] && cellLines[yy][xx]) {
765
- alpha = cellLines[yy][xx].pop()
766
- // completely transparent
767
- if (alpha === 0.0) {
768
- continue
769
- }
770
- // translucency / blending
771
- if (alpha < 1.0) {
772
- attr = cellLines[yy][xx][0]
773
- ch = cellLines[yy][xx][1]
774
- lines[y][x][0] = this.colors.blend(lines[y][x][0], attr, alpha)
775
- if (ch !== ' ') {
776
- lines[y][x][1] = ch
777
- }
778
- lines[y].dirty = true
779
- continue
780
- }
781
- // completely opaque
782
- lines[y][x] = cellLines[yy][xx]
783
- lines[y].dirty = true
784
- }
785
- }
786
- }
787
- }
788
-
789
- PNG.prototype.renderElement = function (bmp, el) {
790
- const xi = el.aleft + el.ileft,
791
- xl = el.aleft + el.width - el.iright,
792
- yi = el.atop + el.itop,
793
- yl = el.atop + el.height - el.ibottom
794
-
795
- return this.renderScreen(bmp, el.screen, xi, xl, yi, yl)
796
- }
797
-
798
- PNG.prototype.pixelToSGR = function (pixel, ch) {
799
- let bga = 1.0,
800
- fga = 0.5,
801
- a = pixel.a / 255,
802
- bg,
803
- fg
804
-
805
- bg = this.colors.match(
806
- (pixel.r * a * bga) | 0,
807
- (pixel.g * a * bga) | 0,
808
- (pixel.b * a * bga) | 0
809
- )
810
-
811
- if (ch && this.options.ascii) {
812
- fg = this.colors.match(
813
- (pixel.r * a * fga) | 0,
814
- (pixel.g * a * fga) | 0,
815
- (pixel.b * a * fga) | 0
816
- )
817
- if (a === 0) {
818
- return '\x1b[38;5;' + fg + 'm' + ch + '\x1b[m'
819
- }
820
- return '\x1b[38;5;' + fg + 'm\x1b[48;5;' + bg + 'm' + ch + '\x1b[m'
821
- }
822
-
823
- if (a === 0) {
824
- return ' '
825
- }
826
-
827
- return '\x1b[48;5;' + bg + 'm \x1b[m'
828
- }
829
-
830
- PNG.prototype.pixelToTags = function (pixel, ch) {
831
- let bga = 1.0,
832
- fga = 0.5,
833
- a = pixel.a / 255,
834
- bg,
835
- fg
836
-
837
- bg = this.colors.RGBtoHex(
838
- (pixel.r * a * bga) | 0,
839
- (pixel.g * a * bga) | 0,
840
- (pixel.b * a * bga) | 0
841
- )
842
-
843
- if (ch && this.options.ascii) {
844
- fg = this.colors.RGBtoHex(
845
- (pixel.r * a * fga) | 0,
846
- (pixel.g * a * fga) | 0,
847
- (pixel.b * a * fga) | 0
848
- )
849
- if (a === 0) {
850
- return '{' + fg + '-fg}' + ch + '{/}'
851
- }
852
- return '{' + fg + '-fg}{' + bg + '-bg}' + ch + '{/}'
853
- }
854
-
855
- if (a === 0) {
856
- return ' '
857
- }
858
-
859
- return '{' + bg + '-bg} {/' + bg + '-bg}'
860
- }
861
-
862
- PNG.prototype.pixelToCell = function (pixel, ch) {
863
- let bga = 1.0,
864
- fga = 0.5,
865
- a = pixel.a / 255,
866
- bg,
867
- fg
868
-
869
- bg = this.colors.match(
870
- (pixel.r * bga) | 0,
871
- (pixel.g * bga) | 0,
872
- (pixel.b * bga) | 0
873
- )
874
-
875
- if (ch && this.options.ascii) {
876
- fg = this.colors.match(
877
- (pixel.r * fga) | 0,
878
- (pixel.g * fga) | 0,
879
- (pixel.b * fga) | 0
880
- )
881
- } else {
882
- fg = 0x1ff
883
- ch = null
884
- }
885
-
886
- // if (a === 0) bg = 0x1ff;
887
-
888
- return [(0 << 18) | (fg << 9) | (bg << 0), ch || ' ', a]
889
- }
890
-
891
- // Taken from libcaca:
892
- PNG.prototype.getOutch = (function () {
893
- const dchars = "????8@8@#8@8##8#MKXWwz$&%x><\\/xo;+=|^-:i'.`, `. "
894
-
895
- const luminance = function (pixel) {
896
- const a = pixel.a / 255,
897
- r = pixel.r * a,
898
- g = pixel.g * a,
899
- b = pixel.b * a,
900
- l = 0.2126 * r + 0.7152 * g + 0.0722 * b
901
-
902
- return l / 255
903
- }
904
-
905
- return function (x, y, line, pixel) {
906
- const lumi = luminance(pixel),
907
- outch = dchars[(lumi * (dchars.length - 1)) | 0]
908
-
909
- return outch
910
- }
911
- })()
912
-
913
- PNG.prototype.compileFrames = function (frames) {
914
- return this.optimization === 'mem'
915
- ? this.compileFrames_lomem(frames)
916
- : this.compileFrames_locpu(frames)
917
- }
918
-
919
- PNG.prototype.compileFrames_lomem = function (frames) {
920
- if (!this.actl) {
921
- return
922
- }
923
- return frames.map(function (frame, i) {
924
- this.width = frame.fctl.width
925
- this.height = frame.fctl.height
926
-
927
- const pixels = frame._pixels || this.parseLines(frame.fdat),
928
- bmp = frame._bmp || this.createBitmap(pixels),
929
- fc = frame.fctl
930
-
931
- return {
932
- actl: this.actl,
933
- fctl: frame.fctl,
934
- delay: ((fc.delayNum / (fc.delayDen || 100)) * 1000) | 0,
935
- bmp: bmp
936
- }
937
- }, this)
938
- }
939
-
940
- PNG.prototype.compileFrames_locpu = function (frames) {
941
- if (!this.actl) {
942
- return
943
- }
944
-
945
- this._curBmp = null
946
- this._lastBmp = null
947
-
948
- return frames.map(function (frame, i) {
949
- this.width = frame.fctl.width
950
- this.height = frame.fctl.height
951
-
952
- const pixels = frame._pixels || this.parseLines(frame.fdat),
953
- bmp = frame._bmp || this.createBitmap(pixels),
954
- renderBmp = this.renderFrame(bmp, frame, i),
955
- cellmap = this.createCellmap(renderBmp),
956
- fc = frame.fctl
957
-
958
- return {
959
- actl: this.actl,
960
- fctl: frame.fctl,
961
- delay: ((fc.delayNum / (fc.delayDen || 100)) * 1000) | 0,
962
- bmp: renderBmp,
963
- cellmap: cellmap
964
- }
965
- }, this)
966
- }
967
-
968
- PNG.prototype.renderFrame = function (bmp, frame, i) {
969
- let first = this.frames[0],
970
- last = this.frames[i - 1],
971
- fc = frame.fctl,
972
- xo = fc.xOffset,
973
- yo = fc.yOffset,
974
- lxo,
975
- lyo,
976
- x,
977
- y,
978
- line,
979
- p
980
-
981
- if (!this._curBmp) {
982
- this._curBmp = []
983
- for (y = 0; y < first.fctl.height; y++) {
984
- line = []
985
- for (x = 0; x < first.fctl.width; x++) {
986
- p = bmp[y][x]
987
- line.push({ r: p.r, g: p.g, b: p.b, a: p.a })
988
- }
989
- this._curBmp.push(line)
990
- }
991
- }
992
-
993
- if (last && last.fctl.disposeOp !== 0) {
994
- lxo = last.fctl.xOffset
995
- lyo = last.fctl.yOffset
996
- for (y = 0; y < last.fctl.height; y++) {
997
- for (x = 0; x < last.fctl.width; x++) {
998
- if (last.fctl.disposeOp === 0) {
999
- // none / keep
1000
- } else if (last.fctl.disposeOp === 1) {
1001
- // background / clear
1002
- this._curBmp[lyo + y][lxo + x] = { r: 0, g: 0, b: 0, a: 0 }
1003
- } else if (last.fctl.disposeOp === 2) {
1004
- // previous / restore
1005
- p = this._lastBmp[y][x]
1006
- this._curBmp[lyo + y][lxo + x] = { r: p.r, g: p.g, b: p.b, a: p.a }
1007
- }
1008
- }
1009
- }
1010
- }
1011
-
1012
- if (frame.fctl.disposeOp === 2) {
1013
- this._lastBmp = []
1014
- for (y = 0; y < frame.fctl.height; y++) {
1015
- line = []
1016
- for (x = 0; x < frame.fctl.width; x++) {
1017
- p = this._curBmp[yo + y][xo + x]
1018
- line.push({ r: p.r, g: p.g, b: p.b, a: p.a })
1019
- }
1020
- this._lastBmp.push(line)
1021
- }
1022
- } else {
1023
- this._lastBmp = null
1024
- }
1025
-
1026
- for (y = 0; y < frame.fctl.height; y++) {
1027
- for (x = 0; x < frame.fctl.width; x++) {
1028
- p = bmp[y][x]
1029
- if (fc.blendOp === 0) {
1030
- // source
1031
- this._curBmp[yo + y][xo + x] = { r: p.r, g: p.g, b: p.b, a: p.a }
1032
- } else if (fc.blendOp === 1) {
1033
- // over
1034
- if (p.a !== 0) {
1035
- this._curBmp[yo + y][xo + x] = { r: p.r, g: p.g, b: p.b, a: p.a }
1036
- }
1037
- }
1038
- }
1039
- }
1040
-
1041
- return this._curBmp
1042
- }
1043
-
1044
- PNG.prototype._animate = function (callback) {
1045
- if (!this.frames) {
1046
- return callback(this.bmp, this.cellmap)
1047
- }
1048
-
1049
- let self = this,
1050
- numPlays = this.actl.numPlays || Infinity,
1051
- running = 0,
1052
- i = -1
1053
-
1054
- this._curBmp = null
1055
- this._lastBmp = null
1056
-
1057
- const next_lomem = function () {
1058
- if (!running) {
1059
- return
1060
- }
1061
-
1062
- const frame = self.frames[++i]
1063
- if (!frame) {
1064
- if (!--numPlays) {
1065
- return callback()
1066
- }
1067
- i = -1
1068
- // XXX may be able to optimize by only setting the self._curBmp once???
1069
- self._curBmp = null
1070
- self._lastBmp = null
1071
- return setImmediate(next)
1072
- }
1073
-
1074
- const bmp = frame.bmp,
1075
- renderBmp = self.renderFrame(bmp, frame, i),
1076
- cellmap = self.createCellmap(renderBmp)
1077
-
1078
- callback(renderBmp, cellmap)
1079
- return setTimeout(next, (frame.delay / self.speed) | 0)
1080
- }
1081
-
1082
- const next_locpu = function () {
1083
- if (!running) {
1084
- return
1085
- }
1086
- const frame = self.frames[++i]
1087
- if (!frame) {
1088
- if (!--numPlays) {
1089
- return callback()
1090
- }
1091
- i = -1
1092
- return setImmediate(next)
1093
- }
1094
- callback(frame.bmp, frame.cellmap)
1095
- return setTimeout(next, (frame.delay / self.speed) | 0)
1096
- }
1097
-
1098
- const next = this.optimization === 'mem' ? next_lomem : next_locpu
1099
-
1100
- this._control = function (state) {
1101
- if (state === -1) {
1102
- i = -1
1103
- self._curBmp = null
1104
- self._lastBmp = null
1105
- running = 0
1106
- callback(
1107
- self.frames[0].bmp,
1108
- self.frames[0].cellmap || self.createCellmap(self.frames[0].bmp)
1109
- )
1110
- return
1111
- }
1112
- if (state === running) {
1113
- return
1114
- }
1115
- running = state
1116
- return next()
1117
- }
1118
-
1119
- this._control(1)
1120
- }
1121
-
1122
- PNG.prototype.play = function (callback) {
1123
- if (!this._control || callback) {
1124
- this.stop()
1125
- return this._animate(callback)
1126
- }
1127
- this._control(1)
1128
- }
1129
-
1130
- PNG.prototype.pause = function () {
1131
- if (!this._control) {
1132
- return
1133
- }
1134
- this._control(0)
1135
- }
1136
-
1137
- PNG.prototype.stop = function () {
1138
- if (!this._control) {
1139
- return
1140
- }
1141
- this._control(-1)
1142
- }
1143
-
1144
- PNG.prototype.toPNG = function (input) {
1145
- let options = this.options,
1146
- file = this.file,
1147
- format = this.format,
1148
- buf,
1149
- img,
1150
- gif,
1151
- i,
1152
- control,
1153
- disposeOp
1154
-
1155
- if (format !== 'gif') {
1156
- buf = exec('convert', [format + ':-', 'png:-'], {
1157
- stdio: ['pipe', 'pipe', 'ignore'],
1158
- input: input
1159
- })
1160
- img = PNG(buf, options)
1161
- img.file = file
1162
- return img
1163
- }
1164
-
1165
- gif = GIF(input, options)
1166
-
1167
- this.width = gif.width
1168
- this.height = gif.height
1169
- this.frames = []
1170
-
1171
- for (i = 0; i < gif.images.length; i++) {
1172
- img = gif.images[i]
1173
- // Convert from gif disposal to png disposal. See:
1174
- // http://www.w3.org/Graphics/GIF/spec-gif89a.txt
1175
- control = img.control || gif
1176
- disposeOp = Math.max(0, (control.disposeMethod || 0) - 1)
1177
- if (disposeOp > 2) {
1178
- disposeOp = 0
1179
- }
1180
- this.frames.push({
1181
- fctl: {
1182
- sequenceNumber: i,
1183
- width: img.width,
1184
- height: img.height,
1185
- xOffset: img.left,
1186
- yOffset: img.top,
1187
- delayNum: control.delay,
1188
- delayDen: 100,
1189
- disposeOp: disposeOp,
1190
- blendOp: 1
1191
- },
1192
- fdat: [],
1193
- _pixels: [],
1194
- _bmp: img.bmp
1195
- })
1196
- }
1197
-
1198
- this.bmp = this.frames[0]._bmp
1199
- this.cellmap = this.createCellmap(this.bmp)
1200
-
1201
- if (this.frames.length > 1) {
1202
- this.actl = { numFrames: gif.images.length, numPlays: gif.numPlays || 0 }
1203
- this.frames = this.compileFrames(this.frames)
1204
- } else {
1205
- this.frames = undefined
1206
- }
1207
-
1208
- return this
1209
- }
1210
-
1211
- // Convert a gif to an apng using imagemagick. Unfortunately imagemagick
1212
- // doesn't support apngs, so we coalesce the gif frames into one image and then
1213
- // slice them into frames.
1214
- PNG.prototype.gifMagick = function (input) {
1215
- let options = this.options,
1216
- file = this.file,
1217
- format = this.format,
1218
- buf,
1219
- fmt,
1220
- img,
1221
- frames,
1222
- frame,
1223
- width,
1224
- height,
1225
- iwidth,
1226
- twidth,
1227
- i,
1228
- lines,
1229
- line,
1230
- x,
1231
- y
1232
-
1233
- buf = exec('convert', [format + ':-', '-coalesce', '+append', 'png:-'], {
1234
- stdio: ['pipe', 'pipe', 'ignore'],
1235
- input: input
1236
- })
1237
-
1238
- fmt = '{"W":%W,"H":%H,"w":%w,"h":%h,"d":%T,"x":"%X","y":"%Y"},'
1239
- frames = exec('identify', ['-format', fmt, format + ':-'], {
1240
- encoding: 'utf8',
1241
- stdio: ['pipe', 'pipe', 'ignore'],
1242
- input: input
1243
- })
1244
- frames = JSON.parse('[' + frames.trim().slice(0, -1) + ']')
1245
-
1246
- img = PNG(buf, options)
1247
- img.file = file
1248
- Object.keys(img).forEach(function (key) {
1249
- this[key] = img[key]
1250
- }, this)
1251
-
1252
- width = frames[0].W
1253
- height = frames[0].H
1254
- iwidth = 0
1255
- twidth = 0
1256
-
1257
- this.width = width
1258
- this.height = height
1259
-
1260
- this.frames = []
1261
-
1262
- for (i = 0; i < frames.length; i++) {
1263
- frame = frames[i]
1264
- frame.x = +frame.x
1265
- frame.y = +frame.y
1266
-
1267
- iwidth = twidth
1268
- twidth += width
1269
-
1270
- lines = []
1271
- for (y = frame.y; y < height; y++) {
1272
- line = []
1273
- for (x = iwidth + frame.x; x < twidth; x++) {
1274
- line.push(img.bmp[y][x])
1275
- }
1276
- lines.push(line)
1277
- }
1278
-
1279
- this.frames.push({
1280
- fctl: {
1281
- sequenceNumber: i,
1282
- width: frame.w,
1283
- height: frame.h,
1284
- xOffset: frame.x,
1285
- yOffset: frame.y,
1286
- delayNum: frame.d,
1287
- delayDen: 100,
1288
- disposeOp: 0,
1289
- blendOp: 0
1290
- },
1291
- fdat: [],
1292
- _pixels: [],
1293
- _bmp: lines
1294
- })
1295
- }
1296
-
1297
- this.bmp = this.frames[0]._bmp
1298
- this.cellmap = this.createCellmap(this.bmp)
1299
-
1300
- if (this.frames.length > 1) {
1301
- this.actl = { numFrames: frames.length, numPlays: 0 }
1302
- this.frames = this.compileFrames(this.frames)
1303
- } else {
1304
- this.frames = undefined
1305
- }
1306
-
1307
- return this
1308
- }
1309
-
1310
- PNG.prototype.decompress = function (buffers) {
1311
- return zlib.inflateSync(
1312
- new Buffer(
1313
- buffers.reduce(function (out, data) {
1314
- return out.concat(Array.prototype.slice.call(data))
1315
- }, [])
1316
- )
1317
- )
1318
- }
1319
-
1320
- /**
1321
- * node-crc
1322
- * https://github.com/alexgorbatchev/node-crc
1323
- * https://github.com/alexgorbatchev/node-crc/blob/master/LICENSE
1324
- *
1325
- * The MIT License (MIT)
1326
- *
1327
- * Copyright 2014 Alex Gorbatchev
1328
- *
1329
- * Permission is hereby granted, free of charge, to any person obtaining
1330
- * a copy of this software and associated documentation files (the
1331
- * "Software"), to deal in the Software without restriction, including
1332
- * without limitation the rights to use, copy, modify, merge, publish,
1333
- * distribute, sublicense, and/or sell copies of the Software, and to
1334
- * permit persons to whom the Software is furnished to do so, subject to
1335
- * the following conditions:
1336
- *
1337
- * The above copyright notice and this permission notice shall be
1338
- * included in all copies or substantial portions of the Software.
1339
- *
1340
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1341
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1342
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1343
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
1344
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
1345
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1346
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1347
- */
1348
-
1349
- PNG.prototype.crc32 = (function () {
1350
- const crcTable = [
1351
- 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
1352
- 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
1353
- 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
1354
- 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
1355
- 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
1356
- 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
1357
- 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
1358
- 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
1359
- 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
1360
- 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
1361
- 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
1362
- 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
1363
- 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
1364
- 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
1365
- 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
1366
- 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
1367
- 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
1368
- 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
1369
- 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
1370
- 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
1371
- 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
1372
- 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
1373
- 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
1374
- 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
1375
- 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
1376
- 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
1377
- 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
1378
- 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
1379
- 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
1380
- 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
1381
- 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
1382
- 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
1383
- 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
1384
- 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
1385
- 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
1386
- 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
1387
- 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
1388
- 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
1389
- 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
1390
- 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
1391
- 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
1392
- 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
1393
- 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
1394
- ]
1395
-
1396
- return function crc32(buf) {
1397
- //var crc = previous === 0 ? 0 : ~~previous ^ -1;
1398
- let crc = -1
1399
- for (let i = 0, len = buf.length; i < len; i++) {
1400
- crc = crcTable[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8)
1401
- }
1402
- return crc ^ -1
1403
- }
1404
- })()
1405
-
1406
- PNG.prototype._debug = function () {
1407
- if (!this.options.log) {
1408
- return
1409
- }
1410
- return this.options.log.apply(null, arguments)
1411
- }
1412
-
1413
- /**
1414
- * GIF
1415
- */
1416
-
1417
- function GIF(file, options) {
1418
- const self = this
1419
-
1420
- if (!(this instanceof GIF)) {
1421
- return new GIF(file, options)
1422
- }
1423
-
1424
- let info = {},
1425
- p = 0,
1426
- buf,
1427
- i,
1428
- total,
1429
- sig,
1430
- desc,
1431
- img,
1432
- ext,
1433
- label,
1434
- size
1435
-
1436
- if (!file) {
1437
- throw new Error('no file')
1438
- }
1439
-
1440
- options = options || {}
1441
-
1442
- this.options = options
1443
-
1444
- // XXX If the gif is not optimized enough
1445
- // it may OOM the process with too many frames.
1446
- // TODO: Implement in PNG reader.
1447
- this.pixelLimit = this.options.pixelLimit || 7622550
1448
- this.totalPixels = 0
1449
-
1450
- if (Buffer.isBuffer(file)) {
1451
- buf = file
1452
- file = null
1453
- } else {
1454
- file = path.resolve(process.cwd(), file)
1455
- buf = fs.readFileSync(file)
1456
- }
1457
-
1458
- sig = buf.slice(0, 6).toString('ascii')
1459
- if (sig !== 'GIF87a' && sig !== 'GIF89a') {
1460
- throw new Error('bad header: ' + sig)
1461
- }
1462
-
1463
- this.width = buf.readUInt16LE(6)
1464
- this.height = buf.readUInt16LE(8)
1465
-
1466
- this.flags = buf.readUInt8(10)
1467
- this.gct = !!(this.flags & 0x80)
1468
- this.gctsize = (this.flags & 0x07) + 1
1469
-
1470
- this.bgIndex = buf.readUInt8(11)
1471
- this.aspect = buf.readUInt8(12)
1472
- p += 13
1473
-
1474
- if (this.gct) {
1475
- this.colors = []
1476
- total = 1 << this.gctsize
1477
- for (i = 0; i < total; i++, p += 3) {
1478
- this.colors.push([buf[p], buf[p + 1], buf[p + 2], 255])
1479
- }
1480
- }
1481
-
1482
- this.images = []
1483
- this.extensions = []
1484
-
1485
- try {
1486
- while (p < buf.length) {
1487
- desc = buf.readUInt8(p)
1488
- p += 1
1489
- if (desc === 0x2c) {
1490
- img = {}
1491
-
1492
- img.left = buf.readUInt16LE(p)
1493
- p += 2
1494
- img.top = buf.readUInt16LE(p)
1495
- p += 2
1496
-
1497
- img.width = buf.readUInt16LE(p)
1498
- p += 2
1499
- img.height = buf.readUInt16LE(p)
1500
- p += 2
1501
-
1502
- img.flags = buf.readUInt8(p)
1503
- p += 1
1504
-
1505
- img.lct = !!(img.flags & 0x80)
1506
- img.ilace = !!(img.flags & 0x40)
1507
- img.lctsize = (img.flags & 0x07) + 1
1508
-
1509
- if (img.lct) {
1510
- img.lcolors = []
1511
- total = 1 << img.lctsize
1512
- for (i = 0; i < total; i++, p += 3) {
1513
- img.lcolors.push([buf[p], buf[p + 1], buf[p + 2], 255])
1514
- }
1515
- }
1516
-
1517
- img.codeSize = buf.readUInt8(p)
1518
- p += 1
1519
-
1520
- img.size = buf.readUInt8(p)
1521
- p += 1
1522
-
1523
- img.lzw = [buf.slice(p, p + img.size)]
1524
- p += img.size
1525
-
1526
- while (buf[p] !== 0x00) {
1527
- // Some gifs screw up their size.
1528
- // XXX Same for all subblocks?
1529
- if (buf[p] === 0x3b && p === buf.length - 1) {
1530
- p--
1531
- break
1532
- }
1533
- size = buf.readUInt8(p)
1534
- p += 1
1535
- img.lzw.push(buf.slice(p, p + size))
1536
- p += size
1537
- }
1538
-
1539
- assert.equal(buf.readUInt8(p), 0x00)
1540
- p += 1
1541
-
1542
- if (ext && ext.label === 0xf9) {
1543
- img.control = ext
1544
- }
1545
-
1546
- this.totalPixels += img.width * img.height
1547
-
1548
- this.images.push(img)
1549
-
1550
- if (this.totalPixels >= this.pixelLimit) {
1551
- break
1552
- }
1553
- } else if (desc === 0x21) {
1554
- // Extensions:
1555
- // http://www.w3.org/Graphics/GIF/spec-gif89a.txt
1556
- ext = {}
1557
- label = buf.readUInt8(p)
1558
- p += 1
1559
- ext.label = label
1560
- if (label === 0xf9) {
1561
- size = buf.readUInt8(p)
1562
- assert.equal(size, 0x04)
1563
- p += 1
1564
- ext.fields = buf.readUInt8(p)
1565
- ext.disposeMethod = (ext.fields >> 2) & 0x07
1566
- ext.useTransparent = !!(ext.fields & 0x01)
1567
- p += 1
1568
- ext.delay = buf.readUInt16LE(p)
1569
- p += 2
1570
- ext.transparentColor = buf.readUInt8(p)
1571
- p += 1
1572
- while (buf[p] !== 0x00) {
1573
- size = buf.readUInt8(p)
1574
- p += 1
1575
- p += size
1576
- }
1577
- assert.equal(buf.readUInt8(p), 0x00)
1578
- p += 1
1579
- this.delay = ext.delay
1580
- this.transparentColor = ext.transparentColor
1581
- this.disposeMethod = ext.disposeMethod
1582
- this.useTransparent = ext.useTransparent
1583
- } else if (label === 0xff) {
1584
- // https://wiki.whatwg.org/wiki/GIF#Specifications
1585
- size = buf.readUInt8(p)
1586
- p += 1
1587
- ext.id = buf.slice(p, p + 8).toString('ascii')
1588
- p += 8
1589
- ext.auth = buf.slice(p, p + 3).toString('ascii')
1590
- p += 3
1591
- ext.data = []
1592
- while (buf[p] !== 0x00) {
1593
- size = buf.readUInt8(p)
1594
- p += 1
1595
- ext.data.push(buf.slice(p, p + size))
1596
- p += size
1597
- }
1598
- ext.data = new Buffer(
1599
- ext.data.reduce(function (out, data) {
1600
- return out.concat(Array.prototype.slice.call(data))
1601
- }, [])
1602
- )
1603
- // AnimExts looping extension (identical to netscape)
1604
- if (ext.id === 'ANIMEXTS' && ext.auth === '1.0') {
1605
- ext.id = 'NETSCAPE'
1606
- ext.auth = '2.0'
1607
- ext.animexts = true
1608
- }
1609
- // Netscape extensions
1610
- if (ext.id === 'NETSCAPE' && ext.auth === '2.0') {
1611
- if (ext.data.readUInt8(0) === 0x01) {
1612
- // Netscape looping extension
1613
- // http://graphcomp.com/info/specs/ani_gif.html
1614
- ext.numPlays = ext.data.readUInt16LE(1)
1615
- this.numPlays = ext.numPlays
1616
- } else if (ext.data.readUInt8(0) === 0x02) {
1617
- // Netscape buffering extension
1618
- this.minBuffer = ext.data
1619
- }
1620
- }
1621
- // Adobe XMP extension
1622
- if (ext.id === 'XMP Data' && ext.auth === 'XMP') {
1623
- ext.xmp = ext.data.toString('utf8')
1624
- this.xmp = ext.xmp
1625
- }
1626
- // ICC extension
1627
- if (ext.id === 'ICCRGBG1' && ext.auth === '012') {
1628
- // NOTE: Says size is 4 bytes, not 1? Maybe just buffer size?
1629
- this.icc = ext.data
1630
- }
1631
- // fractint extension
1632
- if (ext.id === 'fractint' && /^00[1-7]$/.test(ext.auth)) {
1633
- // NOTE: Says size is 4 bytes, not 1? Maybe just buffer size?
1634
- // Size: '!\377\013' == [0x00, 0x15, 0xff, 0x0b]
1635
- this.fractint = ext.data
1636
- }
1637
- assert.equal(buf.readUInt8(p), 0x00)
1638
- p += 1
1639
- } else {
1640
- ext.data = []
1641
- while (buf[p] !== 0x00) {
1642
- size = buf.readUInt8(p)
1643
- p += 1
1644
- ext.data.push(buf.slice(p, p + size))
1645
- p += size
1646
- }
1647
- assert.equal(buf.readUInt8(p), 0x00)
1648
- p += 1
1649
- }
1650
- this.extensions.push(ext)
1651
- } else if (desc === 0x3b) {
1652
- break
1653
- } else if (p === buf.length - 1) {
1654
- // } else if (desc === 0x00 && p === buf.length - 1) {
1655
- break
1656
- } else {
1657
- throw new Error('unknown block')
1658
- }
1659
- }
1660
- } catch (e) {
1661
- if (options.debug) {
1662
- throw e
1663
- }
1664
- }
1665
-
1666
- this.images = this.images
1667
- .map(function (img, imageIndex) {
1668
- const control = img.control || this
1669
-
1670
- img.lzw = new Buffer(
1671
- img.lzw.reduce(function (out, data) {
1672
- return out.concat(Array.prototype.slice.call(data))
1673
- }, [])
1674
- )
1675
-
1676
- try {
1677
- img.data = this.decompress(img.lzw, img.codeSize)
1678
- } catch (e) {
1679
- if (options.debug) {
1680
- throw e
1681
- }
1682
- return
1683
- }
1684
-
1685
- const interlacing = [
1686
- [0, 8],
1687
- [4, 8],
1688
- [2, 4],
1689
- [1, 2],
1690
- [0, 0]
1691
- ]
1692
-
1693
- let table = img.lcolors || this.colors,
1694
- row = 0,
1695
- col = 0,
1696
- ilp = 0,
1697
- p = 0,
1698
- b,
1699
- idx,
1700
- i,
1701
- y,
1702
- x,
1703
- line,
1704
- pixel
1705
-
1706
- img.samples = []
1707
- // Rewritten version of:
1708
- // https://github.com/lbv/ka-cs-programs/blob/master/lib/gif-reader.js
1709
- for (;;) {
1710
- b = img.data[p++]
1711
- if (b == null) {
1712
- break
1713
- }
1714
- idx = (row * img.width + col) * 4
1715
- if (!table[b]) {
1716
- if (options.debug) {
1717
- throw new Error('bad samples')
1718
- }
1719
- table[b] = [0, 0, 0, 0]
1720
- }
1721
- img.samples[idx] = table[b][0]
1722
- img.samples[idx + 1] = table[b][1]
1723
- img.samples[idx + 2] = table[b][2]
1724
- img.samples[idx + 3] = table[b][3]
1725
- if (control.useTransparent && b === control.transparentColor) {
1726
- img.samples[idx + 3] = 0
1727
- }
1728
- if (++col >= img.width) {
1729
- col = 0
1730
- if (img.ilace) {
1731
- row += interlacing[ilp][1]
1732
- if (row >= img.height) {
1733
- row = interlacing[++ilp][0]
1734
- }
1735
- } else {
1736
- row++
1737
- }
1738
- }
1739
- }
1740
-
1741
- img.pixels = []
1742
- for (i = 0; i < img.samples.length; i += 4) {
1743
- img.pixels.push(img.samples.slice(i, i + 4))
1744
- }
1745
-
1746
- img.bmp = []
1747
- for (y = 0, p = 0; y < img.height; y++) {
1748
- line = []
1749
- for (x = 0; x < img.width; x++) {
1750
- pixel = img.pixels[p++]
1751
- if (!pixel) {
1752
- if (options.debug) {
1753
- throw new Error('no pixel')
1754
- }
1755
- line.push({ r: 0, g: 0, b: 0, a: 0 })
1756
- continue
1757
- }
1758
- line.push({ r: pixel[0], g: pixel[1], b: pixel[2], a: pixel[3] })
1759
- }
1760
- img.bmp.push(line)
1761
- }
1762
-
1763
- return img
1764
- }, this)
1765
- .filter(Boolean)
1766
-
1767
- if (!this.images.length) {
1768
- throw new Error('no image data or bad decompress')
1769
- }
1770
- }
1771
-
1772
- // Rewritten version of:
1773
- // https://github.com/lbv/ka-cs-programs/blob/master/lib/gif-reader.js
1774
- GIF.prototype.decompress = function (input, codeSize) {
1775
- let bitDepth = codeSize + 1,
1776
- CC = 1 << codeSize,
1777
- EOI = CC + 1,
1778
- stack = [],
1779
- table = [],
1780
- ntable = 0,
1781
- oldCode = null,
1782
- buffer = 0,
1783
- nbuffer = 0,
1784
- p = 0,
1785
- buf = [],
1786
- bits,
1787
- read,
1788
- ans,
1789
- n,
1790
- code,
1791
- i,
1792
- K,
1793
- b,
1794
- maxElem
1795
-
1796
- for (;;) {
1797
- if (stack.length === 0) {
1798
- bits = bitDepth
1799
- read = 0
1800
- ans = 0
1801
- while (read < bits) {
1802
- if (nbuffer === 0) {
1803
- if (p >= input.length) {
1804
- return buf
1805
- }
1806
- buffer = input[p++]
1807
- nbuffer = 8
1808
- }
1809
- n = Math.min(bits - read, nbuffer)
1810
- ans |= (buffer & ((1 << n) - 1)) << read
1811
- read += n
1812
- nbuffer -= n
1813
- buffer >>= n
1814
- }
1815
- code = ans
1816
-
1817
- if (code === EOI) {
1818
- break
1819
- }
1820
-
1821
- if (code === CC) {
1822
- table = []
1823
- for (i = 0; i < CC; ++i) {
1824
- table[i] = [i, -1, i]
1825
- }
1826
- bitDepth = codeSize + 1
1827
- maxElem = 1 << bitDepth
1828
- ntable = CC + 2
1829
- oldCode = null
1830
- continue
1831
- }
1832
-
1833
- if (oldCode === null) {
1834
- oldCode = code
1835
- buf.push(table[code][0])
1836
- continue
1837
- }
1838
-
1839
- if (code < ntable) {
1840
- for (i = code; i >= 0; i = table[i][1]) {
1841
- stack.push(table[i][0])
1842
- }
1843
- table[ntable++] = [table[code][2], oldCode, table[oldCode][2]]
1844
- } else {
1845
- K = table[oldCode][2]
1846
- table[ntable++] = [K, oldCode, K]
1847
- for (i = code; i >= 0; i = table[i][1]) {
1848
- stack.push(table[i][0])
1849
- }
1850
- }
1851
-
1852
- oldCode = code
1853
- if (ntable === maxElem) {
1854
- maxElem = 1 << ++bitDepth
1855
- if (bitDepth > 12) {
1856
- bitDepth = 12
1857
- }
1858
- }
1859
- }
1860
- b = stack.pop()
1861
- if (b == null) {
1862
- break
1863
- }
1864
- buf.push(b)
1865
- }
1866
-
1867
- return buf
1868
- }
1869
-
1870
- /**
1871
- * Expose
1872
- */
1873
-
1874
- exports = PNG
1875
- exports.png = PNG
1876
- exports.gif = GIF
1877
-
1878
- module.exports = exports