12024
|
1 /*
|
|
2 * affine.c -- Affine Transforms for 2d objects
|
|
3 * Copyright (C) 2002 Charles Yates <charles.yates@pandora.be>
|
|
4 * Portions Copyright (C) 2003 Dan Dennedy <dan@dennedy.org>
|
|
5 * ported from C++ to C
|
|
6 * wrote affine_scale()
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software Foundation,
|
|
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
21 */
|
|
22
|
|
23 #include "affine.h"
|
|
24
|
|
25 static inline void Multiply( affine_transform_t *this, affine_transform_t *that )
|
|
26 {
|
|
27 double output[2][2];
|
|
28 register int i, j;
|
|
29
|
|
30 for ( i = 0; i < 2; i ++ )
|
|
31 for ( j = 0; j < 2; j ++ )
|
|
32 output[ i ][ j ] = this->matrix[ i ][ 0 ] * that->matrix[ j ][ 0 ] +
|
|
33 this->matrix[ i ][ 1 ] * that->matrix[ j ][ 1 ];
|
|
34
|
|
35 this->matrix[ 0 ][ 0 ] = output[ 0 ][ 0 ];
|
|
36 this->matrix[ 0 ][ 1 ] = output[ 0 ][ 1 ];
|
|
37 this->matrix[ 1 ][ 0 ] = output[ 1 ][ 0 ];
|
|
38 this->matrix[ 1 ][ 1 ] = output[ 1 ][ 1 ];
|
|
39 }
|
|
40
|
|
41 void affine_transform_init( affine_transform_t *this )
|
|
42 {
|
|
43 this->matrix[ 0 ][ 0 ] = 1;
|
|
44 this->matrix[ 0 ][ 1 ] = 0;
|
|
45 this->matrix[ 1 ][ 0 ] = 0;
|
|
46 this->matrix[ 1 ][ 1 ] = 1;
|
|
47 }
|
|
48
|
|
49 // Rotate by a given angle
|
|
50 void affine_transform_rotate( affine_transform_t *this, double angle )
|
|
51 {
|
|
52 affine_transform_t affine;
|
|
53 affine.matrix[ 0 ][ 0 ] = cos( angle * M_PI / 180 );
|
|
54 affine.matrix[ 0 ][ 1 ] = 0 - sin( angle * M_PI / 180 );
|
|
55 affine.matrix[ 1 ][ 0 ] = sin( angle * M_PI / 180 );
|
|
56 affine.matrix[ 1 ][ 1 ] = cos( angle * M_PI / 180 );
|
|
57 Multiply( this, &affine );
|
|
58 }
|
|
59
|
|
60 // Shear by a given value
|
|
61 void affine_transform_shear( affine_transform_t *this, double shear )
|
|
62 {
|
|
63 affine_transform_t affine;
|
|
64 affine.matrix[ 0 ][ 0 ] = 1;
|
|
65 affine.matrix[ 0 ][ 1 ] = shear;
|
|
66 affine.matrix[ 1 ][ 0 ] = 0;
|
|
67 affine.matrix[ 1 ][ 1 ] = 1;
|
|
68 Multiply( this, &affine );
|
|
69 }
|
|
70
|
|
71 void affine_transform_scale( affine_transform_t *this, double sx, double sy )
|
|
72 {
|
|
73 affine_transform_t affine;
|
|
74 affine.matrix[ 0 ][ 0 ] = sx;
|
|
75 affine.matrix[ 0 ][ 1 ] = 0;
|
|
76 affine.matrix[ 1 ][ 0 ] = 0;
|
|
77 affine.matrix[ 1 ][ 1 ] = sy;
|
|
78 Multiply( this, &affine );
|
|
79 }
|
|
80
|
|
81 // Obtain the mapped x coordinate of the input
|
|
82 double affine_transform_mapx( affine_transform_t *this, int x, int y )
|
|
83 {
|
|
84 return this->matrix[0][0] * x + this->matrix[0][1] * y;
|
|
85 }
|
|
86
|
|
87 // Obtain the mapped y coordinate of the input
|
|
88 double affine_transform_mapy( affine_transform_t *this, int x, int y )
|
|
89 {
|
|
90 return this->matrix[1][0] * x + this->matrix[1][1] * y;
|
|
91 }
|
|
92
|
|
93 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
|
|
94
|
|
95 void affine_scale( const unsigned char *src, unsigned char *dest, int src_width, int src_height, int dest_width, int dest_height, int bpp )
|
|
96 {
|
|
97 affine_transform_t affine;
|
|
98 double scale_x = (double) dest_width / (double) src_width;
|
|
99 double scale_y = (double) dest_height / (double) src_height;
|
|
100 register unsigned char *d = dest;
|
|
101 register const unsigned char *s = src;
|
|
102 register int i, j, k, x, y;
|
|
103
|
|
104 affine_transform_init( &affine );
|
|
105
|
|
106 if ( scale_x <= 1.0 && scale_y <= 1.0 )
|
|
107 {
|
|
108 affine_transform_scale( &affine, scale_x, scale_y );
|
|
109
|
|
110 for( j = 0; j < src_height; j++ )
|
|
111 for( i = 0; i < src_width; i++ )
|
|
112 {
|
|
113 x = (int) ( affine_transform_mapx( &affine, i - src_width/2, j - src_height/2 ) );
|
|
114 y = (int) ( affine_transform_mapy( &affine, i - src_width/2, j - src_height/2 ) );
|
|
115 x += dest_width/2;
|
|
116 x = CLAMP( x, 0, dest_width);
|
|
117 y += dest_height/2;
|
|
118 y = CLAMP( y, 0, dest_height);
|
|
119 s = src + (j*src_width*bpp) + i*bpp; // + (bpp-1);
|
|
120 d = dest + y*dest_width*bpp + x*bpp;
|
|
121 for ( k = 0; k < bpp; k++ )
|
|
122 *d++ = *s++;
|
|
123 }
|
|
124 }
|
|
125 else if ( scale_x > 1.0 && scale_y > 1.0 )
|
|
126 {
|
|
127 affine_transform_scale( &affine, 1.0/scale_x, 1.0/scale_y );
|
|
128
|
|
129 for( y = 0; y < dest_height; y++ )
|
|
130 for( x = 0; x < dest_width; x++ )
|
|
131 {
|
|
132 i = (int) ( affine_transform_mapx( &affine, x - dest_width/2, y - dest_height/2 ) );
|
|
133 j = (int) ( affine_transform_mapy( &affine, x - dest_width/2, y - dest_height/2 ) );
|
|
134 i += src_width/2;
|
|
135 i = CLAMP( i, 0, dest_width);
|
|
136 j += src_height/2;
|
|
137 j = CLAMP( j, 0, dest_height);
|
|
138 s = src + (j*src_width*bpp) + i*bpp; // + (bpp-1);
|
|
139 d = dest + y*dest_width*bpp + x*bpp;
|
|
140 for ( k = 0; k < bpp; k++ )
|
|
141 *d++ = *s++;
|
|
142 }
|
|
143 }
|
|
144 }
|