Added noise and mosaic magic tools
This commit is contained in:
parent
774c076406
commit
4c71ecdba1
4 changed files with 513 additions and 84 deletions
235
magic/src/mosaic.c
Normal file
235
magic/src/mosaic.c
Normal file
|
|
@ -0,0 +1,235 @@
|
|||
/*
|
||||
mosaic.c
|
||||
|
||||
mosaic, Add a mosaic effect to the image using a combination of other tools.
|
||||
Requires the mosaicAll sharpen and noise tools.
|
||||
Tux Paint - A simple drawing program for children.
|
||||
|
||||
Copyright (c) 2002-2007 by Bill Kendrick and others; see AUTHORS.txt
|
||||
bill@newbreedsoftware.com
|
||||
http://www.tuxpaint.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
(See COPYING.txt)
|
||||
|
||||
Last updated: June 6, 2008
|
||||
$Id$
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <libintl.h>
|
||||
#include "tp_magic_api.h"
|
||||
#include "SDL_image.h"
|
||||
#include "SDL_mixer.h"
|
||||
#include <math.h>
|
||||
#include <limits.h>
|
||||
#include <time.h>
|
||||
#include "noise.c"
|
||||
#include "sharpen.c"
|
||||
#include "blur.c"
|
||||
|
||||
#ifndef gettext_noop
|
||||
#define gettext_noop(String) String
|
||||
#endif
|
||||
|
||||
|
||||
double mosaic_AMOUNT= 30.0;
|
||||
const int mosaic_RADIUS = 16;
|
||||
|
||||
enum {
|
||||
TOOL_MOSAIC,
|
||||
mosaic_NUM_TOOLS
|
||||
};
|
||||
|
||||
static Mix_Chunk * mosaic_snd_effect[mosaic_NUM_TOOLS];
|
||||
|
||||
const char * mosaic_snd_filenames[mosaic_NUM_TOOLS] = {
|
||||
"flip.wav",
|
||||
};
|
||||
const char * mosaic_icon_filenames[mosaic_NUM_TOOLS] = {
|
||||
"flip.png",
|
||||
};
|
||||
const char * mosaic_names[mosaic_NUM_TOOLS] = {
|
||||
gettext_noop("Mosaic"),
|
||||
};
|
||||
const char * mosaic_descs[mosaic_NUM_TOOLS] = {
|
||||
gettext_noop("Click to add a mosaic effect to the image."),
|
||||
};
|
||||
|
||||
Uint32 mosaic_api_version(void) { return(TP_MAGIC_API_VERSION); }
|
||||
|
||||
//Load sounds
|
||||
int mosaic_init(magic_api * api){
|
||||
|
||||
int i;
|
||||
char fname[1024];
|
||||
|
||||
for (i = 0; i < mosaic_NUM_TOOLS; i++){
|
||||
snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, mosaic_snd_filenames[i]);
|
||||
mosaic_snd_effect[i] = Mix_LoadWAV(fname);
|
||||
}
|
||||
|
||||
sharpen_init(api);
|
||||
|
||||
return(1);
|
||||
}
|
||||
|
||||
int mosaic_get_tool_count(magic_api * api){
|
||||
return(mosaic_NUM_TOOLS);
|
||||
}
|
||||
|
||||
// Load our icons:
|
||||
SDL_Surface * mosaic_get_icon(magic_api * api, int which){
|
||||
char fname[1024];
|
||||
snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, mosaic_icon_filenames[which]);
|
||||
return(IMG_Load(fname));
|
||||
}
|
||||
|
||||
// Return our names, localized:
|
||||
char * mosaic_get_name(magic_api * api, int which){
|
||||
return(strdup(gettext(mosaic_names[which])));
|
||||
}
|
||||
|
||||
// Return our descriptions, localized:
|
||||
char * mosaic_get_description(magic_api * api, int which){
|
||||
return(strdup(gettext(mosaic_descs[which])));
|
||||
}
|
||||
|
||||
//Do the effect for one pixel
|
||||
static void do_mosaic_pixel(void * ptr, int which,
|
||||
SDL_Surface * canvas, SDL_Surface * last,
|
||||
int x, int y){
|
||||
magic_api * api = (magic_api *) ptr;
|
||||
int i;
|
||||
for (i=0;i<3;i++){
|
||||
do_noise_pixel(ptr, 0, canvas, last, x, y);
|
||||
}
|
||||
do_blur_pixel(ptr, 0, canvas, last, x, y);
|
||||
do_sharpen_pixel(ptr, 1, canvas, canvas, x, y);
|
||||
}
|
||||
|
||||
// Do the effect for the full image
|
||||
static void do_mosaic_full(void * ptr, SDL_Surface * canvas, SDL_Surface * last, int which){
|
||||
|
||||
magic_api * api = (magic_api *) ptr;
|
||||
|
||||
int x,y;
|
||||
|
||||
for (y = 0; y < last->h; y++){
|
||||
for (x=0; x < last->w; x++){
|
||||
do_mosaic_pixel(ptr, which, canvas, last, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//do the effect for the brush
|
||||
static void do_mosaic_brush(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y){
|
||||
int xx, yy;
|
||||
magic_api * api = (magic_api *) ptr;
|
||||
|
||||
for (yy = y - mosaic_RADIUS; yy < y + mosaic_RADIUS; yy++)
|
||||
{
|
||||
for (xx = x - mosaic_RADIUS; xx < x + mosaic_RADIUS; xx++)
|
||||
{
|
||||
if (api->in_circle(xx - x, yy - y, mosaic_RADIUS) &&
|
||||
!api->touched(xx, yy))
|
||||
{
|
||||
do_mosaic_pixel(api, which, canvas, last, xx, yy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Affect the canvas on drag:
|
||||
void mosaic_drag(magic_api * api, int which, SDL_Surface * canvas,
|
||||
SDL_Surface * last, int ox, int oy, int x, int y,
|
||||
SDL_Rect * update_rect){
|
||||
|
||||
api->line((void *) api, which, canvas, last, ox, oy, x, y, 1, do_mosaic_brush);
|
||||
|
||||
api->playsound(mosaic_snd_effect[which], (x * 255) / canvas->w, 255);
|
||||
|
||||
if (ox > x) { int tmp = ox; ox = x; x = tmp; }
|
||||
if (oy > y) { int tmp = oy; oy = y; y = tmp; }
|
||||
|
||||
update_rect->x = ox - mosaic_RADIUS;
|
||||
update_rect->y = oy - mosaic_RADIUS;
|
||||
update_rect->w = (x + mosaic_RADIUS) - update_rect->x;
|
||||
update_rect->h = (y + mosaic_RADIUS) - update_rect->y;
|
||||
}
|
||||
|
||||
// Affect the canvas on click:
|
||||
void mosaic_click(magic_api * api, int which, int mode,
|
||||
SDL_Surface * canvas, SDL_Surface * last,
|
||||
int x, int y, SDL_Rect * update_rect){
|
||||
if (mode == MODE_PAINT){
|
||||
mosaic_drag(api, which, canvas, last, x, y, x, y, update_rect);
|
||||
}
|
||||
else{
|
||||
update_rect->x = 0;
|
||||
update_rect->y = 0;
|
||||
update_rect->w = canvas->w;
|
||||
update_rect->h = canvas->h;
|
||||
do_mosaic_full(api, canvas, last, which);
|
||||
api->playsound(mosaic_snd_effect[which], 128, 255);
|
||||
}
|
||||
}
|
||||
|
||||
// Affect the canvas on release:
|
||||
void mosaic_release(magic_api * api, int which,
|
||||
SDL_Surface * canvas, SDL_Surface * last,
|
||||
int x, int y, SDL_Rect * update_rect)
|
||||
{
|
||||
}
|
||||
|
||||
// No setup happened:
|
||||
void mosaic_shutdown(magic_api * api)
|
||||
{
|
||||
//Clean up sounds
|
||||
int i;
|
||||
for(i=0; i<mosaic_NUM_TOOLS; i++){
|
||||
if(mosaic_snd_effect[i] != NULL){
|
||||
Mix_FreeChunk(mosaic_snd_effect[i]);
|
||||
}
|
||||
}
|
||||
sharpen_shutdown(api);
|
||||
}
|
||||
|
||||
// Record the color from Tux Paint:
|
||||
void mosaic_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b)
|
||||
{
|
||||
}
|
||||
|
||||
// Use colors:
|
||||
int mosaic_requires_colors(magic_api * api, int which)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mosaic_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas)
|
||||
{
|
||||
}
|
||||
|
||||
void mosaic_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas)
|
||||
{
|
||||
}
|
||||
|
||||
int mosaic_modes(magic_api * api, int which)
|
||||
{
|
||||
return(MODE_FULLSCREEN|MODE_PAINT);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue