Path: utzoo!attcan!uunet!cs.utexas.edu!csd4.milw.wisc.edu!bionet!agate!shelby!portia!Jessica!stergios From: stergios@Jessica.stanford.edu (stergios marinopoulos) Newsgroups: comp.windows.x Subject: Re: Rotated Fonts With X Summary: algorithm & code for rotating bitmap Keywords: Fonts X rotate BATMAN Message-ID: <3144@portia.Stanford.EDU> Date: 24 Jun 89 23:11:03 GMT References: <432@wizard.UUCP> Sender: USENET News System Reply-To: stergios@Jessica.UUCP (stergios marinopoulos) Organization: Stanford University Lines: 59 Print text to a pixmap, grab the image, rotate it, send it back over the wire. Here is how to rotate a bitmap image. Simply iterate over the origonal and apply the rotation tranformation for each pixel. unsigned char image[WIDTH][HEIGHT]; rotate(angle) double angle; /* rotate image 'angle' degrees */ { int color; int xx,yy; for(y = 0;y < HEIGHT; y ++) for(x = 0;x < WIDTH; x ++) { color = image[xx][yy]; xx = x * cos(angle) - y * sin(angle); yy = x * sin(angle) + y * cos(angle); setpixel(xx,yy,color); } } This method will leave "holes" in the rotated image since there is no strict mapping of integral values between arbitary coordinate systems. This algorithm can be generalized and improved (eliminating the `holes') quite easily; simply iterate over the output points rather than the input points. Thus, for (P = each point in the output) { P2 = TRANSFORM(P) ; /* can include rotations, scaling, etc */ setpixel(destination, P, getpixel(source, P2)) ; } TRANSFORM is the usual six-element matrix, that rounds the point after it's done. The resulting bitmap won't be beautiful. Better results can often be obtained by interpolating between source pixels. For instance, if the unrounded transformed result was (2.3, 4.6), then a better looking image might be obtained by using .3[.6[g(2,4),g(2,5)],.6[g(3,4),g(3,5)]] where n[a,b] means ((1-n) * a + n * b) and g(x,y) means the value of the source at pixel (x,y). The image may be more `blurry' this way, though . . . If you are scaling up (to more bits), this type of interpolation is probably not a bad idea. If you are scaling down (to less bits), it can give odd effects; you might want to high-pass filter the image before scaling it down. A lot of the good info above comes from Tom Rokicki (rokicki@polya.stanford.edu) sm stergios@jessica.stanford.edu