Debian Codegraph @

texk/xdvipsk/unpack.c

raw
1 /*
2 * Unpacks the raster data from the packed buffer. This code was
3 * translated from pktopx.web using an automatic translator, then
4 * converted for this purpose. This little routine can be very useful
5 * in other drivers as well.
6 */
7 #ifndef XDVIPSK
8 #include "dvips.h" /* The copyright notice in that file is included too! */
9 #else
10 #include "xdvips.h" /* The copyright notice in that file is included too! */
11 #endif /* XDVIPSK */
12
13 /*
14 * The external declarations:
15 */
16 #include "protos.h"
17
18 /*
19 * Some statics for use here.
20 */
21 static halfword bitweight;
22 static halfword dynf;
23 static halfword gpower[17] = { 0 , 1 , 3 , 7 , 15 , 31 , 63 , 127 ,
24 255 , 511 , 1023 , 2047 , 4095 , 8191 , 16383 , 32767 , 65535 };
25 static long repeatcount;
26 static quarterword *p;
27
28 /*
29 * We need procedures to get a nybble, bit, and packed word from the
30 * packed data structure.
31 */
32
33 static shalfword
34 getnyb(void)
35 {
36 if ( bitweight == 0 )
37 { bitweight = 16;
38 return(*p++ & 15);
39 } else {
40 bitweight = 0;
41 return(*p >> 4);
42 }
43 }
44
45 static Boolean
46 getbit(void)
47 {
48 bitweight >>= 1;
49 if ( bitweight == 0 )
50 { p++;
51 bitweight = 128;
52 }
53 return(*p & bitweight);
54 }
55
56 static long
57 pkpackednum(void) {
58 register halfword i;
59 register long j;
60 i = getnyb ();
61 if ( i == 0 ) {
62 do { j = getnyb ();
63 i++;
64 } while ( j == 0 );
65 while ( i != 0 ) {
66 j = j * 16 + ((long) getnyb ());
67 i--;
68 }
69 return ( j - 15 + ( 13 - dynf ) * 16 + dynf );
70 }
71 else if ( i <= dynf ) return ( i );
72 else if ( i < 14 ) return ( ( i - dynf - 1 )*16 + getnyb() + dynf + 1 );
73 else {
74 if (repeatcount != 0)
75 error("! recursive repeat count in pk file");
76 repeatcount = 1;
77 if ( i == 14 ) repeatcount = pkpackednum ();
78 return ( pkpackednum() );
79 }
80 }
81
82 static void
83 flip(register char *s, register long howmany)
84 {
85 register char t;
86
87 while (howmany > 0) {
88 t = *s;
89 *s = s[1];
90 s[1] = t;
91 howmany--;
92 s += 2;
93 }
94 }
95 /*
96 * And now we have our main routine.
97 */
98 static halfword bftest = 1;
99 long
100 unpack(quarterword *pack, halfword *raster,
101 halfword cwidth, halfword cheight, halfword cmd)
102 {
103 register integer i, j;
104 shalfword wordwidth;
105 register halfword word, wordweight;
106 shalfword rowsleft;
107 Boolean turnon;
108 shalfword hbit, ww;
109 long count;
110 halfword *oraster;
111
112 oraster = raster;
113 p = pack;
114 dynf = cmd / 16;
115 turnon = cmd & 8;
116 wordwidth = (cwidth + 15)/16;
117 if ( dynf == 14 )
118 { bitweight = 256;
119 for ( i = 1; i <= cheight; i ++ )
120 { word = 0;
121 wordweight = 32768;
122 for ( j = 1; j <= cwidth; j ++ )
123 { if ( getbit () ) word += wordweight;
124 wordweight >>= 1;
125 if ( wordweight == 0 )
126 { *raster++ = word;
127 word = 0;
128 wordweight = 32768;
129 }
130 }
131 if ( wordweight != 32768 )
132 *raster++ = word;
133 }
134 } else {
135 rowsleft = cheight;
136 hbit = cwidth;
137 repeatcount = 0;
138 ww = 16;
139 word = 0;
140 bitweight = 16;
141 while ( rowsleft > 0 )
142 { count = pkpackednum();
143 while ( count != 0 )
144 { if ( ( count <= ww ) && ( count < hbit ) )
145 { if ( turnon ) word += gpower [ ww ] - gpower
146 [ ww - count ];
147 hbit -= count;
148 ww -= count;
149 count = 0;
150 }
151 else if ( ( count >= hbit ) && ( hbit <= ww ) )
152 { if ( turnon )
153 word += gpower[ww] - gpower[ww-hbit];
154 *raster++ = word;
155 for ( i = 1; i <= repeatcount; i ++ ) {
156 for ( j = 1; j <= wordwidth; j ++ ) {
157 *raster = *(raster - wordwidth);
158 raster++;
159 }
160 }
161 rowsleft -= repeatcount + 1;
162 repeatcount = 0;
163 word = 0;
164 ww = 16;
165 count -= hbit;
166 hbit = cwidth;
167 }
168 else
169 { if ( turnon ) word += gpower [ ww ];
170 *raster++ = word;
171 word = 0;
172 count -= ww;
173 hbit -= ww;
174 ww = 16;
175 }
176 }
177 turnon = ! turnon;
178 }
179 if ( ( rowsleft != 0 ) || ( (unsigned)hbit != cwidth ) )
180 error ( "! error while unpacking; more bits than required" );
181 }
182 if (*(char *)&bftest) /* is the hardware LittleEndian? */
183 flip((char *)oraster, ((cwidth + 15) >> 4) * (long)cheight);
184 return(p-pack);
185 }