scale-size-keepaspect.c 3.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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
14
 * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
15 16
 *
 * Copyright 2014 Jon Nordby, The Grid <jononor@gmail.com>
17
 *           2017 Øyvind Kolås <pippin@gimp.org>
18 19 20 21 22
 */

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

23
#ifdef GEGL_PROPERTIES
24

25 26 27 28 29 30 31 32 33
property_double (x, _("X"), -1.0)
    description (_("Horizontal size"))
    ui_range (0.0, 20.0)
    value_range (-9000.0, 9000.0)

property_double (y, _("Y"), -1.0)
    description (_("Vertical size"))
    ui_range (0.0, 20.0)
    value_range (-9000.0, 9000.0)
34 35 36

#else

37
#include "gegl-operation-filter.h"
38
#include "scale.h"
39
#define GEGL_OP_NO_SOURCE
40 41
#define GEGL_OP_Parent  OpScale
#define GEGL_OP_PARENT  TYPE_OP_SCALE
42 43 44 45 46
#define GEGL_OP_NAME    scale_size_keepaspect
#define GEGL_OP_BUNDLE
#define GEGL_OP_C_FILE  "scale-size-keepaspect.c"

#include "gegl-op.h"
47 48 49 50 51 52 53

#include <math.h>

static void
create_matrix (OpTransform *op,
               GeglMatrix3 *matrix)
{
54
  GeglProperties *o = GEGL_PROPERTIES (op);
55 56 57 58 59 60 61 62 63 64 65 66 67 68
  GeglOperation *operation  = GEGL_OPERATION (op);
  GeglRectangle  in_rect = {0,0,0,0};
  gdouble height_over_width = 1.0;

  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;

  height_over_width = in_rect.height/(gdouble)in_rect.width;
69
  if (o->x <= 0.0 && o->y <= 0.0) {
70 71 72
    // No dimensions specified, pass through
    matrix->coeff [0][0] = 1.0;
    matrix->coeff [1][1] = 1.0;
73
  } else if (o->x <= 0.0 && o->y > 0.0) {
74
    // X free, Y specified
75
    const gdouble target_x = o->y / height_over_width;
76
    matrix->coeff [0][0] = target_x / (gdouble) in_rect.width;
77 78
    matrix->coeff [1][1] = o->y / (gdouble) in_rect.height;
  } else if (o->y <= 0.0 && o->x > 0.0) {
79
    // Y free, X specified
80 81
    const gdouble target_y = o->x * height_over_width;
    matrix->coeff [0][0] = o->x / (gdouble) in_rect.width;
82 83 84
    matrix->coeff [1][1] = target_y / (gdouble) in_rect.height;
  } else {
    // Fully specified
85 86
    matrix->coeff [0][0] = o->x / (gdouble) in_rect.width;
    matrix->coeff [1][1] = o->y / (gdouble) in_rect.height;
87 88 89
  }
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#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:scale-size-keepaspect",
    "title", _("Scale size keep aspect"),
    "categories", "transform",
106
    "reference-hash", "21055a7509bb983967ab03a37200931a",
107
    "description", _("Scales the buffer to a size, preserving aspect ratio"),
108
    "reference-chain", "load path=images/standard-input.png scale-size-keepaspect x=140 y=-1",
109 110 111
    NULL);
}

112
#endif