mudraw.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799
  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. /*
  23. * mudraw -- command line tool for drawing and converting documents
  24. */
  25. #include "mupdf/fitz.h"
  26. #if FZ_ENABLE_PDF
  27. #include "mupdf/pdf.h" /* for pdf output */
  28. #endif
  29. #ifndef DISABLE_MUTHREADS
  30. #include "mupdf/helpers/mu-threads.h"
  31. #endif
  32. #ifdef HAVE_SMARTOFFICE
  33. #include "sodochandler.h"
  34. #endif
  35. #include <string.h>
  36. #include <limits.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #ifdef _WIN32
  40. struct timeval;
  41. struct timezone;
  42. int gettimeofday(struct timeval *tv, struct timezone *tz);
  43. #else
  44. #include <sys/time.h>
  45. #endif
  46. #ifdef _WIN32
  47. #include <windows.h>
  48. #include <direct.h> /* for getcwd */
  49. #else
  50. #include <sys/stat.h> /* for mkdir */
  51. #include <unistd.h> /* for getcwd */
  52. #endif
  53. /* Allow for windows stdout being made binary */
  54. #ifdef _WIN32
  55. #include <io.h>
  56. #include <fcntl.h>
  57. #endif
  58. /* Enable for helpful threading debug */
  59. /* #define DEBUG_THREADS(A) do { printf A; fflush(stdout); } while (0) */
  60. #define DEBUG_THREADS(A) do { } while (0)
  61. enum {
  62. OUT_BBOX,
  63. OUT_HTML,
  64. OUT_NONE,
  65. OUT_OCR_HTML,
  66. OUT_OCR_PDF,
  67. OUT_OCR_STEXT_JSON,
  68. OUT_OCR_STEXT_XML,
  69. OUT_OCR_TEXT,
  70. OUT_OCR_TRACE,
  71. OUT_OCR_XHTML,
  72. OUT_PAM,
  73. OUT_PBM,
  74. OUT_PCL,
  75. OUT_PCLM,
  76. OUT_PGM,
  77. OUT_PKM,
  78. OUT_PNG,
  79. OUT_J2K,
  80. OUT_PNM,
  81. OUT_PPM,
  82. OUT_PS,
  83. OUT_PSD,
  84. OUT_PWG,
  85. OUT_STEXT_JSON,
  86. OUT_STEXT_XML,
  87. OUT_SVG,
  88. OUT_TEXT,
  89. OUT_TRACE,
  90. OUT_XHTML,
  91. OUT_XMLTEXT,
  92. #if FZ_ENABLE_PDF
  93. OUT_PDF,
  94. #endif
  95. };
  96. enum { CS_INVALID, CS_UNSET, CS_MONO, CS_GRAY, CS_GRAY_ALPHA, CS_RGB, CS_RGB_ALPHA, CS_CMYK, CS_CMYK_ALPHA, CS_ICC };
  97. enum { SPOTS_NONE, SPOTS_OVERPRINT_SIM, SPOTS_FULL };
  98. typedef struct
  99. {
  100. char *suffix;
  101. int format;
  102. int spots;
  103. } suffix_t;
  104. static const suffix_t suffix_table[] =
  105. {
  106. /* All the 'triple extension' ones must go first. */
  107. { ".ocr.stext.json", OUT_OCR_STEXT_JSON, 0 },
  108. /* All the 'double extension' ones must go next. */
  109. { ".ocr.txt", OUT_OCR_TEXT, 0 },
  110. { ".ocr.text", OUT_OCR_TEXT, 0 },
  111. { ".ocr.html", OUT_OCR_HTML, 0 },
  112. { ".ocr.xhtml", OUT_OCR_XHTML, 0 },
  113. { ".ocr.stext", OUT_OCR_STEXT_XML, 0 },
  114. { ".ocr.pdf", OUT_OCR_PDF, 0 },
  115. { ".ocr.trace", OUT_OCR_TRACE, 0 },
  116. { ".stext.json", OUT_STEXT_JSON, 0 },
  117. /* And the 'single extension' ones go last. */
  118. #if FZ_ENABLE_JPX
  119. { ".j2k", OUT_J2K, 0 },
  120. #endif
  121. { ".png", OUT_PNG, 0 },
  122. { ".pgm", OUT_PGM, 0 },
  123. { ".ppm", OUT_PPM, 0 },
  124. { ".pnm", OUT_PNM, 0 },
  125. { ".pam", OUT_PAM, 0 },
  126. { ".pbm", OUT_PBM, 0 },
  127. { ".pkm", OUT_PKM, 0 },
  128. { ".svg", OUT_SVG, 0 },
  129. { ".pwg", OUT_PWG, 0 },
  130. { ".pclm", OUT_PCLM, 0 },
  131. { ".pcl", OUT_PCL, 0 },
  132. #if FZ_ENABLE_PDF
  133. { ".pdf", OUT_PDF, 0 },
  134. #endif
  135. { ".psd", OUT_PSD, 1 },
  136. { ".ps", OUT_PS, 0 },
  137. { ".txt", OUT_TEXT, 0 },
  138. { ".text", OUT_TEXT, 0 },
  139. { ".html", OUT_HTML, 0 },
  140. { ".xhtml", OUT_XHTML, 0 },
  141. { ".stext", OUT_STEXT_XML, 0 },
  142. { ".trace", OUT_TRACE, 0 },
  143. { ".xmltext", OUT_XMLTEXT, 0 },
  144. { ".bbox", OUT_BBOX, 0 },
  145. };
  146. typedef struct
  147. {
  148. char *name;
  149. int colorspace;
  150. } cs_name_t;
  151. static const cs_name_t cs_name_table[] =
  152. {
  153. { "m", CS_MONO },
  154. { "mono", CS_MONO },
  155. { "g", CS_GRAY },
  156. { "gray", CS_GRAY },
  157. { "grey", CS_GRAY },
  158. { "ga", CS_GRAY_ALPHA },
  159. { "grayalpha", CS_GRAY_ALPHA },
  160. { "greyalpha", CS_GRAY_ALPHA },
  161. { "rgb", CS_RGB },
  162. { "rgba", CS_RGB_ALPHA },
  163. { "rgbalpha", CS_RGB_ALPHA },
  164. { "cmyk", CS_CMYK },
  165. { "cmyka", CS_CMYK_ALPHA },
  166. { "cmykalpha", CS_CMYK_ALPHA },
  167. };
  168. typedef struct
  169. {
  170. int format;
  171. int default_cs;
  172. int permitted_cs[7];
  173. } format_cs_table_t;
  174. static const format_cs_table_t format_cs_table[] =
  175. {
  176. { OUT_PNG, CS_RGB, { CS_GRAY, CS_GRAY_ALPHA, CS_RGB, CS_RGB_ALPHA, CS_ICC } },
  177. { OUT_J2K, CS_RGB, { CS_GRAY, CS_RGB } },
  178. { OUT_PPM, CS_RGB, { CS_GRAY, CS_RGB } },
  179. { OUT_PNM, CS_GRAY, { CS_GRAY, CS_RGB } },
  180. { OUT_PAM, CS_RGB_ALPHA, { CS_GRAY, CS_GRAY_ALPHA, CS_RGB, CS_RGB_ALPHA, CS_CMYK, CS_CMYK_ALPHA } },
  181. { OUT_PGM, CS_GRAY, { CS_GRAY, CS_RGB } },
  182. { OUT_PBM, CS_MONO, { CS_MONO } },
  183. { OUT_PKM, CS_CMYK, { CS_CMYK } },
  184. { OUT_PWG, CS_RGB, { CS_MONO, CS_GRAY, CS_RGB, CS_CMYK } },
  185. { OUT_PCL, CS_MONO, { CS_MONO, CS_RGB } },
  186. { OUT_PCLM, CS_RGB, { CS_RGB, CS_GRAY } },
  187. { OUT_PS, CS_RGB, { CS_GRAY, CS_RGB, CS_CMYK } },
  188. { OUT_PSD, CS_CMYK, { CS_GRAY, CS_GRAY_ALPHA, CS_RGB, CS_RGB_ALPHA, CS_CMYK, CS_CMYK_ALPHA, CS_ICC } },
  189. { OUT_TRACE, CS_RGB, { CS_RGB } },
  190. { OUT_XMLTEXT, CS_RGB, { CS_RGB } },
  191. { OUT_BBOX, CS_RGB, { CS_RGB } },
  192. { OUT_SVG, CS_RGB, { CS_RGB } },
  193. { OUT_OCR_PDF, CS_RGB, { CS_RGB, CS_GRAY } },
  194. #if FZ_ENABLE_PDF
  195. { OUT_PDF, CS_RGB, { CS_RGB } },
  196. #endif
  197. { OUT_TEXT, CS_RGB, { CS_RGB } },
  198. { OUT_HTML, CS_RGB, { CS_RGB } },
  199. { OUT_XHTML, CS_RGB, { CS_RGB } },
  200. { OUT_STEXT_XML, CS_RGB, { CS_RGB } },
  201. { OUT_STEXT_JSON, CS_RGB, { CS_RGB } },
  202. { OUT_OCR_TEXT, CS_GRAY, { CS_GRAY } },
  203. { OUT_OCR_HTML, CS_GRAY, { CS_GRAY } },
  204. { OUT_OCR_XHTML, CS_GRAY, { CS_GRAY } },
  205. { OUT_OCR_STEXT_XML, CS_GRAY, { CS_GRAY } },
  206. { OUT_OCR_STEXT_JSON, CS_GRAY, { CS_GRAY } },
  207. { OUT_OCR_TRACE, CS_GRAY, { CS_GRAY } },
  208. };
  209. /*
  210. In the presence of pthreads or Windows threads, we can offer
  211. a multi-threaded option. In the absence, of such, we degrade
  212. nicely.
  213. */
  214. #ifndef DISABLE_MUTHREADS
  215. static mu_mutex mutexes[FZ_LOCK_MAX];
  216. static void mudraw_lock(void *user, int lock)
  217. {
  218. mu_lock_mutex(&mutexes[lock]);
  219. }
  220. static void mudraw_unlock(void *user, int lock)
  221. {
  222. mu_unlock_mutex(&mutexes[lock]);
  223. }
  224. static fz_locks_context mudraw_locks =
  225. {
  226. NULL, mudraw_lock, mudraw_unlock
  227. };
  228. static void fin_mudraw_locks(void)
  229. {
  230. int i;
  231. for (i = 0; i < FZ_LOCK_MAX; i++)
  232. mu_destroy_mutex(&mutexes[i]);
  233. }
  234. static fz_locks_context *init_mudraw_locks(void)
  235. {
  236. int i;
  237. int failed = 0;
  238. for (i = 0; i < FZ_LOCK_MAX; i++)
  239. failed |= mu_create_mutex(&mutexes[i]);
  240. if (failed)
  241. {
  242. fin_mudraw_locks();
  243. return NULL;
  244. }
  245. return &mudraw_locks;
  246. }
  247. #endif
  248. typedef struct worker_t {
  249. fz_context *ctx;
  250. int num;
  251. int band; /* -1 to shutdown, or band to render */
  252. int error;
  253. int running; /* set to 1 by main thread when it thinks the worker is running, 0 when it thinks it is not running */
  254. fz_display_list *list;
  255. fz_matrix ctm;
  256. fz_rect tbounds;
  257. fz_pixmap *pix;
  258. fz_bitmap *bit;
  259. fz_cookie cookie;
  260. #ifndef DISABLE_MUTHREADS
  261. mu_semaphore start;
  262. mu_semaphore stop;
  263. mu_thread thread;
  264. #endif
  265. } worker_t;
  266. static char *output = NULL;
  267. static fz_output *out = NULL;
  268. static int output_pagenum = 0;
  269. static int output_file_per_page = 0;
  270. static char *format = NULL;
  271. static int output_format = OUT_NONE;
  272. static float rotation = 0;
  273. static float resolution = 72;
  274. static int res_specified = 0;
  275. static int width = 0;
  276. static int height = 0;
  277. static int fit = 0;
  278. static int page_box = FZ_CROP_BOX;
  279. static float layout_w = FZ_DEFAULT_LAYOUT_W;
  280. static float layout_h = FZ_DEFAULT_LAYOUT_H;
  281. static float layout_em = FZ_DEFAULT_LAYOUT_EM;
  282. static char *layout_css = NULL;
  283. static int layout_use_doc_css = 1;
  284. static float min_line_width = 0.0f;
  285. static int showfeatures = 0;
  286. static int showtime = 0;
  287. static int showmemory = 0;
  288. static int showmd5 = 0;
  289. #if FZ_ENABLE_PDF
  290. static pdf_document *pdfout = NULL;
  291. #endif
  292. static int no_icc = 0;
  293. static int ignore_errors = 0;
  294. static int uselist = 1;
  295. static int alphabits_text = 8;
  296. static int alphabits_graphics = 8;
  297. static int out_cs = CS_UNSET;
  298. static const char *proof_filename = NULL;
  299. fz_colorspace *proof_cs = NULL;
  300. static const char *icc_filename = NULL;
  301. static float gamma_value = 1;
  302. static int invert = 0;
  303. static int s_kill = 0; /* Using `kill` causes problems on Android. */
  304. static int band_height = 0;
  305. static int lowmemory = 0;
  306. static int quiet = 0;
  307. static int errored = 0;
  308. static fz_colorspace *colorspace = NULL;
  309. static fz_colorspace *oi = NULL;
  310. #if FZ_ENABLE_SPOT_RENDERING
  311. static int spots = SPOTS_OVERPRINT_SIM;
  312. #else
  313. static int spots = SPOTS_NONE;
  314. #endif
  315. static int alpha;
  316. static int useaccel = 1;
  317. static char *filename;
  318. static int files = 0;
  319. static int num_workers = 0;
  320. static worker_t *workers;
  321. static fz_band_writer *bander = NULL;
  322. static const char *layer_config = NULL;
  323. static int layer_list = 0;
  324. static int layer_on[1000];
  325. static int layer_off[1000];
  326. static int layer_on_len;
  327. static int layer_off_len;
  328. static int skew_correct;
  329. static float skew_angle;
  330. static int skew_border;
  331. static const char ocr_language_default[] = "eng";
  332. static const char *ocr_language = ocr_language_default;
  333. static const char *ocr_datadir = NULL;
  334. static struct {
  335. int active;
  336. int started;
  337. fz_context *ctx;
  338. #ifndef DISABLE_MUTHREADS
  339. mu_thread thread;
  340. mu_semaphore start;
  341. mu_semaphore stop;
  342. #endif
  343. int pagenum;
  344. int error;
  345. char *filename;
  346. fz_display_list *list;
  347. fz_page *page;
  348. int interptime;
  349. fz_separations *seps;
  350. } bgprint;
  351. static struct {
  352. int count, total;
  353. int min, max;
  354. int mininterp, maxinterp;
  355. int minpage, maxpage;
  356. char *minfilename;
  357. char *maxfilename;
  358. int layout;
  359. int minlayout, maxlayout;
  360. char *minlayoutfilename;
  361. char *maxlayoutfilename;
  362. } timing;
  363. static int usage(void)
  364. {
  365. fprintf(stderr,
  366. "usage: mudraw [options] file [pages]\n"
  367. "\t-p -\tpassword\n"
  368. "\n"
  369. "\t-o -\toutput file name (%%d for page number)\n"
  370. "\t-F -\toutput format (default inferred from output file name)\n"
  371. "\t\traster: png, pnm, pam, pbm, pkm, pwg, pcl, ps, pdf, j2k\n"
  372. "\t\tvector: svg, pdf, trace, ocr.trace\n"
  373. "\t\ttext: txt, html, xhtml, stext, stext.json\n"
  374. #ifndef OCR_DISABLED
  375. "\t\tocr'd text: ocr.txt, ocr.html, ocr.xhtml, ocr.stext, ocr.stext.json\n"
  376. #else
  377. "\t\tocr'd text: ocr.txt, ocr.html, ocr.xhtml, ocr.stext, ocr.stext.json (disabled)\n"
  378. #endif
  379. "\t\tbitmap-wrapped-as-pdf: pclm, ocr.pdf\n"
  380. "\n"
  381. "\t-q\tbe quiet (don't print progress messages)\n"
  382. "\t-s -\tshow extra information:\n"
  383. "\t\tm - show memory use\n"
  384. "\t\tt - show timings\n"
  385. "\t\tf - show page features\n"
  386. "\t\t5 - show md5 checksum of rendered image\n"
  387. "\n"
  388. "\t-R -\trotate clockwise (default: 0 degrees)\n"
  389. "\t-r -\tresolution in dpi (default: 72)\n"
  390. "\t-w -\twidth (in pixels) (maximum width if -r is specified)\n"
  391. "\t-h -\theight (in pixels) (maximum height if -r is specified)\n"
  392. "\t-f\tfit width and/or height exactly; ignore original aspect ratio\n"
  393. "\t-b -\tuse named page box (MediaBox, CropBox, BleedBox, TrimBox, or ArtBox)\n"
  394. "\t-B -\tmaximum band_height (pXm, pcl, pclm, ocr.pdf, ps, psd and png output only)\n"
  395. #ifndef DISABLE_MUTHREADS
  396. "\t-T -\tnumber of threads to use for rendering (banded mode only)\n"
  397. #else
  398. "\t-T -\tnumber of threads to use for rendering (disabled in this non-threading build)\n"
  399. #endif
  400. "\n"
  401. "\t-W -\tpage width for EPUB layout\n"
  402. "\t-H -\tpage height for EPUB layout\n"
  403. "\t-S -\tfont size for EPUB layout\n"
  404. "\t-U -\tfile name of user stylesheet for EPUB layout\n"
  405. "\t-X\tdisable document styles for EPUB layout\n"
  406. "\t-a\tdisable usage of accelerator file\n"
  407. "\n"
  408. "\t-c -\tcolorspace (mono, gray, grayalpha, rgb, rgba, cmyk, cmykalpha, filename of ICC profile)\n"
  409. "\t-e -\tproof icc profile (filename of ICC profile)\n"
  410. "\t-G -\tapply gamma correction\n"
  411. "\t-I\tinvert colors\n"
  412. "\n"
  413. "\t-A -\tnumber of bits of antialiasing (0 to 8)\n"
  414. "\t-A -/-\tnumber of bits of antialiasing (0 to 8) (graphics, text)\n"
  415. "\t-l -\tminimum stroked line width (in pixels)\n"
  416. "\t-K\tdo not draw text\n"
  417. "\t-KK\tonly draw text\n"
  418. "\t-D\tdisable use of display list\n"
  419. "\t-i\tignore errors\n"
  420. "\t-m -\tlimit memory usage in bytes\n"
  421. "\t-L\tlow memory mode (avoid caching, clear objects after each page)\n"
  422. #ifndef DISABLE_MUTHREADS
  423. "\t-P\tparallel interpretation/rendering\n"
  424. #else
  425. "\t-P\tparallel interpretation/rendering (disabled in this non-threading build)\n"
  426. #endif
  427. "\t-N\tdisable ICC workflow (\"N\"o color management)\n"
  428. "\t-O -\tControl spot/overprint rendering\n"
  429. #if FZ_ENABLE_SPOT_RENDERING
  430. "\t\t 0 = No spot rendering\n"
  431. "\t\t 1 = Overprint simulation (default)\n"
  432. "\t\t 2 = Full spot rendering\n"
  433. #else
  434. "\t\t 0 = No spot rendering (default)\n"
  435. "\t\t 1 = Overprint simulation (Disabled in this build)\n"
  436. "\t\t 2 = Full spot rendering (Disabled in this build)\n"
  437. #endif
  438. #ifndef OCR_DISABLED
  439. "\t-t -\tSpecify language/script for OCR (default: eng)\n"
  440. "\t-d -\tSpecify path for OCR files (default: rely on TESSDATA_PREFIX environment variable)\n"
  441. "\t-k -{,-}\tSkew correction options. auto or angle {0=increase size, 1=maintain size, 2=decrease size}\n"
  442. #else
  443. "\t-t -\tSpecify language/script for OCR (default: eng) (disabled)\n"
  444. "\t-d -\tSpecify path for OCR files (default: rely on TESSDATA_PREFIX environment variable) (disabled)\n"
  445. "\t-k -{,-}\tSkew correction options. auto or angle {0=increase size, 1=maintain size, 2=decrease size} (disabled)\n"
  446. #endif
  447. "\n"
  448. "\t-y l\tList the layer configs to stderr\n"
  449. "\t-y -\tSelect layer config (by number)\n"
  450. "\t-y -{,-}*\tSelect layer config (by number), and toggle the listed entries\n"
  451. "\n"
  452. "\t-Y\tList individual layers to stderr\n"
  453. "\t-z -\tHide individual layer\n"
  454. "\t-Z -\tShow individual layer\n"
  455. "\n"
  456. "\tpages\tcomma separated list of page numbers and ranges\n"
  457. );
  458. return 1;
  459. }
  460. static int gettime(void)
  461. {
  462. static struct timeval first;
  463. static int once = 1;
  464. struct timeval now;
  465. if (once)
  466. {
  467. gettimeofday(&first, NULL);
  468. once = 0;
  469. }
  470. gettimeofday(&now, NULL);
  471. return (now.tv_sec - first.tv_sec) * 1000 + (now.tv_usec - first.tv_usec) / 1000;
  472. }
  473. static int has_percent_d(char *s)
  474. {
  475. /* find '%[0-9]*d' */
  476. while (*s)
  477. {
  478. if (*s++ == '%')
  479. {
  480. while (*s >= '0' && *s <= '9')
  481. ++s;
  482. if (*s == 'd')
  483. return 1;
  484. }
  485. }
  486. return 0;
  487. }
  488. /* Output file level (as opposed to page level) headers */
  489. static void
  490. file_level_headers(fz_context *ctx)
  491. {
  492. if (output_format == OUT_STEXT_XML || output_format == OUT_TRACE || output_format == OUT_BBOX || output_format == OUT_OCR_STEXT_XML || output_format == OUT_XMLTEXT)
  493. fz_write_printf(ctx, out, "<?xml version=\"1.0\"?>\n");
  494. if (output_format == OUT_HTML || output_format == OUT_OCR_HTML)
  495. fz_print_stext_header_as_html(ctx, out);
  496. if (output_format == OUT_XHTML || output_format == OUT_OCR_XHTML)
  497. fz_print_stext_header_as_xhtml(ctx, out);
  498. if (output_format == OUT_STEXT_XML || output_format == OUT_TRACE || output_format == OUT_BBOX || output_format == OUT_OCR_STEXT_XML)
  499. fz_write_printf(ctx, out, "<document name=\"%s\">\n", filename);
  500. if (output_format == OUT_STEXT_JSON || output_format == OUT_OCR_STEXT_JSON)
  501. fz_write_printf(ctx, out, "{%q:%q,%q:[", "file", filename, "pages");
  502. if (output_format == OUT_PS)
  503. fz_write_ps_file_header(ctx, out);
  504. if (output_format == OUT_PWG)
  505. fz_write_pwg_file_header(ctx, out);
  506. if (output_format == OUT_PCLM)
  507. {
  508. fz_pclm_options opts = { 0 };
  509. fz_parse_pclm_options(ctx, &opts, "compression=flate");
  510. bander = fz_new_pclm_band_writer(ctx, out, &opts);
  511. }
  512. if (output_format == OUT_OCR_PDF)
  513. {
  514. char options[300];
  515. fz_pdfocr_options opts = { 0 };
  516. fz_snprintf(options, sizeof(options), "compression=flate,ocr-language=%s", ocr_language);
  517. if (ocr_datadir)
  518. {
  519. fz_strlcat(options, ",ocr-datadir=", sizeof (options));
  520. fz_strlcat(options, ocr_datadir, sizeof (options));
  521. }
  522. fz_parse_pdfocr_options(ctx, &opts, options);
  523. opts.skew_correct = skew_correct;
  524. opts.skew_border = skew_border;
  525. opts.skew_angle = skew_angle;
  526. bander = fz_new_pdfocr_band_writer(ctx, out, &opts);
  527. }
  528. }
  529. static void
  530. file_level_trailers(fz_context *ctx)
  531. {
  532. if (output_format == OUT_STEXT_XML || output_format == OUT_TRACE || output_format == OUT_OCR_TRACE || output_format == OUT_BBOX || output_format == OUT_OCR_STEXT_XML)
  533. fz_write_printf(ctx, out, "</document>\n");
  534. if (output_format == OUT_STEXT_JSON || output_format == OUT_OCR_STEXT_JSON)
  535. fz_write_printf(ctx, out, "]}");
  536. if (output_format == OUT_HTML || output_format == OUT_OCR_HTML)
  537. fz_print_stext_trailer_as_html(ctx, out);
  538. if (output_format == OUT_XHTML || output_format == OUT_OCR_HTML)
  539. fz_print_stext_trailer_as_xhtml(ctx, out);
  540. if (output_format == OUT_PS)
  541. fz_write_ps_file_trailer(ctx, out, output_pagenum);
  542. if (output_format == OUT_PCLM || output_format == OUT_OCR_PDF)
  543. {
  544. fz_close_band_writer(ctx, bander);
  545. fz_drop_band_writer(ctx, bander);
  546. bander = NULL;
  547. }
  548. }
  549. static void apply_kill_switch(fz_device *dev)
  550. {
  551. if (s_kill == 1)
  552. {
  553. /* kill all non-clipping text operators */
  554. dev->fill_text = NULL;
  555. dev->stroke_text = NULL;
  556. dev->ignore_text = NULL;
  557. }
  558. else if (s_kill == 2)
  559. {
  560. /* kill all non-clipping path, image, and shading operators */
  561. dev->fill_path = NULL;
  562. dev->stroke_path = NULL;
  563. dev->fill_shade = NULL;
  564. dev->fill_image = NULL;
  565. dev->fill_image_mask = NULL;
  566. }
  567. }
  568. static void drawband(fz_context *ctx, fz_page *page, fz_display_list *list, fz_matrix ctm, fz_rect tbounds, fz_cookie *cookie, int band_start, fz_pixmap *pix, fz_bitmap **bit)
  569. {
  570. fz_device *dev = NULL;
  571. fz_var(dev);
  572. *bit = NULL;
  573. fz_try(ctx)
  574. {
  575. if (pix->alpha)
  576. fz_clear_pixmap(ctx, pix);
  577. else
  578. fz_clear_pixmap_with_value(ctx, pix, 255);
  579. dev = fz_new_draw_device_with_proof(ctx, fz_identity, pix, proof_cs);
  580. apply_kill_switch(dev);
  581. if (lowmemory)
  582. fz_enable_device_hints(ctx, dev, FZ_NO_CACHE);
  583. if (alphabits_graphics == 0)
  584. fz_enable_device_hints(ctx, dev, FZ_DONT_INTERPOLATE_IMAGES);
  585. if (list)
  586. fz_run_display_list(ctx, list, dev, ctm, tbounds, cookie);
  587. else
  588. fz_run_page(ctx, page, dev, ctm, cookie);
  589. fz_close_device(ctx, dev);
  590. fz_drop_device(ctx, dev);
  591. dev = NULL;
  592. if (invert)
  593. fz_invert_pixmap(ctx, pix);
  594. if (gamma_value != 1)
  595. fz_gamma_pixmap(ctx, pix, gamma_value);
  596. if (((output_format == OUT_PCL || output_format == OUT_PWG) && out_cs == CS_MONO) || (output_format == OUT_PBM) || (output_format == OUT_PKM))
  597. *bit = fz_new_bitmap_from_pixmap_band(ctx, pix, NULL, band_start);
  598. }
  599. fz_catch(ctx)
  600. {
  601. fz_drop_device(ctx, dev);
  602. fz_rethrow(ctx);
  603. }
  604. }
  605. static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, int pagenum, fz_cookie *cookie, int start, int interptime, char *fname, int bg, fz_separations *seps)
  606. {
  607. fz_rect mediabox;
  608. fz_device *dev = NULL;
  609. fz_var(dev);
  610. if (output_file_per_page)
  611. file_level_headers(ctx);
  612. if (list)
  613. mediabox = fz_bound_display_list(ctx, list);
  614. else
  615. mediabox = fz_bound_page_box(ctx, page, page_box);
  616. if (output_format == OUT_TRACE || output_format == OUT_OCR_TRACE)
  617. {
  618. float zoom;
  619. fz_matrix ctm;
  620. fz_device *pre_ocr_dev = NULL;
  621. fz_rect tmediabox;
  622. zoom = resolution / 72;
  623. ctm = fz_pre_scale(fz_rotate(rotation), zoom, zoom);
  624. tmediabox = fz_transform_rect(mediabox, ctm);
  625. fz_var(pre_ocr_dev);
  626. fz_try(ctx)
  627. {
  628. fz_write_printf(ctx, out, "<page mediabox=\"%g %g %g %g\">\n",
  629. tmediabox.x0, tmediabox.y0, tmediabox.x1, tmediabox.y1);
  630. dev = fz_new_trace_device(ctx, out);
  631. apply_kill_switch(dev);
  632. if (output_format == OUT_OCR_TRACE)
  633. {
  634. pre_ocr_dev = dev;
  635. dev = NULL;
  636. dev = fz_new_ocr_device(ctx, pre_ocr_dev, ctm, mediabox, 1, ocr_language, ocr_datadir, NULL, NULL);
  637. }
  638. if (lowmemory)
  639. fz_enable_device_hints(ctx, dev, FZ_NO_CACHE);
  640. if (list)
  641. fz_run_display_list(ctx, list, dev, ctm, fz_infinite_rect, cookie);
  642. else
  643. fz_run_page(ctx, page, dev, ctm, cookie);
  644. fz_close_device(ctx, dev);
  645. fz_drop_device(ctx, dev);
  646. dev = NULL;
  647. fz_close_device(ctx, pre_ocr_dev);
  648. fz_drop_device(ctx, pre_ocr_dev);
  649. pre_ocr_dev = NULL;
  650. fz_write_printf(ctx, out, "</page>\n");
  651. }
  652. fz_always(ctx)
  653. {
  654. fz_drop_device(ctx, pre_ocr_dev);
  655. fz_drop_device(ctx, dev);
  656. }
  657. fz_catch(ctx)
  658. {
  659. fz_rethrow(ctx);
  660. }
  661. }
  662. if (output_format == OUT_XMLTEXT)
  663. {
  664. fz_try(ctx)
  665. {
  666. float zoom;
  667. fz_matrix ctm;
  668. fz_rect tmediabox;
  669. zoom = resolution / 72;
  670. ctm = fz_pre_scale(fz_rotate(rotation), zoom, zoom);
  671. tmediabox = fz_transform_rect(mediabox, ctm);
  672. fz_write_printf(ctx, out, "<page mediabox=\"%g %g %g %g\">\n",
  673. tmediabox.x0, tmediabox.y0, tmediabox.x1, tmediabox.y1);
  674. dev = fz_new_xmltext_device(ctx, out);
  675. apply_kill_switch(dev);
  676. if (list)
  677. fz_run_display_list(ctx, list, dev, ctm, fz_infinite_rect, cookie);
  678. else
  679. fz_run_page(ctx, page, dev, ctm, cookie);
  680. fz_write_printf(ctx, out, "</page>\n");
  681. fz_close_device(ctx, dev);
  682. }
  683. fz_always(ctx)
  684. {
  685. fz_drop_device(ctx, dev);
  686. }
  687. fz_catch(ctx)
  688. {
  689. fz_rethrow(ctx);
  690. }
  691. }
  692. else if (output_format == OUT_BBOX)
  693. {
  694. fz_try(ctx)
  695. {
  696. fz_rect bbox = fz_empty_rect;
  697. float zoom;
  698. fz_matrix ctm;
  699. fz_rect tmediabox;
  700. zoom = resolution / 72;
  701. ctm = fz_pre_scale(fz_rotate(rotation), zoom, zoom);
  702. tmediabox = fz_transform_rect(mediabox, ctm);
  703. dev = fz_new_bbox_device(ctx, &bbox);
  704. apply_kill_switch(dev);
  705. if (lowmemory)
  706. fz_enable_device_hints(ctx, dev, FZ_NO_CACHE);
  707. if (list)
  708. fz_run_display_list(ctx, list, dev, ctm, fz_infinite_rect, cookie);
  709. else
  710. fz_run_page(ctx, page, dev, ctm, cookie);
  711. fz_close_device(ctx, dev);
  712. fz_write_printf(ctx, out, "<page bbox=\"%R\" mediabox=\"%R\" />\n", &bbox, &tmediabox);
  713. }
  714. fz_always(ctx)
  715. {
  716. fz_drop_device(ctx, dev);
  717. }
  718. fz_catch(ctx)
  719. {
  720. fz_rethrow(ctx);
  721. }
  722. }
  723. else if (output_format == OUT_TEXT || output_format == OUT_HTML || output_format == OUT_XHTML || output_format == OUT_STEXT_XML || output_format == OUT_STEXT_JSON ||
  724. output_format == OUT_OCR_TEXT || output_format == OUT_OCR_HTML || output_format == OUT_OCR_XHTML || output_format == OUT_OCR_STEXT_XML || output_format == OUT_OCR_STEXT_JSON)
  725. {
  726. fz_stext_page *text = NULL;
  727. float zoom;
  728. fz_matrix ctm;
  729. fz_device *pre_ocr_dev = NULL;
  730. fz_rect tmediabox;
  731. zoom = resolution / 72;
  732. ctm = fz_pre_scale(fz_rotate(rotation), zoom, zoom);
  733. fz_var(text);
  734. fz_var(pre_ocr_dev);
  735. fz_try(ctx)
  736. {
  737. fz_stext_options stext_options = { 0 };
  738. stext_options.flags = (output_format == OUT_HTML ||
  739. output_format == OUT_XHTML ||
  740. output_format == OUT_OCR_HTML ||
  741. output_format == OUT_OCR_XHTML
  742. ) ? FZ_STEXT_PRESERVE_IMAGES : 0;
  743. stext_options.flags |= FZ_STEXT_CLIP;
  744. stext_options.flags |= FZ_STEXT_ACCURATE_BBOXES;
  745. stext_options.flags |= FZ_STEXT_COLLECT_STYLES;
  746. if (output_format == OUT_STEXT_JSON || output_format == OUT_OCR_STEXT_JSON)
  747. stext_options.flags |= FZ_STEXT_PRESERVE_SPANS;
  748. tmediabox = fz_transform_rect(mediabox, ctm);
  749. text = fz_new_stext_page(ctx, tmediabox);
  750. dev = fz_new_stext_device(ctx, text, &stext_options);
  751. apply_kill_switch(dev);
  752. if (lowmemory)
  753. fz_enable_device_hints(ctx, dev, FZ_NO_CACHE);
  754. if (output_format == OUT_OCR_TEXT ||
  755. output_format == OUT_OCR_STEXT_JSON ||
  756. output_format == OUT_OCR_STEXT_XML ||
  757. output_format == OUT_OCR_HTML ||
  758. output_format == OUT_OCR_XHTML)
  759. {
  760. pre_ocr_dev = dev;
  761. dev = NULL;
  762. dev = fz_new_ocr_device(ctx, pre_ocr_dev, ctm, mediabox, 1, ocr_language, ocr_datadir, NULL, NULL);
  763. }
  764. if (list)
  765. fz_run_display_list(ctx, list, dev, ctm, fz_infinite_rect, cookie);
  766. else
  767. fz_run_page(ctx, page, dev, ctm, cookie);
  768. fz_close_device(ctx, dev);
  769. fz_drop_device(ctx, dev);
  770. dev = NULL;
  771. fz_close_device(ctx, pre_ocr_dev);
  772. fz_drop_device(ctx, pre_ocr_dev);
  773. pre_ocr_dev = NULL;
  774. if (output_format == OUT_STEXT_XML || output_format == OUT_OCR_STEXT_XML)
  775. {
  776. fz_print_stext_page_as_xml(ctx, out, text, pagenum);
  777. }
  778. else if (output_format == OUT_STEXT_JSON || output_format == OUT_OCR_STEXT_JSON)
  779. {
  780. static int first = 1;
  781. if (first)
  782. first = 0;
  783. else
  784. fz_write_string(ctx, out, ",");
  785. fz_print_stext_page_as_json(ctx, out, text, 1);
  786. }
  787. else if (output_format == OUT_HTML || output_format == OUT_OCR_HTML)
  788. {
  789. fz_print_stext_page_as_html(ctx, out, text, pagenum);
  790. }
  791. else if (output_format == OUT_XHTML || output_format == OUT_OCR_XHTML)
  792. {
  793. fz_print_stext_page_as_xhtml(ctx, out, text, pagenum);
  794. }
  795. else if (output_format == OUT_TEXT || output_format == OUT_OCR_TEXT)
  796. {
  797. fz_print_stext_page_as_text(ctx, out, text);
  798. fz_write_printf(ctx, out, "\f\n");
  799. }
  800. }
  801. fz_always(ctx)
  802. {
  803. fz_drop_device(ctx, pre_ocr_dev);
  804. fz_drop_device(ctx, dev);
  805. fz_drop_stext_page(ctx, text);
  806. }
  807. fz_catch(ctx)
  808. {
  809. fz_rethrow(ctx);
  810. }
  811. }
  812. #if FZ_ENABLE_PDF
  813. else if (output_format == OUT_PDF)
  814. {
  815. fz_buffer *contents = NULL;
  816. pdf_obj *resources = NULL;
  817. fz_var(contents);
  818. fz_var(resources);
  819. fz_try(ctx)
  820. {
  821. /* We are ignoring ctm here. Understandable in a way, as resolution makes no sense
  822. * when writing PDFs. Rotation is taken care of by the pdf_add_page call. */
  823. pdf_obj *page_obj;
  824. dev = pdf_page_write(ctx, pdfout, mediabox, &resources, &contents);
  825. apply_kill_switch(dev);
  826. if (list)
  827. fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, cookie);
  828. else
  829. fz_run_page(ctx, page, dev, fz_identity, cookie);
  830. fz_close_device(ctx, dev);
  831. fz_drop_device(ctx, dev);
  832. dev = NULL;
  833. page_obj = pdf_add_page(ctx, pdfout, mediabox, rotation, resources, contents);
  834. pdf_insert_page(ctx, pdfout, -1, page_obj);
  835. pdf_drop_obj(ctx, page_obj);
  836. }
  837. fz_always(ctx)
  838. {
  839. pdf_drop_obj(ctx, resources);
  840. fz_drop_buffer(ctx, contents);
  841. fz_drop_device(ctx, dev);
  842. }
  843. fz_catch(ctx)
  844. {
  845. fz_rethrow(ctx);
  846. }
  847. }
  848. #endif
  849. else if (output_format == OUT_SVG)
  850. {
  851. float zoom;
  852. fz_matrix ctm;
  853. fz_rect tbounds;
  854. char buf[512];
  855. fz_output *outs = NULL;
  856. fz_var(outs);
  857. zoom = resolution / 72;
  858. ctm = fz_pre_rotate(fz_scale(zoom, zoom), rotation);
  859. tbounds = fz_transform_rect(mediabox, ctm);
  860. fz_try(ctx)
  861. {
  862. if (!output || !strcmp(output, "-"))
  863. outs = fz_stdout(ctx);
  864. else
  865. {
  866. fz_format_output_path(ctx, buf, sizeof buf, output, pagenum);
  867. outs = fz_new_output_with_path(ctx, buf, 0);
  868. }
  869. dev = fz_new_svg_device(ctx, outs, tbounds.x1-tbounds.x0, tbounds.y1-tbounds.y0, FZ_SVG_TEXT_AS_PATH, 1);
  870. apply_kill_switch(dev);
  871. if (lowmemory)
  872. fz_enable_device_hints(ctx, dev, FZ_NO_CACHE);
  873. if (list)
  874. fz_run_display_list(ctx, list, dev, ctm, tbounds, cookie);
  875. else
  876. fz_run_page(ctx, page, dev, ctm, cookie);
  877. fz_close_device(ctx, dev);
  878. fz_close_output(ctx, outs);
  879. }
  880. fz_always(ctx)
  881. {
  882. fz_drop_device(ctx, dev);
  883. fz_drop_output(ctx, outs);
  884. }
  885. fz_catch(ctx)
  886. {
  887. fz_rethrow(ctx);
  888. }
  889. }
  890. else
  891. {
  892. float zoom;
  893. fz_matrix ctm;
  894. fz_rect tbounds;
  895. fz_irect ibounds;
  896. fz_pixmap *pix = NULL;
  897. int w, h;
  898. fz_bitmap *bit = NULL;
  899. fz_var(pix);
  900. fz_var(bander);
  901. fz_var(bit);
  902. zoom = resolution / 72;
  903. ctm = fz_pre_scale(fz_rotate(rotation), zoom, zoom);
  904. tbounds = fz_transform_rect(mediabox, ctm);
  905. ibounds = fz_round_rect(tbounds);
  906. /* Make local copies of our width/height */
  907. w = width;
  908. h = height;
  909. /* If a resolution is specified, check to see whether w/h are
  910. * exceeded; if not, unset them. */
  911. if (res_specified)
  912. {
  913. int t;
  914. t = ibounds.x1 - ibounds.x0;
  915. if (w && t <= w)
  916. w = 0;
  917. t = ibounds.y1 - ibounds.y0;
  918. if (h && t <= h)
  919. h = 0;
  920. }
  921. /* Now w or h will be 0 unless they need to be enforced. */
  922. if (w || h)
  923. {
  924. float scalex = w / (tbounds.x1 - tbounds.x0);
  925. float scaley = h / (tbounds.y1 - tbounds.y0);
  926. fz_matrix scale_mat;
  927. if (fit)
  928. {
  929. if (w == 0)
  930. scalex = 1.0f;
  931. if (h == 0)
  932. scaley = 1.0f;
  933. }
  934. else
  935. {
  936. if (w == 0)
  937. scalex = scaley;
  938. if (h == 0)
  939. scaley = scalex;
  940. }
  941. if (!fit)
  942. {
  943. if (scalex > scaley)
  944. scalex = scaley;
  945. else
  946. scaley = scalex;
  947. }
  948. scale_mat = fz_scale(scalex, scaley);
  949. ctm = fz_concat(ctm, scale_mat);
  950. tbounds = fz_transform_rect(mediabox, ctm);
  951. }
  952. ibounds = fz_round_rect(tbounds);
  953. tbounds = fz_rect_from_irect(ibounds);
  954. fz_try(ctx)
  955. {
  956. fz_irect band_ibounds = ibounds;
  957. int band, bands = 1;
  958. int totalheight = ibounds.y1 - ibounds.y0;
  959. int drawheight = totalheight;
  960. if (band_height != 0)
  961. {
  962. /* Banded rendering; we'll only render to a
  963. * given height at a time. */
  964. drawheight = band_height;
  965. if (totalheight > band_height)
  966. band_ibounds.y1 = band_ibounds.y0 + band_height;
  967. bands = (totalheight + band_height-1)/band_height;
  968. tbounds.y1 = tbounds.y0 + band_height + 2;
  969. DEBUG_THREADS(("Using %d Bands\n", bands));
  970. }
  971. if (num_workers > 0)
  972. {
  973. for (band = 0; band < fz_mini(num_workers, bands); band++)
  974. {
  975. workers[band].band = band;
  976. workers[band].error = 0;
  977. workers[band].ctm = ctm;
  978. workers[band].tbounds = tbounds;
  979. tbounds.y0 += band_height;
  980. tbounds.y1 += band_height;
  981. memset(&workers[band].cookie, 0, sizeof(fz_cookie));
  982. workers[band].list = list;
  983. workers[band].pix = fz_new_pixmap_with_bbox(ctx, colorspace, band_ibounds, seps, alpha);
  984. workers[band].pix->y += band * band_height;
  985. fz_set_pixmap_resolution(ctx, workers[band].pix, resolution, resolution);
  986. workers[band].running = 1;
  987. #ifndef DISABLE_MUTHREADS
  988. DEBUG_THREADS(("Worker %d, Pre-triggering band %d\n", band, band));
  989. mu_trigger_semaphore(&workers[band].start);
  990. #endif
  991. }
  992. pix = workers[0].pix;
  993. }
  994. else
  995. {
  996. pix = fz_new_pixmap_with_bbox(ctx, colorspace, band_ibounds, seps, alpha);
  997. fz_set_pixmap_resolution(ctx, pix, resolution, resolution);
  998. }
  999. /* Output any page level headers (for banded formats) */
  1000. if (output)
  1001. {
  1002. if (output_format == OUT_PGM || output_format == OUT_PPM || output_format == OUT_PNM)
  1003. bander = fz_new_pnm_band_writer(ctx, out);
  1004. else if (output_format == OUT_PAM)
  1005. bander = fz_new_pam_band_writer(ctx, out);
  1006. else if (output_format == OUT_PNG)
  1007. bander = fz_new_png_band_writer(ctx, out);
  1008. else if (output_format == OUT_PBM)
  1009. bander = fz_new_pbm_band_writer(ctx, out);
  1010. else if (output_format == OUT_PKM)
  1011. bander = fz_new_pkm_band_writer(ctx, out);
  1012. else if (output_format == OUT_PS)
  1013. bander = fz_new_ps_band_writer(ctx, out);
  1014. else if (output_format == OUT_PSD)
  1015. bander = fz_new_psd_band_writer(ctx, out);
  1016. else if (output_format == OUT_PWG)
  1017. {
  1018. if (out_cs == CS_MONO)
  1019. bander = fz_new_mono_pwg_band_writer(ctx, out, NULL);
  1020. else
  1021. bander = fz_new_pwg_band_writer(ctx, out, NULL);
  1022. }
  1023. else if (output_format == OUT_PCL)
  1024. {
  1025. if (out_cs == CS_MONO)
  1026. bander = fz_new_mono_pcl_band_writer(ctx, out, NULL);
  1027. else
  1028. bander = fz_new_color_pcl_band_writer(ctx, out, NULL);
  1029. }
  1030. if (bander)
  1031. {
  1032. fz_write_header(ctx, bander, pix->w, totalheight, pix->n, pix->alpha, pix->xres, pix->yres, output_pagenum++, pix->colorspace, pix->seps);
  1033. }
  1034. }
  1035. if (output_format == OUT_J2K && bands > 1)
  1036. {
  1037. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't band with J2k output!");
  1038. }
  1039. for (band = 0; band < bands; band++)
  1040. {
  1041. if (num_workers > 0)
  1042. {
  1043. worker_t *work = &workers[band % num_workers];
  1044. #ifndef DISABLE_MUTHREADS
  1045. DEBUG_THREADS(("Waiting for worker %d to complete band %d\n", work->num, band));
  1046. mu_wait_semaphore(&work->stop);
  1047. #endif
  1048. work->running = 0;
  1049. cookie->errors += work->cookie.errors;
  1050. pix = work->pix;
  1051. bit = work->bit;
  1052. work->bit = NULL;
  1053. if (work->error)
  1054. fz_throw(ctx, FZ_ERROR_GENERIC, "worker %d failed to render band %d", work->num, band);
  1055. }
  1056. else
  1057. drawband(ctx, page, list, ctm, tbounds, cookie, band * band_height, pix, &bit);
  1058. if (output)
  1059. {
  1060. if (bander && (pix || bit))
  1061. fz_write_band(ctx, bander, bit ? bit->stride : pix->stride, drawheight, bit ? bit->samples : pix->samples);
  1062. #if FZ_ENABLE_JPX
  1063. if (output_format == OUT_J2K)
  1064. {
  1065. fz_write_pixmap_as_jpx(ctx, out, pix, 80);
  1066. }
  1067. #else
  1068. fz_throw(ctx, FZ_ERROR_GENERIC, "JPX support disabled");
  1069. #endif
  1070. fz_drop_bitmap(ctx, bit);
  1071. bit = NULL;
  1072. }
  1073. if (num_workers > 0 && band + num_workers < bands)
  1074. {
  1075. worker_t *work = &workers[band % num_workers];
  1076. work->band = band + num_workers;
  1077. work->pix->y = band_ibounds.y0 + work->band * band_height;
  1078. work->ctm = ctm;
  1079. work->tbounds = tbounds;
  1080. memset(&work->cookie, 0, sizeof(fz_cookie));
  1081. work->running = 1;
  1082. #ifndef DISABLE_MUTHREADS
  1083. DEBUG_THREADS(("Triggering worker %d for band %d\n", work->num, work->band));
  1084. mu_trigger_semaphore(&work->start);
  1085. #endif
  1086. }
  1087. if (num_workers <= 0)
  1088. pix->y += band_height;
  1089. tbounds.y0 += band_height;
  1090. tbounds.y1 += band_height;
  1091. }
  1092. if (output_format != OUT_PCLM && output_format != OUT_OCR_PDF)
  1093. fz_close_band_writer(ctx, bander);
  1094. /* FIXME */
  1095. if (showmd5 && pix)
  1096. {
  1097. unsigned char digest[16];
  1098. int i;
  1099. fz_md5_pixmap(ctx, pix, digest);
  1100. fprintf(stderr, " ");
  1101. for (i = 0; i < 16; i++)
  1102. fprintf(stderr, "%02x", digest[i]);
  1103. }
  1104. }
  1105. fz_always(ctx)
  1106. {
  1107. if (output_format != OUT_PCLM && output_format != OUT_OCR_PDF)
  1108. {
  1109. fz_drop_band_writer(ctx, bander);
  1110. /* bander must be set to NULL to avoid use-after-frees. A use-after-free
  1111. * would occur when a valid page was followed by a page with invalid
  1112. * pixmap dimensions, causing bander -- a static -- to point to previously
  1113. * freed memory instead of a new band_writer. */
  1114. bander = NULL;
  1115. }
  1116. fz_drop_bitmap(ctx, bit);
  1117. bit = NULL;
  1118. if (num_workers > 0)
  1119. {
  1120. int i;
  1121. DEBUG_THREADS(("Stopping workers and removing their pixmaps\n"));
  1122. for (i = 0; i < num_workers; i++)
  1123. {
  1124. if (workers[i].running)
  1125. {
  1126. #ifndef DISABLE_MUTHREADS
  1127. DEBUG_THREADS(("Waiting on worker %d to finish processing\n", i));
  1128. mu_wait_semaphore(&workers[i].stop);
  1129. #endif
  1130. workers[i].running = 0;
  1131. }
  1132. else
  1133. DEBUG_THREADS(("Worker %d not processing anything\n", i));
  1134. fz_drop_pixmap(ctx, workers[i].pix);
  1135. workers[i].pix = NULL;
  1136. }
  1137. }
  1138. else
  1139. fz_drop_pixmap(ctx, pix);
  1140. }
  1141. fz_catch(ctx)
  1142. {
  1143. if (output_format == OUT_PCLM || output_format == OUT_OCR_PDF)
  1144. {
  1145. fz_drop_band_writer(ctx, bander);
  1146. bander = NULL;
  1147. }
  1148. fz_rethrow(ctx);
  1149. }
  1150. }
  1151. if (output_file_per_page)
  1152. file_level_trailers(ctx);
  1153. if (showtime)
  1154. {
  1155. int end = gettime();
  1156. int diff = end - start;
  1157. if (bg)
  1158. {
  1159. if (diff + interptime < timing.min)
  1160. {
  1161. timing.min = diff + interptime;
  1162. timing.mininterp = interptime;
  1163. timing.minpage = pagenum;
  1164. timing.minfilename = fname;
  1165. }
  1166. if (diff + interptime > timing.max)
  1167. {
  1168. timing.max = diff + interptime;
  1169. timing.maxinterp = interptime;
  1170. timing.maxpage = pagenum;
  1171. timing.maxfilename = fname;
  1172. }
  1173. timing.count ++;
  1174. fprintf(stderr, " %dms (interpretation) %dms (rendering) %dms (total)", interptime, diff, diff + interptime);
  1175. }
  1176. else
  1177. {
  1178. if (diff < timing.min)
  1179. {
  1180. timing.min = diff;
  1181. timing.minpage = pagenum;
  1182. timing.minfilename = fname;
  1183. }
  1184. if (diff > timing.max)
  1185. {
  1186. timing.max = diff;
  1187. timing.maxpage = pagenum;
  1188. timing.maxfilename = fname;
  1189. }
  1190. timing.total += diff;
  1191. timing.count ++;
  1192. fprintf(stderr, " %dms", diff);
  1193. }
  1194. }
  1195. if (!quiet || showfeatures || showtime || showmd5)
  1196. fprintf(stderr, "\n");
  1197. if (lowmemory)
  1198. fz_empty_store(ctx);
  1199. if (showmemory)
  1200. fz_dump_glyph_cache_stats(ctx, fz_stderr(ctx));
  1201. fz_flush_warnings(ctx);
  1202. if (cookie->errors)
  1203. errored = 1;
  1204. }
  1205. static void bgprint_flush(void)
  1206. {
  1207. if (!bgprint.active || !bgprint.started)
  1208. return;
  1209. #ifndef DISABLE_MUTHREADS
  1210. mu_wait_semaphore(&bgprint.stop);
  1211. #endif
  1212. bgprint.started = 0;
  1213. }
  1214. static void drawpage(fz_context *ctx, fz_document *doc, int pagenum)
  1215. {
  1216. fz_page *page;
  1217. fz_display_list *list = NULL;
  1218. fz_device *dev = NULL;
  1219. int start;
  1220. fz_cookie cookie = { 0 };
  1221. fz_separations *seps = NULL;
  1222. const char *features = "";
  1223. fz_var(list);
  1224. fz_var(dev);
  1225. fz_var(seps);
  1226. start = (showtime ? gettime() : 0);
  1227. if (output_file_per_page)
  1228. {
  1229. char text_buffer[512];
  1230. bgprint_flush();
  1231. if (out)
  1232. {
  1233. fz_close_output(ctx, out);
  1234. fz_drop_output(ctx, out);
  1235. }
  1236. fz_format_output_path(ctx, text_buffer, sizeof text_buffer, output, pagenum);
  1237. out = fz_new_output_with_path(ctx, text_buffer, 0);
  1238. }
  1239. page = fz_load_page(ctx, doc, pagenum - 1);
  1240. if (spots != SPOTS_NONE)
  1241. {
  1242. fz_try(ctx)
  1243. {
  1244. seps = fz_page_separations(ctx, page);
  1245. if (seps)
  1246. {
  1247. int i, n = fz_count_separations(ctx, seps);
  1248. if (spots == SPOTS_FULL)
  1249. for (i = 0; i < n; i++)
  1250. fz_set_separation_behavior(ctx, seps, i, FZ_SEPARATION_SPOT);
  1251. else
  1252. for (i = 0; i < n; i++)
  1253. fz_set_separation_behavior(ctx, seps, i, FZ_SEPARATION_COMPOSITE);
  1254. }
  1255. else if (fz_page_uses_overprint(ctx, page))
  1256. {
  1257. /* This page uses overprint, so we need an empty
  1258. * sep object to force the overprint simulation on. */
  1259. seps = fz_new_separations(ctx, 0);
  1260. }
  1261. else if (oi && fz_colorspace_n(ctx, oi) != fz_colorspace_n(ctx, colorspace))
  1262. {
  1263. /* We have an output intent, and it's incompatible
  1264. * with the colorspace our device needs. Force the
  1265. * overprint simulation on, because this ensures that
  1266. * we 'simulate' the output intent too. */
  1267. seps = fz_new_separations(ctx, 0);
  1268. }
  1269. }
  1270. fz_catch(ctx)
  1271. {
  1272. fz_drop_page(ctx, page);
  1273. fz_rethrow(ctx);
  1274. }
  1275. }
  1276. if (uselist)
  1277. {
  1278. fz_try(ctx)
  1279. {
  1280. list = fz_new_display_list(ctx, fz_bound_page_box(ctx, page, page_box));
  1281. dev = fz_new_list_device(ctx, list);
  1282. if (lowmemory)
  1283. fz_enable_device_hints(ctx, dev, FZ_NO_CACHE);
  1284. fz_run_page(ctx, page, dev, fz_identity, &cookie);
  1285. fz_close_device(ctx, dev);
  1286. }
  1287. fz_always(ctx)
  1288. {
  1289. fz_drop_device(ctx, dev);
  1290. dev = NULL;
  1291. }
  1292. fz_catch(ctx)
  1293. {
  1294. fz_drop_display_list(ctx, list);
  1295. fz_drop_separations(ctx, seps);
  1296. fz_drop_page(ctx, page);
  1297. fz_rethrow(ctx);
  1298. }
  1299. if (bgprint.active && showtime)
  1300. {
  1301. int end = gettime();
  1302. start = end - start;
  1303. }
  1304. }
  1305. if (showfeatures)
  1306. {
  1307. int iscolor;
  1308. dev = fz_new_test_device(ctx, &iscolor, 0.02f, 0, NULL);
  1309. apply_kill_switch(dev);
  1310. if (lowmemory)
  1311. fz_enable_device_hints(ctx, dev, FZ_NO_CACHE);
  1312. fz_try(ctx)
  1313. {
  1314. if (list)
  1315. fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, NULL);
  1316. else
  1317. fz_run_page(ctx, page, dev, fz_identity, &cookie);
  1318. fz_close_device(ctx, dev);
  1319. }
  1320. fz_always(ctx)
  1321. {
  1322. fz_drop_device(ctx, dev);
  1323. dev = NULL;
  1324. }
  1325. fz_catch(ctx)
  1326. {
  1327. fz_drop_display_list(ctx, list);
  1328. fz_drop_separations(ctx, seps);
  1329. fz_drop_page(ctx, page);
  1330. fz_rethrow(ctx);
  1331. }
  1332. features = iscolor ? " color" : " grayscale";
  1333. }
  1334. if (bgprint.active)
  1335. {
  1336. bgprint_flush();
  1337. if (bgprint.error)
  1338. {
  1339. fz_drop_display_list(ctx, list);
  1340. fz_drop_separations(ctx, seps);
  1341. fz_drop_page(ctx, page);
  1342. /* it failed, do not continue trying */
  1343. bgprint.active = 0;
  1344. }
  1345. else if (bgprint.active)
  1346. {
  1347. if (!quiet || showfeatures || showtime || showmd5)
  1348. fprintf(stderr, "page %s %d%s", filename, pagenum, features);
  1349. bgprint.started = 1;
  1350. bgprint.page = page;
  1351. bgprint.list = list;
  1352. bgprint.seps = seps;
  1353. bgprint.filename = filename;
  1354. bgprint.pagenum = pagenum;
  1355. bgprint.interptime = start;
  1356. bgprint.error = 0;
  1357. #ifndef DISABLE_MUTHREADS
  1358. mu_trigger_semaphore(&bgprint.start);
  1359. #else
  1360. fz_drop_display_list(ctx, list);
  1361. fz_drop_separations(ctx, seps);
  1362. fz_drop_page(ctx, page);
  1363. #endif
  1364. }
  1365. }
  1366. else
  1367. {
  1368. if (!quiet || showfeatures || showtime || showmd5)
  1369. fprintf(stderr, "page %s %d%s", filename, pagenum, features);
  1370. fz_try(ctx)
  1371. dodrawpage(ctx, page, list, pagenum, &cookie, start, 0, filename, 0, seps);
  1372. fz_always(ctx)
  1373. {
  1374. fz_drop_display_list(ctx, list);
  1375. fz_drop_separations(ctx, seps);
  1376. fz_drop_page(ctx, page);
  1377. }
  1378. fz_catch(ctx)
  1379. {
  1380. fz_rethrow(ctx);
  1381. }
  1382. }
  1383. }
  1384. static void drawrange(fz_context *ctx, fz_document *doc, const char *range)
  1385. {
  1386. int page, spage, epage, pagecount;
  1387. pagecount = fz_count_pages(ctx, doc);
  1388. while ((range = fz_parse_page_range(ctx, range, &spage, &epage, pagecount)))
  1389. {
  1390. if (spage < epage)
  1391. for (page = spage; page <= epage; page++)
  1392. {
  1393. fz_try(ctx)
  1394. drawpage(ctx, doc, page);
  1395. fz_catch(ctx)
  1396. {
  1397. if (ignore_errors)
  1398. fz_warn(ctx, "ignoring error on page %d in '%s'", page, filename);
  1399. else
  1400. fz_rethrow(ctx);
  1401. }
  1402. }
  1403. else
  1404. for (page = spage; page >= epage; page--)
  1405. {
  1406. fz_try(ctx)
  1407. drawpage(ctx, doc, page);
  1408. fz_catch(ctx)
  1409. {
  1410. if (ignore_errors)
  1411. fz_warn(ctx, "ignoring error on page %d in '%s'", page, filename);
  1412. else
  1413. fz_rethrow(ctx);
  1414. }
  1415. }
  1416. }
  1417. }
  1418. static int
  1419. parse_colorspace(const char *name)
  1420. {
  1421. int i;
  1422. for (i = 0; i < (int)nelem(cs_name_table); i++)
  1423. {
  1424. if (!strcmp(name, cs_name_table[i].name))
  1425. return cs_name_table[i].colorspace;
  1426. }
  1427. /* Assume ICC. We will error out later if not the case. */
  1428. icc_filename = name;
  1429. return CS_ICC;
  1430. }
  1431. typedef struct
  1432. {
  1433. size_t size;
  1434. #if defined(_M_IA64) || defined(_M_AMD64)
  1435. size_t align;
  1436. #endif
  1437. } trace_header;
  1438. typedef struct
  1439. {
  1440. size_t current;
  1441. size_t peak;
  1442. size_t total;
  1443. size_t allocs;
  1444. size_t mem_limit;
  1445. size_t alloc_limit;
  1446. } trace_info;
  1447. static void *hit_limit(void *val)
  1448. {
  1449. return val;
  1450. }
  1451. static void *hit_memory_limit(trace_info *info, int is_malloc, size_t oldsize, size_t size)
  1452. {
  1453. if (is_malloc)
  1454. printf("Memory limit (%zu) hit upon malloc(%zu) when %zu already allocated.\n", info->mem_limit, size, info->current);
  1455. else
  1456. printf("Memory limit (%zu) hit upon realloc(%zu) from %zu bytes when %zu already allocated.\n", info->mem_limit, size, oldsize, info->current);
  1457. return hit_limit(NULL);
  1458. }
  1459. static void *hit_alloc_limit(trace_info *info, int is_malloc, size_t oldsize, size_t size)
  1460. {
  1461. if (is_malloc)
  1462. printf("Allocation limit (%zu) hit upon malloc(%zu) when %zu already allocated.\n", info->alloc_limit, size, info->current);
  1463. else
  1464. printf("Allocation limit (%zu) hit upon realloc(%zu) from %zu bytes when %zu already allocated.\n", info->alloc_limit, size, oldsize, info->current);
  1465. return hit_limit(NULL);
  1466. }
  1467. static void *
  1468. trace_malloc(void *arg, size_t size)
  1469. {
  1470. trace_info *info = (trace_info *) arg;
  1471. trace_header *p;
  1472. if (size == 0)
  1473. return NULL;
  1474. if (size > SIZE_MAX - sizeof(trace_header))
  1475. return NULL;
  1476. if (info->mem_limit > 0 && size > info->mem_limit - info->current)
  1477. return hit_memory_limit(info, 1, 0, size);
  1478. if (info->alloc_limit > 0 && info->allocs > info->alloc_limit)
  1479. return hit_alloc_limit(info, 1, 0, size);
  1480. p = malloc(size + sizeof(trace_header));
  1481. if (p == NULL)
  1482. return NULL;
  1483. p[0].size = size;
  1484. info->current += size;
  1485. info->total += size;
  1486. if (info->current > info->peak)
  1487. info->peak = info->current;
  1488. info->allocs++;
  1489. return (void *)&p[1];
  1490. }
  1491. static void
  1492. trace_free(void *arg, void *p_)
  1493. {
  1494. trace_info *info = (trace_info *) arg;
  1495. trace_header *p = (trace_header *)p_;
  1496. if (p == NULL)
  1497. return;
  1498. info->current -= p[-1].size;
  1499. free(&p[-1]);
  1500. }
  1501. static void *
  1502. trace_realloc(void *arg, void *p_, size_t size)
  1503. {
  1504. trace_info *info = (trace_info *) arg;
  1505. trace_header *p = (trace_header *)p_;
  1506. size_t oldsize;
  1507. if (size == 0)
  1508. {
  1509. trace_free(arg, p_);
  1510. return NULL;
  1511. }
  1512. if (p == NULL)
  1513. return trace_malloc(arg, size);
  1514. if (size > SIZE_MAX - sizeof(trace_header))
  1515. return NULL;
  1516. oldsize = p[-1].size;
  1517. if (info->mem_limit > 0 && size > info->mem_limit - info->current + oldsize)
  1518. return hit_memory_limit(info, 0, oldsize, size);
  1519. if (info->alloc_limit > 0 && info->allocs > info->alloc_limit)
  1520. return hit_alloc_limit(info, 0, oldsize, size);
  1521. p = realloc(&p[-1], size + sizeof(trace_header));
  1522. if (p == NULL)
  1523. return NULL;
  1524. info->current += size - oldsize;
  1525. if (size > oldsize)
  1526. info->total += size - oldsize;
  1527. if (info->current > info->peak)
  1528. info->peak = info->current;
  1529. p[0].size = size;
  1530. info->allocs++;
  1531. return &p[1];
  1532. }
  1533. #ifndef DISABLE_MUTHREADS
  1534. static void worker_thread(void *arg)
  1535. {
  1536. worker_t *me = (worker_t *)arg;
  1537. int band;
  1538. do
  1539. {
  1540. DEBUG_THREADS(("Worker %d waiting\n", me->num));
  1541. mu_wait_semaphore(&me->start);
  1542. band = me->band;
  1543. DEBUG_THREADS(("Worker %d woken for band %d\n", me->num, band));
  1544. if (band >= 0)
  1545. {
  1546. fz_try(me->ctx)
  1547. {
  1548. drawband(me->ctx, NULL, me->list, me->ctm, me->tbounds, &me->cookie, band * band_height, me->pix, &me->bit);
  1549. DEBUG_THREADS(("Worker %d completed band %d\n", me->num, band));
  1550. }
  1551. fz_catch(me->ctx)
  1552. {
  1553. DEBUG_THREADS(("Worker %d failed on band %d\n", me->num, band));
  1554. me->error = 1;
  1555. }
  1556. }
  1557. mu_trigger_semaphore(&me->stop);
  1558. }
  1559. while (band >= 0);
  1560. DEBUG_THREADS(("Worker %d shutting down\n", me->num));
  1561. }
  1562. static void bgprint_worker(void *arg)
  1563. {
  1564. fz_cookie cookie = { 0 };
  1565. int pagenum;
  1566. (void)arg;
  1567. do
  1568. {
  1569. DEBUG_THREADS(("BGPrint waiting\n"));
  1570. mu_wait_semaphore(&bgprint.start);
  1571. pagenum = bgprint.pagenum;
  1572. DEBUG_THREADS(("BGPrint woken for pagenum %d\n", pagenum));
  1573. if (pagenum >= 0)
  1574. {
  1575. int start = gettime();
  1576. memset(&cookie, 0, sizeof(cookie));
  1577. fz_try(bgprint.ctx)
  1578. {
  1579. dodrawpage(bgprint.ctx, bgprint.page, bgprint.list, pagenum, &cookie, start, bgprint.interptime, bgprint.filename, 1, bgprint.seps);
  1580. DEBUG_THREADS(("BGPrint completed page %d\n", pagenum));
  1581. }
  1582. fz_always(bgprint.ctx)
  1583. {
  1584. fz_drop_display_list(bgprint.ctx, bgprint.list);
  1585. fz_drop_separations(bgprint.ctx, bgprint.seps);
  1586. fz_drop_page(bgprint.ctx, bgprint.page);
  1587. }
  1588. fz_catch(bgprint.ctx)
  1589. {
  1590. DEBUG_THREADS(("BGPrint failed on page %d\n", pagenum));
  1591. bgprint.error = 1;
  1592. }
  1593. }
  1594. mu_trigger_semaphore(&bgprint.stop);
  1595. }
  1596. while (pagenum >= 0);
  1597. DEBUG_THREADS(("BGPrint shutting down\n"));
  1598. }
  1599. #endif
  1600. static inline int iswhite(int ch)
  1601. {
  1602. return
  1603. ch == '\011' || ch == '\012' ||
  1604. ch == '\014' || ch == '\015' || ch == '\040';
  1605. }
  1606. static void list_layers(fz_context *ctx, fz_document *doc)
  1607. {
  1608. #if FZ_ENABLE_PDF
  1609. pdf_document *pdoc = pdf_specifics(ctx, doc);
  1610. int k, n = pdf_count_layers(ctx, pdoc);
  1611. for (k = 0; k < n; ++k) {
  1612. const char *name = pdf_layer_name(ctx, pdoc, k);
  1613. int state = pdf_layer_is_enabled(ctx, pdoc, k);
  1614. fprintf(stderr, "layer %d (%s): %s\n", k+1, state ? "on" : "off", name);
  1615. }
  1616. #endif
  1617. }
  1618. static void toggle_layers(fz_context *ctx, fz_document *doc)
  1619. {
  1620. #if FZ_ENABLE_PDF
  1621. pdf_document *pdoc = pdf_specifics(ctx, doc);
  1622. int i, k, n;
  1623. n = pdf_count_layers(ctx, pdoc);
  1624. for (i = 0; i < layer_off_len; ++i)
  1625. {
  1626. if (layer_off[i] == -1)
  1627. for (k = 0; k < n; ++k)
  1628. pdf_enable_layer(ctx, pdoc, k, 0);
  1629. else if (layer_off[i] >= 1 && layer_off[i] <= n)
  1630. pdf_enable_layer(ctx, pdoc, layer_off[i] - 1, 0);
  1631. else
  1632. fprintf(stderr, "invalid layer: %d\n", layer_off[i]);
  1633. }
  1634. for (i = 0; i < layer_on_len; ++i)
  1635. {
  1636. if (layer_on[i] == -1)
  1637. for (k = 0; k < n; ++k)
  1638. pdf_enable_layer(ctx, pdoc, k, 1);
  1639. else if (layer_on[i] >= 1 && layer_on[i] <= n)
  1640. pdf_enable_layer(ctx, pdoc, layer_on[i] - 1, 1);
  1641. else
  1642. fprintf(stderr, "invalid layer: %d\n", layer_on[i]);
  1643. }
  1644. #endif
  1645. }
  1646. static void apply_layer_config(fz_context *ctx, fz_document *doc, const char *lc)
  1647. {
  1648. #if FZ_ENABLE_PDF
  1649. pdf_document *pdoc = pdf_specifics(ctx, doc);
  1650. int config;
  1651. int n, j;
  1652. pdf_layer_config info;
  1653. if (!pdoc)
  1654. {
  1655. fz_warn(ctx, "Only PDF files have layers");
  1656. return;
  1657. }
  1658. while (iswhite(*lc))
  1659. lc++;
  1660. if (*lc == 0 || *lc == 'l')
  1661. {
  1662. int num_configs = pdf_count_layer_configs(ctx, pdoc);
  1663. fprintf(stderr, "Layer configs:\n");
  1664. for (config = 0; config < num_configs; config++)
  1665. {
  1666. fprintf(stderr, " %s%d:", config < 10 ? " " : "", config);
  1667. pdf_layer_config_info(ctx, pdoc, config, &info);
  1668. if (info.name)
  1669. fprintf(stderr, " Name=\"%s\"", info.name);
  1670. if (info.creator)
  1671. fprintf(stderr, " Creator=\"%s\"", info.creator);
  1672. fprintf(stderr, "\n");
  1673. }
  1674. return;
  1675. }
  1676. /* Read the config number */
  1677. if (*lc < '0' || *lc > '9')
  1678. {
  1679. fprintf(stderr, "cannot find number expected for -y\n");
  1680. return;
  1681. }
  1682. config = fz_atoi(lc);
  1683. pdf_select_layer_config(ctx, pdoc, config);
  1684. while (*lc)
  1685. {
  1686. int item;
  1687. /* Skip over the last number we read (in the fz_atoi) */
  1688. while (*lc >= '0' && *lc <= '9')
  1689. lc++;
  1690. while (iswhite(*lc))
  1691. lc++;
  1692. if (*lc != ',')
  1693. break;
  1694. lc++;
  1695. while (iswhite(*lc))
  1696. lc++;
  1697. if (*lc < '0' || *lc > '9')
  1698. {
  1699. fprintf(stderr, "Expected a number for UI item to toggle\n");
  1700. return;
  1701. }
  1702. item = fz_atoi(lc);
  1703. pdf_toggle_layer_config_ui(ctx, pdoc, item);
  1704. }
  1705. /* Now list the final state of the config */
  1706. fprintf(stderr, "Layer Config %d:\n", config);
  1707. pdf_layer_config_info(ctx, pdoc, config, &info);
  1708. if (info.name)
  1709. fprintf(stderr, " Name=\"%s\"", info.name);
  1710. if (info.creator)
  1711. fprintf(stderr, " Creator=\"%s\"", info.creator);
  1712. fprintf(stderr, "\n");
  1713. n = pdf_count_layer_config_ui(ctx, pdoc);
  1714. for (j = 0; j < n; j++)
  1715. {
  1716. pdf_layer_config_ui ui;
  1717. pdf_layer_config_ui_info(ctx, pdoc, j, &ui);
  1718. fprintf(stderr, "%s%d: ", j < 10 ? " " : "", j);
  1719. while (ui.depth > 0)
  1720. {
  1721. ui.depth--;
  1722. fprintf(stderr, " ");
  1723. }
  1724. if (ui.type == PDF_LAYER_UI_CHECKBOX)
  1725. fprintf(stderr, " [%c] ", ui.selected ? 'x' : ' ');
  1726. else if (ui.type == PDF_LAYER_UI_RADIOBOX)
  1727. fprintf(stderr, " (%c) ", ui.selected ? 'x' : ' ');
  1728. if (ui.text)
  1729. fprintf(stderr, "%s", ui.text);
  1730. if (ui.type != PDF_LAYER_UI_LABEL && ui.locked)
  1731. fprintf(stderr, " <locked>");
  1732. fprintf(stderr, "\n");
  1733. }
  1734. #endif
  1735. }
  1736. static int create_accel_path(fz_context *ctx, char outname[], size_t len, int create, const char *absname, ...)
  1737. {
  1738. va_list args;
  1739. char *s = outname;
  1740. size_t z, remain = len;
  1741. char *arg;
  1742. va_start(args, absname);
  1743. while ((arg = va_arg(args, char *)) != NULL)
  1744. {
  1745. z = fz_snprintf(s, remain, "%s", arg);
  1746. if (z+1 > remain)
  1747. goto fail; /* won't fit */
  1748. if (create)
  1749. (void) fz_mkdir(outname);
  1750. if (!fz_is_directory(ctx, outname))
  1751. goto fail; /* directory creation failed, or that dir doesn't exist! */
  1752. #ifdef _WIN32
  1753. s[z] = '\\';
  1754. #else
  1755. s[z] = '/';
  1756. #endif
  1757. s[z+1] = 0;
  1758. s += z+1;
  1759. remain -= z+1;
  1760. }
  1761. if (fz_snprintf(s, remain, "%s.accel", absname) >= remain)
  1762. goto fail; /* won't fit */
  1763. va_end(args);
  1764. return 1;
  1765. fail:
  1766. va_end(args);
  1767. return 0;
  1768. }
  1769. static int convert_to_accel_path(fz_context *ctx, char outname[], char *absname, size_t len, int create)
  1770. {
  1771. char *tmpdir;
  1772. char *s;
  1773. if (absname[0] == '/' || absname[0] == '\\')
  1774. ++absname;
  1775. s = absname;
  1776. while (*s) {
  1777. if (*s == '/' || *s == '\\' || *s == ':')
  1778. *s = '%';
  1779. ++s;
  1780. }
  1781. #ifdef _WIN32
  1782. tmpdir = getenv("USERPROFILE");
  1783. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, ".config", "mupdf", NULL))
  1784. return 1; /* OK! */
  1785. /* TEMP and TMP are user-specific on modern windows. */
  1786. tmpdir = getenv("TEMP");
  1787. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, "mupdf", NULL))
  1788. return 1; /* OK! */
  1789. tmpdir = getenv("TMP");
  1790. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, "mupdf", NULL))
  1791. return 1; /* OK! */
  1792. #else
  1793. tmpdir = getenv("XDG_CACHE_HOME");
  1794. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, "mupdf", NULL))
  1795. return 1; /* OK! */
  1796. tmpdir = getenv("HOME");
  1797. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, ".cache", "mupdf", NULL))
  1798. return 1; /* OK! */
  1799. #endif
  1800. return 0; /* Fail */
  1801. }
  1802. static int get_accelerator_filename(fz_context *ctx, char outname[], size_t len, const char *fname, int create)
  1803. {
  1804. char absname[PATH_MAX];
  1805. if (!fz_realpath(fname, absname))
  1806. return 0;
  1807. if (!convert_to_accel_path(ctx, outname, absname, len, create))
  1808. return 0;
  1809. return 1;
  1810. }
  1811. static void save_accelerator(fz_context *ctx, fz_document *doc, const char *fname)
  1812. {
  1813. char absname[PATH_MAX];
  1814. if (!doc)
  1815. return;
  1816. if (!fz_document_supports_accelerator(ctx, doc))
  1817. return;
  1818. if (!get_accelerator_filename(ctx, absname, sizeof(absname), fname, 1))
  1819. return;
  1820. fz_save_accelerator(ctx, doc, absname);
  1821. }
  1822. #ifdef MUDRAW_STANDALONE
  1823. int main(int argc, char **argv)
  1824. #else
  1825. int mudraw_main(int argc, char **argv)
  1826. #endif
  1827. {
  1828. char *password = "";
  1829. fz_document *doc = NULL;
  1830. int c;
  1831. fz_context *ctx;
  1832. trace_info trace_info = { 0, 0, 0, 0, 0, 0 };
  1833. fz_alloc_context trace_alloc_ctx = { &trace_info, trace_malloc, trace_realloc, trace_free };
  1834. fz_alloc_context *alloc_ctx = NULL;
  1835. fz_locks_context *locks = NULL;
  1836. size_t max_store = FZ_STORE_DEFAULT;
  1837. fz_var(doc);
  1838. while ((c = fz_getopt(argc, argv, "qp:o:F:R:r:w:h:fB:c:e:G:Is:A:DiW:H:S:T:t:d:U:XLvPl:y:Yz:Z:NO:am:Kb:k:")) != -1)
  1839. {
  1840. switch (c)
  1841. {
  1842. default: return usage();
  1843. case 'q': quiet = 1; break;
  1844. case 'p': password = fz_optarg; break;
  1845. case 'o': output = fz_optarg; break;
  1846. case 'F': format = fz_optarg; break;
  1847. case 'R': rotation = fz_atof(fz_optarg); break;
  1848. case 'r': resolution = fz_atof(fz_optarg); res_specified = 1; break;
  1849. case 'w': width = fz_atof(fz_optarg); break;
  1850. case 'h': height = fz_atof(fz_optarg); break;
  1851. case 'f': fit = 1; break;
  1852. case 'B': band_height = atoi(fz_optarg); break;
  1853. case 'b':
  1854. page_box = fz_box_type_from_string(fz_optarg);
  1855. if (page_box == FZ_UNKNOWN_BOX)
  1856. {
  1857. fprintf(stderr, "Invalid box type: %s\n", fz_optarg);
  1858. return 1;
  1859. }
  1860. break;
  1861. case 'c': out_cs = parse_colorspace(fz_optarg); break;
  1862. case 'e': proof_filename = fz_optarg; break;
  1863. case 'G': gamma_value = fz_atof(fz_optarg); break;
  1864. case 'I': invert++; break;
  1865. case 'W': layout_w = fz_atof(fz_optarg); break;
  1866. case 'H': layout_h = fz_atof(fz_optarg); break;
  1867. case 'S': layout_em = fz_atof(fz_optarg); break;
  1868. case 'U': layout_css = fz_optarg; break;
  1869. case 'X': layout_use_doc_css = 0; break;
  1870. case 'K': ++s_kill; break;
  1871. case 'O': spots = fz_atof(fz_optarg);
  1872. #ifndef FZ_ENABLE_SPOT_RENDERING
  1873. fprintf(stderr, "Spot rendering/Overprint/Overprint simulation not enabled in this build\n");
  1874. spots = SPOTS_NONE;
  1875. #endif
  1876. break;
  1877. case 's':
  1878. if (strchr(fz_optarg, 't')) ++showtime;
  1879. if (strchr(fz_optarg, 'm')) ++showmemory;
  1880. if (strchr(fz_optarg, 'f')) ++showfeatures;
  1881. if (strchr(fz_optarg, '5')) ++showmd5;
  1882. break;
  1883. case 'A':
  1884. {
  1885. char *sep;
  1886. alphabits_graphics = atoi(fz_optarg);
  1887. sep = strchr(fz_optarg, '/');
  1888. if (sep)
  1889. alphabits_text = atoi(sep+1);
  1890. else
  1891. alphabits_text = alphabits_graphics;
  1892. break;
  1893. }
  1894. case 'D': uselist = 0; break;
  1895. case 'l': min_line_width = fz_atof(fz_optarg); break;
  1896. case 'i': ignore_errors = 1; break;
  1897. case 'N': no_icc = 1; break;
  1898. case 'T':
  1899. #ifndef DISABLE_MUTHREADS
  1900. num_workers = atoi(fz_optarg); break;
  1901. #else
  1902. fprintf(stderr, "Threads not enabled in this build\n");
  1903. break;
  1904. #endif
  1905. case 'd':
  1906. #ifndef OCR_DISABLED
  1907. ocr_datadir = fz_optarg; break;
  1908. #else
  1909. fprintf(stderr, "OCR functionality not enabled in this build\n");
  1910. break;
  1911. #endif
  1912. case 't':
  1913. #ifndef OCR_DISABLED
  1914. ocr_language = fz_optarg; break;
  1915. #else
  1916. fprintf(stderr, "OCR functionality not enabled in this build\n");
  1917. break;
  1918. #endif
  1919. case 'm':
  1920. if (fz_optarg[0] == 's') trace_info.mem_limit = fz_atoi64(&fz_optarg[1]);
  1921. else if (fz_optarg[0] == 'a') trace_info.alloc_limit = fz_atoi64(&fz_optarg[1]);
  1922. else trace_info.mem_limit = fz_atoi64(fz_optarg);
  1923. break;
  1924. case 'L': lowmemory = 1; break;
  1925. case 'P':
  1926. #ifndef DISABLE_MUTHREADS
  1927. bgprint.active = 1; break;
  1928. #else
  1929. fprintf(stderr, "Threads not enabled in this build\n");
  1930. break;
  1931. #endif
  1932. case 'y': layer_config = fz_optarg; break;
  1933. case 'Y': layer_list = 1; break;
  1934. case 'z': layer_off[layer_off_len++] = !strcmp(fz_optarg, "all") ? -1 : fz_atoi(fz_optarg); break;
  1935. case 'Z': layer_on[layer_on_len++] = !strcmp(fz_optarg, "all") ? -1 : fz_atoi(fz_optarg); break;
  1936. case 'a': useaccel = 0; break;
  1937. case 'k':
  1938. {
  1939. const char *a;
  1940. if (fz_optarg[0] == 'a')
  1941. skew_correct = 1;
  1942. else
  1943. skew_correct = 2, skew_angle = fz_atof(fz_optarg);
  1944. a = strchr(fz_optarg, ',');
  1945. if (a != NULL)
  1946. skew_border = fz_atoi(fz_optarg+1);
  1947. break;
  1948. }
  1949. case 'v': fprintf(stderr, "mudraw version %s\n", FZ_VERSION); return 1;
  1950. }
  1951. }
  1952. if (fz_optind == argc)
  1953. return usage();
  1954. if (num_workers > 0)
  1955. {
  1956. if (uselist == 0)
  1957. {
  1958. fprintf(stderr, "cannot use multiple threads without using display list\n");
  1959. exit(1);
  1960. }
  1961. if (band_height == 0)
  1962. {
  1963. fprintf(stderr, "Using multiple threads without banding is pointless\n");
  1964. }
  1965. }
  1966. if (bgprint.active)
  1967. {
  1968. if (uselist == 0)
  1969. {
  1970. fprintf(stderr, "cannot bgprint without using display list\n");
  1971. exit(1);
  1972. }
  1973. }
  1974. #ifndef DISABLE_MUTHREADS
  1975. locks = init_mudraw_locks();
  1976. if (locks == NULL)
  1977. {
  1978. fprintf(stderr, "mutex initialisation failed\n");
  1979. exit(1);
  1980. }
  1981. #endif
  1982. if (trace_info.mem_limit || trace_info.alloc_limit || showmemory)
  1983. alloc_ctx = &trace_alloc_ctx;
  1984. if (lowmemory)
  1985. max_store = 1;
  1986. ctx = fz_new_context(alloc_ctx, locks, max_store);
  1987. if (!ctx)
  1988. {
  1989. fprintf(stderr, "cannot initialise context\n");
  1990. exit(1);
  1991. }
  1992. fz_try(ctx)
  1993. {
  1994. if (proof_filename)
  1995. {
  1996. fz_buffer *proof_buffer = fz_read_file(ctx, proof_filename);
  1997. proof_cs = fz_new_icc_colorspace(ctx, FZ_COLORSPACE_NONE, 0, NULL, proof_buffer);
  1998. fz_drop_buffer(ctx, proof_buffer);
  1999. }
  2000. fz_set_text_aa_level(ctx, alphabits_text);
  2001. fz_set_graphics_aa_level(ctx, alphabits_graphics);
  2002. fz_set_graphics_min_line_width(ctx, min_line_width);
  2003. if (no_icc)
  2004. fz_disable_icc(ctx);
  2005. else
  2006. fz_enable_icc(ctx);
  2007. #ifndef DISABLE_MUTHREADS
  2008. if (bgprint.active)
  2009. {
  2010. int fail = 0;
  2011. bgprint.ctx = fz_clone_context(ctx);
  2012. fail |= mu_create_semaphore(&bgprint.start);
  2013. fail |= mu_create_semaphore(&bgprint.stop);
  2014. fail |= mu_create_thread(&bgprint.thread, bgprint_worker, NULL);
  2015. if (fail)
  2016. {
  2017. fprintf(stderr, "bgprint startup failed\n");
  2018. exit(1);
  2019. }
  2020. }
  2021. if (num_workers > 0)
  2022. {
  2023. int i;
  2024. int fail = 0;
  2025. workers = fz_calloc(ctx, num_workers, sizeof(*workers));
  2026. for (i = 0; i < num_workers; i++)
  2027. {
  2028. workers[i].ctx = fz_clone_context(ctx);
  2029. workers[i].num = i;
  2030. fail |= mu_create_semaphore(&workers[i].start);
  2031. fail |= mu_create_semaphore(&workers[i].stop);
  2032. fail |= mu_create_thread(&workers[i].thread, worker_thread, &workers[i]);
  2033. }
  2034. if (fail)
  2035. {
  2036. fprintf(stderr, "worker startup failed\n");
  2037. exit(1);
  2038. }
  2039. }
  2040. #endif /* DISABLE_MUTHREADS */
  2041. if (layout_css)
  2042. fz_load_user_css(ctx, layout_css);
  2043. fz_set_use_document_css(ctx, layout_use_doc_css);
  2044. /* Determine output type */
  2045. if (band_height < 0)
  2046. {
  2047. fprintf(stderr, "Bandheight must be > 0\n");
  2048. exit(1);
  2049. }
  2050. output_format = OUT_PNG;
  2051. if (format)
  2052. {
  2053. int i;
  2054. for (i = 0; i < (int)nelem(suffix_table); i++)
  2055. {
  2056. if (!strcmp(format, suffix_table[i].suffix+1))
  2057. {
  2058. output_format = suffix_table[i].format;
  2059. if (spots == SPOTS_FULL && suffix_table[i].spots == 0)
  2060. {
  2061. fprintf(stderr, "Output format '%s' does not support spot rendering.\nDoing overprint simulation instead.\n", suffix_table[i].suffix+1);
  2062. spots = SPOTS_OVERPRINT_SIM;
  2063. }
  2064. break;
  2065. }
  2066. }
  2067. if (i == (int)nelem(suffix_table))
  2068. {
  2069. fprintf(stderr, "Unknown output format '%s'\n", format);
  2070. exit(1);
  2071. }
  2072. }
  2073. else if (output)
  2074. {
  2075. char *suffix = output;
  2076. int i;
  2077. for (i = 0; i < (int)nelem(suffix_table); i++)
  2078. {
  2079. char *s = strstr(suffix, suffix_table[i].suffix);
  2080. if (s != NULL)
  2081. {
  2082. suffix = s+strlen(suffix_table[i].suffix);
  2083. output_format = suffix_table[i].format;
  2084. if (spots == SPOTS_FULL && suffix_table[i].spots == 0)
  2085. {
  2086. fprintf(stderr, "Output format '%s' does not support spot rendering.\nDoing overprint simulation instead.\n", suffix_table[i].suffix+1);
  2087. spots = SPOTS_OVERPRINT_SIM;
  2088. }
  2089. i = -1;
  2090. }
  2091. }
  2092. }
  2093. if (band_height)
  2094. {
  2095. if (output_format != OUT_PAM &&
  2096. output_format != OUT_PGM &&
  2097. output_format != OUT_PPM &&
  2098. output_format != OUT_PNM &&
  2099. output_format != OUT_PNG &&
  2100. output_format != OUT_PBM &&
  2101. output_format != OUT_PKM &&
  2102. output_format != OUT_PCL &&
  2103. output_format != OUT_PCLM &&
  2104. output_format != OUT_PS &&
  2105. output_format != OUT_PSD &&
  2106. output_format != OUT_OCR_PDF)
  2107. {
  2108. fprintf(stderr, "Banded operation only possible with PxM, PCL, PCLM, PDFOCR, PS, PSD, and PNG outputs\n");
  2109. exit(1);
  2110. }
  2111. if (showmd5)
  2112. {
  2113. fprintf(stderr, "Banded operation not compatible with MD5\n");
  2114. exit(1);
  2115. }
  2116. }
  2117. {
  2118. int i, j;
  2119. for (i = 0; i < (int)nelem(format_cs_table); i++)
  2120. {
  2121. if (format_cs_table[i].format == output_format)
  2122. {
  2123. if (out_cs == CS_UNSET)
  2124. out_cs = format_cs_table[i].default_cs;
  2125. for (j = 0; j < (int)nelem(format_cs_table[i].permitted_cs); j++)
  2126. {
  2127. if (format_cs_table[i].permitted_cs[j] == out_cs)
  2128. break;
  2129. }
  2130. if (j == (int)nelem(format_cs_table[i].permitted_cs))
  2131. {
  2132. fprintf(stderr, "Unsupported colorspace for this format\n");
  2133. exit(1);
  2134. }
  2135. }
  2136. }
  2137. }
  2138. alpha = 1;
  2139. switch (out_cs)
  2140. {
  2141. case CS_MONO:
  2142. case CS_GRAY:
  2143. case CS_GRAY_ALPHA:
  2144. colorspace = fz_device_gray(ctx);
  2145. alpha = (out_cs == CS_GRAY_ALPHA);
  2146. break;
  2147. case CS_RGB:
  2148. case CS_RGB_ALPHA:
  2149. colorspace = fz_device_rgb(ctx);
  2150. alpha = (out_cs == CS_RGB_ALPHA);
  2151. break;
  2152. case CS_CMYK:
  2153. case CS_CMYK_ALPHA:
  2154. colorspace = fz_device_cmyk(ctx);
  2155. alpha = (out_cs == CS_CMYK_ALPHA);
  2156. break;
  2157. case CS_ICC:
  2158. fz_try(ctx)
  2159. {
  2160. fz_buffer *icc_buffer = fz_read_file(ctx, icc_filename);
  2161. colorspace = fz_new_icc_colorspace(ctx, FZ_COLORSPACE_NONE, 0, NULL, icc_buffer);
  2162. fz_drop_buffer(ctx, icc_buffer);
  2163. }
  2164. fz_catch(ctx)
  2165. {
  2166. fprintf(stderr, "Invalid ICC destination color space\n");
  2167. exit(1);
  2168. }
  2169. if (colorspace == NULL)
  2170. {
  2171. fprintf(stderr, "Invalid ICC destination color space\n");
  2172. exit(1);
  2173. }
  2174. alpha = 0;
  2175. break;
  2176. default:
  2177. fprintf(stderr, "Unknown colorspace!\n");
  2178. exit(1);
  2179. break;
  2180. }
  2181. if (out_cs != CS_ICC)
  2182. colorspace = fz_keep_colorspace(ctx, colorspace);
  2183. else
  2184. {
  2185. int i, j, okay;
  2186. /* Check to make sure this icc profile is ok with the output format */
  2187. okay = 0;
  2188. for (i = 0; i < (int)nelem(format_cs_table); i++)
  2189. {
  2190. if (format_cs_table[i].format == output_format)
  2191. {
  2192. for (j = 0; j < (int)nelem(format_cs_table[i].permitted_cs); j++)
  2193. {
  2194. switch (format_cs_table[i].permitted_cs[j])
  2195. {
  2196. case CS_MONO:
  2197. case CS_GRAY:
  2198. case CS_GRAY_ALPHA:
  2199. if (fz_colorspace_is_gray(ctx, colorspace))
  2200. okay = 1;
  2201. break;
  2202. case CS_RGB:
  2203. case CS_RGB_ALPHA:
  2204. if (fz_colorspace_is_rgb(ctx, colorspace))
  2205. okay = 1;
  2206. break;
  2207. case CS_CMYK:
  2208. case CS_CMYK_ALPHA:
  2209. if (fz_colorspace_is_cmyk(ctx, colorspace))
  2210. okay = 1;
  2211. break;
  2212. }
  2213. }
  2214. }
  2215. }
  2216. if (!okay)
  2217. {
  2218. fprintf(stderr, "ICC profile uses a colorspace that cannot be used for this format\n");
  2219. exit(1);
  2220. }
  2221. }
  2222. #if FZ_ENABLE_PDF
  2223. if (output_format == OUT_PDF)
  2224. {
  2225. pdfout = pdf_create_document(ctx);
  2226. }
  2227. else
  2228. #endif
  2229. if (output_format == OUT_SVG)
  2230. {
  2231. /* SVG files are always opened for each page. Do not open "output". */
  2232. }
  2233. else if (output && (output[0] != '-' || output[1] != 0) && *output != 0)
  2234. {
  2235. if (has_percent_d(output))
  2236. output_file_per_page = 1;
  2237. else
  2238. out = fz_new_output_with_path(ctx, output, 0);
  2239. }
  2240. else
  2241. {
  2242. quiet = 1; /* automatically be quiet if printing to stdout */
  2243. #ifdef _WIN32
  2244. /* Windows specific code to make stdout binary. */
  2245. if (output_format != OUT_TEXT &&
  2246. output_format != OUT_STEXT_XML &&
  2247. output_format != OUT_STEXT_JSON &&
  2248. output_format != OUT_HTML &&
  2249. output_format != OUT_XHTML &&
  2250. output_format != OUT_TRACE &&
  2251. output_format != OUT_OCR_TRACE &&
  2252. output_format != OUT_BBOX &&
  2253. output_format != OUT_OCR_TEXT &&
  2254. output_format != OUT_OCR_STEXT_XML &&
  2255. output_format != OUT_OCR_STEXT_JSON &&
  2256. output_format != OUT_OCR_HTML &&
  2257. output_format != OUT_OCR_XHTML &&
  2258. output_format != OUT_XMLTEXT)
  2259. {
  2260. (void)setmode(fileno(stdout), O_BINARY);
  2261. }
  2262. #endif
  2263. out = fz_stdout(ctx);
  2264. }
  2265. filename = argv[fz_optind];
  2266. timing.count = 0;
  2267. timing.total = 0;
  2268. timing.min = 1 << 30;
  2269. timing.max = 0;
  2270. timing.mininterp = 1 << 30;
  2271. timing.maxinterp = 0;
  2272. timing.minpage = 0;
  2273. timing.maxpage = 0;
  2274. timing.minfilename = "";
  2275. timing.maxfilename = "";
  2276. timing.layout = 0;
  2277. timing.minlayout = 1 << 30;
  2278. timing.maxlayout = 0;
  2279. timing.minlayoutfilename = "";
  2280. timing.maxlayoutfilename = "";
  2281. if (showtime && bgprint.active)
  2282. timing.total = gettime();
  2283. fz_try(ctx)
  2284. {
  2285. if (!output_file_per_page)
  2286. file_level_headers(ctx);
  2287. fz_register_document_handlers(ctx);
  2288. #ifdef HAVE_SMARTOFFICE
  2289. {
  2290. void *cfg = so_doc_handler_enable(ctx, "en-gb", NULL, 1);
  2291. so_doc_handler_configure(ctx, cfg, SO_DOC_HANDLER_MODE, SO_DOC_HANDLER_MODE_HTML);
  2292. }
  2293. #endif
  2294. while (fz_optind < argc)
  2295. {
  2296. char accelpath[PATH_MAX];
  2297. char *accel = NULL;
  2298. time_t atime;
  2299. time_t dtime;
  2300. int layouttime;
  2301. fz_try(ctx)
  2302. {
  2303. filename = argv[fz_optind++];
  2304. files++;
  2305. if (!useaccel)
  2306. accel = NULL;
  2307. /* If there was an accelerator to load, what would it be called? */
  2308. else if (get_accelerator_filename(ctx, accelpath, sizeof(accelpath), filename, 0))
  2309. {
  2310. /* Check whether that file exists, and isn't older than
  2311. * the document. */
  2312. atime = fz_stat_mtime(accelpath);
  2313. dtime = fz_stat_mtime(filename);
  2314. if (atime == 0)
  2315. {
  2316. /* No accelerator */
  2317. }
  2318. else if (atime > dtime)
  2319. accel = accelpath;
  2320. else
  2321. {
  2322. /* Accelerator data is out of date */
  2323. #ifdef _WIN32
  2324. fz_remove_utf8(accelpath);
  2325. #else
  2326. remove(accelpath);
  2327. #endif
  2328. accel = NULL; /* In case we have jumped up from below */
  2329. }
  2330. }
  2331. doc = fz_open_accelerated_document(ctx, filename, accel);
  2332. #ifdef CLUSTER
  2333. /* Load and then drop the outline if we're running under the cluster.
  2334. * This allows our outline handling to be tested automatically. */
  2335. fz_try(ctx)
  2336. fz_drop_outline(ctx, fz_load_outline(ctx, doc));
  2337. fz_catch(ctx)
  2338. {
  2339. /* Drop any error */
  2340. fz_report_error(ctx);
  2341. }
  2342. #endif
  2343. if (fz_needs_password(ctx, doc))
  2344. {
  2345. if (!fz_authenticate_password(ctx, doc, password))
  2346. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot authenticate password: %s", filename);
  2347. }
  2348. #ifdef CLUSTER
  2349. /* Load and then drop the outline if we're running under the cluster.
  2350. * This allows our outline handling to be tested automatically. */
  2351. fz_try(ctx)
  2352. fz_drop_outline(ctx, fz_load_outline(ctx, doc));
  2353. fz_catch(ctx)
  2354. {
  2355. /* Drop any error */
  2356. fz_report_error(ctx);
  2357. }
  2358. #endif
  2359. /* Once document is open check for output intent colorspace */
  2360. oi = fz_document_output_intent(ctx, doc);
  2361. if (oi)
  2362. {
  2363. /* See if we had explicitly set a profile to render */
  2364. if (out_cs != CS_ICC)
  2365. {
  2366. /* In this case, we want to render to the output intent
  2367. * color space if the number of channels is the same */
  2368. if (fz_colorspace_n(ctx, oi) == fz_colorspace_n(ctx, colorspace))
  2369. {
  2370. fz_drop_colorspace(ctx, colorspace);
  2371. colorspace = fz_keep_colorspace(ctx, oi);
  2372. }
  2373. }
  2374. }
  2375. layouttime = gettime();
  2376. fz_layout_document(ctx, doc, layout_w, layout_h, layout_em);
  2377. (void) fz_count_pages(ctx, doc);
  2378. layouttime = gettime() - layouttime;
  2379. timing.layout += layouttime;
  2380. if (layouttime < timing.minlayout)
  2381. {
  2382. timing.minlayout = layouttime;
  2383. timing.minlayoutfilename = filename;
  2384. }
  2385. if (layouttime > timing.maxlayout)
  2386. {
  2387. timing.maxlayout = layouttime;
  2388. timing.maxlayoutfilename = filename;
  2389. }
  2390. if (layer_config)
  2391. apply_layer_config(ctx, doc, layer_config);
  2392. if (layer_on_len > 0 || layer_off_len > 0)
  2393. toggle_layers(ctx, doc);
  2394. if (layer_list)
  2395. list_layers(ctx, doc);
  2396. if (fz_optind == argc || !fz_is_page_range(ctx, argv[fz_optind]))
  2397. drawrange(ctx, doc, "1-N");
  2398. if (fz_optind < argc && fz_is_page_range(ctx, argv[fz_optind]))
  2399. drawrange(ctx, doc, argv[fz_optind++]);
  2400. bgprint_flush();
  2401. if (bgprint.error)
  2402. fz_throw(ctx, FZ_ERROR_GENERIC, "failed to parse page");
  2403. if (useaccel)
  2404. save_accelerator(ctx, doc, filename);
  2405. }
  2406. fz_always(ctx)
  2407. {
  2408. fz_drop_document(ctx, doc);
  2409. doc = NULL;
  2410. }
  2411. fz_catch(ctx)
  2412. {
  2413. if (!ignore_errors)
  2414. fz_rethrow(ctx);
  2415. bgprint_flush();
  2416. fz_report_error(ctx);
  2417. fz_warn(ctx, "ignoring error in '%s'", filename);
  2418. }
  2419. }
  2420. }
  2421. fz_catch(ctx)
  2422. {
  2423. bgprint_flush();
  2424. fz_drop_document(ctx, doc);
  2425. fz_report_error(ctx);
  2426. fz_log_error_printf(ctx, "cannot draw '%s'", filename);
  2427. errored = 1;
  2428. }
  2429. if (!output_file_per_page)
  2430. file_level_trailers(ctx);
  2431. #if FZ_ENABLE_PDF
  2432. if (output_format == OUT_PDF)
  2433. {
  2434. if (!output)
  2435. output = "out.pdf";
  2436. pdf_save_document(ctx, pdfout, output, NULL);
  2437. pdf_drop_document(ctx, pdfout);
  2438. }
  2439. else
  2440. #endif
  2441. {
  2442. fz_close_output(ctx, out);
  2443. fz_drop_output(ctx, out);
  2444. out = NULL;
  2445. }
  2446. if (showtime && timing.count > 0)
  2447. {
  2448. if (bgprint.active)
  2449. timing.total = gettime() - timing.total;
  2450. if (files == 1)
  2451. {
  2452. fprintf(stderr, "total %dms (%dms layout) / %d pages for an average of %dms\n",
  2453. timing.total, timing.layout, timing.count, timing.total / timing.count);
  2454. if (bgprint.active)
  2455. {
  2456. fprintf(stderr, "fastest page %d: %dms (interpretation) %dms (rendering) %dms(total)\n",
  2457. timing.minpage, timing.mininterp, timing.min - timing.mininterp, timing.min);
  2458. fprintf(stderr, "slowest page %d: %dms (interpretation) %dms (rendering) %dms(total)\n",
  2459. timing.maxpage, timing.maxinterp, timing.max - timing.maxinterp, timing.max);
  2460. }
  2461. else
  2462. {
  2463. fprintf(stderr, "fastest page %d: %dms\n", timing.minpage, timing.min);
  2464. fprintf(stderr, "slowest page %d: %dms\n", timing.maxpage, timing.max);
  2465. }
  2466. }
  2467. else
  2468. {
  2469. fprintf(stderr, "total %dms (%dms layout) / %d pages for an average of %dms in %d files\n",
  2470. timing.total, timing.layout, timing.count, timing.total / timing.count, files);
  2471. fprintf(stderr, "fastest layout: %dms (%s)\n", timing.minlayout, timing.minlayoutfilename);
  2472. fprintf(stderr, "slowest layout: %dms (%s)\n", timing.maxlayout, timing.maxlayoutfilename);
  2473. fprintf(stderr, "fastest page %d: %dms (%s)\n", timing.minpage, timing.min, timing.minfilename);
  2474. fprintf(stderr, "slowest page %d: %dms (%s)\n", timing.maxpage, timing.max, timing.maxfilename);
  2475. }
  2476. }
  2477. #ifndef DISABLE_MUTHREADS
  2478. if (num_workers > 0)
  2479. {
  2480. int i;
  2481. DEBUG_THREADS(("Asking workers to shutdown, then destroy their resources\n"));
  2482. for (i = 0; i < num_workers; i++)
  2483. {
  2484. workers[i].band = -1;
  2485. mu_trigger_semaphore(&workers[i].start);
  2486. mu_wait_semaphore(&workers[i].stop);
  2487. mu_destroy_semaphore(&workers[i].start);
  2488. mu_destroy_semaphore(&workers[i].stop);
  2489. mu_destroy_thread(&workers[i].thread);
  2490. fz_drop_context(workers[i].ctx);
  2491. }
  2492. fz_free(ctx, workers);
  2493. }
  2494. if (bgprint.active)
  2495. {
  2496. bgprint.pagenum = -1;
  2497. mu_trigger_semaphore(&bgprint.start);
  2498. mu_wait_semaphore(&bgprint.stop);
  2499. mu_destroy_semaphore(&bgprint.start);
  2500. mu_destroy_semaphore(&bgprint.stop);
  2501. mu_destroy_thread(&bgprint.thread);
  2502. fz_drop_context(bgprint.ctx);
  2503. }
  2504. #endif /* DISABLE_MUTHREADS */
  2505. }
  2506. fz_always(ctx)
  2507. {
  2508. fz_drop_colorspace(ctx, colorspace);
  2509. fz_drop_colorspace(ctx, proof_cs);
  2510. }
  2511. fz_catch(ctx)
  2512. {
  2513. fz_report_error(ctx);
  2514. if (!errored) {
  2515. fprintf(stderr, "Rendering failed\n");
  2516. errored = 1;
  2517. }
  2518. }
  2519. fz_drop_context(ctx);
  2520. #ifndef DISABLE_MUTHREADS
  2521. fin_mudraw_locks();
  2522. #endif /* DISABLE_MUTHREADS */
  2523. if (trace_info.mem_limit || trace_info.alloc_limit || showmemory)
  2524. {
  2525. char buf[200];
  2526. fz_snprintf(buf, sizeof buf, "Memory use total=%zu peak=%zu current=%zu\nAllocations total=%zu\n", trace_info.total, trace_info.peak, trace_info.current, trace_info.allocs);
  2527. fprintf(stderr, "%s", buf);
  2528. }
  2529. return (errored != 0);
  2530. }