poly.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. static
  5. uchar*
  6. addcoord(uchar *p, int oldx, int newx)
  7. {
  8. int dx;
  9. dx = newx-oldx;
  10. /* does dx fit in 7 signed bits? */
  11. if((unsigned)(dx - -0x40) <= 0x7F)
  12. *p++ = dx&0x7F;
  13. else{
  14. *p++ = 0x80 | (newx&0x7F);
  15. *p++ = newx>>7;
  16. *p++ = newx>>15;
  17. }
  18. return p;
  19. }
  20. static
  21. void
  22. dopoly(int cmd, Image *dst, Point *pp, int np, int end0, int end1, int radius, Image *src, Point *sp, Drawop op)
  23. {
  24. uchar *a, *t, *u;
  25. int i, ox, oy;
  26. if(np == 0)
  27. return;
  28. t = malloc(np*2*3);
  29. if(t == nil)
  30. return;
  31. u = t;
  32. ox = oy = 0;
  33. for(i=0; i<np; i++){
  34. u = addcoord(u, ox, pp[i].x);
  35. ox = pp[i].x;
  36. u = addcoord(u, oy, pp[i].y);
  37. oy = pp[i].y;
  38. }
  39. _setdrawop(dst->display, op);
  40. a = bufimage(dst->display, 1+4+2+4+4+4+4+2*4+(u-t));
  41. if(a == 0){
  42. free(t);
  43. fprint(2, "image poly: %r\n");
  44. return;
  45. }
  46. a[0] = cmd;
  47. BPLONG(a+1, dst->id);
  48. BPSHORT(a+5, np-1);
  49. BPLONG(a+7, end0);
  50. BPLONG(a+11, end1);
  51. BPLONG(a+15, radius);
  52. BPLONG(a+19, src->id);
  53. BPLONG(a+23, sp->x);
  54. BPLONG(a+27, sp->y);
  55. memmove(a+31, t, u-t);
  56. free(t);
  57. }
  58. void
  59. poly(Image *dst, Point *p, int np, int end0, int end1, int radius, Image *src, Point sp)
  60. {
  61. dopoly('p', dst, p, np, end0, end1, radius, src, &sp, SoverD);
  62. }
  63. void
  64. polyop(Image *dst, Point *p, int np, int end0, int end1, int radius, Image *src, Point sp, Drawop op)
  65. {
  66. dopoly('p', dst, p, np, end0, end1, radius, src, &sp, op);
  67. }
  68. void
  69. fillpoly(Image *dst, Point *p, int np, int wind, Image *src, Point sp)
  70. {
  71. dopoly('P', dst, p, np, wind, 0, 0, src, &sp, SoverD);
  72. }
  73. void
  74. fillpolyop(Image *dst, Point *p, int np, int wind, Image *src, Point sp, Drawop op)
  75. {
  76. dopoly('P', dst, p, np, wind, 0, 0, src, &sp, op);
  77. }