Skip to content

Commit 980af7c

Browse files
committed
removed jpg.js dependency for processing TypedArray jpg
1 parent 43722d0 commit 980af7c

File tree

4 files changed

+34
-964
lines changed

4 files changed

+34
-964
lines changed

examples/images.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
<script src='../libs/png_support/zlib.js' type='text/javascript'></script>
1616
<script src='../libs/png_support/png.js' type='text/javascript'></script>
17-
<script src='../libs/png_support/jpg.js' type='text/javascript'></script>
1817

19-
<script src='../libs/Deflate/adler32cs.js' type='text/javascript'></script>
2018
<script src='../libs/Deflate/deflate.js' type='text/javascript'></script>
2119

2220
<script src='../jspdf.js' type='text/javascript'></script>

examples/images/grid.png

979 Bytes
Loading

jspdf.plugin.addimage.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,37 @@
581581
blockLength = imgData.charCodeAt(i)*256 + imgData.charCodeAt(i+1)
582582
}
583583
}
584+
}
585+
, getJpegSizeFromBytes = function(data) {
586+
587+
var hdr = (data[0] << 8) | data[1];
588+
589+
if(hdr !== 0xFFD8)
590+
throw new Error('Supplied data is not a JPEG');
591+
592+
var len = data.length,
593+
block = (data[4] << 8) + data[5],
594+
pos = 4,
595+
bytes, width, height;
596+
597+
while(pos < len) {
598+
pos += block;
599+
bytes = readBytes(data, pos);
600+
block = (bytes[2] << 8) + bytes[3];
601+
if((bytes[1] === 0xC0 || bytes[1] === 0xC2) && bytes[0] === 0xFF && block > 7) {
602+
bytes = readBytes(data, pos + 5);
603+
width = (bytes[2] << 8) + bytes[3];
604+
height = (bytes[0] << 8) + bytes[1];
605+
return {width:width, height:height};
606+
}
607+
608+
pos+=2;
609+
}
610+
611+
throw new Error('getJpegSizeFromBytes could not find the size of the image');
612+
}
613+
, readBytes = function(data, offset) {
614+
return data.subarray(offset, offset+ 4);
584615
};
585616

586617

@@ -601,15 +632,12 @@
601632

602633
if(this.isArrayBufferView(data)) {
603634

604-
var img = new JpegImage();
605-
img.parse(data);
635+
dims = getJpegSizeFromBytes(data);
606636

607-
/*
608-
* if we already have a stored binary string rep use that
609-
*/
637+
// if we already have a stored binary string rep use that
610638
data = dataAsBinaryString || this.arrayBufferToBinaryString(data);
611639

612-
return this.createImageInfo(data, img.width, img.height, colorSpace, bpc, filter, index, alias);
640+
return this.createImageInfo(data, dims.width, dims.height, colorSpace, bpc, filter, index, alias);
613641
}
614642

615643
return null;

0 commit comments

Comments
 (0)