Debian Codegraph @

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