loadimage.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. loadimage(Image *i, Rectangle r, uint8_t *data, int ndata)
  14. {
  15. int32_t dy;
  16. int n, bpl;
  17. uint8_t *a;
  18. int chunk;
  19. chunk = i->display->bufsize - 64;
  20. if(!rectinrect(r, i->r)){
  21. werrstr("loadimage: bad rectangle");
  22. return -1;
  23. }
  24. bpl = bytesperline(r, i->depth);
  25. n = bpl*Dy(r);
  26. if(n > ndata){
  27. werrstr("loadimage: insufficient data");
  28. return -1;
  29. }
  30. ndata = 0;
  31. while(r.max.y > r.min.y){
  32. dy = r.max.y - r.min.y;
  33. if(dy*bpl > chunk)
  34. dy = chunk/bpl;
  35. if(dy <= 0){
  36. werrstr("loadimage: image too wide for buffer");
  37. return -1;
  38. }
  39. n = dy*bpl;
  40. a = bufimage(i->display, 21+n);
  41. if(a == nil){
  42. werrstr("bufimage failed");
  43. return -1;
  44. }
  45. a[0] = 'y';
  46. BPLONG(a+1, i->id);
  47. BPLONG(a+5, r.min.x);
  48. BPLONG(a+9, r.min.y);
  49. BPLONG(a+13, r.max.x);
  50. BPLONG(a+17, r.min.y+dy);
  51. memmove(a+21, data, n);
  52. ndata += n;
  53. data += n;
  54. r.min.y += dy;
  55. }
  56. if(flushimage(i->display, 0) < 0)
  57. return -1;
  58. return ndata;
  59. }