Makes new alternate colorspace which handles separation and device.

This commit is contained in:
Brendan Dahl 2011-11-10 14:06:42 -08:00
parent a12419c084
commit c198ec4323

View File

@ -24,7 +24,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); var IR = constructor.parseToIR(cs, xref, res);
if (IR instanceof SeparationCS) if (IR instanceof AlternateCS)
return IR; return IR;
return constructor.fromIR(IR); return constructor.fromIR(IR);
@ -50,11 +50,12 @@ var ColorSpace = (function colorSpaceColorSpace() {
var hiVal = IR[2]; var hiVal = IR[2];
var lookup = IR[3]; var lookup = IR[3];
return new IndexedCS(ColorSpace.fromIR(baseIndexedCS), hiVal, lookup); return new IndexedCS(ColorSpace.fromIR(baseIndexedCS), hiVal, lookup);
case 'SeparationCS': case 'AlternateCS':
var alt = IR[1]; var numComps = IR[1];
var tintFnIR = IR[2]; var alt = IR[2];
var tintFnIR = IR[3];
return new SeparationCS(ColorSpace.fromIR(alt), return new AlternateCS(numComps, ColorSpace.fromIR(alt),
PDFFunction.fromIR(tintFnIR)); PDFFunction.fromIR(tintFnIR));
default: default:
error('Unkown name ' + name); error('Unkown name ' + name);
@ -134,16 +135,16 @@ var ColorSpace = (function colorSpaceColorSpace() {
var lookup = xref.fetchIfRef(cs[3]); var lookup = xref.fetchIfRef(cs[3]);
return ['IndexedCS', baseIndexedCS, hiVal, lookup]; return ['IndexedCS', baseIndexedCS, hiVal, lookup];
case 'Separation': case 'Separation':
var alt = ColorSpace.parseToIR(cs[2], xref, res);
var tintFnIR = PDFFunction.getIR(xref, xref.fetchIfRef(cs[3]));
return ['SeparationCS', alt, tintFnIR];
case 'DeviceN': case 'DeviceN':
if (cs.length > 4) var name = cs[1];
TODO('devicen color with n channels'); var numComps = 1;
debugger; if (isName(name))
numComps = 1;
else if (isArray(name))
numComps = name.length;
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]));
return ['SeparationCS', alt, tintFnIR]; return ['AlternateCS', numComps, alt, tintFnIR];
case 'Lab': case 'Lab':
default: default:
error('unimplemented color space object "' + mode + '"'); error('unimplemented color space object "' + mode + '"');
@ -157,33 +158,37 @@ var ColorSpace = (function colorSpaceColorSpace() {
return constructor; return constructor;
})(); })();
var SeparationCS = (function separationCS() { var AlternateCS = (function alternateCS() {
function constructor(base, tintFn) { function constructor(numComps, base, tintFn) {
this.name = 'Separation'; this.name = 'Alternate';
this.numComps = 1; this.numComps = numComps;
this.defaultColor = [1]; this.defaultColor = [1];
this.base = base; this.base = base;
this.tintFn = tintFn; this.tintFn = tintFn;
} }
constructor.prototype = { constructor.prototype = {
getRgb: function sepcs_getRgb(color) { getRgb: function altcs_getRgb(color) {
var tinted = this.tintFn(color); var tinted = this.tintFn(color);
return this.base.getRgb(tinted); return this.base.getRgb(tinted);
}, },
getRgbBuffer: function sepcs_getRgbBuffer(input, bits) { getRgbBuffer: function altcs_getRgbBuffer(input, bits) {
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 baseNumComps = base.numComps;
var baseBuf = new Uint8Array(numComps * length); var baseBuf = new Uint8Array(baseNumComps * length);
var numComps = this.numComps;
var scaled = new Array(numComps);
for (var i = 0; i < length; ++i) { for (var i = 0; i < length; i += numComps) {
var scaled = input[i] * scale; for (var z = 0; z < numComps; ++z)
var tinted = tintFn([scaled]); scaled[z] = input[i + z] * scale;
for (var j = 0; j < numComps; ++j)
var tinted = tintFn(scaled);
for (var j = 0; j < baseNumComps; ++j)
baseBuf[pos++] = 255 * tinted[j]; baseBuf[pos++] = 255 * tinted[j];
} }
return base.getRgbBuffer(baseBuf, 8); return base.getRgbBuffer(baseBuf, 8);