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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
/*
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This is a plug-in for GIMP.
*
* Blinds plug-in. Distort an image as though it was stuck to
* window blinds and the blinds where opened/closed.
*
* Copyright (C) 1997 Andy Thomas alt@picnic.demon.co.uk
*
* 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 <https://www.gnu.org/licenses/>.
*
* A fair proprotion of this code was taken from the Whirl plug-in
* which was copyrighted by Federico Mena Quintero (as below).
*
* Whirl plug-in --- distort an image into a whirlpool
* Copyright (C) 1997 Federico Mena Quintero
*
*/
#include "config.h"
#include <string.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "libgimp/stdplugins-intl.h"
/***** Magic numbers *****/
#define PLUG_IN_PROC "plug-in-blinds"
#define PLUG_IN_BINARY "blinds"
#define PLUG_IN_ROLE "gimp-blinds"
#define SCALE_WIDTH 150
#define MAX_FANS 100
/* Variables set in dialog box */
typedef struct data
{
gint angledsp;
gint numsegs;
GimpOrientationType orientation;
gboolean bg_trans;
} BlindVals;
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static gboolean blinds_dialog (gint32 drawable_id);
static void dialog_update_preview (gpointer drawable_id,
GimpPreview *preview);
static void apply_blinds (gint32 drawable_id);
/* Array to hold each size of fans. And no there are not each the
* same size (rounding errors...)
*/
static gint fanwidths[MAX_FANS];
/* Values when first invoked */
static BlindVals bvals =
{
30,
3,
GIMP_ORIENTATION_HORIZONTAL,
FALSE
};
const GimpPlugInInfo PLUG_IN_INFO =
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
};
MAIN ()
static void
query (void)
{
static const GimpParamDef args[] =
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
{ GIMP_PDB_INT32, "angle-dsp", "Angle of Displacement" },
{ GIMP_PDB_INT32, "num-segments", "Number of segments in blinds" },
{ GIMP_PDB_INT32, "orientation", "The orientation { ORIENTATION-HORIZONTAL (0), ORIENTATION-VERTICAL (1) }" },
{ GIMP_PDB_INT32, "bg-transparent", "Background transparent { FALSE, TRUE }" }
};
gimp_install_procedure (PLUG_IN_PROC,
N_("Simulate an image painted on window blinds"),
"More here later",
"Andy Thomas",
"Andy Thomas",
"1997",
N_("_Blinds..."),
"RGB*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
}
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
{
static GimpParam values[1];
gint32 drawable_id;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
INIT_I18N ();
gegl_init (NULL, NULL);
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = status;
run_mode = param[0].data.d_int32;
drawable_id = param[2].data.d_drawable;
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
gimp_get_data (PLUG_IN_PROC, &bvals);
if (! blinds_dialog (drawable_id))
return;
break;
case GIMP_RUN_NONINTERACTIVE:
if (nparams != 7)
status = GIMP_PDB_CALLING_ERROR;
if (status == GIMP_PDB_SUCCESS)
{
bvals.angledsp = param[3].data.d_int32;
bvals.numsegs = param[4].data.d_int32;
bvals.orientation = param[5].data.d_int32;
bvals.bg_trans = param[6].data.d_int32;
}
break;
case GIMP_RUN_WITH_LAST_VALS:
gimp_get_data (PLUG_IN_PROC, &bvals);
break;
default:
break;
}
if (gimp_drawable_is_rgb (drawable_id) ||
gimp_drawable_is_gray (drawable_id))
{
gimp_progress_init (_("Adding blinds"));
apply_blinds (drawable_id);
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data (PLUG_IN_PROC, &bvals, sizeof (BlindVals));
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
values[0].data.d_status = status;
}
static gboolean
blinds_dialog (gint32 drawable_id)
{
GtkWidget *dialog;
GtkWidget *main_vbox;
GtkWidget *preview;
GtkWidget *hbox;
GtkWidget *frame;
GtkWidget *table;
GtkObject *size_data;
GtkWidget *toggle;
GtkWidget *horizontal;
GtkWidget *vertical;
gboolean run;
gimp_ui_init (PLUG_IN_BINARY, TRUE);
dialog = gimp_dialog_new (_("Blinds"), PLUG_IN_ROLE,
NULL, 0,
gimp_standard_help_func, PLUG_IN_PROC,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_OK"), GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
gimp_window_set_transient (GTK_WINDOW (dialog));
main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
main_vbox, TRUE, TRUE, 0);
gtk_widget_show (main_vbox);
preview = gimp_aspect_preview_new_from_drawable_id (drawable_id);
gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
gtk_widget_show (preview);
g_signal_connect_swapped (preview, "invalidated",
G_CALLBACK (dialog_update_preview),
GINT_TO_POINTER (drawable_id));
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
gtk_widget_show (hbox);
frame =
gimp_int_radio_group_new (TRUE, _("Orientation"),
G_CALLBACK (gimp_radio_button_update),
&bvals.orientation, bvals.orientation,
_("_Horizontal"), GIMP_ORIENTATION_HORIZONTAL,
&horizontal,
_("_Vertical"), GIMP_ORIENTATION_VERTICAL,
&vertical,
NULL);
gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame);
g_signal_connect_swapped (horizontal, "toggled",
G_CALLBACK (gimp_preview_invalidate),
preview);
g_signal_connect_swapped (vertical, "toggled",
G_CALLBACK (gimp_preview_invalidate),
preview);
frame = gimp_frame_new (_("Background"));
gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame);
toggle = gtk_check_button_new_with_mnemonic (_("_Transparent"));
gtk_container_add (GTK_CONTAINER (frame), toggle);
gtk_widget_show (toggle);
g_signal_connect (toggle, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&bvals.bg_trans);
g_signal_connect_swapped (toggle, "toggled",
G_CALLBACK (gimp_preview_invalidate),
preview);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), bvals.bg_trans);
if (! gimp_drawable_has_alpha (drawable_id))
{
gtk_widget_set_sensitive (toggle, FALSE);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), FALSE);
}
table = gtk_table_new (2, 3, FALSE);
gtk_table_set_col_spacings (GTK_TABLE (table), 6);
gtk_table_set_row_spacings (GTK_TABLE (table), 2);
gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
gtk_widget_show (table);
size_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
_("_Displacement:"), SCALE_WIDTH, 0,
bvals.angledsp, 1, 90, 1, 15, 0,
TRUE, 0, 0,
NULL, NULL);
g_signal_connect (size_data, "value-changed",
G_CALLBACK (gimp_int_adjustment_update),
&bvals.angledsp);
g_signal_connect_swapped (size_data, "value-changed",
G_CALLBACK (gimp_preview_invalidate),
preview);
size_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
_("_Number of segments:"), SCALE_WIDTH, 0,
bvals.numsegs, 1, MAX_FANS, 1, 2, 0,
TRUE, 0, 0,
NULL, NULL);
g_signal_connect (size_data, "value-changed",
G_CALLBACK (gimp_int_adjustment_update),
&bvals.numsegs);
g_signal_connect_swapped (size_data, "value-changed",
G_CALLBACK (gimp_preview_invalidate),
preview);
gtk_widget_show (dialog);
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
gtk_widget_destroy (dialog);
return run;
}
static void
blindsapply (guchar *srow,
guchar *drow,
gint width,
gint bpp,
guchar *bg)
{
guchar *src;
guchar *dst;
gint i,j,k;
gdouble ang;
gint available;
/* Make the row 'shrink' around points along its length */
/* The bvals.numsegs determines how many segments to slip it in to */
/* The angle is the conceptual 'rotation' of each of these segments */
/* Note the row is considered to be made up of a two dim array actual
* pixel locations and the RGB color at these locations.
*/
/* In the process copy the src row to the destination row */
/* Fill in with background color ? */
for (i = 0 ; i < width ; i++)
{
dst = &drow[i*bpp];
for (j = 0 ; j < bpp; j++)
{
dst[j] = bg[j];
}
}
/* Apply it */
available = width;
for (i = 0; i < bvals.numsegs; i++)
{
/* Width of segs are variable */
fanwidths[i] = available / (bvals.numsegs - i);
available -= fanwidths[i];
}
/* do center points first - just for fun...*/
available = 0;
for (k = 1; k <= bvals.numsegs; k++)
{
int point;
point = available + fanwidths[k - 1] / 2;
available += fanwidths[k - 1];
src = &srow[point * bpp];
dst = &drow[point * bpp];
/* Copy pixels across */
for (j = 0 ; j < bpp; j++)
{
dst[j] = src[j];
}
}
/* Disp for each point */
ang = (bvals.angledsp * 2 * G_PI) / 360; /* Angle in rads */
ang = (1 - fabs (cos (ang)));
available = 0;
for (k = 0 ; k < bvals.numsegs; k++)
{
int dx; /* Amount to move by */
int fw;
for (i = 0 ; i < (fanwidths[k]/2) ; i++)
{
/* Copy pixels across of left half of fan */
fw = fanwidths[k] / 2;
dx = (int) (ang * ((double) (fw - (double)(i % fw))));
src = &srow[(available + i) * bpp];
dst = &drow[(available + i + dx) * bpp];
for (j = 0; j < bpp; j++)
{
dst[j] = src[j];
}
/* Right side */
j = i + 1;
src = &srow[(available + fanwidths[k] - j
- (fanwidths[k] % 2)) * bpp];
dst = &drow[(available + fanwidths[k] - j
- (fanwidths[k] % 2) - dx) * bpp];
for (j = 0; j < bpp; j++)
{
dst[j] = src[j];
}
}
available += fanwidths[k];
}
}
static void
dialog_update_preview (gpointer drawable_ptr,
GimpPreview *preview)
{
gint32 drawable_id = GPOINTER_TO_INT (drawable_ptr);
gint y;
guchar *p, *buffer, *cache;
GimpRGB background;
guchar bg[4];
gint width, height, bpp;
gimp_preview_get_size (preview, &width, &height);
cache = gimp_drawable_get_thumbnail_data (drawable_id,
&width, &height, &bpp);
p = cache;
gimp_context_get_background (&background);
if (bvals.bg_trans)
gimp_rgb_set_alpha (&background, 0.0);
if (gimp_drawable_is_gray (drawable_id))
{
bg[0] = gimp_rgb_luminance_uchar (&background);
gimp_rgba_get_uchar (&background, NULL, NULL, NULL, bg + 3);
}
else
{
gimp_rgba_get_uchar (&background, bg, bg + 1, bg + 2, bg + 3);
}
buffer = g_new (guchar, width * height * bpp);
if (bvals.orientation == GIMP_ORIENTATION_VERTICAL)
{
for (y = 0; y < height; y++)
{
blindsapply (p,
buffer + y * width * bpp,
width,
bpp, bg);
p += width * bpp;
}
}
else
{
/* Horizontal blinds */
/* Apply the blinds algo to a single column -
* this act as a transfomation matrix for the
* rows. Make row 0 invalid so we can find it again!
*/
gint i;
guchar *sr = g_new (guchar, height * 4);
guchar *dr = g_new0 (guchar, height * 4);
guchar dummybg[4] = {0, 0, 0, 0};
/* Fill in with background color ? */
for (i = 0 ; i < width ; i++)
{
gint j;
gint bd = bpp;
guchar *dst;
dst = &buffer[i * bd];
for (j = 0 ; j < bd; j++)
{
dst[j] = bg[j];
}
}
for ( y = 0 ; y < height; y++)
{
sr[y] = y+1;
}
/* Bit of a fiddle since blindsapply really works on an image
* row not a set of bytes. - preview can't be > 255
* or must make dr sr int rows.
*/
blindsapply (sr, dr, height, 1, dummybg);
for (y = 0; y < height; y++)
{
if (dr[y] == 0)
{
/* Draw background line */
p = buffer;
}
else
{
/* Draw line from src */
p = cache +
(width * bpp * (dr[y] - 1));
}
memcpy (buffer + y * width * bpp,
p,
width * bpp);
}
g_free (sr);
g_free (dr);
}
gimp_preview_draw_buffer (preview, buffer, width * bpp);
g_free (buffer);
g_free (cache);
}
/* STEP tells us how many rows/columns to gulp down in one go... */
/* Note all the "4" literals around here are to do with the depths
* of the images. Makes it easier to deal with for my small brain.
*/
#define STEP 40
static void
apply_blinds (gint32 drawable_id)
{
GeglBuffer *src_buffer;
GeglBuffer *dest_buffer;
const Babl *format;
guchar *src_rows, *des_rows;
gint bytes;
gint x, y;
GimpRGB background;
guchar bg[4];
gint sel_x1, sel_y1;
gint sel_width, sel_height;
gimp_context_get_background (&background);
if (bvals.bg_trans)
gimp_rgb_set_alpha (&background, 0.0);
gimp_rgba_get_uchar (&background, bg, bg + 1, bg + 2, bg + 3);
if (! gimp_drawable_mask_intersect (drawable_id,
&sel_x1, &sel_y1,
&sel_width, &sel_height))
return;
if (gimp_drawable_has_alpha (drawable_id))
format = babl_format ("R'G'B'A u8");
else
format = babl_format ("R'G'B' u8");
bytes = babl_format_get_bytes_per_pixel (format);
src_buffer = gimp_drawable_get_buffer (drawable_id);
dest_buffer = gimp_drawable_get_shadow_buffer (drawable_id);
src_rows = g_new (guchar, MAX (sel_width, sel_height) * bytes * STEP);
des_rows = g_new (guchar, MAX (sel_width, sel_height) * bytes * STEP);
if (bvals.orientation == GIMP_ORIENTATION_VERTICAL)
{
for (y = 0; y < sel_height; y += STEP)
{
gint rr;
gint step;
if ((y + STEP) > sel_height)
step = sel_height - y;
else
step = STEP;
gegl_buffer_get (src_buffer,
GEGL_RECTANGLE (sel_x1, sel_y1 + y,
sel_width, step), 1.0,
format, src_rows,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
/* OK I could make this better */
for (rr = 0; rr < STEP; rr++)
blindsapply (src_rows + (sel_width * rr * bytes),
des_rows + (sel_width * rr * bytes),
sel_width, bytes, bg);
gegl_buffer_set (dest_buffer,
GEGL_RECTANGLE (sel_x1, sel_y1 + y,
sel_width, step), 0,
format, des_rows,
GEGL_AUTO_ROWSTRIDE);
gimp_progress_update ((double) y / (double) sel_height);
}
}
else
{
/* Horizontal blinds */
/* Apply the blinds algo to a single column -
* this act as a transfomation matrix for the
* rows. Make row 0 invalid so we can find it again!
*/
gint i;
gint *sr = g_new (gint, sel_height * bytes);
gint *dr = g_new (gint, sel_height * bytes);
guchar *dst = g_new (guchar, STEP * bytes);
guchar dummybg[4];
memset (dummybg, 0, 4);
memset (dr, 0, sel_height * bytes); /* all dr rows are background rows */
for (y = 0; y < sel_height; y++)
{
sr[y] = y+1;
}
/* Hmmm. does this work portably? */
/* This "swaps" the integers around that are held in in the
* sr & dr arrays.
*/
blindsapply ((guchar *) sr, (guchar *) dr,
sel_height, sizeof (gint), dummybg);
/* Fill in with background color ? */
for (i = 0 ; i < STEP ; i++)
{
int j;
guchar *bgdst;
bgdst = &dst[i * bytes];
for (j = 0 ; j < bytes; j++)
{
bgdst[j] = bg[j];
}
}
for (x = 0; x < sel_width; x += STEP)
{
int rr;
int step;
guchar *p;
if((x + STEP) > sel_width)
step = sel_width - x;
else
step = STEP;
gegl_buffer_get (src_buffer,
GEGL_RECTANGLE (sel_x1 + x, sel_y1,
step, sel_height), 1.0,
format, src_rows,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
/* OK I could make this better */
for (rr = 0; rr < sel_height; rr++)
{
if(dr[rr] == 0)
{
/* Draw background line */
p = dst;
}
else
{
/* Draw line from src */
p = src_rows + (step * bytes * (dr[rr] - 1));
}
memcpy (des_rows + (rr * step * bytes), p,
step * bytes);
}
gegl_buffer_set (dest_buffer,
GEGL_RECTANGLE (sel_x1 + x, sel_y1,
step, sel_height), 0,
format, des_rows,
GEGL_AUTO_ROWSTRIDE);
gimp_progress_update ((double) x / (double) sel_width);
}
g_free (dst);
g_free (sr);
g_free (dr);
}
g_free (src_rows);
g_free (des_rows);
g_object_unref (src_buffer);
g_object_unref (dest_buffer);
gimp_progress_update (1.0);
gimp_drawable_merge_shadow (drawable_id, TRUE);
gimp_drawable_update (drawable_id,
sel_x1, sel_y1, sel_width, sel_height);
}
|