Fisheye: Support sizes

This commit is contained in:
Bill Kendrick 2023-04-22 12:24:47 -07:00
parent d4db61b084
commit 44bb340e33
2 changed files with 66 additions and 43 deletions

View file

@ -44,6 +44,7 @@ https://tuxpaint.org/
+ Darken, Lighten + Darken, Lighten
+ Distortion + Distortion
+ Emboss + Emboss
+ Fisheye
+ Foam + Foam
+ Googly Eyes + Googly Eyes
+ Kaleidoscope, Symmetric L/R & U/D, Pattern, Tiles + Kaleidoscope, Symmetric L/R & U/D, Pattern, Tiles

View file

@ -25,7 +25,7 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
(See COPYING.txt) (See COPYING.txt)
Last updated: February 12, 2023 Last updated: April 22, 2023
*/ */
#include <math.h> #include <math.h>
@ -35,12 +35,13 @@
Mix_Chunk *fisheye_snd; Mix_Chunk *fisheye_snd;
int last_x, last_y; int last_x, last_y;
int fisheye_radius = 80;
/* Local function prototypes */ /* Local function prototypes */
Uint32 fisheye_api_version(void); Uint32 fisheye_api_version(void);
void fisheye_set_color(magic_api * api, int which, SDL_Surface * canvas, void fisheye_set_color(magic_api * api, int which, SDL_Surface * canvas,
SDL_Surface * last, Uint8 r, Uint8 g, Uint8 b, SDL_Rect * update_rect); SDL_Surface * last, Uint8 r, Uint8 g, Uint8 b, SDL_Rect * update_rect);
int fisheye_init(magic_api * api); int fisheye_init(magic_api * api, Uint32 disabled_features);
int fisheye_get_tool_count(magic_api * api); int fisheye_get_tool_count(magic_api * api);
SDL_Surface *fisheye_get_icon(magic_api * api, int which); SDL_Surface *fisheye_get_icon(magic_api * api, int which);
char *fisheye_get_name(magic_api * api, int which); char *fisheye_get_name(magic_api * api, int which);
@ -63,6 +64,9 @@ void fisheye_switchin(magic_api * api, int which, int mode,
void fisheye_switchout(magic_api * api, int which, int mode, void fisheye_switchout(magic_api * api, int which, int mode,
SDL_Surface * canvas); SDL_Surface * canvas);
int fisheye_modes(magic_api * api, int which); int fisheye_modes(magic_api * api, int which);
Uint8 fisheye_accepted_sizes(magic_api * api, int which, int mode);
Uint8 fisheye_default_size(magic_api * api, int which, int mode);
void fisheye_set_size(magic_api * api, int which, int mode, SDL_Surface * canvas, SDL_Surface * last, Uint8 size, SDL_Rect * update_rect);
// Housekeeping functions // Housekeeping functions
@ -81,7 +85,7 @@ void fisheye_set_color(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNU
{ {
} }
int fisheye_init(magic_api * api) int fisheye_init(magic_api * api, Uint32 disabled_features ATTRIBUTE_UNUSED)
{ {
char fname[1024]; char fname[1024];
@ -155,39 +159,38 @@ void fisheye_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas,
SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y) SDL_Surface * last ATTRIBUTE_UNUSED, int x, int y)
{ {
magic_api *api = (magic_api *) ptr; magic_api *api = (magic_api *) ptr;
SDL_Surface *oryg, *temp_src = NULL, *temp_dest = NULL, *output;
SDL_Surface *oryg, *temp_src, *temp_dest, *output;
SDL_Rect rect, temp_rect; SDL_Rect rect, temp_rect;
int xx, yy; int xx, yy;
unsigned short int i; unsigned short int i;
if (api->in_circle(last_x - x, last_y - y, 80)) if (api->in_circle(last_x - x, last_y - y, fisheye_radius))
return; return;
last_x = x; last_x = x;
last_y = y; last_y = y;
oryg = oryg =
SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 80, canvas->format->BitsPerPixel, SDL_CreateRGBSurface(SDL_SWSURFACE, fisheye_radius, fisheye_radius, canvas->format->BitsPerPixel,
canvas->format->Rmask, canvas->format->Gmask, canvas->format->Rmask, canvas->format->Gmask,
canvas->format->Bmask, canvas->format->Amask); canvas->format->Bmask, canvas->format->Amask);
output = output =
SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 80, canvas->format->BitsPerPixel, SDL_CreateRGBSurface(SDL_SWSURFACE, fisheye_radius, fisheye_radius, canvas->format->BitsPerPixel,
canvas->format->Rmask, canvas->format->Gmask, canvas->format->Rmask, canvas->format->Gmask,
canvas->format->Bmask, canvas->format->Amask); canvas->format->Bmask, canvas->format->Amask);
rect.x = x - 40; rect.x = x - (fisheye_radius / 2);
rect.y = y - 40; rect.y = y - (fisheye_radius / 2);
rect.w = rect.h = 80; rect.w = rect.h = fisheye_radius;
SDL_BlitSurface(canvas, &rect, oryg, NULL); //here we have a piece of source image. Now we've to scale it (keeping aspect ratio) SDL_BlitSurface(canvas, &rect, oryg, NULL); //here we have a piece of source image. Now we've to scale it (keeping aspect ratio)
//do vertical fisheye //do vertical fisheye
for (i = 0; i < 40; i++) for (i = 0; i < fisheye_radius / 2; i++)
{ {
temp_src = temp_src =
SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 80, canvas->format->BitsPerPixel, SDL_CreateRGBSurface(SDL_SWSURFACE, 1, fisheye_radius, canvas->format->BitsPerPixel,
canvas->format->Rmask, canvas->format->Gmask, canvas->format->Rmask, canvas->format->Gmask,
canvas->format->Bmask, canvas->format->Amask); canvas->format->Bmask, canvas->format->Amask);
@ -200,41 +203,41 @@ void fisheye_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas,
SDL_BlitSurface(oryg, &rect, temp_src, NULL); //this bar is copied to temp_src SDL_BlitSurface(oryg, &rect, temp_src, NULL); //this bar is copied to temp_src
temp_dest = temp_dest =
SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 80 + 2 * i, SDL_CreateRGBSurface(SDL_SWSURFACE, 1, fisheye_radius + 2 * i,
canvas->format->BitsPerPixel, canvas->format->BitsPerPixel,
canvas->format->Rmask, canvas->format->Gmask, canvas->format->Rmask, canvas->format->Gmask,
canvas->format->Bmask, canvas->format->Amask); canvas->format->Bmask, canvas->format->Amask);
temp_dest = api->scale(temp_src, 1, 80 + 2 * i, 0); //temp_dest stores scaled temp_src temp_dest = api->scale(temp_src, 1, fisheye_radius + 2 * i, 0); //temp_dest stores scaled temp_src
temp_rect.x = 0; temp_rect.x = 0;
temp_rect.y = i; temp_rect.y = i;
temp_rect.w = 1; temp_rect.w = 1;
temp_rect.h = 80; temp_rect.h = fisheye_radius;
SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); //let's copy it to output SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); //let's copy it to output
//right side then //right side then
rect.x = 79 - i; rect.x = (fisheye_radius - 1) - i;
SDL_BlitSurface(oryg, &rect, temp_src, NULL); //this bar is copied to temp_src //OK SDL_BlitSurface(oryg, &rect, temp_src, NULL); //this bar is copied to temp_src //OK
temp_dest = api->scale(temp_src, 1, 80 + 2 * i, 0); //temp_dest stores scaled temp_src temp_dest = api->scale(temp_src, 1, fisheye_radius + 2 * i, 0); //temp_dest stores scaled temp_src
SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); //let's copy it to output SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); //let's copy it to output
} }
//do horizontal fisheye //do horizontal fisheye
for (i = 0; i < 40; i++) for (i = 0; i < fisheye_radius / 2; i++)
{ {
temp_src = temp_src =
SDL_CreateRGBSurface(SDL_SWSURFACE, 80, 1, canvas->format->BitsPerPixel, SDL_CreateRGBSurface(SDL_SWSURFACE, fisheye_radius, 1, canvas->format->BitsPerPixel,
canvas->format->Rmask, canvas->format->Gmask, canvas->format->Rmask, canvas->format->Gmask,
canvas->format->Bmask, canvas->format->Amask); canvas->format->Bmask, canvas->format->Amask);
temp_dest = temp_dest =
SDL_CreateRGBSurface(SDL_SWSURFACE, 80 + 2 * i, 1, SDL_CreateRGBSurface(SDL_SWSURFACE, fisheye_radius + 2 * i, 1,
canvas->format->BitsPerPixel, canvas->format->BitsPerPixel,
canvas->format->Rmask, canvas->format->Gmask, canvas->format->Rmask, canvas->format->Gmask,
canvas->format->Bmask, canvas->format->Amask); canvas->format->Bmask, canvas->format->Amask);
@ -242,47 +245,50 @@ void fisheye_draw(void *ptr, int which ATTRIBUTE_UNUSED, SDL_Surface * canvas,
//upper side first //upper side first
rect.x = 0; rect.x = 0;
rect.y = i; rect.y = i;
rect.w = 80; rect.w = fisheye_radius;
rect.h = 1; rect.h = 1;
temp_rect.x = i; temp_rect.x = i;
temp_rect.y = 0; temp_rect.y = 0;
temp_rect.w = 80; temp_rect.w = fisheye_radius;
temp_rect.h = 1; temp_rect.h = 1;
SDL_BlitSurface(output, &rect, temp_src, NULL); SDL_BlitSurface(output, &rect, temp_src, NULL);
temp_dest = api->scale(temp_src, 80 + 2 * i, 1, 0); temp_dest = api->scale(temp_src, fisheye_radius + 2 * i, 1, 0);
SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); SDL_BlitSurface(temp_dest, &temp_rect, output, &rect);
//lower side then //lower side then
rect.y = 79 - i; rect.y = (fisheye_radius - 1) - i;
SDL_BlitSurface(output, &rect, temp_src, NULL); SDL_BlitSurface(output, &rect, temp_src, NULL);
temp_dest = api->scale(temp_src, 80 + 2 * i, 1, 0); temp_dest = api->scale(temp_src, fisheye_radius + 2 * i, 1, 0);
SDL_BlitSurface(temp_dest, &temp_rect, output, &rect); SDL_BlitSurface(temp_dest, &temp_rect, output, &rect);
} }
rect.x = x - 40; rect.x = x - (fisheye_radius / 2);
rect.y = y - 40; rect.y = y - (fisheye_radius / 2);
rect.w = rect.h = 80; rect.w = rect.h = fisheye_radius;
//let's blit an area surrounded by a circle //let's blit an area surrounded by a circle
for (yy = y - 40; yy < y + 40; yy++) for (yy = y - (fisheye_radius / 2); yy < y + (fisheye_radius / 2); yy++) {
for (xx = x - 40; xx < x + 40; xx++) for (xx = x - (fisheye_radius / 2); xx < x + (fisheye_radius / 2); xx++) {
if (api->in_circle(xx - x, yy - y, (fisheye_radius / 2))) {
if (api->in_circle(xx - x, yy - y, 40))
api->putpixel(canvas, xx, yy, api->putpixel(canvas, xx, yy,
api->getpixel(output, xx + 40 - x, yy + 40 - y)); api->getpixel(output, xx + (fisheye_radius / 2) - x, yy + (fisheye_radius / 2) - y));
}
}
}
SDL_FreeSurface(oryg); SDL_FreeSurface(oryg);
SDL_FreeSurface(output); SDL_FreeSurface(output);
SDL_FreeSurface(temp_dest); if (temp_dest != NULL)
SDL_FreeSurface(temp_src); SDL_FreeSurface(temp_dest);
if (temp_src != NULL)
SDL_FreeSurface(temp_src);
api->playsound(fisheye_snd, (x * 255) / canvas->w, 255); api->playsound(fisheye_snd, (x * 255) / canvas->w, 255);
} }
@ -293,18 +299,18 @@ void fisheye_drag(magic_api * api, int which, SDL_Surface * canvas,
{ {
api->line(api, which, canvas, snapshot, ox, oy, x, y, 1, fisheye_draw); api->line(api, which, canvas, snapshot, ox, oy, x, y, 1, fisheye_draw);
update_rect->x = min(ox, x) - 40; update_rect->x = min(ox, x) - (fisheye_radius / 2);
update_rect->y = min(oy, y) - 40; update_rect->y = min(oy, y) - (fisheye_radius / 2);
update_rect->w = max(ox, x) - update_rect->x + 40; update_rect->w = max(ox, x) - update_rect->x + (fisheye_radius / 2);
update_rect->h = max(oy, y) - update_rect->y + 40; update_rect->h = max(oy, y) - update_rect->y + (fisheye_radius / 2);
} }
void fisheye_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED, void fisheye_click(magic_api * api, int which, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Surface * canvas, SDL_Surface * last, int x, int y,
SDL_Rect * update_rect) SDL_Rect * update_rect)
{ {
last_x = -80; /* A value that will be beyond any clicked position */ last_x = -fisheye_radius; /* A value that will be beyond any clicked position */
last_y = -80; last_y = -fisheye_radius;
fisheye_drag(api, which, canvas, last, x, y, x, y, update_rect); fisheye_drag(api, which, canvas, last, x, y, x, y, update_rect);
} }
@ -327,3 +333,19 @@ int fisheye_modes(magic_api * api ATTRIBUTE_UNUSED,
{ {
return (MODE_PAINT); return (MODE_PAINT);
} }
Uint8 fisheye_accepted_sizes(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
{
return 5; // FIXME
}
Uint8 fisheye_default_size(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
{
return 2; // FIXME
}
void fisheye_set_size(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED, Uint8 size, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
{
fisheye_radius = size * 40;
}