Debian Codegraph @

converter/pbm/pbmtobbnbg.c

raw
1 /* pbmtobg.c - read a portable bitmap and produce BitGraph graphics
2 **
3 ** Copyright 1989 by Mike Parker.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12
13 /*
14 ** Changed to take advantage of negative Packed Pixed Data values and
15 ** supply ANSI-standard string terminator. Paul Milazzo, 28 May 1990.
16 */
17
18 #include "pbm.h"
19
20 static void write16 ( unsigned int word );
21
22 static int nco;
23
24 int
25 main(int argc, char ** argv) {
26 int rows;
27 int cols;
28 int format;
29 bit *bitrow;
30 int row;
31 unsigned int sixteen;
32 int i;
33 unsigned int mask;
34 int op;
35 int x;
36 int y;
37
38
39 pbm_init( &argc, argv );
40
41 op = 3;
42 switch (argc)
43 { case 1:
44 break;
45 case 2:
46 op = atoi(argv[1]);
47 break;
48 case 3:
49 x = atoi(argv[1]);
50 y = atoi(argv[2]);
51 printf("\33:%d;%dm",x,y);
52 break;
53 case 4:
54 op = atoi(argv[1]);
55 x = atoi(argv[2]);
56 y = atoi(argv[3]);
57 printf("\33:%d;%dm",x,y);
58 break;
59 }
60 nco = 0;
61 pbm_readpbminit(stdin,&cols,&rows,&format);
62 printf("\33P:%d;%d;%ds\n",op,cols,rows);
63 bitrow = pbm_allocrow(cols);
64 for (row=0;row<rows;row++)
65 { pbm_readpbmrow(stdin,bitrow,cols,format);
66 sixteen = 0;
67 mask = 0x8000;
68 for (i=0;i<cols;i++)
69 { if (bitrow[i]==PBM_BLACK) sixteen |= mask;
70 mask >>= 1;
71 if (mask == 0)
72 { mask = 0x8000;
73 write16(sixteen);
74 sixteen = 0;
75 }
76 }
77 if (mask != 0x8000)
78 { write16(sixteen);
79 }
80 }
81 puts("\033\\");
82 exit(0);
83 }
84
85
86
87 #ifdef POSITIVE_VALUES_ONLY
88 static void
89 write16(unsigned int sixteen) {
90 if (nco > 75)
91 { putchar('\n');
92 nco = 0;
93 }
94 if (sixteen & 0xfc00)
95 { putchar(0100+(sixteen>>10));
96 nco ++;
97 }
98 if (sixteen & 0xfff0)
99 { putchar(0100+((sixteen>>4)&0x3f));
100 nco ++;
101 }
102 putchar(060+(sixteen&0xf));
103 nco ++;
104 }
105
106
107
108 #else
109 /*
110 * This version of "write16" uses negative Packed Pixel Data values to
111 * represent numbers in the range 0x7fff--0xffff; negative values will
112 * require fewer characters as they approach the upper end of that range.
113 */
114 static void
115 write16 (unsigned int word) {
116
117 int high;
118 int mid;
119 int low;
120 int signChar;
121
122 if (nco > 75) {
123 putchar ('\n');
124 nco = 0;
125 }
126
127 if (word > 0x7fff) {
128 word = (unsigned int) (0x10000L - (long int) word);
129 signChar = ' ';
130 }
131 else
132 signChar = '0';
133
134 high = (word >> 10) + '@';
135 mid = ((word & 0x3f0) >> 4) + '@';
136 low = (word & 0xf) + signChar;
137
138 if (high != '@') {
139 printf ("%c%c%c", high, mid, low);
140 nco += 3;
141 }
142 else if (mid != '@') {
143 printf ("%c%c", mid, low);
144 nco += 2;
145 }
146 else {
147 putchar (low);
148 nco++;
149 }
150 }
151 #endif
152
153
154