readimage.c 2.8 KB

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