creadimage.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if(dolock)
  58. unlockdisplay(d);
  59. if(i == nil)
  60. return nil;
  61. }else{
  62. i = mallocz(sizeof(Image), 1);
  63. if(i == nil)
  64. return nil;
  65. }
  66. ncblock = _compblocksize(r, chantodepth(chan));
  67. buf = malloc(ncblock);
  68. if(buf == nil)
  69. goto Errout;
  70. miny = r.min.y;
  71. while(miny != r.max.y){
  72. if(readn(fd, hdr, 2*12) != 2*12){
  73. Errout:
  74. if(dolock)
  75. lockdisplay(d);
  76. Erroutlock:
  77. freeimage(i);
  78. if(dolock)
  79. unlockdisplay(d);
  80. free(buf);
  81. return nil;
  82. }
  83. maxy = atoi(hdr+0*12);
  84. nb = atoi(hdr+1*12);
  85. if(maxy<=miny || r.max.y<maxy){
  86. werrstr("creadimage: bad maxy %d", maxy);
  87. goto Errout;
  88. }
  89. if(nb<=0 || ncblock<nb){
  90. werrstr("creadimage: bad count %d", nb);
  91. goto Errout;
  92. }
  93. if(readn(fd, buf, nb)!=nb)
  94. goto Errout;
  95. if(d){
  96. if(dolock)
  97. lockdisplay(d);
  98. a = bufimage(i->display, 21+nb);
  99. if(a == nil)
  100. goto Erroutlock;
  101. a[0] = 'Y';
  102. BPLONG(a+1, i->id);
  103. BPLONG(a+5, r.min.x);
  104. BPLONG(a+9, miny);
  105. BPLONG(a+13, r.max.x);
  106. BPLONG(a+17, maxy);
  107. if(!new) /* old image: flip the data bits */
  108. _twiddlecompressed(buf, nb);
  109. memmove(a+21, buf, nb);
  110. if(dolock)
  111. unlockdisplay(d);
  112. }
  113. miny = maxy;
  114. }
  115. free(buf);
  116. return i;
  117. }