zdevice.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: zdevice.c,v 1.3.6.1 2002/01/25 06:33:09 rayjj Exp $ */
  16. /* Device-related operators */
  17. #include "string_.h"
  18. #include "ghost.h"
  19. #include "oper.h"
  20. #include "ialloc.h"
  21. #include "idict.h"
  22. #include "igstate.h"
  23. #include "iname.h"
  24. #include "interp.h"
  25. #include "iparam.h"
  26. #include "ivmspace.h"
  27. #include "gsmatrix.h"
  28. #include "gsstate.h"
  29. #include "gxdevice.h"
  30. #include "gxgetbit.h"
  31. #include "store.h"
  32. /* <device> <keep_open> .copydevice2 <newdevice> */
  33. private int
  34. zcopydevice2(i_ctx_t *i_ctx_p)
  35. {
  36. os_ptr op = osp;
  37. gx_device *new_dev;
  38. int code;
  39. check_read_type(op[-1], t_device);
  40. check_type(*op, t_boolean);
  41. code = gs_copydevice2(&new_dev, op[-1].value.pdevice, op->value.boolval,
  42. imemory);
  43. if (code < 0)
  44. return code;
  45. new_dev->memory = imemory;
  46. make_tav(op - 1, t_device, icurrent_space | a_all, pdevice, new_dev);
  47. pop(1);
  48. return 0;
  49. }
  50. /* - currentdevice <device> */
  51. int
  52. zcurrentdevice(i_ctx_t *i_ctx_p)
  53. {
  54. os_ptr op = osp;
  55. gx_device *dev = gs_currentdevice(igs);
  56. gs_ref_memory_t *mem = (gs_ref_memory_t *) dev->memory;
  57. push(1);
  58. make_tav(op, t_device,
  59. (mem == 0 ? avm_foreign : imemory_space(mem)) | a_all,
  60. pdevice, dev);
  61. return 0;
  62. }
  63. /* <device> .devicename <string> */
  64. private int
  65. zdevicename(i_ctx_t *i_ctx_p)
  66. {
  67. os_ptr op = osp;
  68. const char *dname;
  69. check_read_type(*op, t_device);
  70. dname = op->value.pdevice->dname;
  71. make_const_string(op, avm_foreign | a_readonly, strlen(dname),
  72. (const byte *)dname);
  73. return 0;
  74. }
  75. /* - .doneshowpage - */
  76. private int
  77. zdoneshowpage(i_ctx_t *i_ctx_p)
  78. {
  79. gx_device *dev = gs_currentdevice(igs);
  80. gx_device *tdev = (*dev_proc(dev, get_page_device)) (dev);
  81. if (tdev != 0)
  82. tdev->ShowpageCount++;
  83. return 0;
  84. }
  85. /* - flushpage - */
  86. int
  87. zflushpage(i_ctx_t *i_ctx_p)
  88. {
  89. return gs_flushpage(igs);
  90. }
  91. /* <device> <x> <y> <width> <max_height> <alpha?> <std_depth|null> <string> */
  92. /* .getbitsrect <height> <substring> */
  93. private int
  94. zgetbitsrect(i_ctx_t *i_ctx_p)
  95. { /*
  96. * alpha? is 0 for no alpha, -1 for alpha first, 1 for alpha last.
  97. * std_depth is null for native pixels, depth/component for
  98. * standard color space.
  99. */
  100. os_ptr op = osp;
  101. gx_device *dev;
  102. gs_int_rect rect;
  103. gs_get_bits_params_t params;
  104. int w, h;
  105. gs_get_bits_options_t options =
  106. GB_ALIGN_ANY | GB_RETURN_COPY | GB_OFFSET_0 | GB_RASTER_STANDARD |
  107. GB_PACKING_CHUNKY;
  108. int depth;
  109. uint raster;
  110. int num_rows;
  111. int code;
  112. check_read_type(op[-7], t_device);
  113. dev = op[-7].value.pdevice;
  114. check_int_leu(op[-6], dev->width);
  115. rect.p.x = op[-6].value.intval;
  116. check_int_leu(op[-5], dev->height);
  117. rect.p.y = op[-5].value.intval;
  118. check_int_leu(op[-4], dev->width);
  119. w = op[-4].value.intval;
  120. check_int_leu(op[-3], dev->height);
  121. h = op[-3].value.intval;
  122. check_type(op[-2], t_integer);
  123. /*
  124. * We use if/else rather than switch because the value is long,
  125. * which is not supported as a switch value in pre-ANSI C.
  126. */
  127. if (op[-2].value.intval == -1)
  128. options |= GB_ALPHA_FIRST;
  129. else if (op[-2].value.intval == 0)
  130. options |= GB_ALPHA_NONE;
  131. else if (op[-2].value.intval == 1)
  132. options |= GB_ALPHA_LAST;
  133. else
  134. return_error(e_rangecheck);
  135. if (r_has_type(op - 1, t_null)) {
  136. options |= GB_COLORS_NATIVE;
  137. depth = dev->color_info.depth;
  138. } else {
  139. static const gs_get_bits_options_t depths[17] = {
  140. 0, GB_DEPTH_1, GB_DEPTH_2, 0, GB_DEPTH_4, 0, 0, 0, GB_DEPTH_8,
  141. 0, 0, 0, GB_DEPTH_12, 0, 0, 0, GB_DEPTH_16
  142. };
  143. gs_get_bits_options_t depth_option;
  144. int std_depth;
  145. check_int_leu(op[-1], 16);
  146. std_depth = (int)op[-1].value.intval;
  147. depth_option = depths[std_depth];
  148. if (depth_option == 0)
  149. return_error(e_rangecheck);
  150. options |= depth_option | gb_colors_for_device(dev);
  151. depth = (dev->color_info.num_components +
  152. (options & GB_ALPHA_NONE ? 0 : 1)) * std_depth;
  153. }
  154. raster = (w * depth + 7) >> 3;
  155. check_write_type(*op, t_string);
  156. num_rows = r_size(op) / raster;
  157. h = min(h, num_rows);
  158. if (h == 0)
  159. return_error(e_rangecheck);
  160. rect.q.x = rect.p.x + w;
  161. rect.q.y = rect.p.y + h;
  162. params.options = options;
  163. params.data[0] = op->value.bytes;
  164. code = (*dev_proc(dev, get_bits_rectangle))(dev, &rect, &params, NULL);
  165. if (code < 0)
  166. return code;
  167. make_int(op - 7, h);
  168. op[-6] = *op;
  169. r_set_size(op - 6, h * raster);
  170. pop(6);
  171. return 0;
  172. }
  173. /* <int> .getdevice <device> */
  174. private int
  175. zgetdevice(i_ctx_t *i_ctx_p)
  176. {
  177. os_ptr op = osp;
  178. const gx_device *dev;
  179. check_type(*op, t_integer);
  180. if (op->value.intval != (int)(op->value.intval))
  181. return_error(e_rangecheck); /* won't fit in an int */
  182. dev = gs_getdevice((int)(op->value.intval));
  183. if (dev == 0) /* index out of range */
  184. return_error(e_rangecheck);
  185. /* Device prototypes are read-only; */
  186. /* the cast is logically unnecessary. */
  187. make_tav(op, t_device, avm_foreign | a_readonly, pdevice,
  188. (gx_device *) dev);
  189. return 0;
  190. }
  191. /* Common functionality of zgethardwareparms & zgetdeviceparams */
  192. private int
  193. zget_device_params(i_ctx_t *i_ctx_p, bool is_hardware)
  194. {
  195. os_ptr op = osp;
  196. ref rkeys;
  197. gx_device *dev;
  198. stack_param_list list;
  199. int code;
  200. ref *pmark;
  201. check_read_type(op[-1], t_device);
  202. rkeys = *op;
  203. dev = op[-1].value.pdevice;
  204. pop(1);
  205. stack_param_list_write(&list, &o_stack, &rkeys, iimemory);
  206. code = gs_get_device_or_hardware_params(dev, (gs_param_list *) & list,
  207. is_hardware);
  208. if (code < 0) {
  209. /* We have to put back the top argument. */
  210. if (list.count > 0)
  211. ref_stack_pop(&o_stack, list.count * 2 - 1);
  212. else
  213. ref_stack_push(&o_stack, 1);
  214. *osp = rkeys;
  215. return code;
  216. }
  217. pmark = ref_stack_index(&o_stack, list.count * 2);
  218. make_mark(pmark);
  219. return 0;
  220. }
  221. /* <device> <key_dict|null> .getdeviceparams <mark> <name> <value> ... */
  222. private int
  223. zgetdeviceparams(i_ctx_t *i_ctx_p)
  224. {
  225. return zget_device_params(i_ctx_p, false);
  226. }
  227. /* <device> <key_dict|null> .gethardwareparams <mark> <name> <value> ... */
  228. private int
  229. zgethardwareparams(i_ctx_t *i_ctx_p)
  230. {
  231. return zget_device_params(i_ctx_p, true);
  232. }
  233. /* <matrix> <width> <height> <palette> <word?> makewordimagedevice <device> */
  234. private int
  235. zmakewordimagedevice(i_ctx_t *i_ctx_p)
  236. {
  237. os_ptr op = osp;
  238. os_ptr op1 = op - 1;
  239. gs_matrix imat;
  240. gx_device *new_dev;
  241. const byte *colors;
  242. int colors_size;
  243. int code;
  244. check_int_leu(op[-3], max_uint >> 1); /* width */
  245. check_int_leu(op[-2], max_uint >> 1); /* height */
  246. check_type(*op, t_boolean);
  247. if (r_has_type(op1, t_null)) { /* true color */
  248. colors = 0;
  249. colors_size = -24; /* 24-bit true color */
  250. } else if (r_has_type(op1, t_integer)) {
  251. /*
  252. * We use if/else rather than switch because the value is long,
  253. * which is not supported as a switch value in pre-ANSI C.
  254. */
  255. if (op1->value.intval != 16 && op1->value.intval != 24 &&
  256. op1->value.intval != 32
  257. )
  258. return_error(e_rangecheck);
  259. colors = 0;
  260. colors_size = -op1->value.intval;
  261. } else {
  262. check_type(*op1, t_string); /* palette */
  263. if (r_size(op1) > 3 * 256)
  264. return_error(e_rangecheck);
  265. colors = op1->value.bytes;
  266. colors_size = r_size(op1);
  267. }
  268. if ((code = read_matrix(op - 4, &imat)) < 0)
  269. return code;
  270. /* Everything OK, create device */
  271. code = gs_makewordimagedevice(&new_dev, &imat,
  272. (int)op[-3].value.intval,
  273. (int)op[-2].value.intval,
  274. colors, colors_size,
  275. op->value.boolval, true, imemory);
  276. if (code == 0) {
  277. new_dev->memory = imemory;
  278. make_tav(op - 4, t_device, imemory_space(iimemory) | a_all,
  279. pdevice, new_dev);
  280. pop(4);
  281. }
  282. return code;
  283. }
  284. /* - nulldevice - */
  285. /* Note that nulldevice clears the current pagedevice. */
  286. private int
  287. znulldevice(i_ctx_t *i_ctx_p)
  288. {
  289. gs_nulldevice(igs);
  290. clear_pagedevice(istate);
  291. return 0;
  292. }
  293. /* <num_copies> <flush_bool> .outputpage - */
  294. private int
  295. zoutputpage(i_ctx_t *i_ctx_p)
  296. {
  297. os_ptr op = osp;
  298. int code;
  299. check_type(op[-1], t_integer);
  300. check_type(*op, t_boolean);
  301. code = gs_output_page(igs, (int)op[-1].value.intval,
  302. op->value.boolval);
  303. if (code < 0)
  304. return code;
  305. pop(2);
  306. return 0;
  307. }
  308. /* <device> <policy_dict|null> <require_all> <mark> <name> <value> ... */
  309. /* .putdeviceparams */
  310. /* (on success) <device> <eraseflag> */
  311. /* (on failure) <device> <policy_dict|null> <require_all> <mark> */
  312. /* <name1> <error1> ... */
  313. /* For a key that simply was not recognized, if require_all is true, */
  314. /* the result will be an /undefined error; if require_all is false, */
  315. /* the key will be ignored. */
  316. /* Note that .putdeviceparams clears the current pagedevice. */
  317. private int
  318. zputdeviceparams(i_ctx_t *i_ctx_p)
  319. {
  320. uint count = ref_stack_counttomark(&o_stack);
  321. ref *prequire_all;
  322. ref *ppolicy;
  323. ref *pdev;
  324. gx_device *dev;
  325. stack_param_list list;
  326. int code;
  327. int old_width, old_height;
  328. int i, dest;
  329. if (count == 0)
  330. return_error(e_unmatchedmark);
  331. prequire_all = ref_stack_index(&o_stack, count);
  332. ppolicy = ref_stack_index(&o_stack, count + 1);
  333. pdev = ref_stack_index(&o_stack, count + 2);
  334. if (pdev == 0)
  335. return_error(e_stackunderflow);
  336. check_type_only(*prequire_all, t_boolean);
  337. check_write_type_only(*pdev, t_device);
  338. dev = pdev->value.pdevice;
  339. code = stack_param_list_read(&list, &o_stack, 0, ppolicy,
  340. prequire_all->value.boolval, iimemory);
  341. if (code < 0)
  342. return code;
  343. old_width = dev->width;
  344. old_height = dev->height;
  345. code = gs_putdeviceparams(dev, (gs_param_list *) & list);
  346. /* Check for names that were undefined or caused errors. */
  347. for (dest = count - 2, i = 0; i < count >> 1; i++)
  348. if (list.results[i] < 0) {
  349. *ref_stack_index(&o_stack, dest) =
  350. *ref_stack_index(&o_stack, count - (i << 1) - 2);
  351. gs_errorname(i_ctx_p, list.results[i],
  352. ref_stack_index(&o_stack, dest - 1));
  353. dest -= 2;
  354. }
  355. iparam_list_release(&list);
  356. if (code < 0) { /* There were errors reported. */
  357. ref_stack_pop(&o_stack, dest + 1);
  358. return 0;
  359. }
  360. if (code > 0 || (code == 0 && (dev->width != old_width || dev->height != old_height))) {
  361. /*
  362. * The device was open and is now closed, or its dimensions have
  363. * changed. If it was the current device, call setdevice to
  364. * reinstall it and erase the page.
  365. */
  366. /****** DOESN'T FIND ALL THE GSTATES THAT REFERENCE THE DEVICE. ******/
  367. if (gs_currentdevice(igs) == dev) {
  368. bool was_open = dev->is_open;
  369. code = gs_setdevice_no_erase(igs, dev);
  370. /* If the device wasn't closed, setdevice won't erase the page. */
  371. if (was_open && code >= 0)
  372. code = 1;
  373. }
  374. }
  375. if (code < 0)
  376. return code;
  377. ref_stack_pop(&o_stack, count + 1);
  378. make_bool(osp, code);
  379. clear_pagedevice(istate);
  380. return 0;
  381. }
  382. /* <device> .setdevice <eraseflag> */
  383. /* Note that .setdevice clears the current pagedevice. */
  384. int
  385. zsetdevice(i_ctx_t *i_ctx_p)
  386. {
  387. gx_device *dev = gs_currentdevice(igs);
  388. os_ptr op = osp;
  389. int code;
  390. check_write_type(*op, t_device);
  391. if (dev->LockSafetyParams) { /* do additional checking if locked */
  392. if(op->value.pdevice != dev) /* don't allow a different device */
  393. return_error(e_invalidaccess);
  394. }
  395. code = gs_setdevice_no_erase(igs, op->value.pdevice);
  396. if (code < 0)
  397. return code;
  398. make_bool(op, code != 0); /* erase page if 1 */
  399. clear_pagedevice(istate);
  400. return code;
  401. }
  402. /* ------ Initialization procedure ------ */
  403. const op_def zdevice_op_defs[] =
  404. {
  405. {"1.copydevice2", zcopydevice2},
  406. {"0currentdevice", zcurrentdevice},
  407. {"1.devicename", zdevicename},
  408. {"0.doneshowpage", zdoneshowpage},
  409. {"0flushpage", zflushpage},
  410. {"7.getbitsrect", zgetbitsrect},
  411. {"1.getdevice", zgetdevice},
  412. {"2.getdeviceparams", zgetdeviceparams},
  413. {"2.gethardwareparams", zgethardwareparams},
  414. {"5makewordimagedevice", zmakewordimagedevice},
  415. {"0nulldevice", znulldevice},
  416. {"2.outputpage", zoutputpage},
  417. {"3.putdeviceparams", zputdeviceparams},
  418. {"1.setdevice", zsetdevice},
  419. op_def_end(0)
  420. };