rotate-on-center.c 4.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* This file is part of GEGL
 *
 * GEGL 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 3 of the License, or (at your option) any later version.
 *
 * GEGL 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2014 Jon Nordby, The Grid <jononor@gmail.com>
17 18 19
 *           2015 Vilson Vieira, The Grid <vilson@void.cc>
 *           2017 Øyvind Kolås
 *
20 21 22 23 24
 */

#include "config.h"
#include <glib/gi18n-lib.h>

25
#ifdef GEGL_PROPERTIES
26

27 28 29 30 31 32 33 34
property_double (degrees, _("Degrees"), 0.0)
    description(_("Angle to rotate (counter-clockwise)"))
    ui_range (-180.0, 180.0)

property_double (origin_x, _("origin-x") , 0.0)
    description(_("Ignored. Always uses center of input buffer"))
property_double (origin_y, _("origin-y") , 0.0)
    description(_("Ignored. Always uses center of input buffer"))
35 36 37

#else

38 39 40 41 42 43 44 45 46 47
#include "gegl-operation-filter.h"
#include "transform-core.h"
#define GEGL_OP_NO_SOURCE
#define GEGL_OP_Parent  OpTransform
#define GEGL_OP_PARENT  TYPE_OP_TRANSFORM
#define GEGL_OP_NAME    rotate_on_center
#define GEGL_OP_BUNDLE
#define GEGL_OP_C_FILE  "rotate-on-center.c"

#include "gegl-op.h"
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

#include <math.h>

static void
generate_matrix (GeglMatrix3 *matrix,
                 gdouble degrees,
                 gdouble x,
                 gdouble y,
                 gdouble width,
                 gdouble height)
{
  gint i;
  gdouble radians = degrees * (2 * G_PI / 360.0),
          tx = 0.0,
          ty = 0.0,
          tcoords[4][2];
  /*
   * Find coordinates of each corner of the bounding box around buffer,
   * after the requested rotation.
   */
  tcoords[0][0] = - x*cos (radians) - y*sin (radians);
  tcoords[0][1] =   x*sin (radians) - y*cos (radians);

  tcoords[1][0] =   width*cos (radians) + tcoords[0][0];
  tcoords[1][1] = - width*sin (radians) + tcoords[0][1];

  tcoords[2][0] =   width*cos (radians) + height*sin (radians) + tcoords[0][0];
  tcoords[2][1] = - width*sin (radians) + height*cos (radians) + tcoords[0][1];

  tcoords[3][0] =   height*sin (radians) + tcoords[0][0];
  tcoords[3][1] =   height*cos (radians) + tcoords[0][1];

  /*
   * Find translation needed to make the bounding box stays in the positive
   * quadrant.
   */
  for (i=0; i<G_N_ELEMENTS (tcoords); i++) {
    tx = MIN (tx, tcoords[i][0]);
    ty = MIN (ty, tcoords[i][1]);
  }

  /*
   * Define an affine matrix that:
   * 1. Translates buffer's center to origin
   * 2. Rotates buffer by given degrees
   * 3. Translates to the positive quadrant
   */
  matrix->coeff [0][0] =  cos (radians);
  matrix->coeff [0][1] =  sin (radians);
  matrix->coeff [0][2] = -tx - x*cos (radians) - y*sin (radians);

  matrix->coeff [1][0] = -sin (radians);
  matrix->coeff [1][1] =  cos (radians);
  matrix->coeff [1][2] = -ty + x*sin (radians) - y*cos (radians);

  matrix->coeff [2][0] = 0;
  matrix->coeff [2][1] = 0;
  matrix->coeff [2][2] = 1;
}

108

109 110 111 112
static void
create_matrix (OpTransform *op,
               GeglMatrix3 *matrix)
{
113 114 115
  GeglProperties *o = GEGL_PROPERTIES (op);
  GeglOperation  *operation  = GEGL_OPERATION (op);
  GeglRectangle   in_rect = {0.0,0.0,0.0,0.0};
116 117 118 119 120 121 122 123 124 125 126

  if (gegl_operation_source_get_bounding_box (operation, "input"))
    in_rect = *gegl_operation_source_get_bounding_box (operation, "input");

  // Avoid divide-by-zero
  if (in_rect.width < 1)
    in_rect.width = 1;
  if (in_rect.height < 1)
    in_rect.height = 1;

  generate_matrix (matrix,
127
                   o->degrees,
128 129 130 131 132 133
                   in_rect.width,
                   in_rect.height,
                   in_rect.width,
                   in_rect.height);
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#include <stdio.h>

static void
gegl_op_class_init (GeglOpClass *klass)
{
  GeglOperationClass *operation_class;
  OpTransformClass   *transform_class;

  operation_class = GEGL_OPERATION_CLASS (klass);
  transform_class = OP_TRANSFORM_CLASS (klass);
  transform_class->create_matrix = create_matrix;

  gegl_operation_class_set_keys (operation_class,
    "name",       "gegl:rotate-on-center",
    "title",      _("Rotate on center"),
    "categories", "transform",
150
    "reference-hash", "91b62bf2166fa4173934b5574b81bc13",
151
    "reference-chain", "load path=images/standard-input.png rotate-on-center degrees=30.0 clip-to-input=true",
152 153 154 155
    "description", _("Rotate the buffer around its center, taking care of possible offsets."),
    NULL);
}

156
#endif