File: charresize.c

package info (click to toggle)
bdfresize 1.5-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 316 kB
  • sloc: ansic: 1,594; sh: 330; makefile: 14
file content (280 lines) | stat: -rw-r--r-- 5,707 bytes parent folder | download | duplicates (6)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * $Id: charresize.c,v 1.5 2000/12/12 11:18:14 kazuhiko Exp $
 *
 * Copyright 1988, 1992 Hiroto Kagotani
 * Copyright 2000 Kazuhiko
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */ 

#include	<stdio.h>
#include	<stdlib.h>
#include	<ctype.h>
#include	<string.h>
#include	"def.h"

static void	processAttributes(void);
static void	startchar(void);
static void	encoding(void);
static void	swidth(void);
static void	dwidth(void);
static void	bbx(void);
static void	bitmap(void);
static void	endchar(void);
static void	read_image(char *srcimage);
static int	get_byte(char *cp);
static void	magnify(register char *srcimage, register int *dstgray);
static void	countup_score(int *dstgray, int x, int y);
static void	write_image(register int *dstgray);

static int	bbw;
static int	newbbw;
static int	bbh;
static int	newbbh;
static int	graylevel;

static int	bit[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };

void
processChar(void)
{
  char	*srcimage;
  int	*dstgray;

  startchar();
  encoding();
  swidth();
  dwidth();
  bbx();
  processAttributes();
  bitmap();

  srcimage = (char*)malloc(bbw * bbh * sizeof(char));
  memset(srcimage, 0, bbw * bbh * sizeof(char));
  dstgray = (int*)malloc(newbbw * newbbh * sizeof(int));
  memset(dstgray, 0, newbbw * newbbh * sizeof(int));

  read_image(srcimage);
  magnify(srcimage, dstgray);
  write_image(dstgray);

  free(dstgray);
  free(srcimage);

  endchar();
}

static void
processAttributes(void)
{
  get_line();
  if (beginwith(linebuf, "ATTRIBUTES")) { /* optional */
    put_line();
    get_line();
  }
}

static void
startchar(void)
{
  get_line();
  if (!beginwith(linebuf, "STARTCHAR")) {
    error("STARTCHAR expected\n");
  }
  put_line();
}

static void
encoding(void)
{
  get_line();
  if (!beginwith(linebuf, "ENCODING")) {
    error("ENCODING expected\n");
  }
  put_line();
}

static void
swidth(void)
{
  int	arg1, arg2;
  int	newwx, newwy;

  get_line();
  if (!beginwith(linebuf, "SWIDTH")) {
    error("SWIDTH expected\n");
  }
  if (sscanf(linebuf, "SWIDTH %d %d", &arg1, &arg2) != 2) {
    error("SWIDTH has illegal arguments\n");
  }
  newwx = WRESIZE(arg1);
  newwy = HRESIZE(arg2);
  sprintf(linebuf, "SWIDTH %d %d", newwx, newwy);
  put_line();
}

static void
dwidth(void)
{
  int	arg1, arg2;
  int	newwx, newwy;

  get_line();
  if (!beginwith(linebuf, "DWIDTH")) {
    error("DWIDTH expected\n");
  }
  if (sscanf(linebuf, "DWIDTH %d %d", &arg1, &arg2) != 2) {
    error("DWIDTH has illegal arguments\n");
  }
  newwx = WRESIZE(arg1);
  newwy = HRESIZE(arg2);
  sprintf(linebuf, "DWIDTH %d %d", newwx, newwy);
  put_line();
}

static void
bbx(void)
{
  int	arg1, arg2, arg3, arg4;
  int	newbbox, newbboy;

  get_line();
  if (!beginwith(linebuf, "BBX")) {
    error("BBX expected\n");
  }
  if (sscanf(linebuf, "BBX %d %d %d %d", &arg1, &arg2, &arg3, &arg4) != 4
      || arg1 < 0 || arg2 < 0) {
    error("BBX has illegal arguments\n");
  }
  bbw = arg1;
  newbbw = WRESIZE(arg1);
  bbh = arg2;
  newbbh = HRESIZE(arg2);
  graylevel = bbw * bbh / blackness;

  newbbox = XRESIZE(arg3);
  newbboy = YRESIZE(arg4);
  sprintf(linebuf, "BBX %d %d %d %d", newbbw, newbbh, newbbox, newbboy);
  put_line();
}

static void
bitmap(void)
{
  if (!beginwith(linebuf, "BITMAP")) {
    error("BITMAP expected\n");
  }
  put_line();
}

static void
endchar(void)
{
  get_line();
  if (!beginwith(linebuf, "ENDCHAR")) {
    error("ENDCHAR expected\n");
  }
  put_line();
}

static void
read_image(char *srcimage)
{
  int	x, y, i;
  int	byte;

  for (y = 0; y < bbh; y ++) {
    get_line();
    for (x = 0; x < bbw; x += 8) {
      byte = get_byte(linebuf+x/4);
      for (i = 0; i < 8 && x+i < bbw; i ++) {
	*srcimage++ = ((byte & bit[i]) != 0);
      }
    }
  }
}

static int
get_byte(char *cp)
{
  int	byte = 0;
  if (!isxdigit((int)*cp)) {
    error("illegal raster data\n");
  }
  byte |= xdigit(*cp);
  byte <<= 4;
  cp ++;
  if (!isxdigit((int)*cp)) {
    error("illegal raster data\n");
  }
  byte |= xdigit(*cp);
  return byte;
}

static void
magnify(register char *srcimage, register int *dstgray)
{
  register int	x, y;

  for (y = 0; y < bbh; y ++) {
    for (x = 0; x < bbw; x ++) {
      if (srcimage[y*bbw + x]) {
	countup_score(dstgray, x, y);
      }
    }
  }
}

static void
countup_score(int *dstgray, int	x, int	y)
{
  int	X = x * newbbw;
  int	Y = y * newbbh;
  int	X1 = X + newbbw;
  int	Y1 = Y + newbbh;

  int	newx, newy;
  int	newxbegin = X / bbw;
  int	newybegin = Y / bbh;
  int	newxend = howmany(X1, bbw);
  int	newyend = howmany(Y1, bbh);

  for (newy = newybegin; newy < newyend; newy ++) {
    for (newx = newxbegin; newx < newxend; newx ++) {
      int	newX = newx * bbw;
      int	newY = newy * bbh;
      int	newX1 = newX + bbw;
      int	newY1 = newY + bbh;
      dstgray[newy * newbbw + newx] +=
	(MIN(X1,newX1) - MAX(X,newX))
	* (MIN(Y1,newY1) - MAX(Y,newY));
    }
  }
}

static void
write_image(register int *dstgray)
{
  register int	x, y, i;
  for (y = 0; y < newbbh; y ++) {
    register int	*row = dstgray + y*newbbw;
    for (x = 0; x < newbbw; x += 8) {
      register int	byte = 0;
      for (i = 0; i < 8 && x+i < newbbw; i ++) {
	if (row[x + i] >= graylevel) {
	  byte |= bit[i];
	}
      }
      printf("%02X", byte);
    }
    printf("\n");
  }
}