1 /*
2 sharpen.c
3
4 Sharpen, Trace Contour and Silhouette Magic Tool Plugin
5 Tux Paint - A simple drawing program for children.
6
7 Credits: Andrew Corcoran <akanewbie@gmail.com>
8
9 Copyright (c) 2002-2009 by Bill Kendrick and others; see AUTHORS.txt
10 bill@newbreedsoftware.com
11 http://www.tuxpaint.org/
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 (See COPYING.txt)
27
28 Last updated: May 6, 2009
29 $Id$
30 */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <libintl.h>
35 #include "tp_magic_api.h"
36 #include "SDL_image.h"
37 #include "SDL_mixer.h"
38 #include <math.h>
39 #include <limits.h>
40
41 #ifndef gettext_noop
42 #define gettext_noop(String) String
43 #endif
44
45 /* Our globals: */
46
47 enum
48 {
49 TOOL_TRACE,
50 TOOL_SHARPEN,
51 TOOL_SILHOUETTE,
52 sharpen_NUM_TOOLS
53 };
54
55 static const int THRESHOLD = 50;
56
57 static const int sharpen_RADIUS = 16;
58
59 static const double SHARPEN = 0.5;
60
61 static Mix_Chunk *sharpen_snd_effect[sharpen_NUM_TOOLS];
62
63 const char *sharpen_snd_filenames[sharpen_NUM_TOOLS] = {
64 "edges.ogg",
65 "sharpen.ogg",
66 "silhouette.ogg"
67 };
68
69 const char *sharpen_icon_filenames[sharpen_NUM_TOOLS] = {
70 "edges.png",
71 "sharpen.png",
72 "silhouette.png"
73 };
74
75 const char *sharpen_names[sharpen_NUM_TOOLS] = {
76 gettext_noop("Edges"),
77 gettext_noop("Sharpen"),
78 gettext_noop("Silhouette")
79 };
80
81 const char *sharpen_descs[sharpen_NUM_TOOLS][2] = {
82 {gettext_noop("Click and drag the mouse to trace edges in parts of your picture."),
83 gettext_noop("Click to trace edges in your entire picture."),},
84 {gettext_noop("Click and drag the mouse to sharpen parts of your picture."),
85 gettext_noop("Click to sharpen the entire picture."),},
86 {gettext_noop("Click and drag the mouse to create a black and white silhouette."),
87 gettext_noop("Click to create a black and white silhouette of your entire picture.")},
88 };
89
90 Uint32 sharpen_api_version(void);
91 int sharpen_init(magic_api * api);
92 int sharpen_get_tool_count(magic_api * api);
93 SDL_Surface *sharpen_get_icon(magic_api * api, int which);
94 char *sharpen_get_name(magic_api * api, int which);
95 char *sharpen_get_description(magic_api * api, int which, int mode);
96 static int sharpen_grey(Uint8 r1, Uint8 g1, Uint8 b1);
97 static void do_sharpen_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y);
98 static void do_sharpen_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which);
99 static void do_sharpen_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y);
100 void sharpen_drag(magic_api * api, int which, SDL_Surface * canvas,
101 SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect);
102 void sharpen_click(magic_api * api, int which, int mode,
103 SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
104
105 void sharpen_release(magic_api * api, int which,
106 SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
107
108 void sharpen_shutdown(magic_api * api);
109 void sharpen_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
110 int sharpen_requires_colors(magic_api * api, int which);
111 void sharpen_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
112 void sharpen_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
113 int sharpen_modes(magic_api * api, int which);
114
sharpen_api_version(void)115 Uint32 sharpen_api_version(void)
116 {
117 return (TP_MAGIC_API_VERSION);
118 }
119
120
121 // No setup required:
sharpen_init(magic_api * api)122 int sharpen_init(magic_api * api)
123 {
124
125 int i;
126 char fname[1024];
127
128 for (i = 0; i < sharpen_NUM_TOOLS; i++)
129 {
130 snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, sharpen_snd_filenames[i]);
131 sharpen_snd_effect[i] = Mix_LoadWAV(fname);
132 }
133
134 return (1);
135 }
136
137 // We have multiple tools:
sharpen_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)138 int sharpen_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
139 {
140 return (sharpen_NUM_TOOLS);
141 }
142
143 // Load our icons:
sharpen_get_icon(magic_api * api,int which)144 SDL_Surface *sharpen_get_icon(magic_api * api, int which)
145 {
146 char fname[1024];
147
148 snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, sharpen_icon_filenames[which]);
149 return (IMG_Load(fname));
150 }
151
152 // Return our names, localized:
sharpen_get_name(magic_api * api ATTRIBUTE_UNUSED,int which)153 char *sharpen_get_name(magic_api * api ATTRIBUTE_UNUSED, int which)
154 {
155 return (strdup(gettext_noop(sharpen_names[which])));
156 }
157
158 // Return our descriptions, localized:
sharpen_get_description(magic_api * api ATTRIBUTE_UNUSED,int which,int mode)159 char *sharpen_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode)
160 {
161 return (strdup(gettext_noop(sharpen_descs[which][mode - 1])));
162 }
163
164 //Calculates the grey scale value for a rgb pixel
sharpen_grey(Uint8 r1,Uint8 g1,Uint8 b1)165 static int sharpen_grey(Uint8 r1, Uint8 g1, Uint8 b1)
166 {
167 return 0.3 * r1 + .59 * g1 + 0.11 * b1;
168 }
169
170 // Do the effect:
do_sharpen_pixel(void * ptr,int which,SDL_Surface * canvas,SDL_Surface * last,int x,int y)171 static void do_sharpen_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y)
172 {
173
174 magic_api *api = (magic_api *) ptr;
175
176 Uint8 r1, g1, b1;
177 int grey;
178 int i, j;
179 double sobel_1 = 0, sobel_2 = 0;
180 double temp;
181
182 //Sobel weighting masks
183 const int sobel_weights_1[3][3] = { {1, 2, 1},
184 {0, 0, 0},
185 {-1, -2, -1}
186 };
187 const int sobel_weights_2[3][3] = { {-1, 0, 1},
188 {-2, 0, 2},
189 {-1, 0, 1}
190 };
191
192 sobel_1 = 0;
193 sobel_2 = 0;
194 for (i = -1; i < 2; i++)
195 {
196 for (j = -1; j < 2; j++)
197 {
198 //No need to check if inside canvas, getpixel does it for us.
199 SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &r1, &g1, &b1);
200 grey = sharpen_grey(r1, g1, b1);
201 sobel_1 += grey * sobel_weights_1[i + 1][j + 1];
202 sobel_2 += grey * sobel_weights_2[i + 1][j + 1];
203 }
204 }
205
206 temp = sqrt(sobel_1 * sobel_1 + sobel_2 * sobel_2);
207 temp = (temp / 1443) * 255.0;
208
209 // set image to white where edge value is below THRESHOLD
210 if (which == TOOL_TRACE)
211 {
212 if (temp < THRESHOLD)
213 {
214 api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 255, 255, 255));
215 }
216 }
217 //Simply display the edge values - provides a nice black and white silhouette image
218 else if (which == TOOL_SILHOUETTE)
219 {
220 api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, temp, temp, temp));
221 }
222 //Add the edge values to the original image, creating a more distinct jump in contrast at edges
223 else if (which == TOOL_SHARPEN)
224 {
225 SDL_GetRGB(api->getpixel(last, x, y), last->format, &r1, &g1, &b1);
226 api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, clamp(0.0, r1 + SHARPEN * temp, 255.0),
227 clamp(0.0, g1 + SHARPEN * temp, 255.0),
228 clamp(0.0, b1 + SHARPEN * temp, 255.0)));
229 }
230 }
231
232 // Do the effect for the full image
do_sharpen_full(void * ptr,SDL_Surface * canvas,SDL_Surface * last,int which)233 static void do_sharpen_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which)
234 {
235
236 // magic_api * api = (magic_api *) ptr;
237
238 int x, y;
239
240 for (y = 0; y < last->h; y++)
241 {
242 for (x = 0; x < last->w; x++)
243 {
244 do_sharpen_pixel(ptr, which, canvas, last, x, y);
245 }
246 }
247 }
248
249 //do the effect for the brush
do_sharpen_brush(void * ptr,int which,SDL_Surface * canvas,SDL_Surface * last,int x,int y)250 static void do_sharpen_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y)
251 {
252 int xx, yy;
253 magic_api *api = (magic_api *) ptr;
254
255 for (yy = y - sharpen_RADIUS; yy < y + sharpen_RADIUS; yy++)
256 {
257 for (xx = x - sharpen_RADIUS; xx < x + sharpen_RADIUS; xx++)
258 {
259 if (api->in_circle(xx - x, yy - y, sharpen_RADIUS) && !api->touched(xx, yy))
260 {
261 do_sharpen_pixel(api, which, canvas, last, xx, yy);
262 }
263 }
264 }
265 }
266
267 // Affect the canvas on drag:
sharpen_drag(magic_api * api,int which,SDL_Surface * canvas,SDL_Surface * last,int ox,int oy,int x,int y,SDL_Rect * update_rect)268 void sharpen_drag(magic_api * api, int which, SDL_Surface * canvas,
269 SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect)
270 {
271
272 api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_sharpen_brush);
273
274 api->playsound(sharpen_snd_effect[which], (x * 255) / canvas->w, 255);
275
276 if (ox > x)
277 {
278 int tmp = ox;
279
280 ox = x;
281 x = tmp;
282 }
283 if (oy > y)
284 {
285 int tmp = oy;
286
287 oy = y;
288 y = tmp;
289 }
290
291 update_rect->x = ox - sharpen_RADIUS;
292 update_rect->y = oy - sharpen_RADIUS;
293 update_rect->w = (x + sharpen_RADIUS) - update_rect->x;
294 update_rect->h = (y + sharpen_RADIUS) - update_rect->y;
295 }
296
297 // Affect the canvas on click:
sharpen_click(magic_api * api,int which,int mode,SDL_Surface * canvas,SDL_Surface * last,int x,int y,SDL_Rect * update_rect)298 void sharpen_click(magic_api * api, int which, int mode,
299 SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect)
300 {
301 if (mode == MODE_PAINT)
302 sharpen_drag(api, which, canvas, last, x, y, x, y, update_rect);
303 else
304 {
305 update_rect->x = 0;
306 update_rect->y = 0;
307 update_rect->w = canvas->w;
308 update_rect->h = canvas->h;
309 do_sharpen_full(api, canvas, last, which);
310 api->playsound(sharpen_snd_effect[which], 128, 255);
311 }
312 }
313
314 // Affect the canvas on release:
sharpen_release(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED,SDL_Surface * canvas ATTRIBUTE_UNUSED,SDL_Surface * last ATTRIBUTE_UNUSED,int x ATTRIBUTE_UNUSED,int y ATTRIBUTE_UNUSED,SDL_Rect * update_rect ATTRIBUTE_UNUSED)315 void sharpen_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
316 SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
317 int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
318 {
319 }
320
321 // No setup happened:
sharpen_shutdown(magic_api * api ATTRIBUTE_UNUSED)322 void sharpen_shutdown(magic_api * api ATTRIBUTE_UNUSED)
323 {
324 //Clean up sounds
325 int i;
326
327 for (i = 0; i < sharpen_NUM_TOOLS; i++)
328 {
329 if (sharpen_snd_effect[i] != NULL)
330 {
331 Mix_FreeChunk(sharpen_snd_effect[i]);
332 }
333 }
334 }
335
336 // Record the color from Tux Paint:
sharpen_set_color(magic_api * api ATTRIBUTE_UNUSED,Uint8 r ATTRIBUTE_UNUSED,Uint8 g ATTRIBUTE_UNUSED,Uint8 b ATTRIBUTE_UNUSED)337 void sharpen_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED,
338 Uint8 b ATTRIBUTE_UNUSED)
339 {
340 }
341
342 // Use colors:
sharpen_requires_colors(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED)343 int sharpen_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
344 {
345 return 0;
346 }
347
sharpen_switchin(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED,int mode ATTRIBUTE_UNUSED,SDL_Surface * canvas ATTRIBUTE_UNUSED)348 void sharpen_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
349 SDL_Surface * canvas ATTRIBUTE_UNUSED)
350 {
351 }
352
sharpen_switchout(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED,int mode ATTRIBUTE_UNUSED,SDL_Surface * canvas ATTRIBUTE_UNUSED)353 void sharpen_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
354 SDL_Surface * canvas ATTRIBUTE_UNUSED)
355 {
356 }
357
sharpen_modes(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED)358 int sharpen_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
359 {
360 return (MODE_FULLSCREEN | MODE_PAINT);
361 }
362