source: tuxpaint/trunk/fuentes/magic/src/emboss.c @ 219

Last change on this file since 219 was 219, checked in by mabarracus, 4 years ago

added sources from trusty

File size: 6.4 KB
Line 
1/*
2  emboss.c
3
4  Emboss Magic Tool Plugin
5  Tux Paint - A simple drawing program for children.
6
7  Copyright (c) 2002-2008 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: July 8, 2008
27  $Id: emboss.c,v 1.12 2011/11/26 22:04:50 perepujal Exp $
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
38static Mix_Chunk * emboss_snd;
39
40// Prototypes
41Uint32 emboss_api_version(void);
42int emboss_init(magic_api * api);
43int emboss_get_tool_count(magic_api * api);
44SDL_Surface * emboss_get_icon(magic_api * api, int which);
45char * emboss_get_name(magic_api * api, int which);
46char * emboss_get_description(magic_api * api, int which, int mode);
47
48void emboss_drag(magic_api * api, int which, SDL_Surface * canvas,
49                  SDL_Surface * last, int ox, int oy, int x, int y,
50                  SDL_Rect * update_rect);
51
52void emboss_click(magic_api * api, int which, int mode,
53                   SDL_Surface * canvas, SDL_Surface * last,
54                   int x, int y, SDL_Rect * update_rect);
55
56void emboss_release(magic_api * api, int which,
57                   SDL_Surface * canvas, SDL_Surface * last,
58                   int x, int y, SDL_Rect * update_rect);
59
60void emboss_shutdown(magic_api * api);
61void emboss_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
62int emboss_requires_colors(magic_api * api, int which);
63void emboss_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
64void emboss_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
65int emboss_modes(magic_api * api, int which);
66
67
68Uint32 emboss_api_version(void) { return(TP_MAGIC_API_VERSION); }
69
70
71// No setup required:
72int emboss_init(magic_api * api)
73{
74  char fname[1024];
75
76  snprintf(fname, sizeof(fname), "%s/sounds/magic/emboss.ogg",
77            api->data_directory);
78  emboss_snd = Mix_LoadWAV(fname);
79
80  return(1);
81}
82
83// We have multiple tools:
84int emboss_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
85{
86  return(1);
87}
88
89// Load our icons:
90SDL_Surface * emboss_get_icon(magic_api * api, int which ATTRIBUTE_UNUSED)
91{
92  char fname[1024];
93
94  snprintf(fname, sizeof(fname), "%s/images/magic/emboss.png",
95           api->data_directory);
96
97  return(IMG_Load(fname));
98}
99
100// Return our names, localized:
101char * emboss_get_name(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
102{
103  return(strdup(gettext_noop("Emboss")));
104}
105
106// Return our descriptions, localized:
107char * emboss_get_description(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
108{
109  return(strdup(gettext_noop("Click and drag the mouse to emboss the picture.")));
110}
111
112// Do the effect:
113
114static void do_emboss(void * ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas, SDL_Surface * last,
115                int x, int y)
116{
117  magic_api * api = (magic_api *) ptr;
118  int xx, yy;
119  Uint8 r1, g1, b1,
120        r2, g2, b2;
121  int r, g, b;
122  float h, s, v;
123  int avg1, avg2;
124
125  for (yy = -16; yy < 16; yy++)
126  {
127    for (xx = -16; xx < 16; xx++)
128    {
129      if (api->in_circle(xx, yy, 16))
130      {
131        if (!api->touched(x + xx, y + yy))
132        {
133          SDL_GetRGB(api->getpixel(last, x + xx, y + yy), last->format, &r1, &g1, &b1);
134          SDL_GetRGB(api->getpixel(last, x + xx + 2, y + yy + 2), last->format, &r2, &g2, &b2);
135
136          avg1 = (r1 + g1 + b1) / 3;
137          avg2 = (r2 + g2 + b2) / 3;
138
139          api->rgbtohsv(r1, g1, b1, &h, &s, &v);
140
141          r = 128 + (((avg1 - avg2) * 3) / 2);
142          if (r < 0) r = 0;
143          if (r > 255) r = 255;
144          g = b = r;
145
146          v = (r / 255.0);
147
148          api->hsvtorgb(h, s, v, &r1, &g1, &b1);
149
150          api->putpixel(canvas, x + xx, y + yy, SDL_MapRGB(canvas->format, r1, g1, b1));
151        }
152      }
153    }
154  }
155}
156
157// Affect the canvas on drag:
158void emboss_drag(magic_api * api, int which, SDL_Surface * canvas,
159                  SDL_Surface * last, int ox, int oy, int x, int y,
160                  SDL_Rect * update_rect)
161{
162  api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_emboss);
163
164  if (ox > x) { int tmp = ox; ox = x; x = tmp; }
165  if (oy > y) { int tmp = oy; oy = y; y = tmp; }
166
167  update_rect->x = ox - 16;
168  update_rect->y = oy - 16;
169  update_rect->w = (x + 16) - update_rect->x;
170  update_rect->h = (y + 16) - update_rect->h;
171
172  api->playsound(emboss_snd,
173                 (x * 255) / canvas->w, 255);
174}
175
176// Affect the canvas on click:
177void emboss_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
178                   SDL_Surface * canvas, SDL_Surface * last,
179                   int x, int y, SDL_Rect * update_rect)
180{
181  emboss_drag(api, which, canvas, last, x, y, x, y, update_rect);
182}
183
184// Affect the canvas on release:
185void emboss_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
186                   SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
187                   int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
188{
189}
190
191// No setup happened:
192void emboss_shutdown(magic_api * api ATTRIBUTE_UNUSED)
193{
194  if (emboss_snd != NULL)
195    Mix_FreeChunk(emboss_snd);
196}
197
198// Record the color from Tux Paint:
199void emboss_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED)
200{
201}
202
203// Use colors:
204int emboss_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
205{
206  return 0;
207}
208
209void emboss_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
210{
211}
212
213void emboss_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
214{
215}
216
217int emboss_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
218{
219  return(MODE_PAINT); /* FIXME - Can also be turned into a full-image effect */
220}
Note: See TracBrowser for help on using the repository browser.