|
581 | 581 | blockLength = imgData.charCodeAt(i)*256 + imgData.charCodeAt(i+1)
|
582 | 582 | }
|
583 | 583 | }
|
| 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); |
584 | 615 | };
|
585 | 616 |
|
586 | 617 |
|
|
601 | 632 |
|
602 | 633 | if(this.isArrayBufferView(data)) {
|
603 | 634 |
|
604 |
| - var img = new JpegImage(); |
605 |
| - img.parse(data); |
| 635 | + dims = getJpegSizeFromBytes(data); |
606 | 636 |
|
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 |
610 | 638 | data = dataAsBinaryString || this.arrayBufferToBinaryString(data);
|
611 | 639 |
|
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); |
613 | 641 | }
|
614 | 642 |
|
615 | 643 | return null;
|
|
0 commit comments