|
557 | 557 | //Algorithm from: http://www.64lines.com/jpeg-width-height
|
558 | 558 | var getJpegSize = function(imgData) {
|
559 | 559 | 'use strict'
|
560 |
| - var width, height; |
| 560 | + var width, height, numcomponents; |
561 | 561 | // Verify we have a valid jpeg header 0xff,0xd8,0xff,0xe0,?,?,'J','F','I','F',0x00
|
562 | 562 | if (!imgData.charCodeAt(0) === 0xff ||
|
563 | 563 | !imgData.charCodeAt(1) === 0xd8 ||
|
|
587 | 587 | imgData.charCodeAt(i+1) === 0xc7) {
|
588 | 588 | height = imgData.charCodeAt(i+5)*256 + imgData.charCodeAt(i+6);
|
589 | 589 | 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]; |
591 | 592 | } else {
|
592 | 593 | i += 2;
|
593 | 594 | blockLength = imgData.charCodeAt(i)*256 + imgData.charCodeAt(i+1)
|
|
604 | 605 | var len = data.length,
|
605 | 606 | block = (data[4] << 8) + data[5],
|
606 | 607 | pos = 4,
|
607 |
| - bytes, width, height; |
| 608 | + bytes, width, height, numcomponents; |
608 | 609 |
|
609 | 610 | while(pos < len) {
|
610 | 611 | pos += block;
|
|
614 | 615 | bytes = readBytes(data, pos + 5);
|
615 | 616 | width = (bytes[2] << 8) + bytes[3];
|
616 | 617 | height = (bytes[0] << 8) + bytes[1];
|
617 |
| - return {width:width, height:height}; |
| 618 | + numcomponents = bytes[4]; |
| 619 | + return {width:width, height:height, numcomponents: numcomponents}; |
618 | 620 | }
|
619 | 621 |
|
620 | 622 | pos+=2;
|
|
623 | 625 | throw new Error('getJpegSizeFromBytes could not find the size of the image');
|
624 | 626 | }
|
625 | 627 | , readBytes = function(data, offset) {
|
626 |
| - return data.subarray(offset, offset+ 4); |
| 628 | + return data.subarray(offset, offset+ 5); |
627 | 629 | };
|
628 | 630 |
|
629 | 631 | jsPDFAPI.processJPEG = function(data, index, alias, compression, dataAsBinaryString) {
|
|
635 | 637 |
|
636 | 638 | if(this.isString(data)) {
|
637 | 639 | 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); |
639 | 641 | }
|
640 | 642 |
|
641 | 643 | if(this.isArrayBuffer(data))
|
|
648 | 650 | // if we already have a stored binary string rep use that
|
649 | 651 | data = dataAsBinaryString || this.arrayBufferToBinaryString(data);
|
650 | 652 |
|
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); |
652 | 654 | }
|
653 | 655 |
|
654 | 656 | return null;
|
|
0 commit comments