creadimage.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. Image *
  13. creadimage(Display *d, int fd, int dolock)
  14. {
  15. char hdr[5*12+1];
  16. Rectangle r;
  17. int m, nb, miny, maxy, new, ldepth, ncblock;
  18. uint8_t *buf, *a;
  19. Image *i;
  20. uint32_t chan;
  21. if(readn(fd, hdr, 5*12) != 5*12)
  22. return nil;
  23. /*
  24. * distinguish new channel descriptor from old ldepth.
  25. * channel descriptors have letters as well as numbers,
  26. * while ldepths are a single digit formatted as %-11d.
  27. */
  28. new = 0;
  29. for(m=0; m<10; m++){
  30. if(hdr[m] != ' '){
  31. new = 1;
  32. break;
  33. }
  34. }
  35. if(hdr[11] != ' '){
  36. werrstr("creadimage: bad format");
  37. return nil;
  38. }
  39. if(new){
  40. hdr[11] = '\0';
  41. if((chan = strtochan(hdr)) == 0){
  42. werrstr("creadimage: bad channel string %s", hdr);
  43. return nil;
  44. }
  45. }else{
  46. ldepth = ((int)hdr[10])-'0';
  47. if(ldepth<0 || ldepth>3){
  48. werrstr("creadimage: bad ldepth %d", ldepth);
  49. return nil;
  50. }
  51. chan = drawld2chan[ldepth];
  52. }
  53. r.min.x=atoi(hdr+1*12);
  54. r.min.y=atoi(hdr+2*12);
  55. r.max.x=atoi(hdr+3*12);
  56. r.max.y=atoi(hdr+4*12);
  57. if(r.min.x>r.max.x || r.min.y>r.max.y){
  58. werrstr("creadimage: bad rectangle");
  59. return nil;
  60. }
  61. if(d){
  62. if(dolock)
  63. lockdisplay(d);
  64. i = allocimage(d, r, chan, 0, 0);
  65. setmalloctag(i, getcallerpc(&d));
  66. if(dolock)
  67. unlockdisplay(d);
  68. if(i == nil)
  69. return nil;
  70. }else{
  71. i = mallocz(sizeof(Image), 1);
  72. if(i == nil)
  73. return nil;
  74. }
  75. ncblock = _compblocksize(r, chantodepth(chan));
  76. buf = malloc(ncblock);
  77. if(buf == nil)
  78. goto Errout;
  79. miny = r.min.y;
  80. while(miny != r.max.y){
  81. if(readn(fd, hdr, 2*12) != 2*12){
  82. Errout:
  83. if(dolock)
  84. lockdisplay(d);
  85. Erroutlock:
  86. freeimage(i);
  87. if(dolock)
  88. unlockdisplay(d);
  89. free(buf);
  90. return nil;
  91. }
  92. maxy = atoi(hdr+0*12);
  93. nb = atoi(hdr+1*12);
  94. if(maxy<=miny || r.max.y<maxy){
  95. werrstr("creadimage: bad maxy %d", maxy);
  96. goto Errout;
  97. }
  98. if(nb<=0 || ncblock<nb){
  99. werrstr("creadimage: bad count %d", nb);
  100. goto Errout;
  101. }
  102. if(readn(fd, buf, nb)!=nb)
  103. goto Errout;
  104. if(d){
  105. if(dolock)
  106. lockdisplay(d);
  107. a = bufimage(i->display, 21+nb);
  108. if(a == nil)
  109. goto Erroutlock;
  110. a[0] = 'Y';
  111. BPLONG(a+1, i->id);
  112. BPLONG(a+5, r.min.x);
  113. BPLONG(a+9, miny);
  114. BPLONG(a+13, r.max.x);
  115. BPLONG(a+17, maxy);
  116. if(!new) /* old image: flip the data bits */
  117. _twiddlecompressed(buf, nb);
  118. memmove(a+21, buf, nb);
  119. if(dolock)
  120. unlockdisplay(d);
  121. }
  122. miny = maxy;
  123. }
  124. free(buf);
  125. return i;
  126. }