-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathx_rotate.c
582 lines (506 loc) · 18.7 KB
/
x_rotate.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*@
* Copyright(c) 1995-1997 Gregory M. Messner
* All rights reserved
*
* Permission to use, copy, modify and distribute this material for
* non-commercial personal and educational use without fee is hereby
* granted, provided that the above copyright notice and this permission
* notice appear in all copies, and that the name of Gregory M. Messner
* not be used in advertising or publicity pertaining to this material
* without the specific, prior written permission of Gregory M. Messner
* or an authorized representative.
*
* GREGORY M. MESSNER MAKES NO REPRESENTATIONS AND EXTENDS NO WARRANTIES,
* EXPRESS OR IMPLIED, WITH RESPECT TO THE SOFTWARE, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* ANY PARTICULAR PURPOSE, AND THE WARRANTY AGAINST INFRINGEMENT OF PATENTS
* OR OTHER INTELLECTUAL PROPERTY RIGHTS. THE SOFTWARE IS PROVIDED "AS IS",
* AND IN NO EVENT SHALL GREGORY M. MESSNER BE LIABLE FOR ANY DAMAGES,
* INCLUDING ANY LOST PROFITS OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES
* RELATING TO THE SOFTWARE.
*
*/
/*************************************************************************\
* Copyright (c) 1994-2004 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 1997-2003 Southeastern Universities Research Association,
* as Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 1997-2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/* +++FILEHDR+++
*
* Filename: x_rotate.c
*
* Description:
* This is the source file for X rotation functions.
*
*
*
* Changes
*
* By Date Description
* ------ -------------- ----------------------------------------------
* GMM 07/10/95 Original file.
* GMM 07/13/95 Fixed memory leak where XFreeFont() was not
* being called after call to XQueryFont().
* This fixed by replacing XQueryFont() and
* XTextWidth() with XQueryTextExtents(). Also
* performed some code optimization.
*
*
* ---FHDR--- */
#include <string.h>
#include <stdlib.h>
#include <X11/Intrinsic.h>
#ifdef _HPUX_SOURCE
# include <Xm/Xm.h>
#endif
#include "x_rotate.h"
/* +++MANHDR+++
*
*
* NAME
* XgDrawRotatedText - Draw rotated and aligned text
*
*
* SYNOPSIS
* int XgDrawRotatedText(dpy, drawable, gc, x, y, alignment,
* degrees, text, len)
* Display *dpy;
* Drawable drawable;
* GC gc;
* int x;
* int y;
* int alignment;
* int degrees;
* char *text;
* int len;
*
*
* ARGUMENTS
* dpy Specifies the connection to the X server.
*
* drawable Specifies the drawable.
*
* gc Specifies the GC.
*
* x
* y Specify the x and y coordinates, which are rela-
* tive to the origin of the specified drawable.
*
* alignment Specifies the allignment for the text. Possible
* values are XgALIGN_CENTER, XgALIGN_LEFT, or
* XgALIGN_RIGHT.
*
* degrees Specifies the degrees of rotation for drawing the
* text. Possible values are 0, 90, 180, or 270.
*
* text Specifies the character string to draw.
*
* len Specifies the number of characters in the text
* argument. A -1 value will cause XgDrawRotatedText
* to calculate the length of the string.
*
* DESCRIPTION
* XgDrawRotatedText draws a rotated and aligned string.
* It draws the characters in the foreground and fills the
* bounding box with the background.
*
*
* RETURN VALUE
* On success, zero is returned. On error, -1 is returned.
*
*
* ---MANHDR--- */
#ifndef _NO_PROTO
int
XgDrawRotatedText(Display *dpy, Drawable drawable, GC gc, int x, int y,
int alignment, int degrees, char *text, int len)
#else
int
XgDrawRotatedText(dpy, drawable, gc, x, y, alignment, degrees, text, len)
Display *dpy;
Drawable drawable;
GC gc;
int x;
int y;
int alignment;
int degrees;
char *text;
int len;
#endif
{
XImage *text_image;
Pixmap bitmap;
int direction, font_height, font_descent, font_ascent;
int text_len;
int w, h, width, height;
int image_x, image_y;
XGCValues values;
char *image_data;
XCharStruct overall;
static GC font_gc = None;
/*
* -1 as the length means we compute the string length
*/
if ( len == -1 )
len = strlen(text);
/*
* Get the font ID from the passed GC
*/
if ( XGetGCValues(dpy, gc, GCFont, &values) == 0 )
return -1;
/*
* Get the extents info for the text
*/
XQueryTextExtents(dpy, values.font, text, len,
&direction, &font_ascent, &font_descent, &overall);
font_height = font_ascent + font_descent;
text_len = overall.width;
/*
* If rotation is 0 degrees then draw it normally, but
* adjust for alignment
*/
if ( degrees == 0 )
{
/*
* Adjust for alignment
*/
if ( alignment == XgALIGN_CENTER )
x -= text_len / 2;
else if ( alignment == XgALIGN_LEFT )
x = x;
else if ( alignment == XgALIGN_RIGHT )
x -= text_len;
/*
* Draw the text
*/
XDrawImageString(dpy, drawable, gc, x, y, text, len);
return 0;
}
/*
* Create a 1 plane pixmap to render the normal text in
*/
w = text_len;
h = font_height;
bitmap = XCreatePixmap(dpy, DefaultRootWindow(dpy), w, h, 1);
if ( bitmap == None )
return -1;
/*
* Create a 1 plane GC for drawing the text(only once)
*/
if ( font_gc == None )
{
XGCValues font_values;
/*
* Create GC against this 1 plane pixmap, we set the
* foreground to 1 and the background to 0 because that's
* what an XImage expects
*/
font_values.background = 0;
font_values.foreground = 1;
font_values.font = values.font;
font_gc = XCreateGC(dpy, bitmap,
GCFont | GCForeground | GCBackground, &font_values);
if ( font_gc == None )
{
XFreePixmap(dpy, bitmap);
return -1;
}
}
else
{
XSetFont(dpy, font_gc, values.font);
}
/*
* Draw the text into the 1 plane pixmap we just created
*/
XDrawImageString(dpy, bitmap, font_gc, 0, font_ascent, text, len);
/*
* Round the width and height to 8 bit boundries, the rotation
* algorithim requires this
*/
width = ((w + 7) / 8) * 8;
height = ((h + 7) / 8) * 8;
/*
* Create an XImage to hold the text for rotation, use calloc
* becuase we need the bitmap zeroed out (cleared)
*/
image_data = (char *)calloc((width * height) / 8, 1);
text_image = XCreateImage(dpy, DefaultVisual(dpy, 0), 1,
XYBitmap, 0, image_data, width, height, 8, 0);
/*
* Now get the text out of the pixmap and put it in the XImage
*/
text_image->bitmap_bit_order = MSBFirst;
text_image->byte_order = MSBFirst;
text_image->xoffset = 0;
XGetSubImage(dpy, bitmap, 0, 0,
w, h, AllPlanes, XYPixmap, text_image, 0, 0);
/*
* We don't need the pixmap (bitmap) anymore, free it
*/
XFreePixmap(dpy, bitmap);
/*
* Now rotate the XImage (that is the text, boys)
*/
XgRotateXImage(text_image, degrees);
/*
* Did we do a 90 or 270 degree rotation? Yea then swap width/height
* We also adjust things for the 8 bit boundry that we rounded to
*/
if ( degrees == 90 || degrees == 270 )
{
int tmp_w;
/*
* Swap width and height
*/
tmp_w = width;
width = height;
height = tmp_w;
tmp_w = w;
w = h;
h = tmp_w;
text_image->width = width;
text_image->height = height;
text_image->bytes_per_line = width / 8;
/*
* Adjust for alignment
*/
if ( degrees == 90 )
{
image_y = 0;
if ( alignment == XgALIGN_CENTER )
y -= text_len / 2;
else if ( alignment == XgALIGN_RIGHT )
y -= text_len;
}
else
{
image_y = height - h;
if ( alignment == XgALIGN_LEFT )
y -= text_len;
else if ( alignment == XgALIGN_CENTER )
y -= text_len / 2;
}
}
else
{
image_y = height - h;
/*
* Adjust for alignment
*/
if ( alignment == XgALIGN_CENTER )
x -= text_len / 2;
else if ( alignment == XgALIGN_RIGHT )
x -= text_len;
}
/*
* Now image the text by using XPutImage with the
* image we rotated on the passed drawable
*/
text_image->format = XYBitmap;
image_x = width - w;
XPutImage(dpy, drawable, gc, text_image,
image_x, image_y, x, y, w, h);
/*
* Don't need the XImage anymore, destroy it
*/
/* Free the memory we allocated to insure it is done
* with the same routines that allocated it. Can be
* a problem on WIN32 if X frees it. */
if(text_image->data) free(text_image->data);
text_image->data=NULL;
/* Then destroy the image */
XDestroyImage(text_image);
return 0;
}
/*
* This table is used with the 180 degree rotation stuff
*/
static unsigned char reverse_bits[] = {
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0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0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
};
/************************************************************************
*
* Function Name: XgRotateXImage()
*
* Description:
* Given a pointer to an XImage structure, rotate it the mentioned
* number of degrees clockwise.
*
* This version assumes MSB-first data all around.
*
************************************************************************/
int
XgRotateXImage(image, degrees)
XImage *image;
int degrees;
{
register unsigned char *bitmap;
int width, height;
register unsigned char *newbufp;
unsigned char *bitmap_save;
int nbytes, row_inc, saved_nbyterows;
/*
* We only do 90, 180, and 270 degrees here
*/
if ( degrees != 90 && degrees != 270 && degrees != 180 )
return -1;
/*
* We do not have the capability of rotating bitmaps whose dimensions
* are not even multiples of 8 (one byte). If either of these two
* parameters are not a muliple of 8 return with error.
*/
width = (int)image->width;
height = (int)image->height;
if ( width % 8 || height % 8 )
{
return -1;
}
/*
* If rotation is 180 then we can do it in place
*/
if ( degrees == 180 )
{
register unsigned char *p, *q;
width = width / 8;
bitmap = (unsigned char *)image->data;
for ( p = bitmap, q = (bitmap + height * width) - 1; p <= q; )
{
register unsigned char c = *p;
*p++ = reverse_bits[*q];
*q-- = reverse_bits[c];
}
return 0;
}
/*
* Set everything up
*/
bitmap = (unsigned char *)image->data;
bitmap_save = bitmap;
nbytes = width * height / 8;
row_inc = (height / 8);
saved_nbyterows = (width / 8);
/*
* Try and allocate a buffer to rotate to, if not successful then
* get outa here with an error code
*/
if ( newbufp = (unsigned char *) malloc(nbytes), newbufp == NULL )
return -1;
memset(newbufp, 0, nbytes);
if ( degrees == 90 )
{
/*
* This outer loop runs once for each horizontal line of bits
* in the original image. The input goes left->right,
* top->bottom, and in the output image it fills top->bottom,
* right->left.
*/
while ( height-- > 0 )
{
int nbyterows = saved_nbyterows;
register unsigned char *outp = newbufp + (height / 8);
unsigned int mask = 1 << (7 - (height % 8));
while (nbyterows-- > 0)
{
register unsigned int c = *bitmap++;
/*
* As a special case, we don't have to do anything
* if the byte to be set is zero.
*/
if ( !c )
{
outp += (row_inc * 8);
continue;
}
if (c & 0x80) *outp |= mask; outp += row_inc;
if (c & 0x40) *outp |= mask; outp += row_inc;
if (c & 0x20) *outp |= mask; outp += row_inc;
if (c & 0x10) *outp |= mask; outp += row_inc;
if (c & 0x08) *outp |= mask; outp += row_inc;
if (c & 0x04) *outp |= mask; outp += row_inc;
if (c & 0x02) *outp |= mask; outp += row_inc;
if (c & 0x01) *outp |= mask; outp += row_inc;
}
}
}
else
{
/*
* This outer loop runs once for each horizontal line of bits in
* the original image. The input goes left->right, top->bottom,
* and in the output image it fills bottom->top, left->right.
*/
height += 7;
while ( height-- > 7 )
{
int nbyterows = saved_nbyterows;
register unsigned char *outp = newbufp + nbytes - (height / 8);
unsigned int mask = 1 << (height % 8);
while (nbyterows-- > 0)
{
register unsigned int c = *bitmap++;
/*
* As a special case, we don't have to do anything
* if the byte to be set is zero.
*/
if ( !c )
{
outp -= (row_inc * 8);
continue;
}
if (c & 0x80) *outp |= mask; outp -= row_inc;
if (c & 0x40) *outp |= mask; outp -= row_inc;
if (c & 0x20) *outp |= mask; outp -= row_inc;
if (c & 0x10) *outp |= mask; outp -= row_inc;
if (c & 0x08) *outp |= mask; outp -= row_inc;
if (c & 0x04) *outp |= mask; outp -= row_inc;
if (c & 0x02) *outp |= mask; outp -= row_inc;
if (c & 0x01) *outp |= mask; outp -= row_inc;
}
}
}
/*
* Now we have copied the entire bitmap in to the newbufp, so copy
* the modified version back into the user's buffer. Then we can
* free our memory area.
*/
memcpy(bitmap_save, newbufp, nbytes);
free(newbufp);
return 0;
}