Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.

Commit 83489c1

Browse files
author
Duncan MacKenzie
committed
Add support for GIF images
1 parent 49f2c5c commit 83489c1

3 files changed

Lines changed: 54 additions & 6 deletions

File tree

lib/image.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ By Devon Govett
66
import fs from 'fs'
77
import JPEG from './image/jpeg'
88
import PNG from './image/png'
9+
import GIF from './image/gif'
910

1011
class PDFImage {
11-
static open(src, label) {
12+
static open(src, label, options) {
1213
let data
1314
if (Buffer.isBuffer(src)) {
1415
data = src
@@ -27,9 +28,11 @@ class PDFImage {
2728
}
2829

2930
if (data[0] === 0xff && data[1] === 0xd8) {
30-
return new JPEG(data, label)
31+
return new JPEG(data, label, options)
3132
} else if (data[0] === 0x89 && data.toString('ascii', 1, 4) === 'PNG') {
32-
return new PNG(data, label)
33+
return new PNG(data, label, options)
34+
} else if (data[0] === 71 && data[1] === 73 && data[2] === 70) {
35+
return new GIF(data, label, options)
3336
} else {
3437
throw new Error('Unknown image format.')
3538
}

lib/image/gif.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const COLOR_SPACE_MAP = {
2+
1: 'DeviceGray',
3+
3: 'DeviceRGB',
4+
4: 'DeviceCMYK'
5+
}
6+
class GIF {
7+
constructor(data, label, { width, height } = {}) {
8+
this.data = data;
9+
this.label = label;
10+
11+
// validate data integrity
12+
13+
// assign bits per component to this.bits
14+
this.bits = 8;
15+
16+
// assign image width and height to this.width and this.height
17+
this.width = width;
18+
this.height = height;
19+
20+
this.colorSpace = COLOR_SPACE_MAP[3];
21+
22+
this.obj = null;
23+
}
24+
25+
embed(document) {
26+
if (this.obj) { return; }
27+
28+
this.obj = document.ref({
29+
Type: 'XObject',
30+
Subtype: 'Image',
31+
BitsPerComponent: this.bits,
32+
Width: this.width,
33+
Height: this.height,
34+
ColorSpace: this.colorSpace,
35+
Filter: 'DCTDecode'
36+
});
37+
38+
this.obj.end(this.data);
39+
40+
// free memory
41+
return this.data = null;
42+
}
43+
};
44+
45+
export default GIF;

lib/mixins/images.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525
if (src.width && src.height) {
2626
image = src;
2727
} else {
28-
image = this.openImage(src);
28+
image = this.openImage(src, { width: options.width, height: options.height });
2929
}
3030
}
3131

@@ -102,14 +102,14 @@ export default {
102102
return this;
103103
},
104104

105-
openImage(src) {
105+
openImage(src, options) {
106106
let image;
107107
if (typeof src === 'string') {
108108
image = this._imageRegistry[src];
109109
}
110110

111111
if (!image) {
112-
image = PDFImage.open(src, `I${++this._imageCount}`);
112+
image = PDFImage.open(src, `I${++this._imageCount}`, options);
113113
if (typeof src === 'string') {
114114
this._imageRegistry[src] = image;
115115
}

0 commit comments

Comments
 (0)