From da8f0120a5d4a2917acf860acc3453423ad27626 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 7 Jul 2011 09:38:58 -0700 Subject: [PATCH 1/9] working separation cs --- pdf.js | 101 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 28 deletions(-) diff --git a/pdf.js b/pdf.js index 209027537..3829df975 100644 --- a/pdf.js +++ b/pdf.js @@ -1613,7 +1613,7 @@ var CCITTFaxStream = (function() { } if (this.byteAlign) - inputBits &= ~7; + this.inputBits &= ~7; var gotEOL = false; @@ -4601,8 +4601,14 @@ var ColorSpace = (function() { var hiVal = cs[2] + 1; var lookup = xref.fetchIfRef(cs[3]); return new IndexedCS(base, hiVal, lookup); + break; + case 'Separation': + var name = cs[1]; + var alt = ColorSpace.parse(cs[2], xref, res); + var tintFn = new PDFFunction(xref, xref.fetchIfRef(cs[3])); + return new TintCS("Separation", alt, tintFn); + break; case 'Lab': - case 'Seperation': case 'DeviceN': default: error("unrecognized color space object '" + mode + "'"); @@ -4615,6 +4621,44 @@ var ColorSpace = (function() { return constructor; })(); +var TintCS = (function() { + function constructor(name, base, tintFn) { + this.name = name; + this.numComps = 1; + this.defaultColor = [0]; + + this.base = base; + this.tintFn = tintFn; + } + + constructor.prototype = { + getRgb: function tintcs_getRgb(color) { + var tinted = this.tintFn.func(color); + return this.base.getRgb(tinted); + }, + getRgbBuffer: function tintcs_getRgbBuffer(input) { + var tintFn = this.tintFn; + var base = this.base; + + var length = 3 * input.length; + var rgbBuf = new Uint8Array(length); + var pos = 0; + + for (var i = 0, ii = input.length; i < ii; ++i) { + var scaled = input[i] / 255; + var tinted = tintFn.func([scaled]); + var rgb = base.getRgb(tinted); + for (var j = 0; j < 3; ++j) + rgbBuf[pos++] = Math.round(255 * rgb[j]); + } + + return rgbBuf; + } + }; + + return constructor; +})(); + var PatternCS = (function() { function constructor() { this.name = 'Pattern'; @@ -4655,7 +4699,9 @@ var IndexedCS = (function() { var numComps = base.numComps; var c = []; - for (var i = 0; i < numComps; ++i) + var start = color[0] * numComps; + + for (var i = start, ii = start + numComps; i < ii; ++i) c.push(lookup[i]); return this.base.getRgb(c); }, @@ -4693,7 +4739,7 @@ var DeviceGrayCS = (function() { return [c, c, c]; }, getRgbBuffer: function graycs_getRgbBuffer(input) { - var length = input.length; + var length = input.length * 3; var rgbBuf = new Uint8Array(length); for (var i = 0, j = 0; i < length; ++i) { var c = input[i]; @@ -4786,8 +4832,22 @@ var DeviceCmykCS = (function() { return [r, g, b]; }, getRgbBuffer: function cmykcs_getRgbBuffer(colorBuf) { - error('conversion from rgb to cmyk not implemented for images'); - return colorBuf; + var length = colorBuf.length / 4; + var rgbBuf = new Uint8Array(length * 3); + var rgbBufPos = 0; + var colorBufPos = 0; + + for (var i = 0; i < length; i++) { + var cmyk = []; + for (var j = 0; j < 4; ++j) + cmyk.push(colorBuf[colorBufPos++]/255); + + var rgb = this.getRgb(cmyk); + for (var j = 0; j < 3; ++j) + rgb[rgbBufPos++] = Math.round(rgb[j] * 255); + } + + return rgbBuf; } }; return constructor; @@ -4904,7 +4964,7 @@ var PDFImage = (function() { output[i] = Math.round(255 * ret / ((1 << bpc) - 1)); } } - return this.colorSpace.getRbaBuffer(output); + return output; }, getOpacity: function getOpacity() { var smask = this.smask; @@ -4936,32 +4996,17 @@ var PDFImage = (function() { var rowBytes = (width * numComps * bpc + 7) >> 3; var imgArray = this.image.getBytes(height * rowBytes); - var comps = this.getComponents(imgArray); + var comps = this.colorSpace.getRgbBuffer(this.getComponents(imgArray)); var compsPos = 0; var opacity = this.getOpacity(); var opacityPos = 0; var length = width * height * 4; - switch (numComps) { - case 1: - for (var i = 0; i < length; i += 4) { - var p = comps[compsPos++]; - buffer[i] = p; - buffer[i + 1] = p; - buffer[i + 2] = p; - buffer[i + 3] = opacity[opacityPos++]; - } - break; - case 3: - for (var i = 0; i < length; i += 4) { - buffer[i] = comps[compsPos++]; - buffer[i + 1] = comps[compsPos++]; - buffer[i + 2] = comps[compsPos++]; - buffer[i + 3] = opacity[opacityPos++]; - } - break; - default: - TODO('Images with ' + numComps + ' components per pixel'); + for (var i = 0; i < length; i += 4) { + buffer[i] = comps[compsPos++]; + buffer[i + 1] = comps[compsPos++]; + buffer[i + 2] = comps[compsPos++]; + buffer[i + 3] = opacity[opacityPos++]; } }, fillGrayBuffer: function fillGrayBuffer(buffer) { From 1007e279c6b86b85803304f41b506434ce588988 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 7 Jul 2011 11:01:14 -0700 Subject: [PATCH 2/9] added comments --- pdf.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pdf.js b/pdf.js index 3829df975..c2760b546 100644 --- a/pdf.js +++ b/pdf.js @@ -4520,10 +4520,24 @@ var CanvasGraphics = (function() { })(); var ColorSpace = (function() { + // Constructor should define this.numComps, this.defaultColor, this.name function constructor() { error('should not call ColorSpace constructor'); }; + constructor.prototype = { + // Input: array of size numComps representing color component values + // Output: array of rgb values, each value ranging from [0.1] + getRgb: function cs_getRgb(color) { + error('Should not call ColorSpace.getRgb'); + }, + // Input: Uint8Array of component values, each value scaled to [0,255] + // Output: Uint8Array of rgb values, each value scaled to [0,255] + getRgbBuffer: function cs_getRgbBuffer(input) { + error('Should not call ColorSpace.getRgbBuffer'); + } + }; + constructor.parse = function colorspace_parse(cs, xref, res) { if (IsName(cs)) { var colorSpaces = res.get('ColorSpace'); From 54ae4fc6cba5a346a204c2720d09ac93ab2ea746 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 7 Jul 2011 11:13:39 -0700 Subject: [PATCH 3/9] changed name from TintCS to SeparationCS --- pdf.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pdf.js b/pdf.js index c2760b546..02efa39be 100644 --- a/pdf.js +++ b/pdf.js @@ -4620,7 +4620,7 @@ var ColorSpace = (function() { var name = cs[1]; var alt = ColorSpace.parse(cs[2], xref, res); var tintFn = new PDFFunction(xref, xref.fetchIfRef(cs[3])); - return new TintCS("Separation", alt, tintFn); + return new SeparationCS(alt, tintFn); break; case 'Lab': case 'DeviceN': @@ -4635,11 +4635,11 @@ var ColorSpace = (function() { return constructor; })(); -var TintCS = (function() { - function constructor(name, base, tintFn) { - this.name = name; +var SeparationCS = (function() { + function constructor(base, tintFn) { + this.name = "Separation"; this.numComps = 1; - this.defaultColor = [0]; + this.defaultColor = [1]; this.base = base; this.tintFn = tintFn; From 6ae81e1f8c55b3cca7defd3c7e201e3d063a493c Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 7 Jul 2011 11:21:41 -0700 Subject: [PATCH 4/9] cleanup --- pdf.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pdf.js b/pdf.js index 02efa39be..e9830f872 100644 --- a/pdf.js +++ b/pdf.js @@ -4708,15 +4708,14 @@ var IndexedCS = (function() { constructor.prototype = { getRgb: function graycs_getRgb(color) { - var lookup = this.lookup; - var base = this.base; var numComps = base.numComps; - var c = []; var start = color[0] * numComps; + var c = []; for (var i = start, ii = start + numComps; i < ii; ++i) - c.push(lookup[i]); + c.push(this.lookup[i]); + return this.base.getRgb(c); }, getRgbBuffer: function graycs_getRgbBuffer(input) { From b62eddd93712e3dd83ea0a6d8f258edbbb2711c6 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 7 Jul 2011 11:26:29 -0700 Subject: [PATCH 5/9] fixed to index cs --- pdf.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pdf.js b/pdf.js index e9830f872..d28a74945 100644 --- a/pdf.js +++ b/pdf.js @@ -4707,7 +4707,7 @@ var IndexedCS = (function() { } constructor.prototype = { - getRgb: function graycs_getRgb(color) { + getRgb: function indexcs_getRgb(color) { var numComps = base.numComps; var start = color[0] * numComps; @@ -4718,8 +4718,7 @@ var IndexedCS = (function() { return this.base.getRgb(c); }, - getRgbBuffer: function graycs_getRgbBuffer(input) { - var base = this.base; + getRgbBuffer: function indexcs_getRgbBuffer(input) { var numComps = base.numComps; var lookup = this.lookup; var length = input.length; @@ -4727,13 +4726,13 @@ var IndexedCS = (function() { var baseBuf = new Uint8Array(length * numComps); var baseBufPos = 0; for (var i = 0; i < length; ++i) { - var lookupPos = input[i]; + var lookupPos = input[i] * numComps; for (var j = 0; j < numComps; ++j) { baseBuf[baseBufPos++] = lookup[lookupPos + j]; } } - return base.getRgbBuffer(baseBuf); + return this.base.getRgbBuffer(baseBuf); } }; return constructor; From 39624e91739e24ebf52c83df19863c96b26cb922 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 7 Jul 2011 11:46:52 -0700 Subject: [PATCH 6/9] fix performance issue with SeparationCS --- pdf.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pdf.js b/pdf.js index d28a74945..78ac9eb3b 100644 --- a/pdf.js +++ b/pdf.js @@ -4658,15 +4658,16 @@ var SeparationCS = (function() { var rgbBuf = new Uint8Array(length); var pos = 0; + var numComps = base.numComps; + var baseBuf = new Uint8Array(numComps * input.length); for (var i = 0, ii = input.length; i < ii; ++i) { var scaled = input[i] / 255; var tinted = tintFn.func([scaled]); - var rgb = base.getRgb(tinted); - for (var j = 0; j < 3; ++j) - rgbBuf[pos++] = Math.round(255 * rgb[j]); + for (var j = 0; j < numComps; ++j) + baseBuf[pos++] = 255 * tinted[j]; } + return base.getRgbBuffer(baseBuf); - return rgbBuf; } }; @@ -4856,7 +4857,7 @@ var DeviceCmykCS = (function() { var rgb = this.getRgb(cmyk); for (var j = 0; j < 3; ++j) - rgb[rgbBufPos++] = Math.round(rgb[j] * 255); + rgbBuf[rgbBufPos++] = Math.round(rgb[j] * 255); } return rgbBuf; From f34308efd3304707357988374f277e43cd634448 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 7 Jul 2011 11:47:49 -0700 Subject: [PATCH 7/9] cleanup --- pdf.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pdf.js b/pdf.js index 78ac9eb3b..ecd5f48a3 100644 --- a/pdf.js +++ b/pdf.js @@ -4646,11 +4646,11 @@ var SeparationCS = (function() { } constructor.prototype = { - getRgb: function tintcs_getRgb(color) { + getRgb: function sepcs_getRgb(color) { var tinted = this.tintFn.func(color); return this.base.getRgb(tinted); }, - getRgbBuffer: function tintcs_getRgbBuffer(input) { + getRgbBuffer: function sepcs_getRgbBuffer(input) { var tintFn = this.tintFn; var base = this.base; From 15a50e423786b828252c4b679e8434c41287e08c Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 7 Jul 2011 11:48:34 -0700 Subject: [PATCH 8/9] cleanup --- pdf.js | 1 - 1 file changed, 1 deletion(-) diff --git a/pdf.js b/pdf.js index ecd5f48a3..c41031ecc 100644 --- a/pdf.js +++ b/pdf.js @@ -4655,7 +4655,6 @@ var SeparationCS = (function() { var base = this.base; var length = 3 * input.length; - var rgbBuf = new Uint8Array(length); var pos = 0; var numComps = base.numComps; From 8db37875445e3fc664b79261f14c222a152709d1 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 7 Jul 2011 11:56:10 -0700 Subject: [PATCH 9/9] fix bug with unref var --- pdf.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pdf.js b/pdf.js index c41031ecc..d41a98c27 100644 --- a/pdf.js +++ b/pdf.js @@ -4719,6 +4719,7 @@ var IndexedCS = (function() { return this.base.getRgb(c); }, getRgbBuffer: function indexcs_getRgbBuffer(input) { + var base = this.base; var numComps = base.numComps; var lookup = this.lookup; var length = input.length; @@ -4732,7 +4733,7 @@ var IndexedCS = (function() { } } - return this.base.getRgbBuffer(baseBuf); + return base.getRgbBuffer(baseBuf); } }; return constructor;