gimp/plug-ins/lighting/lighting-main.c

335 lines
12 KiB
C
Raw Normal View History

/* Lighting Effects 0.2.2 -- image filter plug-in for GIMP
*
* Copyright (C) 1996-98 Tom Bech
* Copyright (C) 1996-98 Federico Mena Quintero
*
* E-mail: tomb@gimp.org (Tom) or quartic@gimp.org (Federico)
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gtk/gtk.h>
#include <libgimp/gimp.h>
#include "lighting-apply.h"
#include "lighting-image.h"
#include "lighting-main.h"
#include "lighting-preview.h"
#include "lighting-shade.h"
#include "lighting-ui.h"
1998-06-11 14:04:59 +08:00
#include "libgimp/stdplugins-intl.h"
1998-06-11 14:04:59 +08:00
1998-06-11 14:04:59 +08:00
LightingValues mapvals;
/******************/
/* Implementation */
/******************/
/*************************************/
/* Set parameters to standard values */
/*************************************/
static void
set_default_settings (void)
1998-06-11 14:04:59 +08:00
{
gint k;
mapvals.update_enabled = TRUE;
mapvals.light_selected = 0;
mapvals.light_isolated = FALSE;
gimp_vector3_set (&mapvals.viewpoint, 0.5, 0.5, 0.25);
gimp_vector3_set (&mapvals.planenormal, 0.0, 0.0, 1.0);
renamed gimp_drawable_image() to gimp_drawable_get_image() for symmetry 2003-12-04 Michael Natterer <mitch@gimp.org> * tools/pdbgen/pdb/drawable.pdb: renamed gimp_drawable_image() to gimp_drawable_get_image() for symmetry with gimp_drawable_set_image(). * libgimp/gimpchannel.h: removed gimp_channel_get_image_id #define. * libgimp/gimpdrawable.h: removed gimp_drawable_image_id #define. * libgimp/gimplayer.h:: removed gimp_layer_get_image_id #define. * libgimp/gimpcompat.h: added the old stuff here. * app/pdb/drawable_cmds.c * libgimp/gimpdrawable_pdb.[ch]: regenerated. * libgimp/gimpmiscui.c * plug-ins/Lighting/lighting_main.c * plug-ins/MapObject/mapobject_main.c * plug-ins/common/curve_bend.c * plug-ins/common/film.c * plug-ins/common/newsprint.c * plug-ins/common/pixelize.c * plug-ins/common/ps.c * plug-ins/common/sample_colorize.c * plug-ins/common/smooth_palette.c * plug-ins/common/warp.c * plug-ins/imagemap/imap_cmd_gimp_guides.c * plug-ins/imagemap/imap_main.c * plug-ins/imagemap/imap_preview.c * plug-ins/maze/maze.c * plug-ins/pygimp/pygimp-drawable.c * plug-ins/rcm/rcm_misc.c * plug-ins/script-fu/scripts/addborder.scm * plug-ins/script-fu/scripts/carve-it.scm * plug-ins/script-fu/scripts/weave.scm: changed accordingly. * plug-ins/maze/maze.c: completely reindented. * plug-ins/script-fu/siod/trace.c: removed trailing whitespace. 2003-12-04 Michael Natterer <mitch@gimp.org> * libgimp/libgimp-sections.txt * libgimp/tmpl/gimpchannel.sgml * libgimp/tmpl/gimpdrawable.sgml * libgimp/tmpl/gimplayer.sgml: updated after gimp_drawable_get_image() cleanup.
2003-12-04 21:21:27 +08:00
gimp_vector3_set (&mapvals.lightsource[0].position, -1.0, -1.0, 1.0);
gimp_vector3_set (&mapvals.lightsource[0].direction, -1.0, -1.0, 1.0);
gimp_rgba_set (&mapvals.lightsource[0].color, 1.0, 1.0, 1.0, 1.0);
mapvals.lightsource[0].intensity = 1.0;
mapvals.lightsource[0].type = POINT_LIGHT;
mapvals.lightsource[0].active = TRUE;
/* init lights 2 and 3 pos to upper left and below */
gimp_vector3_set (&mapvals.lightsource[1].position, 2.0, -1.0, 1.0);
gimp_vector3_set (&mapvals.lightsource[1].direction, 1.0, -1.0, 1.0);
1998-06-11 14:04:59 +08:00
gimp_vector3_set (&mapvals.lightsource[2].position, 1.0, 2.0, 1.0);
gimp_vector3_set (&mapvals.lightsource[2].direction, 0.0, 1.0, 1.0);
/* init any remaining lights to directly overhead */
for (k = 3; k < NUM_LIGHTS; k++)
{
gimp_vector3_set (&mapvals.lightsource[k].position, 0.0, 0.0, 1.0);
gimp_vector3_set (&mapvals.lightsource[k].direction, 0.0, 0.0, 1.0);
}
for (k = 1; k < NUM_LIGHTS; k++)
{
gimp_rgba_set (&mapvals.lightsource[k].color, 1.0, 1.0, 1.0, 1.0);
mapvals.lightsource[k].intensity = 1.0;
mapvals.lightsource[k].type = NO_LIGHT;
mapvals.lightsource[k].active = TRUE;
}
1998-06-11 14:04:59 +08:00
mapvals.material.ambient_int = 0.2;
mapvals.material.diffuse_int = 0.5;
mapvals.material.diffuse_ref = 0.4;
mapvals.material.specular_ref = 0.5;
mapvals.material.highlight = 27.0;
mapvals.material.metallic = FALSE;
1998-06-11 14:04:59 +08:00
mapvals.pixel_treshold = 0.25;
mapvals.max_depth = 3.0;
mapvals.preview_zoom_factor = 1.0;
mapvals.bumpmaptype = 0;
mapvals.bumpmin = 0.0;
mapvals.bumpmax = 0.1;
1998-06-11 14:04:59 +08:00
mapvals.antialiasing = FALSE;
mapvals.create_new_image = FALSE;
1998-06-11 14:04:59 +08:00
mapvals.transparent_background = FALSE;
mapvals.bump_mapped = FALSE;
mapvals.env_mapped = FALSE;
mapvals.ref_mapped = FALSE;
mapvals.previewquality = FALSE;
mapvals.interactive_preview = TRUE;
mapvals.bumpmap_id = -1;
mapvals.envmap_id = -1;
1998-06-11 14:04:59 +08:00
}
static void
check_drawables (void)
1998-06-11 14:04:59 +08:00
{
if (mapvals.bump_mapped)
{
if (mapvals.bumpmap_id != -1 &&
gimp_item_get_image (mapvals.bumpmap_id) == -1)
{
mapvals.bump_mapped = FALSE;
mapvals.bumpmap_id = -1;
}
if (gimp_drawable_is_indexed (mapvals.bumpmap_id) ||
(gimp_drawable_width (mapvals.drawable_id) !=
gimp_drawable_width (mapvals.bumpmap_id)) ||
(gimp_drawable_height (mapvals.drawable_id) !=
gimp_drawable_height (mapvals.bumpmap_id)))
1998-06-11 14:04:59 +08:00
{
mapvals.bump_mapped = FALSE;
mapvals.bumpmap_id = -1;
1998-06-11 14:04:59 +08:00
}
}
if (mapvals.env_mapped)
{
if (mapvals.envmap_id != -1 &&
gimp_item_get_image (mapvals.envmap_id) == -1)
{
mapvals.env_mapped = FALSE;
mapvals.envmap_id = -1;
}
renamed gimp_drawable_image() to gimp_drawable_get_image() for symmetry 2003-12-04 Michael Natterer <mitch@gimp.org> * tools/pdbgen/pdb/drawable.pdb: renamed gimp_drawable_image() to gimp_drawable_get_image() for symmetry with gimp_drawable_set_image(). * libgimp/gimpchannel.h: removed gimp_channel_get_image_id #define. * libgimp/gimpdrawable.h: removed gimp_drawable_image_id #define. * libgimp/gimplayer.h:: removed gimp_layer_get_image_id #define. * libgimp/gimpcompat.h: added the old stuff here. * app/pdb/drawable_cmds.c * libgimp/gimpdrawable_pdb.[ch]: regenerated. * libgimp/gimpmiscui.c * plug-ins/Lighting/lighting_main.c * plug-ins/MapObject/mapobject_main.c * plug-ins/common/curve_bend.c * plug-ins/common/film.c * plug-ins/common/newsprint.c * plug-ins/common/pixelize.c * plug-ins/common/ps.c * plug-ins/common/sample_colorize.c * plug-ins/common/smooth_palette.c * plug-ins/common/warp.c * plug-ins/imagemap/imap_cmd_gimp_guides.c * plug-ins/imagemap/imap_main.c * plug-ins/imagemap/imap_preview.c * plug-ins/maze/maze.c * plug-ins/pygimp/pygimp-drawable.c * plug-ins/rcm/rcm_misc.c * plug-ins/script-fu/scripts/addborder.scm * plug-ins/script-fu/scripts/carve-it.scm * plug-ins/script-fu/scripts/weave.scm: changed accordingly. * plug-ins/maze/maze.c: completely reindented. * plug-ins/script-fu/siod/trace.c: removed trailing whitespace. 2003-12-04 Michael Natterer <mitch@gimp.org> * libgimp/libgimp-sections.txt * libgimp/tmpl/gimpchannel.sgml * libgimp/tmpl/gimpdrawable.sgml * libgimp/tmpl/gimplayer.sgml: updated after gimp_drawable_get_image() cleanup.
2003-12-04 21:21:27 +08:00
if (gimp_drawable_is_gray (mapvals.envmap_id) ||
gimp_drawable_has_alpha (mapvals.envmap_id))
1998-06-11 14:04:59 +08:00
{
mapvals.env_mapped = FALSE;
mapvals.envmap_id = -1;
1998-06-11 14:04:59 +08:00
}
}
}
static void
query (void)
1998-06-11 14:04:59 +08:00
{
static const GimpParamDef args[] =
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
{ GIMP_PDB_DRAWABLE, "bumpdrawable", "Bumpmap drawable (set to 0 if disabled)" },
{ GIMP_PDB_DRAWABLE, "envdrawable", "Environmentmap drawable (set to 0 if disabled)" },
{ GIMP_PDB_INT32, "dobumpmap", "Enable bumpmapping (TRUE/FALSE)" },
{ GIMP_PDB_INT32, "doenvmap", "Enable envmapping (TRUE/FALSE)" },
{ GIMP_PDB_INT32, "bumpmaptype", "Type of mapping (0=linear,1=log, 2=sinusoidal, 3=spherical)" },
{ GIMP_PDB_INT32, "lighttype", "Type of lightsource (0=point,1=directional,3=spot,4=none)" },
{ GIMP_PDB_COLOR, "lightcolor", "Lightsource color (r,g,b)" },
{ GIMP_PDB_FLOAT, "lightposition-x", "Lightsource position (x,y,z)" },
{ GIMP_PDB_FLOAT, "lightposition-y", "Lightsource position (x,y,z)" },
{ GIMP_PDB_FLOAT, "lightposition-z", "Lightsource position (x,y,z)" },
{ GIMP_PDB_FLOAT, "lightdirection-x", "Lightsource direction [x,y,z]" },
{ GIMP_PDB_FLOAT, "lightdirection-y", "Lightsource direction [x,y,z]" },
{ GIMP_PDB_FLOAT, "lightdirection-z", "Lightsource direction [x,y,z]" },
{ GIMP_PDB_FLOAT, "ambient-intensity", "Material ambient intensity (0..1)" },
{ GIMP_PDB_FLOAT, "diffuse-intensity", "Material diffuse intensity (0..1)" },
{ GIMP_PDB_FLOAT, "diffuse-reflectivity", "Material diffuse reflectivity (0..1)" },
{ GIMP_PDB_FLOAT, "specular-reflectivity", "Material specular reflectivity (0..1)" },
{ GIMP_PDB_FLOAT, "highlight", "Material highlight (0..->), note: it's expotential" },
{ GIMP_PDB_INT32, "antialiasing", "Apply antialiasing (TRUE/FALSE)" },
{ GIMP_PDB_INT32, "newimage", "Create a new image (TRUE/FALSE)" },
{ GIMP_PDB_INT32, "transparentbackground", "Make background transparent (TRUE/FALSE)" }
};
1998-06-11 14:04:59 +08:00
gimp_install_procedure (PLUG_IN_PROC,
N_("Apply various lighting effects to an image"),
"No help yet",
"Tom Bech & Federico Mena Quintero",
"Tom Bech & Federico Mena Quintero",
"Version 0.2.0, March 15 1998",
N_("_Lighting Effects..."),
"RGB*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
gimp_plugin_menu_register (PLUG_IN_PROC,
"<Image>/Filters/Light and Shadow/Light");
1998-06-11 14:04:59 +08:00
}
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
1998-06-11 14:04:59 +08:00
{
static GimpParam values[1];
GimpDrawable *drawable;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
1998-06-11 14:04:59 +08:00
run_mode = param[0].data.d_int32;
INIT_I18N ();
1998-06-11 14:04:59 +08:00
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
1998-06-11 14:04:59 +08:00
values[0].data.d_status = status;
/* Set default values */
/* ================== */
set_default_settings ();
1998-06-11 14:04:59 +08:00
/* Possibly retrieve data */
/* ====================== */
gimp_get_data (PLUG_IN_PROC, &mapvals);
1998-06-11 14:04:59 +08:00
/* Get the specified drawable */
/* ========================== */
1998-06-11 14:04:59 +08:00
drawable = gimp_drawable_get (param[2].data.d_drawable);
mapvals.drawable_id = drawable->drawable_id;
1998-06-11 14:04:59 +08:00
check_drawables ();
1998-06-11 14:04:59 +08:00
if (status == GIMP_PDB_SUCCESS)
1998-06-11 14:04:59 +08:00
{
/* Make sure that the drawable is RGBA or RGB color */
/* ================================================ */
if (gimp_drawable_is_rgb (drawable->drawable_id))
{
/* Set the tile cache size */
1998-06-11 14:04:59 +08:00
/* ======================= */
gimp_tile_cache_ntiles (TILE_CACHE_SIZE);
renamed gimp_drawable_image() to gimp_drawable_get_image() for symmetry 2003-12-04 Michael Natterer <mitch@gimp.org> * tools/pdbgen/pdb/drawable.pdb: renamed gimp_drawable_image() to gimp_drawable_get_image() for symmetry with gimp_drawable_set_image(). * libgimp/gimpchannel.h: removed gimp_channel_get_image_id #define. * libgimp/gimpdrawable.h: removed gimp_drawable_image_id #define. * libgimp/gimplayer.h:: removed gimp_layer_get_image_id #define. * libgimp/gimpcompat.h: added the old stuff here. * app/pdb/drawable_cmds.c * libgimp/gimpdrawable_pdb.[ch]: regenerated. * libgimp/gimpmiscui.c * plug-ins/Lighting/lighting_main.c * plug-ins/MapObject/mapobject_main.c * plug-ins/common/curve_bend.c * plug-ins/common/film.c * plug-ins/common/newsprint.c * plug-ins/common/pixelize.c * plug-ins/common/ps.c * plug-ins/common/sample_colorize.c * plug-ins/common/smooth_palette.c * plug-ins/common/warp.c * plug-ins/imagemap/imap_cmd_gimp_guides.c * plug-ins/imagemap/imap_main.c * plug-ins/imagemap/imap_preview.c * plug-ins/maze/maze.c * plug-ins/pygimp/pygimp-drawable.c * plug-ins/rcm/rcm_misc.c * plug-ins/script-fu/scripts/addborder.scm * plug-ins/script-fu/scripts/carve-it.scm * plug-ins/script-fu/scripts/weave.scm: changed accordingly. * plug-ins/maze/maze.c: completely reindented. * plug-ins/script-fu/siod/trace.c: removed trailing whitespace. 2003-12-04 Michael Natterer <mitch@gimp.org> * libgimp/libgimp-sections.txt * libgimp/tmpl/gimpchannel.sgml * libgimp/tmpl/gimpdrawable.sgml * libgimp/tmpl/gimplayer.sgml: updated after gimp_drawable_get_image() cleanup.
2003-12-04 21:21:27 +08:00
1998-06-11 14:04:59 +08:00
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
if (main_dialog (drawable))
{
compute_image ();
gimp_set_data (PLUG_IN_PROC,
&mapvals, sizeof (LightingValues));
gimp_displays_flush ();
}
1998-06-11 14:04:59 +08:00
break;
case GIMP_RUN_WITH_LAST_VALS:
if (image_setup (drawable, FALSE))
compute_image ();
gimp_displays_flush ();
1998-06-11 14:04:59 +08:00
break;
case GIMP_RUN_NONINTERACTIVE:
1998-06-11 14:04:59 +08:00
if (nparams != 24)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
1998-06-11 14:04:59 +08:00
{
mapvals.bumpmap_id = param[3].data.d_drawable;
mapvals.envmap_id = param[4].data.d_drawable;
mapvals.bump_mapped = (gint) param[5].data.d_int32;
mapvals.env_mapped = (gint) param[6].data.d_int32;
mapvals.bumpmaptype = (gint) param[7].data.d_int32;
mapvals.lightsource[0].type = (LightType) param[8].data.d_int32;
mapvals.lightsource[0].color = param[9].data.d_color;
mapvals.lightsource[0].position.x = param[10].data.d_float;
mapvals.lightsource[0].position.y = param[11].data.d_float;
mapvals.lightsource[0].position.z = param[12].data.d_float;
mapvals.lightsource[0].direction.x = param[13].data.d_float;
mapvals.lightsource[0].direction.y = param[14].data.d_float;
mapvals.lightsource[0].direction.z = param[15].data.d_float;
mapvals.material.ambient_int = param[16].data.d_float;
mapvals.material.diffuse_int = param[17].data.d_float;
mapvals.material.diffuse_ref = param[18].data.d_float;
mapvals.material.specular_ref = param[19].data.d_float;
mapvals.material.highlight = param[20].data.d_float;
mapvals.antialiasing = (gint) param[21].data.d_int32;
mapvals.create_new_image = (gint) param[22].data.d_int32;
mapvals.transparent_background = (gint) param[23].data.d_int32;
1998-06-11 14:04:59 +08:00
check_drawables ();
if (image_setup (drawable, FALSE))
compute_image ();
1998-06-11 14:04:59 +08:00
}
default:
break;
}
}
else
status = GIMP_PDB_EXECUTION_ERROR;
1998-06-11 14:04:59 +08:00
}
values[0].data.d_status = status;
gimp_drawable_detach (drawable);
applied gimp-lecorfec-99041[02]-0, changes follow * applied gimp-lecorfec-99041[02]-0, changes follow * plug-ins/FractalExplorer/Dialogs.h (make_color_map): replaced free with g_free to fix segfault. * plug-ins/Lighting/lighting_preview.c (compute_preview): allocate xpostab and ypostab only when needed (it could also be allocated on stack with a compilation-fixed size like MapObject). It avoids to lose some Kb on each preview :) Also reindented (unfortunate C-c C-q) some other lines. * plug-ins/Lighting/lighting_main.c (run): release allocated postabs. * plug-ins/Lighting/lighting_ui.c: callbacks now have only one argument because gck widget use gtk_signal_connect_object. Caused segfault for scale widget. * plug-ins/autocrop/autocrop.c (doit): return if image has only background (thus fixing a segfault). * plug-ins/emboss/emboss.c (pluginCore, emboss_do_preview): replaced malloc/free with g_malloc/g_free (unneeded, but shouldn't everyone use glib calls ? :) * plug-ins/flame/flame.c : replaced a segfaulting free, and several harmless malloc/free pairs. * plug-ins/flame/megawidget.c (mw_preview_build): replaced harmless malloc/free pair. Note : mwp->bits is malloc'ed but seems to be never freed. * plug-ins/fractaltrace/fractaltrace.c (pixels_free): replaced a bunch of segfaulting free. (pixels_get, dialog_show): replaced gtk_signal_connect_object with gtk_signal_connect to accomodate callbacks (caused STRANGE dialog behaviour, coz you destroyed buttons one by one). * plug-ins/illusion/illusion.c (dialog): same gtk_signal_connect_object replacement for same reasons. * plug-ins/libgck/gck/gckcolor.c : changed all gck_rgb_to_color* functions to use a static GdkColor instead of a malloc'ed area. Provided reentrant functions with the old behaviour (gck_rgb_to_color*_r). Made some private functions static, too. gck_rgb_to_gdkcolor now use the new functions while gck_rgb_to_gdkcolor_r is the reentrant version. Also affected by this change: gck_gc_set_foreground and gck_gc_set_background (no more free(color)). * plug-ins/libgck/gck/gckcolor.h : added the gck_rgb_to_gdkcolor_r proto. * plug-ins/lic/lic.c (ok_button_clicked, cancel_button_clicked) : segfault on gtk_widget_destroy, now calls gtk_main_quit. (dialog_destroy) : segfault on window closure when called by "destroy" event. Now called by "delete_event". * plug-ins/megawidget/megawidget.c (mw_preview_build): replaced harmless malloc/free pair. Note : mwp->bits is malloc'ed but seems to be never freed. * plug-ins/png/png.c (load_image): replaced 2 segfaulting free. * plug-ins/print/print-ps.c (ps_print): replaced a segfaulting free (called many times :). * plug-ins/sgi/sgi.c (load_image, save_image): replaced a bunch of segfaulting free, and did some harmless inits to avoid a few gcc warnings. * plug-ins/wind/wind.c (render_wind): replaced a segfaulting free. (render_blast): replaced harmless malloc/free pair. * plug-ins/bmp/bmpread.c (ReadImage): yet another free()/g_free() problem fixed. * plug-ins/exchange/exchange.c (real_exchange): ditto. * plug-ins/fp/fp.h: added Frames_Check_Button_In_A_Box proto. * plug-ins/fp/fp_gtk.c: closing subdialogs via window manager wasn't handled, thus leading to errors and crashes. Now delete_event signals the dialog control button to close a dialog with the good way. * plug-ins/ifscompose/ifscompose.c (value_pair_create): tried to set events mask on scale widget (a NO_WINDOW widget). * plug-ins/png/png.c (save_image): Replaced 2 free() with g_free() for g_malloc'ed memory. Mysteriously I corrected the loading bug but not the saving one :) -Yosh
1999-04-16 05:49:09 +08:00
g_free (xpostab);
g_free (ypostab);
1998-06-11 14:04:59 +08:00
}
const GimpPlugInInfo PLUG_IN_INFO =
1998-06-11 14:04:59 +08:00
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
1998-06-11 14:04:59 +08:00
};
MAIN ()