unloadimage.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. int
  13. unloadimage(Image *i, Rectangle r, uint8_t *data, int ndata)
  14. {
  15. int bpl, n, ntot, dy;
  16. uint8_t *a;
  17. Display *d;
  18. if(!rectinrect(r, i->r)){
  19. werrstr("unloadimage: bad rectangle");
  20. return -1;
  21. }
  22. bpl = bytesperline(r, i->depth);
  23. if(ndata < bpl*Dy(r)){
  24. werrstr("unloadimage: buffer too small");
  25. return -1;
  26. }
  27. d = i->display;
  28. flushimage(d, 0); /* make sure subsequent flush is for us only */
  29. ntot = 0;
  30. while(r.min.y < r.max.y){
  31. a = bufimage(d, 1+4+4*4);
  32. if(a == 0){
  33. werrstr("unloadimage: %r");
  34. return -1;
  35. }
  36. dy = 8000/bpl;
  37. if(dy <= 0){
  38. werrstr("unloadimage: image too wide");
  39. return -1;
  40. }
  41. if(dy > Dy(r))
  42. dy = Dy(r);
  43. a[0] = 'r';
  44. BPLONG(a+1, i->id);
  45. BPLONG(a+5, r.min.x);
  46. BPLONG(a+9, r.min.y);
  47. BPLONG(a+13, r.max.x);
  48. BPLONG(a+17, r.min.y+dy);
  49. if(flushimage(d, 0) < 0)
  50. return -1;
  51. n = read(d->fd, data+ntot, ndata-ntot);
  52. if(n < 0)
  53. return n;
  54. ntot += n;
  55. r.min.y += dy;
  56. }
  57. return ntot;
  58. }