summaryrefslogtreecommitdiff
path: root/dviware/dvi2pcl/pknum.c
blob: 0813516df524ffb6e23e66f88563844153a4a91f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* $Log:	pknum.c,v $
 * Revision 0.8  92/11/23  19:46:52  19:46:52  bt (Bo Thide')
 * Fixed resolution bug. Portable downloading. Added/changed options. PJXL color support
 * 
 * Revision 0.7  92/11/13  02:41:37  02:41:37  bt (Bo Thide')
 * More bug fixes and improvements. Support for PaintJet XL
 * 
 * Revision 0.6  92/11/10  21:48:34  21:48:34  bt (Bo Thide')
 * Bug fixes. Added -R option. Better font handling.
 * 
 * Revision 0.5  92/11/09  16:25:40  16:25:40  bt (Bo Thide')
 * Rewrite of dospecial.c. Extended \special support
 * 
 * Revision 0.4  92/11/08  02:45:56  02:45:56  bt (Bo Thide')
 * Changed to portable bit manipulations. Replaced strrstr for non-POSIX compliant C. Fixed numerous bugs. Added support for more \special's.
 * 
 * Revision 0.3  92/08/24  12:45:47  12:45:47  bt (Bo Thide')
 * Fixed 8 bit (dc font) support.
 * 
 * Revision 0.2  92/08/23  17:28:59  17:28:59  bt (Bo Thide')
 * Source cleaned up.  Changed certain function calls.  Removed globals.
 * 
 * Revision 0.1  92/08/22  23:58:48  23:58:48  bt (Bo Thide')
 * First Release.
 *  */

/*
 * This routine provides the final algorithm for decoding the run counts,
 * assuming a procedure (macro) 'getnib()' is available to get the next
 * nibble from the 'pkbuffer'. It returns the next run_count as an integer,
 * and if the row needs to be repeated it returns additionally the repeat
 * value to the global variable 'repeat_count'. Since a repeat count can
 * occur only once within a row, namely after the first transition within a
 * row, it is the responsibility of the calling procedure to repeat such a
 * row and to reset 'repeat_count' to zero, before calling the next run count
 * for the next row.  The global variable 'c_black' changes from 0 to -1 and
 * back to 0 each time 'pknum' is called, indicating a run count for
 * black pixels by 'c_black = -1' or for white pixels by 'c_black = 0', resp.
 * Note that this routine is recursive, but since a repeat count can never
 * directly follow another repeat count, it can only be recursive to one level
 */

#include "globals.h"
#include "macros.h"

#define getnib() ((c_nib = ~c_nib) ? (*pk_ptr & 0xf0) >> 4 : *pk_ptr++ & 0xf)

static char rcsid[] = "$Header: pknum.c,v 0.8 92/11/23 19:46:52 bt Exp $";

pknum()
{
	int	i,j;
	extern	int repeat_count;

	c_black = ~c_black;
	if(!(i = getnib())) {
		do
			i++;
		while(!(j = getnib()));
		while(i--)
			j = (j << 4) | getnib();
		return(j - 15 + ((13 - dyn_f)<<4) + dyn_f);
	}
	else if(i <= dyn_f)
	 	return(i);
	else if(i < 14)
		return(((i - dyn_f - 1)<<4) + getnib() + dyn_f + 1);
	else {
		if(repeat_count)
			prerror("Extra repeat count\n");
		if(i == 14)
			repeat_count = pknum();
		else {
			repeat_count = 1;
			c_black = ~c_black;
		}
		return(pknum());
	}
}