dssread.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "sky.h"
  5. static void dodecode(Biobuf*, Pix*, int, int, uchar*);
  6. static long getlong(uchar*);
  7. int debug;
  8. Img*
  9. dssread(char *file)
  10. {
  11. int nx, ny, scale, sumall;
  12. Pix *p, *pend;
  13. uchar buf[21];
  14. Biobuf *bp;
  15. Img *ip;
  16. if(debug)
  17. Bprint(&bout, "reading %s\n", file);
  18. bp = Bopen(file, OREAD);
  19. if(bp == 0)
  20. return 0;
  21. if(Bread(bp, buf, sizeof(buf)) != sizeof(buf) ||
  22. buf[0] != 0xdd || buf[1] != 0x99){
  23. werrstr("bad format");
  24. return 0;
  25. }
  26. nx = getlong(buf+2);
  27. ny = getlong(buf+6);
  28. scale = getlong(buf+10);
  29. sumall = getlong(buf+14);
  30. if(debug)
  31. fprint(2, "%s: nx=%d, ny=%d, scale=%d, sumall=%d, nbitplanes=%d,%d,%d\n",
  32. file, nx, ny, scale, sumall, buf[18], buf[19], buf[20]);
  33. ip = malloc(sizeof(Img) + (nx*ny-1)*sizeof(int));
  34. if(ip == 0){
  35. Bterm(bp);
  36. werrstr("no memory");
  37. return 0;
  38. }
  39. ip->nx = nx;
  40. ip->ny = ny;
  41. dodecode(bp, ip->a, nx, ny, buf+18);
  42. ip->a[0] = sumall; /* sum of all pixels */
  43. Bterm(bp);
  44. if(scale > 1){
  45. p = ip->a;
  46. pend = &ip->a[nx*ny];
  47. while(p < pend)
  48. *p++ *= scale;
  49. }
  50. hinv(ip->a, nx, ny);
  51. return ip;
  52. }
  53. static
  54. void
  55. dodecode(Biobuf *infile, Pix *a, int nx, int ny, uchar *nbitplanes)
  56. {
  57. int nel, nx2, ny2, bits, mask;
  58. Pix *aend, px;
  59. nel = nx*ny;
  60. nx2 = (nx+1)/2;
  61. ny2 = (ny+1)/2;
  62. memset(a, 0, nel*sizeof(*a));
  63. /*
  64. * Initialize bit input
  65. */
  66. start_inputing_bits();
  67. /*
  68. * read bit planes for each quadrant
  69. */
  70. qtree_decode(infile, &a[0], ny, nx2, ny2, nbitplanes[0]);
  71. qtree_decode(infile, &a[ny2], ny, nx2, ny/2, nbitplanes[1]);
  72. qtree_decode(infile, &a[ny*nx2], ny, nx/2, ny2, nbitplanes[1]);
  73. qtree_decode(infile, &a[ny*nx2+ny2], ny, nx/2, ny/2, nbitplanes[2]);
  74. /*
  75. * make sure there is an EOF symbol (nybble=0) at end
  76. */
  77. if(input_nybble(infile) != 0){
  78. fprint(2, "dodecode: bad bit plane values\n");
  79. exits("format");
  80. }
  81. /*
  82. * get the sign bits
  83. */
  84. aend = &a[nel];
  85. mask = 0;
  86. bits = 0;;
  87. for(; a<aend; a++) {
  88. if(px = *a) {
  89. if(mask == 0) {
  90. mask = 0x80;
  91. bits = Bgetc(infile);
  92. }
  93. if(mask & bits)
  94. *a = -px;
  95. mask >>= 1;
  96. }
  97. }
  98. }
  99. static
  100. long getlong(uchar *p)
  101. {
  102. return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  103. }