cloadimage.c 933 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. int
  5. cloadimage(Image *i, Rectangle r, uchar *data, int ndata)
  6. {
  7. int m, nb, miny, maxy, ncblock;
  8. uchar *a;
  9. if(!rectinrect(r, i->r)){
  10. werrstr("cloadimage: bad rectangle");
  11. return -1;
  12. }
  13. miny = r.min.y;
  14. m = 0;
  15. ncblock = _compblocksize(r, i->depth);
  16. while(miny != r.max.y){
  17. maxy = atoi((char*)data+0*12);
  18. nb = atoi((char*)data+1*12);
  19. if(maxy<=miny || r.max.y<maxy){
  20. werrstr("creadimage: bad maxy %d", maxy);
  21. return -1;
  22. }
  23. data += 2*12;
  24. ndata -= 2*12;
  25. m += 2*12;
  26. if(nb<=0 || ncblock<nb || nb>ndata){
  27. werrstr("creadimage: bad count %d", nb);
  28. return -1;
  29. }
  30. a = bufimage(i->display, 21+nb);
  31. if(a == nil)
  32. return -1;
  33. a[0] = 'Y';
  34. BPLONG(a+1, i->id);
  35. BPLONG(a+5, r.min.x);
  36. BPLONG(a+9, miny);
  37. BPLONG(a+13, r.max.x);
  38. BPLONG(a+17, maxy);
  39. memmove(a+21, data, nb);
  40. miny = maxy;
  41. data += nb;
  42. ndata += nb;
  43. m += nb;
  44. }
  45. return m;
  46. }