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 / ZPAINT.C < prev    next >
C/C++ Source or Header  |  1992-06-18  |  9KB  |  316 lines

  1. /* Copyright (C) 1989, 1990, 1991 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. /* zpaint.c */
  21. /* Painting operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "estack.h"            /* for image[mask] */
  27. #include "store.h"
  28. #include "gsmatrix.h"
  29. #include "gspaint.h"
  30. #include "state.h"
  31. #include "stream.h"
  32.  
  33. /* Forward references */
  34. /* zimage_setup is used by zimage2.c */
  35. int zimage_setup(P9(int width, int height, gs_matrix *pmat,
  36.   ref *sources, int bits_per_component, int num_components, int masked,
  37.   const float *decode, int npop));
  38. private int image_opaque_setup(P3(os_ptr, int, int));
  39. private int image_setup(P6(os_ptr, int, int, int, const float *, int));
  40. private int image_read(P2(ref *, ref *));
  41. private int image_continue(P1(os_ptr));
  42. private int i_image_continue;
  43.  
  44. /* erasepage */
  45. int
  46. zerasepage(register os_ptr op)
  47. {    return gs_erasepage(igs);
  48. }
  49.  
  50. /* fill */
  51. int
  52. zfill(register os_ptr op)
  53. {    return gs_fill(igs);
  54. }
  55.  
  56. /* eofill */
  57. int
  58. zeofill(register os_ptr op)
  59. {    return gs_eofill(igs);
  60. }
  61.  
  62. /* stroke */
  63. int
  64. zstroke(register os_ptr op)
  65. {    return gs_stroke(igs);
  66. }
  67.  
  68. /* Standard decoding maps for images. */
  69. static const float decode_01[2] = { 0.0, 1.0 };
  70. static const float decode_10[2] = { 1.0, 0.0 };
  71.  
  72. /* colorimage */
  73. int
  74. zcolorimage(register os_ptr op)
  75. {    int spp;            /* samples per pixel */
  76.     int npop = 7;
  77.     os_ptr procp = op - 2;
  78.     check_type(*op, t_integer);    /* ncolors */
  79.     check_type(op[-1], t_boolean);    /* multiproc */
  80.     if ( (ulong)(op->value.intval) > 4 ) return e_rangecheck;
  81.     switch ( (spp = (int)(op->value.intval)) )
  82.       {
  83.       case 1:
  84.         break;
  85.       case 3: case 4:
  86.         if ( op[-1].value.index )    /* planar format */
  87.           npop += spp - 1,
  88.           procp -= spp - 1,
  89.           spp = - spp;
  90.         break;
  91.       default:
  92.         return e_rangecheck;
  93.       }
  94.     return image_opaque_setup(procp, spp, npop);
  95. }
  96.  
  97. /* image */
  98. int
  99. zimage(register os_ptr op)
  100. {    return image_opaque_setup(op, 1, 5);
  101. }
  102.  
  103. /* imagemask */
  104. int
  105. zimagemask(register os_ptr op)
  106. {    check_type(op[-2], t_boolean);
  107.     return image_setup(op, 1, 1, 1,
  108.                (op[-2].value.index ? decode_01 : decode_10), 5);
  109. }
  110.  
  111. /* Common setup for image and colorimage. */
  112. private int
  113. image_opaque_setup(register os_ptr op, int spp, int npop)
  114. {    check_type(op[-2], t_integer);    /* bits/sample */
  115.     if ( (ulong)(op[-2].value.intval) > 8 ) return e_rangecheck;
  116.     return image_setup(op, (int)op[-2].value.intval, spp, 0, decode_01, npop);
  117. }
  118.  
  119. /* Common setup for [color]image and imagemask. */
  120. private int
  121. image_setup(register os_ptr op, int bps, int spp, int masked,
  122.   const float *decode, int npop)
  123. {    gs_matrix mat;
  124.     int code;
  125.     check_type(op[-4], t_integer);    /* width */
  126.     check_type(op[-3], t_integer);    /* height */
  127.     if ( op[-4].value.intval <= 0 || op[-3].value.intval < 0 )
  128.         return e_rangecheck;
  129.     if ( (code = read_matrix(op - 1, &mat)) < 0 )
  130.         return code;
  131.     return zimage_setup((int)op[-4].value.intval,
  132.                 (int)op[-3].value.intval,
  133.                 &mat, op, bps, spp, masked, decode, npop);
  134. }
  135.  
  136. /* Common setup for Level 1 image/imagemask/colorimage and */
  137. /* the Level 2 dictionary form of image/imagemask. */
  138. int
  139. zimage_setup(int width, int height, gs_matrix *pmat,
  140.   ref *sources, int bits_per_component,
  141.   int num_components /* negated if multiple data sources */,
  142.   int masked, const float *decode, int npop)
  143. {    int code;
  144.     gs_image_enum *penum;
  145.     int px;
  146.     ref *pp;
  147.     int num_sources = (num_components < 0 ? - num_components : 1);
  148.     ref source1;
  149.     /* We push on the estack: */
  150.     /*    Control mark, 4 procs, last plane index, */
  151.     /*    enumeration structure (as bytes). */
  152. #define inumpush 7
  153.     check_estack(inumpush + 2);    /* stuff above, + continuation + proc */
  154.     /* Note that the "procedures" might not be procedures, */
  155.     /* but might be strings or files. */
  156.     for ( px = 0, pp = sources; px < num_sources; px++, pp++ )
  157.     {    switch ( r_type(pp) )
  158.         {
  159.         case t_string:
  160.         case t_file:
  161.             check_read(*pp);
  162.             break;
  163.         default:
  164.             check_proc(*pp);
  165.         }
  166.     }
  167.     if ( height == 0 ) return 0;    /* empty image */
  168.     if ( masked )
  169.     {    /* Make sure decode is 0..1 or 1..0 */
  170.         if ( decode[0] == 0.0 && decode[1] == 1.0 )
  171.             masked = 1;
  172.         else if ( decode[0] == 1.0 && decode[1] == 0.0 )
  173.             masked = -1;
  174.         else
  175.             return e_rangecheck;
  176.     }
  177.     if ( (penum = (gs_image_enum *)alloc(1, gs_image_enum_sizeof, "image_setup")) == 0 )
  178.         return e_VMerror;
  179.     code = (masked != 0 ?
  180.         gs_imagemask_init(penum, igs, width, height,
  181.                   masked < 0, pmat, 1) :
  182.         gs_image_init(penum, igs, width, height,
  183.                   bits_per_component, num_components,
  184.                   decode, pmat) );
  185.     if ( code < 0 ) return code;
  186.     mark_estack(es_other);
  187.     ++esp;
  188.     for ( px = 0, pp = sources; px < 4; esp++, px++, pp++ )
  189.       if ( px < num_sources )
  190.         *esp = *pp;
  191.       else
  192.         make_null(esp);
  193.     make_int(esp, 0);        /* current plane */
  194.     r_set_size(esp, num_sources);
  195.     ++esp;
  196.     make_tasv(esp, t_string, 0, gs_image_enum_sizeof, bytes, (byte *)penum);
  197.     code = image_read(sources, &source1);
  198.     switch ( code )
  199.     {
  200.     case 1:            /* string or file */
  201.         pop(npop - 1);
  202.         *osp = source1;
  203.         return image_continue(osp);
  204.     case 0:            /* procedure */
  205.         pop(npop);
  206.         return o_push_estack;
  207.     default:
  208.         return code;
  209.     }
  210. }
  211. /* Continuation procedure.  Hand the string to the enumerator. */
  212. private int
  213. image_continue(register os_ptr op)
  214. {    gs_image_enum *penum = (gs_image_enum *)esp->value.bytes;
  215.     int code;
  216.     if ( !r_has_type(op, t_string) )
  217.        {    /* Procedure didn't return a string.  Quit. */
  218.         esp -= inumpush;
  219.         alloc_free((char *)penum, 1, gs_image_enum_sizeof,
  220.                "image_continue(quit)");
  221.         return e_typecheck;
  222.        }
  223. top:    code = gs_image_next(penum, op->value.bytes, r_size(op));
  224.     if ( r_size(op) == 0 || code != 0 )    /* stop now */
  225.        {    esp -= inumpush;
  226.         alloc_free((char *)penum, 1, gs_image_enum_sizeof,
  227.                "image_continue(finished)");
  228.         if ( code < 0 ) return code;
  229.         code = o_pop_estack;
  230.        }
  231.     else
  232.        {    int px = (int)++(esp[-1].value.intval);
  233.         es_ptr pproc = esp - 5;
  234.         if ( px == r_size(esp - 1) )
  235.             esp[-1].value.intval = px = 0;
  236.         code = image_read(pproc + px, op);
  237.         switch ( code )
  238.         {
  239.         case 1:            /* string or file */
  240.             goto top;
  241.         case 0:            /* procedure */
  242.             code = o_push_estack;
  243.             break;
  244.         default:
  245.             return code;
  246.         }
  247.        }
  248.     pop(1);
  249.     return code;
  250. }
  251. /* Prepare to read from an image data source. */
  252. /* Return 1 for a string (and store the string at *psrc), */
  253. /* 0 for a procedure (push on the estack), <0 for error. */
  254. private int
  255. image_read(ref *psref, ref *psrc)
  256. {    switch ( r_type(psref) )
  257.     {
  258.     case t_string:
  259.         *psrc = *psref;
  260.         return 1;
  261.     case t_file:
  262.     {    stream *s = psref->value.pfile;
  263.         int avail;
  264.         while ( (avail = sbufavailable(s)) == 0 )
  265.         {    int next = sgetc(s);
  266.             if ( next == EOFC )        /* end of data, */
  267.                         /* break with avail == 0 */
  268.                 break;
  269.             if ( next == ERRC )
  270.                 return e_ioerror;
  271.             sputback(s);
  272.         }
  273.         make_tasv(psrc, t_string, a_readonly, avail, bytes, sbufptr(s));
  274.         sskip(s, avail);
  275.     }    return 1;
  276.     default:
  277.         push_op_estack(image_continue, i_image_continue);
  278.         *++esp = *psref;
  279.         return 0;
  280.     }
  281. }
  282.  
  283. /* ------ Ghostscript-specific operators ------ */
  284.  
  285. /* .imagepath */
  286. int
  287. zimagepath(register os_ptr op)
  288. {    int code;
  289.     check_type(op[-2], t_integer);    /* width */
  290.     check_type(op[-1], t_integer);    /* height */
  291.     check_read_type(*op, t_string);    /* data */
  292.     if ( r_size(op) < ((op[-2].value.intval + 7) >> 3) * op[-1].value.intval )
  293.         return e_rangecheck;
  294.     code = gs_imagepath(igs,
  295.         (int)op[-2].value.intval, (int)op[-1].value.intval,
  296.         op->value.const_bytes);
  297.     if ( code == 0 ) pop(3);
  298.     return code;
  299. }
  300.  
  301. /* ------ Initialization procedure ------ */
  302.  
  303. op_def zpaint_op_defs[] = {
  304.     {"0eofill", zeofill},
  305.     {"0erasepage", zerasepage},
  306.     {"0fill", zfill},
  307.     {"7colorimage", zcolorimage},
  308.     {"5image", zimage},
  309.     {"5imagemask", zimagemask},
  310.     {"3.imagepath", zimagepath},
  311.     {"0stroke", zstroke},
  312.         /* Internal operators */
  313.     {"0%image_continue", image_continue, &i_image_continue},
  314.     op_def_end(0)
  315. };
  316.