1 /*
2   emboss.c
3 
4   Emboss Magic Tool Plugin
5   Tux Paint - A simple drawing program for children.
6 
7   Copyright (c) 2002-2019 by Bill Kendrick and others; see AUTHORS.txt
8   bill@newbreedsoftware.com
9   http://www.tuxpaint.org/
10 
11   This program is free software; you can redistribute it and/or modify
12   it under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2 of the License, or
14   (at your option) any later version.
15 
16   This program is distributed in the hope that it will be useful,
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   GNU General Public License for more details.
20 
21   You should have received a copy of the GNU General Public License
22   along with this program; if not, write to the Free Software
23   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   (See COPYING.txt)
25 
26   Last updated: August 29, 2019
27   $Id$
28 */
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include "tp_magic_api.h"
33 #include "SDL_image.h"
34 #include "SDL_mixer.h"
35 
36 /* Our globals: */
37 
38 static Mix_Chunk *emboss_snd;
39 
40 // Prototypes
41 Uint32 emboss_api_version(void);
42 int emboss_init(magic_api * api);
43 int emboss_get_tool_count(magic_api * api);
44 SDL_Surface *emboss_get_icon(magic_api * api, int which);
45 char *emboss_get_name(magic_api * api, int which);
46 char *emboss_get_description(magic_api * api, int which, int mode);
47 
48 void emboss_drag(magic_api * api, int which, SDL_Surface * canvas,
49                  SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect);
50 
51 void emboss_click(magic_api * api, int which, int mode,
52                   SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
53 
54 void emboss_release(magic_api * api, int which,
55                     SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
56 
57 void emboss_shutdown(magic_api * api);
58 void emboss_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
59 int emboss_requires_colors(magic_api * api, int which);
60 void emboss_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
61 void emboss_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
62 int emboss_modes(magic_api * api, int which);
63 
64 
emboss_api_version(void)65 Uint32 emboss_api_version(void)
66 {
67   return (TP_MAGIC_API_VERSION);
68 }
69 
70 
71 // No setup required:
emboss_init(magic_api * api)72 int emboss_init(magic_api * api)
73 {
74   char fname[1024];
75 
76   snprintf(fname, sizeof(fname), "%s/sounds/magic/emboss.ogg", api->data_directory);
77   emboss_snd = Mix_LoadWAV(fname);
78 
79   return (1);
80 }
81 
82 // We have multiple tools:
emboss_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)83 int emboss_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
84 {
85   return (1);
86 }
87 
88 // Load our icons:
emboss_get_icon(magic_api * api,int which ATTRIBUTE_UNUSED)89 SDL_Surface *emboss_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
90 {
91   char fname[1024];
92 
93   snprintf(fname, sizeof(fname), "%s/images/magic/emboss.png", api->data_directory);
94 
95   return (IMG_Load(fname));
96 }
97 
98 // Return our names, localized:
emboss_get_name(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED)99 char *emboss_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
100 {
101   return (strdup(gettext_noop("Emboss")));
102 }
103 
104 // Return our descriptions, localized:
emboss_get_description(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED,int mode ATTRIBUTE_UNUSED)105 char *emboss_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
106 {
107   return (strdup(gettext_noop("Click and drag the mouse to emboss the picture.")));
108 }
109 
110 // Do the effect:
111 
do_emboss(void * ptr,int which ATTRIBUTE_UNUSED,SDL_Surface * canvas,SDL_Surface * last,int x,int y)112 static void do_emboss(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last, int x, int y)
113 {
114   magic_api *api = (magic_api *) ptr;
115   int xx, yy;
116   Uint8 r1, g1, b1, r2, g2, b2;
117   int r;
118   float h, s, v;
119   int avg1, avg2;
120 
121   for (yy = -16; yy < 16; yy++)
122     {
123       for (xx = -16; xx < 16; xx++)
124         {
125           if (api->in_circle(xx, yy, 16))
126             {
127               if (!api->touched(x + xx, y + yy))
128                 {
129                   SDL_GetRGB(api->getpixel(last, x + xx, y + yy), last->format, &r1, &g1, &b1);
130                   SDL_GetRGB(api->getpixel(last, x + xx + 2, y + yy + 2), last->format, &r2, &g2, &b2);
131 
132                   avg1 = (r1 + g1 + b1) / 3;
133                   avg2 = (r2 + g2 + b2) / 3;
134 
135                   api->rgbtohsv(r1, g1, b1, &h, &s, &v);
136 
137                   r = 128 + (((avg1 - avg2) * 3) / 2);
138                   if (r < 0)
139                     r = 0;
140                   if (r > 255)
141                     r = 255;
142 
143                   v = (r / 255.0);
144 
145                   api->hsvtorgb(h, s, v, &r1, &g1, &b1);
146 
147                   api->putpixel(canvas, x + xx, y + yy, SDL_MapRGB(canvas->format, r1, g1, b1));
148                 }
149             }
150         }
151     }
152 }
153 
154 // Affect the canvas on drag:
emboss_drag(magic_api * api,int which,SDL_Surface * canvas,SDL_Surface * last,int ox,int oy,int x,int y,SDL_Rect * update_rect)155 void emboss_drag(magic_api * api, int which, SDL_Surface * canvas,
156                  SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect)
157 {
158   api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_emboss);
159 
160   if (ox > x)
161     {
162       int tmp = ox;
163 
164       ox = x;
165       x = tmp;
166     }
167   if (oy > y)
168     {
169       int tmp = oy;
170 
171       oy = y;
172       y = tmp;
173     }
174 
175   update_rect->x = ox - 16;
176   update_rect->y = oy - 16;
177   update_rect->w = (x + 16) - update_rect->x;
178   update_rect->h = (y + 16) - update_rect->h;
179 
180   api->playsound(emboss_snd, (x * 255) / canvas->w, 255);
181 }
182 
183 // Affect the canvas on click:
emboss_click(magic_api * api,int which,int mode ATTRIBUTE_UNUSED,SDL_Surface * canvas,SDL_Surface * last,int x,int y,SDL_Rect * update_rect)184 void emboss_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
185                   SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect)
186 {
187   emboss_drag(api, which, canvas, last, x, y, x, y, update_rect);
188 }
189 
190 // Affect the canvas on release:
emboss_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)191 void emboss_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
192                     SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
193                     int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
194 {
195 }
196 
197 // No setup happened:
emboss_shutdown(magic_api * api ATTRIBUTE_UNUSED)198 void emboss_shutdown(magic_api * api ATTRIBUTE_UNUSED)
199 {
200   if (emboss_snd != NULL)
201     Mix_FreeChunk(emboss_snd);
202 }
203 
204 // Record the color from Tux Paint:
emboss_set_color(magic_api * api ATTRIBUTE_UNUSED,Uint8 r ATTRIBUTE_UNUSED,Uint8 g ATTRIBUTE_UNUSED,Uint8 b ATTRIBUTE_UNUSED)205 void emboss_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED,
206                       Uint8 b ATTRIBUTE_UNUSED)
207 {
208 }
209 
210 // Use colors:
emboss_requires_colors(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED)211 int emboss_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
212 {
213   return 0;
214 }
215 
emboss_switchin(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED,int mode ATTRIBUTE_UNUSED,SDL_Surface * canvas ATTRIBUTE_UNUSED)216 void emboss_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
217                      SDL_Surface * canvas ATTRIBUTE_UNUSED)
218 {
219 }
220 
emboss_switchout(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED,int mode ATTRIBUTE_UNUSED,SDL_Surface * canvas ATTRIBUTE_UNUSED)221 void emboss_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
222                       SDL_Surface * canvas ATTRIBUTE_UNUSED)
223 {
224 }
225 
emboss_modes(magic_api * api ATTRIBUTE_UNUSED,int which ATTRIBUTE_UNUSED)226 int emboss_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
227 {
228   return (MODE_PAINT);          /* FIXME - Can also be turned into a full-image effect */
229 }
230