cloadimage.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. cloadimage(Image *i, Rectangle r, uint8_t *data, int ndata)
  14. {
  15. int m, nb, miny, maxy, ncblock;
  16. uint8_t *a;
  17. if(!rectinrect(r, i->r)){
  18. werrstr("cloadimage: bad rectangle");
  19. return -1;
  20. }
  21. miny = r.min.y;
  22. m = 0;
  23. ncblock = _compblocksize(r, i->depth);
  24. while(miny != r.max.y){
  25. maxy = atoi((char*)data+0*12);
  26. nb = atoi((char*)data+1*12);
  27. if(maxy<=miny || r.max.y<maxy){
  28. werrstr("creadimage: bad maxy %d", maxy);
  29. return -1;
  30. }
  31. data += 2*12;
  32. ndata -= 2*12;
  33. m += 2*12;
  34. if(nb<=0 || ncblock<nb || nb>ndata){
  35. werrstr("creadimage: bad count %d", nb);
  36. return -1;
  37. }
  38. a = bufimage(i->display, 21+nb);
  39. if(a == nil)
  40. return -1;
  41. a[0] = 'Y';
  42. BPLONG(a+1, i->id);
  43. BPLONG(a+5, r.min.x);
  44. BPLONG(a+9, miny);
  45. BPLONG(a+13, r.max.x);
  46. BPLONG(a+17, maxy);
  47. memmove(a+21, data, nb);
  48. miny = maxy;
  49. data += nb;
  50. ndata += nb;
  51. m += nb;
  52. }
  53. return m;
  54. }