Skip to content

Commit fea4507

Browse files
committed
Create color maps for single component color spaces.
1 parent 921f321 commit fea4507

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/colorspace.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,35 @@ var ColorSpace = (function ColorSpaceClosure() {
7474
if (this.isPassthrough(bits)) {
7575
return src.subarray(srcOffset);
7676
}
77-
var destLength = this.getOutputLength(count * this.numComps);
78-
var dest = new Uint8Array(destLength);
77+
var dest = new Uint8Array(count * 3);
78+
var numComponentColors = 1 << bits;
79+
// Optimization: create a color map when there is just one component and
80+
// we are converting more colors than the size of the color map. We
81+
// don't build the map if the colorspace is gray or rgb since those
82+
// methods are faster than building a map. This mainly offers big speed
83+
// ups for indexed and alternate colorspaces.
84+
if (this.numComps === 1 && count > numComponentColors &&
85+
this.name !== 'DeviceGray' && this.name !== 'DeviceRGB') {
86+
// TODO it may be worth while to cache the color map. While running
87+
// testing I never hit a cache so I will leave that out for now (perhaps
88+
// we are reparsing colorspaces too much?).
89+
var allColors = bits <= 8 ? new Uint8Array(numComponentColors) :
90+
new Uint16Array(numComponentColors);
91+
for (var i = 0; i < numComponentColors; i++) {
92+
allColors[i] = i;
93+
}
94+
var colorMap = new Uint8Array(numComponentColors * 3);
95+
this.getRgbBuffer(allColors, 0, numComponentColors, colorMap, 0, bits);
96+
97+
var destOffset = 0;
98+
for (var i = 0; i < count; ++i) {
99+
var key = src[srcOffset++] * 3;
100+
dest[destOffset++] = colorMap[key];
101+
dest[destOffset++] = colorMap[key + 1];
102+
dest[destOffset++] = colorMap[key + 2];
103+
}
104+
return dest;
105+
}
79106
this.getRgbBuffer(src, srcOffset, count, dest, 0, bits);
80107
return dest;
81108
}

0 commit comments

Comments
 (0)