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
  
     | 
    
      /*
 *
 *  This file is part of the XForms library package.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with XForms; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 */
/*
 * $Id: image_rotate.c,v 1.0 2002/05/08 05:16:30 zhao Release $
 *
 *.
 *  This file is part of the XForms library package.
 *  Copyright (c) 1993, 1998-2002 By  T.C. Zhao
 *  All rights reserved.
 *.
 *
 *      Image rotation
 */
#include "forms.h"
#include "flimage.h"
#include "flimage_int.h"
#include <stdlib.h>
#include <math.h>
#ifndef M_PI
#define M_PI   3.1415926
#endif
static void *rotate_matrix(void *m, int row, int col, int deg, size_t e);
/* rotation angle is one-tenth of a degree */
int
flimage_rotate(FL_IMAGE * im, int deg, int subp)
{
    int nw, nh;
    float mat[2][2];
    void *r, *g = 0, *b = 0;
    /* normalize the angle to within (0,360) */
    while (deg < 0)
	deg += 3600;
    while (deg >= 3600)
	deg -= 3600;
    if (deg == 0 || deg == 3600)
	return 0;
    /* handle special cases TODO */
    if ((deg % 900) == 0)
    {
	deg /= 10;
	if (im->type == FL_IMAGE_RGB)
	{
	    r = rotate_matrix(im->red, im->h, im->w, deg, sizeof(**im->red));
	    g = rotate_matrix(im->green, im->h, im->w, deg, sizeof(**im->red));
	    b = rotate_matrix(im->blue, im->h, im->w, deg, sizeof(**im->red));
	}
	else
	{
	    r = rotate_matrix(im->ci, im->h, im->w, deg, sizeof(**im->ci));
	}
	if ((deg % 180) == 0)
	{
	    nw = im->w;
	    nh = im->h;
	}
	else
	{
	    nw = im->h;
	    nh = im->w;
	}
	if (!r)
	    return -1;
	flimage_replace_image(im, nw, nh, r, g, b);
	return 0;
    }
    /* three shear is slightly faster , but with general transform, 
     * we save a lot of code */
    mat[0][0] = mat[1][1] = cos(deg * M_PI / 1800.0);
    mat[0][1] = sin(deg * M_PI / 1800.0);
    mat[1][0] = -mat[0][1];
    nw = nh = 0;
    if (flimage_warp(im, mat, nw, nh, subp) >= 0)
    {
	im->completed = im->h;
	im->visual_cue(im, "Rotation Done");
	return 0;
    }
    else
	return -1;
}
static int flip_matrix(void *, int, int, int, int);
int
flimage_flip(FL_IMAGE * im, int axis)
{
    int err = 0;
    if (im->type == FL_IMAGE_RGB)
    {
	err = flip_matrix(im->red, im->h, im->w, 1, axis) < 0;
	err = err || flip_matrix(im->green, im->h, im->w, 1, axis) < 0;
	err = err || flip_matrix(im->blue, im->h, im->w, 1, axis) < 0;
    }
    else if (FL_IsGray(im->type))
    {
	err = flip_matrix(im->gray, im->h, im->w, 2, axis) < 0;
    }
    else
    {
	err = flip_matrix(im->ci, im->h, im->w, 2, axis) < 0;
    }
    if (!err)
	im->modified = 1;
    return err ? -1 : 0;
}
/* low level matrix stuff */
#define flip_col(type, matrix, rows, cols)                             \
      do  {                                                            \
         type **mm = matrix, *t, *h, tmp; int j;                       \
         for ( j = 0; j < rows; j++)                                   \
            for( t = ( h = mm[j]) + (cols-1); t > h; t--, h++)         \
            {                                                          \
               tmp = *h; *h = *t; *t = tmp;                            \
            }                                                          \
      } while (0)
/* flip a matrix. Mirror about the x or y */
static int
flip_matrix(void *matrix, int rows, int cols, int esize, int what)
{
    if (what == 'c' || what == 'x')	/* flip columns */
    {
	if (esize == 2)
	    flip_col(unsigned short, matrix, rows, cols);
	else
	    flip_col(unsigned char, matrix, rows, cols);
    }
    else
    {
	int i, is = (rows / 2);
	size_t size = esize * cols;
	unsigned char *tmp, **mm = matrix;
	if (!(tmp = fl_malloc(size)))
	    return -1;
	/* if we only flip the pointers, this will be faster, but then, the
	   1-d property is violated */
	for (i = 0; i < is; i++)
	{
	    memcpy(tmp, mm[i], size);
	    memcpy(mm[i], mm[rows - 1 - i], size);
	    memcpy(mm[rows - 1 - i], tmp, size);
	}
	fl_free(tmp);
    }
    return 0;
}
/* special angles: +-90, +-180 */
/***************************************************************
 * rotate a matrix by 90, or -90 or multiples of it
 * Rotate 180 can be implented as two 90 rotations, but current
 * code is faster.
 *
 * NOTE: input dimension is the diemsnion of the matrix to be
 *       rotated. caller must take care of the rotated dimensions
 **************************************************************/
/*  Rotate 90 degrees */
#define DO_M90(type, out, in)                                          \
           do {                                                       \
             register type *p= ((type **)out)[0], **o= in;             \
             register int i, j;                                       \
             for ( j= 0; j < col; j++) {                              \
                for (i= row-1; i>=0; i--) *p++ = o[i][j];             \
             }                                                        \
           } while (0)
/*  Rotate -90 degrees */
#define DO_90(type, out, in)                                         \
           do {                                                       \
              register type *p= ((type **) out)[0], **o= in;          \
              register int i, j;                                      \
              for (j= col - 1; j >= 0; j--) {                         \
                 for (i= 0; i < row; i++) *p++ = o[i][j];             \
               }                                                      \
           } while (0)
#define DO_180(ty, out, in)                                           \
           do {                                                       \
             register ty *p= ((ty **)out)[0], *o=((ty **)in)[0];       \
             register  ty *os;                                        \
             for (os= o + col*row-1 ; os>o; *p++ = *os--) ;           \
           } while(0)
static void *
rotate_matrix(void *m, int row, int col, int deg, size_t e)
{
    int nrow = row, ncol = col;
    void *mm;
    /*
     * Coerce angle to be +/- 360.
     */
    deg %= 360;
    /*
     * Now coerce it into +/- 180.
     */
    if ( abs( deg ) > 180 ) {
	if ( deg < 0 )
	    deg += 360;
	else
	    deg -= 360;
    }
    if (deg == 90 || deg == -90)
    {
	nrow = col;
	ncol = row;
    }
    if (!(mm = fl_get_matrix(nrow, ncol, e)))
	return 0;
    if (deg == 90)
    {
	if (e == 2)
	    DO_90(unsigned short, mm, m);
	else
	    DO_90(unsigned char, mm, m);
    }
    else if (deg == -90)
    {
	if (e == 2)
	    DO_M90(unsigned short, mm, m);
	else
	    DO_M90(unsigned char, mm, m);
    }
    else if (deg == 180 || deg == -180)
    {
	if (e == 2)
	    DO_180(unsigned short, mm, m);
	else
	    DO_180(unsigned char, mm, m);
    }
    else
    {
	M_err("RotateMatrix", "InternalError: bad special angle\n");
	return 0;
    }
    return mm;
}
 
     |