gxclrast.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529
  1. /* Copyright (C) 1998, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /*$Id: gxclrast.c,v 1.33 2005/03/14 18:08:36 dan Exp $ */
  14. /* Command list interpreter/rasterizer */
  15. #include "memory_.h"
  16. #include "gx.h"
  17. #include "gp.h" /* for gp_fmode_rb */
  18. #include "gpcheck.h"
  19. #include "gserrors.h"
  20. #include "gscdefs.h" /* for image type table */
  21. #include "gsbitops.h"
  22. #include "gsparams.h"
  23. #include "gsstate.h" /* (should only be imager state) */
  24. #include "gxdcolor.h"
  25. #include "gxdevice.h"
  26. #include "gscoord.h" /* requires gsmatrix.h */
  27. #include "gsdevice.h" /* for gs_deviceinitialmatrix */
  28. #include "gsiparm4.h"
  29. #include "gxdevmem.h" /* must precede gxcldev.h */
  30. #include "gxcldev.h"
  31. #include "gxclpath.h"
  32. #include "gxcmap.h"
  33. #include "gxcolor2.h"
  34. #include "gxcspace.h" /* for gs_color_space_type */
  35. #include "gxdhtres.h"
  36. #include "gxgetbit.h"
  37. #include "gxpaint.h" /* for gx_fill/stroke_params */
  38. #include "gxhttile.h"
  39. #include "gxiparam.h"
  40. #include "gzpath.h"
  41. #include "gzcpath.h"
  42. #include "gzacpath.h"
  43. #include "stream.h"
  44. #include "strimpl.h"
  45. #include "gxcomp.h"
  46. #include "gsserial.h"
  47. #include "gxdhtserial.h"
  48. #include "gzht.h"
  49. extern_gx_device_halftone_list();
  50. extern_gx_image_type_table();
  51. /* We need color space types for constructing temporary color spaces. */
  52. extern const gs_color_space_type gs_color_space_type_Indexed;
  53. /* Print a bitmap for tracing */
  54. #ifdef DEBUG
  55. private void
  56. cmd_print_bits(const byte * data, int width, int height, int raster)
  57. {
  58. int i, j;
  59. dlprintf3("[L]width=%d, height=%d, raster=%d\n",
  60. width, height, raster);
  61. for (i = 0; i < height; i++) {
  62. const byte *row = data + i * raster;
  63. dlprintf("[L]");
  64. for (j = 0; j < raster; j++)
  65. dprintf1(" %02x", row[j]);
  66. dputc('\n');
  67. }
  68. }
  69. #else
  70. # define cmd_print_bits(data, width, height, raster) DO_NOTHING
  71. #endif
  72. /* Get a variable-length integer operand. */
  73. #define cmd_getw(var, p)\
  74. BEGIN\
  75. if ( *p < 0x80 ) var = *p++;\
  76. else { const byte *_cbp; var = cmd_get_w(p, &_cbp); p = _cbp; }\
  77. END
  78. private long
  79. cmd_get_w(const byte * p, const byte ** rp)
  80. {
  81. long val = *p++ & 0x7f;
  82. int shift = 7;
  83. for (; val += (long)(*p & 0x7f) << shift, *p++ > 0x7f; shift += 7);
  84. *rp = p;
  85. return val;
  86. }
  87. /*
  88. * Define the structure for keeping track of the command reading buffer.
  89. *
  90. * The ptr member is only used for passing the current pointer to, and
  91. * receiving an updated pointer from, commands implemented as separate
  92. * procedures: normally it is kept in a register.
  93. */
  94. typedef struct command_buf_s {
  95. byte *data; /* actual buffer, guaranteed aligned */
  96. uint size;
  97. const byte *ptr; /* next byte to be read (see above) */
  98. const byte *limit; /* refill warning point */
  99. const byte *end; /* byte just beyond valid data */
  100. stream *s; /* for refilling buffer */
  101. int end_status;
  102. } command_buf_t;
  103. /* Set the end of a command buffer. */
  104. private void
  105. set_cb_end(command_buf_t *pcb, const byte *end)
  106. {
  107. pcb->end = end;
  108. pcb->limit = pcb->data + (pcb->size - cmd_largest_size + 1);
  109. if ( pcb->limit > pcb->end )
  110. pcb->limit = pcb->end;
  111. }
  112. /* Read more data into a command buffer. */
  113. private const byte *
  114. top_up_cbuf(command_buf_t *pcb, const byte *cbp)
  115. {
  116. uint nread;
  117. byte *cb_top = pcb->data + (pcb->end - cbp);
  118. memmove(pcb->data, cbp, pcb->end - cbp);
  119. nread = pcb->end - cb_top;
  120. pcb->end_status = sgets(pcb->s, cb_top, nread, &nread);
  121. if ( nread == 0 ) {
  122. /* No data for this band at all. */
  123. *cb_top = cmd_opv_end_run;
  124. nread = 1;
  125. }
  126. set_cb_end(pcb, cb_top + nread);
  127. process_interrupts(pcb->s->memory);
  128. return pcb->data;
  129. }
  130. /* Read data from the command buffer and stream. */
  131. private const byte *
  132. cmd_read_data(command_buf_t *pcb, byte *ptr, uint rsize, const byte *cbp)
  133. {
  134. if (pcb->end - cbp >= rsize) {
  135. memcpy(ptr, cbp, rsize);
  136. return cbp + rsize;
  137. } else {
  138. uint cleft = pcb->end - cbp;
  139. uint rleft = rsize - cleft;
  140. memcpy(ptr, cbp, cleft);
  141. sgets(pcb->s, ptr + cleft, rleft, &rleft);
  142. return pcb->end;
  143. }
  144. }
  145. #define cmd_read(ptr, rsize, cbp)\
  146. cbp = cmd_read_data(&cbuf, ptr, rsize, cbp)
  147. /* Read a fixed-size value from the command buffer. */
  148. inline private const byte *
  149. cmd_copy_value(void *pvar, int var_size, const byte *cbp)
  150. {
  151. memcpy(pvar, cbp, var_size);
  152. return cbp + var_size;
  153. }
  154. #define cmd_get_value(var, cbp)\
  155. cbp = cmd_copy_value(&var, sizeof(var), cbp)
  156. /*
  157. * Define a buffer structure to hold a serialized halftone. This is
  158. * used only if the serialized halftone is too large to fit into
  159. * the command buffer.
  160. */
  161. typedef struct ht_buff_s {
  162. uint ht_size, read_size;
  163. byte * pcurr;
  164. byte * pbuff;
  165. } ht_buff_t;
  166. /*
  167. * Render one band to a specified target device. Note that if
  168. * action == setup, target may be 0.
  169. */
  170. private int read_set_tile_size(command_buf_t *pcb, tile_slot *bits);
  171. private int read_set_bits(command_buf_t *pcb, tile_slot *bits,
  172. int compress, gx_clist_state *pcls,
  173. gx_strip_bitmap *tile, tile_slot **pslot,
  174. gx_device_clist_reader *cdev, gs_memory_t *mem);
  175. private int read_set_misc2(command_buf_t *pcb, gs_imager_state *pis,
  176. segment_notes *pnotes);
  177. private int read_set_color_space(command_buf_t *pcb, gs_imager_state *pis,
  178. const gs_color_space **ppcs,
  179. gs_color_space *pcolor_space,
  180. gs_memory_t *mem);
  181. private int read_begin_image(command_buf_t *pcb, gs_image_common_t *pic,
  182. const gs_color_space *pcs);
  183. private int read_put_params(command_buf_t *pcb, gs_imager_state *pis,
  184. gx_device_clist_reader *cdev,
  185. gs_memory_t *mem);
  186. private int read_create_compositor(command_buf_t *pcb, gs_imager_state *pis,
  187. gx_device_clist_reader *cdev,
  188. gs_memory_t *mem, gx_device ** ptarget);
  189. private int read_alloc_ht_buff(ht_buff_t *, uint, gs_memory_t *);
  190. private int read_ht_segment(ht_buff_t *, command_buf_t *, gs_imager_state *,
  191. gx_device *, gs_memory_t *);
  192. private const byte *cmd_read_rect(int, gx_cmd_rect *, const byte *);
  193. private const byte *cmd_read_matrix(gs_matrix *, const byte *);
  194. private const byte *cmd_read_short_bits(command_buf_t *pcb, byte *data,
  195. int width_bytes, int height,
  196. uint raster, const byte *cbp);
  197. private int cmd_select_map(cmd_map_index, cmd_map_contents,
  198. gs_imager_state *, int **,
  199. frac **, uint *, gs_memory_t *);
  200. private int cmd_create_dev_ht(gx_device_halftone **, gs_memory_t *);
  201. private int cmd_resize_halftone(gx_device_halftone **, uint,
  202. gs_memory_t *);
  203. private int clist_decode_segment(gx_path *, int, fixed[6],
  204. gs_fixed_point *, int, int,
  205. segment_notes);
  206. private int clist_do_polyfill(gx_device *, gx_path *,
  207. const gx_drawing_color *,
  208. gs_logical_operation_t);
  209. int
  210. clist_playback_band(clist_playback_action playback_action,
  211. gx_device_clist_reader *cdev, stream *s,
  212. gx_device *target, int x0, int y0, gs_memory_t * mem)
  213. {
  214. /* cbuf must be maximally aligned, but still be a byte *. */
  215. typedef union { void *p; double d; long l; } aligner_t;
  216. aligner_t cbuf_storage[cbuf_size / sizeof(aligner_t) + 1];
  217. command_buf_t cbuf;
  218. /* data_bits is for short copy_* bits and copy_* compressed, */
  219. /* must be aligned */
  220. #define data_bits_size cbuf_size
  221. byte *data_bits = 0;
  222. register const byte *cbp;
  223. int dev_depth; /* May vary due to compositing devices */
  224. int dev_depth_bytes;
  225. int odd_delta_shift;
  226. int num_zero_bytes;
  227. gx_device *tdev;
  228. gx_clist_state state;
  229. gx_color_index *set_colors;
  230. tile_slot *state_slot;
  231. gx_strip_bitmap state_tile; /* parameters for reading tiles */
  232. tile_slot tile_bits; /* parameters of current tile */
  233. gs_int_point tile_phase, color_phase;
  234. gx_path path;
  235. bool in_path;
  236. gs_fixed_point ppos;
  237. gx_clip_path clip_path;
  238. bool use_clip;
  239. gx_clip_path *pcpath;
  240. gx_device_cpath_accum clip_accum;
  241. gs_fixed_rect target_box;
  242. struct _cas {
  243. bool lop_enabled;
  244. gs_fixed_point fill_adjust;
  245. gx_device_color dcolor;
  246. } clip_save;
  247. bool in_clip = false;
  248. gs_imager_state imager_state;
  249. gx_device_color dev_color;
  250. float dash_pattern[cmd_max_dash];
  251. gx_fill_params fill_params;
  252. gx_stroke_params stroke_params;
  253. gs_halftone_type halftone_type;
  254. union im_ {
  255. gs_image_common_t c;
  256. gs_data_image_t d;
  257. gs_image1_t i1;
  258. gs_image4_t i4;
  259. } image;
  260. gs_int_rect image_rect;
  261. gs_color_space color_space; /* only used for indexed spaces */
  262. const gs_color_space *pcs;
  263. gs_color_space cs;
  264. gx_image_enum_common_t *image_info;
  265. gx_image_plane_t planes[32];
  266. uint data_height;
  267. uint data_size;
  268. byte *data_on_heap;
  269. fixed vs[6];
  270. segment_notes notes;
  271. int data_x;
  272. int code = 0;
  273. ht_buff_t ht_buff;
  274. gx_device *const orig_target = target;
  275. cbuf.data = (byte *)cbuf_storage;
  276. cbuf.size = cbuf_size;
  277. cbuf.s = s;
  278. cbuf.end_status = 0;
  279. set_cb_end(&cbuf, cbuf.data + cbuf.size);
  280. cbp = cbuf.end;
  281. memset(&ht_buff, 0, sizeof(ht_buff));
  282. in: /* Initialize for a new page. */
  283. tdev = target;
  284. set_colors = state.colors;
  285. use_clip = false;
  286. pcpath = NULL;
  287. notes = sn_none;
  288. data_x = 0;
  289. {
  290. static const gx_clist_state cls_initial = { cls_initial_values };
  291. state = cls_initial;
  292. }
  293. state_tile.id = gx_no_bitmap_id;
  294. state_tile.shift = state_tile.rep_shift = 0;
  295. tile_phase.x = color_phase.x = x0;
  296. tile_phase.y = color_phase.y = y0;
  297. gx_path_init_local(&path, mem);
  298. in_path = false;
  299. /*
  300. * Initialize the clipping region to the full page.
  301. * (Since we also initialize use_clip to false, this is arbitrary.)
  302. */
  303. {
  304. gs_fixed_rect cbox;
  305. gx_cpath_init_local(&clip_path, mem);
  306. cbox.p.x = 0;
  307. cbox.p.y = 0;
  308. cbox.q.x = cdev->width;
  309. cbox.q.y = cdev->height;
  310. gx_cpath_from_rectangle(&clip_path, &cbox);
  311. }
  312. if (target != 0)
  313. (*dev_proc(target, get_clipping_box))(target, &target_box);
  314. imager_state = clist_imager_state_initial;
  315. code = gs_imager_state_initialize(&imager_state, mem);
  316. if (code < 0)
  317. goto out;
  318. imager_state.line_params.dash.pattern = dash_pattern;
  319. if (tdev != 0)
  320. gx_set_cmap_procs(&imager_state, tdev);
  321. gx_imager_setscreenphase(&imager_state, -x0, -y0, gs_color_select_all);
  322. halftone_type = ht_type_none;
  323. fill_params.fill_zero_width = false;
  324. gs_cspace_init_DeviceGray(mem, &cs);
  325. pcs = &cs;
  326. color_unset(&dev_color);
  327. color_space.params.indexed.use_proc = 0;
  328. color_space.params.indexed.lookup.table.size = 0;
  329. data_bits = gs_alloc_bytes(mem, data_bits_size,
  330. "clist_playback_band(data_bits)");
  331. if (data_bits == 0) {
  332. code = gs_note_error(gs_error_VMerror);
  333. goto out;
  334. }
  335. while (code >= 0) {
  336. int op;
  337. int compress;
  338. int depth = 0x7badf00d; /* Initialize against indeterminizm. */
  339. int raster = 0x7badf00d; /* Initialize against indeterminizm. */
  340. byte *source = NULL; /* Initialize against indeterminizm. */
  341. gx_color_index colors[2];
  342. gx_color_index *pcolor;
  343. gs_logical_operation_t log_op;
  344. tile_slot bits; /* parameters for reading bits */
  345. /* Make sure the buffer contains a full command. */
  346. if (cbp >= cbuf.limit) {
  347. if (cbuf.end_status < 0) { /* End of file or error. */
  348. if (cbp == cbuf.end) {
  349. code = (cbuf.end_status == EOFC ? 0 :
  350. gs_note_error(gs_error_ioerror));
  351. break;
  352. }
  353. } else {
  354. cbp = top_up_cbuf(&cbuf, cbp);
  355. }
  356. }
  357. op = *cbp++;
  358. #ifdef DEBUG
  359. if (gs_debug_c('L')) {
  360. const char *const *sub = cmd_sub_op_names[op >> 4];
  361. if (sub)
  362. dlprintf1("[L]%s:", sub[op & 0xf]);
  363. else
  364. dlprintf2("[L]%s %d:", cmd_op_names[op >> 4], op & 0xf);
  365. }
  366. #endif
  367. switch (op >> 4) {
  368. case cmd_op_misc >> 4:
  369. switch (op) {
  370. case cmd_opv_end_run:
  371. if_debug0('L', "\n");
  372. continue;
  373. case cmd_opv_set_tile_size:
  374. cbuf.ptr = cbp;
  375. code = read_set_tile_size(&cbuf, &tile_bits);
  376. cbp = cbuf.ptr;
  377. if (code < 0)
  378. goto out;
  379. continue;
  380. case cmd_opv_set_tile_phase:
  381. cmd_getw(state.tile_phase.x, cbp);
  382. cmd_getw(state.tile_phase.y, cbp);
  383. if_debug2('L', " (%d,%d)\n",
  384. state.tile_phase.x,
  385. state.tile_phase.y);
  386. goto set_phase;
  387. case cmd_opv_set_tile_bits:
  388. bits = tile_bits;
  389. compress = 0;
  390. stb:
  391. cbuf.ptr = cbp;
  392. code = read_set_bits(&cbuf, &bits, compress,
  393. &state, &state_tile, &state_slot,
  394. cdev, mem);
  395. cbp = cbuf.ptr;
  396. if (code < 0)
  397. goto out;
  398. goto stp;
  399. case cmd_opv_set_bits:
  400. compress = *cbp & 3;
  401. bits.cb_depth = *cbp++ >> 2;
  402. cmd_getw(bits.width, cbp);
  403. cmd_getw(bits.height, cbp);
  404. if_debug4('L', " compress=%d depth=%d size=(%d,%d)",
  405. compress, bits.cb_depth,
  406. bits.width, bits.height);
  407. bits.cb_raster =
  408. bitmap_raster(bits.width * bits.cb_depth);
  409. bits.x_reps = bits.y_reps = 1;
  410. bits.shift = bits.rep_shift = 0;
  411. goto stb;
  412. case cmd_opv_set_tile_color:
  413. set_colors = state.tile_colors;
  414. if_debug0('L', "\n");
  415. continue;
  416. case cmd_opv_set_misc:
  417. {
  418. uint cb = *cbp++;
  419. switch (cb >> 6) {
  420. case cmd_set_misc_lop >> 6:
  421. cmd_getw(state.lop, cbp);
  422. state.lop = (state.lop << 6) + (cb & 0x3f);
  423. if_debug1('L', " lop=0x%x\n", state.lop);
  424. if (state.lop_enabled)
  425. imager_state.log_op = state.lop;
  426. break;
  427. case cmd_set_misc_data_x >> 6:
  428. if (cb & 0x20)
  429. cmd_getw(data_x, cbp);
  430. else
  431. data_x = 0;
  432. data_x = (data_x << 5) + (cb & 0x1f);
  433. if_debug1('L', " data_x=%d\n", data_x);
  434. break;
  435. case cmd_set_misc_map >> 6:
  436. {
  437. frac *mdata;
  438. int *pcomp_num;
  439. uint count;
  440. cmd_map_contents cont =
  441. (cmd_map_contents)(cb & 0x30) >> 4;
  442. code = cmd_select_map(cb & 0xf, cont,
  443. &imager_state,
  444. &pcomp_num,
  445. &mdata, &count, mem);
  446. if (code < 0)
  447. goto out;
  448. /* Get component number if relevant */
  449. if (pcomp_num == NULL)
  450. cbp++;
  451. else {
  452. *pcomp_num = (int) *cbp++;
  453. if_debug1('L', " comp_num=%d",
  454. *pcomp_num);
  455. }
  456. if (cont == cmd_map_other) {
  457. cmd_read((byte *)mdata, count, cbp);
  458. #ifdef DEBUG
  459. if (gs_debug_c('L')) {
  460. uint i;
  461. for (i = 0; i < count / sizeof(*mdata); ++i)
  462. dprintf1(" 0x%04x", mdata[i]);
  463. dputc('\n');
  464. }
  465. } else {
  466. if_debug0('L', " none\n");
  467. #endif
  468. }
  469. }
  470. /* Recompute the effective transfer, */
  471. /* in case this was a transfer map. */
  472. gx_imager_set_effective_xfer(&imager_state);
  473. break;
  474. case cmd_set_misc_halftone >> 6: {
  475. uint num_comp;
  476. halftone_type = cb & 0x3f;
  477. cmd_getw(num_comp, cbp);
  478. if_debug2('L', " halftone type=%d num_comp=%u\n",
  479. halftone_type, num_comp);
  480. code = cmd_resize_halftone(
  481. &imager_state.dev_ht,
  482. num_comp, mem);
  483. if (code < 0)
  484. goto out;
  485. break;
  486. }
  487. default:
  488. goto bad_op;
  489. }
  490. }
  491. continue;
  492. case cmd_opv_enable_lop:
  493. state.lop_enabled = true;
  494. imager_state.log_op = state.lop;
  495. if_debug0('L', "\n");
  496. continue;
  497. case cmd_opv_disable_lop:
  498. state.lop_enabled = false;
  499. imager_state.log_op = lop_default;
  500. if_debug0('L', "\n");
  501. continue;
  502. case cmd_opv_end_page:
  503. if_debug0('L', "\n");
  504. /*
  505. * Do end-of-page cleanup, then reinitialize if
  506. * there are more pages to come.
  507. */
  508. goto out;
  509. case cmd_opv_delta_color0:
  510. pcolor = &set_colors[0];
  511. goto delta2_c;
  512. case cmd_opv_delta_color1:
  513. pcolor = &set_colors[1];
  514. delta2_c:set_colors = state.colors;
  515. /* See comments for cmd_put_color() in gxclutil.c. */
  516. {
  517. gx_color_index delta = 0;
  518. uint data;
  519. dev_depth = cdev->color_info.depth;
  520. dev_depth_bytes = (dev_depth + 7) >> 3;
  521. switch (dev_depth_bytes) {
  522. /* For cases with an even number of bytes */
  523. case 8:
  524. data = *cbp++;
  525. delta = ((gx_color_index)
  526. ((data & 0xf0) << 4) + (data & 0x0f)) << 48;
  527. case 6:
  528. data = *cbp++;
  529. delta |= ((gx_color_index)
  530. ((data & 0xf0) << 4) + (data & 0x0f)) << 32;
  531. case 4:
  532. data = *cbp++;
  533. delta |= ((gx_color_index)
  534. ((data & 0xf0) << 4) + (data & 0x0f)) << 16;
  535. case 2:
  536. data = *cbp++;
  537. delta |= ((gx_color_index)
  538. ((data & 0xf0) << 4) + (data & 0x0f));
  539. break;
  540. /* For cases with an odd number of bytes */
  541. case 7:
  542. data = *cbp++;
  543. delta = ((gx_color_index)
  544. ((data & 0xf0) << 4) + (data & 0x0f)) << 16;
  545. case 5:
  546. data = *cbp++;
  547. delta |= ((gx_color_index)
  548. ((data & 0xf0) << 4) + (data & 0x0f));
  549. case 3:
  550. data = *cbp++;
  551. odd_delta_shift = (dev_depth_bytes - 3) * 8;
  552. delta |= ((gx_color_index)
  553. ((data & 0xe0) << 3) + (data & 0x1f)) << odd_delta_shift;
  554. data = *cbp++;
  555. delta |= ((gx_color_index) ((data & 0xf8) << 2) + (data & 0x07))
  556. << (odd_delta_shift + 11);
  557. }
  558. *pcolor += delta - cmd_delta_offsets[dev_depth_bytes];
  559. }
  560. if_debug1('L', " 0x%lx\n", *pcolor);
  561. continue;
  562. case cmd_opv_set_copy_color:
  563. state.color_is_alpha = 0;
  564. if_debug0('L', "\n");
  565. continue;
  566. case cmd_opv_set_copy_alpha:
  567. state.color_is_alpha = 1;
  568. if_debug0('L', "\n");
  569. continue;
  570. default:
  571. goto bad_op;
  572. }
  573. /*NOTREACHED */
  574. case cmd_op_set_color0 >> 4:
  575. pcolor = &set_colors[0];
  576. goto set_color;
  577. case cmd_op_set_color1 >> 4:
  578. pcolor = &set_colors[1];
  579. set_color:set_colors = state.colors;
  580. /*
  581. * We have a special case for gx_no_color_index. If the low
  582. * order 4 bits are "cmd_no_color_index" then we really
  583. * have a value of gx_no_color_index. Otherwise the these
  584. * bits indicate the number of low order zero bytes in the
  585. * value. See comments for cmd_put_color() in gxclutil.c.
  586. */
  587. num_zero_bytes = op & 0x0f;
  588. if (num_zero_bytes == cmd_no_color_index)
  589. *pcolor = gx_no_color_index;
  590. else {
  591. gx_color_index color = 0;
  592. dev_depth = cdev->color_info.depth;
  593. dev_depth_bytes = (dev_depth + 7) >> 3;
  594. switch (dev_depth_bytes - num_zero_bytes) {
  595. case 8:
  596. color = (gx_color_index) * cbp++;
  597. case 7:
  598. color = (color << 8) | (gx_color_index) * cbp++;
  599. case 6:
  600. color = (color << 8) | (gx_color_index) * cbp++;
  601. case 5:
  602. color = (color << 8) | (gx_color_index) * cbp++;
  603. case 4:
  604. color = (color << 8) | (gx_color_index) * cbp++;
  605. case 3:
  606. color = (color << 8) | (gx_color_index) * cbp++;
  607. case 2:
  608. color = (color << 8) | (gx_color_index) * cbp++;
  609. case 1:
  610. color = (color << 8) | (gx_color_index) * cbp++;
  611. default:
  612. break;
  613. }
  614. color <<= num_zero_bytes * 8;
  615. *pcolor = color;
  616. }
  617. if_debug1('L', " 0x%lx\n", *pcolor);
  618. continue;
  619. case cmd_op_fill_rect >> 4:
  620. case cmd_op_tile_rect >> 4:
  621. cbp = cmd_read_rect(op, &state.rect, cbp);
  622. break;
  623. case cmd_op_fill_rect_short >> 4:
  624. case cmd_op_tile_rect_short >> 4:
  625. state.rect.x += *cbp + cmd_min_short;
  626. state.rect.width += cbp[1] + cmd_min_short;
  627. if (op & 0xf) {
  628. state.rect.height += (op & 0xf) + cmd_min_dxy_tiny;
  629. cbp += 2;
  630. } else {
  631. state.rect.y += cbp[2] + cmd_min_short;
  632. state.rect.height += cbp[3] + cmd_min_short;
  633. cbp += 4;
  634. }
  635. break;
  636. case cmd_op_fill_rect_tiny >> 4:
  637. case cmd_op_tile_rect_tiny >> 4:
  638. if (op & 8)
  639. state.rect.x += state.rect.width;
  640. else {
  641. int txy = *cbp++;
  642. state.rect.x += (txy >> 4) + cmd_min_dxy_tiny;
  643. state.rect.y += (txy & 0xf) + cmd_min_dxy_tiny;
  644. }
  645. state.rect.width += (op & 7) + cmd_min_dw_tiny;
  646. break;
  647. case cmd_op_copy_mono >> 4:
  648. depth = 1;
  649. goto copy;
  650. case cmd_op_copy_color_alpha >> 4:
  651. if (state.color_is_alpha) {
  652. if (!(op & 8))
  653. depth = *cbp++;
  654. } else
  655. depth = cdev->color_info.depth;
  656. copy:cmd_getw(state.rect.x, cbp);
  657. cmd_getw(state.rect.y, cbp);
  658. if (op & 8) { /* Use the current "tile". */
  659. #ifdef DEBUG
  660. if (state_slot->index != state.tile_index) {
  661. lprintf2("state_slot->index = %d, state.tile_index = %d!\n",
  662. state_slot->index,
  663. state.tile_index);
  664. code = gs_note_error(gs_error_ioerror);
  665. goto out;
  666. }
  667. #endif
  668. depth = state_slot->cb_depth;
  669. state.rect.width = state_slot->width;
  670. state.rect.height = state_slot->height;
  671. raster = state_slot->cb_raster;
  672. source = (byte *) (state_slot + 1);
  673. } else { /* Read width, height, bits. */
  674. /* depth was set already. */
  675. uint width_bits, width_bytes;
  676. uint bytes;
  677. cmd_getw(state.rect.width, cbp);
  678. cmd_getw(state.rect.height, cbp);
  679. width_bits = state.rect.width * depth;
  680. bytes =
  681. clist_bitmap_bytes(width_bits,
  682. state.rect.height,
  683. op & 3, &width_bytes,
  684. (uint *)&raster);
  685. /* copy_mono and copy_color/alpha */
  686. /* ensure that the bits will fit in a single buffer, */
  687. /* even after decompression if compressed. */
  688. #ifdef DEBUG
  689. if (bytes > cbuf_size) {
  690. lprintf6("bitmap size exceeds buffer! width=%d raster=%d height=%d\n file pos %ld buf pos %d/%d\n",
  691. state.rect.width, raster,
  692. state.rect.height,
  693. stell(s), (int)(cbp - cbuf.data),
  694. (int)(cbuf.end - cbuf.data));
  695. code = gs_note_error(gs_error_ioerror);
  696. goto out;
  697. }
  698. #endif
  699. if (op & 3) { /* Decompress the image data. */
  700. stream_cursor_read r;
  701. stream_cursor_write w;
  702. /* We don't know the data length a priori, */
  703. /* so to be conservative, we read */
  704. /* the uncompressed size. */
  705. uint cleft = cbuf.end - cbp;
  706. if (cleft < bytes) {
  707. uint nread = cbuf_size - cleft;
  708. memmove(cbuf.data, cbp, cleft);
  709. cbuf.end_status = sgets(s, cbuf.data + cleft, nread, &nread);
  710. set_cb_end(&cbuf, cbuf.data + cleft + nread);
  711. cbp = cbuf.data;
  712. }
  713. r.ptr = cbp - 1;
  714. r.limit = cbuf.end - 1;
  715. w.ptr = data_bits - 1;
  716. w.limit = w.ptr + data_bits_size;
  717. switch (op & 3) {
  718. case cmd_compress_rle:
  719. {
  720. stream_RLD_state sstate;
  721. clist_rld_init(&sstate);
  722. /* The process procedure can't fail. */
  723. (*s_RLD_template.process)
  724. ((stream_state *)&sstate, &r, &w, true);
  725. }
  726. break;
  727. case cmd_compress_cfe:
  728. {
  729. stream_CFD_state sstate;
  730. clist_cfd_init(&sstate,
  731. width_bytes << 3 /*state.rect.width */ ,
  732. state.rect.height, mem);
  733. /* The process procedure can't fail. */
  734. (*s_CFD_template.process)
  735. ((stream_state *)&sstate, &r, &w, true);
  736. (*s_CFD_template.release)
  737. ((stream_state *)&sstate);
  738. }
  739. break;
  740. default:
  741. goto bad_op;
  742. }
  743. cbp = r.ptr + 1;
  744. source = data_bits;
  745. } else if (state.rect.height > 1 &&
  746. width_bytes != raster
  747. ) {
  748. source = data_bits;
  749. cbp = cmd_read_short_bits(&cbuf, source, width_bytes,
  750. state.rect.height,
  751. raster, cbp);
  752. } else {
  753. cmd_read(cbuf.data, bytes, cbp);
  754. source = cbuf.data;
  755. }
  756. #ifdef DEBUG
  757. if (gs_debug_c('L')) {
  758. dprintf2(" depth=%d, data_x=%d\n",
  759. depth, data_x);
  760. cmd_print_bits(source, state.rect.width,
  761. state.rect.height, raster);
  762. }
  763. #endif
  764. }
  765. break;
  766. case cmd_op_delta_tile_index >> 4:
  767. state.tile_index += (int)(op & 0xf) - 8;
  768. goto sti;
  769. case cmd_op_set_tile_index >> 4:
  770. state.tile_index =
  771. ((op & 0xf) << 8) + *cbp++;
  772. sti:state_slot =
  773. (tile_slot *) (cdev->chunk.data +
  774. cdev->tile_table[state.tile_index].offset);
  775. if_debug2('L', " index=%u offset=%lu\n",
  776. state.tile_index,
  777. cdev->tile_table[state.tile_index].offset);
  778. state_tile.data = (byte *) (state_slot + 1);
  779. stp:state_tile.size.x = state_slot->width;
  780. state_tile.size.y = state_slot->height;
  781. state_tile.raster = state_slot->cb_raster;
  782. state_tile.rep_width = state_tile.size.x /
  783. state_slot->x_reps;
  784. state_tile.rep_height = state_tile.size.y /
  785. state_slot->y_reps;
  786. state_tile.rep_shift = state_slot->rep_shift;
  787. state_tile.shift = state_slot->shift;
  788. set_phase: /*
  789. * state.tile_phase is overloaded according to the command
  790. * to which it will apply:
  791. * For fill_path, stroke_path, fill_triangle/p'gram,
  792. * fill_mask, and (mask or CombineWithColor) images,
  793. * it is pdcolor->phase. For these operations, we
  794. * precompute the color_phase values.
  795. * For strip_tile_rectangle and strip_copy_rop,
  796. * it is the phase arguments of the call, used with
  797. * state_tile. For these operations, we precompute the
  798. * tile_phase values.
  799. *
  800. * Note that control may get here before one or both of
  801. * state_tile or dev_ht has been set.
  802. */
  803. if (state_tile.size.x)
  804. tile_phase.x =
  805. (state.tile_phase.x + x0) % state_tile.size.x;
  806. if (imager_state.dev_ht && imager_state.dev_ht->lcm_width)
  807. color_phase.x =
  808. (state.tile_phase.x + x0) %
  809. imager_state.dev_ht->lcm_width;
  810. /*
  811. * The true tile height for shifted tiles is not
  812. * size.y: see gxbitmap.h for the computation.
  813. */
  814. if (state_tile.size.y) {
  815. int full_height;
  816. if (state_tile.shift == 0)
  817. full_height = state_tile.size.y;
  818. else
  819. full_height = state_tile.rep_height *
  820. (state_tile.rep_width /
  821. igcd(state_tile.rep_shift,
  822. state_tile.rep_width));
  823. tile_phase.y =
  824. (state.tile_phase.y + y0) % full_height;
  825. }
  826. if (imager_state.dev_ht && imager_state.dev_ht->lcm_height)
  827. color_phase.y =
  828. (state.tile_phase.y + y0) %
  829. imager_state.dev_ht->lcm_height;
  830. gx_imager_setscreenphase(&imager_state,
  831. -(state.tile_phase.x + x0),
  832. -(state.tile_phase.y + y0),
  833. gs_color_select_all);
  834. continue;
  835. case cmd_op_misc2 >> 4:
  836. switch (op) {
  837. case cmd_opv_set_fill_adjust:
  838. cmd_get_value(imager_state.fill_adjust.x, cbp);
  839. cmd_get_value(imager_state.fill_adjust.y, cbp);
  840. if_debug2('L', " (%g,%g)\n",
  841. fixed2float(imager_state.fill_adjust.x),
  842. fixed2float(imager_state.fill_adjust.y));
  843. continue;
  844. case cmd_opv_set_ctm:
  845. {
  846. gs_matrix mat;
  847. cbp = cmd_read_matrix(&mat, cbp);
  848. mat.tx -= x0;
  849. mat.ty -= y0;
  850. gs_imager_setmatrix(&imager_state, &mat);
  851. if_debug6('L', " [%g %g %g %g %g %g]\n",
  852. mat.xx, mat.xy, mat.yx, mat.yy,
  853. mat.tx, mat.ty);
  854. }
  855. continue;
  856. case cmd_opv_set_misc2:
  857. cbuf.ptr = cbp;
  858. code = read_set_misc2(&cbuf, &imager_state, &notes);
  859. cbp = cbuf.ptr;
  860. if (code < 0)
  861. goto out;
  862. continue;
  863. case cmd_opv_set_dash:
  864. {
  865. int nb = *cbp++;
  866. int n = nb & 0x3f;
  867. float dot_length, offset;
  868. cmd_get_value(dot_length, cbp);
  869. cmd_get_value(offset, cbp);
  870. memcpy(dash_pattern, cbp, n * sizeof(float));
  871. gx_set_dash(&imager_state.line_params.dash,
  872. dash_pattern, n, offset,
  873. NULL);
  874. gx_set_dash_adapt(&imager_state.line_params.dash,
  875. (nb & 0x80) != 0);
  876. gx_set_dot_length(&imager_state.line_params,
  877. dot_length,
  878. (nb & 0x40) != 0);
  879. #ifdef DEBUG
  880. if (gs_debug_c('L')) {
  881. int i;
  882. dprintf4(" dot=%g(mode %d) adapt=%d offset=%g [",
  883. dot_length,
  884. (nb & 0x40) != 0,
  885. (nb & 0x80) != 0, offset);
  886. for (i = 0; i < n; ++i)
  887. dprintf1("%g ", dash_pattern[i]);
  888. dputs("]\n");
  889. }
  890. #endif
  891. cbp += n * sizeof(float);
  892. }
  893. break;
  894. case cmd_opv_enable_clip:
  895. pcpath = (use_clip ? &clip_path : NULL);
  896. if_debug0('L', "\n");
  897. break;
  898. case cmd_opv_disable_clip:
  899. pcpath = NULL;
  900. if_debug0('L', "\n");
  901. break;
  902. case cmd_opv_begin_clip:
  903. pcpath = NULL;
  904. in_clip = true;
  905. if_debug0('L', "\n");
  906. code = gx_cpath_reset(&clip_path);
  907. if (code < 0)
  908. goto out;
  909. gx_cpath_accum_begin(&clip_accum, mem);
  910. gx_cpath_accum_set_cbox(&clip_accum,
  911. &target_box);
  912. tdev = (gx_device *)&clip_accum;
  913. clip_save.lop_enabled = state.lop_enabled;
  914. clip_save.fill_adjust =
  915. imager_state.fill_adjust;
  916. clip_save.dcolor = dev_color;
  917. /* temporarily set a solid color */
  918. color_set_pure(&dev_color, (gx_color_index)1);
  919. state.lop_enabled = false;
  920. imager_state.log_op = lop_default;
  921. imager_state.fill_adjust.x =
  922. imager_state.fill_adjust.y = fixed_half;
  923. break;
  924. case cmd_opv_end_clip:
  925. if_debug0('L', "\n");
  926. gx_cpath_accum_end(&clip_accum, &clip_path);
  927. tdev = target;
  928. /*
  929. * If the entire band falls within the clip
  930. * path, no clipping is needed.
  931. */
  932. {
  933. gs_fixed_rect cbox;
  934. gx_cpath_inner_box(&clip_path, &cbox);
  935. use_clip =
  936. !(cbox.p.x <= target_box.p.x &&
  937. cbox.q.x >= target_box.q.x &&
  938. cbox.p.y <= target_box.p.y &&
  939. cbox.q.y >= target_box.q.y);
  940. }
  941. pcpath = (use_clip ? &clip_path : NULL);
  942. state.lop_enabled = clip_save.lop_enabled;
  943. imager_state.log_op =
  944. (state.lop_enabled ? state.lop :
  945. lop_default);
  946. imager_state.fill_adjust =
  947. clip_save.fill_adjust;
  948. dev_color = clip_save.dcolor;
  949. in_clip = false;
  950. break;
  951. case cmd_opv_set_color_space:
  952. cbuf.ptr = cbp;
  953. code = read_set_color_space(&cbuf, &imager_state,
  954. &pcs, &color_space, mem);
  955. cbp = cbuf.ptr;
  956. if (code < 0) {
  957. if (code == gs_error_rangecheck)
  958. goto bad_op;
  959. goto out;
  960. }
  961. break;
  962. case cmd_opv_begin_image_rect:
  963. cbuf.ptr = cbp;
  964. code = read_begin_image(&cbuf, &image.c, pcs);
  965. cbp = cbuf.ptr;
  966. if (code < 0)
  967. goto out;
  968. {
  969. uint diff;
  970. cmd_getw(image_rect.p.x, cbp);
  971. cmd_getw(image_rect.p.y, cbp);
  972. cmd_getw(diff, cbp);
  973. image_rect.q.x = image.d.Width - diff;
  974. cmd_getw(diff, cbp);
  975. image_rect.q.y = image.d.Height - diff;
  976. if_debug4('L', " rect=(%d,%d),(%d,%d)",
  977. image_rect.p.x, image_rect.p.y,
  978. image_rect.q.x, image_rect.q.y);
  979. }
  980. goto ibegin;
  981. case cmd_opv_begin_image:
  982. cbuf.ptr = cbp;
  983. code = read_begin_image(&cbuf, &image.c, pcs);
  984. cbp = cbuf.ptr;
  985. if (code < 0)
  986. goto out;
  987. image_rect.p.x = 0;
  988. image_rect.p.y = 0;
  989. image_rect.q.x = image.d.Width;
  990. image_rect.q.y = image.d.Height;
  991. if_debug2('L', " size=(%d,%d)",
  992. image.d.Width, image.d.Height);
  993. ibegin: if_debug0('L', "\n");
  994. {
  995. code = (*dev_proc(tdev, begin_typed_image))
  996. (tdev, &imager_state, NULL,
  997. (const gs_image_common_t *)&image,
  998. &image_rect, &dev_color, pcpath, mem,
  999. &image_info);
  1000. }
  1001. if (code < 0)
  1002. goto out;
  1003. break;
  1004. case cmd_opv_image_plane_data:
  1005. cmd_getw(data_height, cbp);
  1006. if (data_height == 0) {
  1007. if_debug0('L', " done image\n");
  1008. code = gx_image_end(image_info, true);
  1009. if (code < 0)
  1010. goto out;
  1011. continue;
  1012. }
  1013. {
  1014. uint flags;
  1015. int plane;
  1016. uint raster1 = 0xbaadf00d; /* Initialize against indeterminizm. */
  1017. cmd_getw(flags, cbp);
  1018. for (plane = 0;
  1019. plane < image_info->num_planes;
  1020. ++plane, flags >>= 1
  1021. ) {
  1022. if (flags & 1) {
  1023. if (cbuf.end - cbp <
  1024. 2 * cmd_max_intsize(sizeof(uint)))
  1025. cbp = top_up_cbuf(&cbuf, cbp);
  1026. cmd_getw(planes[plane].raster, cbp);
  1027. if ((raster1 = planes[plane].raster) != 0)
  1028. cmd_getw(data_x, cbp);
  1029. } else {
  1030. planes[plane].raster = raster1;
  1031. }
  1032. planes[plane].data_x = data_x;
  1033. }
  1034. }
  1035. goto idata;
  1036. case cmd_opv_image_data:
  1037. cmd_getw(data_height, cbp);
  1038. if (data_height == 0) {
  1039. if_debug0('L', " done image\n");
  1040. code = gx_image_end(image_info, true);
  1041. if (code < 0)
  1042. goto out;
  1043. continue;
  1044. }
  1045. {
  1046. uint bytes_per_plane;
  1047. int plane;
  1048. cmd_getw(bytes_per_plane, cbp);
  1049. if_debug2('L', " height=%u raster=%u\n",
  1050. data_height, bytes_per_plane);
  1051. for (plane = 0;
  1052. plane < image_info->num_planes;
  1053. ++plane
  1054. ) {
  1055. planes[plane].data_x = data_x;
  1056. planes[plane].raster = bytes_per_plane;
  1057. }
  1058. }
  1059. idata: data_size = 0;
  1060. {
  1061. int plane;
  1062. for (plane = 0; plane < image_info->num_planes;
  1063. ++plane)
  1064. data_size += planes[plane].raster;
  1065. }
  1066. data_size *= data_height;
  1067. data_on_heap = 0;
  1068. if (cbuf.end - cbp < data_size)
  1069. cbp = top_up_cbuf(&cbuf, cbp);
  1070. if (cbuf.end - cbp >= data_size) {
  1071. planes[0].data = cbp;
  1072. cbp += data_size;
  1073. } else {
  1074. uint cleft = cbuf.end - cbp;
  1075. uint rleft = data_size - cleft;
  1076. byte *rdata;
  1077. if (data_size > cbuf.end - cbuf.data) {
  1078. /* Allocate a separate buffer. */
  1079. rdata = data_on_heap =
  1080. gs_alloc_bytes(mem, data_size,
  1081. "clist image_data");
  1082. if (rdata == 0) {
  1083. code = gs_note_error(gs_error_VMerror);
  1084. goto out;
  1085. }
  1086. } else
  1087. rdata = cbuf.data;
  1088. memmove(rdata, cbp, cleft);
  1089. sgets(s, rdata + cleft, rleft,
  1090. &rleft);
  1091. planes[0].data = rdata;
  1092. cbp = cbuf.end; /* force refill */
  1093. }
  1094. {
  1095. int plane;
  1096. const byte *data = planes[0].data;
  1097. for (plane = 0;
  1098. plane < image_info->num_planes;
  1099. ++plane
  1100. ) {
  1101. if (planes[plane].raster == 0)
  1102. planes[plane].data = 0;
  1103. else {
  1104. planes[plane].data = data;
  1105. data += planes[plane].raster *
  1106. data_height;
  1107. }
  1108. }
  1109. }
  1110. #ifdef DEBUG
  1111. if (gs_debug_c('L')) {
  1112. int plane;
  1113. for (plane = 0; plane < image_info->num_planes;
  1114. ++plane)
  1115. if (planes[plane].data != 0)
  1116. cmd_print_bits(planes[plane].data,
  1117. image_rect.q.x -
  1118. image_rect.p.x,
  1119. data_height,
  1120. planes[plane].raster);
  1121. }
  1122. #endif
  1123. code = gx_image_plane_data(image_info, planes,
  1124. data_height);
  1125. if (data_on_heap)
  1126. gs_free_object(mem, data_on_heap,
  1127. "clist image_data");
  1128. data_x = 0;
  1129. if (code < 0)
  1130. goto out;
  1131. continue;
  1132. case cmd_opv_extend:
  1133. switch (*cbp++) {
  1134. case cmd_opv_ext_put_params:
  1135. cbuf.ptr = cbp;
  1136. code = read_put_params(&cbuf, &imager_state,
  1137. cdev, mem);
  1138. cbp = cbuf.ptr;
  1139. if (code > 0)
  1140. break; /* empty list */
  1141. if (code < 0)
  1142. goto out;
  1143. if (playback_action == playback_action_setup)
  1144. goto out;
  1145. break;
  1146. case cmd_opv_ext_create_compositor:
  1147. cbuf.ptr = cbp;
  1148. /*
  1149. * The screen phase may have been changed during
  1150. * the processing of masked images.
  1151. */
  1152. gx_imager_setscreenphase(&imager_state,
  1153. -x0, -y0, gs_color_select_all);
  1154. code = read_create_compositor(&cbuf, &imager_state,
  1155. cdev, mem, &target);
  1156. cbp = cbuf.ptr;
  1157. tdev = target;
  1158. if (code < 0)
  1159. goto out;
  1160. break;
  1161. case cmd_opv_ext_put_halftone:
  1162. {
  1163. uint ht_size;
  1164. enc_u_getw(ht_size, cbp);
  1165. code = read_alloc_ht_buff(&ht_buff, ht_size, mem);
  1166. if (code < 0)
  1167. goto out;
  1168. }
  1169. break;
  1170. case cmd_opv_ext_put_ht_seg:
  1171. cbuf.ptr = cbp;
  1172. code = read_ht_segment(&ht_buff, &cbuf,
  1173. &imager_state, tdev,
  1174. mem);
  1175. cbp = cbuf.ptr;
  1176. if (code < 0)
  1177. goto out;
  1178. break;
  1179. case cmd_opv_ext_put_drawing_color:
  1180. {
  1181. uint color_size;
  1182. const gx_device_color_type_t * pdct;
  1183. pdct = gx_get_dc_type_from_index(*cbp++);
  1184. if (pdct == 0) {
  1185. code = gs_note_error(gs_error_rangecheck);
  1186. goto out;
  1187. }
  1188. enc_u_getw(color_size, cbp);
  1189. if (cbp + color_size > cbuf.limit)
  1190. cbp = top_up_cbuf(&cbuf, cbp);
  1191. code = pdct->read(&dev_color, &imager_state,
  1192. &dev_color, tdev, cbp,
  1193. color_size, mem);
  1194. if (code < 0)
  1195. goto out;
  1196. cbp += color_size;
  1197. code = gx_color_load(&dev_color,
  1198. &imager_state, tdev);
  1199. if (code < 0)
  1200. goto out;
  1201. }
  1202. break;
  1203. default:
  1204. goto bad_op;
  1205. }
  1206. break;
  1207. default:
  1208. goto bad_op;
  1209. }
  1210. continue;
  1211. case cmd_op_segment >> 4:
  1212. {
  1213. int i, code;
  1214. static const byte op_num_operands[] = {
  1215. cmd_segment_op_num_operands_values
  1216. };
  1217. if (!in_path) {
  1218. ppos.x = int2fixed(state.rect.x);
  1219. ppos.y = int2fixed(state.rect.y);
  1220. if_debug2('L', " (%d,%d)", state.rect.x,
  1221. state.rect.y);
  1222. notes = sn_none;
  1223. in_path = true;
  1224. }
  1225. for (i = 0; i < op_num_operands[op & 0xf]; ++i) {
  1226. fixed v;
  1227. int b = *cbp;
  1228. switch (b >> 5) {
  1229. case 0:
  1230. case 1:
  1231. vs[i++] =
  1232. ((fixed) ((b ^ 0x20) - 0x20) << 13) +
  1233. ((int)cbp[1] << 5) + (cbp[2] >> 3);
  1234. if_debug1('L', " %g", fixed2float(vs[i - 1]));
  1235. cbp += 2;
  1236. v = (int)((*cbp & 7) ^ 4) - 4;
  1237. break;
  1238. case 2:
  1239. case 3:
  1240. v = (b ^ 0x60) - 0x20;
  1241. break;
  1242. case 4:
  1243. case 5:
  1244. /*
  1245. * Without the following cast, C's
  1246. * brain-damaged coercion rules cause the
  1247. * result to be considered unsigned, and not
  1248. * sign-extended on machines where
  1249. * sizeof(long) > sizeof(int).
  1250. */
  1251. v = (((b ^ 0xa0) - 0x20) << 8) + (int)*++cbp;
  1252. break;
  1253. case 6:
  1254. v = (b ^ 0xd0) - 0x10;
  1255. vs[i] =
  1256. ((v << 8) + cbp[1]) << (_fixed_shift - 2);
  1257. if_debug1('L', " %g", fixed2float(vs[i]));
  1258. cbp += 2;
  1259. continue;
  1260. default /*case 7 */ :
  1261. v = (int)(*++cbp ^ 0x80) - 0x80;
  1262. for (b = 0; b < sizeof(fixed) - 3; ++b)
  1263. v = (v << 8) + *++cbp;
  1264. break;
  1265. }
  1266. cbp += 3;
  1267. /* Absent the cast in the next statement, */
  1268. /* the Borland C++ 4.5 compiler incorrectly */
  1269. /* sign-extends the result of the shift. */
  1270. vs[i] = (v << 16) + (uint) (cbp[-2] << 8) + cbp[-1];
  1271. if_debug1('L', " %g", fixed2float(vs[i]));
  1272. }
  1273. if_debug0('L', "\n");
  1274. code = clist_decode_segment(&path, op, vs, &ppos,
  1275. x0, y0, notes);
  1276. if (code < 0)
  1277. goto out;
  1278. }
  1279. continue;
  1280. case cmd_op_path >> 4:
  1281. {
  1282. gx_path fpath;
  1283. gx_path * ppath = &path;
  1284. if_debug0('L', "\n");
  1285. /* if in clip, flatten path first */
  1286. if (in_clip) {
  1287. gx_path_init_local(&fpath, mem);
  1288. code = gx_path_add_flattened_accurate(&path, &fpath,
  1289. gs_currentflat_inline(&imager_state),
  1290. imager_state.accurate_curves);
  1291. if (code < 0)
  1292. goto out;
  1293. ppath = &fpath;
  1294. }
  1295. switch (op) {
  1296. case cmd_opv_fill:
  1297. fill_params.rule = gx_rule_winding_number;
  1298. goto fill;
  1299. case cmd_opv_eofill:
  1300. fill_params.rule = gx_rule_even_odd;
  1301. fill:
  1302. fill_params.adjust = imager_state.fill_adjust;
  1303. fill_params.flatness = imager_state.flatness;
  1304. fill_params.fill_zero_width =
  1305. fill_params.adjust.x != 0 ||
  1306. fill_params.adjust.y != 0;
  1307. code = gx_fill_path_only(ppath, tdev,
  1308. &imager_state,
  1309. &fill_params,
  1310. &dev_color, pcpath);
  1311. break;
  1312. case cmd_opv_stroke:
  1313. stroke_params.flatness = imager_state.flatness;
  1314. code = gx_stroke_path_only(ppath,
  1315. (gx_path *) 0, tdev,
  1316. &imager_state, &stroke_params,
  1317. &dev_color, pcpath);
  1318. break;
  1319. case cmd_opv_polyfill:
  1320. code = clist_do_polyfill(tdev, ppath, &dev_color,
  1321. imager_state.log_op);
  1322. break;
  1323. default:
  1324. goto bad_op;
  1325. }
  1326. if (ppath != &path)
  1327. gx_path_free(ppath, "clist_render_band");
  1328. }
  1329. if (in_path) { /* path might be empty! */
  1330. state.rect.x = fixed2int_var(ppos.x);
  1331. state.rect.y = fixed2int_var(ppos.y);
  1332. in_path = false;
  1333. }
  1334. gx_path_free(&path, "clist_render_band");
  1335. gx_path_init_local(&path, mem);
  1336. if (code < 0)
  1337. goto out;
  1338. continue;
  1339. default:
  1340. bad_op:lprintf5("Bad op %02x band y0 = %d file pos %ld buf pos %d/%d\n",
  1341. op, y0, stell(s), (int)(cbp - cbuf.data), (int)(cbuf.end - cbuf.data));
  1342. {
  1343. const byte *pp;
  1344. for (pp = cbuf.data; pp < cbuf.end; pp += 10) {
  1345. dlprintf1("%4d:", (int)(pp - cbuf.data));
  1346. dprintf10(" %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1347. pp[0], pp[1], pp[2], pp[3], pp[4],
  1348. pp[5], pp[6], pp[7], pp[8], pp[9]);
  1349. }
  1350. }
  1351. code = gs_note_error(gs_error_Fatal);
  1352. goto out;
  1353. }
  1354. if_debug4('L', " x=%d y=%d w=%d h=%d\n",
  1355. state.rect.x, state.rect.y, state.rect.width,
  1356. state.rect.height);
  1357. switch (op >> 4) {
  1358. case cmd_op_fill_rect >> 4:
  1359. case cmd_op_fill_rect_short >> 4:
  1360. case cmd_op_fill_rect_tiny >> 4:
  1361. if (!state.lop_enabled) {
  1362. code = (*dev_proc(tdev, fill_rectangle))
  1363. (tdev, state.rect.x - x0, state.rect.y - y0,
  1364. state.rect.width, state.rect.height,
  1365. state.colors[1]);
  1366. break;
  1367. }
  1368. source = NULL;
  1369. data_x = 0;
  1370. raster = 0;
  1371. colors[0] = colors[1] = state.colors[1];
  1372. log_op = state.lop;
  1373. pcolor = colors;
  1374. do_rop:code = (*dev_proc(tdev, strip_copy_rop))
  1375. (tdev, source, data_x, raster, gx_no_bitmap_id,
  1376. pcolor, &state_tile,
  1377. (state.tile_colors[0] == gx_no_color_index &&
  1378. state.tile_colors[1] == gx_no_color_index ?
  1379. NULL : state.tile_colors),
  1380. state.rect.x - x0, state.rect.y - y0,
  1381. state.rect.width - data_x, state.rect.height,
  1382. tile_phase.x, tile_phase.y, log_op);
  1383. data_x = 0;
  1384. break;
  1385. case cmd_op_tile_rect >> 4:
  1386. case cmd_op_tile_rect_short >> 4:
  1387. case cmd_op_tile_rect_tiny >> 4:
  1388. /* Currently we don't use lop with tile_rectangle. */
  1389. code = (*dev_proc(tdev, strip_tile_rectangle))
  1390. (tdev, &state_tile,
  1391. state.rect.x - x0, state.rect.y - y0,
  1392. state.rect.width, state.rect.height,
  1393. state.tile_colors[0], state.tile_colors[1],
  1394. tile_phase.x, tile_phase.y);
  1395. break;
  1396. case cmd_op_copy_mono >> 4:
  1397. if (state.lop_enabled) {
  1398. pcolor = state.colors;
  1399. log_op = state.lop;
  1400. goto do_rop;
  1401. }
  1402. if ((op & cmd_copy_use_tile) || pcpath != NULL) { /*
  1403. * This call of copy_mono originated as a call
  1404. * of fill_mask.
  1405. */
  1406. code = (*dev_proc(tdev, fill_mask))
  1407. (tdev, source, data_x, raster, gx_no_bitmap_id,
  1408. state.rect.x - x0, state.rect.y - y0,
  1409. state.rect.width - data_x, state.rect.height,
  1410. &dev_color, 1, imager_state.log_op, pcpath);
  1411. } else
  1412. code = (*dev_proc(tdev, copy_mono))
  1413. (tdev, source, data_x, raster, gx_no_bitmap_id,
  1414. state.rect.x - x0, state.rect.y - y0,
  1415. state.rect.width - data_x, state.rect.height,
  1416. state.colors[0], state.colors[1]);
  1417. data_x = 0;
  1418. break;
  1419. case cmd_op_copy_color_alpha >> 4:
  1420. if (state.color_is_alpha) {
  1421. /****** CAN'T DO ROP WITH ALPHA ******/
  1422. code = (*dev_proc(tdev, copy_alpha))
  1423. (tdev, source, data_x, raster, gx_no_bitmap_id,
  1424. state.rect.x - x0, state.rect.y - y0,
  1425. state.rect.width - data_x, state.rect.height,
  1426. state.colors[1], depth);
  1427. } else {
  1428. if (state.lop_enabled) {
  1429. pcolor = NULL;
  1430. log_op = state.lop;
  1431. goto do_rop;
  1432. }
  1433. code = (*dev_proc(tdev, copy_color))
  1434. (tdev, source, data_x, raster, gx_no_bitmap_id,
  1435. state.rect.x - x0, state.rect.y - y0,
  1436. state.rect.width - data_x, state.rect.height);
  1437. }
  1438. data_x = 0;
  1439. break;
  1440. default: /* can't happen */
  1441. goto bad_op;
  1442. }
  1443. }
  1444. /* Clean up before we exit. */
  1445. out:
  1446. if (ht_buff.pbuff != 0) {
  1447. gs_free_object(mem, ht_buff.pbuff, "clist_playback_band(ht_buff)");
  1448. ht_buff.pbuff = 0;
  1449. ht_buff.pcurr = 0;
  1450. }
  1451. ht_buff.ht_size = 0;
  1452. ht_buff.read_size = 0;
  1453. if (color_space.params.indexed.lookup.table.size)
  1454. gs_free_const_string(mem,
  1455. color_space.params.indexed.lookup.table.data,
  1456. color_space.params.indexed.lookup.table.size,
  1457. "color_space indexed table");
  1458. gx_cpath_free(&clip_path, "clist_render_band exit");
  1459. gx_path_free(&path, "clist_render_band exit");
  1460. gs_imager_state_release(&imager_state);
  1461. gs_free_object(mem, data_bits, "clist_playback_band(data_bits)");
  1462. if (target != orig_target) {
  1463. dev_proc(target, close_device)(target);
  1464. gs_free_object(target->memory, target, "gxclrast discard compositor");
  1465. target = orig_target;
  1466. }
  1467. if (code < 0)
  1468. return_error(code);
  1469. /* Check whether we have more pages to process. */
  1470. if (playback_action != playback_action_setup &&
  1471. (cbp < cbuf.end || !seofp(s))
  1472. )
  1473. goto in;
  1474. return code;
  1475. }
  1476. /* ---------------- Individual commands ---------------- */
  1477. /*
  1478. * These single-use procedures implement a few large individual commands,
  1479. * primarily for readability but also to avoid overflowing compilers'
  1480. * optimization limits. They all take the command buffer as their first
  1481. * parameter (pcb), assume that the current buffer pointer is in pcb->ptr,
  1482. * and update it there.
  1483. */
  1484. private int
  1485. read_set_tile_size(command_buf_t *pcb, tile_slot *bits)
  1486. {
  1487. const byte *cbp = pcb->ptr;
  1488. uint rep_width, rep_height;
  1489. byte bd = *cbp++;
  1490. bits->cb_depth = cmd_code_to_depth(bd);
  1491. cmd_getw(rep_width, cbp);
  1492. cmd_getw(rep_height, cbp);
  1493. if (bd & 0x20) {
  1494. cmd_getw(bits->x_reps, cbp);
  1495. bits->width = rep_width * bits->x_reps;
  1496. } else {
  1497. bits->x_reps = 1;
  1498. bits->width = rep_width;
  1499. }
  1500. if (bd & 0x40) {
  1501. cmd_getw(bits->y_reps, cbp);
  1502. bits->height = rep_height * bits->y_reps;
  1503. } else {
  1504. bits->y_reps = 1;
  1505. bits->height = rep_height;
  1506. }
  1507. if (bd & 0x80)
  1508. cmd_getw(bits->rep_shift, cbp);
  1509. else
  1510. bits->rep_shift = 0;
  1511. if_debug6('L', " depth=%d size=(%d,%d), rep_size=(%d,%d), rep_shift=%d\n",
  1512. bits->cb_depth, bits->width,
  1513. bits->height, rep_width,
  1514. rep_height, bits->rep_shift);
  1515. bits->shift =
  1516. (bits->rep_shift == 0 ? 0 :
  1517. (bits->rep_shift * (bits->height / rep_height)) % rep_width);
  1518. bits->cb_raster = bitmap_raster(bits->width * bits->cb_depth);
  1519. pcb->ptr = cbp;
  1520. return 0;
  1521. }
  1522. private int
  1523. read_set_bits(command_buf_t *pcb, tile_slot *bits, int compress,
  1524. gx_clist_state *pcls, gx_strip_bitmap *tile, tile_slot **pslot,
  1525. gx_device_clist_reader *cdev, gs_memory_t *mem)
  1526. {
  1527. const byte *cbp = pcb->ptr;
  1528. uint rep_width = bits->width / bits->x_reps;
  1529. uint rep_height = bits->height / bits->y_reps;
  1530. uint index;
  1531. ulong offset;
  1532. uint width_bits = rep_width * bits->cb_depth;
  1533. uint width_bytes;
  1534. uint raster;
  1535. uint bytes =
  1536. clist_bitmap_bytes(width_bits, rep_height,
  1537. compress |
  1538. (rep_width < bits->width ?
  1539. decompress_spread : 0) |
  1540. decompress_elsewhere,
  1541. &width_bytes,
  1542. (uint *)&raster);
  1543. byte *data;
  1544. tile_slot *slot;
  1545. cmd_getw(index, cbp);
  1546. cmd_getw(offset, cbp);
  1547. if_debug2('L', " index=%d offset=%lu\n", pcls->tile_index, offset);
  1548. pcls->tile_index = index;
  1549. cdev->tile_table[pcls->tile_index].offset = offset;
  1550. slot = (tile_slot *)(cdev->chunk.data + offset);
  1551. *pslot = slot;
  1552. *slot = *bits;
  1553. tile->data = data = (byte *)(slot + 1);
  1554. #ifdef DEBUG
  1555. slot->index = pcls->tile_index;
  1556. #endif
  1557. if (compress) {
  1558. /*
  1559. * Decompress the image data. We'd like to share this code with the
  1560. * similar code in copy_*, but right now we don't see how.
  1561. */
  1562. stream_cursor_read r;
  1563. stream_cursor_write w;
  1564. /*
  1565. * We don't know the data length a priori, so to be conservative, we
  1566. * read the uncompressed size.
  1567. */
  1568. uint cleft = pcb->end - cbp;
  1569. if (cleft < bytes) {
  1570. uint nread = cbuf_size - cleft;
  1571. memmove(pcb->data, cbp, cleft);
  1572. pcb->end_status = sgets(pcb->s, pcb->data + cleft, nread, &nread);
  1573. set_cb_end(pcb, pcb->data + cleft + nread);
  1574. cbp = pcb->data;
  1575. }
  1576. r.ptr = cbp - 1;
  1577. r.limit = pcb->end - 1;
  1578. w.ptr = data - 1;
  1579. w.limit = w.ptr + bytes;
  1580. switch (compress) {
  1581. case cmd_compress_rle:
  1582. {
  1583. stream_RLD_state sstate;
  1584. clist_rld_init(&sstate);
  1585. (*s_RLD_template.process)
  1586. ((stream_state *)&sstate, &r, &w, true);
  1587. }
  1588. break;
  1589. case cmd_compress_cfe:
  1590. {
  1591. stream_CFD_state sstate;
  1592. clist_cfd_init(&sstate,
  1593. width_bytes << 3 /*width_bits */ ,
  1594. rep_height, mem);
  1595. (*s_CFD_template.process)
  1596. ((stream_state *)&sstate, &r, &w, true);
  1597. (*s_CFD_template.release)
  1598. ((stream_state *)&sstate);
  1599. }
  1600. break;
  1601. default:
  1602. return_error(gs_error_unregistered);
  1603. }
  1604. cbp = r.ptr + 1;
  1605. } else if (rep_height > 1 && width_bytes != bits->cb_raster) {
  1606. cbp = cmd_read_short_bits(pcb, data,
  1607. width_bytes, rep_height,
  1608. bits->cb_raster, cbp);
  1609. } else {
  1610. cbp = cmd_read_data(pcb, data, bytes, cbp);
  1611. }
  1612. if (bits->width > rep_width)
  1613. bits_replicate_horizontally(data,
  1614. rep_width * bits->cb_depth, rep_height,
  1615. bits->cb_raster,
  1616. bits->width * bits->cb_depth,
  1617. bits->cb_raster);
  1618. if (bits->height > rep_height)
  1619. bits_replicate_vertically(data,
  1620. rep_height, bits->cb_raster,
  1621. bits->height);
  1622. #ifdef DEBUG
  1623. if (gs_debug_c('L'))
  1624. cmd_print_bits(data, bits->width, bits->height, bits->cb_raster);
  1625. #endif
  1626. pcb->ptr = cbp;
  1627. return 0;
  1628. }
  1629. /* if necessary, allocate a buffer to hold a serialized halftone */
  1630. private int
  1631. read_alloc_ht_buff(ht_buff_t * pht_buff, uint ht_size, gs_memory_t * mem)
  1632. {
  1633. /* free the existing buffer, if any (usually none) */
  1634. if (pht_buff->pbuff != 0) {
  1635. gs_free_object(mem, pht_buff->pbuff, "read_alloc_ht_buff");
  1636. pht_buff->pbuff = 0;
  1637. }
  1638. /*
  1639. * If the serialized halftone fits in the command buffer, no
  1640. * additional buffer is required.
  1641. */
  1642. if (ht_size > cbuf_ht_seg_max_size) {
  1643. pht_buff->pbuff = gs_alloc_bytes(mem, ht_size, "read_alloc_ht_buff");
  1644. if (pht_buff->pbuff == 0)
  1645. return_error(gs_error_VMerror);
  1646. }
  1647. pht_buff->ht_size = ht_size;
  1648. pht_buff->read_size = 0;
  1649. pht_buff->pcurr = pht_buff->pbuff;
  1650. return 0;
  1651. }
  1652. /* read a halftone segment; if it is the final segment, build the halftone */
  1653. private int
  1654. read_ht_segment(
  1655. ht_buff_t * pht_buff,
  1656. command_buf_t * pcb,
  1657. gs_imager_state * pis,
  1658. gx_device * dev,
  1659. gs_memory_t * mem )
  1660. {
  1661. const byte * cbp = pcb->ptr;
  1662. const byte * pbuff = 0;
  1663. uint ht_size = pht_buff->ht_size, seg_size;
  1664. int code = 0;
  1665. /* get the segment size; refill command buffer if necessary */
  1666. enc_u_getw(seg_size, cbp);
  1667. if (cbp + seg_size > pcb->limit)
  1668. cbp = top_up_cbuf(pcb, cbp);
  1669. if (pht_buff->pbuff == 0) {
  1670. /* if not separate buffer, must be only one segment */
  1671. if (seg_size != ht_size)
  1672. return_error(gs_error_unknownerror);
  1673. pbuff = cbp;
  1674. } else {
  1675. if (seg_size + pht_buff->read_size > pht_buff->ht_size)
  1676. return_error(gs_error_unknownerror);
  1677. memcpy(pht_buff->pcurr, cbp, seg_size);
  1678. pht_buff->pcurr += seg_size;
  1679. if ((pht_buff->read_size += seg_size) == ht_size)
  1680. pbuff = pht_buff->pbuff;
  1681. }
  1682. /* if everything has be read, covert back to a halftone */
  1683. if (pbuff != 0) {
  1684. code = gx_ht_read_and_install(pis, dev, pbuff, ht_size, mem);
  1685. /* release any buffered information */
  1686. if (pht_buff->pbuff != 0) {
  1687. gs_free_object(mem, pht_buff->pbuff, "read_alloc_ht_buff");
  1688. pht_buff->pbuff = 0;
  1689. pht_buff->pcurr = 0;
  1690. }
  1691. pht_buff->ht_size = 0;
  1692. pht_buff->read_size = 0;
  1693. }
  1694. /* update the command buffer ponter */
  1695. pcb->ptr = cbp + seg_size;
  1696. return code;
  1697. }
  1698. private int
  1699. read_set_misc2(command_buf_t *pcb, gs_imager_state *pis, segment_notes *pnotes)
  1700. {
  1701. const byte *cbp = pcb->ptr;
  1702. uint mask, cb;
  1703. cmd_getw(mask, cbp);
  1704. if (mask & cap_join_known) {
  1705. cb = *cbp++;
  1706. pis->line_params.cap = (gs_line_cap)((cb >> 3) & 7);
  1707. pis->line_params.join = (gs_line_join)(cb & 7);
  1708. if_debug2('L', " cap=%d join=%d\n",
  1709. pis->line_params.cap, pis->line_params.join);
  1710. }
  1711. if (mask & cj_ac_sa_known) {
  1712. cb = *cbp++;
  1713. pis->line_params.curve_join = ((cb >> 2) & 7) - 1;
  1714. pis->accurate_curves = (cb & 2) != 0;
  1715. pis->stroke_adjust = cb & 1;
  1716. if_debug3('L', " CJ=%d AC=%d SA=%d\n",
  1717. pis->line_params.curve_join, pis->accurate_curves,
  1718. pis->stroke_adjust);
  1719. }
  1720. if (mask & flatness_known) {
  1721. cmd_get_value(pis->flatness, cbp);
  1722. if_debug1('L', " flatness=%g\n", pis->flatness);
  1723. }
  1724. if (mask & line_width_known) {
  1725. float width;
  1726. cmd_get_value(width, cbp);
  1727. if_debug1('L', " line_width=%g\n", width);
  1728. gx_set_line_width(&pis->line_params, width);
  1729. }
  1730. if (mask & miter_limit_known) {
  1731. float limit;
  1732. cmd_get_value(limit, cbp);
  1733. if_debug1('L', " miter_limit=%g\n", limit);
  1734. gx_set_miter_limit(&pis->line_params, limit);
  1735. }
  1736. if (mask & op_bm_tk_known) {
  1737. cb = *cbp++;
  1738. pis->blend_mode = cb >> 3;
  1739. pis->text_knockout = (cb & 4) != 0;
  1740. /* the following usually have no effect; see gxclpath.c */
  1741. pis->overprint_mode = (cb >> 1) & 1;
  1742. pis->effective_overprint_mode = pis->overprint_mode;
  1743. pis->overprint = cb & 1;
  1744. if_debug4('L', " BM=%d TK=%d OPM=%d OP=%d\n",
  1745. pis->blend_mode, pis->text_knockout, pis->overprint_mode,
  1746. pis->overprint);
  1747. }
  1748. if (mask & segment_notes_known) {
  1749. cb = *cbp++;
  1750. *pnotes = (segment_notes)(cb & 0x3f);
  1751. if_debug1('L', " notes=%d\n", *pnotes);
  1752. }
  1753. if (mask & opacity_alpha_known) {
  1754. cmd_get_value(pis->opacity.alpha, cbp);
  1755. if_debug1('L', " opacity.alpha=%g\n", pis->opacity.alpha);
  1756. }
  1757. if (mask & shape_alpha_known) {
  1758. cmd_get_value(pis->shape.alpha, cbp);
  1759. if_debug1('L', " shape.alpha=%g\n", pis->shape.alpha);
  1760. }
  1761. if (mask & alpha_known) {
  1762. cmd_get_value(pis->alpha, cbp);
  1763. if_debug1('L', " alpha=%u\n", pis->alpha);
  1764. }
  1765. pcb->ptr = cbp;
  1766. return 0;
  1767. }
  1768. private int
  1769. read_set_color_space(command_buf_t *pcb, gs_imager_state *pis,
  1770. const gs_color_space **ppcs, gs_color_space *pcolor_space,
  1771. gs_memory_t *mem)
  1772. {
  1773. const byte *cbp = pcb->ptr;
  1774. byte b = *cbp++;
  1775. int index = b >> 4;
  1776. const gs_color_space *pcs;
  1777. int code = 0;
  1778. /*
  1779. * The following are used only for a single, parameterless color space,
  1780. * so they do not introduce any re-entrancy problems.
  1781. */
  1782. static gs_color_space gray_cs, rgb_cs, cmyk_cs;
  1783. if_debug3('L', " %d%s%s\n", index,
  1784. (b & 8 ? " (indexed)" : ""),
  1785. (b & 4 ? "(proc)" : ""));
  1786. switch (index) {
  1787. case gs_color_space_index_DeviceGray:
  1788. gs_cspace_init_DeviceGray(mem, &gray_cs);
  1789. pcs = &gray_cs;
  1790. break;
  1791. case gs_color_space_index_DeviceRGB:
  1792. gs_cspace_init_DeviceRGB(mem, &rgb_cs);
  1793. pcs = &rgb_cs;
  1794. break;
  1795. case gs_color_space_index_DeviceCMYK:
  1796. gs_cspace_init_DeviceCMYK(mem, &cmyk_cs);
  1797. pcs = &cmyk_cs;
  1798. break;
  1799. default:
  1800. code = gs_note_error(gs_error_rangecheck); /* others are NYI */
  1801. goto out;
  1802. }
  1803. /* Free any old indexed color space data. */
  1804. if (pcolor_space->params.indexed.use_proc) {
  1805. if (pcolor_space->params.indexed.lookup.map)
  1806. gs_free_object(mem,
  1807. pcolor_space->params.indexed.lookup.map->values,
  1808. "old indexed map values");
  1809. gs_free_object(mem, pcolor_space->params.indexed.lookup.map,
  1810. "old indexed map");
  1811. pcolor_space->params.indexed.lookup.map = 0;
  1812. } else {
  1813. if (pcolor_space->params.indexed.lookup.table.size)
  1814. gs_free_const_string(mem,
  1815. pcolor_space->params.indexed.lookup.table.data,
  1816. pcolor_space->params.indexed.lookup.table.size,
  1817. "color_spapce indexed table");
  1818. pcolor_space->params.indexed.lookup.table.size = 0;
  1819. }
  1820. if (b & 8) {
  1821. bool use_proc = (b & 4) != 0;
  1822. int hival;
  1823. int num_values;
  1824. byte *data;
  1825. uint data_size;
  1826. cmd_getw(hival, cbp);
  1827. num_values = (hival + 1) * gs_color_space_num_components(pcs);
  1828. if (use_proc) {
  1829. gs_indexed_map *map;
  1830. code = alloc_indexed_map(&map, num_values, mem, "indexed map");
  1831. if (code < 0)
  1832. goto out;
  1833. map->proc.lookup_index = lookup_indexed_map;
  1834. pcolor_space->params.indexed.lookup.map = map;
  1835. data = (byte *)map->values;
  1836. data_size = num_values * sizeof(map->values[0]);
  1837. } else {
  1838. byte *table = gs_alloc_string(mem, num_values, "color_space indexed table");
  1839. if (table == 0) {
  1840. code = gs_note_error(gs_error_VMerror);
  1841. goto out;
  1842. }
  1843. pcolor_space->params.indexed.lookup.table.data = table;
  1844. pcolor_space->params.indexed.lookup.table.size = num_values;
  1845. data = table;
  1846. data_size = num_values;
  1847. }
  1848. cbp = cmd_read_data(pcb, data, data_size, cbp);
  1849. pcolor_space->type =
  1850. &gs_color_space_type_Indexed;
  1851. memmove(&pcolor_space->params.indexed.base_space, pcs,
  1852. sizeof(pcolor_space->params.indexed.base_space));
  1853. pcolor_space->params.indexed.hival = hival;
  1854. pcolor_space->params.indexed.use_proc = use_proc;
  1855. pcs = pcolor_space;
  1856. }
  1857. *ppcs = pcs;
  1858. out:
  1859. pcb->ptr = cbp;
  1860. return code;
  1861. }
  1862. private int
  1863. read_begin_image(command_buf_t *pcb, gs_image_common_t *pic,
  1864. const gs_color_space *pcs)
  1865. {
  1866. uint index = *(pcb->ptr)++;
  1867. const gx_image_type_t *image_type = gx_image_type_table[index];
  1868. stream s;
  1869. int code;
  1870. /* This is sloppy, but we don't have enough information to do better. */
  1871. pcb->ptr = top_up_cbuf(pcb, pcb->ptr);
  1872. s_init(&s, NULL);
  1873. sread_string(&s, pcb->ptr, pcb->end - pcb->ptr);
  1874. code = image_type->sget(pic, &s, pcs);
  1875. pcb->ptr = sbufptr(&s);
  1876. return code;
  1877. }
  1878. private int
  1879. read_put_params(command_buf_t *pcb, gs_imager_state *pis,
  1880. gx_device_clist_reader *cdev, gs_memory_t *mem)
  1881. {
  1882. const byte *cbp = pcb->ptr;
  1883. gs_c_param_list param_list;
  1884. uint cleft;
  1885. uint rleft;
  1886. bool alloc_data_on_heap = false;
  1887. byte *param_buf;
  1888. uint param_length;
  1889. int code = 0;
  1890. cmd_get_value(param_length, cbp);
  1891. if_debug1('L', " length=%d\n", param_length);
  1892. if (param_length == 0) {
  1893. code = 1; /* empty list */
  1894. goto out;
  1895. }
  1896. /* Make sure entire serialized param list is in cbuf */
  1897. /* + force void* alignment */
  1898. cbp = top_up_cbuf(pcb, cbp);
  1899. if (pcb->end - cbp >= param_length) {
  1900. param_buf = (byte *)cbp;
  1901. cbp += param_length;
  1902. } else {
  1903. /* NOTE: param_buf must be maximally aligned */
  1904. param_buf = gs_alloc_bytes(mem, param_length,
  1905. "clist put_params");
  1906. if (param_buf == 0) {
  1907. code = gs_note_error(gs_error_VMerror);
  1908. goto out;
  1909. }
  1910. alloc_data_on_heap = true;
  1911. cleft = pcb->end - cbp;
  1912. rleft = param_length - cleft;
  1913. memmove(param_buf, cbp, cleft);
  1914. pcb->end_status = sgets(pcb->s, param_buf + cleft, rleft, &rleft);
  1915. cbp = pcb->end; /* force refill */
  1916. }
  1917. /*
  1918. * Create a gs_c_param_list & expand into it.
  1919. * NB that gs_c_param_list doesn't copy objects into
  1920. * it, but rather keeps *pointers* to what's passed.
  1921. * That's OK because the serialized format keeps enough
  1922. * space to hold expanded versions of the structures,
  1923. * but this means we cannot deallocate source buffer
  1924. * until the gs_c_param_list is deleted.
  1925. */
  1926. gs_c_param_list_write(&param_list, mem);
  1927. code = gs_param_list_unserialize
  1928. ( (gs_param_list *)&param_list, param_buf );
  1929. if (code >= 0 && code != param_length)
  1930. code = gs_error_unknownerror; /* must match */
  1931. if (code >= 0) {
  1932. gs_c_param_list_read(&param_list);
  1933. code = gs_imager_putdeviceparams(pis, (gx_device *)cdev,
  1934. (gs_param_list *)&param_list);
  1935. }
  1936. gs_c_param_list_release(&param_list);
  1937. if (alloc_data_on_heap)
  1938. gs_free_object(mem, param_buf, "clist put_params");
  1939. out:
  1940. pcb->ptr = cbp;
  1941. return code;
  1942. }
  1943. /*
  1944. * Read a "create_compositor" command, and execute the command.
  1945. *
  1946. * This code assumes that a the largest create compositor command,
  1947. * including the compositor name size, is smaller than the data buffer
  1948. * size. This assumption is inherent in the basic design of the coding
  1949. * and the de-serializer interface, as no length field is provided.
  1950. * At the time of this writing, no compositor comes remotely close to
  1951. * violating this assumption (largest create_compositor is about 16
  1952. * bytes, while the command data buffer is 800 bytes), nor is any likely
  1953. * to do so in the future. In the unlikely event that this assumption
  1954. * is violated, a change in the encoding would be called for).
  1955. */
  1956. extern_gs_find_compositor();
  1957. private int
  1958. read_create_compositor(
  1959. command_buf_t * pcb,
  1960. gs_imager_state * pis,
  1961. gx_device_clist_reader * cdev,
  1962. gs_memory_t * mem,
  1963. gx_device ** ptarget )
  1964. {
  1965. const byte * cbp = pcb->ptr;
  1966. int comp_id = 0, code = 0;
  1967. const gs_composite_type_t * pcomp_type = 0;
  1968. gs_composite_t * pcomp;
  1969. gx_device * tdev = *ptarget;
  1970. /* fill the command buffer (see comment above) */
  1971. cbp = top_up_cbuf(pcb, cbp);
  1972. /* find the appropriate compositor method vector */
  1973. comp_id = *cbp++;
  1974. if ((pcomp_type = gs_find_compositor(comp_id)) == 0)
  1975. return_error(gs_error_unknownerror);
  1976. /* de-serialize the compositor */
  1977. code = pcomp_type->procs.read(&pcomp, cbp, pcb->end - cbp, mem);
  1978. if (code > 0)
  1979. cbp += code;
  1980. pcb->ptr = cbp;
  1981. if (code < 0 || pcomp == 0)
  1982. return code;
  1983. /*
  1984. * Apply the compositor to the target device; note that this may
  1985. * change the target device.
  1986. */
  1987. code = dev_proc(tdev, create_compositor)(tdev, &tdev, pcomp, pis, mem);
  1988. if (code >= 0 && tdev != *ptarget) {
  1989. rc_increment(tdev);
  1990. *ptarget = tdev;
  1991. }
  1992. /* Perform any updates for the clist device required */
  1993. code = pcomp->type->procs.clist_compositor_read_update(pcomp,
  1994. (gx_device *)cdev, tdev, pis, mem);
  1995. if (code < 0)
  1996. return code;
  1997. /* free the compositor object */
  1998. if (pcomp != 0)
  1999. gs_free_object(mem, pcomp, "read_create_compositor");
  2000. return code;
  2001. }
  2002. /* ---------------- Utilities ---------------- */
  2003. /* Read and unpack a short bitmap */
  2004. private const byte *
  2005. cmd_read_short_bits(command_buf_t *pcb, byte *data, int width_bytes,
  2006. int height, uint raster, const byte *cbp)
  2007. {
  2008. uint bytes = width_bytes * height;
  2009. const byte *pdata = data /*src*/ + bytes;
  2010. byte *udata = data /*dest*/ + height * raster;
  2011. cbp = cmd_read_data(pcb, data, width_bytes * height, cbp);
  2012. while (--height >= 0) {
  2013. udata -= raster, pdata -= width_bytes;
  2014. switch (width_bytes) {
  2015. default:
  2016. memmove(udata, pdata, width_bytes);
  2017. break;
  2018. case 6:
  2019. udata[5] = pdata[5];
  2020. case 5:
  2021. udata[4] = pdata[4];
  2022. case 4:
  2023. udata[3] = pdata[3];
  2024. case 3:
  2025. udata[2] = pdata[2];
  2026. case 2:
  2027. udata[1] = pdata[1];
  2028. case 1:
  2029. udata[0] = pdata[0];
  2030. case 0:; /* shouldn't happen */
  2031. }
  2032. }
  2033. return cbp;
  2034. }
  2035. /* Read a rectangle. */
  2036. private const byte *
  2037. cmd_read_rect(int op, gx_cmd_rect * prect, const byte * cbp)
  2038. {
  2039. cmd_getw(prect->x, cbp);
  2040. if (op & 0xf)
  2041. prect->y += ((op >> 2) & 3) - 2;
  2042. else {
  2043. cmd_getw(prect->y, cbp);
  2044. }
  2045. cmd_getw(prect->width, cbp);
  2046. if (op & 0xf)
  2047. prect->height += (op & 3) - 2;
  2048. else {
  2049. cmd_getw(prect->height, cbp);
  2050. }
  2051. return cbp;
  2052. }
  2053. /* Read a transformation matrix. */
  2054. private const byte *
  2055. cmd_read_matrix(gs_matrix * pmat, const byte * cbp)
  2056. {
  2057. stream s;
  2058. s_init(&s, NULL);
  2059. sread_string(&s, cbp, 1 + sizeof(*pmat));
  2060. sget_matrix(&s, pmat);
  2061. return cbp + stell(&s);
  2062. }
  2063. /*
  2064. * Select a map for loading with data.
  2065. *
  2066. * This routine has three outputs:
  2067. * *pmdata - points to the map data.
  2068. * *pcomp_num - points to a component number if the map is a transfer
  2069. * map which has been set via the setcolortransfer operator.
  2070. * A. value of NULL indicates that no component number is to
  2071. * be sent for this map.
  2072. * *pcount - the size of the map (in bytes).
  2073. */
  2074. private int
  2075. cmd_select_map(cmd_map_index map_index, cmd_map_contents cont,
  2076. gs_imager_state * pis, int ** pcomp_num, frac ** pmdata,
  2077. uint * pcount, gs_memory_t * mem)
  2078. {
  2079. gx_transfer_map *map;
  2080. gx_transfer_map **pmap;
  2081. const char *cname;
  2082. *pcomp_num = NULL; /* Only used for color transfer maps */
  2083. switch (map_index) {
  2084. case cmd_map_transfer:
  2085. if_debug0('L', " transfer");
  2086. rc_unshare_struct(pis->set_transfer.gray, gx_transfer_map,
  2087. &st_transfer_map, mem, return_error(gs_error_VMerror),
  2088. "cmd_select_map(default_transfer)");
  2089. map = pis->set_transfer.gray;
  2090. /* Release all current maps */
  2091. rc_decrement(pis->set_transfer.red, "cmd_select_map(red)");
  2092. pis->set_transfer.red = NULL;
  2093. pis->set_transfer.red_component_num = -1;
  2094. rc_decrement(pis->set_transfer.green, "cmd_select_map(green)");
  2095. pis->set_transfer.green = NULL;
  2096. pis->set_transfer.green_component_num = -1;
  2097. rc_decrement(pis->set_transfer.blue, "cmd_select_map(blue)");
  2098. pis->set_transfer.blue = NULL;
  2099. pis->set_transfer.blue_component_num = -1;
  2100. goto transfer2;
  2101. case cmd_map_transfer_0:
  2102. pmap = &pis->set_transfer.red;
  2103. *pcomp_num = &pis->set_transfer.red_component_num;
  2104. goto transfer1;
  2105. case cmd_map_transfer_1:
  2106. pmap = &pis->set_transfer.green;
  2107. *pcomp_num = &pis->set_transfer.green_component_num;
  2108. goto transfer1;
  2109. case cmd_map_transfer_2:
  2110. pmap = &pis->set_transfer.blue;
  2111. *pcomp_num = &pis->set_transfer.blue_component_num;
  2112. goto transfer1;
  2113. case cmd_map_transfer_3:
  2114. pmap = &pis->set_transfer.gray;
  2115. *pcomp_num = &pis->set_transfer.gray_component_num;
  2116. transfer1: {
  2117. int i = map_index - cmd_map_transfer_0;
  2118. if_debug1('L', " transfer[%d]", i);
  2119. }
  2120. rc_unshare_struct(*pmap, gx_transfer_map, &st_transfer_map, mem,
  2121. return_error(gs_error_VMerror), "cmd_select_map(transfer)");
  2122. map = *pmap;
  2123. transfer2: if (cont != cmd_map_other) {
  2124. gx_set_identity_transfer(map);
  2125. *pmdata = 0;
  2126. *pcount = 0;
  2127. return 0;
  2128. }
  2129. break;
  2130. case cmd_map_black_generation:
  2131. if_debug0('L', " black generation");
  2132. pmap = &pis->black_generation;
  2133. cname = "cmd_select_map(black generation)";
  2134. goto alloc;
  2135. case cmd_map_undercolor_removal:
  2136. if_debug0('L', " undercolor removal");
  2137. pmap = &pis->undercolor_removal;
  2138. cname = "cmd_select_map(undercolor removal)";
  2139. alloc: if (cont == cmd_map_none) {
  2140. rc_decrement(*pmap, cname);
  2141. *pmap = 0;
  2142. *pmdata = 0;
  2143. *pcount = 0;
  2144. return 0;
  2145. }
  2146. rc_unshare_struct(*pmap, gx_transfer_map, &st_transfer_map,
  2147. mem, return_error(gs_error_VMerror), cname);
  2148. map = *pmap;
  2149. if (cont == cmd_map_identity) {
  2150. gx_set_identity_transfer(map);
  2151. *pmdata = 0;
  2152. *pcount = 0;
  2153. return 0;
  2154. }
  2155. break;
  2156. default:
  2157. *pmdata = 0;
  2158. return 0;
  2159. }
  2160. map->proc = gs_mapped_transfer;
  2161. *pmdata = map->values;
  2162. *pcount = sizeof(map->values);
  2163. return 0;
  2164. }
  2165. /* Create a device halftone for the imager if necessary. */
  2166. private int
  2167. cmd_create_dev_ht(gx_device_halftone **ppdht, gs_memory_t *mem)
  2168. {
  2169. gx_device_halftone *pdht = *ppdht;
  2170. if (pdht == 0) {
  2171. rc_header rc;
  2172. rc_alloc_struct_1(pdht, gx_device_halftone, &st_device_halftone, mem,
  2173. return_error(gs_error_VMerror),
  2174. "cmd_create_dev_ht");
  2175. rc = pdht->rc;
  2176. memset(pdht, 0, sizeof(*pdht));
  2177. pdht->rc = rc;
  2178. *ppdht = pdht;
  2179. }
  2180. return 0;
  2181. }
  2182. /* Resize the halftone components array if necessary. */
  2183. private int
  2184. cmd_resize_halftone(gx_device_halftone **ppdht, uint num_comp,
  2185. gs_memory_t * mem)
  2186. {
  2187. int code = cmd_create_dev_ht(ppdht, mem);
  2188. gx_device_halftone *pdht = *ppdht;
  2189. if (code < 0)
  2190. return code;
  2191. if (num_comp != pdht->num_comp) {
  2192. gx_ht_order_component *pcomp;
  2193. /*
  2194. * We must be careful not to shrink or free the components array
  2195. * before releasing any relevant elements.
  2196. */
  2197. if (num_comp < pdht->num_comp) {
  2198. uint i;
  2199. /* Don't release the default order. */
  2200. for (i = pdht->num_comp; i-- > num_comp;)
  2201. if (pdht->components[i].corder.bit_data != pdht->order.bit_data)
  2202. gx_ht_order_release(&pdht->components[i].corder, mem, true);
  2203. if (num_comp == 0) {
  2204. gs_free_object(mem, pdht->components, "cmd_resize_halftone");
  2205. pcomp = 0;
  2206. } else {
  2207. pcomp = gs_resize_object(mem, pdht->components, num_comp,
  2208. "cmd_resize_halftone");
  2209. if (pcomp == 0) {
  2210. pdht->num_comp = num_comp; /* attempt consistency */
  2211. return_error(gs_error_VMerror);
  2212. }
  2213. }
  2214. } else {
  2215. /* num_comp > pdht->num_comp */
  2216. if (pdht->num_comp == 0)
  2217. pcomp = gs_alloc_struct_array(mem, num_comp,
  2218. gx_ht_order_component,
  2219. &st_ht_order_component_element,
  2220. "cmd_resize_halftone");
  2221. else
  2222. pcomp = gs_resize_object(mem, pdht->components, num_comp,
  2223. "cmd_resize_halftone");
  2224. if (pcomp == 0)
  2225. return_error(gs_error_VMerror);
  2226. memset(&pcomp[pdht->num_comp], 0,
  2227. sizeof(*pcomp) * (num_comp - pdht->num_comp));
  2228. }
  2229. pdht->num_comp = num_comp;
  2230. pdht->components = pcomp;
  2231. }
  2232. return 0;
  2233. }
  2234. /* ------ Path operations ------ */
  2235. /* Decode a path segment. */
  2236. private int
  2237. clist_decode_segment(gx_path * ppath, int op, fixed vs[6],
  2238. gs_fixed_point * ppos, int x0, int y0, segment_notes notes)
  2239. {
  2240. fixed px = ppos->x - int2fixed(x0);
  2241. fixed py = ppos->y - int2fixed(y0);
  2242. int code;
  2243. #define A vs[0]
  2244. #define B vs[1]
  2245. #define C vs[2]
  2246. #define D vs[3]
  2247. #define E vs[4]
  2248. #define F vs[5]
  2249. switch (op) {
  2250. case cmd_opv_rmoveto:
  2251. code = gx_path_add_point(ppath, px += A, py += B);
  2252. break;
  2253. case cmd_opv_rlineto:
  2254. code = gx_path_add_line_notes(ppath, px += A, py += B, notes);
  2255. break;
  2256. case cmd_opv_hlineto:
  2257. code = gx_path_add_line_notes(ppath, px += A, py, notes);
  2258. break;
  2259. case cmd_opv_vlineto:
  2260. code = gx_path_add_line_notes(ppath, px, py += A, notes);
  2261. break;
  2262. case cmd_opv_rmlineto:
  2263. if ((code = gx_path_add_point(ppath, px += A, py += B)) < 0)
  2264. break;
  2265. code = gx_path_add_line_notes(ppath, px += C, py += D, notes);
  2266. break;
  2267. case cmd_opv_rm2lineto:
  2268. if ((code = gx_path_add_point(ppath, px += A, py += B)) < 0 ||
  2269. (code = gx_path_add_line_notes(ppath, px += C, py += D,
  2270. notes)) < 0
  2271. )
  2272. break;
  2273. code = gx_path_add_line_notes(ppath, px += E, py += F, notes);
  2274. break;
  2275. case cmd_opv_rm3lineto:
  2276. if ((code = gx_path_add_point(ppath, px += A, py += B)) < 0 ||
  2277. (code = gx_path_add_line_notes(ppath, px += C, py += D,
  2278. notes)) < 0 ||
  2279. (code = gx_path_add_line_notes(ppath, px += E, py += F,
  2280. notes)) < 0
  2281. )
  2282. break;
  2283. code = gx_path_add_line_notes(ppath, px -= C, py -= D, notes);
  2284. break;
  2285. case cmd_opv_rrcurveto: /* a b c d e f => a b a+c b+d a+c+e b+d+f */
  2286. rrc: E += (C += A);
  2287. F += (D += B);
  2288. curve: code = gx_path_add_curve_notes(ppath, px + A, py + B,
  2289. px + C, py + D,
  2290. px + E, py + F, notes);
  2291. px += E, py += F;
  2292. break;
  2293. case cmd_opv_hvcurveto: /* a b c d => a 0 a+b c a+b c+d */
  2294. hvc: F = C + D, D = C, E = C = A + B, B = 0;
  2295. goto curve;
  2296. case cmd_opv_vhcurveto: /* a b c d => 0 a b a+c b+d a+c */
  2297. vhc: E = B + D, F = D = A + C, C = B, B = A, A = 0;
  2298. goto curve;
  2299. case cmd_opv_nrcurveto: /* a b c d => 0 0 a b a+c b+d */
  2300. F = B + D, E = A + C, D = B, C = A, B = A = 0;
  2301. goto curve;
  2302. case cmd_opv_rncurveto: /* a b c d => a b a+c b+d a+c b+d */
  2303. F = D += B, E = C += A;
  2304. goto curve;
  2305. case cmd_opv_vqcurveto: /* a b => VH a b TS(a,b) TS(b,a) */
  2306. if ((A ^ B) < 0)
  2307. C = -B, D = -A;
  2308. else
  2309. C = B, D = A;
  2310. goto vhc;
  2311. case cmd_opv_hqcurveto: /* a b => HV a TS(a,b) b TS(b,a) */
  2312. if ((A ^ B) < 0)
  2313. D = -A, C = B, B = -B;
  2314. else
  2315. D = A, C = B;
  2316. goto hvc;
  2317. case cmd_opv_scurveto: /* (a b c d e f) => */
  2318. {
  2319. fixed a = A, b = B;
  2320. /* See gxclpath.h for details on the following. */
  2321. if (A == 0) {
  2322. /* Previous curve was vh or vv */
  2323. A = E - C, B = D - F, C = C - a, D = b - D, E = a, F = -b;
  2324. } else {
  2325. /* Previous curve was hv or hh */
  2326. A = C - E, B = F - D, C = a - C, D = D - b, E = -a, F = b;
  2327. }
  2328. }
  2329. goto rrc;
  2330. case cmd_opv_closepath:
  2331. code = gx_path_close_subpath(ppath);
  2332. gx_path_current_point(ppath, (gs_fixed_point *) vs);
  2333. px = A, py = B;
  2334. break;
  2335. default:
  2336. return_error(gs_error_rangecheck);
  2337. }
  2338. #undef A
  2339. #undef B
  2340. #undef C
  2341. #undef D
  2342. #undef E
  2343. #undef F
  2344. ppos->x = px + int2fixed(x0);
  2345. ppos->y = py + int2fixed(y0);
  2346. return code;
  2347. }
  2348. /*
  2349. * Execute a polyfill -- either a fill_parallelogram or a fill_triangle.
  2350. * If we ever implement fill_trapezoid in the band list, that will be
  2351. * detected here too.
  2352. *
  2353. * Note that degenerate parallelograms or triangles may collapse into
  2354. * a single line or point. We must check for this so we don't try to
  2355. * access non-existent segments.
  2356. */
  2357. private int
  2358. clist_do_polyfill(gx_device *dev, gx_path *ppath,
  2359. const gx_drawing_color *pdcolor,
  2360. gs_logical_operation_t lop)
  2361. {
  2362. const subpath *psub = ppath->first_subpath;
  2363. const segment *pseg1;
  2364. const segment *pseg2;
  2365. int code;
  2366. if (psub && (pseg1 = psub->next) != 0 && (pseg2 = pseg1->next) != 0) {
  2367. fixed px = psub->pt.x, py = psub->pt.y;
  2368. fixed ax = pseg1->pt.x - px, ay = pseg1->pt.y - py;
  2369. fixed bx, by;
  2370. /*
  2371. * We take advantage of the fact that the parameter lists for
  2372. * fill_parallelogram and fill_triangle are identical.
  2373. */
  2374. dev_proc_fill_parallelogram((*fill));
  2375. if (pseg2->next) {
  2376. /* Parallelogram */
  2377. fill = dev_proc(dev, fill_parallelogram);
  2378. bx = pseg2->pt.x - pseg1->pt.x;
  2379. by = pseg2->pt.y - pseg1->pt.y;
  2380. } else {
  2381. /* Triangle */
  2382. fill = dev_proc(dev, fill_triangle);
  2383. bx = pseg2->pt.x - px;
  2384. by = pseg2->pt.y - py;
  2385. }
  2386. code = fill(dev, px, py, ax, ay, bx, by, pdcolor, lop);
  2387. } else
  2388. code = 0;
  2389. gx_path_new(ppath);
  2390. return code;
  2391. }