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

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

added sources from trusty

File size: 7.9 KB
Line 
1/*
2  blur.c
3//
4  blur, Blur tool
5  Tux Paint - A simple drawing program for children.
6
7  Credits: Bill Kendrick<bill@newbreedsoftware.com> & Andrew Corcoran <akanewbie@gmail.com>
8
9  Copyright (c) 2002-2008 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: July 8, 2008
29  $Id: blur.c,v 1.19 2011/11/26 22:04:50 perepujal Exp $
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// Prototypes
42Uint32 blur_api_version(void);
43int blur_init(magic_api * api);
44int blur_get_tool_count(magic_api * api);
45SDL_Surface * blur_get_icon(magic_api * api, int which);
46char * blur_get_name(magic_api * api, int which);
47char * blur_get_description(magic_api * api, int which, int mode);
48void blur_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);
51void blur_click(magic_api * api, int which, int mode,
52                    SDL_Surface * canvas, SDL_Surface * last,
53                    int x, int y, SDL_Rect * update_rect);
54void blur_release(magic_api * api, int which,
55                   SDL_Surface * canvas, SDL_Surface * last,
56                   int x, int y, SDL_Rect * update_rect);
57void blur_shutdown(magic_api * api);
58void blur_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
59int blur_requires_colors(magic_api * api, int which);
60void blur_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas);
61void blur_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
62int blur_modes(magic_api * api, int which);
63
64enum {
65        TOOL_blur,
66        blur_NUM_TOOLS
67};
68
69static const int blur_RADIUS = 16;
70
71static Mix_Chunk * blur_snd_effect[blur_NUM_TOOLS];
72
73const char *blur_snd_filenames[blur_NUM_TOOLS] = {
74  "blur.wav",
75};
76const char * blur_icon_filenames[blur_NUM_TOOLS] = {
77  "blur.png",
78};
79const char * blur_names[blur_NUM_TOOLS] = {
80  gettext_noop("Blur"),
81};
82const char * blur_descs[blur_NUM_TOOLS][2] = {
83  {gettext_noop("Click and move the mouse around to blur the image."), 
84    gettext_noop("Click to blur the entire image.")},
85};
86
87Uint32 blur_api_version(void) { return(TP_MAGIC_API_VERSION); }
88
89//Load sounds
90int blur_init(magic_api * api){
91
92  int i;
93  char fname[1024];
94
95  for (i = 0; i < blur_NUM_TOOLS; i++){
96    snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, blur_snd_filenames[i]);
97    blur_snd_effect[i] = Mix_LoadWAV(fname);
98  }
99  return(1);
100}
101
102int blur_get_tool_count(magic_api * api ATTRIBUTE_UNUSED){
103  return(blur_NUM_TOOLS);
104}
105
106// Load our icons:
107SDL_Surface * blur_get_icon(magic_api * api, int which){
108  char fname[1024];
109  snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, blur_icon_filenames[which]);
110  return(IMG_Load(fname));
111}
112
113// Return our names, localized:
114char * blur_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){
115    return(strdup(gettext_noop(blur_names[which])));
116}
117
118// Return our descriptions, localized:
119char * blur_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode){
120  return(strdup(gettext_noop(blur_descs[which][mode-1])));
121}
122
123//Do the effect for one pixel
124static void do_blur_pixel(void * ptr, int which ATTRIBUTE_UNUSED, 
125                 SDL_Surface * canvas, SDL_Surface * last,
126                 int x, int y){
127  magic_api * api = (magic_api *) ptr;
128  int i,j,k;
129        Uint8 temp[3];
130  double blurValue[3];
131  //5x5 gaussiann weighting window
132  const int weight[5][5] = {  {1,4,7,4,1},
133                              {4,16,26,16,4},
134                              {7,26,41,26,7},
135                              {4,16,26,16,4},
136                              {1,4,7,4,1}};
137
138  for (k =0;k<3;k++){
139                blurValue[k] = 0;
140  }
141
142  for (i=-2;i<3;i++){
143    for (j=-2;j<3;j++){
144      //Add the pixels around the current one wieghted
145                        SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &temp[0], &temp[1], &temp[2]);
146      for (k =0;k<3;k++){
147                          blurValue[k] += temp[k]* weight[i+2][j+2];
148      }
149    }
150  }
151  for (k =0;k<3;k++){
152    blurValue[k] /= 273;
153  }
154        api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, blurValue[0], blurValue[1], blurValue[2]));
155}
156
157// Do the effect for the full image
158static void do_blur_full(void * ptr,SDL_Surface * canvas, SDL_Surface * last, int which){
159
160        //magic_api * api = (magic_api *) ptr;
161
162        int x,y;
163
164        for (y = 0; y < last->h; y++){
165                for (x=0; x < last->w; x++){
166      do_blur_pixel(ptr, which, canvas, last, x, y);
167          }
168  }
169}
170
171//do the effect for the brush
172static void do_blur_brush(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y){
173  int xx, yy;
174  magic_api * api = (magic_api *) ptr;
175
176  for (yy = y - blur_RADIUS; yy < y + blur_RADIUS; yy++)
177  {
178    for (xx = x - blur_RADIUS; xx < x + blur_RADIUS; xx++)
179    {
180      if (api->in_circle(xx - x, yy - y, blur_RADIUS) &&
181          !api->touched(xx, yy))
182      {
183        do_blur_pixel(api, which, canvas, last, xx, yy);
184      }
185    }
186  }
187}
188
189// Affect the canvas on drag:
190void blur_drag(magic_api * api, int which, SDL_Surface * canvas,
191                  SDL_Surface * last, int ox, int oy, int x, int y,
192                  SDL_Rect * update_rect){
193
194  api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_blur_brush);
195
196  api->playsound(blur_snd_effect[which], (x * 255) / canvas->w, 255);
197
198  if (ox > x) { int tmp = ox; ox = x; x = tmp; }
199  if (oy > y) { int tmp = oy; oy = y; y = tmp; }
200
201  update_rect->x = ox - blur_RADIUS;
202  update_rect->y = oy - blur_RADIUS;
203  update_rect->w = (x + blur_RADIUS) - update_rect->x;
204  update_rect->h = (y + blur_RADIUS) - update_rect->y;
205}
206
207// Affect the canvas on click:
208void blur_click(magic_api * api, int which, int mode,
209                    SDL_Surface * canvas, SDL_Surface * last,
210                    int x, int y, SDL_Rect * update_rect){
211  if (mode == MODE_PAINT)
212    blur_drag(api, which, canvas, last, x, y, x, y, update_rect);
213  else{
214    update_rect->x = 0;
215    update_rect->y = 0;
216    update_rect->w = canvas->w;
217    update_rect->h = canvas->h;
218    do_blur_full(api, canvas,  last, which);
219    api->playsound(blur_snd_effect[which], 128, 255);
220  }
221}
222
223// Affect the canvas on release:
224void blur_release(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
225                   SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
226                   int x ATTRIBUTE_UNUSED, int y ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
227{
228}
229
230// No setup happened:
231void blur_shutdown(magic_api * api ATTRIBUTE_UNUSED)
232{
233        //Clean up sounds
234        int i;
235        for(i=0; i<blur_NUM_TOOLS; i++){
236                if(blur_snd_effect[i] != NULL){
237                        Mix_FreeChunk(blur_snd_effect[i]);
238                }
239        }
240}
241
242// Record the color from Tux Paint:
243void blur_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED)
244{
245}
246
247// Use colors:
248int blur_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
249{
250  return 0;
251}
252
253void blur_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
254{
255}
256
257void blur_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
258{
259}
260
261int blur_modes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED)
262{
263  return(MODE_FULLSCREEN|MODE_PAINT);
264}
Note: See TracBrowser for help on using the repository browser.