Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export const toBinaryString = (bytes) => {
return out;
};

export const fromBase64 = (b64) => {
const binary = atob(b64);
const out = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
out[i] = binary.charCodeAt(i);
}
return out;
};

export const readUInt16BE = (bytes, offset = 0) =>
((bytes[offset] << 8) | bytes[offset + 1]) >>> 0;

Expand Down
3 changes: 2 additions & 1 deletion lib/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ By Devon Govett
*/

import fs from 'fs';
import { fromBase64 } from './binary';
import JPEG from './image/jpeg';
import PNG from './image/png';

Expand All @@ -17,7 +18,7 @@ class PDFImage {
} else {
const match = /^data:.+?;base64,(.*)$/.exec(src);
if (match) {
data = Buffer.from(match[1], 'base64');
data = fromBase64(match[1]);
} else {
data = fs.readFileSync(src);
if (!data) {
Expand Down
10 changes: 5 additions & 5 deletions lib/image/jpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class JPEG {
let marker;
this.data = data;
this.label = label;
if (this.data.readUInt16BE(0) !== 0xffd8) {
if (readUInt16BE(this.data, 0) !== 0xffd8) {
throw 'SOI not found in JPEG';
}

Expand All @@ -112,12 +112,12 @@ class JPEG {
while (pos < this.data.length && this.data[pos] !== 0xff) pos++;
if (pos >= this.data.length) break;

marker = this.data.readUInt16BE(pos);
marker = readUInt16BE(this.data, pos);
pos += 2;
if (MARKERS.includes(marker)) {
break;
}
pos += this.data.readUInt16BE(pos);
pos += readUInt16BE(this.data, pos);
}

if (!MARKERS.includes(marker)) {
Expand All @@ -126,10 +126,10 @@ class JPEG {
pos += 2;

this.bits = this.data[pos++];
this.height = this.data.readUInt16BE(pos);
this.height = readUInt16BE(this.data, pos);
pos += 2;

this.width = this.data.readUInt16BE(pos);
this.width = readUInt16BE(this.data, pos);
pos += 2;

const channels = this.data[pos];
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/image.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import PDFDocument from '../../lib/document';
import fs from 'fs';
import JPEG from '../../lib/image/jpeg';
import PDFImage from '../../lib/image';
import dataURIs from '../images/bee';

describe('Image', function () {
/**
Expand Down Expand Up @@ -29,6 +31,12 @@ describe('Image', function () {
expect(jpeg.orientation).toBe(1);
});

test.each(['png', 'jpeg'])('%s base64 data URI is decoded', (format) => {
const image = PDFImage.open(dataURIs[format], 'test');
expect(image.width).toBe(409);
expect(image.height).toBe(400);
});

test('RGB JPEG is parsed with DeviceRGB color space (regression)', () => {
const data = fs.readFileSync('./tests/images/bee.jpg');
const jpeg = new JPEG(data, 'test');
Expand Down
Loading