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

24.05% Statements 38/158
6.06% Branches 2/33
13.64% Functions 3/22
24.05% Lines 38/158
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247                                                                                                                                                                                                                                                                                                                                                                                                                                  
define(["require", "exports", "./byte-array", "./../../primitives/pdf-stream", "./../../input-output/pdf-dictionary-properties", "./../../primitives/pdf-name", "./../../primitives/pdf-number", "./../../primitives/pdf-boolean", "./../../primitives/pdf-dictionary"], function (require, exports, byte_array_1, pdf_stream_1, pdf_dictionary_properties_1, pdf_name_1, pdf_number_1, pdf_boolean_1, pdf_dictionary_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ImageFormat;
    (function (ImageFormat) {
        ImageFormat[ImageFormat["Unknown"] = 0] = "Unknown";
        ImageFormat[ImageFormat["Bmp"] = 1] = "Bmp";
        ImageFormat[ImageFormat["Emf"] = 2] = "Emf";
        ImageFormat[ImageFormat["Gif"] = 3] = "Gif";
        ImageFormat[ImageFormat["Jpeg"] = 4] = "Jpeg";
        ImageFormat[ImageFormat["Png"] = 5] = "Png";
        ImageFormat[ImageFormat["Wmf"] = 6] = "Wmf";
        ImageFormat[ImageFormat["Icon"] = 7] = "Icon";
    })(ImageFormat = exports.ImageFormat || (exports.ImageFormat = {}));
    var ImageDecoder = (function () {
        function ImageDecoder(stream) {
            this.sof1Marker = 0x00C1;
            this.sof2Marker = 0x00C2;
            this.sof3Marker = 0x00C3;
            this.sof5Marker = 0x00C5;
            this.sof6Marker = 0x00C6;
            this.sof7Marker = 0x00C7;
            this.sof9Marker = 0x00C9;
            this.sof10Marker = 0x00CA;
            this.sof11Marker = 0x00CB;
            this.sof13Marker = 0x00CD;
            this.sof14Marker = 0x00CE;
            this.sof15Marker = 0x00CF;
            this.mFormat = ImageFormat.Unknown;
            this.mbitsPerComponent = 8;
            this.dictionaryProperties = new pdf_dictionary_properties_1.DictionaryProperties();
            this.mStream = stream;
            this.initialize();
        }
        Object.defineProperty(ImageDecoder.prototype, "height", {
            get: function () {
                return this.mHeight;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ImageDecoder.prototype, "width", {
            get: function () {
                return this.mWidth;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ImageDecoder.prototype, "bitsPerComponent", {
            get: function () {
                return this.mbitsPerComponent;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ImageDecoder.prototype, "size", {
            get: function () {
                return this.mImageData.count;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ImageDecoder.prototype, "imageData", {
            get: function () {
                return this.mImageData;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ImageDecoder.prototype, "imageDataAsNumberArray", {
            get: function () {
                return this.mImageData.internalBuffer.buffer;
            },
            enumerable: true,
            configurable: true
        });
        ImageDecoder.prototype.initialize = function () {
            if (this.mFormat === ImageFormat.Unknown && this.checkIfJpeg()) {
                this.mFormat = ImageFormat.Jpeg;
                this.parseJpegImage();
            }
            else {
                throw new TypeError('Only the JPEG format is supported');
            }
            this.reset();
            this.mImageData = new byte_array_1.ByteArray(this.mStream.count);
            this.mStream.read(this.mImageData, 0, this.mImageData.count);
        };
        ImageDecoder.prototype.reset = function () {
            this.mStream.position = 0;
        };
        ImageDecoder.prototype.parseJpegImage = function () {
            this.reset();
            var imgData = new byte_array_1.ByteArray(this.mStream.count);
            this.mStream.read(imgData, 0, imgData.count);
            var i = 4;
            var isLengthExceed = false;
            var length = imgData.getBuffer(i) * 256 + imgData.getBuffer(i + 1);
            while (i < imgData.count) {
                i += length;
                if (i < imgData.count) {
                    if (imgData.getBuffer(i + 1) === 192) {
                        this.mHeight = imgData.getBuffer(i + 5) * 256 + imgData.getBuffer(i + 6);
                        this.mWidth = imgData.getBuffer(i + 7) * 256 + imgData.getBuffer(i + 8);
                        return;
                    }
                    else {
                        i += 2;
                        length = imgData.getBuffer(i) * 256 + imgData.getBuffer(i + 1);
                    }
                }
                else {
                    isLengthExceed = true;
                    break;
                }
            }
            if (isLengthExceed) {
                this.mStream.position = 0;
                this.skip(this.mStream, 2);
                this.readExceededJPGImage(this.mStream);
            }
        };
        Object.defineProperty(ImageDecoder.prototype, "format", {
            get: function () {
                return this.mFormat;
            },
            enumerable: true,
            configurable: true
        });
        ImageDecoder.prototype.checkIfJpeg = function () {
            this.reset();
            for (var i = 0; i < ImageDecoder.mJpegHeader.length; i++) {
                if (ImageDecoder.mJpegHeader[i] !== this.mStream.readByte(i)) {
                    return false;
                }
                this.mStream.position++;
            }
            return true;
        };
        ImageDecoder.prototype.getImageDictionary = function () {
            if (this.mFormat === ImageFormat.Jpeg) {
                var tempArrayBuffer = this.imageData.internalBuffer.length;
                this.imageStream = new pdf_stream_1.PdfStream();
                this.imageStream.isResource = true;
                var tempString = '';
                var decodedString = '';
                for (var i = 0; i < this.imageDataAsNumberArray.byteLength; i++) {
                    tempString += String.fromCharCode(null, this.mStream.readByte(i));
                }
                for (var i = 0; i < tempString.length; i++) {
                    if (i % 2 !== 0) {
                        decodedString += tempString[i];
                    }
                }
                this.imageStream.data = [decodedString];
                this.imageStream.compress = false;
                this.imageStream.items.setValue(this.dictionaryProperties.type, new pdf_name_1.PdfName(this.dictionaryProperties.xObject));
                this.imageStream.items.setValue(this.dictionaryProperties.subtype, new pdf_name_1.PdfName(this.dictionaryProperties.image));
                this.imageStream.items.setValue(this.dictionaryProperties.width, new pdf_number_1.PdfNumber(this.width));
                this.imageStream.items.setValue(this.dictionaryProperties.height, new pdf_number_1.PdfNumber(this.height));
                this.imageStream.items.setValue(this.dictionaryProperties.bitsPerComponent, new pdf_number_1.PdfNumber(this.bitsPerComponent));
                this.imageStream.items.setValue(this.dictionaryProperties.filter, new pdf_name_1.PdfName(this.dictionaryProperties.dctdecode));
                this.imageStream.items.setValue(this.dictionaryProperties.colorSpace, new pdf_name_1.PdfName(this.getColorSpace()));
                this.imageStream.items.setValue(this.dictionaryProperties.decodeParms, this.getDecodeParams());
                return this.imageStream;
            }
            else {
                return this.imageStream;
            }
        };
        ImageDecoder.prototype.getColorSpace = function () {
            return this.dictionaryProperties.deviceRgb;
        };
        ImageDecoder.prototype.getDecodeParams = function () {
            var decodeParams = new pdf_dictionary_1.PdfDictionary();
            decodeParams.items.setValue(this.dictionaryProperties.columns, new pdf_number_1.PdfNumber(this.width));
            decodeParams.items.setValue(this.dictionaryProperties.blackIs1, new pdf_boolean_1.PdfBoolean(true));
            decodeParams.items.setValue(this.dictionaryProperties.k, new pdf_number_1.PdfNumber(-1));
            decodeParams.items.setValue(this.dictionaryProperties.predictor, new pdf_number_1.PdfNumber(15));
            decodeParams.items.setValue(this.dictionaryProperties.bitsPerComponent, new pdf_number_1.PdfNumber(this.bitsPerComponent));
            return decodeParams;
        };
        ImageDecoder.prototype.readExceededJPGImage = function (stream) {
            this.mStream = stream;
            var isContinueReading = true;
            while (isContinueReading) {
                var marker = this.getMarker(stream);
                switch (marker) {
                    case this.sof1Marker:
                    case this.sof2Marker:
                    case this.sof3Marker:
                    case this.sof5Marker:
                    case this.sof6Marker:
                    case this.sof7Marker:
                    case this.sof9Marker:
                    case this.sof10Marker:
                    case this.sof11Marker:
                    case this.sof13Marker:
                    case this.sof14Marker:
                    case this.sof15Marker:
                        stream.position += 3;
                        this.mHeight = this.mStream.readNextTwoBytes(stream);
                        this.mWidth = this.mStream.readNextTwoBytes(stream);
                        isContinueReading = false;
                        break;
                    default:
                        this.skipStream(stream);
                        break;
                }
            }
        };
        ImageDecoder.prototype.skip = function (stream, noOfBytes) {
            this.mStream = stream;
            var temp = new byte_array_1.ByteArray(noOfBytes);
            this.mStream.read(temp, 0, temp.count);
        };
        ImageDecoder.prototype.getMarker = function (stream) {
            var skippedByte = 0;
            var marker = 32;
            marker = stream.readByte(this.mStream.position);
            stream.position++;
            while (marker !== 255) {
                skippedByte++;
                marker = stream.readByte(this.mStream.position);
                stream.position++;
            }
            do {
                marker = stream.readByte(this.mStream.position);
                stream.position++;
            } while (marker === 255);
            return marker;
        };
        ImageDecoder.prototype.skipStream = function (stream) {
            var markerLength = this.mStream.readNextTwoBytes(stream) - 2;
            if (markerLength > 0) {
                stream.position += markerLength;
            }
        };
        ImageDecoder.mPngHeader = [137, 80, 78, 71, 13, 10, 26, 10];
        ImageDecoder.mJpegHeader = [255, 216];
        ImageDecoder.GIF_HEADER = 'G,I,F,8';
        ImageDecoder.BMP_HEADER = 'B,M';
        return ImageDecoder;
    }());
    exports.ImageDecoder = ImageDecoder;
});