home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ZDEVICE.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  5KB  |  201 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zdevice.c */
  21. /* Device-related operators for GhostScript */
  22. #include "ghost.h"
  23. #include "alloc.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "state.h"
  27. #include "gsmatrix.h"
  28. #include "gsstate.h"
  29. #include "gxdevice.h"
  30. #include "store.h"
  31.  
  32. /* copydevice */
  33. int
  34. zcopydevice(register os_ptr op)
  35. {    gx_device *new_dev;
  36.     int code;
  37.     check_type(*op, t_device);
  38.     code = gs_copydevice(&new_dev, op->value.pdevice, alloc);
  39.     if ( code < 0 ) return code;
  40.     make_tv(op, t_device, pdevice, new_dev);
  41.     return 0;
  42. }
  43.  
  44. /* copyscanlines */
  45. int
  46. zcopyscanlines(register os_ptr op)
  47. {    os_ptr op1 = op - 1;
  48.     os_ptr op2 = op - 2;
  49.     gx_device *dev;
  50.     int code;
  51.     uint bytes_copied;
  52.     check_type(*op2, t_device);
  53.     dev = op2->value.pdevice;
  54.     check_type(*op1, t_integer);
  55.     if ( op1->value.intval < 0 || op1->value.intval > dev->height )
  56.         return e_rangecheck;
  57.     check_write_type(*op, t_string);
  58.     code = gs_copyscanlines(dev, (int)op1->value.intval,
  59.         op->value.bytes, r_size(op), NULL, &bytes_copied);
  60.     if ( code < 0 ) return e_typecheck;    /* not a memory device */
  61.     *op2 = *op;
  62.     r_set_size(op2, bytes_copied);
  63.     pop(2);
  64.     return 0;
  65. }
  66.  
  67. /* currentdevice */
  68. int
  69. zcurrentdevice(register os_ptr op)
  70. {    gx_device *dev = gs_currentdevice(igs);
  71.     push(1);
  72.     make_tv(op, t_device, pdevice, dev);
  73.     return 0;
  74. }
  75.  
  76. /* devicename */
  77. int
  78. zdevicename(register os_ptr op)
  79. {    const char *dname;
  80.     int code;
  81.     check_type(*op, t_device);
  82.     dname = gs_devicename(op->value.pdevice);
  83.     code = string_to_ref(dname, op, "devicename");
  84.     if ( code < 0 ) return code;
  85.     return 0;
  86. }
  87.  
  88. /* deviceinitialmatrix */
  89. int
  90. zdeviceinitialmatrix(register os_ptr op)
  91. {    int code = write_matrix(op);
  92.     if ( code < 0 ) return code;
  93.     check_type(op[-1], t_device);
  94.     gs_deviceinitialmatrix(op[-1].value.pdevice, (gs_matrix *)op->value.refs);
  95.     op[-1] = *op;
  96.     pop(1);
  97.     return 0;
  98. }
  99.  
  100. /* flushpage */
  101. int
  102. zflushpage(register os_ptr op)
  103. {    return gs_flushpage(igs);
  104. }
  105.  
  106. /* getdevice */
  107. int
  108. zgetdevice(register os_ptr op)
  109. {    gx_device *dev;
  110.     check_type(*op, t_integer);
  111.     if ( op->value.intval != (int)(op->value.intval) )
  112.         return e_rangecheck;    /* won't fit in an int */
  113.     dev = gs_getdevice((int)(op->value.intval));
  114.     if ( dev == 0 ) return e_rangecheck;    /* index out of range */
  115.     make_tv(op, t_device, pdevice, dev);
  116.     return 0;
  117. }
  118.  
  119. /* makeimagedevice */
  120. int
  121. zmakeimagedevice(register os_ptr op)
  122. {    gs_matrix imat;
  123.     gx_device *new_dev;
  124.     const byte *colors;
  125.     int num_colors;
  126.     int code;
  127.     static const gs_memory_procs mprocs = { alloc, alloc_free };
  128.     check_type(op[-2], t_integer);    /* width */
  129.     check_type(op[-1], t_integer);    /* height */
  130.     if ( r_has_type(op, t_null) )    /* true color */
  131.        {    colors = 0;
  132.         num_colors = -24;    /* 24-bit true color */
  133.        }
  134.     else
  135.        {    check_type(*op, t_string);    /* palette */
  136.         if ( r_size(op) > 3*256 ) return e_rangecheck;
  137.         colors = op->value.bytes;
  138.         num_colors = r_size(op);
  139.        }
  140.     if (    (ulong)(op[-2].value.intval) > max_uint >> 1 ||
  141.         (ulong)(op[-1].value.intval) > max_uint >> 1
  142.        ) return e_rangecheck;
  143.     if ( (code = read_matrix(op - 3, &imat)) < 0 ) return code;
  144.     /* Everything OK, create device */
  145.     code = gs_makeimagedevice(&new_dev, &imat,
  146.                   (int)op[-2].value.intval,
  147.                   (int)op[-1].value.intval,
  148.                   colors, num_colors, &mprocs);
  149.     if ( code == 0 )
  150.        {    make_tv(op - 3, t_device, pdevice, new_dev);
  151.         pop(3);
  152.        }
  153.     return code;
  154. }
  155.  
  156. /* nulldevice */
  157. int
  158. znulldevice(register os_ptr op)
  159. {    gs_nulldevice(igs);
  160.     return 0;
  161. }
  162.  
  163. /* .outputpage */
  164. int
  165. zoutputpage(register os_ptr op)
  166. {    int code;
  167.     check_type(op[-1], t_integer);
  168.     check_type(*op, t_boolean);
  169.     code = gs_output_page(igs, (int)op[-1].value.intval, op->value.index);
  170.     if ( code < 0 ) return code;
  171.     pop(2);
  172.     return 0;
  173. }
  174.  
  175. /* setdevice */
  176. int
  177. zsetdevice(register os_ptr op)
  178. {    int code;
  179.     check_type(*op, t_device);
  180.     code = gs_setdevice(igs, op->value.pdevice);
  181.     if ( code == 0 ) pop(1);
  182.     return code;
  183. }
  184.  
  185. /* ------ Initialization procedure ------ */
  186.  
  187. op_def zdevice_op_defs[] = {
  188.     {"copydevice", zcopydevice},
  189.     {"3copyscanlines", zcopyscanlines},
  190.     {"0currentdevice", zcurrentdevice},
  191.     {"2deviceinitialmatrix", zdeviceinitialmatrix},
  192.     {"1devicename", zdevicename},
  193.     {"0flushpage", zflushpage},
  194.     {"1getdevice", zgetdevice},
  195.     {"4makeimagedevice", zmakeimagedevice},
  196.     {"0nulldevice", znulldevice},
  197.     {"2.outputpage", zoutputpage},
  198.     {"1setdevice", zsetdevice},
  199.     op_def_end(0)
  200. };
  201.