3 typedef struct psobj_s psobj;
13 typedef struct pdf_function_s pdf_function;
18 int type; /* 0=sample 2=exponential 3=stitching 4=postscript */
19 float domain[FZ_FN_MAXM][2]; /* even index : min value, odd index : max value */
20 float range[FZ_FN_MAXN][2]; /* even index : min value, odd index : max value */
28 float encode[FZ_FN_MAXM][2];
29 float decode[FZ_FN_MAXN][2];
41 fz_function **funcs; /* k */
42 float *bounds; /* k - 1 */
43 float *encode; /* k * 2 */
53 #define RADIAN 57.2957795
55 static inline float lerp(float x, float xmin, float xmax, float ymin, float ymax)
61 return ymin + (x - xmin) * (ymax - ymin) / (xmax - xmin);
65 * PostScript calculator
68 enum { PS_BOOL, PS_INT, PS_REAL, PS_OPERATOR, PS_BLOCK };
72 PS_OP_ABS, PS_OP_ADD, PS_OP_AND, PS_OP_ATAN, PS_OP_BITSHIFT,
73 PS_OP_CEILING, PS_OP_COPY, PS_OP_COS, PS_OP_CVI, PS_OP_CVR,
74 PS_OP_DIV, PS_OP_DUP, PS_OP_EQ, PS_OP_EXCH, PS_OP_EXP,
75 PS_OP_FALSE, PS_OP_FLOOR, PS_OP_GE, PS_OP_GT, PS_OP_IDIV, PS_OP_IF,
76 PS_OP_IFELSE, PS_OP_INDEX, PS_OP_LE, PS_OP_LN, PS_OP_LOG, PS_OP_LT,
77 PS_OP_MOD, PS_OP_MUL, PS_OP_NE, PS_OP_NEG, PS_OP_NOT, PS_OP_OR,
78 PS_OP_POP, PS_OP_RETURN, PS_OP_ROLL, PS_OP_ROUND, PS_OP_SIN,
79 PS_OP_SQRT, PS_OP_SUB, PS_OP_TRUE, PS_OP_TRUNCATE, PS_OP_XOR
82 static char *ps_op_names[] =
84 "abs", "add", "and", "atan", "bitshift", "ceiling", "copy",
85 "cos", "cvi", "cvr", "div", "dup", "eq", "exch", "exp",
86 "false", "floor", "ge", "gt", "idiv", "if", "ifelse", "index", "le", "ln",
87 "log", "lt", "mod", "mul", "ne", "neg", "not", "or", "pop", "return",
88 "roll", "round", "sin", "sqrt", "sub", "true", "truncate", "xor"
96 int b; /* boolean (stack only) */
97 int i; /* integer (stack and code) */
98 float f; /* real (stack and code) */
99 int op; /* operator (code only) */
100 int block; /* if/ifelse block pointer (code only) */
104 typedef struct ps_stack_s ps_stack;
114 pdf_debug_ps_stack(ps_stack *st)
120 for (i = 0; i < st->sp; i++)
122 switch (st->stack[i].type)
125 if (st->stack[i].u.b)
132 printf("%d ", st->stack[i].u.i);
136 printf("%g ", st->stack[i].u.f);
146 ps_init_stack(ps_stack *st)
148 memset(st->stack, 0, sizeof(st->stack));
152 static inline int ps_overflow(ps_stack *st, int n)
154 return n < 0 || st->sp + n >= nelem(st->stack);
157 static inline int ps_underflow(ps_stack *st, int n)
159 return n < 0 || st->sp - n < 0;
162 static inline int ps_is_type(ps_stack *st, int t)
164 return !ps_underflow(st, 1) && st->stack[st->sp - 1].type == t;
167 static inline int ps_is_type2(ps_stack *st, int t)
169 return !ps_underflow(st, 2) && st->stack[st->sp - 1].type == t && st->stack[st->sp - 2].type == t;
173 ps_push_bool(ps_stack *st, int b)
175 if (!ps_overflow(st, 1))
177 st->stack[st->sp].type = PS_BOOL;
178 st->stack[st->sp].u.b = b;
184 ps_push_int(ps_stack *st, int n)
186 if (!ps_overflow(st, 1))
188 st->stack[st->sp].type = PS_INT;
189 st->stack[st->sp].u.i = n;
195 ps_push_real(ps_stack *st, float n)
197 if (!ps_overflow(st, 1))
199 st->stack[st->sp].type = PS_REAL;
202 /* Push 1.0, as it's a small known value that won't
203 * cause a divide by 0. Same reason as in fz_atof. */
206 st->stack[st->sp].u.f = fz_clamp(n, -FLT_MAX, FLT_MAX);
212 ps_pop_bool(ps_stack *st)
214 if (!ps_underflow(st, 1))
216 if (ps_is_type(st, PS_BOOL))
217 return st->stack[--st->sp].u.b;
223 ps_pop_int(ps_stack *st)
225 if (!ps_underflow(st, 1))
227 if (ps_is_type(st, PS_INT))
228 return st->stack[--st->sp].u.i;
229 if (ps_is_type(st, PS_REAL))
230 return st->stack[--st->sp].u.f;
236 ps_pop_real(ps_stack *st)
238 if (!ps_underflow(st, 1))
240 if (ps_is_type(st, PS_INT))
241 return st->stack[--st->sp].u.i;
242 if (ps_is_type(st, PS_REAL))
243 return st->stack[--st->sp].u.f;
249 ps_copy(ps_stack *st, int n)
251 if (!ps_underflow(st, n) && !ps_overflow(st, n))
253 memcpy(st->stack + st->sp, st->stack + st->sp - n, n * sizeof(psobj));
259 ps_roll(ps_stack *st, int n, int j)
264 if (ps_underflow(st, n) || j == 0 || n == 0)
278 for (i = 0; i < j; i++)
280 tmp = st->stack[st->sp - 1];
281 memmove(st->stack + st->sp - n + 1, st->stack + st->sp - n, n * sizeof(psobj));
282 st->stack[st->sp - n] = tmp;
287 ps_index(ps_stack *st, int n)
289 if (!ps_overflow(st, 1) && !ps_underflow(st, n))
291 st->stack[st->sp] = st->stack[st->sp - n - 1];
297 ps_run(fz_context *ctx, psobj *code, ps_stack *st, int pc)
305 switch (code[pc].type)
308 ps_push_int(st, code[pc++].u.i);
312 ps_push_real(st, code[pc++].u.f);
316 switch (code[pc++].u.op)
319 if (ps_is_type(st, PS_INT))
320 ps_push_int(st, abs(ps_pop_int(st)));
322 ps_push_real(st, fabsf(ps_pop_real(st)));
326 if (ps_is_type2(st, PS_INT)) {
329 ps_push_int(st, i1 + i2);
332 r2 = ps_pop_real(st);
333 r1 = ps_pop_real(st);
334 ps_push_real(st, r1 + r2);
339 if (ps_is_type2(st, PS_INT)) {
342 ps_push_int(st, i1 & i2);
345 b2 = ps_pop_bool(st);
346 b1 = ps_pop_bool(st);
347 ps_push_bool(st, b1 && b2);
352 r2 = ps_pop_real(st);
353 r1 = ps_pop_real(st);
354 r1 = atan2f(r1, r2) * RADIAN;
357 ps_push_real(st, r1);
363 if (i2 > 0 && i2 < 8 * sizeof (i2))
364 ps_push_int(st, i1 << i2);
365 else if (i2 < 0 && i2 > -8 * (int)sizeof (i2))
366 ps_push_int(st, (int)((unsigned int)i1 >> -i2));
372 r1 = ps_pop_real(st);
373 ps_push_real(st, ceilf(r1));
377 ps_copy(st, ps_pop_int(st));
381 r1 = ps_pop_real(st);
382 ps_push_real(st, cosf(r1/RADIAN));
386 ps_push_int(st, ps_pop_int(st));
390 ps_push_real(st, ps_pop_real(st));
394 r2 = ps_pop_real(st);
395 r1 = ps_pop_real(st);
396 if (fabsf(r2) >= FLT_EPSILON)
397 ps_push_real(st, r1 / r2);
399 ps_push_real(st, DIV_BY_ZERO(r1, r2, -FLT_MAX, FLT_MAX));
407 if (ps_is_type2(st, PS_BOOL)) {
408 b2 = ps_pop_bool(st);
409 b1 = ps_pop_bool(st);
410 ps_push_bool(st, b1 == b2);
412 else if (ps_is_type2(st, PS_INT)) {
415 ps_push_bool(st, i1 == i2);
418 r2 = ps_pop_real(st);
419 r1 = ps_pop_real(st);
420 ps_push_bool(st, r1 == r2);
429 r2 = ps_pop_real(st);
430 r1 = ps_pop_real(st);
431 ps_push_real(st, powf(r1, r2));
439 r1 = ps_pop_real(st);
440 ps_push_real(st, floorf(r1));
444 if (ps_is_type2(st, PS_INT)) {
447 ps_push_bool(st, i1 >= i2);
450 r2 = ps_pop_real(st);
451 r1 = ps_pop_real(st);
452 ps_push_bool(st, r1 >= r2);
457 if (ps_is_type2(st, PS_INT)) {
460 ps_push_bool(st, i1 > i2);
463 r2 = ps_pop_real(st);
464 r1 = ps_pop_real(st);
465 ps_push_bool(st, r1 > r2);
473 ps_push_int(st, i1 / i2);
475 ps_push_int(st, DIV_BY_ZERO(i1, i2, INT_MIN, INT_MAX));
479 ps_index(st, ps_pop_int(st));
483 if (ps_is_type2(st, PS_INT)) {
486 ps_push_bool(st, i1 <= i2);
489 r2 = ps_pop_real(st);
490 r1 = ps_pop_real(st);
491 ps_push_bool(st, r1 <= r2);
496 r1 = ps_pop_real(st);
497 /* Bug 692941 - logf as separate statement */
499 ps_push_real(st, r2);
503 r1 = ps_pop_real(st);
504 ps_push_real(st, log10f(r1));
508 if (ps_is_type2(st, PS_INT)) {
511 ps_push_bool(st, i1 < i2);
514 r2 = ps_pop_real(st);
515 r1 = ps_pop_real(st);
516 ps_push_bool(st, r1 < r2);
524 ps_push_int(st, i1 % i2);
526 ps_push_int(st, DIV_BY_ZERO(i1, i2, INT_MIN, INT_MAX));
530 if (ps_is_type2(st, PS_INT)) {
533 ps_push_int(st, i1 * i2);
536 r2 = ps_pop_real(st);
537 r1 = ps_pop_real(st);
538 ps_push_real(st, r1 * r2);
543 if (ps_is_type2(st, PS_BOOL)) {
544 b2 = ps_pop_bool(st);
545 b1 = ps_pop_bool(st);
546 ps_push_bool(st, b1 != b2);
548 else if (ps_is_type2(st, PS_INT)) {
551 ps_push_bool(st, i1 != i2);
554 r2 = ps_pop_real(st);
555 r1 = ps_pop_real(st);
556 ps_push_bool(st, r1 != r2);
561 if (ps_is_type(st, PS_INT))
562 ps_push_int(st, -ps_pop_int(st));
564 ps_push_real(st, -ps_pop_real(st));
568 if (ps_is_type(st, PS_BOOL))
569 ps_push_bool(st, !ps_pop_bool(st));
571 ps_push_int(st, ~ps_pop_int(st));
575 if (ps_is_type2(st, PS_BOOL)) {
576 b2 = ps_pop_bool(st);
577 b1 = ps_pop_bool(st);
578 ps_push_bool(st, b1 || b2);
583 ps_push_int(st, i1 | i2);
588 if (!ps_underflow(st, 1))
599 if (!ps_is_type(st, PS_INT)) {
600 r1 = ps_pop_real(st);
601 ps_push_real(st, (r1 >= 0) ? floorf(r1 + 0.5f) : ceilf(r1 - 0.5f));
606 r1 = ps_pop_real(st);
607 ps_push_real(st, sinf(r1/RADIAN));
611 r1 = ps_pop_real(st);
612 ps_push_real(st, sqrtf(r1));
616 if (ps_is_type2(st, PS_INT)) {
619 ps_push_int(st, i1 - i2);
622 r2 = ps_pop_real(st);
623 r1 = ps_pop_real(st);
624 ps_push_real(st, r1 - r2);
633 if (!ps_is_type(st, PS_INT)) {
634 r1 = ps_pop_real(st);
635 ps_push_real(st, (r1 >= 0) ? floorf(r1) : ceilf(r1));
640 if (ps_is_type2(st, PS_BOOL)) {
641 b2 = ps_pop_bool(st);
642 b1 = ps_pop_bool(st);
643 ps_push_bool(st, b1 ^ b2);
648 ps_push_int(st, i1 ^ i2);
653 b1 = ps_pop_bool(st);
655 ps_run(ctx, code, st, code[pc + 1].u.block);
656 pc = code[pc + 2].u.block;
660 b1 = ps_pop_bool(st);
662 ps_run(ctx, code, st, code[pc + 1].u.block);
664 ps_run(ctx, code, st, code[pc + 0].u.block);
665 pc = code[pc + 2].u.block;
672 fz_warn(ctx, "foreign operator in calculator function");
678 fz_warn(ctx, "foreign object in calculator function");
685 resize_code(fz_context *ctx, pdf_function *func, int newsize)
687 if (newsize >= func->u.p.cap)
689 int new_cap = func->u.p.cap + 64;
690 func->u.p.code = fz_resize_array(ctx, func->u.p.code, new_cap, sizeof(psobj));
691 func->u.p.cap = new_cap;
696 parse_code(pdf_function *func, fz_stream *stream, int *codeptr, pdf_lexbuf *buf)
699 int opptr, elseptr, ifptr;
701 fz_context *ctx = stream->ctx;
705 tok = pdf_lex(stream, buf);
710 fz_throw(ctx, FZ_ERROR_GENERIC, "truncated calculator function");
713 resize_code(ctx, func, *codeptr);
714 func->u.p.code[*codeptr].type = PS_INT;
715 func->u.p.code[*codeptr].u.i = buf->i;
720 resize_code(ctx, func, *codeptr);
721 func->u.p.code[*codeptr].type = PS_BOOL;
722 func->u.p.code[*codeptr].u.b = 1;
727 resize_code(ctx, func, *codeptr);
728 func->u.p.code[*codeptr].type = PS_BOOL;
729 func->u.p.code[*codeptr].u.b = 0;
734 resize_code(ctx, func, *codeptr);
735 func->u.p.code[*codeptr].type = PS_REAL;
736 func->u.p.code[*codeptr].u.f = buf->f;
740 case PDF_TOK_OPEN_BRACE:
744 resize_code(ctx, func, *codeptr);
747 parse_code(func, stream, codeptr, buf);
749 tok = pdf_lex(stream, buf);
751 if (tok == PDF_TOK_OPEN_BRACE)
754 parse_code(func, stream, codeptr, buf);
756 tok = pdf_lex(stream, buf);
763 if (tok != PDF_TOK_KEYWORD)
764 fz_throw(ctx, FZ_ERROR_GENERIC, "missing keyword in 'if-else' context");
766 if (!strcmp(buf->scratch, "if"))
769 fz_throw(ctx, FZ_ERROR_GENERIC, "too many branches for 'if'");
770 func->u.p.code[opptr].type = PS_OPERATOR;
771 func->u.p.code[opptr].u.op = PS_OP_IF;
772 func->u.p.code[opptr+2].type = PS_BLOCK;
773 func->u.p.code[opptr+2].u.block = ifptr;
774 func->u.p.code[opptr+3].type = PS_BLOCK;
775 func->u.p.code[opptr+3].u.block = *codeptr;
777 else if (!strcmp(buf->scratch, "ifelse"))
780 fz_throw(ctx, FZ_ERROR_GENERIC, "not enough branches for 'ifelse'");
781 func->u.p.code[opptr].type = PS_OPERATOR;
782 func->u.p.code[opptr].u.op = PS_OP_IFELSE;
783 func->u.p.code[opptr+1].type = PS_BLOCK;
784 func->u.p.code[opptr+1].u.block = elseptr;
785 func->u.p.code[opptr+2].type = PS_BLOCK;
786 func->u.p.code[opptr+2].u.block = ifptr;
787 func->u.p.code[opptr+3].type = PS_BLOCK;
788 func->u.p.code[opptr+3].u.block = *codeptr;
792 fz_throw(ctx, FZ_ERROR_GENERIC, "unknown keyword in 'if-else' context: '%s'", buf->scratch);
796 case PDF_TOK_CLOSE_BRACE:
797 resize_code(ctx, func, *codeptr);
798 func->u.p.code[*codeptr].type = PS_OPERATOR;
799 func->u.p.code[*codeptr].u.op = PS_OP_RETURN;
803 case PDF_TOK_KEYWORD:
806 b = nelem(ps_op_names);
810 cmp = strcmp(buf->scratch, ps_op_names[mid]);
819 fz_throw(ctx, FZ_ERROR_GENERIC, "unknown operator: '%s'", buf->scratch);
820 if (a == PS_OP_IFELSE)
821 fz_throw(ctx, FZ_ERROR_GENERIC, "illegally positioned ifelse operator in function");
823 fz_throw(ctx, FZ_ERROR_GENERIC, "illegally positioned if operator in function");
825 resize_code(ctx, func, *codeptr);
826 func->u.p.code[*codeptr].type = PS_OPERATOR;
827 func->u.p.code[*codeptr].u.op = a;
832 fz_throw(ctx, FZ_ERROR_GENERIC, "calculator function syntax error");
838 load_postscript_func(pdf_function *func, pdf_document *doc, pdf_obj *dict, int num, int gen)
840 fz_stream *stream = NULL;
844 fz_context *ctx = doc->ctx;
847 pdf_lexbuf_init(ctx, &buf, PDF_LEXBUF_SMALL);
854 stream = pdf_open_stream(doc, num, gen);
856 tok = pdf_lex(stream, &buf);
857 if (tok != PDF_TOK_OPEN_BRACE)
859 fz_throw(ctx, FZ_ERROR_GENERIC, "stream is not a calculator function");
862 func->u.p.code = NULL;
866 parse_code(func, stream, &codeptr, &buf);
871 pdf_lexbuf_fin(&buf);
875 fz_rethrow_message(ctx, "cannot parse calculator function (%d %d R)", num, gen);
878 func->base.size += func->u.p.cap * sizeof(psobj);
882 eval_postscript_func(fz_context *ctx, pdf_function *func, const float *in, float *out)
890 for (i = 0; i < func->base.m; i++)
892 x = fz_clamp(in[i], func->domain[i][0], func->domain[i][1]);
893 ps_push_real(&st, x);
896 ps_run(ctx, func->u.p.code, &st, 0);
898 for (i = func->base.n - 1; i >= 0; i--)
900 x = ps_pop_real(&st);
901 out[i] = fz_clamp(x, func->range[i][0], func->range[i][1]);
909 #define MAX_SAMPLE_FUNCTION_SIZE (100 << 20)
912 load_sample_func(pdf_function *func, pdf_document *doc, pdf_obj *dict, int num, int gen)
914 fz_context *ctx = doc->ctx;
923 func->u.sa.samples = NULL;
925 obj = pdf_dict_gets(dict, "Size");
926 if (pdf_array_len(obj) < func->base.m)
927 fz_throw(ctx, FZ_ERROR_GENERIC, "too few sample function dimension sizes");
928 if (pdf_array_len(obj) > func->base.m)
929 fz_warn(ctx, "too many sample function dimension sizes");
930 for (i = 0; i < func->base.m; i++)
932 func->u.sa.size[i] = pdf_to_int(pdf_array_get(obj, i));
933 if (func->u.sa.size[i] <= 0)
935 fz_warn(ctx, "non-positive sample function dimension size");
936 func->u.sa.size[i] = 1;
940 obj = pdf_dict_gets(dict, "BitsPerSample");
941 func->u.sa.bps = bps = pdf_to_int(obj);
943 for (i = 0; i < func->base.m; i++)
945 func->u.sa.encode[i][0] = 0;
946 func->u.sa.encode[i][1] = func->u.sa.size[i] - 1;
948 obj = pdf_dict_gets(dict, "Encode");
949 if (pdf_is_array(obj))
951 int ranges = fz_mini(func->base.m, pdf_array_len(obj) / 2);
952 if (ranges != func->base.m)
953 fz_warn(ctx, "wrong number of sample function input mappings");
955 for (i = 0; i < ranges; i++)
957 func->u.sa.encode[i][0] = pdf_to_real(pdf_array_get(obj, i * 2 + 0));
958 func->u.sa.encode[i][1] = pdf_to_real(pdf_array_get(obj, i * 2 + 1));
962 for (i = 0; i < func->base.n; i++)
964 func->u.sa.decode[i][0] = func->range[i][0];
965 func->u.sa.decode[i][1] = func->range[i][1];
968 obj = pdf_dict_gets(dict, "Decode");
969 if (pdf_is_array(obj))
971 int ranges = fz_mini(func->base.n, pdf_array_len(obj) / 2);
972 if (ranges != func->base.n)
973 fz_warn(ctx, "wrong number of sample function output mappings");
975 for (i = 0; i < ranges; i++)
977 func->u.sa.decode[i][0] = pdf_to_real(pdf_array_get(obj, i * 2 + 0));
978 func->u.sa.decode[i][1] = pdf_to_real(pdf_array_get(obj, i * 2 + 1));
982 for (i = 0, samplecount = func->base.n; i < func->base.m; i++)
983 samplecount *= func->u.sa.size[i];
985 if (samplecount > MAX_SAMPLE_FUNCTION_SIZE)
986 fz_throw(ctx, FZ_ERROR_GENERIC, "sample function too large");
988 func->u.sa.samples = fz_malloc_array(ctx, samplecount, sizeof(float));
989 func->base.size += samplecount * sizeof(float);
991 stream = pdf_open_stream(doc, num, gen);
996 for (i = 0; i < samplecount; i++)
1001 if (fz_is_eof_bits(stream))
1002 fz_throw(ctx, FZ_ERROR_GENERIC, "truncated sample function stream");
1006 case 1: s = fz_read_bits(stream, 1); break;
1007 case 2: s = fz_read_bits(stream, 2) / 3.0f; break;
1008 case 4: s = fz_read_bits(stream, 4) / 15.0f; break;
1009 case 8: s = fz_read_byte(stream) / 255.0f; break;
1010 case 12: s = fz_read_bits(stream, 12) / 4095.0f; break;
1012 x = fz_read_byte(stream) << 8;
1013 x |= fz_read_byte(stream);
1017 x = fz_read_byte(stream) << 16;
1018 x |= fz_read_byte(stream) << 8;
1019 x |= fz_read_byte(stream);
1020 s = x / 16777215.0f;
1023 x = fz_read_byte(stream) << 24;
1024 x |= fz_read_byte(stream) << 16;
1025 x |= fz_read_byte(stream) << 8;
1026 x |= fz_read_byte(stream);
1027 s = x / 4294967295.0f;
1030 fz_throw(ctx, FZ_ERROR_GENERIC, "sample stream bit depth %d unsupported", bps);
1033 func->u.sa.samples[i] = s;
1047 interpolate_sample(pdf_function *func, int *scale, int *e0, int *e1, float *efrac, int dim, int idx)
1052 idx0 = e0[dim] * scale[dim] + idx;
1053 idx1 = e1[dim] * scale[dim] + idx;
1057 a = func->u.sa.samples[idx0];
1058 b = func->u.sa.samples[idx1];
1062 a = interpolate_sample(func, scale, e0, e1, efrac, dim - 1, idx0);
1063 b = interpolate_sample(func, scale, e0, e1, efrac, dim - 1, idx1);
1066 return a + (b - a) * efrac[dim];
1070 eval_sample_func(fz_context *ctx, pdf_function *func, const float *in, float *out)
1072 int e0[FZ_FN_MAXM], e1[FZ_FN_MAXM], scale[FZ_FN_MAXM];
1073 float efrac[FZ_FN_MAXM];
1077 /* encode input coordinates */
1078 for (i = 0; i < func->base.m; i++)
1080 x = fz_clamp(in[i], func->domain[i][0], func->domain[i][1]);
1081 x = lerp(x, func->domain[i][0], func->domain[i][1],
1082 func->u.sa.encode[i][0], func->u.sa.encode[i][1]);
1083 x = fz_clamp(x, 0, func->u.sa.size[i] - 1);
1086 efrac[i] = x - floorf(x);
1089 scale[0] = func->base.n;
1090 for (i = 1; i < func->base.m; i++)
1091 scale[i] = scale[i - 1] * func->u.sa.size[i-1];
1093 for (i = 0; i < func->base.n; i++)
1095 if (func->base.m == 1)
1097 float a = func->u.sa.samples[e0[0] * func->base.n + i];
1098 float b = func->u.sa.samples[e1[0] * func->base.n + i];
1100 float ab = a + (b - a) * efrac[0];
1102 out[i] = lerp(ab, 0, 1, func->u.sa.decode[i][0], func->u.sa.decode[i][1]);
1103 out[i] = fz_clamp(out[i], func->range[i][0], func->range[i][1]);
1106 else if (func->base.m == 2)
1108 int s0 = func->base.n;
1109 int s1 = s0 * func->u.sa.size[0];
1111 float a = func->u.sa.samples[e0[0] * s0 + e0[1] * s1 + i];
1112 float b = func->u.sa.samples[e1[0] * s0 + e0[1] * s1 + i];
1113 float c = func->u.sa.samples[e0[0] * s0 + e1[1] * s1 + i];
1114 float d = func->u.sa.samples[e1[0] * s0 + e1[1] * s1 + i];
1116 float ab = a + (b - a) * efrac[0];
1117 float cd = c + (d - c) * efrac[0];
1118 float abcd = ab + (cd - ab) * efrac[1];
1120 out[i] = lerp(abcd, 0, 1, func->u.sa.decode[i][0], func->u.sa.decode[i][1]);
1121 out[i] = fz_clamp(out[i], func->range[i][0], func->range[i][1]);
1126 x = interpolate_sample(func, scale, e0, e1, efrac, func->base.m - 1, i);
1127 out[i] = lerp(x, 0, 1, func->u.sa.decode[i][0], func->u.sa.decode[i][1]);
1128 out[i] = fz_clamp(out[i], func->range[i][0], func->range[i][1]);
1134 * Exponential function
1138 load_exponential_func(fz_context *ctx, pdf_function *func, pdf_obj *dict)
1143 if (func->base.m > 1)
1144 fz_warn(ctx, "exponential functions have at most one input");
1147 obj = pdf_dict_gets(dict, "N");
1148 func->u.e.n = pdf_to_real(obj);
1150 /* See exponential functions (PDF 1.7 section 3.9.2) */
1151 if (func->u.e.n != (int) func->u.e.n)
1153 /* If N is non-integer, input values may never be negative */
1154 for (i = 0; i < func->base.m; i++)
1155 if (func->domain[i][0] < 0 || func->domain[i][1] < 0)
1156 fz_warn(ctx, "exponential function input domain includes illegal negative input values");
1158 else if (func->u.e.n < 0)
1160 /* if N is negative, input values may never be zero */
1161 for (i = 0; i < func->base.m; i++)
1162 if (func->domain[i][0] == 0 || func->domain[i][1] == 0 ||
1163 (func->domain[i][0] < 0 && func->domain[i][1] > 0))
1164 fz_warn(ctx, "exponential function input domain includes illegal input value zero");
1167 for (i = 0; i < func->base.n; i++)
1169 func->u.e.c0[i] = 0;
1170 func->u.e.c1[i] = 1;
1173 obj = pdf_dict_gets(dict, "C0");
1174 if (pdf_is_array(obj))
1176 int ranges = fz_mini(func->base.n, pdf_array_len(obj));
1177 if (ranges != func->base.n)
1178 fz_warn(ctx, "wrong number of C0 constants for exponential function");
1180 for (i = 0; i < ranges; i++)
1181 func->u.e.c0[i] = pdf_to_real(pdf_array_get(obj, i));
1184 obj = pdf_dict_gets(dict, "C1");
1185 if (pdf_is_array(obj))
1187 int ranges = fz_mini(func->base.n, pdf_array_len(obj));
1188 if (ranges != func->base.n)
1189 fz_warn(ctx, "wrong number of C1 constants for exponential function");
1191 for (i = 0; i < ranges; i++)
1192 func->u.e.c1[i] = pdf_to_real(pdf_array_get(obj, i));
1197 eval_exponential_func(fz_context *ctx, pdf_function *func, float in, float *out)
1203 x = fz_clamp(x, func->domain[0][0], func->domain[0][1]);
1205 /* Default output is zero, which is suitable for violated constraints */
1206 if ((func->u.e.n != (int)func->u.e.n && x < 0) || (func->u.e.n < 0 && x == 0))
1209 tmp = powf(x, func->u.e.n);
1210 for (i = 0; i < func->base.n; i++)
1212 out[i] = func->u.e.c0[i] + tmp * (func->u.e.c1[i] - func->u.e.c0[i]);
1213 if (func->has_range)
1214 out[i] = fz_clamp(out[i], func->range[i][0], func->range[i][1]);
1219 * Stitching function
1223 load_stitching_func(pdf_function *func, pdf_document *doc, pdf_obj *dict)
1225 fz_context *ctx = doc->ctx;
1226 fz_function **funcs;
1235 if (func->base.m > 1)
1236 fz_warn(ctx, "stitching functions have at most one input");
1239 obj = pdf_dict_gets(dict, "Functions");
1240 if (!pdf_is_array(obj))
1241 fz_throw(ctx, FZ_ERROR_GENERIC, "stitching function has no input functions");
1246 k = pdf_array_len(obj);
1248 func->u.st.funcs = fz_malloc_array(ctx, k, sizeof(fz_function*));
1249 func->u.st.bounds = fz_malloc_array(ctx, k - 1, sizeof(float));
1250 func->u.st.encode = fz_malloc_array(ctx, k * 2, sizeof(float));
1251 funcs = func->u.st.funcs;
1253 for (i = 0; i < k; i++)
1255 sub = pdf_array_get(obj, i);
1256 funcs[i] = pdf_load_function(doc, sub, 1, func->base.n);
1258 func->base.size += fz_function_size(funcs[i]);
1261 if (funcs[i]->m != func->base.m)
1262 fz_warn(ctx, "wrong number of inputs for sub function %d", i);
1263 if (funcs[i]->n != func->base.n)
1264 fz_warn(ctx, "wrong number of outputs for sub function %d", i);
1269 pdf_unmark_obj(obj);
1276 obj = pdf_dict_gets(dict, "Bounds");
1277 if (!pdf_is_array(obj))
1278 fz_throw(ctx, FZ_ERROR_GENERIC, "stitching function has no bounds");
1280 if (pdf_array_len(obj) < k - 1)
1281 fz_throw(ctx, FZ_ERROR_GENERIC, "too few subfunction boundaries");
1282 if (pdf_array_len(obj) > k)
1283 fz_warn(ctx, "too many subfunction boundaries");
1285 for (i = 0; i < k - 1; i++)
1287 num = pdf_array_get(obj, i);
1288 func->u.st.bounds[i] = pdf_to_real(num);
1289 if (i && func->u.st.bounds[i - 1] > func->u.st.bounds[i])
1290 fz_throw(ctx, FZ_ERROR_GENERIC, "subfunction %d boundary out of range", i);
1293 if (k > 1 && (func->domain[0][0] > func->u.st.bounds[0] ||
1294 func->domain[0][1] < func->u.st.bounds[k - 2]))
1295 fz_warn(ctx, "subfunction boundaries outside of input mapping");
1298 for (i = 0; i < k; i++)
1300 func->u.st.encode[i * 2 + 0] = 0;
1301 func->u.st.encode[i * 2 + 1] = 0;
1304 obj = pdf_dict_gets(dict, "Encode");
1305 if (pdf_is_array(obj))
1307 int ranges = fz_mini(k, pdf_array_len(obj) / 2);
1309 fz_warn(ctx, "wrong number of stitching function input mappings");
1311 for (i = 0; i < ranges; i++)
1313 func->u.st.encode[i * 2 + 0] = pdf_to_real(pdf_array_get(obj, i * 2 + 0));
1314 func->u.st.encode[i * 2 + 1] = pdf_to_real(pdf_array_get(obj, i * 2 + 1));
1320 eval_stitching_func(fz_context *ctx, pdf_function *func, float in, float *out)
1323 int k = func->u.st.k;
1324 float *bounds = func->u.st.bounds;
1327 in = fz_clamp(in, func->domain[0][0], func->domain[0][1]);
1329 for (i = 0; i < k - 1; i++)
1335 if (i == 0 && k == 1)
1337 low = func->domain[0][0];
1338 high = func->domain[0][1];
1342 low = func->domain[0][0];
1345 else if (i == k - 1)
1347 low = bounds[k - 2];
1348 high = func->domain[0][1];
1352 low = bounds[i - 1];
1356 in = lerp(in, low, high, func->u.st.encode[i * 2 + 0], func->u.st.encode[i * 2 + 1]);
1358 fz_eval_function(ctx, func->u.st.funcs[i], &in, 1, out, func->u.st.funcs[i]->n);
1366 pdf_free_function_imp(fz_context *ctx, fz_storable *func_)
1368 pdf_function *func = (pdf_function *)func_;
1374 fz_free(ctx, func->u.sa.samples);
1379 for (i = 0; i < func->u.st.k; i++)
1380 fz_drop_function(ctx, func->u.st.funcs[i]);
1381 fz_free(ctx, func->u.st.funcs);
1382 fz_free(ctx, func->u.st.bounds);
1383 fz_free(ctx, func->u.st.encode);
1386 fz_free(ctx, func->u.p.code);
1393 pdf_eval_function(fz_context *ctx, fz_function *func_, const float *in, float *out)
1395 pdf_function *func = (pdf_function *)func_;
1399 case SAMPLE: eval_sample_func(ctx, func, in, out); break;
1400 case EXPONENTIAL: eval_exponential_func(ctx, func, *in, out); break;
1401 case STITCHING: eval_stitching_func(ctx, func, *in, out); break;
1402 case POSTSCRIPT: eval_postscript_func(ctx, func, in, out); break;
1412 pdf_debug_indent(char *prefix, int level, char *suffix)
1416 printf("%s", prefix);
1418 for (i = 0; i < level; i++)
1421 printf("%s", suffix);
1425 pdf_debug_ps_func_code(psobj *funccode, psobj *code, int level)
1429 pdf_debug_indent("", level, "{");
1431 /* Print empty blocks as { }, instead of separating braces on different lines. */
1432 if (code->type == PS_OPERATOR && code->u.op == PS_OP_RETURN)
1438 pdf_debug_indent("\n", ++level, "");
1448 pdf_debug_indent("\n", level, "");
1450 printf("%d ", code->u.i);
1457 pdf_debug_indent("\n", level, "");
1459 printf("%g ", code->u.f);
1465 if (code->u.op == PS_OP_RETURN)
1470 else if (code->u.op == PS_OP_IF)
1473 pdf_debug_ps_func_code(funccode, &funccode[(code + 2)->u.block], level);
1475 printf("%s", ps_op_names[code->u.op]);
1476 code = &funccode[(code + 3)->u.block];
1477 if (code->type != PS_OPERATOR || code->u.op != PS_OP_RETURN)
1478 pdf_debug_indent("\n", level, "");
1482 else if (code->u.op == PS_OP_IFELSE)
1485 pdf_debug_ps_func_code(funccode, &funccode[(code + 2)->u.block], level);
1488 pdf_debug_ps_func_code(funccode, &funccode[(code + 1)->u.block], level);
1490 printf("%s", ps_op_names[code->u.op]);
1491 code = &funccode[(code + 3)->u.block];
1492 if (code->type != PS_OPERATOR || code->u.op != PS_OP_RETURN)
1493 pdf_debug_indent("\n", level, "");
1499 printf("%s ", ps_op_names[code->u.op]);
1507 pdf_debug_indent("", --level, "} ");
1511 pdf_debug_function_imp(fz_function *func_, int level)
1514 pdf_function *func = (pdf_function *)func_;
1516 pdf_debug_indent("", level, "function {\n");
1518 pdf_debug_indent("", ++level, "");
1525 printf("exponential");
1528 printf("stitching");
1531 printf("postscript");
1535 pdf_debug_indent("\n", level, "");
1536 printf("%d input -> %d output\n", func->base.m, func->base.n);
1538 pdf_debug_indent("", level, "domain ");
1539 for (i = 0; i < func->base.m; i++)
1540 printf("%g %g ", func->domain[i][0], func->domain[i][1]);
1543 if (func->has_range)
1545 pdf_debug_indent("", level, "range ");
1546 for (i = 0; i < func->base.n; i++)
1547 printf("%g %g ", func->range[i][0], func->range[i][1]);
1554 pdf_debug_indent("", level, "");
1555 printf("bps: %d\n", func->u.sa.bps);
1557 pdf_debug_indent("", level, "");
1559 for (i = 0; i < func->base.m; i++)
1560 printf("%d ", func->u.sa.size[i]);
1563 pdf_debug_indent("", level, "");
1564 printf("encode: [ ");
1565 for (i = 0; i < func->base.m; i++)
1566 printf("%g %g ", func->u.sa.encode[i][0], func->u.sa.encode[i][1]);
1569 pdf_debug_indent("", level, "");
1570 printf("decode: [ ");
1571 for (i = 0; i < func->base.m; i++)
1572 printf("%g %g ", func->u.sa.decode[i][0], func->u.sa.decode[i][1]);
1577 pdf_debug_indent("", level, "");
1578 printf("n: %g\n", func->u.e.n);
1580 pdf_debug_indent("", level, "");
1582 for (i = 0; i < func->base.n; i++)
1583 printf("%g ", func->u.e.c0[i]);
1586 pdf_debug_indent("", level, "");
1588 for (i = 0; i < func->base.n; i++)
1589 printf("%g ", func->u.e.c1[i]);
1594 pdf_debug_indent("", level, "");
1595 printf("%d functions\n", func->u.st.k);
1597 pdf_debug_indent("", level, "");
1598 printf("bounds: [ ");
1599 for (i = 0; i < func->u.st.k - 1; i++)
1600 printf("%g ", func->u.st.bounds[i]);
1603 pdf_debug_indent("", level, "");
1604 printf("encode: [ ");
1605 for (i = 0; i < func->u.st.k * 2; i++)
1606 printf("%g ", func->u.st.encode[i]);
1609 for (i = 0; i < func->u.st.k; i++)
1610 pdf_debug_function_imp(func->u.st.funcs[i], level);
1614 pdf_debug_ps_func_code(func->u.p.code, func->u.p.code, level);
1619 pdf_debug_indent("", --level, "}\n");
1623 pdf_debug_function(fz_function *func)
1625 pdf_debug_function_imp(func, 0);
1630 pdf_load_function(pdf_document *doc, pdf_obj *dict, int in, int out)
1632 fz_context *ctx = doc->ctx;
1637 if (pdf_obj_marked(dict))
1638 fz_throw(ctx, FZ_ERROR_GENERIC, "Recursion in function definition");
1640 if ((func = pdf_find_item(ctx, pdf_free_function_imp, dict)) != NULL)
1642 return (fz_function *)func;
1645 func = fz_malloc_struct(ctx, pdf_function);
1646 FZ_INIT_STORABLE(&func->base, 1, pdf_free_function_imp);
1647 func->base.size = sizeof(*func);
1648 func->base.evaluate = pdf_eval_function;
1650 func->base.debug = pdf_debug_function;
1653 obj = pdf_dict_gets(dict, "FunctionType");
1654 func->type = pdf_to_int(obj);
1656 /* required for all */
1657 obj = pdf_dict_gets(dict, "Domain");
1658 func->base.m = fz_clampi(pdf_array_len(obj) / 2, 1, FZ_FN_MAXM);
1659 for (i = 0; i < func->base.m; i++)
1661 func->domain[i][0] = pdf_to_real(pdf_array_get(obj, i * 2 + 0));
1662 func->domain[i][1] = pdf_to_real(pdf_array_get(obj, i * 2 + 1));
1665 /* required for type0 and type4, optional otherwise */
1666 obj = pdf_dict_gets(dict, "Range");
1667 if (pdf_is_array(obj))
1669 func->has_range = 1;
1670 func->base.n = fz_clampi(pdf_array_len(obj) / 2, 1, FZ_FN_MAXN);
1671 for (i = 0; i < func->base.n; i++)
1673 func->range[i][0] = pdf_to_real(pdf_array_get(obj, i * 2 + 0));
1674 func->range[i][1] = pdf_to_real(pdf_array_get(obj, i * 2 + 1));
1679 func->has_range = 0;
1683 if (func->base.m != in)
1684 fz_warn(ctx, "wrong number of function inputs");
1685 if (func->base.n != out)
1686 fz_warn(ctx, "wrong number of function outputs");
1693 load_sample_func(func, doc, dict, pdf_to_num(dict), pdf_to_gen(dict));
1697 load_exponential_func(ctx, func, dict);
1701 load_stitching_func(func, doc, dict);
1705 load_postscript_func(func, doc, dict, pdf_to_num(dict), pdf_to_gen(dict));
1709 fz_throw(ctx, FZ_ERROR_GENERIC, "unknown function type (%d %d R)", pdf_to_num(dict), pdf_to_gen(dict));
1712 pdf_store_item(ctx, dict, func, func->base.size);
1716 int type = func->type;
1717 fz_drop_function(ctx, (fz_function *)func);
1718 fz_rethrow_message(ctx, "cannot load %s function (%d %d R)",
1719 type == SAMPLE ? "sampled" :
1720 type == EXPONENTIAL ? "exponential" :
1721 type == STITCHING ? "stitching" :
1722 type == POSTSCRIPT ? "calculator" :
1724 pdf_to_num(dict), pdf_to_gen(dict));
1727 return (fz_function *)func;