home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZMATRIX.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  7KB  |  288 lines

  1. /* Copyright (C) 1989, 1991, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zmatrix.c */
  20. /* Matrix operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "igstate.h"
  25. #include "gsmatrix.h"
  26. #include "gscoord.h"
  27. #include "store.h"
  28.  
  29. /* Forward references */
  30. private int near common_transform(P3(os_ptr,
  31.   int (*)(P4(gs_state *, floatp, floatp, gs_point *)),
  32.   int (*)(P4(floatp, floatp, const gs_matrix *, gs_point *))));
  33.  
  34. /* - initmatrix - */
  35. int
  36. zinitmatrix(os_ptr op)
  37. {    return gs_initmatrix(igs);
  38. }
  39.  
  40. /* <matrix> currentmatrix <matrix> */
  41. int
  42. zcurrentmatrix(register os_ptr op)
  43. {    gs_matrix mat;
  44.     gs_currentmatrix(igs, &mat);
  45.     return write_matrix(op, &mat);
  46. }
  47.  
  48. /* <matrix> setmatrix - */
  49. int
  50. zsetmatrix(register os_ptr op)
  51. {    gs_matrix mat;
  52.     int code = read_matrix(op, &mat);
  53.     if ( code < 0 )
  54.         return code;
  55.     if ( (code = gs_setmatrix(igs, &mat)) < 0 )
  56.         return code;
  57.     pop(1);
  58.     return 0;
  59. }
  60.  
  61. /* <tx> <ty> translate - */
  62. /* <tx> <ty> <matrix> translate <matrix> */
  63. int
  64. ztranslate(register os_ptr op)
  65. {    int code;
  66.     float trans[2];
  67.     if ( (code = num_params(op, 2, trans)) >= 0 )
  68.     {    code = gs_translate(igs, trans[0], trans[1]);
  69.         if ( code < 0 )
  70.             return code;
  71.     }
  72.     else                /* matrix operand */
  73.     {    gs_matrix mat;
  74.         /* The num_params failure might be a stack underflow. */
  75.         check_op(2);
  76.         if ( (code = num_params(op - 1, 2, trans)) < 0 ||
  77.              (code = gs_make_translation(trans[0], trans[1], &mat)) < 0 ||
  78.              (code = write_matrix(op, &mat)) < 0
  79.            )
  80.           {    /* Might be a stack underflow. */
  81.             check_op(3);
  82.             return code;
  83.           }
  84.         op[-2] = *op;
  85.     }
  86.     pop(2);
  87.     return code;
  88. }
  89.  
  90. /* <sx> <sy> scale - */
  91. /* <sx> <sy> <matrix> scale <matrix> */
  92. int
  93. zscale(register os_ptr op)
  94. {    int code;
  95.     float scale[2];
  96.     if ( (code = num_params(op, 2, scale)) >= 0 )
  97.     {    code = gs_scale(igs, scale[0], scale[1]);
  98.         if ( code < 0 )
  99.             return code;
  100.     }
  101.     else                /* matrix operand */
  102.     {    gs_matrix mat;
  103.         /* The num_params failure might be a stack underflow. */
  104.         check_op(2);
  105.         if ( (code = num_params(op - 1, 2, scale)) < 0 ||
  106.              (code = gs_make_scaling(scale[0], scale[1], &mat)) < 0 ||
  107.              (code = write_matrix(op, &mat)) < 0
  108.            )
  109.           {    /* Might be a stack underflow. */
  110.             check_op(3);
  111.             return code;
  112.           }
  113.         op[-2] = *op;
  114.     }
  115.     pop(2);
  116.     return code;
  117. }
  118.  
  119. /* <angle> rotate - */
  120. /* <angle> <matrix> rotate <matrix> */
  121. int
  122. zrotate(register os_ptr op)
  123. {    int code;
  124.     float ang;
  125.     if ( (code = num_params(op, 1, &ang)) >= 0 )
  126.     {    code = gs_rotate(igs, ang);
  127.         if ( code < 0 )
  128.             return code;
  129.     }
  130.     else                /* matrix operand */
  131.     {    gs_matrix mat;
  132.         /* The num_params failure might be a stack underflow. */
  133.         check_op(1);
  134.         if ( (code = num_params(op - 1, 1, &ang)) < 0 ||
  135.              (code = gs_make_rotation(ang, &mat)) < 0 ||
  136.              (code = write_matrix(op, &mat)) < 0
  137.            )
  138.           {    /* Might be a stack underflow. */
  139.             check_op(2);
  140.             return code;
  141.           }
  142.         op[-1] = *op;
  143.     }
  144.     pop(1);
  145.     return code;
  146. }
  147.  
  148. /* <matrix> concat - */
  149. int
  150. zconcat(register os_ptr op)
  151. {    gs_matrix mat;
  152.     int code = read_matrix(op, &mat);
  153.     if ( code < 0 ) return code;
  154.     code = gs_concat(igs, &mat);
  155.     if ( code < 0 ) return code;
  156.     pop(1);
  157.     return 0;
  158. }
  159.  
  160. /* <matrix1> <matrix2> <matrix> concatmatrix <matrix> */
  161. int
  162. zconcatmatrix(register os_ptr op)
  163. {    gs_matrix m1, m2, mp;
  164.     int code;
  165.     if (    (code = read_matrix(op - 2, &m1)) < 0 ||
  166.         (code = read_matrix(op - 1, &m2)) < 0 ||
  167.         (code = gs_matrix_multiply(&m1, &m2, &mp)) < 0 ||
  168.         (code = write_matrix(op, &mp)) < 0
  169.        ) return code;
  170.     op[-2] = *op;
  171.     pop(2);
  172.     return code;
  173. }
  174.  
  175. /* <x> <y> transform <xt> <yt> */
  176. /* <x> <y> <matrix> transform <xt> <yt> */
  177. int
  178. ztransform(register os_ptr op)
  179. {    return common_transform(op, gs_transform, gs_point_transform);
  180. }
  181.  
  182. /* <dx> <dy> dtransform <dxt> <dyt> */
  183. /* <dx> <dy> <matrix> dtransform <dxt> <dyt> */
  184. int
  185. zdtransform(register os_ptr op)
  186. {    return common_transform(op, gs_dtransform, gs_distance_transform);
  187. }
  188.  
  189. /* <xt> <yt> itransform <x> <y> */
  190. /* <xt> <yt> <matrix> itransform <x> <y> */
  191. int
  192. zitransform(register os_ptr op)
  193. {    return common_transform(op, gs_itransform, gs_point_transform_inverse);
  194. }
  195.  
  196. /* <dxt> <dyt> idtransform <dx> <dy> */
  197. /* <dxt> <dyt> <matrix> idtransform <dx> <dy> */
  198. int
  199. zidtransform(register os_ptr op)
  200. {    return common_transform(op, gs_idtransform, gs_distance_transform_inverse);
  201. }
  202.  
  203. /* Common logic for [i][d]transform */
  204. private int near
  205. common_transform(register os_ptr op,
  206.   int (*ptproc)(P4(gs_state *, floatp, floatp, gs_point *)),
  207.   int (*matproc)(P4(floatp, floatp, const gs_matrix *, gs_point *)))
  208. {    float opxy[2];
  209.     gs_point pt;
  210.     int code;
  211.     /* Optimize for the non-matrix case */
  212.     switch ( r_type(op) )
  213.        {
  214.     case t_real:
  215.         opxy[1] = op->value.realval;
  216.         break;
  217.     case t_integer:
  218.         opxy[1] = op->value.intval;
  219.         break;
  220.     case t_array:                /* might be a matrix */
  221.        {    gs_matrix mat;
  222.         gs_matrix *pmat = &mat;
  223.         if (    (code = read_matrix(op, pmat)) < 0 ||
  224.             (code = num_params(op - 1, 2, opxy)) < 0 ||
  225.             (code = (*matproc)(opxy[0], opxy[1], pmat, &pt)) < 0
  226.            )
  227.           {    /* Might be a stack underflow. */
  228.             check_op(3);
  229.             return code;
  230.           }
  231.         op--;
  232.         pop(1);
  233.         goto out;
  234.        }
  235.     default:
  236.         return_error(e_typecheck);
  237.        }
  238.     switch ( r_type(op - 1) )
  239.        {
  240.     case t_real:
  241.         opxy[0] = (op - 1)->value.realval;
  242.         break;
  243.     case t_integer:
  244.         opxy[0] = (op - 1)->value.intval;
  245.         break;
  246.     default:
  247.         return_error(e_typecheck);
  248.        }
  249.     if ( (code = (*ptproc)(igs, opxy[0], opxy[1], &pt)) < 0 )
  250.         return code;
  251. out:    make_real(op - 1, pt.x);
  252.     make_real(op, pt.y);
  253.     return 0;
  254. }
  255.  
  256. /* <matrix> <inv_matrix> invertmatrix <inv_matrix> */
  257. int
  258. zinvertmatrix(register os_ptr op)
  259. {    gs_matrix m;
  260.     int code;
  261.     if (    (code = read_matrix(op - 1, &m)) < 0 ||
  262.         (code = gs_matrix_invert(&m, &m)) < 0 ||
  263.         (code = write_matrix(op, &m)) < 0
  264.        ) return code;
  265.     op[-1] = *op;
  266.     pop(1);
  267.     return code;
  268. }
  269.  
  270. /* ------ Initialization procedure ------ */
  271.  
  272. op_def zmatrix_op_defs[] = {
  273.     {"1concat", zconcat},
  274.     {"2dtransform", zdtransform},
  275.     {"3concatmatrix", zconcatmatrix},
  276.     {"1currentmatrix", zcurrentmatrix},
  277.     {"2idtransform", zidtransform},
  278.     {"0initmatrix", zinitmatrix},
  279.     {"2invertmatrix", zinvertmatrix},
  280.     {"2itransform", zitransform},
  281.     {"1rotate", zrotate},
  282.     {"2scale", zscale},
  283.     {"1setmatrix", zsetmatrix},
  284.     {"2transform", ztransform},
  285.     {"2translate", ztranslate},
  286.     op_def_end(0)
  287. };
  288.