loadimage.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "lib9.h"
  2. #include "draw.h"
  3. #include "kernel.h"
  4. int
  5. loadimage(Image *i, Rectangle r, uchar *data, int ndata)
  6. {
  7. long dy;
  8. int n, bpl, roff, dstroff, lskip, llen, y;
  9. uchar *a;
  10. int chunk;
  11. Rectangle dstr;
  12. chunk = i->display->bufsize - 64;
  13. bpl = bytesperline(r, i->depth);
  14. n = bpl*Dy(r);
  15. if(n > ndata){
  16. kwerrstr("loadimage: insufficient data");
  17. return -1;
  18. }
  19. dstr = r;
  20. rectclip(&dstr, i->r);
  21. rectclip(&dstr, i->clipr);
  22. if (!rectinrect(dstr, i->r))
  23. return 0;
  24. roff = (r.min.x*i->depth)>>3;
  25. dstroff = dstr.min.x * i->depth >> 3;
  26. lskip = dstroff - roff;
  27. llen = (dstr.max.x*i->depth + 7 >> 3) - dstroff;
  28. data += (dstr.min.y - r.min.y) * bpl + lskip;
  29. ndata = 0;
  30. while(dstr.max.y > dstr.min.y){
  31. dy = dstr.max.y - dstr.min.y;
  32. if(dy*llen > chunk)
  33. dy = chunk/llen;
  34. if(dy <= 0){
  35. kwerrstr("loadimage: image too wide for buffer");
  36. return -1;
  37. }
  38. n = dy*llen;
  39. a = bufimage(i->display, 21+n);
  40. if(a == nil){
  41. kwerrstr("bufimage failed");
  42. return -1;
  43. }
  44. a[0] = 'y';
  45. BPLONG(a+1, i->id);
  46. BPLONG(a+5, dstr.min.x);
  47. BPLONG(a+9, dstr.min.y);
  48. BPLONG(a+13, dstr.max.x);
  49. BPLONG(a+17, dstr.min.y+dy);
  50. a += 21;
  51. for (y = 0; y < dy; y++) {
  52. memmove(a, data, llen);
  53. a += llen;
  54. ndata += llen;
  55. data += bpl;
  56. }
  57. dstr.min.y += dy;
  58. }
  59. if(flushimage(i->display, 0) < 0)
  60. return -1;
  61. return ndata;
  62. }