-
Notifications
You must be signed in to change notification settings - Fork 2
/
RotateBitmap.c
510 lines (435 loc) · 13.3 KB
/
RotateBitmap.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#ifdef DYNAMIC_BITMAPS
/*
* rotatebitmap.c * Routines to rotate bitmaps *
*
*/
#include <errno.h>
#include <stdio.h>
#include <math.h>
#ifndef NO_X_HEADERS
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/Xmu/Drawing.h>
#include <X11/Xutil.h>
#else
#define NO_READ_BITMAPFILE
/* with that out of the way, all tht we need is XImage to be defined */
/*
* WARNING:Also, make sure whatever calls RotateBitmap() uses the SAME *
* XImage typedef, or else RotateBitmap() will be very confused about * just
* what its arguments
*/
/*
* an abbreviated XImage struct, useful only for this code. * some X stuff
* might understand it, though
*/
typedef struct _XImage {
int width, height;
int xoffset;
int format;
unsigned char *data;
int byte_order;
int bitmap_unit;
int bitmap_bit_order;
int bitmap_pad;
int depth;
int bytes_per_line;
} XImage;
#define XYBitmap 0
#define LSBFirst 0
#endif
/*
* define NO_READ_BITMAPFILE and this code file will #ifdef out all the *
* routines and code related to the reading and writing of * the bitmap
* file[s], and thus be useful as utilities for manipulating * bitmap files
* in other programs *
*
* #define NO_READ_BITMAPFILE
*/
#ifndef NO_READ_BITMAPFILE
/*
* 'global' variables for the bitmap file reading code swiped from the *
* 'bitmap' program code
*/
static int raster_length; /* how many chars in the raster[] array */
static unsigned char *stripped_name = "bitmap";
/* file name without directory path or extension */
#endif /* NO_READ_BITMAPFILE */
/* used primarily when reading from the .xbm files */
#ifndef OUTTA_RANGE
#define OUTTA_RANGE -1
#endif
int x_hot_spot = OUTTA_RANGE;
int y_hot_spot = OUTTA_RANGE;
#define PI 3.14152136
#ifndef NO_READ_BITMAPFILE
static XImage image =
{
0, 0, /* (int) width, (int) height */
0, XYBitmap, NULL, /* (int) xoffset, (int) format, (char *) data */
LSBFirst, 8, /* (int) byte_order, (int) bitmap-unit */
LSBFirst, 8, 1, /* (int) bitmap_bit_order, bitmap_pad, depth */
0 /* (int) bytes_per_line */
};
#endif
/* useful utility: number of 8 bit bytes to form a line in xbm format */
#define BYTEWIDTH(width) (int) ((width) % 8 ? ((width)/8 + 1) : ((width)/8))
/*
* basically, an extra byte is needed for the remaining bits if it's not *
* divisible by 8.
*/
/* maximum size bitmaps that these routines can handle... */
#ifndef MAXHEIGHT
#define MAXHEIGHT 100
#endif
#ifndef MAXWIDTH
#define MAXWIDTH 100
#endif
#define TEMPSIZE MAXHEIGHT * BYTEWIDTH(MAXWIDTH)
unsigned char rotatedarray[TEMPSIZE]; /* really, I ought to make this
* dynamic... */
static XImage rotatedimage =
{
0, 0, /* width, height */
0, XYBitmap, rotatedarray, /* xoffset, format, data */
LSBFirst, 8, /* byte_order, bitmap_unit */
LSBFirst, 8, 1, /* bitmap_bit_order, bitmap_pad, depth */
0 /* bytes_per_line */
};
/* establish the functions used in this code */
XImage *RotateBitmap( /* XImage bitmap,int rotation, int filtering */ );
int pixel( /* int x,int y, char * data, int width,int
height */ );
void set_pixel( /* int rot_x,int rot_y, char * data, int
width,int height */ );
#ifndef NO_READ_BITMAPFILE
int ReadBitmapFile( /* char * filename */ );
void WriteXImageFile( /* char * filename, XImage imagedata */ );
void usage();
#endif
/*
* #define NETREK if this is going to be used as part of netrek code, * in
* which there are arrays (double) Sin[256] and (double) Cos[256] * this way
* this function doesn't need to call its own Sin and Cos * functinos.
*/
#ifdef NETREK
extern double Sin[];
extern double Cos[];
#else
double Sin(int angle);
double Cos(int angle);
#endif
#ifndef NO_READ_BITMAPFILE
/*
* the wrapper for the standalone rotation utility. * see the usage() routine
* for usage instructions...
*/
int
main(int argc , char **argv) {
char string[80];
int angle;
int filtering = 5;
XImage *imagedata = NULL;
if (argc < 3)
{
usage();
exit(1);
}
argv++;
argc--; /* skip argv[0] */
/* -f [##] option-- filtering level. 4-9 defualt 5 */
if (**argv == '-' && (*argv)[1] == 'f') {
argc--;
argv++;
if (argc <= 0)
usage();
filtering = atoi(*argv);
printf("Filtering level: %d(%s)\n", filtering, *argv);
argc--;
argv++;
if (argc <= 0)
usage();
}
angle = atoi(*argv);
/* put angle in range */
while (angle < 0)
angle += 256;
angle = angle % 256;
argc--;
argv++;
printf("Rotation angle: %d\n", angle);
#ifdef DEBUG
fprintf(stderr, "rotation angle %d\n", angle);
#endif
/* loop over all arguments */
while (--argc >= 0) {
#ifdef DEBUG
fprintf(stderr, "parsing argument: %s\n", *argv);
#endif
ReadBitmapFile(*argv);
imagedata = RotateBitmap(image, angle, filtering);
/* fix name to format oldfilename.rotation */
sprintf(string, "%s.%d", *argv, angle);
WriteXImageFile(string, *imagedata);
argv++;
}
return (0);
}
#endif /* NO_READ_BITMAPFILE */
/*
* RotateBitmap() : Rotates X image structure, * returns pointer to rotated
* XImage, NULL on error * args: standard XImage structure, rotation(0-255), *
* filtering to use in non-right angle rotations (see below for explanation) *
* Rotation is in units such that 256 equals full circle *
*
* for non-right angle rotations, 'filtering' is used. * rotate alg is
* improved from version 1 in that we map each pixel from * rotated map onto
* the non-rotated one. * furthermore, we check points to either side of the
* center of the * rotated map's pixels. if enough of them (set by filtering)
* are in a pixel, the * corresponding rotated pixel is mapped that color.
*/
/* maximum bitmap size handled. */
XImage *
RotateBitmap(bitmap, angle, filtering)
XImage bitmap;
int angle;
int filtering;
{
int i, j;
int x, y;
int width = bitmap.width;
int height = bitmap.height;
double cent_x = x_hot_spot;
double cent_y = y_hot_spot;
int maxbytes = 0;
int bytesperline = 0;
double increment = .30;
double x_test, y_test;
static unsigned char bitfield[MAXWIDTH][MAXHEIGHT] =
{0}; /* this really should be dynamically
* allocated */
char *rotatedata = rotatedimage.data;
double sinval, cosval;
#ifndef NETREK
sinval = Sin(-angle);
cosval = Cos(-angle);
#else
/*
* oddly, the netrek Sin[] and Cos[] are rotated oddly from normal * math
* functions. We must compensate for this`
*/
if (angle >= 0 && angle < 256) {
sinval = Cos[256 - angle];
cosval = -Sin[256 - angle];
} else {
/* deal with illegal range angles by just assuming 0 rotation */
sinval = 0;
cosval = 1;
}
#endif
width = bitmap.width;
height = bitmap.height;
if (cent_x == OUTTA_RANGE || cent_y == OUTTA_RANGE) {
cent_x = (((double) width) - 1) / 2;
cent_y = (((double) height) - 1) / 2;
}
rotatedimage.width = width;
rotatedimage.height = height;
rotatedimage.bytes_per_line = bytesperline = BYTEWIDTH(width);
#ifndef DEBUG
/* handle simple cases */
if (angle == 0) {
for (i = 0; i < bytesperline * height; i++)
rotatedimage.data[i] = bitmap.data[i];
return (&rotatedimage);
};
#endif
if ((sinval * sinval) <= 0.0001 || (sinval * sinval) >= 0.9999) {
/*
* don't have to do fancy increment stuff for right angle * rotations
*/
increment = 0;
filtering = 1;
#ifdef DEBUG
printf("Performing Right angle rotation!\n");
#endif
}
#ifdef DEBUG
printf("Sin(), Cos() of the angle are %f, %f\n", sinval, cosval);
#endif
/* calculate number of bytes needed */
maxbytes = height * bytesperline;
/* clear bitmap space */
for (i = 0; i < maxbytes; i++)
*(rotatedata + i) = 0;
for (i = 0; i < height; i++) { /* row number */
for (j = 0; j < width; j++) { /* column number */
/*
* note for case where increment==0, following loops should get *
* executed only _once_
*/
bitfield[i][j] = 0;
for (x_test = i - increment; x_test <= i + increment; x_test += increment) {
for (y_test = j - increment; y_test <= j + increment; y_test += increment) {
x = (int) (cosval * (x_test - cent_x) +
sinval * (y_test - cent_y) + cent_x + .5);
y = (int) (-sinval * (x_test - cent_x) +
cosval * (y_test - cent_y) + cent_x + .5);
bitfield[i][j] += pixel(x, y, bitmap.data, width, height);
if (x_test == i && y_test == j)
bitfield[i][j] += pixel(x, y, bitmap.data, width, height);
/* double weight of pixels exactly on */
if (increment == 0)
y_test++; /* prevent infinite loop! */
}
if (increment == 0)
x_test++; /* prevent infinite loop! */
}
}
}
for (i = 0; i < height; i++) { /* row number */
for (j = 0; j < width; j++) { /* column number */
if (bitfield[i][j] >= filtering) {
set_pixel(i, j, rotatedata, width, height);
}
}
}
return (&rotatedimage);
}
/*
* returns 1 if pixel corresponding to x,y in bitmap array data is set *
* returns 0 in other cases
*/
int
pixel(x, y, data, width, height)
int x, y;
char *data;
int width, height;
{
int bytepos;
int bitmask;
#ifdef MASSIVEDEBUG
int j;
#endif
if (x < 0 || x >= width)
return (0);
if (y < 0 || y >= height)
return (0);
bytepos = x * BYTEWIDTH(width) + (y / 8);
bitmask = 1 << (y % 8);
#ifdef MASSIVEDEBUG
for (j = 0; j < BYTEWIDTH(width) * height; j++) {
fprintf(stderr, "/%d", *(data + j));
}
fprintf(stderr, "\n");
fprintf(stderr, "Checking pixel at %d, %d, (byte %d, mask %d)",
x, y, bytepos, bitmask);
#endif
if (*(data + bytepos) & bitmask) {
#ifdef BIGDEBUG
fprintf(stderr, "-\n");
#endif
return (1);
} else {
#ifdef BIGDEBUG
fprintf(stderr, "#\n");
#endif
return (0);
}
}
/*
* sets a pixel in the width/height data bitmap array, at * rot_x, rot_y
*/
void
set_pixel(rot_x, rot_y, data, width, height)
int rot_x, rot_y;
char *data;
int width, height;
{
int bytepos;
int bitmask;
if (rot_x < 0 || rot_x >= width)
return;
if (rot_y < 0 || rot_y >= height)
return;
bytepos = rot_x * BYTEWIDTH(width) + rot_y / 8;
bitmask = 1 << (rot_y % 8);
*(data + bytepos) |= bitmask;
return;
}
#ifndef NO_READ_BITMAPFILE
/*
* Shamelessy grabbed from the source code to 'bitmap' from the * mit X
* standard Distribution...
*/
int
ReadBitmapFile(filename)
char *filename;
{
unsigned int width, height;
int x_hot, y_hot;
unsigned char *data;
int status;
status = XmuReadBitmapDataFromFile(filename, &width, &height, &data,
&x_hot, &y_hot);
if (status != BitmapSuccess)
return status;
image.width = width;
image.height = height;
image.data = (char *) data;
image.bytes_per_line = (image.width + 7) / 8;
raster_length = image.bytes_per_line * image.height;
x_hot_spot = x_hot;
y_hot_spot = y_hot;
return BitmapSuccess;
}
void
WriteXImageFile(char *filename, XImage image) {
FILE *file;
register int i;
int num_bytes = 0;
file = fopen(filename, "w");
if (file == NULL)
{
fprintf(stderr, "Ack! Couldn't save %s!\n", filename);
}
#ifdef DEBUG
fprintf(stderr, "Writing to file %s (%d)\n", filename, file);
#endif
fprintf(file, "#define %s_width %d\n", stripped_name, rotatedimage.width);
fprintf(file, "#define %s_height %d\n", stripped_name, rotatedimage.height);
if (x_hot_spot != OUTTA_RANGE)
fprintf(file, "#define %s_x_hot %d\n", stripped_name, x_hot_spot);
if (y_hot_spot != OUTTA_RANGE)
fprintf(file, "#define %s_y_hot %d\n", stripped_name, y_hot_spot);
fprintf(file, "static char %s_bits[] = {\n 0x%02x",
stripped_name, (unsigned char) rotatedimage.data[0]);
num_bytes = BYTEWIDTH(rotatedimage.width) * rotatedimage.height;
#ifdef DEBUG
fprintf(stderr, "number of bytes: %d\n", num_bytes);
#endif
for (i = 1; i < num_bytes; i++) {
fprintf(file, ",");
fprintf(file, (i % 12) ? " " : "\n ");
fprintf(file, "0x%02x", (unsigned char) rotatedimage.data[i]);
}
fprintf(file, "};\n");
}
double
Sin(int angle) {
return (sin((float) angle * PI / 128));
}
double Cos(int angle) {
return (cos((float) angle * PI / 128));
}
void usage()
{
fprintf(stderr, "Usage: rotatebitmap [-f filterlevel] angle bitmaplist\n");
fprintf(stderr, "This program takes the bitmap files given to it, and\n");
fprintf(stderr, "rotates them, outputting them as [name].angle \n");
fprintf(stderr, "NOTE: angle is in units such that 0-255 describes a full circle \n");
exit(1);
}
#endif /* NO_READ_BITMAPFILE */
#endif