Skip to content

Commit b161451

Browse files
committed
Feature: Support GIFs
Pronounce it however you like, we still synchronously support 'em.
1 parent 7efb323 commit b161451

4 files changed

Lines changed: 48 additions & 14 deletions

File tree

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
{
22
"name": "@netlogo/synchrodecoder",
3-
"description": "A JavaScript library for synchronously decoding JPEGs and PNGs to binary data",
3+
"description": "A JavaScript library for synchronously decoding GIFs, JPEGs, and PNGs to binary data",
44
"version": "3.0.0",
55
"author": "Center for Connected Learning (https://ccl.northwestern.edu/)",
66
"contributors": [
77
"Jason Bertsche <jason.bertsche@gmail.com> (https://www.github.com/TheBizzle)"
88
],
99
"homepage": "https://ccl.northwestern.edu/netlogo",
10-
"keywords": ["image", "png", "jpeg", "synchronous"],
10+
"keywords": [
11+
"image",
12+
"png",
13+
"jpeg",
14+
"synchronous"
15+
],
1116
"license": "CC0-1.0",
1217
"repository": "https://github.com/NetLogo/SynchroDecoder",
1318
"dependencies": {
14-
"upng-js": "2.1.0",
15-
"jpeg-js": "0.4.4"
19+
"jpeg-js": "0.4.4",
20+
"omggif": "^1.0.10",
21+
"upng-js": "2.1.0"
1622
},
1723
"devDependencies": {
18-
"buffer": "6.0.3",
19-
"grunt": "1.6.2",
20-
"grunt-contrib-coffee": "2.1.0",
21-
"grunt-contrib-copy": "1.0.0",
22-
"grunt-rollup": "12.0.0",
23-
"grunt-terser": "2.0.0",
24-
"@rollup/plugin-commonjs": "29.0.2",
25-
"@rollup/plugin-node-resolve": "16.0.3"
24+
"@rollup/plugin-commonjs": "29.0.2",
25+
"@rollup/plugin-node-resolve": "16.0.3",
26+
"buffer": "6.0.3",
27+
"grunt": "1.6.2",
28+
"grunt-contrib-coffee": "2.1.0",
29+
"grunt-contrib-copy": "1.0.0",
30+
"grunt-rollup": "12.0.0",
31+
"grunt-terser": "2.0.0"
2632
}
2733
}

src/synchrodecoder.coffee

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Buffer } from 'buffer/index.js'
22

3-
import PNG from 'upng-js/UPNG.js'
4-
import JPEG from 'jpeg-js/index.js'
3+
import PNG from 'upng-js/UPNG.js'
4+
import JPEG from 'jpeg-js/index.js'
5+
import { GifReader } from 'omggif'
56

67
dataURIToBytes = (uri) ->
78
raw = window.atob(uri.slice(uri.indexOf(",") + 1))
@@ -15,6 +16,7 @@ aIsPrefixOfB = (a, b) ->
1516

1617
pngSignature = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
1718
jpgSignature = [0xFF, 0xD8, 0xFF]
19+
gifSignature = [0x47, 0x49, 0x46, 0x38]
1820

1921
synchroDecoder =
2022
(b64) ->
@@ -44,6 +46,18 @@ synchroDecoder =
4446
else
4547
throw new Error("Converted JPEG bytes have length #{array.length}, but valid JPEG bytes would have length #{image.height * image.width * 4}.")
4648

49+
else if aIsPrefixOfB(gifSignature, bytes)
50+
51+
reader = new GifReader(bytes)
52+
pixelData = new Uint8Array(reader.width * reader.height * 4)
53+
reader.decodeAndBlitFrameRGBA(0, pixelData)
54+
array = new Uint8ClampedArray(pixelData.buffer)
55+
56+
if array.length is (reader.height * reader.width * 4)
57+
{ array, height: reader.height, width: reader.width, didSucceed: true }
58+
else
59+
throw new Error("Converted GIF bytes have length #{array.length}, but valid GIF bytes would have length #{reader.height * reader.width * 4}.")
60+
4761
else
4862
throw new Error("Undecodable file type")
4963

test.html

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)