Skip to content

Commit 0e817c0

Browse files
committed
include samples & scripts for testing
1 parent 39332f8 commit 0e817c0

File tree

8 files changed

+108
-6
lines changed

8 files changed

+108
-6
lines changed

.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ src/
44
test/
55
private/
66

7+
scripts/
8+
samples/
9+
710
*.tgz

samples/sample1-lzma.swf

108 KB
Binary file not shown.

samples/sample1-raw.swf

177 KB
Binary file not shown.

samples/sample1-zlib.swf

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sample1.swf

samples/sample1.swf

144 KB
Binary file not shown.

scripts/all-formats.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable no-console */
2+
const fs = require('fs')
3+
const _ = require('lodash')
4+
5+
const { readFromBufferP } = require('../dist')
6+
7+
const cmp = (x,y) => {
8+
const newX = Object.assign({},x)
9+
delete newX.fileLength
10+
const newY = Object.assign({},y)
11+
delete newY.fileLength
12+
return _.isEqual(newX,newY)
13+
}
14+
15+
[
16+
['raw', '../samples/sample1-raw.swf'],
17+
['zlib', '../samples/sample1-zlib.swf'],
18+
['lzma', '../samples/sample1-lzma.swf'],
19+
].reduce(
20+
(p, [ty, fName]) => p.then(results => {
21+
const rawData = fs.readFileSync(fName)
22+
const testLabel = `test.${ty}`
23+
console.time(testLabel)
24+
return readFromBufferP(rawData).then(d => {
25+
console.timeEnd(testLabel)
26+
return (results.push(d), results)
27+
})
28+
}),
29+
Promise.resolve([]))
30+
.then(results => {
31+
console.log(_.sum(results[0].tags.map(x => x.rawData.length)))
32+
console.log(cmp(results[0],results[1]))
33+
})

scripts/coverage.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* eslint-disable no-console */
2+
const fs = require('fs')
3+
const { readFromBufferP, extractImages } = require('../dist')
4+
5+
const rawData = fs.readFileSync('../samples/sample1.swf');
6+
7+
(async () => {
8+
const swf = await readFromBufferP(rawData)
9+
console.time('extract')
10+
const tr = ({code, characterId, imgType, imgData}) =>
11+
({code, characterId, imgType, imgDataLen: imgData.length})
12+
const ts = await Promise.all(extractImages(swf.tags))
13+
console.timeEnd('extract')
14+
15+
ts.map(t => console.log(tr(t)))
16+
})()
17+
18+
/*
19+
20+
coverage:
21+
22+
- SwfTags.DefineBits
23+
24+
- empty JPEGTables: sample1.swf
25+
- non-empty JPEGTables
26+
27+
- SwfTags.DefineBitsJPEG2
28+
29+
- png: sample1.swf
30+
- gif: sample1.swf
31+
- jpeg: sample1.swf
32+
33+
- SwfTags.DefineBitsJPEG3
34+
35+
- png: sample1.swf
36+
- gif: sample1.swf
37+
- jpeg: sample1.swf
38+
39+
- SwfTags.DefineBitsLossless
40+
41+
- 15-bit RGB
42+
- 24-bit RGB: sample1.swf
43+
- 8-bit colormapped image: sample1.swf
44+
45+
- SwfTags.DefineBitsLossless2
46+
47+
- 32-bit ARGB image: sample1.swf
48+
- 8-bit colormapped image: sample1.swf
49+
50+
- SwfTags.DefineBitsJPEG4
51+
52+
- png: sample1.swf
53+
- gif: sample1.swf
54+
- jpeg: sample1.swf
55+
56+
*/

src/extract-images.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ const imageTagCodes = [
1818
SwfTags.DefineBitsJPEG4,
1919
]
2020

21+
const coverageFlag = false
22+
const coverage =
23+
coverageFlag ?
24+
// eslint-disable-next-line no-console
25+
msg => console.log(`coverage: ${msg}`) :
26+
() => undefined
2127

2228
// each extractor either returns either a value or a Promise
2329
const extractors = {}
@@ -33,9 +39,9 @@ const define = (code, extractor) => {
3339
const pngMagic = Buffer.from('0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A'.split(' ').map(Number))
3440
const gifMagic = Buffer.from('0x47 0x49 0x46 0x38 0x39 0x61'.split(' ').map(Number))
3541
const recognizeHeader = buffer => {
36-
if (pngMagic.equals(Buffer.from(buffer.buffer, 0, pngMagic.length)))
42+
if (pngMagic.equals(buffer.slice(0, pngMagic.length)))
3743
return 'png'
38-
if (gifMagic.equals(Buffer.from(buffer.buffer, 0, gifMagic.length)))
44+
if (gifMagic.equals(buffer.slice(0, gifMagic.length)))
3945
return 'gif'
4046
return 'jpeg'
4147
}
@@ -96,7 +102,6 @@ const gDefineBitsJPEG3or4Handler = code => tagData => {
96102
enc.end(output)
97103
}))
98104
})
99-
100105
toArray(enc).then(parts => {
101106
const buffers = parts
102107
.map(part => Buffer.isBuffer(part) ? part : Buffer.from(part))
@@ -115,7 +120,6 @@ extractors[SwfTags.DefineBitsJPEG3] =
115120
extractors[SwfTags.DefineBitsJPEG4] =
116121
gDefineBitsJPEG3or4Handler(SwfTags.DefineBitsJPEG4)
117122

118-
119123
extractors[SwfTags.DefineBitsLossless] = tagData => new Promise(
120124
(resolve,reject) => {
121125
const {
@@ -126,8 +130,9 @@ extractors[SwfTags.DefineBitsLossless] = tagData => new Promise(
126130

127131
const enc = new PNGEncoder(bitmapWidth, bitmapHeight, {colorSpace: 'rgb'})
128132
zlib.unzip(zlibBitmapData, (err, dataBuf) => {
129-
if (err) throw reject(new Error(err))
130-
const output = new Buffer(bitmapHeight * bitmapHeight * 3)
133+
if (err)
134+
reject(new Error(err))
135+
const output = new Buffer(bitmapWidth * bitmapHeight * 3)
131136
let index = 0
132137
let ptr = 0
133138
/* eslint-disable no-bitwise */
@@ -137,6 +142,9 @@ extractors[SwfTags.DefineBitsLossless] = tagData => new Promise(
137142
// 24-bit RGB image
138143
bitmapFormat === 5
139144
) {
145+
if (bitmapFormat === 4)
146+
coverage(`DefineBitsLossless 15-bit`)
147+
140148
for (let y = 0; y < bitmapHeight; y++) {
141149
for (let x = 0; x < bitmapWidth; x++) {
142150
if (bitmapFormat === 4) {
@@ -285,6 +293,7 @@ const mkContext = rawTags => ({
285293
if (jpegTablesTag.jpegData.length === 0) {
286294
return _.identity
287295
} else {
296+
coverage('JPEGTables non-empty')
288297
return imgBuffer =>
289298
Buffer.concat([
290299
jpegTablesTag.jpegData,

0 commit comments

Comments
 (0)