indent sharpen.c

This commit is contained in:
Bill Kendrick 2017-10-15 11:49:48 -07:00
parent ac74e0ed51
commit fee4c7ca44

View file

@ -44,7 +44,8 @@
/* Our globals: */ /* Our globals: */
enum { enum
{
TOOL_TRACE, TOOL_TRACE,
TOOL_SHARPEN, TOOL_SHARPEN,
TOOL_SILHOUETTE, TOOL_SILHOUETTE,
@ -64,16 +65,19 @@ const char * sharpen_snd_filenames[sharpen_NUM_TOOLS] = {
"sharpen.ogg", "sharpen.ogg",
"silhouette.ogg" "silhouette.ogg"
}; };
const char *sharpen_icon_filenames[sharpen_NUM_TOOLS] = { const char *sharpen_icon_filenames[sharpen_NUM_TOOLS] = {
"edges.png", "edges.png",
"sharpen.png", "sharpen.png",
"silhouette.png" "silhouette.png"
}; };
const char *sharpen_names[sharpen_NUM_TOOLS] = { const char *sharpen_names[sharpen_NUM_TOOLS] = {
gettext_noop("Edges"), gettext_noop("Edges"),
gettext_noop("Sharpen"), gettext_noop("Sharpen"),
gettext_noop("Silhouette") gettext_noop("Silhouette")
}; };
const char *sharpen_descs[sharpen_NUM_TOOLS][2] = { const char *sharpen_descs[sharpen_NUM_TOOLS][2] = {
{gettext_noop("Click and drag the mouse to trace edges in parts of your picture."), {gettext_noop("Click and drag the mouse to trace edges in parts of your picture."),
gettext_noop("Click to trace edges in your entire picture."),}, gettext_noop("Click to trace edges in your entire picture."),},
@ -90,21 +94,16 @@ SDL_Surface * sharpen_get_icon(magic_api * api, int which);
char *sharpen_get_name(magic_api * api, int which); char *sharpen_get_name(magic_api * api, int which);
char *sharpen_get_description(magic_api * api, int which, int mode); char *sharpen_get_description(magic_api * api, int which, int mode);
static int sharpen_grey(Uint8 r1, Uint8 g1, Uint8 b1); static int sharpen_grey(Uint8 r1, Uint8 g1, Uint8 b1);
static void do_sharpen_pixel(void * ptr, int which, static void do_sharpen_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y);
SDL_Surface * canvas, SDL_Surface * last,
int x, int y);
static void do_sharpen_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which); static void do_sharpen_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which);
static void do_sharpen_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y); static void do_sharpen_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y);
void sharpen_drag(magic_api * api, int which, SDL_Surface * canvas, void sharpen_drag(magic_api * api, int which, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect);
SDL_Rect * update_rect);
void sharpen_click(magic_api * api, int which, int mode, void sharpen_click(magic_api * api, int which, int mode,
SDL_Surface * canvas, SDL_Surface * last, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
int x, int y, SDL_Rect * update_rect);
void sharpen_release(magic_api * api, int which, void sharpen_release(magic_api * api, int which,
SDL_Surface * canvas, SDL_Surface * last, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect);
int x, int y, SDL_Rect * update_rect);
void sharpen_shutdown(magic_api * api); void sharpen_shutdown(magic_api * api);
void sharpen_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b); void sharpen_set_color(magic_api * api, Uint8 r, Uint8 g, Uint8 b);
@ -113,16 +112,21 @@ void sharpen_switchin(magic_api * api, int which, int mode, SDL_Surface * canvas
void sharpen_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas); void sharpen_switchout(magic_api * api, int which, int mode, SDL_Surface * canvas);
int sharpen_modes(magic_api * api, int which); int sharpen_modes(magic_api * api, int which);
Uint32 sharpen_api_version(void) { return(TP_MAGIC_API_VERSION); } Uint32 sharpen_api_version(void)
{
return (TP_MAGIC_API_VERSION);
}
// No setup required: // No setup required:
int sharpen_init(magic_api * api){ int sharpen_init(magic_api * api)
{
int i; int i;
char fname[1024]; char fname[1024];
for (i = 0; i < sharpen_NUM_TOOLS; i++){ for (i = 0; i < sharpen_NUM_TOOLS; i++)
{
snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, sharpen_snd_filenames[i]); snprintf(fname, sizeof(fname), "%s/sounds/magic/%s", api->data_directory, sharpen_snd_filenames[i]);
sharpen_snd_effect[i] = Mix_LoadWAV(fname); sharpen_snd_effect[i] = Mix_LoadWAV(fname);
} }
@ -137,31 +141,35 @@ int sharpen_get_tool_count(magic_api * api ATTRIBUTE_UNUSED)
} }
// Load our icons: // Load our icons:
SDL_Surface * sharpen_get_icon(magic_api * api, int which){ SDL_Surface *sharpen_get_icon(magic_api * api, int which)
{
char fname[1024]; char fname[1024];
snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, sharpen_icon_filenames[which]); snprintf(fname, sizeof(fname), "%simages/magic/%s", api->data_directory, sharpen_icon_filenames[which]);
return (IMG_Load(fname)); return (IMG_Load(fname));
} }
// Return our names, localized: // Return our names, localized:
char * sharpen_get_name(magic_api * api ATTRIBUTE_UNUSED, int which){ char *sharpen_get_name(magic_api * api ATTRIBUTE_UNUSED, int which)
{
return (strdup(gettext_noop(sharpen_names[which]))); return (strdup(gettext_noop(sharpen_names[which])));
} }
// Return our descriptions, localized: // Return our descriptions, localized:
char * sharpen_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode){ char *sharpen_get_description(magic_api * api ATTRIBUTE_UNUSED, int which, int mode)
{
return (strdup(gettext_noop(sharpen_descs[which][mode - 1]))); return (strdup(gettext_noop(sharpen_descs[which][mode - 1])));
} }
//Calculates the grey scale value for a rgb pixel //Calculates the grey scale value for a rgb pixel
static int sharpen_grey(Uint8 r1,Uint8 g1,Uint8 b1){ static int sharpen_grey(Uint8 r1, Uint8 g1, Uint8 b1)
{
return 0.3 * r1 + .59 * g1 + 0.11 * b1; return 0.3 * r1 + .59 * g1 + 0.11 * b1;
} }
// Do the effect: // Do the effect:
static void do_sharpen_pixel(void * ptr, int which, static void do_sharpen_pixel(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y)
SDL_Surface * canvas, SDL_Surface * last, {
int x, int y){
magic_api *api = (magic_api *) ptr; magic_api *api = (magic_api *) ptr;
@ -174,15 +182,19 @@ static void do_sharpen_pixel(void * ptr, int which,
//Sobel weighting masks //Sobel weighting masks
const int sobel_weights_1[3][3] = { {1, 2, 1}, const int sobel_weights_1[3][3] = { {1, 2, 1},
{0, 0, 0}, {0, 0, 0},
{-1,-2,-1}}; {-1, -2, -1}
};
const int sobel_weights_2[3][3] = { {-1, 0, 1}, const int sobel_weights_2[3][3] = { {-1, 0, 1},
{-2, 0, 2}, {-2, 0, 2},
{-1,0,1}}; {-1, 0, 1}
};
sobel_1 = 0; sobel_1 = 0;
sobel_2 = 0; sobel_2 = 0;
for (i=-1;i<2;i++){ for (i = -1; i < 2; i++)
for(j=-1; j<2; j++){ {
for (j = -1; j < 2; j++)
{
//No need to check if inside canvas, getpixel does it for us. //No need to check if inside canvas, getpixel does it for us.
SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &r1, &g1, &b1); SDL_GetRGB(api->getpixel(last, x + i, y + j), last->format, &r1, &g1, &b1);
grey = sharpen_grey(r1, g1, b1); grey = sharpen_grey(r1, g1, b1);
@ -195,17 +207,21 @@ static void do_sharpen_pixel(void * ptr, int which,
temp = (temp / 1443) * 255.0; temp = (temp / 1443) * 255.0;
// set image to white where edge value is below THRESHOLD // set image to white where edge value is below THRESHOLD
if (which == TOOL_TRACE){ if (which == TOOL_TRACE)
if (temp<THRESHOLD){ {
if (temp < THRESHOLD)
{
api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 255, 255, 255)); api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, 255, 255, 255));
} }
} }
//Simply display the edge values - provides a nice black and white silhouette image //Simply display the edge values - provides a nice black and white silhouette image
else if (which == TOOL_SILHOUETTE){ else if (which == TOOL_SILHOUETTE)
{
api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, temp, temp, temp)); api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, temp, temp, temp));
} }
//Add the edge values to the original image, creating a more distinct jump in contrast at edges //Add the edge values to the original image, creating a more distinct jump in contrast at edges
else if(which == TOOL_SHARPEN){ else if (which == TOOL_SHARPEN)
{
SDL_GetRGB(api->getpixel(last, x, y), last->format, &r1, &g1, &b1); SDL_GetRGB(api->getpixel(last, x, y), last->format, &r1, &g1, &b1);
api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, clamp(0.0, r1 + SHARPEN * temp, 255.0), api->putpixel(canvas, x, y, SDL_MapRGB(canvas->format, clamp(0.0, r1 + SHARPEN * temp, 255.0),
clamp(0.0, g1 + SHARPEN * temp, 255.0), clamp(0.0, g1 + SHARPEN * temp, 255.0),
@ -214,21 +230,25 @@ static void do_sharpen_pixel(void * ptr, int which,
} }
// Do the effect for the full image // Do the effect for the full image
static void do_sharpen_full(void * ptr,SDL_Surface * canvas, SDL_Surface * last, int which){ static void do_sharpen_full(void *ptr, SDL_Surface * canvas, SDL_Surface * last, int which)
{
// magic_api * api = (magic_api *) ptr; // magic_api * api = (magic_api *) ptr;
int x, y; int x, y;
for (y = 0; y < last->h; y++){ for (y = 0; y < last->h; y++)
for (x=0; x < last->w; x++){ {
for (x = 0; x < last->w; x++)
{
do_sharpen_pixel(ptr, which, canvas, last, x, y); do_sharpen_pixel(ptr, which, canvas, last, x, y);
} }
} }
} }
//do the effect for the brush //do the effect for the brush
static void do_sharpen_brush(void * ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y){ static void do_sharpen_brush(void *ptr, int which, SDL_Surface * canvas, SDL_Surface * last, int x, int y)
{
int xx, yy; int xx, yy;
magic_api *api = (magic_api *) ptr; magic_api *api = (magic_api *) ptr;
@ -236,8 +256,7 @@ static void do_sharpen_brush(void * ptr, int which, SDL_Surface * canvas, SDL_Su
{ {
for (xx = x - sharpen_RADIUS; xx < x + sharpen_RADIUS; xx++) for (xx = x - sharpen_RADIUS; xx < x + sharpen_RADIUS; xx++)
{ {
if (api->in_circle(xx - x, yy - y, sharpen_RADIUS) && if (api->in_circle(xx - x, yy - y, sharpen_RADIUS) && !api->touched(xx, yy))
!api->touched(xx, yy))
{ {
do_sharpen_pixel(api, which, canvas, last, xx, yy); do_sharpen_pixel(api, which, canvas, last, xx, yy);
} }
@ -247,15 +266,27 @@ static void do_sharpen_brush(void * ptr, int which, SDL_Surface * canvas, SDL_Su
// Affect the canvas on drag: // Affect the canvas on drag:
void sharpen_drag(magic_api * api, int which, SDL_Surface * canvas, void sharpen_drag(magic_api * api, int which, SDL_Surface * canvas,
SDL_Surface * last, int ox, int oy, int x, int y, SDL_Surface * last, int ox, int oy, int x, int y, SDL_Rect * update_rect)
SDL_Rect * update_rect){ {
api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_sharpen_brush); api->line((void *)api, which, canvas, last, ox, oy, x, y, 1, do_sharpen_brush);
api->playsound(sharpen_snd_effect[which], (x * 255) / canvas->w, 255); api->playsound(sharpen_snd_effect[which], (x * 255) / canvas->w, 255);
if (ox > x) { int tmp = ox; ox = x; x = tmp; } if (ox > x)
if (oy > y) { int tmp = oy; oy = y; y = tmp; } {
int tmp = ox;
ox = x;
x = tmp;
}
if (oy > y)
{
int tmp = oy;
oy = y;
y = tmp;
}
update_rect->x = ox - sharpen_RADIUS; update_rect->x = ox - sharpen_RADIUS;
update_rect->y = oy - sharpen_RADIUS; update_rect->y = oy - sharpen_RADIUS;
@ -265,11 +296,12 @@ void sharpen_drag(magic_api * api, int which, SDL_Surface * canvas,
// Affect the canvas on click: // Affect the canvas on click:
void sharpen_click(magic_api * api, int which, int mode, void sharpen_click(magic_api * api, int which, int mode,
SDL_Surface * canvas, SDL_Surface * last, SDL_Surface * canvas, SDL_Surface * last, int x, int y, SDL_Rect * update_rect)
int x, int y, SDL_Rect * update_rect){ {
if (mode == MODE_PAINT) if (mode == MODE_PAINT)
sharpen_drag(api, which, canvas, last, x, y, x, y, update_rect); sharpen_drag(api, which, canvas, last, x, y, x, y, update_rect);
else{ else
{
update_rect->x = 0; update_rect->x = 0;
update_rect->y = 0; update_rect->y = 0;
update_rect->w = canvas->w; update_rect->w = canvas->w;
@ -291,15 +323,19 @@ void sharpen_shutdown(magic_api * api ATTRIBUTE_UNUSED)
{ {
//Clean up sounds //Clean up sounds
int i; int i;
for(i=0; i<sharpen_NUM_TOOLS; i++){
if(sharpen_snd_effect[i] != NULL){ for (i = 0; i < sharpen_NUM_TOOLS; i++)
{
if (sharpen_snd_effect[i] != NULL)
{
Mix_FreeChunk(sharpen_snd_effect[i]); Mix_FreeChunk(sharpen_snd_effect[i]);
} }
} }
} }
// Record the color from Tux Paint: // Record the color from Tux Paint:
void sharpen_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED, Uint8 b ATTRIBUTE_UNUSED) void sharpen_set_color(magic_api * api ATTRIBUTE_UNUSED, Uint8 r ATTRIBUTE_UNUSED, Uint8 g ATTRIBUTE_UNUSED,
Uint8 b ATTRIBUTE_UNUSED)
{ {
} }
@ -309,11 +345,13 @@ int sharpen_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUT
return 0; return 0;
} }
void sharpen_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) void sharpen_switchin(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{ {
} }
void sharpen_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED) void sharpen_switchout(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{ {
} }