unloadimage.c 1000 B

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