@@ -74,8 +74,35 @@ var ColorSpace = (function ColorSpaceClosure() {
74
74
if (this .isPassthrough (bits )) {
75
75
return src .subarray (srcOffset );
76
76
}
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
+ }
79
106
this .getRgbBuffer (src , srcOffset , count , dest , 0 , bits );
80
107
return dest ;
81
108
}
0 commit comments