Merge pull request #5165 from kkujala/calrgb

implement CalRGB color space
This commit is contained in:
Yury Delendik 2014-08-15 10:19:49 -05:00
commit fa8d385818
3 changed files with 9722 additions and 3 deletions

View File

@ -177,7 +177,7 @@ var ColorSpace = (function ColorSpaceClosure() {
ColorSpace.fromIR = function ColorSpace_fromIR(IR) { ColorSpace.fromIR = function ColorSpace_fromIR(IR) {
var name = isArray(IR) ? IR[0] : IR; var name = isArray(IR) ? IR[0] : IR;
var whitePoint, blackPoint; var whitePoint, blackPoint, gamma;
switch (name) { switch (name) {
case 'DeviceGrayCS': case 'DeviceGrayCS':
@ -189,8 +189,14 @@ var ColorSpace = (function ColorSpaceClosure() {
case 'CalGrayCS': case 'CalGrayCS':
whitePoint = IR[1].WhitePoint; whitePoint = IR[1].WhitePoint;
blackPoint = IR[1].BlackPoint; blackPoint = IR[1].BlackPoint;
var gamma = IR[1].Gamma; gamma = IR[1].Gamma;
return new CalGrayCS(whitePoint, blackPoint, gamma); return new CalGrayCS(whitePoint, blackPoint, gamma);
case 'CalRGBCS':
whitePoint = IR[1].WhitePoint;
blackPoint = IR[1].BlackPoint;
gamma = IR[1].Gamma;
var matrix = IR[1].Matrix;
return new CalRGBCS(whitePoint, blackPoint, gamma, matrix);
case 'PatternCS': case 'PatternCS':
var basePatternCS = IR[1]; var basePatternCS = IR[1];
if (basePatternCS) { if (basePatternCS) {
@ -272,7 +278,8 @@ var ColorSpace = (function ColorSpaceClosure() {
params = cs[1].getAll(); params = cs[1].getAll();
return ['CalGrayCS', params]; return ['CalGrayCS', params];
case 'CalRGB': case 'CalRGB':
return 'DeviceRgbCS'; params = cs[1].getAll();
return ['CalRGBCS', params];
case 'ICCBased': case 'ICCBased':
var stream = xref.fetchIfRef(cs[1]); var stream = xref.fetchIfRef(cs[1]);
var dict = stream.dict; var dict = stream.dict;
@ -793,6 +800,288 @@ var CalGrayCS = (function CalGrayCSClosure() {
return CalGrayCS; return CalGrayCS;
})(); })();
//
// CalRGBCS: Based on "PDF Reference, Sixth Ed", p.247
//
var CalRGBCS = (function CalRGBCSClosure() {
// See http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html for these
// matrices.
var BRADFORD_SCALE_MATRIX = new Float32Array([
0.8951, 0.2664, -0.1614,
-0.7502, 1.7135, 0.0367,
0.0389, -0.0685, 1.0296]);
var BRADFORD_SCALE_INVERSE_MATRIX = new Float32Array([
0.9869929, -0.1470543, 0.1599627,
0.4323053, 0.5183603, 0.0492912,
-0.0085287, 0.0400428, 0.9684867]);
// See http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html.
var SRGB_D65_XYZ_TO_RGB_MATRIX = new Float32Array([
3.2404542, -1.5371385, -0.4985314,
-0.9692660, 1.8760108, 0.0415560,
0.0556434, -0.2040259, 1.0572252]);
var FLAT_WHITEPOINT_MATRIX = new Float32Array([1, 1, 1]);
var tempNormalizeMatrix = new Float32Array(3);
var tempConvertMatrix1 = new Float32Array(3);
var tempConvertMatrix2 = new Float32Array(3);
function CalRGBCS(whitePoint, blackPoint, gamma, matrix) {
this.name = 'CalRGB';
this.numComps = 3;
this.defaultColor = new Float32Array(3);
if (!whitePoint) {
error('WhitePoint missing - required for color space CalRGB');
}
blackPoint = blackPoint || new Float32Array(3);
gamma = gamma || new Float32Array([1, 1, 1]);
matrix = matrix || new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
// Translate arguments to spec variables.
var XW = whitePoint[0];
var YW = whitePoint[1];
var ZW = whitePoint[2];
this.whitePoint = whitePoint;
var XB = blackPoint[0];
var YB = blackPoint[1];
var ZB = blackPoint[2];
this.blackPoint = blackPoint;
this.GR = gamma[0];
this.GG = gamma[1];
this.GB = gamma[2];
this.MXA = matrix[0];
this.MYA = matrix[1];
this.MZA = matrix[2];
this.MXB = matrix[3];
this.MYB = matrix[4];
this.MZB = matrix[5];
this.MXC = matrix[6];
this.MYC = matrix[7];
this.MZC = matrix[8];
// Validate variables as per spec.
if (XW < 0 || ZW < 0 || YW !== 1) {
error('Invalid WhitePoint components for ' + this.name +
', no fallback available');
}
if (XB < 0 || YB < 0 || ZB < 0) {
info('Invalid BlackPoint for ' + this.name + ' [' + XB + ', ' + YB +
', ' + ZB + '], falling back to default');
this.blackPoint = new Float32Array(3);
}
if (this.GR < 0 || this.GG < 0 || this.GB < 0) {
info('Invalid Gamma [' + this.GR + ', ' + this.GG + ', ' + this.GB +
'] for ' + this.name + ', falling back to default');
this.GR = this.GG = this.GB = 1;
}
if (this.MXA < 0 || this.MYA < 0 || this.MZA < 0 ||
this.MXB < 0 || this.MYB < 0 || this.MZB < 0 ||
this.MXC < 0 || this.MYC < 0 || this.MZC < 0) {
info('Invalid Matrix for ' + this.name + ' [' +
this.MXA + ', ' + this.MYA + ', ' + this.MZA +
this.MXB + ', ' + this.MYB + ', ' + this.MZB +
this.MXC + ', ' + this.MYC + ', ' + this.MZC +
'], falling back to default');
this.MXA = this.MYB = this.MZC = 1;
this.MXB = this.MYA = this.MZA = this.MXC = this.MYC = this.MZB = 0;
}
}
function matrixProduct(a, b, result) {
result[0] = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
result[1] = a[3] * b[0] + a[4] * b[1] + a[5] * b[2];
result[2] = a[6] * b[0] + a[7] * b[1] + a[8] * b[2];
}
function convertToFlat(sourceWhitePoint, LMS, result) {
result[0] = LMS[0] * 1 / sourceWhitePoint[0];
result[1] = LMS[1] * 1 / sourceWhitePoint[1];
result[2] = LMS[2] * 1 / sourceWhitePoint[2];
}
function convertToD65(sourceWhitePoint, LMS, result) {
var D65X = 0.95047;
var D65Y = 1;
var D65Z = 1.08883;
result[0] = LMS[0] * D65X / sourceWhitePoint[0];
result[1] = LMS[1] * D65Y / sourceWhitePoint[1];
result[2] = LMS[2] * D65Z / sourceWhitePoint[2];
}
function sRGBTransferFunction(color) {
// See http://en.wikipedia.org/wiki/SRGB.
if (color <= 0.0031308){
return adjustToRange(0, 1, 12.92 * color);
}
return adjustToRange(0, 1, (1 + 0.055) * Math.pow(color, 1 / 2.4) - 0.055);
}
function adjustToRange(min, max, value) {
return Math.max(min, Math.min(max, value));
}
function decodeL(L) {
if (L < 0) {
return -decodeL(-L);
}
if (L > 80) {
return Math.pow(((L + 16) / 116), 3);
}
return L * Math.pow(((8 + 16) / 116), 3) / 8.0;
}
function compensateBlackPoint(sourceBlackPoint, XYZ_Flat, result) {
// For the BlackPoint calculation details, please see
// http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/sdk/
// AdobeBPC.pdf.
// The destination BlackPoint is the default BlackPoint [0, 0, 0].
var zeroDecodeL = decodeL(0);
var X_DST = zeroDecodeL;
var X_SRC = decodeL(sourceBlackPoint[0]);
var Y_DST = zeroDecodeL;
var Y_SRC = decodeL(sourceBlackPoint[1]);
var Z_DST = zeroDecodeL;
var Z_SRC = decodeL(sourceBlackPoint[2]);
var X_Scale = (1 - X_DST) / (1 - X_SRC);
var X_Offset = 1 - X_Scale;
var Y_Scale = (1 - Y_DST) / (1 - Y_SRC);
var Y_Offset = 1 - Y_Scale;
var Z_Scale = (1 - Z_DST) / (1 - Z_SRC);
var Z_Offset = 1 - Z_Scale;
result[0] = XYZ_Flat[0] * X_Scale + X_Offset;
result[1] = XYZ_Flat[1] * Y_Scale + Y_Offset;
result[2] = XYZ_Flat[2] * Z_Scale + Z_Offset;
}
function normalizeWhitePointToFlat(sourceWhitePoint, XYZ_In, result) {
var LMS = result;
matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
var LMS_Flat = tempNormalizeMatrix;
convertToFlat(sourceWhitePoint, LMS, LMS_Flat);
matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_Flat, result);
}
function normalizeWhitePointToD65(sourceWhitePoint, XYZ_In, result) {
var LMS = result;
matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
var LMS_D65 = tempNormalizeMatrix;
convertToD65(sourceWhitePoint, LMS, LMS_D65);
matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_D65, result);
}
function convertToRgb(cs, src, srcOffset, dest, destOffset, scale) {
// A, B and C represent a red, green and blue components of a calibrated
// rgb space.
var A = adjustToRange(0, 1, src[srcOffset] * scale);
var B = adjustToRange(0, 1, src[srcOffset + 1] * scale);
var C = adjustToRange(0, 1, src[srcOffset + 2] * scale);
// A <---> AGR in the spec
// B <---> BGG in the spec
// C <---> CGB in the spec
var AGR = Math.pow(A, cs.GR);
var BGG = Math.pow(B, cs.GG);
var CGB = Math.pow(C, cs.GB);
// Computes intermediate variables M, L, N as per spec.
var M = cs.MXA * AGR + cs.MXB * BGG + cs.MXC * CGB;
var L = cs.MYA * AGR + cs.MYB * BGG + cs.MYC * CGB;
var N = cs.MZA * AGR + cs.MZB * BGG + cs.MZC * CGB;
// Decode XYZ, as per spec.
var X = M;
var Y = L;
var Z = N;
// The following calculations are based on this document:
// http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/sdk/
// AdobeBPC.pdf.
var XYZ = tempConvertMatrix1;
XYZ[0] = X;
XYZ[1] = Y;
XYZ[2] = Z;
var XYZ_Flat = tempConvertMatrix2;
normalizeWhitePointToFlat(cs.whitePoint, XYZ, XYZ_Flat);
var XYZ_Black = tempConvertMatrix1;
compensateBlackPoint(cs.blackPoint, XYZ_Flat, XYZ_Black);
var XYZ_D65 = tempConvertMatrix2;
normalizeWhitePointToD65(FLAT_WHITEPOINT_MATRIX, XYZ_Black, XYZ_D65);
var SRGB = tempConvertMatrix1;
matrixProduct(SRGB_D65_XYZ_TO_RGB_MATRIX, XYZ_D65, SRGB);
var sR = sRGBTransferFunction(SRGB[0]);
var sG = sRGBTransferFunction(SRGB[1]);
var sB = sRGBTransferFunction(SRGB[2]);
// Convert the values to rgb range [0, 255].
dest[destOffset] = Math.round(sR * 255);
dest[destOffset + 1] = Math.round(sG * 255);
dest[destOffset + 2] = Math.round(sB * 255);
}
CalRGBCS.prototype = {
getRgb: function CalRGBCS_getRgb(src, srcOffset) {
var rgb = new Uint8Array(3);
this.getRgbItem(src, srcOffset, rgb, 0);
return rgb;
},
getRgbItem: function CalRGBCS_getRgbItem(src, srcOffset,
dest, destOffset) {
convertToRgb(this, src, srcOffset, dest, destOffset, 1);
},
getRgbBuffer: function CalRGBCS_getRgbBuffer(src, srcOffset, count,
dest, destOffset, bits) {
var scale = 1 / ((1 << bits) - 1);
for (var i = 0; i < count; ++i) {
convertToRgb(this, src, srcOffset, dest, destOffset, scale);
srcOffset += 3;
destOffset += 3;
}
},
getOutputLength: function CalRGBCS_getOutputLength(inputLength) {
return inputLength;
},
isPassthrough: ColorSpace.prototype.isPassthrough,
createRgbBuffer: ColorSpace.prototype.createRgbBuffer,
isDefaultDecode: function CalRGBCS_isDefaultDecode(decodeMap) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
},
usesZeroToOneRange: true
};
return CalRGBCS;
})();
// //
// LabCS: Based on "PDF Reference, Sixth Ed", p.250 // LabCS: Based on "PDF Reference, Sixth Ed", p.250
// //

9423
test/pdfs/calrgb.pdf Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1691,6 +1691,13 @@
"rounds": 1, "rounds": 1,
"type": "eq" "type": "eq"
}, },
{ "id": "calrgb",
"file": "pdfs/calrgb.pdf",
"md5": "5f3dfda1d3a795cd214a457244130bfa",
"rounds": 1,
"lastPage": 8,
"type": "eq"
},
{ "id": "bug900822", { "id": "bug900822",
"file": "pdfs/bug900822.pdf", "file": "pdfs/bug900822.pdf",
"md5": "70e2a3c5922574eeda169c955cf9d084", "md5": "70e2a3c5922574eeda169c955cf9d084",