readimage.c 2.2 KB

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