-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharpEdge.c
524 lines (445 loc) · 12.6 KB
/
sharpEdge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
typedef struct
{
gint radius;
gint threshold;
gboolean preview;
} MyParaVals;
static void query(void);
static void run(const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void sharpEdge(GimpDrawable *drawable,
GimpPreview *preview);
static void init_mem(guchar ***row,
gint num_bytes);
static void process_row(guchar **row,
gint width,
gint height,
gint channels);
static void shuffle(GimpPixelRgn *rgn_in,
guchar **row,
gint x1,
gint y1,
gint width,
gint ypos);
static gboolean sharpEdge_dialog(GimpDrawable *drawable);
/* Set up default values for options */
static MyParaVals bvals =
{
4, /* radius */
20, /* threshold */
1 /* preview */
};
GimpPlugInInfo PLUG_IN_INFO =
{
NULL,
NULL,
query,
run
};
MAIN()
static void
query(void)
{
static GimpParamDef args[] =
{
{
GIMP_PDB_INT32,
"run-mode",
"Run mode"
},
{
GIMP_PDB_IMAGE,
"image",
"Input image"
},
{
GIMP_PDB_DRAWABLE,
"drawable",
"Input drawable"
},
{
GIMP_PDB_INT32,
"radius",
"Radius"
},
{
GIMP_PDB_INT32,
"threshold",
"Threshold"
}
};
gimp_install_procedure(
"plug-in-sharpEdge",
"Sharp Edge (preview)",
"Sharp the image Edge",
"Oberon Leung",
"Copyright Oberon Leung",
"Oct 2014",
"Sharp Edge (preview)",
"RGB*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS(args), 0,
args, NULL);
gimp_plugin_menu_register("plug-in-sharpEdge",
"<Image>/Filters/Enhance");
}
static void
run(const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
{
static GimpParam values[1];
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GimpRunMode run_mode;
GimpDrawable *drawable;
/* Setting mandatory output values */
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = status;
/* Getting run_mode - we won't display a dialog if
* we are in NONINTERACTIVE mode */
run_mode = param[0].data.d_int32;
/* Get the specified drawable */
drawable = gimp_drawable_get(param[2].data.d_drawable);
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
/* Get options last values if needed */
gimp_get_data("plug-in-sharpEdge", &bvals);
/* Display the dialog */
if (!sharpEdge_dialog(drawable))
return;
break;
case GIMP_RUN_NONINTERACTIVE:
if (nparams != 5)
status = GIMP_PDB_CALLING_ERROR;
if (status == GIMP_PDB_SUCCESS) {
bvals.radius = param[3].data.d_int32;
bvals.threshold = param[4].data.d_int32;
}
break;
case GIMP_RUN_WITH_LAST_VALS:
/* Get options last values if needed */
gimp_get_data("plug-in-sharpEdge", &bvals);
break;
default:
break;
}
sharpEdge(drawable, NULL);
gimp_displays_flush();
gimp_drawable_detach(drawable);
/* Finally, set options in the core */
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data("plug-in-sharpEdge", &bvals, sizeof(MyParaVals));
return;
}
static void
sharpEdge(GimpDrawable *drawable,
GimpPreview *preview)
{
gint i, ii, channels;
gint x1, y1, x2, y2;
GimpPixelRgn rgn_in, rgn_out;
guchar **row;
gint width, height;
if (!preview)
gimp_progress_init("Sharp Edge...");
/* Gets upper left and lower right coordinates,
* and layers number in the image */
if (preview)
{
gimp_preview_get_position(preview, &x1, &y1);
gimp_preview_get_size(preview, &width, &height);
x2 = x1 + width;
y2 = y1 + height;
}
else
{
gimp_drawable_mask_bounds(drawable->drawable_id,
&x1, &y1,
&x2, &y2);
width = x2 - x1;
height = y2 - y1;
}
channels = gimp_drawable_bpp(drawable->drawable_id);
/* Allocate a big enough tile cache */
gimp_tile_cache_ntiles(bvals.radius * (drawable->width / gimp_tile_width() + 1));
/* Initialises two PixelRgns, one to read original data,
* and the other to write output data. That second one will
* be merged at the end by the call to
* gimp_drawable_merge_shadow() */
gimp_pixel_rgn_init(&rgn_in,
drawable,
x1, y1,
width, height,
FALSE, FALSE);
gimp_pixel_rgn_init(&rgn_out,
drawable,
x1, y1,
width, height,
preview == NULL, TRUE);
/* Allocate memory for input and output tile rows */
init_mem(&row, width * channels);
for (ii = 0; ii < bvals.radius; ii++)
{
gimp_pixel_rgn_get_row(&rgn_in,
row[ii],
x1, y1 + ii,
width);
}
for (i = 0; i < height-bvals.radius+1; i++) {
/* To be done for each tile row */
process_row(row, width, height, channels);
gimp_pixel_rgn_set_row(&rgn_out,row[0], x1, y1 + i, width);
/* shift tile rows to insert the new one at the end */
shuffle(&rgn_in, row, x1, y1, width, i);
if (!preview && i % 16 == 0)
gimp_progress_update((gdouble)i / (gdouble)height);
}
for (ii=0; i < height; i++) {
gimp_pixel_rgn_set_row(&rgn_out,row[ii++],x1, y1 + i,width);
}
/* We could also put that in a separate function but it's
* rather simple */
for (ii = 0; ii < bvals.radius; ii++) {
g_free(row[ii]);
}
g_free(row);
/* Update the modified region */
if (preview) {
gimp_drawable_preview_draw_region(GIMP_DRAWABLE_PREVIEW(preview),&rgn_out);
}
else {
gimp_drawable_flush(drawable);
gimp_drawable_merge_shadow(drawable->drawable_id, TRUE);
gimp_drawable_update(drawable->drawable_id, x1, y1, width, height);
}
}
static void
init_mem(guchar ***row,
gint num_bytes)
{
gint i;
/* Allocate enough memory for row */
*row = g_new(guchar *, bvals.radius);
for (i = 0; i < bvals.radius; i++) {
(*row)[i] = g_new(guchar, num_bytes);
}
}
static void
process_row(guchar **row,
gint width,
gint height,
gint channels
)
{
gint j, pos = 0;
gint lastp;
lastp = bvals.radius - 1;
#if 1
for (j = 0; j < width- lastp; j++)
{
gint k;
for (k = 0; k < channels; k++)
{
gint dif, lfc, rgc;
gint sign;
dif = (0xff & row[0][pos + k]) - (0xff & row[lastp][pos + k]);
if (dif >= bvals.threshold || -dif >= bvals.threshold) {
gint lp, rp;
/* is mono direction */
for (lp = 1; lp < bvals.radius; ++lp) {
if (dif > 0 && (0xff & row[lp-1][pos + k]) < (0xff & row[lp][pos + k]))
break;
if (dif < 0 && (0xff & row[lp - 1][pos + k]) > (0xff & row[lp][pos + k]))
break;
}
if (lp >= bvals.radius) {
if (dif > 0) {
sign = 1;
}
else {
sign = -1;
}
lp = 1;
rp = bvals.radius - 1;
while (lp < rp-1) {
lfc = ((0xff & row[lp - 1][pos + k]) - (0xff & row[lp][pos + k])) * sign;
rgc = ((0xff & row[rp - 1][pos + k]) - (0xff & row[rp][pos + k])) * sign;
if (lfc >= rgc) {
row[rp - 1][pos + k] = (row[rp - 1][pos + k] & 0xff) - rgc * sign;
row[lp][pos + k] = (row[lp][pos + k] & 0xff) + rgc * sign;
--rp;
if (lfc == rgc)
++lp;
}
else {
row[lp][pos + k] = (row[lp][pos + k] & 0xff) + lfc * sign;
row[rp-1][pos + k] = (row[rp-1][pos + k] & 0xff) - lfc * sign;
++lp;
}
}
}
}
}
pos += channels;
}
#endif
#if 1
pos = 0;
for (j = 0; j < width-lastp; j++)
{
gint k;
for (k = 0; k < channels; k++)
{
gint dif, lfc, rgc;
gint sign;
dif = (0xff & row[0][pos + k]) - (0xff & row[0][pos + k+channels*3]);
if (dif >= bvals.threshold || -dif >= bvals.threshold) {
gint lp, rp;
/* is mono direction */
for (lp = 1; lp < bvals.radius; ++lp) {
if (dif > 0 && (0xff & row[0][pos + k+channels*(lp-1)]) < (0xff & row[0][pos + k+channels*lp]))
break;
if (dif < 0 && (0xff & row[0][pos + k+channels*(lp-1)]) >(0xff & row[0][pos + k + channels*lp]))
break;
}
if (lp >= bvals.radius) {
if (dif > 0) {
sign = 1;
}
else {
sign = -1;
}
lp = 1;
rp = bvals.radius - 1;
while (lp < rp - 1) {
lfc = ((0xff & row[0][pos + k+channels*(lp-1)]) - (0xff & row[0][pos + k+channels*(lp)])) * sign;
rgc = ((0xff & row[0][pos + k+channels*(rp-1)]) - (0xff & row[0][pos + k+channels*(rp)])) * sign;
if (lfc >= rgc) {
row[0][pos + k+channels*(lp)] = (row[0][pos + k+channels*(lp)] & 0xff) + rgc * sign;
row[0][pos + k+channels*(rp-1)] = (row[0][pos + k+channels*(rp-1)] & 0xff) - rgc * sign;
--rp;
}
else {
row[0][pos + k+channels*(lp)] = (row[0][pos + k+channels*lp] & 0xff) + lfc * sign;
row[0][pos + k+channels*(rp-1)] = (row[0][pos + k+channels*(rp-1)] & 0xff) - lfc * sign;
++lp;
}
}
}
}
}
pos += channels;
}
#endif
}
static void
shuffle(GimpPixelRgn *rgn_in,
guchar **row,
gint x1,
gint y1,
gint width,
gint ypos)
{
guchar *tmp_row1;
gint ir;
/* Get tile row (i + threshold + 1) into row[0] */
gimp_pixel_rgn_get_row(rgn_in,row[0],x1, ypos + y1 + bvals.radius,width);
tmp_row1 = row[0];
for (ir = 1; ir < bvals.radius; ++ir) {
row[ir-1] = row[ir];
}
row[bvals.radius-1] = tmp_row1;
}
static gboolean
sharpEdge_dialog(GimpDrawable *drawable)
{
GtkWidget *dialog;
GtkWidget *main_vbox;
GtkWidget *main_hbox;
GtkWidget *preview;
GtkWidget *frame;
GtkWidget *threshold_label;
GtkWidget *radius_label;
GtkWidget *alignment;
GtkWidget *spinbutton;
GtkObject *spinbutton1_adj;
GtkWidget *spinbutton1;
GtkObject *spinbutton_adj;
GtkWidget *frame_label;
gboolean run;
gimp_ui_init("Sharp Edge", FALSE);
dialog = gimp_dialog_new("Sharp Edge", "sharpEdge",
NULL, 0,
gimp_standard_help_func, "plug-in-sharpEdge",
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
NULL);
main_vbox = gtk_vbox_new(FALSE, 6);
gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), main_vbox);
gtk_widget_show(main_vbox);
preview = gimp_drawable_preview_new(drawable, &bvals.preview);
gtk_box_pack_start(GTK_BOX(main_vbox), preview, TRUE, TRUE, 0);
gtk_widget_show(preview);
frame = gtk_frame_new(NULL);
gtk_widget_show(frame);
gtk_box_pack_start(GTK_BOX(main_vbox), frame, TRUE, TRUE, 0);
gtk_container_set_border_width(GTK_CONTAINER(frame), 6);
alignment = gtk_alignment_new(0.5, 0.5, 1, 1);
gtk_widget_show(alignment);
gtk_container_add(GTK_CONTAINER(frame), alignment);
gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), 6, 6, 6, 6);
main_hbox = gtk_hbox_new(FALSE, 0);
gtk_widget_show(main_hbox);
gtk_container_add(GTK_CONTAINER(alignment), main_hbox);
/* radius */
radius_label = gtk_label_new_with_mnemonic("_radius:");
gtk_widget_show(radius_label);
gtk_box_pack_start(GTK_BOX(main_hbox), radius_label, FALSE, FALSE, 6);
gtk_label_set_justify(GTK_LABEL(radius_label), GTK_JUSTIFY_RIGHT);
/* spin button*/
spinbutton1 = gimp_spin_button_new(&spinbutton1_adj, bvals.radius,
4, 20, 1, 1, 1, 1, 0);
gtk_box_pack_start(GTK_BOX(main_hbox), spinbutton1, FALSE, FALSE, 0);
gtk_widget_show(spinbutton1);
/* threshold */
threshold_label = gtk_label_new_with_mnemonic("_threshold:");
gtk_widget_show(threshold_label);
gtk_box_pack_start(GTK_BOX(main_hbox), threshold_label, FALSE, FALSE, 6);
gtk_label_set_justify(GTK_LABEL(threshold_label), GTK_JUSTIFY_RIGHT);
/* spin button */
spinbutton = gimp_spin_button_new(&spinbutton_adj, bvals.threshold,
1, 128, 5, 1, 1, 5, 0);
gtk_box_pack_start(GTK_BOX(main_hbox), spinbutton, FALSE, FALSE, 0);
gtk_widget_show(spinbutton);
frame_label = gtk_label_new("<b>Modify parameters</b>");
gtk_widget_show(frame_label);
gtk_frame_set_label_widget(GTK_FRAME(frame), frame_label);
gtk_label_set_use_markup(GTK_LABEL(frame_label), TRUE);
g_signal_connect_swapped(preview, "invalidated",
G_CALLBACK(sharpEdge),
drawable);
g_signal_connect_swapped(spinbutton_adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
sharpEdge(drawable, GIMP_PREVIEW(preview));
g_signal_connect(spinbutton_adj, "value_changed",
G_CALLBACK(gimp_int_adjustment_update),
&bvals.threshold);
g_signal_connect(spinbutton1_adj, "value_changed",
G_CALLBACK(gimp_int_adjustment_update),
&bvals.radius);
gtk_widget_show(dialog);
run = (gimp_dialog_run(GIMP_DIALOG(dialog)) == GTK_RESPONSE_OK);
gtk_widget_destroy(dialog);
return run;
}