1 /*
2 Parameters for resynthesizer Gimp plugin.
3 Hides indexing of GimpParam via internal ParametersStruct with named fields.
4 Also stores last values as Gimp data under the name of the plugin.
5 
6 This is shared by the gui control plugin and the engine plugin.
7 
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12 
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17 
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22 
23 
24 /*
25 Parameters passed from a Gimp plugin
26 to the adapter from Gimp to the innermost engine.
27 */
28 typedef struct GIMPAdapterParametersStruct {
29 
30   int h_tile;
31   int v_tile;
32   int use_border;
33 
34   /* GIMP.  gint32 */
35   int corpus_id, input_map_id, output_map_id;
36 
37   double map_weight;
38   double autism;
39 
40   int neighbours;
41   int trys;
42 } TGimpAdapterParameters;
43 
44 
45 
46 static GimpParamDef resynth_paramdefs[] =
47 {
48   { GIMP_PDB_INT32,     (gchar*)"run_mode",   (gchar*)"Interactive, non-interactive" },
49   { GIMP_PDB_IMAGE,     (gchar*)"image",      (gchar*)"Input image" },
50   { GIMP_PDB_DRAWABLE,  (gchar*)"drawable",   (gchar*)"Input drawable" },
51 
52   { GIMP_PDB_INT32,     (gchar*)"vtile",      (gchar*)"Make tilable vertically 0,1" },
53   { GIMP_PDB_INT32,     (gchar*)"htile",      (gchar*)"Make tilable horizontally 0,1" },
54   /*
55   This parameter tells whether and how to make the edges match the context.
56   It is moot if there is no selection, i.e. the entire image is synthesized.
57   With version 1.0, it also has expanded meaning, with values beyond 0,1 meaning
58   which direction to proceed with synthesis.  E.G. 2 means inward, giving more coherence
59   with context.
60   */
61   { GIMP_PDB_INT32,     (gchar*)"use_context",   (gchar*)"Make edges match context 0,1,2,..." },
62   /* The next three should more specifically be GIMP_PDB_DRAWABLE.
63   But for backward compatibility, maintain as GIMP_PDB_INT32 for the drawable ID.
64   From python drawable object, pass drawable.ID or -1 (not None)
65   */
66   { GIMP_PDB_INT32,     (gchar*)"corpus",     (gchar*)"Layer to use as corpus" },
67   { GIMP_PDB_INT32,     (gchar*)"inmask",     (gchar*)"Layer to use as input mask, -1 for none" },
68   { GIMP_PDB_INT32,     (gchar*)"outmask",    (gchar*)"Layer to use as output mask, -1 for none" },
69   { GIMP_PDB_FLOAT,     (gchar*)"map_weight", (gchar*)"Weight to give to map, if map is used" },
70   { GIMP_PDB_FLOAT,     (gchar*)"autism",     (gchar*)"Sensitivity to outliers" },
71   { GIMP_PDB_INT32,     (gchar*)"neighbourhood", (gchar*)"Neighbourhood size" },
72   { GIMP_PDB_INT32,     (gchar*)"trys",       (gchar*)"Search thoroughness" }
73 };
74 
75 
76 /*
77 Set the default parameters for the GIMP plugin that adapts the engine.
78 The parameters to the plugin are distinct from parameters to the engine.
79 */
80 static void
setDefaultPluginParams(TGimpAdapterParameters * param)81 setDefaultPluginParams(
82   TGimpAdapterParameters* param
83   )
84 {
85   /*
86   Following are unique to the plugin.
87   These are 3 of the 4 images that can be passed to the engine.
88   The 4th image is always passed to every plugin and is not listed here.
89   */
90   param->corpus_id = -1;
91   param->input_map_id = -1;
92   param->output_map_id = -1;
93   // Following are similar to parameters to engine.
94   param->v_tile = FALSE;  // lkk was TRUE
95   param->h_tile = FALSE;
96   param->use_border = 1;    // lkk was true, now is an enum
97   param->map_weight = 0.5;
98   param->autism = 0.117; // 30/256
99   param->neighbours = 30;
100   param->trys = 200;
101 }
102 
103 
104 /* Restore the last parameters used, get from GIMP. */
105 static gboolean
get_last_parameters(TGimpAdapterParameters * param,gint default_drawable,gchar * plugin_name)106 get_last_parameters(
107   TGimpAdapterParameters* param,
108   gint default_drawable,
109   gchar * plugin_name
110   )
111 {
112   /* Defaults in case this is our first run */
113   setDefaultPluginParams(param);
114 
115   if ( ! gimp_get_data(plugin_name, param))
116     /* No data was stored previously, not an exception. */
117     gimp_message("No last settings, using defaults.");
118 
119   /* If image was resynthesized from itself last time, resythesize this image from itself too */
120   if (param->corpus_id == -1)
121     param->corpus_id = default_drawable;
122 
123   return TRUE;  /* TODO should be void. */
124 }
125 
126 /*
127 Remember the last parameters the user entered.
128 Only called for runmode INTERACTIVE, by the control plugin,
129 not by the engine plugin.
130 */
131 static void
set_last_parameters(TGimpAdapterParameters * params,gint drawable_id)132 set_last_parameters(
133   TGimpAdapterParameters* params,
134   gint drawable_id
135   )
136 {
137   /*
138   The GUI defaults the corpus drawable to the same as the target drawable.
139   We really want to store the fact that the user did not choose a different corpus drawable.
140   Use -1 to signify that, then we can restore that condition, see get_last_parameters().
141 
142   TODO
143   We can't store a drawable_id meaningfully since they are not persistent ID's.
144   What if the drawable has been deleted in this session?
145   Also, test what happens with map drawable IDs.
146   */
147   if (params->corpus_id == drawable_id)
148     params->corpus_id = -1;
149   gimp_set_data(RESYNTH_CONTROLS_PDB_NAME, params, sizeof(TGimpAdapterParameters));
150   if (params->corpus_id == -1)
151     params->corpus_id = drawable_id;
152 }
153 
154 
155 /* Convert argument list into internal parameters */
156 static gboolean
get_parameters_from_list(TGimpAdapterParameters * param,gint n_args,const GimpParam * args)157 get_parameters_from_list(
158   TGimpAdapterParameters *param,
159   gint n_args,
160   const GimpParam *args
161   )
162 {
163   if (n_args != RESYNTH_PARAMETER_COUNT)
164   {
165     gimp_message("Wrong parameter count.");
166     return FALSE;
167   }
168 
169   param->v_tile = args[3].data.d_int32;
170   param->h_tile = args[4].data.d_int32;
171   param->use_border = args[5].data.d_int32;
172   param->corpus_id = args[6].data.d_int32;      /* a d_drawable? */
173   param->input_map_id = args[7].data.d_int32;
174   param->output_map_id = args[8].data.d_int32;
175   param->map_weight = args[9].data.d_float;
176   param->autism = args[10].data.d_float;
177   param->neighbours = args[11].data.d_int32;
178   param->trys = args[12].data.d_int32;
179 
180   return TRUE;
181 }
182 
183 /*
184 Copy passed parameters and user entered parameters to new Gimp params struct,
185 to pass to the engine.
186 We can't use the const GimpParam passed in: a compiler warning about writing a const,
187 and its too small (nargs ==3 for INTERACTIVE.)
188 */
189 static void
set_parameters_to_list(const TGimpAdapterParameters * param,const GimpParam * args,const gint nargs,gint runmode,GimpParam temp[])190 set_parameters_to_list(
191   const TGimpAdapterParameters *param, // IN  parameter values user chose
192   const GimpParam *args,  // IN   args: parameters passed to control plugin
193   const gint nargs,       // IN
194   gint runmode,           // IN
195   GimpParam temp[]        // OUT
196   )
197 {
198   gint i;
199 
200   /* !!! Note c won't let you check lengh of temp, it must be RESYNTH_PARAMETER_COUNT. */
201   g_assert(nargs>=3 && nargs<=RESYNTH_PARAMETER_COUNT);
202 
203   /* Copy from read only to writeable. */
204   for (i=0; i<nargs; i++)
205   {
206     temp[i] = args[i];  /* Copy type and data. */
207     // g_printf("type %d value %d\n", args[i].type, args[i].data);
208   }
209 
210   /* Set runmode specially. */
211   temp[0].data.d_int32 = runmode;
212 
213   /* leave 1,2 the same: image, drawable. */
214 
215   /* Set the data. */
216   temp[3].data.d_int32 = param->v_tile;
217   temp[4].data.d_int32 = param->h_tile;
218   temp[5].data.d_int32 = param->use_border;
219   temp[6].data.d_int32 = param->corpus_id;
220   temp[7].data.d_int32 = param->input_map_id;
221   temp[8].data.d_int32 = param->output_map_id;
222   temp[9].data.d_float = param->map_weight;
223   temp[10].data.d_float = param->autism;
224   temp[11].data.d_int32 = param->neighbours;
225   temp[12].data.d_int32 = param->trys;
226 
227   /* Set the type of user enterable params to the PDB declared type. */
228   for (i=3; i<RESYNTH_PARAMETER_COUNT; i++)
229   {
230     temp[i].type = resynth_paramdefs[i].type;
231   }
232 }
233 
234 
235 
236 
237