readimage.c 2.4 KB

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