pdf_function.c 37 KB

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