1 /*
2  * $Id: charresize.c,v 1.5 2000/12/12 11:18:14 kazuhiko Exp $
3  *
4  * Copyright 1988, 1992 Hiroto Kagotani
5  * Copyright 2000 Kazuhiko
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include	<stdio.h>
19 #include	<stdlib.h>
20 #include	<ctype.h>
21 #include	<string.h>
22 #include	"def.h"
23 
24 static void	processAttributes(void);
25 static void	startchar(void);
26 static void	encoding(void);
27 static void	swidth(void);
28 static void	dwidth(void);
29 static void	bbx(void);
30 static void	bitmap(void);
31 static void	endchar(void);
32 static void	read_image(char *srcimage);
33 static int	get_byte(char *cp);
34 static void	magnify(register char *srcimage, register int *dstgray);
35 static void	countup_score(int *dstgray, int x, int y);
36 static void	write_image(register int *dstgray);
37 
38 static int	bbw;
39 static int	newbbw;
40 static int	bbh;
41 static int	newbbh;
42 static int	graylevel;
43 
44 static int	bit[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
45 
46 void
processChar(void)47 processChar(void)
48 {
49   char	*srcimage;
50   int	*dstgray;
51 
52   startchar();
53   encoding();
54   swidth();
55   dwidth();
56   bbx();
57   processAttributes();
58   bitmap();
59 
60   srcimage = (char*)malloc(bbw * bbh * sizeof(char));
61   memset(srcimage, 0, bbw * bbh * sizeof(char));
62   dstgray = (int*)malloc(newbbw * newbbh * sizeof(int));
63   memset(dstgray, 0, newbbw * newbbh * sizeof(int));
64 
65   read_image(srcimage);
66   magnify(srcimage, dstgray);
67   write_image(dstgray);
68 
69   free(dstgray);
70   free(srcimage);
71 
72   endchar();
73 }
74 
75 static void
processAttributes(void)76 processAttributes(void)
77 {
78   get_line();
79   if (beginwith(linebuf, "ATTRIBUTES")) { /* optional */
80     put_line();
81     get_line();
82   }
83 }
84 
85 static void
startchar(void)86 startchar(void)
87 {
88   get_line();
89   if (!beginwith(linebuf, "STARTCHAR")) {
90     error("STARTCHAR expected\n");
91   }
92   put_line();
93 }
94 
95 static void
encoding(void)96 encoding(void)
97 {
98   get_line();
99   if (!beginwith(linebuf, "ENCODING")) {
100     error("ENCODING expected\n");
101   }
102   put_line();
103 }
104 
105 static void
swidth(void)106 swidth(void)
107 {
108   int	arg1, arg2;
109   int	newwx, newwy;
110 
111   get_line();
112   if (!beginwith(linebuf, "SWIDTH")) {
113     error("SWIDTH expected\n");
114   }
115   if (sscanf(linebuf, "SWIDTH %d %d", &arg1, &arg2) != 2) {
116     error("SWIDTH has illegal arguments\n");
117   }
118   newwx = WRESIZE(arg1);
119   newwy = HRESIZE(arg2);
120   sprintf(linebuf, "SWIDTH %d %d", newwx, newwy);
121   put_line();
122 }
123 
124 static void
dwidth(void)125 dwidth(void)
126 {
127   int	arg1, arg2;
128   int	newwx, newwy;
129 
130   get_line();
131   if (!beginwith(linebuf, "DWIDTH")) {
132     error("DWIDTH expected\n");
133   }
134   if (sscanf(linebuf, "DWIDTH %d %d", &arg1, &arg2) != 2) {
135     error("DWIDTH has illegal arguments\n");
136   }
137   newwx = WRESIZE(arg1);
138   newwy = HRESIZE(arg2);
139   sprintf(linebuf, "DWIDTH %d %d", newwx, newwy);
140   put_line();
141 }
142 
143 static void
bbx(void)144 bbx(void)
145 {
146   int	arg1, arg2, arg3, arg4;
147   int	newbbox, newbboy;
148 
149   get_line();
150   if (!beginwith(linebuf, "BBX")) {
151     error("BBX expected\n");
152   }
153   if (sscanf(linebuf, "BBX %d %d %d %d", &arg1, &arg2, &arg3, &arg4) != 4
154       || arg1 < 0 || arg2 < 0) {
155     error("BBX has illegal arguments\n");
156   }
157   bbw = arg1;
158   newbbw = WRESIZE(arg1);
159   bbh = arg2;
160   newbbh = HRESIZE(arg2);
161   graylevel = bbw * bbh / blackness;
162 
163   newbbox = XRESIZE(arg3);
164   newbboy = YRESIZE(arg4);
165   sprintf(linebuf, "BBX %d %d %d %d", newbbw, newbbh, newbbox, newbboy);
166   put_line();
167 }
168 
169 static void
bitmap(void)170 bitmap(void)
171 {
172   if (!beginwith(linebuf, "BITMAP")) {
173     error("BITMAP expected\n");
174   }
175   put_line();
176 }
177 
178 static void
endchar(void)179 endchar(void)
180 {
181   get_line();
182   if (!beginwith(linebuf, "ENDCHAR")) {
183     error("ENDCHAR expected\n");
184   }
185   put_line();
186 }
187 
188 static void
read_image(char * srcimage)189 read_image(char *srcimage)
190 {
191   int	x, y, i;
192   int	byte;
193 
194   for (y = 0; y < bbh; y ++) {
195     get_line();
196     for (x = 0; x < bbw; x += 8) {
197       byte = get_byte(linebuf+x/4);
198       for (i = 0; i < 8 && x+i < bbw; i ++) {
199 	*srcimage++ = ((byte & bit[i]) != 0);
200       }
201     }
202   }
203 }
204 
205 static int
get_byte(char * cp)206 get_byte(char *cp)
207 {
208   int	byte = 0;
209   if (!isxdigit((int)*cp)) {
210     error("illegal raster data\n");
211   }
212   byte |= xdigit(*cp);
213   byte <<= 4;
214   cp ++;
215   if (!isxdigit((int)*cp)) {
216     error("illegal raster data\n");
217   }
218   byte |= xdigit(*cp);
219   return byte;
220 }
221 
222 static void
magnify(register char * srcimage,register int * dstgray)223 magnify(register char *srcimage, register int *dstgray)
224 {
225   register int	x, y;
226 
227   for (y = 0; y < bbh; y ++) {
228     for (x = 0; x < bbw; x ++) {
229       if (srcimage[y*bbw + x]) {
230 	countup_score(dstgray, x, y);
231       }
232     }
233   }
234 }
235 
236 static void
countup_score(int * dstgray,int x,int y)237 countup_score(int *dstgray, int	x, int	y)
238 {
239   int	X = x * newbbw;
240   int	Y = y * newbbh;
241   int	X1 = X + newbbw;
242   int	Y1 = Y + newbbh;
243 
244   int	newx, newy;
245   int	newxbegin = X / bbw;
246   int	newybegin = Y / bbh;
247   int	newxend = howmany(X1, bbw);
248   int	newyend = howmany(Y1, bbh);
249 
250   for (newy = newybegin; newy < newyend; newy ++) {
251     for (newx = newxbegin; newx < newxend; newx ++) {
252       int	newX = newx * bbw;
253       int	newY = newy * bbh;
254       int	newX1 = newX + bbw;
255       int	newY1 = newY + bbh;
256       dstgray[newy * newbbw + newx] +=
257 	(MIN(X1,newX1) - MAX(X,newX))
258 	* (MIN(Y1,newY1) - MAX(Y,newY));
259     }
260   }
261 }
262 
263 static void
write_image(register int * dstgray)264 write_image(register int *dstgray)
265 {
266   register int	x, y, i;
267   for (y = 0; y < newbbh; y ++) {
268     register int	*row = dstgray + y*newbbw;
269     for (x = 0; x < newbbw; x += 8) {
270       register int	byte = 0;
271       for (i = 0; i < 8 && x+i < newbbw; i ++) {
272 	if (row[x + i] >= graylevel) {
273 	  byte |= bit[i];
274 	}
275       }
276       printf("%02X", byte);
277     }
278     printf("\n");
279   }
280 }
281