unloadimage.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "lib9.h"
  2. #include "draw.h"
  3. #include "kernel.h"
  4. #include "interp.h"
  5. int
  6. unloadimage(Image *i, Rectangle r, uchar *data, int ndata)
  7. {
  8. int bpl, n, ntot, dy;
  9. uchar *a;
  10. Display *d;
  11. if(!rectinrect(r, i->r)){
  12. kwerrstr("unloadimage: bad rectangle");
  13. return -1;
  14. }
  15. bpl = bytesperline(r, i->depth);
  16. if(ndata < bpl*Dy(r)){
  17. kwerrstr("unloadimage: buffer too small");
  18. return -1;
  19. }
  20. d = i->display;
  21. flushimage(d, 0); /* make sure subsequent flush is for us only */
  22. ntot = 0;
  23. while(r.min.y < r.max.y){
  24. a = bufimage(d, 1+4+4*4);
  25. if(a == 0){
  26. kwerrstr("unloadimage: %r");
  27. return -1;
  28. }
  29. dy = 8000/bpl;
  30. if(dy <= 0){
  31. kwerrstr("unloadimage: image too wide");
  32. return -1;
  33. }
  34. if(dy > Dy(r))
  35. dy = Dy(r);
  36. a[0] = 'r';
  37. BPLONG(a+1, i->id);
  38. BPLONG(a+5, r.min.x);
  39. BPLONG(a+9, r.min.y);
  40. BPLONG(a+13, r.max.x);
  41. BPLONG(a+17, r.min.y+dy);
  42. if(flushimage(d, 0) < 0)
  43. return -1;
  44. if(d->local == 0)
  45. release();
  46. n = kchanio(d->datachan, data+ntot, ndata-ntot, OREAD);
  47. if(d->local == 0)
  48. acquire();
  49. if(n < 0)
  50. return n;
  51. ntot += n;
  52. r.min.y += dy;
  53. }
  54. return ntot;
  55. }