Merge pull request #1151 from notmasteryet/sampled
implement sampled functions based on the PDF spec (part 2)
This commit is contained in:
commit
218714fe57
156
src/function.js
156
src/function.js
@ -125,109 +125,99 @@ var PDFFunction = (function PDFFunctionClosure() {
|
|||||||
else
|
else
|
||||||
decode = toMultiArray(decode);
|
decode = toMultiArray(decode);
|
||||||
|
|
||||||
// Precalc the multipliers
|
|
||||||
var inputMul = new Float64Array(inputSize);
|
|
||||||
for (var i = 0; i < inputSize; ++i) {
|
|
||||||
inputMul[i] = (encode[i][1] - encode[i][0]) /
|
|
||||||
(domain[i][1] - domain[i][0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
var idxMul = new Int32Array(inputSize);
|
|
||||||
idxMul[0] = outputSize;
|
|
||||||
for (i = 1; i < inputSize; ++i) {
|
|
||||||
idxMul[i] = idxMul[i - 1] * size[i - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
var nSamples = outputSize;
|
|
||||||
for (i = 0; i < inputSize; ++i)
|
|
||||||
nSamples *= size[i];
|
|
||||||
|
|
||||||
var samples = this.getSampleArray(size, outputSize, bps, str);
|
var samples = this.getSampleArray(size, outputSize, bps, str);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
CONSTRUCT_SAMPLED, inputSize, domain, encode, decode, samples, size,
|
CONSTRUCT_SAMPLED, inputSize, domain, encode, decode, samples, size,
|
||||||
outputSize, bps, range, inputMul, idxMul, nSamples
|
outputSize, Math.pow(2, bps) - 1, range
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
|
|
||||||
constructSampledFromIR: function pdfFunctionConstructSampledFromIR(IR) {
|
constructSampledFromIR: function pdfFunctionConstructSampledFromIR(IR) {
|
||||||
var inputSize = IR[1];
|
// See chapter 3, page 109 of the PDF reference
|
||||||
var domain = IR[2];
|
function interpolate(x, xmin, xmax, ymin, ymax) {
|
||||||
var encode = IR[3];
|
return ymin + ((x - xmin) * ((ymax - ymin) / (xmax - xmin)));
|
||||||
var decode = IR[4];
|
}
|
||||||
var samples = IR[5];
|
|
||||||
var size = IR[6];
|
|
||||||
var outputSize = IR[7];
|
|
||||||
var bps = IR[8];
|
|
||||||
var range = IR[9];
|
|
||||||
var inputMul = IR[10];
|
|
||||||
var idxMul = IR[11];
|
|
||||||
var nSamples = IR[12];
|
|
||||||
|
|
||||||
return function constructSampledFromIRResult(args) {
|
return function constructSampledFromIRResult(args) {
|
||||||
if (inputSize != args.length)
|
// See chapter 3, page 110 of the PDF reference.
|
||||||
|
var m = IR[1];
|
||||||
|
var domain = IR[2];
|
||||||
|
var encode = IR[3];
|
||||||
|
var decode = IR[4];
|
||||||
|
var samples = IR[5];
|
||||||
|
var size = IR[6];
|
||||||
|
var n = IR[7];
|
||||||
|
var mask = IR[8];
|
||||||
|
var range = IR[9];
|
||||||
|
|
||||||
|
if (m != args.length)
|
||||||
error('Incorrect number of arguments: ' + inputSize + ' != ' +
|
error('Incorrect number of arguments: ' + inputSize + ' != ' +
|
||||||
args.length);
|
args.length);
|
||||||
// Most of the below is a port of Poppler's implementation.
|
|
||||||
// TODO: There's a few other ways to do multilinear interpolation such
|
|
||||||
// as piecewise, which is much faster but an approximation.
|
|
||||||
var out = new Float64Array(outputSize);
|
|
||||||
var x;
|
|
||||||
var e = new Array(inputSize);
|
|
||||||
var efrac0 = new Float64Array(inputSize);
|
|
||||||
var efrac1 = new Float64Array(inputSize);
|
|
||||||
var sBuf = new Float64Array(1 << inputSize);
|
|
||||||
var i, j, k, idx, t;
|
|
||||||
|
|
||||||
// map input values into sample array
|
var x = args;
|
||||||
for (i = 0; i < inputSize; ++i) {
|
|
||||||
x = (args[i] - domain[i][0]) * inputMul[i] + encode[i][0];
|
|
||||||
if (x < 0) {
|
|
||||||
x = 0;
|
|
||||||
} else if (x > size[i] - 1) {
|
|
||||||
x = size[i] - 1;
|
|
||||||
}
|
|
||||||
e[i] = [Math.floor(x), 0];
|
|
||||||
if ((e[i][1] = e[i][0] + 1) >= size[i]) {
|
|
||||||
// this happens if in[i] = domain[i][1]
|
|
||||||
e[i][1] = e[i][0];
|
|
||||||
}
|
|
||||||
efrac1[i] = x - e[i][0];
|
|
||||||
efrac0[i] = 1 - efrac1[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// for each output, do m-linear interpolation
|
// Building the cube vertices: its part and sample index
|
||||||
for (i = 0; i < outputSize; ++i) {
|
// http://rjwagner49.com/Mathematics/Interpolation.pdf
|
||||||
|
var cubeVertices = 1 << m;
|
||||||
|
var cubeN = new Float64Array(cubeVertices);
|
||||||
|
var cubeVertex = new Uint32Array(cubeVertices);
|
||||||
|
for (var j = 0; j < cubeVertices; j++)
|
||||||
|
cubeN[j] = 1;
|
||||||
|
|
||||||
// pull 2^m values out of the sample array
|
var k = n, pos = 1;
|
||||||
for (j = 0; j < (1 << inputSize); ++j) {
|
// Map x_i to y_j for 0 <= i < m using the sampled function.
|
||||||
idx = i;
|
for (var i = 0; i < m; ++i) {
|
||||||
for (k = 0, t = j; k < inputSize; ++k, t >>= 1) {
|
// x_i' = min(max(x_i, Domain_2i), Domain_2i+1)
|
||||||
idx += idxMul[k] * (e[k][t & 1]);
|
var domain_2i = domain[i][0];
|
||||||
}
|
var domain_2i_1 = domain[i][1];
|
||||||
if (idx >= 0 && idx < nSamples) {
|
var xi = Math.min(Math.max(x[i], domain_2i), domain_2i_1);
|
||||||
sBuf[j] = samples[idx];
|
|
||||||
|
// e_i = Interpolate(x_i', Domain_2i, Domain_2i+1,
|
||||||
|
// Encode_2i, Encode_2i+1)
|
||||||
|
var e = interpolate(xi, domain_2i, domain_2i_1,
|
||||||
|
encode[i][0], encode[i][1]);
|
||||||
|
|
||||||
|
// e_i' = min(max(e_i, 0), Size_i - 1)
|
||||||
|
var size_i = size[i];
|
||||||
|
e = Math.min(Math.max(e, 0), size_i - 1);
|
||||||
|
|
||||||
|
// Adjusting the cube: N and vertex sample index
|
||||||
|
var e0 = e < size_i - 1 ? Math.floor(e) : e - 1; // e1 = e0 + 1;
|
||||||
|
var n0 = e0 + 1 - e; // (e1 - e) / (e1 - e0);
|
||||||
|
var n1 = e - e0; // (e - e0) / (e1 - e0);
|
||||||
|
var offset0 = e0 * k;
|
||||||
|
var offset1 = offset0 + k; // e1 * k
|
||||||
|
for (var j = 0; j < cubeVertices; j++) {
|
||||||
|
if (j & pos) {
|
||||||
|
cubeN[j] *= n1;
|
||||||
|
cubeVertex[j] += offset1;
|
||||||
} else {
|
} else {
|
||||||
sBuf[j] = 0; // TODO Investigate if this is what Adobe does
|
cubeN[j] *= n0;
|
||||||
|
cubeVertex[j] += offset0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// do m sets of interpolations
|
k *= size_i;
|
||||||
for (j = 0, t = (1 << inputSize); j < inputSize; ++j, t >>= 1) {
|
pos <<= 1;
|
||||||
for (k = 0; k < t; k += 2) {
|
|
||||||
sBuf[k >> 1] = efrac0[j] * sBuf[k] + efrac1[j] * sBuf[k + 1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// map output value to range
|
|
||||||
out[i] = (sBuf[0] * (decode[i][1] - decode[i][0]) + decode[i][0]);
|
|
||||||
if (out[i] < range[i][0]) {
|
|
||||||
out[i] = range[i][0];
|
|
||||||
} else if (out[i] > range[i][1]) {
|
|
||||||
out[i] = range[i][1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return out;
|
|
||||||
|
var y = new Float64Array(n);
|
||||||
|
for (var j = 0; j < n; ++j) {
|
||||||
|
// Sum all cube vertices' samples portions
|
||||||
|
var rj = 0;
|
||||||
|
for (var i = 0; i < cubeVertices; i++)
|
||||||
|
rj += samples[cubeVertex[i] + j] * cubeN[i];
|
||||||
|
|
||||||
|
// r_j' = Interpolate(r_j, 0, 2^BitsPerSample - 1,
|
||||||
|
// Decode_2j, Decode_2j+1)
|
||||||
|
rj = interpolate(rj, 0, 1, decode[j][0], decode[j][1]);
|
||||||
|
|
||||||
|
// y_j = min(max(r_j, range_2j), range_2j+1)
|
||||||
|
y[j] = Math.min(Math.max(rj, range[j][0]), range[j][1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return y;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user