1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /*
4 * GThumb
5 *
6 * Copyright (C) 2014 Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23 #include <math.h>
24 #include <gthumb.h>
25 #include "cairo-blur.h"
26 #include "cairo-effects.h"
27 #include "gth-curve.h"
28 #include "gth-file-tool-lomo.h"
29
30
31 static void
lomo_init(GthAsyncTask * task,gpointer user_data)32 lomo_init (GthAsyncTask *task,
33 gpointer user_data)
34 {
35 gimp_op_init ();
36 }
37
38
39 static gpointer
lomo_exec(GthAsyncTask * task,gpointer user_data)40 lomo_exec (GthAsyncTask *task,
41 gpointer user_data)
42 {
43 cairo_surface_t *original;
44 cairo_surface_t *source;
45 GthCurve *curve[GTH_HISTOGRAM_N_CHANNELS];
46 cairo_format_t format;
47 int width;
48 int height;
49 int source_stride;
50 cairo_surface_t *destination;
51 int destination_stride;
52 cairo_surface_t *blurred;
53 int blurred_stride;
54 unsigned char *p_source_line;
55 unsigned char *p_destination_line;
56 unsigned char *p_blurred_line;
57 unsigned char *p_source;
58 unsigned char *p_destination;
59 unsigned char *p_blurred;
60 gboolean cancelled = FALSE;
61 double progress;
62 int x, y;
63 double center_x, center_y, radius, d;
64 int red, green, blue, alpha;
65 int image_red, image_green, image_blue, image_alpha;
66 int layer_red, layer_green, layer_blue, layer_alpha;
67 int temp;
68 int c;
69
70 /* curves layer */
71
72 original = gth_image_task_get_source_surface (GTH_IMAGE_TASK (task));
73 source = _cairo_image_surface_copy (original);
74
75 curve[GTH_HISTOGRAM_CHANNEL_VALUE] = gth_curve_new_for_points (GTH_TYPE_BEZIER, 0);
76 curve[GTH_HISTOGRAM_CHANNEL_RED] = gth_curve_new_for_points (GTH_TYPE_BEZIER, 4, 0, 0, 56, 45, 195, 197, 255, 216);
77 curve[GTH_HISTOGRAM_CHANNEL_GREEN] = gth_curve_new_for_points (GTH_TYPE_BEZIER, 4, 0, 0, 65, 40, 162, 174, 238, 255);
78 curve[GTH_HISTOGRAM_CHANNEL_BLUE] = gth_curve_new_for_points (GTH_TYPE_BEZIER, 4, 0, 0, 68, 79, 210, 174, 255, 255);
79 if (! cairo_image_surface_apply_curves (source, curve, task)) {
80 cairo_surface_destroy (source);
81 cairo_surface_destroy (original);
82 return NULL;
83 }
84
85 format = cairo_image_surface_get_format (source);
86 width = cairo_image_surface_get_width (source);
87 height = cairo_image_surface_get_height (source);
88 source_stride = cairo_image_surface_get_stride (source);
89
90 cairo_surface_destroy (original);
91
92 /* other effects */
93
94 blurred = _cairo_image_surface_copy (source);
95 blurred_stride = cairo_image_surface_get_stride (blurred);
96 if (! _cairo_image_surface_blur (blurred, 1, task)) {
97 cairo_surface_destroy (blurred);
98 cairo_surface_destroy (source);
99 return NULL;
100 }
101
102 center_x = width / 2.0;
103 center_y = height / 2.0;
104 radius = MAX (width, height) / 2.0;
105
106 destination = cairo_image_surface_create (format, width, height);
107 destination_stride = cairo_image_surface_get_stride (destination);
108
109 p_source_line = _cairo_image_surface_flush_and_get_data (source);
110 p_blurred_line = _cairo_image_surface_flush_and_get_data (blurred);
111 p_destination_line = _cairo_image_surface_flush_and_get_data (destination);
112 for (y = 0; y < height; y++) {
113 gth_async_task_get_data (task, NULL, &cancelled, NULL);
114 if (cancelled)
115 break;
116
117 progress = (double) y / height;
118 gth_async_task_set_data (task, NULL, NULL, &progress);
119
120 p_source = p_source_line;
121 p_blurred = p_blurred_line;
122 p_destination = p_destination_line;
123 for (x = 0; x < width; x++) {
124 d = sqrt (SQR (x - center_x) + SQR (y - center_y));
125 d = CLAMP_PIXEL ((d >= radius) ? 0 : 255 - (255.0 * (d / radius)));
126
127 CAIRO_GET_RGBA (p_source, image_red, image_green, image_blue, image_alpha);
128
129 /* blurred image layer with a radial mask (to blur the edges) */
130
131 CAIRO_GET_RGBA (p_blurred, layer_red, layer_green, layer_blue, layer_alpha);
132 layer_alpha = (guchar) (255 - d);
133 image_red = GIMP_OP_NORMAL (layer_red, image_red, layer_alpha);
134 image_green = GIMP_OP_NORMAL (layer_green, image_green, layer_alpha);
135 image_blue = GIMP_OP_NORMAL (layer_blue, image_blue, layer_alpha);
136
137 /* radial grandient layer in overlay mode */
138
139 layer_green = layer_blue = layer_red = d;
140 layer_alpha = 255;
141
142 red = GIMP_OP_SOFT_LIGHT (layer_red, image_red);
143 green = GIMP_OP_SOFT_LIGHT (layer_green, image_green);
144 blue = GIMP_OP_SOFT_LIGHT (layer_blue, image_blue);
145 alpha = ADD_ALPHA (image_alpha, layer_alpha);
146
147 p_destination[CAIRO_RED] = GIMP_OP_NORMAL (red, image_red, alpha);
148 p_destination[CAIRO_GREEN] = GIMP_OP_NORMAL (green, image_green, alpha);
149 p_destination[CAIRO_BLUE] = GIMP_OP_NORMAL (blue, image_blue, alpha);
150 p_destination[CAIRO_ALPHA] = GIMP_OP_NORMAL (255, image_alpha, alpha);
151
152 p_source += 4;
153 p_blurred += 4;
154 p_destination += 4;
155 }
156 p_source_line += source_stride;
157 p_blurred_line += blurred_stride;
158 p_destination_line += destination_stride;
159 }
160
161 if (! cancelled) {
162 cairo_surface_mark_dirty (destination);
163 gth_image_task_set_destination_surface (GTH_IMAGE_TASK (task), destination);
164 }
165
166 cairo_surface_destroy (destination);
167 cairo_surface_destroy (blurred);
168 cairo_surface_destroy (source);
169
170 for (c = GTH_HISTOGRAM_CHANNEL_VALUE; c <= GTH_HISTOGRAM_CHANNEL_BLUE; c++)
171 g_object_unref (curve[c]);
172
173 return NULL;
174 }
175
176
177 void
lomo_add_to_special_effects(GthFilterGrid * grid)178 lomo_add_to_special_effects (GthFilterGrid *grid)
179 {
180 gth_filter_grid_add_filter (grid,
181 GTH_FILTER_GRID_NEW_FILTER_ID,
182 gth_image_task_new (_("Applying changes"), lomo_init, lomo_exec, NULL, NULL, NULL),
183 _("Lomo"),
184 NULL);
185 }
186