Update Chinese (China) translation
[gegl.git] / operations / common / linear-gradient.c
blob196e3b6a7b7110b98533d4e6034598182a985db2
1 /* This file is an image processing operation for GEGL
3 * GEGL is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 3 of the License, or (at your option) any later version.
8 * GEGL is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * 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/>.
16 * Copyright 2008 Øyvind Kolås <pippin@gimp.org>
17 * 2013 Daniel Sabo
20 #include "config.h"
21 #include <glib/gi18n-lib.h>
23 #ifdef GEGL_PROPERTIES
25 property_double (start_x, _("X1"), 25.0)
26 ui_meta("unit", "pixel-coordinate")
27 ui_meta("axis", "x")
29 property_double (start_y, _("Y1"), 25.0)
30 ui_meta("unit", "pixel-coordinate")
31 ui_meta("axis", "y")
33 property_double (end_x, _("X2"), 150.0)
34 ui_meta("unit", "pixel-coordinate")
35 ui_meta("axis", "x")
37 property_double (end_y, _("Y2"), 150.0)
38 ui_meta ("unit", "pixel-coordinate")
39 ui_meta ("axis", "y")
41 property_color (start_color, _("Start Color"), "black")
42 description (_("The color at (x1, y1)"))
43 ui_meta ("role", "color-primary")
45 property_color (end_color, _("End Color"), "white")
46 description (_("The color at (x2, y2)"))
47 ui_meta ("role", "color-secondary")
49 #else
51 #define GEGL_OP_POINT_RENDER
52 #define GEGL_OP_NAME linear_gradient
53 #define GEGL_OP_C_SOURCE linear-gradient.c
55 #include "gegl-op.h"
57 static void
58 prepare (GeglOperation *operation)
60 gegl_operation_set_format (operation, "output", babl_format ("R'G'B'A float"));
63 static GeglRectangle
64 get_bounding_box (GeglOperation *operation)
66 return gegl_rectangle_infinite_plane ();
69 static gboolean
70 process (GeglOperation *operation,
71 void *out_buf,
72 glong n_pixels,
73 const GeglRectangle *roi,
74 gint level)
76 GeglProperties *o = GEGL_PROPERTIES (operation);
77 gfloat *out_pixel = out_buf;
78 gfloat color1[4], color2[4], length, dx, dy;
79 gfloat factor = 1.0/(1 << level);
81 dx = (o->end_x - o->start_x) * factor;
82 dy = (o->end_y - o->start_y) * factor;
84 length = dx * dx + dy * dy;
86 if (GEGL_FLOAT_IS_ZERO (length))
88 memset (out_buf, 0, n_pixels * sizeof(float) * 4);
90 else
92 gfloat vec0 = dx / length;
93 gfloat vec1 = dy / length;
94 gint x, y;
96 gegl_color_get_pixel (o->start_color, babl_format ("R'G'B'A float"), color1);
97 gegl_color_get_pixel (o->end_color, babl_format ("R'G'B'A float"), color2);
99 for (y = roi->y; y < roi->y + roi->height; ++y)
101 for (x = roi->x; x < roi->x + roi->width; ++x)
103 gint c;
104 gfloat v = vec0 * (x - o->start_x * factor) + vec1 * (y - o->start_y * factor);
106 if (v > 1.0f - GEGL_FLOAT_EPSILON)
107 v = 1.0f;
108 if (v < 0.0f + GEGL_FLOAT_EPSILON)
109 v = 0.0f;
111 for (c = 0; c < 4; c++)
112 out_pixel[c] = color2[c] * v + color1[c] * (1.0f - v);
114 out_pixel += 4;
119 return TRUE;
122 static void
123 gegl_op_class_init (GeglOpClass *klass)
125 GeglOperationClass *operation_class;
126 GeglOperationPointRenderClass *point_render_class;
128 operation_class = GEGL_OPERATION_CLASS (klass);
129 point_render_class = GEGL_OPERATION_POINT_RENDER_CLASS (klass);
131 point_render_class->process = process;
132 operation_class->get_bounding_box = get_bounding_box;
133 operation_class->prepare = prepare;
135 gegl_operation_class_set_keys (operation_class,
136 "name", "gegl:linear-gradient",
137 "title", _("Linear Gradient"),
138 "categories", "render:gradient",
139 "position-dependent", "true",
140 "reference-hash", "f53de20993b50915061e67e69ab006f4",
141 "reference-hashB", "7c514dcf1a0d580fe52476084246a0f4",
142 "description" , _("Linear gradient renderer"),
143 NULL);
146 #endif