pdf-function.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "mupdf/pdf.h"
  24. #include <string.h>
  25. #include <math.h>
  26. #include <float.h>
  27. #include <limits.h>
  28. static pdf_function *pdf_load_function_imp(fz_context *ctx, pdf_obj *dict, int in, int out, pdf_cycle_list *cycle);
  29. #define DIV_BY_ZERO(a, b, min, max) (((a) < 0) ^ ((b) < 0) ? (min) : (max))
  30. enum
  31. {
  32. MAX_N = FZ_MAX_COLORS,
  33. MAX_M = FZ_MAX_COLORS
  34. };
  35. enum
  36. {
  37. SAMPLE = 0,
  38. EXPONENTIAL = 2,
  39. STITCHING = 3,
  40. POSTSCRIPT = 4
  41. };
  42. typedef struct
  43. {
  44. int type;
  45. union
  46. {
  47. int b; /* boolean (stack only) */
  48. int i; /* integer (stack and code) */
  49. float f; /* real (stack and code) */
  50. int op; /* operator (code only) */
  51. int block; /* if/ifelse block pointer (code only) */
  52. } u;
  53. } psobj;
  54. struct pdf_function
  55. {
  56. fz_function super;
  57. float domain[MAX_M][2]; /* even index : min value, odd index : max value */
  58. float range[MAX_N][2]; /* even index : min value, odd index : max value */
  59. int has_range;
  60. };
  61. typedef struct
  62. {
  63. pdf_function super;
  64. unsigned short bps;
  65. int size[MAX_M];
  66. float encode[MAX_M][2];
  67. float decode[MAX_N][2];
  68. float *samples;
  69. } pdf_function_sa;
  70. typedef struct
  71. {
  72. pdf_function super;
  73. float n;
  74. float c0[MAX_N];
  75. float c1[MAX_N];
  76. } pdf_function_e;
  77. typedef struct
  78. {
  79. pdf_function super;
  80. int k;
  81. pdf_function **funcs; /* k */
  82. float *bounds; /* k - 1 */
  83. float *encode; /* k * 2 */
  84. } pdf_function_st;
  85. typedef struct
  86. {
  87. pdf_function super;
  88. psobj *code;
  89. int cap;
  90. } pdf_function_p;
  91. pdf_function *
  92. pdf_keep_function(fz_context *ctx, pdf_function *func)
  93. {
  94. return (pdf_function *)fz_keep_function(ctx, &func->super);
  95. }
  96. void
  97. pdf_drop_function(fz_context *ctx, pdf_function *func)
  98. {
  99. fz_drop_function(ctx, &func->super);
  100. }
  101. size_t
  102. pdf_function_size(fz_context *ctx, pdf_function *func)
  103. {
  104. return fz_function_size(ctx, &func->super);
  105. }
  106. static inline float lerp(float x, float xmin, float xmax, float ymin, float ymax)
  107. {
  108. if (xmin == xmax)
  109. return ymin;
  110. if (ymin == ymax)
  111. return ymin;
  112. return ymin + (x - xmin) * (ymax - ymin) / (xmax - xmin);
  113. }
  114. /*
  115. * PostScript calculator
  116. */
  117. enum { PS_BOOL, PS_INT, PS_REAL, PS_OPERATOR, PS_BLOCK };
  118. enum
  119. {
  120. PS_OP_ABS, PS_OP_ADD, PS_OP_AND, PS_OP_ATAN, PS_OP_BITSHIFT,
  121. PS_OP_CEILING, PS_OP_COPY, PS_OP_COS, PS_OP_CVI, PS_OP_CVR,
  122. PS_OP_DIV, PS_OP_DUP, PS_OP_EQ, PS_OP_EXCH, PS_OP_EXP,
  123. PS_OP_FALSE, PS_OP_FLOOR, PS_OP_GE, PS_OP_GT, PS_OP_IDIV, PS_OP_IF,
  124. PS_OP_IFELSE, PS_OP_INDEX, PS_OP_LE, PS_OP_LN, PS_OP_LOG, PS_OP_LT,
  125. PS_OP_MOD, PS_OP_MUL, PS_OP_NE, PS_OP_NEG, PS_OP_NOT, PS_OP_OR,
  126. PS_OP_POP, PS_OP_RETURN, PS_OP_ROLL, PS_OP_ROUND, PS_OP_SIN,
  127. PS_OP_SQRT, PS_OP_SUB, PS_OP_TRUE, PS_OP_TRUNCATE, PS_OP_XOR
  128. };
  129. static char *ps_op_names[] =
  130. {
  131. "abs", "add", "and", "atan", "bitshift", "ceiling", "copy",
  132. "cos", "cvi", "cvr", "div", "dup", "eq", "exch", "exp",
  133. "false", "floor", "ge", "gt", "idiv", "if", "ifelse", "index", "le", "ln",
  134. "log", "lt", "mod", "mul", "ne", "neg", "not", "or", "pop", "return",
  135. "roll", "round", "sin", "sqrt", "sub", "true", "truncate", "xor"
  136. };
  137. typedef struct
  138. {
  139. psobj stack[100];
  140. int sp;
  141. } ps_stack;
  142. static void
  143. ps_init_stack(ps_stack *st)
  144. {
  145. memset(st->stack, 0, sizeof(st->stack));
  146. st->sp = 0;
  147. }
  148. static inline int ps_overflow(ps_stack *st, int n)
  149. {
  150. return n < 0 || st->sp + n >= (int)nelem(st->stack);
  151. }
  152. static inline int ps_underflow(ps_stack *st, int n)
  153. {
  154. return n < 0 || n > st->sp;
  155. }
  156. static inline int ps_is_type(ps_stack *st, int t)
  157. {
  158. return !ps_underflow(st, 1) && st->stack[st->sp - 1].type == t;
  159. }
  160. static inline int ps_is_type2(ps_stack *st, int t)
  161. {
  162. return !ps_underflow(st, 2) && st->stack[st->sp - 1].type == t && st->stack[st->sp - 2].type == t;
  163. }
  164. static void
  165. ps_push_bool(ps_stack *st, int b)
  166. {
  167. if (!ps_overflow(st, 1))
  168. {
  169. st->stack[st->sp].type = PS_BOOL;
  170. st->stack[st->sp].u.b = b;
  171. st->sp++;
  172. }
  173. }
  174. static void
  175. ps_push_int(ps_stack *st, int n)
  176. {
  177. if (!ps_overflow(st, 1))
  178. {
  179. st->stack[st->sp].type = PS_INT;
  180. st->stack[st->sp].u.i = n;
  181. st->sp++;
  182. }
  183. }
  184. static void
  185. ps_push_real(ps_stack *st, float n)
  186. {
  187. if (!ps_overflow(st, 1))
  188. {
  189. st->stack[st->sp].type = PS_REAL;
  190. if (isnan(n))
  191. {
  192. /* Push 1.0, as it's a small known value that won't
  193. * cause a divide by 0. Same reason as in fz_atof. */
  194. n = 1.0f;
  195. }
  196. st->stack[st->sp].u.f = fz_clamp(n, -FLT_MAX, FLT_MAX);
  197. st->sp++;
  198. }
  199. }
  200. static int
  201. ps_pop_bool(ps_stack *st)
  202. {
  203. if (!ps_underflow(st, 1))
  204. {
  205. if (ps_is_type(st, PS_BOOL))
  206. return st->stack[--st->sp].u.b;
  207. }
  208. return 0;
  209. }
  210. static int
  211. ps_pop_int(ps_stack *st)
  212. {
  213. if (!ps_underflow(st, 1))
  214. {
  215. if (ps_is_type(st, PS_INT))
  216. return st->stack[--st->sp].u.i;
  217. if (ps_is_type(st, PS_REAL))
  218. return st->stack[--st->sp].u.f;
  219. }
  220. return 0;
  221. }
  222. static float
  223. ps_pop_real(ps_stack *st)
  224. {
  225. if (!ps_underflow(st, 1))
  226. {
  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;
  231. }
  232. return 0;
  233. }
  234. static void
  235. ps_copy(ps_stack *st, int n)
  236. {
  237. if (!ps_underflow(st, n) && !ps_overflow(st, n))
  238. {
  239. memcpy(st->stack + st->sp, st->stack + st->sp - n, n * sizeof(psobj));
  240. st->sp += n;
  241. }
  242. }
  243. static void
  244. ps_roll(ps_stack *st, int n, int j)
  245. {
  246. psobj tmp;
  247. int i;
  248. if (ps_underflow(st, n) || j == 0 || n == 0)
  249. return;
  250. if (j >= 0)
  251. {
  252. j %= n;
  253. }
  254. else
  255. {
  256. j = -j % n;
  257. if (j != 0)
  258. j = n - j;
  259. }
  260. for (i = 0; i < j; i++)
  261. {
  262. tmp = st->stack[st->sp - 1];
  263. memmove(st->stack + st->sp - n + 1, st->stack + st->sp - n, n * sizeof(psobj));
  264. st->stack[st->sp - n] = tmp;
  265. }
  266. }
  267. static void
  268. ps_index(ps_stack *st, int n)
  269. {
  270. if (!ps_overflow(st, 1) && !ps_underflow(st, n + 1))
  271. {
  272. st->stack[st->sp] = st->stack[st->sp - n - 1];
  273. st->sp++;
  274. }
  275. }
  276. static void
  277. ps_run(fz_context *ctx, psobj *code, ps_stack *st, int pc)
  278. {
  279. int i1, i2;
  280. float r1, r2;
  281. int b1, b2;
  282. while (1)
  283. {
  284. switch (code[pc].type)
  285. {
  286. case PS_INT:
  287. ps_push_int(st, code[pc++].u.i);
  288. break;
  289. case PS_REAL:
  290. ps_push_real(st, code[pc++].u.f);
  291. break;
  292. case PS_OPERATOR:
  293. switch (code[pc++].u.op)
  294. {
  295. case PS_OP_ABS:
  296. if (ps_is_type(st, PS_INT))
  297. ps_push_int(st, fz_absi(ps_pop_int(st)));
  298. else
  299. ps_push_real(st, fz_abs(ps_pop_real(st)));
  300. break;
  301. case PS_OP_ADD:
  302. if (ps_is_type2(st, PS_INT)) {
  303. i2 = ps_pop_int(st);
  304. i1 = ps_pop_int(st);
  305. ps_push_int(st, i1 + i2);
  306. }
  307. else {
  308. r2 = ps_pop_real(st);
  309. r1 = ps_pop_real(st);
  310. ps_push_real(st, r1 + r2);
  311. }
  312. break;
  313. case PS_OP_AND:
  314. if (ps_is_type2(st, PS_INT)) {
  315. i2 = ps_pop_int(st);
  316. i1 = ps_pop_int(st);
  317. ps_push_int(st, i1 & i2);
  318. }
  319. else {
  320. b2 = ps_pop_bool(st);
  321. b1 = ps_pop_bool(st);
  322. ps_push_bool(st, b1 && b2);
  323. }
  324. break;
  325. case PS_OP_ATAN:
  326. r2 = ps_pop_real(st);
  327. r1 = ps_pop_real(st);
  328. r1 = atan2f(r1, r2) * FZ_RADIAN;
  329. if (r1 < 0)
  330. r1 += 360;
  331. ps_push_real(st, r1);
  332. break;
  333. case PS_OP_BITSHIFT:
  334. i2 = ps_pop_int(st);
  335. i1 = ps_pop_int(st);
  336. if (i2 > 0 && i2 < 8 * (int)sizeof (i2))
  337. ps_push_int(st, i1 << i2);
  338. else if (i2 < 0 && i2 > -8 * (int)sizeof (i2))
  339. ps_push_int(st, (int)((unsigned int)i1 >> -i2));
  340. else
  341. ps_push_int(st, i1);
  342. break;
  343. case PS_OP_CEILING:
  344. r1 = ps_pop_real(st);
  345. ps_push_real(st, ceilf(r1));
  346. break;
  347. case PS_OP_COPY:
  348. ps_copy(st, ps_pop_int(st));
  349. break;
  350. case PS_OP_COS:
  351. r1 = ps_pop_real(st);
  352. ps_push_real(st, cosf(r1/FZ_RADIAN));
  353. break;
  354. case PS_OP_CVI:
  355. ps_push_int(st, ps_pop_int(st));
  356. break;
  357. case PS_OP_CVR:
  358. ps_push_real(st, ps_pop_real(st));
  359. break;
  360. case PS_OP_DIV:
  361. r2 = ps_pop_real(st);
  362. r1 = ps_pop_real(st);
  363. if (fabsf(r2) >= FLT_EPSILON)
  364. ps_push_real(st, r1 / r2);
  365. else
  366. ps_push_real(st, DIV_BY_ZERO(r1, r2, -FLT_MAX, FLT_MAX));
  367. break;
  368. case PS_OP_DUP:
  369. ps_copy(st, 1);
  370. break;
  371. case PS_OP_EQ:
  372. if (ps_is_type2(st, PS_BOOL)) {
  373. b2 = ps_pop_bool(st);
  374. b1 = ps_pop_bool(st);
  375. ps_push_bool(st, b1 == b2);
  376. }
  377. else if (ps_is_type2(st, PS_INT)) {
  378. i2 = ps_pop_int(st);
  379. i1 = ps_pop_int(st);
  380. ps_push_bool(st, i1 == i2);
  381. }
  382. else {
  383. r2 = ps_pop_real(st);
  384. r1 = ps_pop_real(st);
  385. ps_push_bool(st, r1 == r2);
  386. }
  387. break;
  388. case PS_OP_EXCH:
  389. ps_roll(st, 2, 1);
  390. break;
  391. case PS_OP_EXP:
  392. r2 = ps_pop_real(st);
  393. r1 = ps_pop_real(st);
  394. ps_push_real(st, powf(r1, r2));
  395. break;
  396. case PS_OP_FALSE:
  397. ps_push_bool(st, 0);
  398. break;
  399. case PS_OP_FLOOR:
  400. r1 = ps_pop_real(st);
  401. ps_push_real(st, floorf(r1));
  402. break;
  403. case PS_OP_GE:
  404. if (ps_is_type2(st, PS_INT)) {
  405. i2 = ps_pop_int(st);
  406. i1 = ps_pop_int(st);
  407. ps_push_bool(st, i1 >= i2);
  408. }
  409. else {
  410. r2 = ps_pop_real(st);
  411. r1 = ps_pop_real(st);
  412. ps_push_bool(st, r1 >= r2);
  413. }
  414. break;
  415. case PS_OP_GT:
  416. if (ps_is_type2(st, PS_INT)) {
  417. i2 = ps_pop_int(st);
  418. i1 = ps_pop_int(st);
  419. ps_push_bool(st, i1 > i2);
  420. }
  421. else {
  422. r2 = ps_pop_real(st);
  423. r1 = ps_pop_real(st);
  424. ps_push_bool(st, r1 > r2);
  425. }
  426. break;
  427. case PS_OP_IDIV:
  428. i2 = ps_pop_int(st);
  429. i1 = ps_pop_int(st);
  430. if (i2 == 0) {
  431. ps_push_int(st, DIV_BY_ZERO(i1, i2, INT_MIN, INT_MAX));
  432. }
  433. else if (i1 == INT_MIN && i2 == -1) {
  434. ps_push_int(st, INT_MAX);
  435. }
  436. else {
  437. ps_push_int(st, i1 / i2);
  438. }
  439. break;
  440. case PS_OP_INDEX:
  441. ps_index(st, ps_pop_int(st));
  442. break;
  443. case PS_OP_LE:
  444. if (ps_is_type2(st, PS_INT)) {
  445. i2 = ps_pop_int(st);
  446. i1 = ps_pop_int(st);
  447. ps_push_bool(st, i1 <= i2);
  448. }
  449. else {
  450. r2 = ps_pop_real(st);
  451. r1 = ps_pop_real(st);
  452. ps_push_bool(st, r1 <= r2);
  453. }
  454. break;
  455. case PS_OP_LN:
  456. r1 = ps_pop_real(st);
  457. /* Bug 692941 - logf as separate statement */
  458. r2 = logf(r1);
  459. ps_push_real(st, r2);
  460. break;
  461. case PS_OP_LOG:
  462. r1 = ps_pop_real(st);
  463. ps_push_real(st, log10f(r1));
  464. break;
  465. case PS_OP_LT:
  466. if (ps_is_type2(st, PS_INT)) {
  467. i2 = ps_pop_int(st);
  468. i1 = ps_pop_int(st);
  469. ps_push_bool(st, i1 < i2);
  470. }
  471. else {
  472. r2 = ps_pop_real(st);
  473. r1 = ps_pop_real(st);
  474. ps_push_bool(st, r1 < r2);
  475. }
  476. break;
  477. case PS_OP_MOD:
  478. i2 = ps_pop_int(st);
  479. i1 = ps_pop_int(st);
  480. if (i2 == 0) {
  481. ps_push_int(st, DIV_BY_ZERO(i1, i2, INT_MIN, INT_MAX));
  482. }
  483. else if (i1 == INT_MIN && i2 == -1) {
  484. ps_push_int(st, 0);
  485. }
  486. else {
  487. ps_push_int(st, i1 % i2);
  488. }
  489. break;
  490. case PS_OP_MUL:
  491. if (ps_is_type2(st, PS_INT)) {
  492. i2 = ps_pop_int(st);
  493. i1 = ps_pop_int(st);
  494. ps_push_int(st, i1 * i2);
  495. }
  496. else {
  497. r2 = ps_pop_real(st);
  498. r1 = ps_pop_real(st);
  499. ps_push_real(st, r1 * r2);
  500. }
  501. break;
  502. case PS_OP_NE:
  503. if (ps_is_type2(st, PS_BOOL)) {
  504. b2 = ps_pop_bool(st);
  505. b1 = ps_pop_bool(st);
  506. ps_push_bool(st, b1 != b2);
  507. }
  508. else if (ps_is_type2(st, PS_INT)) {
  509. i2 = ps_pop_int(st);
  510. i1 = ps_pop_int(st);
  511. ps_push_bool(st, i1 != i2);
  512. }
  513. else {
  514. r2 = ps_pop_real(st);
  515. r1 = ps_pop_real(st);
  516. ps_push_bool(st, r1 != r2);
  517. }
  518. break;
  519. case PS_OP_NEG:
  520. if (ps_is_type(st, PS_INT))
  521. ps_push_int(st, -ps_pop_int(st));
  522. else
  523. ps_push_real(st, -ps_pop_real(st));
  524. break;
  525. case PS_OP_NOT:
  526. if (ps_is_type(st, PS_BOOL))
  527. ps_push_bool(st, !ps_pop_bool(st));
  528. else
  529. ps_push_int(st, ~ps_pop_int(st));
  530. break;
  531. case PS_OP_OR:
  532. if (ps_is_type2(st, PS_BOOL)) {
  533. b2 = ps_pop_bool(st);
  534. b1 = ps_pop_bool(st);
  535. ps_push_bool(st, b1 || b2);
  536. }
  537. else {
  538. i2 = ps_pop_int(st);
  539. i1 = ps_pop_int(st);
  540. ps_push_int(st, i1 | i2);
  541. }
  542. break;
  543. case PS_OP_POP:
  544. if (!ps_underflow(st, 1))
  545. st->sp--;
  546. break;
  547. case PS_OP_ROLL:
  548. i2 = ps_pop_int(st);
  549. i1 = ps_pop_int(st);
  550. ps_roll(st, i1, i2);
  551. break;
  552. case PS_OP_ROUND:
  553. if (!ps_is_type(st, PS_INT)) {
  554. r1 = ps_pop_real(st);
  555. ps_push_real(st, (r1 >= 0) ? floorf(r1 + 0.5f) : ceilf(r1 - 0.5f));
  556. }
  557. break;
  558. case PS_OP_SIN:
  559. r1 = ps_pop_real(st);
  560. ps_push_real(st, sinf(r1/FZ_RADIAN));
  561. break;
  562. case PS_OP_SQRT:
  563. r1 = ps_pop_real(st);
  564. ps_push_real(st, sqrtf(r1));
  565. break;
  566. case PS_OP_SUB:
  567. if (ps_is_type2(st, PS_INT)) {
  568. i2 = ps_pop_int(st);
  569. i1 = ps_pop_int(st);
  570. ps_push_int(st, i1 - i2);
  571. }
  572. else {
  573. r2 = ps_pop_real(st);
  574. r1 = ps_pop_real(st);
  575. ps_push_real(st, r1 - r2);
  576. }
  577. break;
  578. case PS_OP_TRUE:
  579. ps_push_bool(st, 1);
  580. break;
  581. case PS_OP_TRUNCATE:
  582. if (!ps_is_type(st, PS_INT)) {
  583. r1 = ps_pop_real(st);
  584. ps_push_real(st, (r1 >= 0) ? floorf(r1) : ceilf(r1));
  585. }
  586. break;
  587. case PS_OP_XOR:
  588. if (ps_is_type2(st, PS_BOOL)) {
  589. b2 = ps_pop_bool(st);
  590. b1 = ps_pop_bool(st);
  591. ps_push_bool(st, b1 ^ b2);
  592. }
  593. else {
  594. i2 = ps_pop_int(st);
  595. i1 = ps_pop_int(st);
  596. ps_push_int(st, i1 ^ i2);
  597. }
  598. break;
  599. case PS_OP_IF:
  600. b1 = ps_pop_bool(st);
  601. if (b1)
  602. ps_run(ctx, code, st, code[pc + 1].u.block);
  603. pc = code[pc + 2].u.block;
  604. break;
  605. case PS_OP_IFELSE:
  606. b1 = ps_pop_bool(st);
  607. if (b1)
  608. ps_run(ctx, code, st, code[pc + 1].u.block);
  609. else
  610. ps_run(ctx, code, st, code[pc + 0].u.block);
  611. pc = code[pc + 2].u.block;
  612. break;
  613. case PS_OP_RETURN:
  614. return;
  615. default:
  616. fz_warn(ctx, "foreign operator in calculator function");
  617. return;
  618. }
  619. break;
  620. default:
  621. fz_warn(ctx, "foreign object in calculator function");
  622. return;
  623. }
  624. }
  625. }
  626. static void
  627. resize_code(fz_context *ctx, pdf_function_p *func, int newsize)
  628. {
  629. if (newsize >= func->cap)
  630. {
  631. int new_cap = func->cap + 64;
  632. func->code = fz_realloc_array(ctx, func->code, new_cap, psobj);
  633. func->cap = new_cap;
  634. }
  635. }
  636. static void
  637. parse_code(fz_context *ctx, pdf_function_p *func, fz_stream *stream, int *codeptr, pdf_lexbuf *buf, int depth)
  638. {
  639. pdf_token tok;
  640. int opptr, elseptr, ifptr;
  641. int a, b, mid, cmp;
  642. if (depth > 100)
  643. fz_throw(ctx, FZ_ERROR_SYNTAX, "too much nesting in calculator function");
  644. while (1)
  645. {
  646. tok = pdf_lex(ctx, stream, buf);
  647. switch (tok)
  648. {
  649. case PDF_TOK_EOF:
  650. fz_throw(ctx, FZ_ERROR_SYNTAX, "truncated calculator function");
  651. case PDF_TOK_INT:
  652. resize_code(ctx, func, *codeptr);
  653. func->code[*codeptr].type = PS_INT;
  654. func->code[*codeptr].u.i = buf->i;
  655. ++*codeptr;
  656. break;
  657. case PDF_TOK_TRUE:
  658. resize_code(ctx, func, *codeptr);
  659. func->code[*codeptr].type = PS_BOOL;
  660. func->code[*codeptr].u.b = 1;
  661. ++*codeptr;
  662. break;
  663. case PDF_TOK_FALSE:
  664. resize_code(ctx, func, *codeptr);
  665. func->code[*codeptr].type = PS_BOOL;
  666. func->code[*codeptr].u.b = 0;
  667. ++*codeptr;
  668. break;
  669. case PDF_TOK_REAL:
  670. resize_code(ctx, func, *codeptr);
  671. func->code[*codeptr].type = PS_REAL;
  672. func->code[*codeptr].u.f = buf->f;
  673. ++*codeptr;
  674. break;
  675. case PDF_TOK_OPEN_BRACE:
  676. opptr = *codeptr;
  677. *codeptr += 4;
  678. resize_code(ctx, func, *codeptr);
  679. ifptr = *codeptr;
  680. parse_code(ctx, func, stream, codeptr, buf, depth + 1);
  681. tok = pdf_lex(ctx, stream, buf);
  682. if (tok == PDF_TOK_OPEN_BRACE)
  683. {
  684. elseptr = *codeptr;
  685. parse_code(ctx, func, stream, codeptr, buf, depth + 1);
  686. tok = pdf_lex(ctx, stream, buf);
  687. }
  688. else
  689. {
  690. elseptr = -1;
  691. }
  692. if (tok != PDF_TOK_KEYWORD)
  693. fz_throw(ctx, FZ_ERROR_SYNTAX, "missing keyword in 'if-else' context");
  694. if (!strcmp(buf->scratch, "if"))
  695. {
  696. if (elseptr >= 0)
  697. fz_throw(ctx, FZ_ERROR_SYNTAX, "too many branches for 'if'");
  698. func->code[opptr].type = PS_OPERATOR;
  699. func->code[opptr].u.op = PS_OP_IF;
  700. func->code[opptr+2].type = PS_BLOCK;
  701. func->code[opptr+2].u.block = ifptr;
  702. func->code[opptr+3].type = PS_BLOCK;
  703. func->code[opptr+3].u.block = *codeptr;
  704. }
  705. else if (!strcmp(buf->scratch, "ifelse"))
  706. {
  707. if (elseptr < 0)
  708. fz_throw(ctx, FZ_ERROR_SYNTAX, "not enough branches for 'ifelse'");
  709. func->code[opptr].type = PS_OPERATOR;
  710. func->code[opptr].u.op = PS_OP_IFELSE;
  711. func->code[opptr+1].type = PS_BLOCK;
  712. func->code[opptr+1].u.block = elseptr;
  713. func->code[opptr+2].type = PS_BLOCK;
  714. func->code[opptr+2].u.block = ifptr;
  715. func->code[opptr+3].type = PS_BLOCK;
  716. func->code[opptr+3].u.block = *codeptr;
  717. }
  718. else
  719. {
  720. fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown keyword in 'if-else' context: '%s'", buf->scratch);
  721. }
  722. break;
  723. case PDF_TOK_CLOSE_BRACE:
  724. resize_code(ctx, func, *codeptr);
  725. func->code[*codeptr].type = PS_OPERATOR;
  726. func->code[*codeptr].u.op = PS_OP_RETURN;
  727. ++*codeptr;
  728. return;
  729. case PDF_TOK_KEYWORD:
  730. cmp = -1;
  731. a = -1;
  732. b = nelem(ps_op_names);
  733. while (b - a > 1)
  734. {
  735. mid = (a + b) / 2;
  736. cmp = strcmp(buf->scratch, ps_op_names[mid]);
  737. if (cmp > 0)
  738. a = mid;
  739. else if (cmp < 0)
  740. b = mid;
  741. else
  742. a = b = mid;
  743. }
  744. if (cmp != 0)
  745. fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown operator: '%s'", buf->scratch);
  746. if (a == PS_OP_IFELSE)
  747. fz_throw(ctx, FZ_ERROR_SYNTAX, "illegally positioned ifelse operator in function");
  748. if (a == PS_OP_IF)
  749. fz_throw(ctx, FZ_ERROR_SYNTAX, "illegally positioned if operator in function");
  750. resize_code(ctx, func, *codeptr);
  751. func->code[*codeptr].type = PS_OPERATOR;
  752. func->code[*codeptr].u.op = a;
  753. ++*codeptr;
  754. break;
  755. default:
  756. fz_throw(ctx, FZ_ERROR_SYNTAX, "calculator function syntax error");
  757. }
  758. }
  759. }
  760. static void
  761. load_postscript_func(fz_context *ctx, pdf_function *func_, pdf_obj *dict)
  762. {
  763. pdf_function_p *func = (pdf_function_p *)func_;
  764. fz_stream *stream = NULL;
  765. int codeptr;
  766. pdf_lexbuf buf;
  767. pdf_token tok;
  768. pdf_lexbuf_init(ctx, &buf, PDF_LEXBUF_SMALL);
  769. fz_var(stream);
  770. fz_try(ctx)
  771. {
  772. stream = pdf_open_stream(ctx, dict);
  773. tok = pdf_lex(ctx, stream, &buf);
  774. if (tok != PDF_TOK_OPEN_BRACE)
  775. {
  776. fz_throw(ctx, FZ_ERROR_SYNTAX, "stream is not a calculator function");
  777. }
  778. func->code = NULL;
  779. func->cap = 0;
  780. codeptr = 0;
  781. parse_code(ctx, func, stream, &codeptr, &buf, 0);
  782. }
  783. fz_always(ctx)
  784. {
  785. fz_drop_stream(ctx, stream);
  786. pdf_lexbuf_fin(ctx, &buf);
  787. }
  788. fz_catch(ctx)
  789. {
  790. fz_rethrow(ctx);
  791. }
  792. func->super.super.size += func->cap * sizeof(psobj);
  793. }
  794. static void
  795. eval_postscript_func(fz_context *ctx, fz_function *func_, const float *in, float *out)
  796. {
  797. pdf_function_p *func = (pdf_function_p *)func_;
  798. ps_stack st;
  799. float x;
  800. int i;
  801. ps_init_stack(&st);
  802. for (i = 0; i < func->super.super.m; i++)
  803. {
  804. x = fz_clamp(in[i], func->super.domain[i][0], func->super.domain[i][1]);
  805. ps_push_real(&st, x);
  806. }
  807. ps_run(ctx, func->code, &st, 0);
  808. for (i = func->super.super.n - 1; i >= 0; i--)
  809. {
  810. x = ps_pop_real(&st);
  811. out[i] = fz_clamp(x, func->super.range[i][0], func->super.range[i][1]);
  812. }
  813. }
  814. /*
  815. * Sample function
  816. */
  817. #define MAX_SAMPLE_FUNCTION_SIZE (100 << 20)
  818. static void
  819. load_sample_func(fz_context *ctx, pdf_function *func_, pdf_obj *dict)
  820. {
  821. pdf_function_sa *func = (pdf_function_sa *)func_;
  822. fz_stream *stream;
  823. pdf_obj *obj;
  824. int samplecount;
  825. int bps;
  826. int i;
  827. func->samples = NULL;
  828. obj = pdf_dict_get(ctx, dict, PDF_NAME(Size));
  829. if (pdf_array_len(ctx, obj) < func->super.super.m)
  830. fz_throw(ctx, FZ_ERROR_SYNTAX, "too few sample function dimension sizes");
  831. if (pdf_array_len(ctx, obj) > func->super.super.m)
  832. fz_warn(ctx, "too many sample function dimension sizes");
  833. for (i = 0; i < func->super.super.m; i++)
  834. {
  835. func->size[i] = pdf_array_get_int(ctx, obj, i);
  836. if (func->size[i] <= 0)
  837. {
  838. fz_warn(ctx, "non-positive sample function dimension size");
  839. func->size[i] = 1;
  840. }
  841. }
  842. func->bps = bps = pdf_dict_get_int(ctx, dict, PDF_NAME(BitsPerSample));
  843. for (i = 0; i < func->super.super.m; i++)
  844. {
  845. func->encode[i][0] = 0;
  846. func->encode[i][1] = func->size[i] - 1;
  847. }
  848. obj = pdf_dict_get(ctx, dict, PDF_NAME(Encode));
  849. if (pdf_is_array(ctx, obj))
  850. {
  851. int ranges = fz_mini(func->super.super.m, pdf_array_len(ctx, obj) / 2);
  852. if (ranges != func->super.super.m)
  853. fz_warn(ctx, "wrong number of sample function input mappings");
  854. for (i = 0; i < ranges; i++)
  855. {
  856. func->encode[i][0] = pdf_array_get_real(ctx, obj, i * 2 + 0);
  857. func->encode[i][1] = pdf_array_get_real(ctx, obj, i * 2 + 1);
  858. }
  859. }
  860. for (i = 0; i < func->super.super.n; i++)
  861. {
  862. func->decode[i][0] = func->super.range[i][0];
  863. func->decode[i][1] = func->super.range[i][1];
  864. }
  865. obj = pdf_dict_get(ctx, dict, PDF_NAME(Decode));
  866. if (pdf_is_array(ctx, obj))
  867. {
  868. int ranges = fz_mini(func->super.super.n, pdf_array_len(ctx, obj) / 2);
  869. if (ranges != func->super.super.n)
  870. fz_warn(ctx, "wrong number of sample function output mappings");
  871. for (i = 0; i < ranges; i++)
  872. {
  873. func->decode[i][0] = pdf_array_get_real(ctx, obj, i * 2 + 0);
  874. func->decode[i][1] = pdf_array_get_real(ctx, obj, i * 2 + 1);
  875. }
  876. }
  877. for (i = 0, samplecount = func->super.super.n; i < func->super.super.m; i++)
  878. {
  879. if (samplecount > MAX_SAMPLE_FUNCTION_SIZE / func->size[i])
  880. fz_throw(ctx, FZ_ERROR_SYNTAX, "sample function too large");
  881. samplecount *= func->size[i];
  882. }
  883. if (samplecount > MAX_SAMPLE_FUNCTION_SIZE)
  884. fz_throw(ctx, FZ_ERROR_SYNTAX, "sample function too large");
  885. func->samples = Memento_label(fz_malloc_array(ctx, samplecount, float), "function_samples");
  886. func->super.super.size += samplecount * sizeof(float);
  887. stream = pdf_open_stream(ctx, dict);
  888. fz_try(ctx)
  889. {
  890. /* read samples */
  891. for (i = 0; i < samplecount; i++)
  892. {
  893. float s;
  894. if (fz_is_eof_bits(ctx, stream))
  895. fz_throw(ctx, FZ_ERROR_SYNTAX, "truncated sample function stream");
  896. switch (bps)
  897. {
  898. case 1: s = fz_read_bits(ctx, stream, 1); break;
  899. case 2: s = fz_read_bits(ctx, stream, 2) / 3.0f; break;
  900. case 4: s = fz_read_bits(ctx, stream, 4) / 15.0f; break;
  901. case 8: s = fz_read_byte(ctx, stream) / 255.0f; break;
  902. case 12: s = fz_read_bits(ctx, stream, 12) / 4095.0f; break;
  903. case 16: s = fz_read_uint16(ctx, stream) / 65535.0f; break;
  904. case 24: s = fz_read_uint24(ctx, stream) / 16777215.0f; break;
  905. case 32: s = fz_read_uint32(ctx, stream) / 4294967295.0f; break;
  906. default: fz_throw(ctx, FZ_ERROR_SYNTAX, "sample stream bit depth %d unsupported", bps);
  907. }
  908. func->samples[i] = s;
  909. }
  910. }
  911. fz_always(ctx)
  912. {
  913. fz_drop_stream(ctx, stream);
  914. }
  915. fz_catch(ctx)
  916. {
  917. fz_rethrow(ctx);
  918. }
  919. }
  920. static float
  921. interpolate_sample(pdf_function_sa *func, int *scale, int *e0, int *e1, float *efrac, int dim, int idx)
  922. {
  923. float a, b;
  924. int idx0, idx1;
  925. idx0 = e0[dim] * scale[dim] + idx;
  926. idx1 = e1[dim] * scale[dim] + idx;
  927. if (dim == 0)
  928. {
  929. a = func->samples[idx0];
  930. b = func->samples[idx1];
  931. }
  932. else
  933. {
  934. a = interpolate_sample(func, scale, e0, e1, efrac, dim - 1, idx0);
  935. b = interpolate_sample(func, scale, e0, e1, efrac, dim - 1, idx1);
  936. }
  937. return a + (b - a) * efrac[dim];
  938. }
  939. static void
  940. eval_sample_func(fz_context *ctx, fz_function *func_, const float *in, float *out)
  941. {
  942. pdf_function_sa *func = (pdf_function_sa *)func_;
  943. int e0[MAX_M], e1[MAX_M], scale[MAX_M];
  944. float efrac[MAX_M];
  945. float x;
  946. int i;
  947. /* encode input coordinates */
  948. for (i = 0; i < func->super.super.m; i++)
  949. {
  950. x = fz_clamp(in[i], func->super.domain[i][0], func->super.domain[i][1]);
  951. x = lerp(x, func->super.domain[i][0], func->super.domain[i][1],
  952. func->encode[i][0], func->encode[i][1]);
  953. x = fz_clamp(x, 0, func->size[i] - 1);
  954. e0[i] = floorf(x);
  955. e1[i] = ceilf(x);
  956. efrac[i] = x - e0[i];
  957. }
  958. scale[0] = func->super.super.n;
  959. for (i = 1; i < func->super.super.m; i++)
  960. scale[i] = scale[i - 1] * func->size[i-1];
  961. for (i = 0; i < func->super.super.n; i++)
  962. {
  963. if (func->super.super.m == 1)
  964. {
  965. float a = func->samples[e0[0] * func->super.super.n + i];
  966. float b = func->samples[e1[0] * func->super.super.n + i];
  967. float ab = a + (b - a) * efrac[0];
  968. out[i] = lerp(ab, 0, 1, func->decode[i][0], func->decode[i][1]);
  969. out[i] = fz_clamp(out[i], func->super.range[i][0], func->super.range[i][1]);
  970. }
  971. else if (func->super.super.m == 2)
  972. {
  973. int s0 = func->super.super.n;
  974. int s1 = s0 * func->size[0];
  975. float a = func->samples[e0[0] * s0 + e0[1] * s1 + i];
  976. float b = func->samples[e1[0] * s0 + e0[1] * s1 + i];
  977. float c = func->samples[e0[0] * s0 + e1[1] * s1 + i];
  978. float d = func->samples[e1[0] * s0 + e1[1] * s1 + i];
  979. float ab = a + (b - a) * efrac[0];
  980. float cd = c + (d - c) * efrac[0];
  981. float abcd = ab + (cd - ab) * efrac[1];
  982. out[i] = lerp(abcd, 0, 1, func->decode[i][0], func->decode[i][1]);
  983. out[i] = fz_clamp(out[i], func->super.range[i][0], func->super.range[i][1]);
  984. }
  985. else
  986. {
  987. x = interpolate_sample(func, scale, e0, e1, efrac, func->super.super.m - 1, i);
  988. out[i] = lerp(x, 0, 1, func->decode[i][0], func->decode[i][1]);
  989. out[i] = fz_clamp(out[i], func->super.range[i][0], func->super.range[i][1]);
  990. }
  991. }
  992. }
  993. /*
  994. * Exponential function
  995. */
  996. static void
  997. load_exponential_func(fz_context *ctx, pdf_function *func_, pdf_obj *dict)
  998. {
  999. pdf_function_e *func = (pdf_function_e *)func_;
  1000. pdf_obj *obj;
  1001. int i;
  1002. if (func->super.super.m > 1)
  1003. fz_warn(ctx, "exponential functions have at most one input");
  1004. func->super.super.m = 1;
  1005. func->n = pdf_dict_get_real(ctx, dict, PDF_NAME(N));
  1006. /* See exponential functions (PDF 1.7 section 3.9.2) */
  1007. if (func->n != (int) func->n)
  1008. {
  1009. /* If N is non-integer, input values may never be negative */
  1010. for (i = 0; i < func->super.super.m; i++)
  1011. if (func->super.domain[i][0] < 0 || func->super.domain[i][1] < 0)
  1012. fz_warn(ctx, "exponential function input domain includes illegal negative input values");
  1013. }
  1014. else if (func->n < 0)
  1015. {
  1016. /* if N is negative, input values may never be zero */
  1017. for (i = 0; i < func->super.super.m; i++)
  1018. if (func->super.domain[i][0] == 0 || func->super.domain[i][1] == 0 ||
  1019. (func->super.domain[i][0] < 0 && func->super.domain[i][1] > 0))
  1020. fz_warn(ctx, "exponential function input domain includes illegal input value zero");
  1021. }
  1022. for (i = 0; i < func->super.super.n; i++)
  1023. {
  1024. func->c0[i] = 0;
  1025. func->c1[i] = 1;
  1026. }
  1027. obj = pdf_dict_get(ctx, dict, PDF_NAME(C0));
  1028. if (pdf_is_array(ctx, obj))
  1029. {
  1030. int ranges = fz_mini(func->super.super.n, pdf_array_len(ctx, obj));
  1031. if (ranges != func->super.super.n)
  1032. fz_warn(ctx, "wrong number of C0 constants for exponential function");
  1033. for (i = 0; i < ranges; i++)
  1034. func->c0[i] = pdf_array_get_real(ctx, obj, i);
  1035. }
  1036. obj = pdf_dict_get(ctx, dict, PDF_NAME(C1));
  1037. if (pdf_is_array(ctx, obj))
  1038. {
  1039. int ranges = fz_mini(func->super.super.n, pdf_array_len(ctx, obj));
  1040. if (ranges != func->super.super.n)
  1041. fz_warn(ctx, "wrong number of C1 constants for exponential function");
  1042. for (i = 0; i < ranges; i++)
  1043. func->c1[i] = pdf_array_get_real(ctx, obj, i);
  1044. }
  1045. }
  1046. static void
  1047. eval_exponential_func(fz_context *ctx, fz_function *func_, const float *in, float *out)
  1048. {
  1049. pdf_function_e *func = (pdf_function_e *)func_;
  1050. float x = *in;
  1051. float tmp;
  1052. int i;
  1053. x = fz_clamp(x, func->super.domain[0][0], func->super.domain[0][1]);
  1054. /* Default output is zero, which is suitable for violated constraints */
  1055. if ((func->n != (int)func->n && x < 0) || (func->n < 0 && x == 0))
  1056. {
  1057. for (i = 0; i < func->super.super.n; i++)
  1058. out[i] = 0;
  1059. return;
  1060. }
  1061. tmp = powf(x, func->n);
  1062. for (i = 0; i < func->super.super.n; i++)
  1063. {
  1064. out[i] = func->c0[i] + tmp * (func->c1[i] - func->c0[i]);
  1065. if (func->super.has_range)
  1066. out[i] = fz_clamp(out[i], func->super.range[i][0], func->super.range[i][1]);
  1067. }
  1068. }
  1069. /*
  1070. * Stitching function
  1071. */
  1072. static void
  1073. load_stitching_func(fz_context *ctx, pdf_function *func_, pdf_obj *dict, pdf_cycle_list *cycle_up)
  1074. {
  1075. pdf_function_st *func = (pdf_function_st *)func_;
  1076. pdf_function **funcs;
  1077. pdf_obj *obj;
  1078. pdf_obj *sub;
  1079. int k;
  1080. int i;
  1081. func->k = 0;
  1082. if (func->super.super.m > 1)
  1083. fz_warn(ctx, "stitching functions have at most one input");
  1084. func->super.super.m = 1;
  1085. obj = pdf_dict_get(ctx, dict, PDF_NAME(Functions));
  1086. if (!pdf_is_array(ctx, obj))
  1087. fz_throw(ctx, FZ_ERROR_SYNTAX, "stitching function has no input functions");
  1088. k = pdf_array_len(ctx, obj);
  1089. func->funcs = Memento_label(fz_malloc_array(ctx, k, pdf_function*), "stitch_fns");
  1090. func->bounds = Memento_label(fz_malloc_array(ctx, k - 1, float), "stitch_bounds");
  1091. func->encode = Memento_label(fz_malloc_array(ctx, k * 2, float), "stitch_encode");
  1092. funcs = func->funcs;
  1093. for (i = 0; i < k; i++)
  1094. {
  1095. sub = pdf_array_get(ctx, obj, i);
  1096. funcs[i] = pdf_load_function_imp(ctx, sub, 1, func->super.super.n, cycle_up);
  1097. func->super.super.size += pdf_function_size(ctx, funcs[i]);
  1098. func->k ++;
  1099. if (funcs[i]->super.m != func->super.super.m)
  1100. fz_warn(ctx, "wrong number of inputs for sub function %d", i);
  1101. if (funcs[i]->super.n != func->super.super.n)
  1102. fz_warn(ctx, "wrong number of outputs for sub function %d", i);
  1103. }
  1104. obj = pdf_dict_get(ctx, dict, PDF_NAME(Bounds));
  1105. if (!pdf_is_array(ctx, obj))
  1106. fz_throw(ctx, FZ_ERROR_SYNTAX, "stitching function has no bounds");
  1107. {
  1108. if (pdf_array_len(ctx, obj) < k - 1)
  1109. fz_throw(ctx, FZ_ERROR_SYNTAX, "too few subfunction boundaries");
  1110. if (pdf_array_len(ctx, obj) > k)
  1111. fz_warn(ctx, "too many subfunction boundaries");
  1112. for (i = 0; i < k - 1; i++)
  1113. {
  1114. func->bounds[i] = pdf_array_get_real(ctx, obj, i);
  1115. if (i && func->bounds[i - 1] > func->bounds[i])
  1116. fz_throw(ctx, FZ_ERROR_SYNTAX, "subfunction %d boundary out of range", i);
  1117. }
  1118. if (k > 1 && (func->super.domain[0][0] > func->bounds[0] ||
  1119. func->super.domain[0][1] < func->bounds[k - 2]))
  1120. fz_warn(ctx, "subfunction boundaries outside of input mapping");
  1121. }
  1122. for (i = 0; i < k; i++)
  1123. {
  1124. func->encode[i * 2 + 0] = 0;
  1125. func->encode[i * 2 + 1] = 0;
  1126. }
  1127. obj = pdf_dict_get(ctx, dict, PDF_NAME(Encode));
  1128. if (pdf_is_array(ctx, obj))
  1129. {
  1130. int ranges = fz_mini(k, pdf_array_len(ctx, obj) / 2);
  1131. if (ranges != k)
  1132. fz_warn(ctx, "wrong number of stitching function input mappings");
  1133. for (i = 0; i < ranges; i++)
  1134. {
  1135. func->encode[i * 2 + 0] = pdf_array_get_real(ctx, obj, i * 2 + 0);
  1136. func->encode[i * 2 + 1] = pdf_array_get_real(ctx, obj, i * 2 + 1);
  1137. }
  1138. }
  1139. }
  1140. static void
  1141. eval_stitching_func(fz_context *ctx, fz_function *func_, const float *inp, float *out)
  1142. {
  1143. pdf_function_st *func = (pdf_function_st *)func_;
  1144. float low, high;
  1145. int k = func->k;
  1146. float *bounds = func->bounds;
  1147. int i;
  1148. float in = fz_clamp(*inp, func->super.domain[0][0], func->super.domain[0][1]);
  1149. for (i = 0; i < k - 1; i++)
  1150. {
  1151. if (in < bounds[i])
  1152. break;
  1153. }
  1154. if (i == 0 && k == 1)
  1155. {
  1156. low = func->super.domain[0][0];
  1157. high = func->super.domain[0][1];
  1158. }
  1159. else if (i == 0)
  1160. {
  1161. low = func->super.domain[0][0];
  1162. high = bounds[0];
  1163. }
  1164. else if (i == k - 1)
  1165. {
  1166. low = bounds[k - 2];
  1167. high = func->super.domain[0][1];
  1168. }
  1169. else
  1170. {
  1171. low = bounds[i - 1];
  1172. high = bounds[i];
  1173. }
  1174. in = lerp(in, low, high, func->encode[i * 2 + 0], func->encode[i * 2 + 1]);
  1175. pdf_eval_function(ctx, func->funcs[i], &in, 1, out, func->super.super.n);
  1176. }
  1177. /*
  1178. * Common
  1179. */
  1180. static void
  1181. pdf_drop_function_sa(fz_context *ctx, fz_storable *func_)
  1182. {
  1183. pdf_function_sa *func = (pdf_function_sa *)func_;
  1184. fz_free(ctx, func->samples);
  1185. fz_free(ctx, func);
  1186. }
  1187. static void
  1188. pdf_drop_function_e(fz_context *ctx, fz_storable *func)
  1189. {
  1190. fz_free(ctx, func);
  1191. }
  1192. static void
  1193. pdf_drop_function_st(fz_context *ctx, fz_storable *func_)
  1194. {
  1195. pdf_function_st *func = (pdf_function_st *)func_;
  1196. int i;
  1197. for (i = 0; i < func->k; i++)
  1198. pdf_drop_function(ctx, func->funcs[i]);
  1199. fz_free(ctx, func->funcs);
  1200. fz_free(ctx, func->bounds);
  1201. fz_free(ctx, func->encode);
  1202. fz_free(ctx, func);
  1203. }
  1204. static void
  1205. pdf_drop_function_p(fz_context *ctx, fz_storable *func_)
  1206. {
  1207. pdf_function_p *func = (pdf_function_p *)func_;
  1208. fz_free(ctx, func->code);
  1209. fz_free(ctx, func);
  1210. }
  1211. void
  1212. pdf_eval_function(fz_context *ctx, pdf_function *func, const float *in, int inlen, float *out, int outlen)
  1213. {
  1214. fz_eval_function(ctx, &func->super, in, inlen, out, outlen);
  1215. }
  1216. static pdf_function *
  1217. pdf_load_function_imp(fz_context *ctx, pdf_obj *dict, int in, int out, pdf_cycle_list *cycle_up)
  1218. {
  1219. pdf_cycle_list cycle;
  1220. pdf_function *func;
  1221. pdf_obj *obj;
  1222. int i;
  1223. int type;
  1224. fz_store_drop_fn *drop;
  1225. if (pdf_cycle(ctx, &cycle, cycle_up, dict))
  1226. fz_throw(ctx, FZ_ERROR_SYNTAX, "recursive function");
  1227. type = pdf_dict_get_int(ctx, dict, PDF_NAME(FunctionType));
  1228. switch (type)
  1229. {
  1230. case SAMPLE:
  1231. drop = pdf_drop_function_sa;
  1232. break;
  1233. case EXPONENTIAL:
  1234. drop = pdf_drop_function_e;
  1235. break;
  1236. case STITCHING:
  1237. drop = pdf_drop_function_st;
  1238. break;
  1239. case POSTSCRIPT:
  1240. drop = pdf_drop_function_p;
  1241. break;
  1242. default:
  1243. fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown function type (%d 0 R)", pdf_to_num(ctx, dict));
  1244. }
  1245. if ((func = pdf_find_item(ctx, drop, dict)) != NULL)
  1246. return func;
  1247. switch (type)
  1248. {
  1249. case SAMPLE:
  1250. func = &fz_new_derived_function(ctx, pdf_function_sa, sizeof(pdf_function_sa), 1, 1, eval_sample_func, pdf_drop_function_sa)->super;
  1251. break;
  1252. case EXPONENTIAL:
  1253. func = &fz_new_derived_function(ctx, pdf_function_e, sizeof(pdf_function_e), 1, 1, eval_exponential_func, pdf_drop_function_e)->super;
  1254. break;
  1255. case STITCHING:
  1256. func = &fz_new_derived_function(ctx, pdf_function_st, sizeof(pdf_function_st), 1, 1, eval_stitching_func, pdf_drop_function_st)->super;
  1257. break;
  1258. case POSTSCRIPT:
  1259. func = &fz_new_derived_function(ctx, pdf_function_p, sizeof(pdf_function_p), 1, 1, eval_postscript_func, pdf_drop_function_p)->super;
  1260. break;
  1261. }
  1262. /* required for all */
  1263. obj = pdf_dict_get(ctx, dict, PDF_NAME(Domain));
  1264. func->super.m = fz_clampi(pdf_array_len(ctx, obj) / 2, 1, MAX_M);
  1265. for (i = 0; i < func->super.m; i++)
  1266. {
  1267. func->domain[i][0] = pdf_array_get_real(ctx, obj, i * 2 + 0);
  1268. func->domain[i][1] = pdf_array_get_real(ctx, obj, i * 2 + 1);
  1269. }
  1270. /* required for type0 and type4, optional otherwise */
  1271. obj = pdf_dict_get(ctx, dict, PDF_NAME(Range));
  1272. if (pdf_is_array(ctx, obj))
  1273. {
  1274. func->has_range = 1;
  1275. func->super.n = fz_clampi(pdf_array_len(ctx, obj) / 2, 1, MAX_N);
  1276. for (i = 0; i < func->super.n; i++)
  1277. {
  1278. func->range[i][0] = pdf_array_get_real(ctx, obj, i * 2 + 0);
  1279. func->range[i][1] = pdf_array_get_real(ctx, obj, i * 2 + 1);
  1280. }
  1281. }
  1282. else
  1283. {
  1284. func->has_range = 0;
  1285. func->super.n = out;
  1286. }
  1287. if (func->super.m != in)
  1288. fz_warn(ctx, "wrong number of function inputs");
  1289. if (func->super.n != out)
  1290. fz_warn(ctx, "wrong number of function outputs");
  1291. fz_try(ctx)
  1292. {
  1293. switch (type)
  1294. {
  1295. case SAMPLE:
  1296. load_sample_func(ctx, func, dict);
  1297. break;
  1298. case EXPONENTIAL:
  1299. load_exponential_func(ctx, func, dict);
  1300. break;
  1301. case STITCHING:
  1302. load_stitching_func(ctx, func, dict, &cycle);
  1303. break;
  1304. case POSTSCRIPT:
  1305. load_postscript_func(ctx, func, dict);
  1306. break;
  1307. default:
  1308. fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown function type (%d 0 R)", pdf_to_num(ctx, dict));
  1309. }
  1310. pdf_store_item(ctx, dict, func, func->super.size);
  1311. }
  1312. fz_catch(ctx)
  1313. {
  1314. pdf_drop_function(ctx, func);
  1315. fz_rethrow(ctx);
  1316. }
  1317. return func;
  1318. }
  1319. pdf_function *
  1320. pdf_load_function(fz_context *ctx, pdf_obj *dict, int in, int out)
  1321. {
  1322. return pdf_load_function_imp(ctx, dict, in, out, NULL);
  1323. }