creadimage.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. Image *
  5. creadimage(Display *d, int fd, int dolock)
  6. {
  7. char hdr[5*12+1];
  8. Rectangle r;
  9. int m, nb, miny, maxy, new, ldepth, ncblock;
  10. uchar *buf, *a;
  11. Image *i;
  12. ulong chan;
  13. if(readn(fd, hdr, 5*12) != 5*12)
  14. return nil;
  15. /*
  16. * distinguish new channel descriptor from old ldepth.
  17. * channel descriptors have letters as well as numbers,
  18. * while ldepths are a single digit formatted as %-11d.
  19. */
  20. new = 0;
  21. for(m=0; m<10; m++){
  22. if(hdr[m] != ' '){
  23. new = 1;
  24. break;
  25. }
  26. }
  27. if(hdr[11] != ' '){
  28. werrstr("creadimage: bad format");
  29. return nil;
  30. }
  31. if(new){
  32. hdr[11] = '\0';
  33. if((chan = strtochan(hdr)) == 0){
  34. werrstr("creadimage: bad channel string %s", hdr);
  35. return nil;
  36. }
  37. }else{
  38. ldepth = ((int)hdr[10])-'0';
  39. if(ldepth<0 || ldepth>3){
  40. werrstr("creadimage: bad ldepth %d", ldepth);
  41. return nil;
  42. }
  43. chan = drawld2chan[ldepth];
  44. }
  45. r.min.x=atoi(hdr+1*12);
  46. r.min.y=atoi(hdr+2*12);
  47. r.max.x=atoi(hdr+3*12);
  48. r.max.y=atoi(hdr+4*12);
  49. if(r.min.x>r.max.x || r.min.y>r.max.y){
  50. werrstr("creadimage: bad rectangle");
  51. return nil;
  52. }
  53. if(d){
  54. if(dolock)
  55. lockdisplay(d);
  56. i = allocimage(d, r, chan, 0, 0);
  57. setmalloctag(i, getcallerpc(&d));
  58. if(dolock)
  59. unlockdisplay(d);
  60. if(i == nil)
  61. return nil;
  62. }else{
  63. i = mallocz(sizeof(Image), 1);
  64. if(i == nil)
  65. return nil;
  66. }
  67. ncblock = _compblocksize(r, chantodepth(chan));
  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. Errout:
  75. if(dolock)
  76. lockdisplay(d);
  77. Erroutlock:
  78. freeimage(i);
  79. if(dolock)
  80. unlockdisplay(d);
  81. free(buf);
  82. return nil;
  83. }
  84. maxy = atoi(hdr+0*12);
  85. nb = atoi(hdr+1*12);
  86. if(maxy<=miny || r.max.y<maxy){
  87. werrstr("creadimage: bad maxy %d", maxy);
  88. goto Errout;
  89. }
  90. if(nb<=0 || ncblock<nb){
  91. werrstr("creadimage: bad count %d", nb);
  92. goto Errout;
  93. }
  94. if(readn(fd, buf, nb)!=nb)
  95. goto Errout;
  96. if(d){
  97. if(dolock)
  98. lockdisplay(d);
  99. a = bufimage(i->display, 21+nb);
  100. if(a == nil)
  101. goto Erroutlock;
  102. a[0] = 'Y';
  103. BPLONG(a+1, i->id);
  104. BPLONG(a+5, r.min.x);
  105. BPLONG(a+9, miny);
  106. BPLONG(a+13, r.max.x);
  107. BPLONG(a+17, maxy);
  108. if(!new) /* old image: flip the data bits */
  109. _twiddlecompressed(buf, nb);
  110. memmove(a+21, buf, nb);
  111. if(dolock)
  112. unlockdisplay(d);
  113. }
  114. miny = maxy;
  115. }
  116. free(buf);
  117. return i;
  118. }