Browse Source

Merge pull request #7863 from timvandermeij/colorspace

Colorspace: refactoring to prevent unnecessary creation of intermediate arrays
Jonas Jenwald 8 years ago committed by GitHub
parent
commit
94ddd8f61d
  1. 57
      src/core/colorspace.js

57
src/core/colorspace.js

@ -294,13 +294,8 @@ var ColorSpace = (function ColorSpaceClosure() {
} }
cs = xref.fetchIfRef(cs); cs = xref.fetchIfRef(cs);
var mode;
if (isName(cs)) { if (isName(cs)) {
mode = cs.name; switch (cs.name) {
this.mode = mode;
switch (mode) {
case 'DeviceGray': case 'DeviceGray':
case 'G': case 'G':
return 'DeviceGrayCS'; return 'DeviceGrayCS';
@ -313,11 +308,10 @@ var ColorSpace = (function ColorSpaceClosure() {
case 'Pattern': case 'Pattern':
return ['PatternCS', null]; return ['PatternCS', null];
default: default:
error('unrecognized colorspace ' + mode); error('unrecognized colorspace ' + cs.name);
} }
} else if (isArray(cs)) { } else if (isArray(cs)) {
mode = xref.fetchIfRef(cs[0]).name; var mode = xref.fetchIfRef(cs[0]).name;
this.mode = mode;
var numComps, params, alt, whitePoint, blackPoint, gamma; var numComps, params, alt, whitePoint, blackPoint, gamma;
switch (mode) { switch (mode) {
@ -384,12 +378,7 @@ var ColorSpace = (function ColorSpaceClosure() {
case 'Separation': case 'Separation':
case 'DeviceN': case 'DeviceN':
var name = xref.fetchIfRef(cs[1]); var name = xref.fetchIfRef(cs[1]);
numComps = 1; numComps = isArray(name) ? name.length : 1;
if (isName(name)) {
numComps = 1;
} else if (isArray(name)) {
numComps = name.length;
}
alt = ColorSpace.parseToIR(cs[2], xref, res); alt = ColorSpace.parseToIR(cs[2], xref, res);
var tintFnIR = PDFFunction.getIR(xref, xref.fetchIfRef(cs[3])); var tintFnIR = PDFFunction.getIR(xref, xref.fetchIfRef(cs[3]));
return ['AlternateCS', numComps, alt, tintFnIR]; return ['AlternateCS', numComps, alt, tintFnIR];
@ -492,26 +481,22 @@ var AlternateCS = (function AlternateCSClosure() {
var scaled = new Float32Array(numComps); var scaled = new Float32Array(numComps);
var tinted = new Float32Array(baseNumComps); var tinted = new Float32Array(baseNumComps);
var i, j; var i, j;
if (usesZeroToOneRange) {
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
for (j = 0; j < numComps; j++) { for (j = 0; j < numComps; j++) {
scaled[j] = src[srcOffset++] * scale; scaled[j] = src[srcOffset++] * scale;
} }
tintFn(scaled, 0, tinted, 0); tintFn(scaled, 0, tinted, 0);
if (usesZeroToOneRange) {
for (j = 0; j < baseNumComps; j++) { for (j = 0; j < baseNumComps; j++) {
baseBuf[pos++] = tinted[j] * 255; baseBuf[pos++] = tinted[j] * 255;
} }
}
} else { } else {
for (i = 0; i < count; i++) {
for (j = 0; j < numComps; j++) {
scaled[j] = src[srcOffset++] * scale;
}
tintFn(scaled, 0, tinted, 0);
base.getRgbItem(tinted, 0, baseBuf, pos); base.getRgbItem(tinted, 0, baseBuf, pos);
pos += baseNumComps; pos += baseNumComps;
} }
} }
if (!isPassthrough) { if (!isPassthrough) {
base.getRgbBuffer(baseBuf, 0, count, dest, destOffset, 8, alpha01); base.getRgbBuffer(baseBuf, 0, count, dest, destOffset, 8, alpha01);
} }
@ -547,29 +532,27 @@ var IndexedCS = (function IndexedCSClosure() {
function IndexedCS(base, highVal, lookup) { function IndexedCS(base, highVal, lookup) {
this.name = 'Indexed'; this.name = 'Indexed';
this.numComps = 1; this.numComps = 1;
this.defaultColor = new Uint8Array([0]); this.defaultColor = new Uint8Array(this.numComps);
this.base = base; this.base = base;
this.highVal = highVal; this.highVal = highVal;
var baseNumComps = base.numComps; var baseNumComps = base.numComps;
var length = baseNumComps * highVal; var length = baseNumComps * highVal;
var lookupArray;
if (isStream(lookup)) { if (isStream(lookup)) {
lookupArray = new Uint8Array(length); this.lookup = new Uint8Array(length);
var bytes = lookup.getBytes(length); var bytes = lookup.getBytes(length);
lookupArray.set(bytes); this.lookup.set(bytes);
} else if (isString(lookup)) { } else if (isString(lookup)) {
lookupArray = new Uint8Array(length); this.lookup = new Uint8Array(length);
for (var i = 0; i < length; ++i) { for (var i = 0; i < length; ++i) {
lookupArray[i] = lookup.charCodeAt(i); this.lookup[i] = lookup.charCodeAt(i);
} }
} else if (lookup instanceof Uint8Array || lookup instanceof Array) { } else if (lookup instanceof Uint8Array || lookup instanceof Array) {
lookupArray = lookup; this.lookup = lookup;
} else { } else {
error('Unrecognized lookup table: ' + lookup); error('Unrecognized lookup table: ' + lookup);
} }
this.lookup = lookupArray;
} }
IndexedCS.prototype = { IndexedCS.prototype = {
@ -613,7 +596,7 @@ var DeviceGrayCS = (function DeviceGrayCSClosure() {
function DeviceGrayCS() { function DeviceGrayCS() {
this.name = 'DeviceGray'; this.name = 'DeviceGray';
this.numComps = 1; this.numComps = 1;
this.defaultColor = new Float32Array([0]); this.defaultColor = new Float32Array(this.numComps);
} }
DeviceGrayCS.prototype = { DeviceGrayCS.prototype = {
@ -655,7 +638,7 @@ var DeviceRgbCS = (function DeviceRgbCSClosure() {
function DeviceRgbCS() { function DeviceRgbCS() {
this.name = 'DeviceRGB'; this.name = 'DeviceRGB';
this.numComps = 3; this.numComps = 3;
this.defaultColor = new Float32Array([0, 0, 0]); this.defaultColor = new Float32Array(this.numComps);
} }
DeviceRgbCS.prototype = { DeviceRgbCS.prototype = {
getRgb: ColorSpace.prototype.getRgb, getRgb: ColorSpace.prototype.getRgb,
@ -748,7 +731,9 @@ var DeviceCmykCS = (function DeviceCmykCSClosure() {
function DeviceCmykCS() { function DeviceCmykCS() {
this.name = 'DeviceCMYK'; this.name = 'DeviceCMYK';
this.numComps = 4; this.numComps = 4;
this.defaultColor = new Float32Array([0, 0, 0, 1]); this.defaultColor = new Float32Array(this.numComps);
// Set the fourth component to the maximum value for a black color.
this.defaultColor[3] = 1;
} }
DeviceCmykCS.prototype = { DeviceCmykCS.prototype = {
getRgb: ColorSpace.prototype.getRgb, getRgb: ColorSpace.prototype.getRgb,
@ -788,7 +773,7 @@ var CalGrayCS = (function CalGrayCSClosure() {
function CalGrayCS(whitePoint, blackPoint, gamma) { function CalGrayCS(whitePoint, blackPoint, gamma) {
this.name = 'CalGray'; this.name = 'CalGray';
this.numComps = 1; this.numComps = 1;
this.defaultColor = new Float32Array([0]); this.defaultColor = new Float32Array(this.numComps);
if (!whitePoint) { if (!whitePoint) {
error('WhitePoint missing - required for color space CalGray'); error('WhitePoint missing - required for color space CalGray');
@ -911,7 +896,7 @@ var CalRGBCS = (function CalRGBCSClosure() {
function CalRGBCS(whitePoint, blackPoint, gamma, matrix) { function CalRGBCS(whitePoint, blackPoint, gamma, matrix) {
this.name = 'CalRGB'; this.name = 'CalRGB';
this.numComps = 3; this.numComps = 3;
this.defaultColor = new Float32Array(3); this.defaultColor = new Float32Array(this.numComps);
if (!whitePoint) { if (!whitePoint) {
error('WhitePoint missing - required for color space CalRGB'); error('WhitePoint missing - required for color space CalRGB');
@ -1187,7 +1172,7 @@ var LabCS = (function LabCSClosure() {
function LabCS(whitePoint, blackPoint, range) { function LabCS(whitePoint, blackPoint, range) {
this.name = 'Lab'; this.name = 'Lab';
this.numComps = 3; this.numComps = 3;
this.defaultColor = new Float32Array([0, 0, 0]); this.defaultColor = new Float32Array(this.numComps);
if (!whitePoint) { if (!whitePoint) {
error('WhitePoint missing - required for color space Lab'); error('WhitePoint missing - required for color space Lab');

Loading…
Cancel
Save