cread.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. creadmemimage(int fd)
  15. {
  16. char hdr[5*12+1];
  17. Rectangle r;
  18. int m, nb, miny, maxy, new, ldepth, ncblock;
  19. uint8_t *buf;
  20. Memimage *i;
  21. uint32_t chan;
  22. if(readn(fd, hdr, 5*12) != 5*12){
  23. werrstr("readmemimage: short header (2)");
  24. return nil;
  25. }
  26. /*
  27. * distinguish new channel descriptor from old ldepth.
  28. * channel descriptors have letters as well as numbers,
  29. * while ldepths are a single digit formatted as %-11d.
  30. */
  31. new = 0;
  32. for(m=0; m<10; m++){
  33. if(hdr[m] != ' '){
  34. new = 1;
  35. break;
  36. }
  37. }
  38. if(hdr[11] != ' '){
  39. werrstr("creadimage: bad format");
  40. return nil;
  41. }
  42. if(new){
  43. hdr[11] = '\0';
  44. if((chan = strtochan(hdr)) == 0){
  45. werrstr("creadimage: bad channel string %s", hdr);
  46. return nil;
  47. }
  48. }else{
  49. ldepth = ((int)hdr[10])-'0';
  50. if(ldepth<0 || ldepth>3){
  51. werrstr("creadimage: bad ldepth %d", ldepth);
  52. return nil;
  53. }
  54. chan = drawld2chan[ldepth];
  55. }
  56. r.min.x=atoi(hdr+1*12);
  57. r.min.y=atoi(hdr+2*12);
  58. r.max.x=atoi(hdr+3*12);
  59. r.max.y=atoi(hdr+4*12);
  60. if(r.min.x>r.max.x || r.min.y>r.max.y){
  61. werrstr("creadimage: bad rectangle");
  62. return nil;
  63. }
  64. i = allocmemimage(r, chan);
  65. if(i == nil)
  66. return nil;
  67. ncblock = _compblocksize(r, i->depth);
  68. buf = malloc(ncblock);
  69. if(buf == nil)
  70. goto Errout;
  71. miny = r.min.y;
  72. while(miny != r.max.y){
  73. if(readn(fd, hdr, 2*12) != 2*12){
  74. Shortread:
  75. werrstr("readmemimage: short read");
  76. Errout:
  77. freememimage(i);
  78. free(buf);
  79. return nil;
  80. }
  81. maxy = atoi(hdr+0*12);
  82. nb = atoi(hdr+1*12);
  83. if(maxy<=miny || r.max.y<maxy){
  84. werrstr("readimage: bad maxy %d", maxy);
  85. goto Errout;
  86. }
  87. if(nb<=0 || ncblock<nb){
  88. werrstr("readimage: bad count %d", nb);
  89. goto Errout;
  90. }
  91. if(readn(fd, buf, nb)!=nb)
  92. goto Shortread;
  93. if(!new) /* old image: flip the data bits */
  94. _twiddlecompressed(buf, nb);
  95. cloadmemimage(i, Rect(r.min.x, miny, r.max.x, maxy), buf, nb);
  96. miny = maxy;
  97. }
  98. free(buf);
  99. return i;
  100. }