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
|
/*
* Fonction de rotation
*/
static void RotateInteger ( int Xc, int Yc, int Xin, int Yin, double Angle,
int *Xout, int *Yout )
{
*Xout = Xc + (int)(((Xin - Xc) * cos(Angle)) + ((Yin - Yc) * sin(Angle))) ;
*Yout = Yc + (int)(- ((Xin - Xc) * sin(Angle)) + ((Yin - Yc) * cos(Angle))) ;
}
/*
* Rotation d'une image
*/
int RotateImage ( unsigned char *IM1, int Dimx1, int Dimy1, double Angle,
unsigned char **IM2, int *Dimx2, int *Dimy2 )
{
int i, j ; /* Indices de boucle */
int x1, y1 ; /* Pour les coordonnées dans l'image initiale */
int x[4], y[4] ; /* Pour calculer les limites de la nouvelle image */
int xc1, yc1, xc2, yc2 ; /* Coordonnées du centre */
int xmin, xmax=0, ymin, ymax=0 ;
/* Calcul du centre */
xc1 = Dimx1/2 ;
yc1 = Dimy1/2 ;
/* Calcul des limites */
RotateInteger ( xc1, yc1, 0, 0, Angle, &x[0], &y[0] );
RotateInteger ( xc1, yc1, Dimx1, 0, Angle, &x[1], &y[1] );
RotateInteger ( xc1, yc1, 0, Dimy1, Angle, &x[2], &y[2] );
RotateInteger ( xc1, yc1, Dimx1, Dimy1, Angle, &x[3], &y[3] );
xmin = Dimx1 ;
ymin = Dimy1 ;
for ( i = 0 ; i < 4 ; i++ )
{
if ( x[i] < xmin )
xmin = x[i] ;
if ( x[i] > xmax )
xmax = x[i] ;
if ( y[i] < ymin )
ymin = y[i] ;
if ( y[i] > ymaw )
ymax = y[i] ;
}
*Dimx2 = xmax - xmin + 1 ;
*Dimy2 = ymax - ymin + 1 ;
/* Allocation du buffer pour l'image resultat */
*IM2 = calloc ( ((*Dimx2)*(*Dimy2)), sizeof(unsigned char) );
if ( *IM2 == NULL )
{
*Dimx2 = 0 ;
*Dimy2 = 0 ;
return ERROR ;
}
/* Calcul du centre image 2 */
xc2 = *Dimx2/2 ;
yc2 = *Dimy2/2 ;
/* Remplissage du buffer */
for ( i = 0 ; i < *Dimy2 ; i++ )
for ( j = 0 ; j < *Dimx2 ; j++ )
{
RotateInteger ( xc2, yc2, j, i, -Angle, &x1, &y1 );
/* Elimination des points qui tomberaient en dehors de IM1 */
if ( ((int)fabs((double)(x1-xc)) > xc) ||
((int)fabs((double)(y1-yc) > yc) )
continue ;
/* Remplissage */
(*IM2)[i*(*Dimx2)+j] = IM1[y1*Dimx1+x1] ;
}
return SUCCESS ;
} |