Skip to content

Commit 07908c3

Browse files
committed
fixed grayscale JPEG
1 parent e07d5bb commit 07908c3

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

jspdf.plugin.addimage.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@
557557
//Algorithm from: http://www.64lines.com/jpeg-width-height
558558
var getJpegSize = function(imgData) {
559559
'use strict'
560-
var width, height;
560+
var width, height, numcomponents;
561561
// Verify we have a valid jpeg header 0xff,0xd8,0xff,0xe0,?,?,'J','F','I','F',0x00
562562
if (!imgData.charCodeAt(0) === 0xff ||
563563
!imgData.charCodeAt(1) === 0xd8 ||
@@ -587,7 +587,8 @@
587587
imgData.charCodeAt(i+1) === 0xc7) {
588588
height = imgData.charCodeAt(i+5)*256 + imgData.charCodeAt(i+6);
589589
width = imgData.charCodeAt(i+7)*256 + imgData.charCodeAt(i+8);
590-
return [width, height];
590+
numcomponents = imgData.charCodeAt(i+9);
591+
return [width, height, numcomponents];
591592
} else {
592593
i += 2;
593594
blockLength = imgData.charCodeAt(i)*256 + imgData.charCodeAt(i+1)
@@ -604,7 +605,7 @@
604605
var len = data.length,
605606
block = (data[4] << 8) + data[5],
606607
pos = 4,
607-
bytes, width, height;
608+
bytes, width, height, numcomponents;
608609

609610
while(pos < len) {
610611
pos += block;
@@ -614,7 +615,8 @@
614615
bytes = readBytes(data, pos + 5);
615616
width = (bytes[2] << 8) + bytes[3];
616617
height = (bytes[0] << 8) + bytes[1];
617-
return {width:width, height:height};
618+
numcomponents = bytes[4];
619+
return {width:width, height:height, numcomponents: numcomponents};
618620
}
619621

620622
pos+=2;
@@ -623,7 +625,7 @@
623625
throw new Error('getJpegSizeFromBytes could not find the size of the image');
624626
}
625627
, readBytes = function(data, offset) {
626-
return data.subarray(offset, offset+ 4);
628+
return data.subarray(offset, offset+ 5);
627629
};
628630

629631
jsPDFAPI.processJPEG = function(data, index, alias, compression, dataAsBinaryString) {
@@ -635,7 +637,7 @@
635637

636638
if(this.isString(data)) {
637639
dims = getJpegSize(data);
638-
return this.createImageInfo(data, dims[0], dims[1], colorSpace, bpc, filter, index, alias);
640+
return this.createImageInfo(data, dims[0], dims[1], dims[3] == 1 ? this.color_spaces.DEVICE_GRAY:colorSpace, bpc, filter, index, alias);
639641
}
640642

641643
if(this.isArrayBuffer(data))
@@ -648,7 +650,7 @@
648650
// if we already have a stored binary string rep use that
649651
data = dataAsBinaryString || this.arrayBufferToBinaryString(data);
650652

651-
return this.createImageInfo(data, dims.width, dims.height, colorSpace, bpc, filter, index, alias);
653+
return this.createImageInfo(data, dims.width, dims.height, dims.numcomponents == 1 ? this.color_spaces.DEVICE_GRAY:colorSpace, bpc, filter, index, alias);
652654
}
653655

654656
return null;

0 commit comments

Comments
 (0)