Skip to content

Commit 4f74ee7

Browse files
committed
fix: detect endianess of the platform in decoder
1 parent d920bc0 commit 4f74ee7

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

‎src/PNGDecoder.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {pngSignature, crc} from './common';
55
const empty = new Uint8Array(0);
66
const NULL = '\0';
77

8+
const uint16 = new Uint16Array([0x00ff]);
9+
const uint8 = new Uint8Array(uint16.buffer);
10+
const osIsLittleEndian = uint8[0] === 0xff;
11+
812
export default class PNGDecoder extends IOBuffer {
913
constructor(data, options) {
1014
super(data);
@@ -215,9 +219,11 @@ export default class PNGDecoder extends IOBuffer {
215219

216220
if (this._png.bitDepth === 16) {
217221
const uint16Data = new Uint16Array(newData.buffer);
218-
for (var k = 0; k < uint16Data.length; k++) {
219-
// PNG is in big endian. Return to little endian.
220-
uint16Data[k] = swap16(uint16Data[k]);
222+
if (osIsLittleEndian) {
223+
for (var k = 0; k < uint16Data.length; k++) {
224+
// PNG is always big endian. Swap the bytes.
225+
uint16Data[k] = swap16(uint16Data[k]);
226+
}
221227
}
222228
this._png.data = uint16Data;
223229
} else {

0 commit comments

Comments
 (0)