1998-06-11 14:04:59 +08:00
|
|
|
/*************************************/
|
|
|
|
/* GIMP image manipulation routines. */
|
|
|
|
/*************************************/
|
|
|
|
|
2000-12-30 08:23:34 +08:00
|
|
|
#include "config.h"
|
2000-02-18 21:59:18 +08:00
|
|
|
|
2003-12-15 01:17:56 +08:00
|
|
|
#include <gtk/gtk.h>
|
2000-02-18 21:59:18 +08:00
|
|
|
|
2003-12-15 01:17:56 +08:00
|
|
|
#include <libgimp/gimp.h>
|
2000-02-18 21:59:18 +08:00
|
|
|
|
|
|
|
#include "lighting_main.h"
|
1998-06-11 14:04:59 +08:00
|
|
|
#include "lighting_image.h"
|
2000-02-18 07:48:13 +08:00
|
|
|
#include "lighting_preview.h"
|
|
|
|
#include "lighting_ui.h"
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2003-12-15 01:17:56 +08:00
|
|
|
|
2000-08-22 11:27:14 +08:00
|
|
|
GimpDrawable *input_drawable,*output_drawable;
|
|
|
|
GimpPixelRgn source_region, dest_region;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-08-22 11:27:14 +08:00
|
|
|
GimpDrawable *bump_drawable = NULL;
|
|
|
|
GimpPixelRgn bump_region;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-08-22 11:27:14 +08:00
|
|
|
GimpDrawable *env_drawable = NULL;
|
|
|
|
GimpPixelRgn env_region;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
guchar *preview_rgb_data = NULL;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
glong maxcounter;
|
|
|
|
gint imgtype, width, height, env_width, env_height, in_channels, out_channels;
|
2001-01-02 02:35:09 +08:00
|
|
|
GimpRGB background;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
gint border_x1, border_y1, border_x2, border_y2;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
guchar sinemap[256], spheremap[256], logmap[256];
|
1998-06-11 14:04:59 +08:00
|
|
|
|
|
|
|
/******************/
|
|
|
|
/* Implementation */
|
|
|
|
/******************/
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
guchar
|
2000-08-22 11:27:14 +08:00
|
|
|
peek_map (GimpPixelRgn *region,
|
2000-02-18 07:48:13 +08:00
|
|
|
gint x,
|
|
|
|
gint y)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2002-10-17 05:18:11 +08:00
|
|
|
guchar data[4];
|
|
|
|
guchar ret_val;
|
|
|
|
|
|
|
|
|
|
|
|
gimp_pixel_rgn_get_pixel (region, data, x, y);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2002-10-17 05:18:11 +08:00
|
|
|
if (region->bpp == 1)
|
|
|
|
{
|
|
|
|
ret_val = data[0];
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
ret_val = (guchar)((float)((data[0] + data[1] + data[2])/3.0));
|
|
|
|
}
|
2000-02-18 07:48:13 +08:00
|
|
|
|
2002-10-17 05:18:11 +08:00
|
|
|
return ret_val;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
GimpRGB
|
2000-02-18 07:48:13 +08:00
|
|
|
peek (gint x,
|
|
|
|
gint y)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
|
|
|
guchar data[4];
|
2001-01-02 02:35:09 +08:00
|
|
|
GimpRGB color;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
gimp_pixel_rgn_get_pixel (&source_region,data, x, y);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
color.r = (gdouble) (data[0]) / 255.0;
|
|
|
|
color.g = (gdouble) (data[1]) / 255.0;
|
|
|
|
color.b = (gdouble) (data[2]) / 255.0;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
if (input_drawable->bpp == 4)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
if (in_channels == 4)
|
|
|
|
color.a = (gdouble) (data[3]) / 255.0;
|
1998-06-11 14:04:59 +08:00
|
|
|
else
|
2000-02-18 07:48:13 +08:00
|
|
|
color.a = 1.0;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
else
|
2000-12-30 08:23:34 +08:00
|
|
|
{
|
|
|
|
color.a = 1.0;
|
|
|
|
}
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
return color;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
GimpRGB
|
2000-02-18 07:48:13 +08:00
|
|
|
peek_env_map (gint x,
|
|
|
|
gint y)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
|
|
|
guchar data[4];
|
2001-01-02 02:35:09 +08:00
|
|
|
GimpRGB color;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
if (x < 0)
|
|
|
|
x = 0;
|
|
|
|
else if (x >= env_width)
|
|
|
|
x = env_width - 1;
|
|
|
|
if (y < 0)
|
|
|
|
y = 0;
|
|
|
|
else if (y >= env_height)
|
|
|
|
y = env_height - 1;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
gimp_pixel_rgn_get_pixel (&env_region, data, x, y);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
color.r = (gdouble) (data[0]) / 255.0;
|
|
|
|
color.g = (gdouble) (data[1]) / 255.0;
|
|
|
|
color.b = (gdouble) (data[2]) / 255.0;
|
|
|
|
color.a = 1.0;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
return color;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
void
|
|
|
|
poke (gint x,
|
|
|
|
gint y,
|
2001-01-02 02:35:09 +08:00
|
|
|
GimpRGB *color)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
|
|
|
static guchar data[4];
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
if (x < 0)
|
|
|
|
x = 0;
|
|
|
|
else if (x >= width)
|
|
|
|
x = width - 1;
|
|
|
|
if (y < 0)
|
|
|
|
y = 0;
|
|
|
|
else if (y >= height)
|
|
|
|
y = height - 1;
|
|
|
|
|
|
|
|
data[0] = (guchar) (color->r * 255.0);
|
|
|
|
data[1] = (guchar) (color->g * 255.0);
|
|
|
|
data[2] = (guchar) (color->b * 255.0);
|
|
|
|
data[3] = (guchar) (color->a * 255.0);
|
|
|
|
|
|
|
|
gimp_pixel_rgn_set_pixel (&dest_region, data, x, y);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
gint
|
|
|
|
check_bounds (gint x,
|
|
|
|
gint y)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
if (x < border_x1 || y < border_y1 || x >= border_x2 || y >= border_y2)
|
|
|
|
return FALSE;
|
1998-06-11 14:04:59 +08:00
|
|
|
else
|
2000-02-18 07:48:13 +08:00
|
|
|
return TRUE;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
GimpVector3
|
|
|
|
int_to_pos (gint x,
|
|
|
|
gint y)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-15 00:29:41 +08:00
|
|
|
GimpVector3 pos;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
if (width >= height)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
pos.x = (gdouble) x / (gdouble) width;
|
|
|
|
pos.y = (gdouble) y / (gdouble) width;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
pos.y += 0.5 * (1.0 - (gdouble) height / (gdouble) width);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
pos.x = (gdouble) x / (gdouble) height;
|
|
|
|
pos.y = (gdouble) y / (gdouble) height;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
pos.x += 0.5 * (1.0 - (gdouble) width / (gdouble) height);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
2000-02-18 07:48:13 +08:00
|
|
|
|
|
|
|
pos.z = 0.0;
|
|
|
|
return pos;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
GimpVector3
|
|
|
|
int_to_posf (gdouble x,
|
|
|
|
gdouble y)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-15 00:29:41 +08:00
|
|
|
GimpVector3 pos;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
if (width >= height)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
pos.x = x / (gdouble) width;
|
|
|
|
pos.y = y / (gdouble) width;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
pos.y += 0.5 * (1.0 - (gdouble) height / (gdouble) width);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
pos.x = x / (gdouble) height;
|
|
|
|
pos.y = y / (gdouble) height;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
pos.x += 0.5 * (1.0 - (gdouble) width / (gdouble) height);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
2000-02-18 07:48:13 +08:00
|
|
|
|
|
|
|
pos.z = 0.0;
|
|
|
|
return pos;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
void
|
|
|
|
pos_to_int (gdouble x,
|
|
|
|
gdouble y,
|
|
|
|
gint *scr_x,
|
2000-12-30 08:23:34 +08:00
|
|
|
gint *scr_y)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
if (width >= height)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
y -= 0.5 * (1.0 - (gdouble) height / (gdouble) width);
|
2000-12-30 08:23:34 +08:00
|
|
|
*scr_x = RINT ((x * (gdouble) width));
|
|
|
|
*scr_y = RINT ((y * (gdouble) width));
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
x -= 0.5 * (1.0 - (gdouble) width / (gdouble) height);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-12-30 08:23:34 +08:00
|
|
|
*scr_x = RINT ((x * (gdouble) height));
|
|
|
|
*scr_y = RINT ((y *(gdouble) height));
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
void
|
|
|
|
pos_to_float (gdouble x,
|
|
|
|
gdouble y,
|
|
|
|
gdouble *xf,
|
|
|
|
gdouble *yf)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
if (width >= height)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
y -= 0.5 * (1.0 - (gdouble) height / (gdouble) width);
|
|
|
|
|
2001-09-25 12:20:23 +08:00
|
|
|
*xf = x * (gdouble) (width-1);
|
|
|
|
*yf = y * (gdouble) (width-1);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
x -= 0.5 * (1.0 - (gdouble) width / (gdouble) height);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2001-09-25 12:20:23 +08:00
|
|
|
*xf = x * (gdouble) (height-1);
|
|
|
|
*yf = y * (gdouble) (height-1);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************/
|
|
|
|
/* Compute the image color at pos (u,v) using */
|
|
|
|
/* Quartics bilinear interpolation stuff. */
|
|
|
|
/**********************************************/
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
GimpRGB
|
2000-02-18 07:48:13 +08:00
|
|
|
get_image_color (gdouble u,
|
|
|
|
gdouble v,
|
|
|
|
gint *inside)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2001-01-23 08:53:12 +08:00
|
|
|
gint x1, y1, x2, y2;
|
2001-01-02 02:35:09 +08:00
|
|
|
GimpRGB p[4];
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-12-30 08:23:34 +08:00
|
|
|
x1 = RINT (u);
|
|
|
|
y1 = RINT (v);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
if (check_bounds (x1, y1) == FALSE)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
*inside = FALSE;
|
|
|
|
return background;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
x2 = (x1 + 1);
|
|
|
|
y2 = (y1 + 1);
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
if (check_bounds (x2, y2) == FALSE)
|
|
|
|
{
|
|
|
|
*inside = TRUE;
|
|
|
|
return peek (x1, y1);
|
|
|
|
}
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
*inside = TRUE;
|
|
|
|
p[0] = peek (x1, y1);
|
|
|
|
p[1] = peek (x2, y1);
|
|
|
|
p[2] = peek (x1, y2);
|
|
|
|
p[3] = peek (x2, y2);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2001-01-23 08:53:12 +08:00
|
|
|
return gimp_bilinear_rgba (u, v, p);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
gdouble
|
2000-08-22 11:27:14 +08:00
|
|
|
get_map_value (GimpPixelRgn *region,
|
2000-02-18 07:48:13 +08:00
|
|
|
gdouble u,
|
|
|
|
gdouble v,
|
|
|
|
gint *inside)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
|
|
|
gint x1, y1, x2, y2;
|
|
|
|
gdouble p[4];
|
|
|
|
|
2000-12-30 08:23:34 +08:00
|
|
|
x1 = RINT (u);
|
|
|
|
y1 = RINT (v);
|
2000-02-18 07:48:13 +08:00
|
|
|
|
1998-06-11 14:04:59 +08:00
|
|
|
x2 = (x1 + 1);
|
|
|
|
y2 = (y1 + 1);
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
if (check_bounds (x2, y2) == FALSE)
|
|
|
|
{
|
|
|
|
*inside = TRUE;
|
|
|
|
return (gdouble) peek_map (region, x1, y1);
|
|
|
|
}
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
*inside = TRUE;
|
|
|
|
p[0] = (gdouble) peek_map (region, x1, y1);
|
|
|
|
p[1] = (gdouble) peek_map (region, x2, y1);
|
|
|
|
p[2] = (gdouble) peek_map (region, x1, y2);
|
|
|
|
p[3] = (gdouble) peek_map (region, x2, y2);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2001-01-23 08:53:12 +08:00
|
|
|
return gimp_bilinear (u, v, p);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 21:59:18 +08:00
|
|
|
static void
|
2000-02-18 07:48:13 +08:00
|
|
|
compute_maps (void)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
gint x;
|
|
|
|
gdouble val, c, d;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
|
|
|
/* Compute Sine, Log ans Spherical transfer function maps */
|
|
|
|
/* ====================================================== */
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
c = 1.0 / 255.0;
|
|
|
|
d = 1.15 * 255.0;
|
|
|
|
|
|
|
|
for (x = 0; x < 256; x++)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
sinemap[x] = (guchar) (255.0 * (0.5 * (sin ((G_PI * c * (gdouble) x) -
|
|
|
|
0.5 * G_PI) +
|
|
|
|
1.0)));
|
|
|
|
spheremap[x] = (guchar) (255.0 * (sqrt (sin (G_PI * (gdouble) x /
|
|
|
|
512.0))));
|
|
|
|
val = (d * exp (-1.0 / (8.0 * c * ((gdouble) x + 5.0))));
|
|
|
|
|
|
|
|
if (val > 255.0)
|
|
|
|
val = 255.0;
|
|
|
|
logmap[x] = (guchar) val;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/* Allocate memory for temporary images */
|
|
|
|
/****************************************/
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
gint
|
2000-08-22 11:27:14 +08:00
|
|
|
image_setup (GimpDrawable *drawable,
|
2003-12-15 01:17:56 +08:00
|
|
|
gint interactive)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2000-02-18 07:48:13 +08:00
|
|
|
compute_maps ();
|
|
|
|
|
1998-06-11 14:04:59 +08:00
|
|
|
/* Get some useful info on the input drawable */
|
|
|
|
/* ========================================== */
|
|
|
|
|
2000-12-30 08:23:34 +08:00
|
|
|
input_drawable = drawable;
|
2000-02-18 07:48:13 +08:00
|
|
|
output_drawable = drawable;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2001-06-15 04:07:38 +08:00
|
|
|
gimp_drawable_mask_bounds (drawable->drawable_id,
|
2000-02-18 07:48:13 +08:00
|
|
|
&border_x1, &border_y1, &border_x2, &border_y2);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-12-30 08:23:34 +08:00
|
|
|
width = input_drawable->width;
|
2000-02-18 07:48:13 +08:00
|
|
|
height = input_drawable->height;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
gimp_pixel_rgn_init (&source_region, input_drawable,
|
|
|
|
0, 0, width, height, FALSE, FALSE);
|
1998-06-11 14:04:59 +08:00
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
maxcounter = (glong) width * (glong) height;
|
1998-06-11 14:04:59 +08:00
|
|
|
|
|
|
|
/* Assume at least RGB */
|
|
|
|
/* =================== */
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
in_channels = 3;
|
2001-06-15 04:07:38 +08:00
|
|
|
if (gimp_drawable_has_alpha (input_drawable->drawable_id) == TRUE)
|
1998-06-11 14:04:59 +08:00
|
|
|
in_channels++;
|
|
|
|
|
2003-12-15 01:17:56 +08:00
|
|
|
if (interactive)
|
1998-06-11 14:04:59 +08:00
|
|
|
{
|
2003-12-15 01:17:56 +08:00
|
|
|
preview_rgb_data = g_new0 (guchar, PREVIEW_WIDTH * PREVIEW_HEIGHT * 3);
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 07:48:13 +08:00
|
|
|
return TRUE;
|
1998-06-11 14:04:59 +08:00
|
|
|
}
|