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

27.45% Statements 14/51
0% Branches 0/12
18.18% Functions 2/11
27.45% Lines 14/51
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                                                                                                                        
define(["require", "exports", "./../../enumerator"], function (require, exports, enumerator_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _ImageDecoder = (function () {
        function _ImageDecoder() {
            this._format = enumerator_1._ImageFormat.unknown;
            this._height = 0;
            this._width = 0;
            this._bitsPerComponent = 8;
            this._position = 0;
            this._noOfComponents = -1;
        }
        _ImageDecoder.prototype._reset = function () {
            this._position = 0;
        };
        _ImageDecoder.prototype._getBuffer = function (index) {
            return this._stream[index];
        };
        _ImageDecoder.prototype._read = function (buffer, offset, count, stream) {
            if (stream && Array.isArray(stream)) {
                var result = 0;
                if (count <= stream.length && stream.length - offset >= count) {
                    for (var i = 0; i < count; i++) {
                        buffer[i] = stream[offset];
                        offset++;
                        result++;
                    }
                }
                return { 'outputBuffer': buffer, 'offset': offset, 'length': result };
            }
            else {
                for (var index = offset; index < count; index++) {
                    var position = this._position;
                    buffer[index] = this._getBuffer(position);
                    this._position++;
                }
            }
        };
        _ImageDecoder.prototype._readString = function (length) {
            var result = '';
            for (var i = 0; i < length; i++) {
                result += String.fromCharCode(this._readByte());
            }
            return result;
        };
        _ImageDecoder.prototype._seek = function (length) {
            this._position += length;
        };
        _ImageDecoder.prototype._readByte = function () {
            if (this._position < this._stream.byteLength) {
                var value = this._getBuffer(this._position);
                this._position += 1;
                return value;
            }
            else {
                throw new Error('Error decoding JPEG image. Invalid offset.');
            }
        };
        _ImageDecoder.prototype._toUnsigned16 = function (value) {
            value = value & 0xFFFF;
            return value < 0 ? (value + 0x10000) : value;
        };
        _ImageDecoder.prototype._readUnsigned32 = function (offset) {
            var i1 = this._getBuffer(offset + 3);
            var i2 = this._getBuffer(offset + 2);
            var i3 = this._getBuffer(offset + 1);
            var i4 = this._getBuffer(offset);
            return i1 | (i2 << 8) | (i3 << 16) | (i4 << 24);
        };
        return _ImageDecoder;
    }());
    exports._ImageDecoder = _ImageDecoder;
});