read.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <memdraw.h>
  13. Memimage*
  14. readmemimage(int fd)
  15. {
  16. char hdr[5*12+1];
  17. int dy;
  18. uint32_t chan;
  19. uint l, n;
  20. int m, j;
  21. int new, miny, maxy;
  22. Rectangle r;
  23. uint8_t *tmp;
  24. int ldepth, chunk;
  25. Memimage *i;
  26. if(readn(fd, hdr, 11) != 11){
  27. werrstr("readimage: short header");
  28. return nil;
  29. }
  30. if(memcmp(hdr, "compressed\n", 11) == 0)
  31. return creadmemimage(fd);
  32. if(readn(fd, hdr+11, 5*12-11) != 5*12-11){
  33. werrstr("readimage: short header (2)");
  34. return nil;
  35. }
  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. i = allocmemimage(r, chan);
  78. if(i == nil)
  79. return nil;
  80. chunk = 32*1024;
  81. if(chunk < l)
  82. chunk = l;
  83. tmp = malloc(chunk);
  84. if(tmp == nil)
  85. goto Err;
  86. while(maxy > miny){
  87. dy = maxy - miny;
  88. if(dy*l > chunk)
  89. dy = chunk/l;
  90. if(dy <= 0){
  91. werrstr("readmemimage: image too wide for buffer");
  92. goto Err;
  93. }
  94. n = dy*l;
  95. m = readn(fd, tmp, n);
  96. if(m != n){
  97. werrstr("readmemimage: read count %d not %d: %r", m, n);
  98. Err:
  99. freememimage(i);
  100. free(tmp);
  101. return nil;
  102. }
  103. if(!new) /* an old image: must flip all the bits */
  104. for(j=0; j<chunk; j++)
  105. tmp[j] ^= 0xFF;
  106. if(loadmemimage(i, Rect(r.min.x, miny, r.max.x, miny+dy), tmp, chunk) <= 0)
  107. goto Err;
  108. miny += dy;
  109. }
  110. free(tmp);
  111. return i;
  112. }