@@ -100,10 +100,10 @@ module.exports = {
100
100
101
101
const width = view . getUint32 ( 16 ) ;
102
102
const height = view . getUint32 ( 20 ) ;
103
- const bpc = array [ 24 ] ;
104
- const pixel_type = array [ 25 ] ;
105
- let channels = ( { 3 : 1 , 0 : 1 , 4 : 2 , 2 : 3 , 6 : 4 } ) [ pixel_type ] ;
106
- const bytespp = channels * bpc / 8 ;
103
+ let bit_depth = array [ 24 ] ;
104
+ const color_type = array [ 25 ] ;
105
+ let channels = ( { 3 : 1 , 0 : 1 , 4 : 2 , 2 : 3 , 6 : 4 } ) [ color_type ] ;
106
+ const bytespp = channels * bit_depth / 8 ;
107
107
108
108
const row_length = width * bytespp ;
109
109
let pixels = new Uint8Array ( height * row_length ) ;
@@ -156,24 +156,25 @@ module.exports = {
156
156
p_offset += row_length ;
157
157
}
158
158
159
- if ( pixel_type === 3 ) {
159
+ if ( color_type === 3 ) {
160
160
if ( ! palette )
161
161
throw new Error ( 'Indexed color PNG has no PLTE' ) ;
162
162
163
163
if ( alphaPalette )
164
164
for ( let i = 0 ; i < alphaPalette . length ; i ++ )
165
165
palette [ i ] &= 0xffffff00 | alphaPalette [ i ] ;
166
166
167
- channels = 4 ;
168
167
const newPixels = new Uint8Array ( width * height * 4 ) ;
169
168
const pixelView = new DataView ( newPixels . buffer , newPixels . byteOffset , newPixels . byteLength ) ;
170
- for ( let i = 0 ; i < pixels . length ; i ++ )
171
- pixelView . setUint32 ( i * 4 , palette [ pixels [ i ] ] , false ) ;
169
+ for ( let i = 0 ; i < pixels . length * ( 8 / bit_depth ) ; i ++ )
170
+ pixelView . setUint32 ( i * 4 , palette [ pixels [ ~ ~ ( i / ( 8 / bit_depth ) ) ] & ( 2 ** bit_depth - 1 ) ] , false ) ;
171
+ channels = 4 ;
172
+ bit_depth = 8 ;
172
173
pixels = newPixels ;
173
174
}
174
175
175
- if ( bpc !== 8 ) {
176
- const newPixels = new Uint8Array ( pixels . length / bpc * 8 ) ;
176
+ if ( bit_depth !== 8 ) {
177
+ const newPixels = new Uint8Array ( pixels . length / bit_depth * 8 ) ;
177
178
for ( let i = 0 ; i < pixels . length ; i += 2 )
178
179
newPixels [ i / 2 ] = pixels [ i ] ;
179
180
pixels = newPixels ;
@@ -254,4 +255,4 @@ module.exports = {
254
255
}
255
256
}
256
257
}
257
- } ;
258
+ } ;
0 commit comments