all files / core/graphics/images/ jpeg-decoder.js

22.14% Statements 29/131
21.57% Branches 11/51
44.44% Functions 8/18
20.31% Lines 26/128
11 statements, 6 functions, 9 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                                                                                                                                                                                                                                                                                                                 
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "./image-decoder", "./../../base-stream", "./../../pdf-primitives", "./../../enumerator"], function (require, exports, image_decoder_1, base_stream_1, pdf_primitives_1, enumerator_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _JpegDecoder = (function (_super) {
        __extends(_JpegDecoder, _super);
        function _JpegDecoder(stream) {
            var _this = _super.call(this) || this;
            _this._stream = stream;
            _this._initialize();
            return _this;
        }
        Object.defineProperty(_JpegDecoder.prototype, "_imageDataAsNumberArray", {
            get: function () {
                return this._imageData;
            },
            enumerable: true,
            configurable: true
        });
        _JpegDecoder.prototype._initialize = function () {
            this._format = enumerator_1._ImageFormat.jpeg;
            this._readHeader();
            this._reset();
            this._imageData = new Uint8Array(this._stream.byteLength);
            this._read(this._imageData, 0, this._imageData.byteLength);
        };
        _JpegDecoder.prototype._readHeader = function () {
            this._reset();
            var imgData = new Uint8Array(this._stream.byteLength);
            this._read(imgData, 0, imgData.byteLength);
            var i = 4;
            var length = this._getBuffer(i) * 256 + this._getBuffer(i + 1);
            var isLengthExceed = false;
            while (i < imgData.byteLength) {
                i += length;
                if (i < imgData.byteLength) {
                    if (this._getBuffer(i + 1) === 192) {
                        this._height = this._getBuffer(i + 5) * 256 + this._getBuffer(i + 6);
                        this._width = this._getBuffer(i + 7) * 256 + this._getBuffer(i + 8);
                        this._noOfComponents = this._getBuffer(i + 9);
                        if (this._width !== 0 && this._height !== 0) {
                            return;
                        }
                    }
                    else {
                        i += 2;
                        length = this._getBuffer(i) * 256 + this._getBuffer(i + 1);
                    }
                }
                else {
                    isLengthExceed = true;
                    break;
                }
            }
            if (isLengthExceed) {
                this._reset();
                this._seek(2);
                this._readExceededJpegImage();
            }
        };
        _JpegDecoder.prototype._getImageDictionary = function () {
            if (this._imageStream && this._imageStream.length > 0) {
                return this._imageStream;
            }
            else {
                var data = [];
                this._imageStream = new base_stream_1._PdfStream(data, new pdf_primitives_1._PdfDictionary());
                this._imageStream.isImageStream = true;
                var entryLength = this._imageDataAsNumberArray.byteLength;
                this._imageStream.bytes = new Uint8Array(entryLength);
                var chunkSize = 1024;
                for (var offset = 0; offset < entryLength; offset += chunkSize) {
                    var length_1 = Math.min(chunkSize, entryLength - offset);
                    for (var i = 0; i < length_1; i++) {
                        this._imageStream.bytes[offset + i] = this._getBuffer(offset + i);
                    }
                }
                this._imageStream._isCompress = false;
                var dictionary = new pdf_primitives_1._PdfDictionary();
                dictionary.set('Type', new pdf_primitives_1._PdfName('XObject'));
                dictionary.set('Subtype', new pdf_primitives_1._PdfName('Image'));
                dictionary.set('Width', this._width);
                dictionary.set('Height', this._height);
                dictionary.set('BitsPerComponent', this._bitsPerComponent);
                dictionary.set('Filter', new pdf_primitives_1._PdfName('DCTDecode'));
                dictionary.set('ColorSpace', new pdf_primitives_1._PdfName(this._getColorSpace()));
                dictionary.set('DecodeParms', this._getDecodeParams());
                this._imageStream.dictionary = dictionary;
                this._imageStream.end = this._imageStream.bytes.length;
                this._imageStream.dictionary._updated = true;
                return this._imageStream;
            }
        };
        _JpegDecoder.prototype._getColorSpace = function () {
            if (this._noOfComponents === 1) {
                return 'DeviceGray';
            }
            else if (this._noOfComponents === 4) {
                return 'DeviceCMYK';
            }
            return 'DeviceRGB';
        };
        _JpegDecoder.prototype._getDecodeParams = function () {
            var decodeParams = new pdf_primitives_1._PdfDictionary();
            decodeParams.set('Columns', this._width);
            decodeParams.set('BlackIs1', true);
            decodeParams.set('K', -1);
            decodeParams.set('Predictor', 15);
            decodeParams.set('BitsPerComponent', this._bitsPerComponent);
            return decodeParams;
        };
        _JpegDecoder.prototype._skipStream = function () {
            var length = this._getBuffer(this._position) << 8 | this._getBuffer(this._position + 1);
            this._seek(2);
            if (length < 2) {
                throw new Error('Error decoding JPEG image');
            }
            else if (length > 0) {
                this._seek(length - 2);
            }
        };
        _JpegDecoder.prototype._readExceededJpegImage = function () {
            var isContinueReading = true;
            while (isContinueReading) {
                var marker = this._getMarker();
                switch (marker) {
                    case 0x00C0:
                    case 0x00C1:
                    case 0x00C2:
                    case 0x00C3:
                    case 0x00C5:
                    case 0x00C6:
                    case 0x00C7:
                    case 0x00C9:
                    case 0x00CA:
                    case 0x00CB:
                    case 0x00CD:
                    case 0x00CE:
                    case 0x00CF:
                        this._seek(3);
                        this._height = this._getBuffer(this._position) << 8 | this._getBuffer(this._position + 1);
                        this._seek(2);
                        this._width = this._getBuffer(this._position) << 8 | this._getBuffer(this._position + 1);
                        this._seek(2);
                        this._noOfComponents = this._getBuffer(this._position);
                        this._seek(1);
                        isContinueReading = false;
                        break;
                    default:
                        this._skipStream();
                        break;
                }
            }
        };
        _JpegDecoder.prototype._getMarker = function () {
            var skippedByte = 0;
            var marker = this._readByte();
            while (marker !== 255) {
                skippedByte++;
                marker = this._readByte();
            }
            do {
                marker = this._readByte();
            } while (marker === 255);
            if (skippedByte !== 0) {
                throw new Error('Error decoding JPEG image');
            }
            return this._toUnsigned16(marker);
        };
        return _JpegDecoder;
    }(image_decoder_1._ImageDecoder));
    exports._JpegDecoder = _JpegDecoder;
});