poly.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. static
  13. uint8_t*
  14. addcoord(uint8_t *p, int oldx, int newx)
  15. {
  16. int dx;
  17. dx = newx-oldx;
  18. /* does dx fit in 7 signed bits? */
  19. if((unsigned)(dx - -0x40) <= 0x7F)
  20. *p++ = dx&0x7F;
  21. else{
  22. *p++ = 0x80 | (newx&0x7F);
  23. *p++ = newx>>7;
  24. *p++ = newx>>15;
  25. }
  26. return p;
  27. }
  28. static
  29. void
  30. dopoly(int cmd, Image *dst, Point *pp, int np, int end0, int end1, int radius, Image *src, Point *sp, Drawop op)
  31. {
  32. uint8_t *a, *t, *u;
  33. int i, ox, oy;
  34. if(np == 0)
  35. return;
  36. t = malloc(np*2*3);
  37. if(t == nil)
  38. return;
  39. u = t;
  40. ox = oy = 0;
  41. for(i=0; i<np; i++){
  42. u = addcoord(u, ox, pp[i].x);
  43. ox = pp[i].x;
  44. u = addcoord(u, oy, pp[i].y);
  45. oy = pp[i].y;
  46. }
  47. _setdrawop(dst->display, op);
  48. a = bufimage(dst->display, 1+4+2+4+4+4+4+2*4+(u-t));
  49. if(a == 0){
  50. free(t);
  51. fprint(2, "image poly: %r\n");
  52. return;
  53. }
  54. a[0] = cmd;
  55. BPLONG(a+1, dst->id);
  56. BPSHORT(a+5, np-1);
  57. BPLONG(a+7, end0);
  58. BPLONG(a+11, end1);
  59. BPLONG(a+15, radius);
  60. BPLONG(a+19, src->id);
  61. BPLONG(a+23, sp->x);
  62. BPLONG(a+27, sp->y);
  63. memmove(a+31, t, u-t);
  64. free(t);
  65. }
  66. void
  67. poly(Image *dst, Point *p, int np, int end0, int end1, int radius, Image *src, Point sp)
  68. {
  69. dopoly('p', dst, p, np, end0, end1, radius, src, &sp, SoverD);
  70. }
  71. void
  72. polyop(Image *dst, Point *p, int np, int end0, int end1, int radius, Image *src, Point sp, Drawop op)
  73. {
  74. dopoly('p', dst, p, np, end0, end1, radius, src, &sp, op);
  75. }
  76. void
  77. fillpoly(Image *dst, Point *p, int np, int wind, Image *src, Point sp)
  78. {
  79. dopoly('P', dst, p, np, wind, 0, 0, src, &sp, SoverD);
  80. }
  81. void
  82. fillpolyop(Image *dst, Point *p, int np, int wind, Image *src, Point sp, Drawop op)
  83. {
  84. dopoly('P', dst, p, np, wind, 0, 0, src, &sp, op);
  85. }