Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.

Commit dfb5f93

Browse files
committed
Avoid code duplication in copy image data routine
1 parent b7edc73 commit dfb5f93

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

lib/image/png.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,15 @@ class PNGImage {
118118

119119
let i = p = a = 0;
120120
const len = pixels.length;
121-
if (this.image.bits === 8) {
122-
while (i < len) {
123-
for (let colorIndex = 0; colorIndex < colorCount; colorIndex++) {
124-
imgData[p++] = pixels[i++];
125-
}
126-
alphaChannel[a++] = pixels[i++];
127-
}
128-
} else {
129-
// copy only most significant byte (MSB) - PNG data is always stored in network byte order (MSB first)
130-
while (i < len) {
131-
for (let colorIndex = 0; colorIndex < colorCount; colorIndex++) {
132-
imgData[p++] = pixels[i++];
133-
i++;
134-
}
135-
alphaChannel[a++] = pixels[i++];
136-
i++;
121+
// For 16bit images copy only most significant byte (MSB) - PNG data is always stored in network byte order (MSB first)
122+
const skipByteCount = this.image.bits === 16 ? 1 : 0;
123+
while (i < len) {
124+
for (let colorIndex = 0; colorIndex < colorCount; colorIndex++) {
125+
imgData[p++] = pixels[i++];
126+
i += skipByteCount;
137127
}
128+
alphaChannel[a++] = pixels[i++];
129+
i += skipByteCount;
138130
}
139131

140132
this.imgData = zlib.deflateSync(imgData);

0 commit comments

Comments
 (0)