Commit 89fd6901 authored by Maurits Rijk's avatar Maurits Rijk
Browse files

Fix for #119937 and #119938 plus lots of code cleanup.

parent 22c5a50b
2003-08-27 Maurits Rijk <lpeek.mrijk@consunet.nl>
* plug-ins/common/curve_bend.c: (party) applied patch from
hof@gimp.org. Fixes both #119937 and #119938. Some cleanup of the
code.
2003-08-27 Michael Natterer <mitch@gimp.org>
 
Cleaned up my "To Selection" cleanup:
/* curve_bend plugin for the GIMP (tested with GIMP 1.1.17, requires gtk+ 1.2) */
/* curve_bend plugin for the GIMP */
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
......@@ -19,6 +19,7 @@
*/
/* Revision history
* (2003/08/24) v1.3.18 hof: #119937 show busy cursor when recalculating preview
* (2002/09/xx) v1.1.18 mitch and gsr: clean interface
* (2000/02/16) v1.1.17b hof: added spinbuttons for rotate entry
* (2000/02/16) v1.1.17 hof: undo bugfix (#6012)
......@@ -52,7 +53,7 @@
/* Defines */
#define PLUG_IN_NAME "plug_in_curve_bend"
#define PLUG_IN_VERSION "v1.1.17 (2000/02/16)"
#define PLUG_IN_VERSION "v1.3.18 (2003/08/26)"
#define PLUG_IN_IMAGE_TYPES "RGB*, GRAY*"
#define PLUG_IN_AUTHOR "Wolfgang Hofer (hof@hotbot.com)"
#define PLUG_IN_COPYRIGHT "Wolfgang Hofer"
......@@ -122,12 +123,10 @@ struct _BenderValues
gint work_on_copy;
gdouble rotation;
gint32 total_steps;
gdouble current_step;
};
typedef struct _Curves Curves;
struct _Curves
......@@ -147,6 +146,9 @@ struct _BenderDialog
GdkPixmap *pixmap;
GtkWidget *filesel;
GdkCursor *cursor_wait;
GdkCursor *cursor_acitve;
GimpDrawable *drawable;
int color;
int outline;
......@@ -216,44 +218,18 @@ struct _BenderDialog
typedef double CRMatrix[4][4];
/* p_buildmenu Structures */
typedef struct _MenuItem MenuItem;
typedef void (*MenuItemCallback) (GtkWidget *widget,
gpointer user_data);
struct _MenuItem
{
gchar *label;
gchar unused_accelerator_key;
gint unused_accelerator_mods;
MenuItemCallback callback;
gpointer user_data;
MenuItem *unused_subitems;
GtkWidget *widget;
};
typedef struct
{
GimpDrawable *drawable;
GimpPixelRgn pr;
gint x1;
gint y1;
gint x2;
gint y2;
gint index_alpha; /* 0 == no alpha, 1 == GREYA, 3 == RGBA */
gint bpp;
GimpTile *tile;
gint tile_row;
gint tile_col;
GimpPixelFetcher *pft;
gint tile_width;
gint tile_height;
gint tile_dirty;
gint shadow;
gint32 seldeltax;
gint32 seldeltay;
gint32 tile_swapcount;
} t_GDRW;
typedef struct
......@@ -279,10 +255,8 @@ static void bender_plot_curve (BenderDialog *,
static void bender_calculate_curve (BenderDialog *, gint32,
gint32, gint);
static void bender_rotate_adj_callback (GtkAdjustment *, gpointer);
static void bender_upper_callback (GtkWidget *, gpointer);
static void bender_lower_callback (GtkWidget *, gpointer);
static void bender_smooth_callback (GtkWidget *, gpointer);
static void bender_free_callback (GtkWidget *, gpointer);
static void bender_border_callback (GtkWidget *, gpointer);
static void bender_type_callback (GtkWidget *, gpointer);
static void bender_reset_callback (GtkWidget *, gpointer);
static void bender_copy_callback (GtkWidget *, gpointer);
static void bender_copy_inv_callback (GtkWidget *, gpointer);
......@@ -304,7 +278,6 @@ static void bender_CR_compose (CRMatrix, CRMatrix,
CRMatrix);
static void bender_init_min_max (BenderDialog *, gint32);
static BenderDialog * do_dialog (GimpDrawable *);
GtkWidget * p_buildmenu (MenuItem *);
static void p_init_gdrw (t_GDRW *gdrw, GimpDrawable *drawable,
int dirty, int shadow);
static void p_end_gdrw (t_GDRW *gdrw);
......@@ -353,21 +326,7 @@ static CRMatrix CR_basis =
{ 0.0, 1.0, 0.0, 0.0 },
};
static MenuItem outline_items[] =
{
{ N_("Upper"), 0, 0, bender_upper_callback, NULL, NULL, NULL },
{ N_("Lower"), 0, 0, bender_lower_callback, NULL, NULL, NULL },
{ NULL, 0, 0, NULL, NULL, NULL, NULL }
};
static MenuItem curve_type_items[] =
{
{ N_("Smooth"), 0, 0, bender_smooth_callback, NULL, NULL, NULL },
{ N_("Free"), 0, 0, bender_free_callback, NULL, NULL, NULL },
{ NULL, 0, 0, NULL, NULL, NULL, NULL }
};
int gb_debug = FALSE;
static int gb_debug = FALSE;
/* Functions */
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX PDB_STUFF XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
......@@ -411,21 +370,14 @@ p_pdb_procedure_available (const gchar *proc_name)
&l_params, &l_return_vals))
{
/* procedure found in PDB */
return (l_nparams);
return l_nparams;
}
printf("Warning: Procedure %s not found.\n", proc_name);
return -1;
} /* end p_pdb_procedure_available */
/* ============================================================================
* p_gimp_rotate
* PDB call of 'gimp_rotate'
* ============================================================================
*/
}
gint
static gint
p_gimp_rotate (gint32 image_id,
gint32 drawable_id,
gint32 interpolation,
......@@ -501,111 +453,38 @@ p_gimp_rotate (gint32 image_id,
{
printf("Error: Procedure %s not found.\n",l_rotate_proc);
}
return (l_rc);
} /* end p_gimp_rotate */
/* ============================================================================
* p_gimp_edit_copy
*
* ============================================================================
*/
gint32
p_gimp_edit_copy (gint32 image_id,
gint32 drawable_id)
{
static char *l_procname = "gimp_edit_copy";
GimpParam *return_vals;
int nreturn_vals;
if (p_pdb_procedure_available(l_procname) >= 0)
{
return_vals = gimp_run_procedure (l_procname,
&nreturn_vals,
GIMP_PDB_DRAWABLE, drawable_id,
GIMP_PDB_END);
if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
{
gimp_destroy_params (return_vals, nreturn_vals);
return 0;
}
printf("Error: PDB call of %s failed status:%d\n", l_procname, (int)return_vals[0].data.d_status);
}
return(-1);
} /* end p_gimp_edit_copy */
/* ============================================================================
* p_gimp_edit_paste
*
* ============================================================================
*/
gint32
p_gimp_edit_paste (gint32 image_id,
gint32 drawable_id,
gint32 paste_into)
{
static char *l_procname = "gimp_edit_paste";
GimpParam *return_vals;
int nreturn_vals;
gint32 fsel_layer_id;
if (p_pdb_procedure_available(l_procname) >= 0)
{
return_vals = gimp_run_procedure (l_procname,
&nreturn_vals,
GIMP_PDB_DRAWABLE, drawable_id,
GIMP_PDB_INT32, paste_into,
GIMP_PDB_END);
if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
{
fsel_layer_id = return_vals[1].data.d_layer;
gimp_destroy_params (return_vals, nreturn_vals);
return (fsel_layer_id);
}
printf("Error: PDB call of %s failed status:%d\n", l_procname, (int)return_vals[0].data.d_status);
}
return(-1);
} /* end p_gimp_edit_paste */
return l_rc;
}
/* ============================================================================
* p_if_selection_float_it
* ============================================================================
*/
gint32
static gint32
p_if_selection_float_it (gint32 image_id,
gint32 layer_id)
{
gint32 l_sel_channel_id;
gint32 l_layer_id;
gint32 l_x1, l_x2, l_y1, l_y2;
gint32 non_empty;
l_layer_id = layer_id;
if(!gimp_layer_is_floating_selection (layer_id) )
{
/* check and see if we have a selection mask */
l_sel_channel_id = gimp_image_get_selection(image_id);
gimp_selection_bounds (image_id, &non_empty, &l_x1, &l_y1, &l_x2, &l_y2);
if (non_empty && l_sel_channel_id >= 0)
if (!gimp_layer_is_floating_selection (layer_id))
{
/* selection is TRUE, make a layer (floating selection) from the selection */
p_gimp_edit_copy(image_id, layer_id);
l_layer_id = p_gimp_edit_paste(image_id, layer_id, FALSE);
gint32 l_sel_channel_id;
gint32 l_x1, l_x2, l_y1, l_y2;
gint32 non_empty;
/* check and see if we have a selection mask */
l_sel_channel_id = gimp_image_get_selection(image_id);
gimp_selection_bounds (image_id, &non_empty, &l_x1, &l_y1, &l_x2, &l_y2);
if (non_empty && l_sel_channel_id >= 0)
{
/* selection is TRUE, make a layer (floating selection) from
the selection */
gimp_edit_copy (layer_id);
layer_id = gimp_edit_paste (layer_id, FALSE);
}
}
}
return(l_layer_id);
return layer_id;
}
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX END_PDB_STUFF XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
......@@ -680,7 +559,7 @@ query (void)
PLUG_IN_AUTHOR,
PLUG_IN_COPYRIGHT,
PLUG_IN_VERSION,
N_("<Image>/Filters/Distorts/_CurveBend..."),
N_("<Image>/Filters/Distorts/_Curve Bend..."),
PLUG_IN_IMAGE_TYPES,
GIMP_PLUGIN,
G_N_ELEMENTS (args),
......@@ -733,7 +612,7 @@ run (const gchar *name,
cd = NULL;
l_env = g_getenv("BEND_DEBUG");
if(l_env != NULL)
if (l_env != NULL)
{
if((*l_env != 'n') && (*l_env != 'N')) gb_debug = 1;
}
......@@ -798,19 +677,15 @@ run (const gchar *name,
return;
}
/* get image and drawable */
l_image_id = param[1].data.d_int32;
l_layer_id = param[2].data.d_drawable;
gimp_undo_push_group_start (l_image_id);
if(!gimp_drawable_is_layer(l_layer_id))
if (!gimp_drawable_is_layer(l_layer_id))
{
gimp_message(_("CurveBend operates on layers only (but was called on channel or mask)"));
printf("Passed drawable is no Layer\n");
status = GIMP_PDB_EXECUTION_ERROR;
}
/* check for layermask */
......@@ -827,7 +702,6 @@ run (const gchar *name,
/* if there is a selection, make it the floating selection layer */
l_active_drawable = gimp_drawable_get (p_if_selection_float_it(l_image_id, l_layer_id));
/* how are we running today? */
if(status == GIMP_PDB_SUCCESS)
{
......@@ -887,7 +761,7 @@ run (const gchar *name,
}
}
if (cd == NULL)
if (!cd)
{
status = GIMP_PDB_EXECUTION_ERROR;
}
......@@ -896,7 +770,7 @@ run (const gchar *name,
{
/* Run the main function */
if(cd->run)
if (cd->run)
{
l_bent_layer_id = p_main_bend(cd, cd->drawable, cd->work_on_copy);
......@@ -908,13 +782,11 @@ run (const gchar *name,
}
else
{
status = GIMP_PDB_EXECUTION_ERROR; /* dialog ended with cancel button */
status = GIMP_PDB_EXECUTION_ERROR; /* dialog ended with cancel button */
}
gimp_undo_push_group_end (l_image_id);
/* If run mode is interactive, flush displays, else (script) don't
do it, as the screen updates would make the scripts slow */
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
......@@ -925,25 +797,15 @@ run (const gchar *name,
}
values[0].data.d_status = status;
values[1].data.d_int32 = l_bent_layer_id; /* return the id of handled layer */
}
if (gb_debug)
printf ("end run curve_bend plugin\n");
} /* end run */
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
/* Last_VALUEs and ITERATOR stuff */
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
int
static int
p_save_pointfile (BenderDialog *cd,
const gchar *filename)
{
int j;
gint j;
FILE *l_fp;
if(gb_debug) printf("Saving curve to file:%s\n", filename);
l_fp = fopen(filename, "w+");
if (!l_fp)
{
......@@ -960,7 +822,7 @@ p_save_pointfile (BenderDialog *cd,
fprintf(l_fp, "# UPPERX UPPERY LOWERX LOWERY\n");
fprintf(l_fp, "\n");
for(j=0; j < 17; j++)
for(j = 0; j < 17; j++)
{
fprintf(l_fp, "%s %+.6f %+.6f %+.6f %+.6f\n", KEY_POINTS,
(float)cd->points[OUTLINE_UPPER][j][0],
......@@ -975,7 +837,7 @@ p_save_pointfile (BenderDialog *cd,
fprintf(l_fp, "# UPPER_Y LOWER_Y\n");
fprintf(l_fp, "\n");
for(j=0; j < 256; j++)
for (j = 0; j < 256; j++)
{
fprintf(l_fp, "%s %3d %3d\n", KEY_VAL_Y,
(int)cd->curve[OUTLINE_UPPER][j],
......@@ -983,39 +845,35 @@ p_save_pointfile (BenderDialog *cd,
}
fclose(l_fp);
return 0; /* OK */
} /* end p_save_pointfile */
return 0;
}
int
static int
p_load_pointfile (BenderDialog *cd,
const gchar *filename)
{
int l_pi, l_ci, l_n, l_len;
gint l_pi, l_ci, l_n, l_len;
FILE *l_fp;
char l_buff[2000];
float l_fux, l_fuy, l_flx, l_fly;
int l_iuy, l_ily ;
if(gb_debug) printf("Loading curve from file:%s\n", filename);
gint l_iuy, l_ily ;
l_fp = fopen(filename, "r");
if (l_fp == NULL)
if (!l_fp)
{
g_message (_("Failed to open file '%s':\n%s"),
filename, g_strerror (errno));
return -1;
}
l_pi= 0;
l_ci= 0;
l_pi = 0;
l_ci = 0;
fgets (l_buff, 2000-1, l_fp);
if(strncmp(l_buff, KEY_POINTFILE, strlen(KEY_POINTFILE)) == 0)
fgets (l_buff, 2000 - 1, l_fp);
if (strncmp(l_buff, KEY_POINTFILE, strlen(KEY_POINTFILE)) == 0)
{
while (NULL != fgets (l_buff, 2000-1, l_fp))
{
if(gb_debug) printf("FGETS: %s\n", l_buff);
l_len = strlen(KEY_POINTS);
if(strncmp(l_buff, KEY_POINTS, l_len) == 0)
{
......@@ -1026,7 +884,6 @@ p_load_pointfile (BenderDialog *cd,
cd->points[OUTLINE_UPPER][l_pi][1] = l_fuy;
cd->points[OUTLINE_LOWER][l_pi][0] = l_flx;
cd->points[OUTLINE_LOWER][l_pi][1] = l_fly;
if(gb_debug) printf("OK points[%d]\n", l_pi);
l_pi++;
}
else
......@@ -1035,15 +892,14 @@ p_load_pointfile (BenderDialog *cd,
}
}
l_len = strlen(KEY_VAL_Y);
if(strncmp(l_buff, KEY_VAL_Y, l_len) == 0)
if (strncmp(l_buff, KEY_VAL_Y, l_len) == 0)
{
l_n = sscanf(&l_buff[l_len], "%d %d", &l_iuy, &l_ily);
if((l_n == 2) && (l_ci < 256))
if ((l_n == 2) && (l_ci < 256))
{
cd->curve[OUTLINE_UPPER][l_ci] = l_iuy;
cd->curve[OUTLINE_LOWER][l_ci] = l_ily;
l_ci++;
if(gb_debug) printf("OK y_val[%d]\n", l_pi);
}
else
{
......@@ -1054,31 +910,29 @@ p_load_pointfile (BenderDialog *cd,
}
}
fclose(l_fp);
return 0; /* OK */
} /* end p_load_pointfile */
return 0;
}
void
static void
p_cd_to_bval (BenderDialog *cd,
BenderValues *bval)
{
int i,j;
gint i,j;
for(i=0; i<2; i++)
for (i = 0; i < 2; i++)
{
for(j=0; j<256; j++)
for(j = 0; j < 256; j++)
{
bval->curve[i][j] = cd->curve[i][j];
}
for(j=0; j<17; j++)
for(j = 0; j < 17; j++)
{
bval->points[i][j][0] = cd->points[i][j][0]; /* x */
bval->points[i][j][1] = cd->points[i][j][1]; /* y */
}
}
bval->curve_type = cd->curve_type;
bval->smoothing = cd->smoothing;
bval->antialias = cd->antialias;
......@@ -1086,22 +940,22 @@ p_cd_to_bval (BenderDialog *cd,
bval->rotation = cd->rotation;
bval->total_steps = 0;
bval->current_step =0.0;
bval->current_step = 0.0;
}
void
static void
p_cd_from_bval(BenderDialog *cd, BenderValues *bval)
{
int i,j;
gint i,j;
for(i=0; i<2; i++)
for(i = 0; i < 2; i++)
{
for(j=0; j<256; j++)
for(j = 0; j < 256; j++)
{
cd->curve[i][j] = bval->curve[i][j];
}
for(j=0; j<17; j++)
for(j = 0; j < 17; j++)
{
cd->points[i][j][0] = bval->points[i][j][0]; /* x */
cd->points[i][j][1] = bval->points[i][j][1]; /* y */
......@@ -1113,10 +967,9 @@ p_cd_from_bval(BenderDialog *cd, BenderValues *bval)
cd->antialias = bval->antialias;
cd->work_on_copy = bval->work_on_copy;
cd->rotation = bval->rotation;
}
void
static void
p_store_values (BenderDialog *cd)
{
BenderValues l_bval;
......@@ -1125,7 +978,7 @@ p_store_values (BenderDialog *cd)
gimp_set_data(PLUG_IN_NAME, &l_bval, sizeof(l_bval));
}
void
static void
p_retrieve_values (BenderDialog *cd)
{
BenderValues l_bval;
......@@ -1135,7 +988,7 @@ p_retrieve_values (BenderDialog *cd)
gimp_get_data (PLUG_IN_NAME, &l_bval);
if(l_bval.total_steps == 0)
if (l_bval.total_steps == 0)
{
cd->bval_from = NULL;
cd->bval_to = NULL;
......@@ -1161,7 +1014,6 @@ p_retrieve_values (BenderDialog *cd)
}
}
static void
p_delta_gdouble (double *val,
double val_from,
......@@ -1186,13 +1038,12 @@ p_delta_gint32 (gint32 *val,
{
double delta;
if(total_steps < 1) return;
if (total_steps < 1) return;
delta = ((double)(val_to - val_from) / (double)total_steps) * ((double)total_steps - current_step);
*val = val_from + delta;
}
void
p_copy_points (BenderDialog *cd,
int outline,
......@@ -1202,11 +1053,11 @@ p_copy_points (BenderDialog *cd,
{
int j;
for(j=0; j < 17; j++)
for (j = 0; j < 17; j++)
{
cd->points[outline][j][xy] = -1;
}
for(j=0; j < argc; j++)
for(j = 0; j < argc; j++)
{
cd->points[outline][j][xy] = floatarray[j];
}
......@@ -1222,9 +1073,9 @@ p_copy_yval (BenderDialog *cd,
guchar fill;