Refactor colorspace.js and fix jslint warnings.

This commit is contained in:
Kalervo Kujala 2011-11-05 22:13:16 +02:00
parent 33af12abd0
commit cac9044161

View File

@ -23,7 +23,7 @@ var ColorSpace = (function colorSpaceColorSpace() {
}; };
constructor.parse = function colorSpaceParse(cs, xref, res) { constructor.parse = function colorSpaceParse(cs, xref, res) {
var IR = constructor.parseToIR(cs, xref, res, true); var IR = constructor.parseToIR(cs, xref, res);
if (IR instanceof SeparationCS) if (IR instanceof SeparationCS)
return IR; return IR;
@ -31,12 +31,7 @@ var ColorSpace = (function colorSpaceColorSpace() {
}; };
constructor.fromIR = function colorSpaceFromIR(IR) { constructor.fromIR = function colorSpaceFromIR(IR) {
var name; var name = isArray(IR) ? IR[0] : IR;
if (isArray(IR)) {
name = IR[0];
} else {
name = IR;
}
switch (name) { switch (name) {
case 'DeviceGrayCS': case 'DeviceGrayCS':
@ -46,33 +41,28 @@ var ColorSpace = (function colorSpaceColorSpace() {
case 'DeviceCmykCS': case 'DeviceCmykCS':
return new DeviceCmykCS(); return new DeviceCmykCS();
case 'PatternCS': case 'PatternCS':
var baseCS = IR[1]; var basePatternCS = IR[1];
if (baseCS == null) { if (basePatternCS)
return new PatternCS(null); basePatternCS = ColorSpace.fromIR(basePatternCS);
} else { return new PatternCS(basePatternCS);
return new PatternCS(ColorSpace.fromIR(baseCS));
}
case 'IndexedCS': case 'IndexedCS':
var baseCS = IR[1]; var baseIndexedCS = IR[1];
var hiVal = IR[2]; var hiVal = IR[2];
var lookup = IR[3]; var lookup = IR[3];
return new IndexedCS(ColorSpace.fromIR(baseCS), hiVal, lookup); return new IndexedCS(ColorSpace.fromIR(baseIndexedCS), hiVal, lookup);
case 'SeparationCS': case 'SeparationCS':
var alt = IR[1]; var alt = IR[1];
var tintFnIR = IR[2]; var tintFnIR = IR[2];
return new SeparationCS( return new SeparationCS(ColorSpace.fromIR(alt),
ColorSpace.fromIR(alt), PDFFunction.fromIR(tintFnIR));
PDFFunction.fromIR(tintFnIR)
);
default: default:
error('Unkown name ' + name); error('Unkown name ' + name);
} }
return null; return null;
} };
constructor.parseToIR = function colorSpaceParseToIR(cs, xref, res, constructor.parseToIR = function colorSpaceParseToIR(cs, xref, res) {
parseOnly) {
if (isName(cs)) { if (isName(cs)) {
var colorSpaces = xref.fetchIfRef(res.get('ColorSpace')); var colorSpaces = xref.fetchIfRef(res.get('ColorSpace'));
if (isDict(colorSpaces)) { if (isDict(colorSpaces)) {
@ -83,9 +73,10 @@ var ColorSpace = (function colorSpaceColorSpace() {
} }
cs = xref.fetchIfRef(cs); cs = xref.fetchIfRef(cs);
var mode;
if (isName(cs)) { if (isName(cs)) {
var mode = cs.name; mode = cs.name;
this.mode = mode; this.mode = mode;
switch (mode) { switch (mode) {
@ -104,7 +95,7 @@ var ColorSpace = (function colorSpaceColorSpace() {
error('unrecognized colorspace ' + mode); error('unrecognized colorspace ' + mode);
} }
} else if (isArray(cs)) { } else if (isArray(cs)) {
var mode = cs[0].name; mode = cs[0].name;
this.mode = mode; this.mode = mode;
switch (mode) { switch (mode) {
@ -133,15 +124,15 @@ var ColorSpace = (function colorSpaceColorSpace() {
return 'DeviceCmykCS'; return 'DeviceCmykCS';
break; break;
case 'Pattern': case 'Pattern':
var baseCS = cs[1]; var basePatternCS = cs[1];
if (baseCS) if (basePatternCS)
baseCS = ColorSpace.parseToIR(baseCS, xref, res); basePatternCS = ColorSpace.parseToIR(basePatternCS, xref, res);
return ['PatternCS', baseCS]; return ['PatternCS', basePatternCS];
case 'Indexed': case 'Indexed':
var baseCS = ColorSpace.parseToIR(cs[1], xref, res); var baseIndexedCS = ColorSpace.parseToIR(cs[1], xref, res);
var hiVal = cs[2] + 1; var hiVal = cs[2] + 1;
var lookup = xref.fetchIfRef(cs[3]); var lookup = xref.fetchIfRef(cs[3]);
return ['IndexedCS', baseCS, hiVal, lookup]; return ['IndexedCS', baseIndexedCS, hiVal, lookup];
case 'Separation': case 'Separation':
var alt = ColorSpace.parseToIR(cs[2], xref, res); var 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]));
@ -165,7 +156,6 @@ var SeparationCS = (function separationCS() {
this.name = 'Separation'; this.name = 'Separation';
this.numComps = 1; this.numComps = 1;
this.defaultColor = [1]; this.defaultColor = [1];
this.base = base; this.base = base;
this.tintFn = tintFn; this.tintFn = tintFn;
} }
@ -179,12 +169,11 @@ var SeparationCS = (function separationCS() {
var tintFn = this.tintFn; var tintFn = this.tintFn;
var base = this.base; var base = this.base;
var scale = 1 / ((1 << bits) - 1); var scale = 1 / ((1 << bits) - 1);
var length = input.length; var length = input.length;
var pos = 0; var pos = 0;
var numComps = base.numComps; var numComps = base.numComps;
var baseBuf = new Uint8Array(numComps * length); var baseBuf = new Uint8Array(numComps * length);
for (var i = 0; i < length; ++i) { for (var i = 0; i < length; ++i) {
var scaled = input[i] * scale; var scaled = input[i] * scale;
var tinted = tintFn([scaled]); var tinted = tintFn([scaled]);
@ -213,13 +202,13 @@ var IndexedCS = (function indexedCS() {
this.name = 'Indexed'; this.name = 'Indexed';
this.numComps = 1; this.numComps = 1;
this.defaultColor = [0]; this.defaultColor = [0];
this.base = base; this.base = base;
var baseNumComps = base.numComps;
this.highVal = highVal; this.highVal = highVal;
var baseNumComps = base.numComps;
var length = baseNumComps * highVal; var length = baseNumComps * highVal;
var lookupArray = new Uint8Array(length); var lookupArray = new Uint8Array(length);
if (isStream(lookup)) { if (isStream(lookup)) {
var bytes = lookup.getBytes(length); var bytes = lookup.getBytes(length);
lookupArray.set(bytes); lookupArray.set(bytes);
@ -235,7 +224,6 @@ var IndexedCS = (function indexedCS() {
constructor.prototype = { constructor.prototype = {
getRgb: function indexcs_getRgb(color) { getRgb: function indexcs_getRgb(color) {
var numComps = this.base.numComps; var numComps = this.base.numComps;
var start = color[0] * numComps; var start = color[0] * numComps;
var c = []; var c = [];
@ -249,9 +237,9 @@ var IndexedCS = (function indexedCS() {
var numComps = base.numComps; var numComps = base.numComps;
var lookup = this.lookup; var lookup = this.lookup;
var length = input.length; var length = input.length;
var baseBuf = new Uint8Array(length * numComps); var baseBuf = new Uint8Array(length * numComps);
var baseBufPos = 0; var baseBufPos = 0;
for (var i = 0; i < length; ++i) { for (var i = 0; i < length; ++i) {
var lookupPos = input[i] * numComps; var lookupPos = input[i] * numComps;
for (var j = 0; j < numComps; ++j) { for (var j = 0; j < numComps; ++j) {
@ -294,7 +282,7 @@ var DeviceGrayCS = (function deviceGrayCS() {
})(); })();
var DeviceRgbCS = (function deviceRgbCS() { var DeviceRgbCS = (function deviceRgbCS() {
function constructor(bits) { function constructor() {
this.name = 'DeviceRGB'; this.name = 'DeviceRGB';
this.numComps = 3; this.numComps = 3;
this.defaultColor = [0, 0, 0]; this.defaultColor = [0, 0, 0];
@ -337,41 +325,41 @@ var DeviceCmykCS = (function deviceCmykCS() {
r += 0.1373 * x; r += 0.1373 * x;
g += 0.1216 * x; g += 0.1216 * x;
b += 0.1255 * x; b += 0.1255 * x;
x = c1 * m1 * y * k1; // 0 0 1 0 x = c1 * m1 * y * k1; // 0 0 1 0
r += x; r += x;
g += 0.9490 * x; g += 0.9490 * x;
x = c1 * m1 * y * k; // 0 0 1 1 x = c1 * m1 * y * k; // 0 0 1 1
r += 0.1098 * x; r += 0.1098 * x;
g += 0.1020 * x; g += 0.1020 * x;
x = c1 * m * y1 * k1; // 0 1 0 0 x = c1 * m * y1 * k1; // 0 1 0 0
r += 0.9255 * x; r += 0.9255 * x;
b += 0.5490 * x; b += 0.5490 * x;
x = c1 * m * y1 * k; // 0 1 0 1 x = c1 * m * y1 * k; // 0 1 0 1
r += 0.1412 * x; r += 0.1412 * x;
x = c1 * m * y * k1; // 0 1 1 0 x = c1 * m * y * k1; // 0 1 1 0
r += 0.9294 * x; r += 0.9294 * x;
g += 0.1098 * x; g += 0.1098 * x;
b += 0.1412 * x; b += 0.1412 * x;
x = c1 * m * y * k; // 0 1 1 1 x = c1 * m * y * k; // 0 1 1 1
r += 0.1333 * x; r += 0.1333 * x;
x = c * m1 * y1 * k1; // 1 0 0 0 x = c * m1 * y1 * k1; // 1 0 0 0
g += 0.6784 * x; g += 0.6784 * x;
b += 0.9373 * x; b += 0.9373 * x;
x = c * m1 * y1 * k; // 1 0 0 1 x = c * m1 * y1 * k; // 1 0 0 1
g += 0.0588 * x; g += 0.0588 * x;
b += 0.1412 * x; b += 0.1412 * x;
x = c * m1 * y * k1; // 1 0 1 0 x = c * m1 * y * k1; // 1 0 1 0
g += 0.6510 * x; g += 0.6510 * x;
b += 0.3137 * x; b += 0.3137 * x;
x = c * m1 * y * k; // 1 0 1 1 x = c * m1 * y * k; // 1 0 1 1
g += 0.0745 * x; g += 0.0745 * x;
x = c * m * y1 * k1; // 1 1 0 0 x = c * m * y1 * k1; // 1 1 0 0
r += 0.1804 * x; r += 0.1804 * x;
g += 0.1922 * x; g += 0.1922 * x;
b += 0.5725 * x; b += 0.5725 * x;
x = c * m * y1 * k; // 1 1 0 1 x = c * m * y1 * k; // 1 1 0 1
b += 0.0078 * x; b += 0.0078 * x;
x = c * m * y * k1; // 1 1 1 0 x = c * m * y * k1; // 1 1 1 0
r += 0.2118 * x; r += 0.2118 * x;
g += 0.2119 * x; g += 0.2119 * x;
b += 0.2235 * x; b += 0.2235 * x;