@syncfusion/ej2-pdf
Version:
Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.
156 lines (155 loc) • 4.94 kB
JavaScript
import { _ImageFormat } from './../../enumerator';
/**
* Base class for image decoders that read bytes from an underlying stream,
* parse headers/metadata, and expose decoded image data and properties for PDF embedding.
* Concrete implementations must provide header parsing and dictionary creation.
*
* @private
*/
var _ImageDecoder = /** @class */ (function () {
function _ImageDecoder() {
/**
* Detected or assigned image format.
*
* @private
*/
this._format = _ImageFormat.unknown;
/**
* Image height in pixels.
*
* @private
*/
this._height = 0;
/**
* Image width in pixels.
*
* @private
*/
this._width = 0;
/**
* Number of bits per color component.
*
* @private
*/
this._bitsPerComponent = 8;
/**
* Current read position within the source stream.
*
* @private
*/
this._position = 0;
/**
* Number of color components in the decoded image.
*
* @private
*/
this._noOfComponents = -1;
}
/**
* Resets the stream read position to the beginning.
*
* @private
* @returns {void}
*/
_ImageDecoder.prototype._reset = function () {
this._position = 0;
};
/**
* Reads a single byte from the underlying stream at the specified index without moving the cursor.
*
* @private
* @param {number} index The zero based position in the stream to read from.
* @returns {number} The byte value at the given index.
*/
_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++;
}
}
};
/**
* Reads a string of the specified length from the current stream position using 8 bit char codes.
*
* @private
* @param {number} length The number of characters to read.
* @returns {string} The decoded string.
*/
_ImageDecoder.prototype._readString = function (length) {
var result = '';
for (var i = 0; i < length; i++) {
result += String.fromCharCode(this._readByte());
}
return result;
};
/**
* Advances the current read position by the specified number of bytes.
*
* @private
* @param {number} length The number of bytes to skip forward.
* @returns {void}
*/
_ImageDecoder.prototype._seek = function (length) {
this._position += length;
};
/**
* Reads a single byte from the current stream position and advances the cursor.
*
* @private
* @returns {number} The next byte value.
* @throws {Error} Throws if attempting to read beyond the end of the stream.
*/
_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.');
}
};
/**
* Converts a number to an unsigned 16 bit value.
*
* @private
* @param {number} value The input value to convert.
* @returns {number} The unsigned 16 bit representation.
*/
_ImageDecoder.prototype._toUnsigned16 = function (value) {
value = value & 0xFFFF;
return value < 0 ? (value + 0x10000) : value;
};
/**
* Reads a 32 bit unsigned integer from the stream at the specified offset.
*
* @private
* @param {number} offset The byte offset at which to read the 32 bit value.
* @returns {number} The 32 bit unsigned integer.
*/
_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;
}());
export { _ImageDecoder };