From ae873551ff1f191c504ab8c4107bf9329bb5e309 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Sat, 16 Jan 2010 20:00:10 +0000 Subject: [PATCH] -Fixed and extended the brush factory API. The factory now does a backup before running a script. -Updated the convolution sample factory to do a proper convolution, reading from the backup. There is still no colormapping. git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1262 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- factory.c | 27 ++++++++++++++++++++++++++- scripts/aafilter.lua | 23 +++++++++++++---------- windows.c | 8 +++++--- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/factory.c b/factory.c index 173dbc62..35d69cb8 100644 --- a/factory.c +++ b/factory.c @@ -31,8 +31,9 @@ #include "filesel.h" // Get_item_by_index #include "global.h" #include "graph.h" -#include "io.h" // find_last_slash +#include "io.h" // find_last_slash #include "misc.h" +#include "pages.h" // Backup() #include "readline.h" #include "sdlscreen.h" #include "windows.h" @@ -102,6 +103,14 @@ int L_GetPicturePixel(lua_State* L) return 1; } +int L_GetBackupPixel(lua_State* L) +{ + uint8_t c = Read_pixel_from_backup_screen(lua_tonumber(L, 1), + lua_tonumber(L, 2)); + lua_pushinteger(L, c); + return 1; +} + int L_SetColor(lua_State* L) { Set_color(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tonumber(L, 3), @@ -120,6 +129,13 @@ int L_GetColor(lua_State* L) return 3; } +int L_MatchColor(lua_State* L) +{ + int c = Best_color_nonexcluded(lua_tonumber(L,1), lua_tonumber(L, 2), lua_tonumber(L, 3)); + lua_pushinteger(L, c); + return 1; +} + int L_BrushEnable(__attribute__((unused)) lua_State* L) { Change_paintbrush_shape(PAINTBRUSH_SHAPE_COLOR_BRUSH); @@ -204,12 +220,14 @@ void Button_Brush_Factory(void) lua_register(L,"getbrushpixel",L_GetBrushPixel); lua_register(L,"putpicturepixel",L_PutPicturePixel); lua_register(L,"getpicturepixel",L_GetPicturePixel); + lua_register(L,"getbackuppixel",L_GetBackupPixel); lua_register(L,"setbrushsize",L_SetBrushSize); lua_register(L,"setpicturesize",L_SetPictureSize); lua_register(L,"getbrushsize",L_GetBrushSize); lua_register(L,"getpicturesize",L_GetPictureSize); lua_register(L,"setcolor",L_SetColor); lua_register(L,"getcolor",L_GetColor); + lua_register(L,"matchcolor",L_MatchColor); lua_register(L,"brushenable",L_BrushEnable); // For debug only @@ -220,6 +238,13 @@ void Button_Brush_Factory(void) strcat(scriptdir, Get_item_by_index(&Scripts_list, scriptlist->List_start + scriptlist->Cursor_position) -> Full_name); + + // TODO The script may modify the picture, so we do a backup here. + // If the script is only touching the brush, this isn't needed... + // The backup also allows the script to read from it to make something + // like a feedback off effect (convolution matrix comes to mind). + Backup(); + if (luaL_loadfile(L,scriptdir) != 0) { message = lua_tostring(L, 1); diff --git a/scripts/aafilter.lua b/scripts/aafilter.lua index 9a199f93..0a076320 100644 --- a/scripts/aafilter.lua +++ b/scripts/aafilter.lua @@ -2,7 +2,10 @@ w, h = getpicturesize(); -- Here is the filtering matrix - matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; + matrix = { + { 0, -1, 0 }, + { -1, 5, -1 }, + { 0, -1, 0 }}; -- Loop trough all the pixels -- To make this script simpler we don't handle the picture borders @@ -11,15 +14,15 @@ w, h = getpicturesize(); for y = 1, h - 2, 1 do for x = 1, w - 2, 1 do filtered = - matrix[1][1] * getpicturepixel(x - 1, y - 1) + - matrix[1][2] * getpicturepixel(x , y - 1) + - matrix[1][3] * getpicturepixel(x + 1, y - 1) + - matrix[2][1] * getpicturepixel(x - 1, y ) + - matrix[2][2] * getpicturepixel(x , y ) + - matrix[2][3] * getpicturepixel(x + 1, y ) + - matrix[3][1] * getpicturepixel(x - 1, y + 1) + - matrix[3][2] * getpicturepixel(x , y + 1) + - matrix[3][3] * getpicturepixel(x + 1, y + 1); + matrix[1][1] * getbackuppixel(x - 1, y - 1) + + matrix[1][2] * getbackuppixel(x , y - 1) + + matrix[1][3] * getbackuppixel(x + 1, y - 1) + + matrix[2][1] * getbackuppixel(x - 1, y ) + + matrix[2][2] * getbackuppixel(x , y ) + + matrix[2][3] * getbackuppixel(x + 1, y ) + + matrix[3][1] * getbackuppixel(x - 1, y + 1) + + matrix[3][2] * getbackuppixel(x , y + 1) + + matrix[3][3] * getbackuppixel(x + 1, y + 1); putpicturepixel(x,y,filtered); end end diff --git a/windows.c b/windows.c index 850e91ce..d2966c3e 100644 --- a/windows.c +++ b/windows.c @@ -2609,11 +2609,13 @@ byte Best_color_nonexcluded(byte red,byte green,byte blue) delta_r=(int)Main_palette[col].R-red; delta_g=(int)Main_palette[col].G-green; delta_b=(int)Main_palette[col].B-blue; + + if(delta_r == delta_g == delta_b) return col; + rmean = ( Main_palette[col].R + red ) / 2; - if (!(dist= ( ( (512+rmean) *delta_r*delta_r) >>8) + 4*delta_g*delta_g + (((767-rmean)*delta_b*delta_b)>>8))) - //if (!(dist=(delta_r*delta_r*30)+(delta_g*delta_g*59)+(delta_b*delta_b*11))) - return col; + dist= ( ( (512+rmean) *delta_r*delta_r) >>8) + 4*delta_g*delta_g + (((767-rmean)*delta_b*delta_b)>>8); + //dist=(delta_r*delta_r*30)+(delta_g*delta_g*59)+(delta_b*delta_b*11) if (dist