summaryrefslogtreecommitdiffstats
path: root/hacks/distort.c
blob: a3586cfa0ccd3dad57a45d39dff7f1ee7957a762 (plain) (blame)
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/* -*- mode: C; tab-width: 4 -*-
 * xscreensaver, Copyright (c) 1992-2018 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

/* distort
 * by Jonas Munsin (jmunsin@iki.fi) and Jamie Zawinski <jwz@jwz.org>
 * TODO:
 *	-check the allocations in init_round_lense again, maybe make it possible again
 * 	 to use swamp without pre-allocating/calculating (although that
 *	 makes it slower) - -swamp is memory hungry
 *	-more distortion matrices (fortunately, I'm out of ideas :)
 * Stuff that would be cool but probably too much of a resource hog:
 *	-some kind of interpolation to avoid jaggies
 *    -large speed values leaves the image distorted
 * program idea borrowed from a screensaver on a non-*NIX OS,
 *
 * 28 Sep 1999 Jonas Munsin (jmunsin@iki.fi)
 *    Added about 10x faster algortim for 8, 16 and 32 bpp (modifies pixels
 *    directly avoiding costly XPutPixle(XGetPixel()) calls, inspired by
 *    xwhirl made by horvai@clipper.ens.fr (Peter Horvai) and the XFree86
 *    Xlib sources.
 *    This piece of code is really horrible, but it works, and at the moment
 *    I don't have time or inspiration to fix something that works (knock
 *    on wood).
 * 08 Oct 1999 Jonas Munsin (jmunsin@iki.fi)
 *	Corrected several bugs causing references beyond allocated memory.
 * 09 Oct 2016 Dave Odell (dmo2118@gmail.com)
 *  Updated for new xshm.c.
 */

#include <math.h>
#include <time.h>
#include "screenhack.h"
/*#include <X11/Xmd.h>*/
# include "xshm.h"

#define CARD32 unsigned int
#define CARD16 unsigned short
#define CARD8  unsigned char


struct coo {
	int x;
	int y;
	int r, r_change;
	int xmove, ymove;
};

struct state {
  Display *dpy;
  Window window;

  struct coo xy_coo[10];

  int delay, radius, speed, number, blackhole, vortex, magnify, reflect, slow;
  int duration;
  time_t start_time;

  XWindowAttributes xgwa;
  GC gc;
  unsigned long black_pixel;

  XImage *orig_map, *buffer_map;
  unsigned long *buffer_map_cache;

  int ***from;
  int ****from_array;
  int *fast_from;
  int from_size;

  int bpp_size;

  XShmSegmentInfo shm_info;

  void (*effect) (struct state *, int);
  void (*draw) (struct state *, int);
  void (*draw_routine) (struct state *st, XImage *, XImage *, int, int, int *);

  async_load_state *img_loader;
  Pixmap pm;
};


static void move_lense(struct state *, int);
static void swamp_thing(struct state *, int);
static void new_rnd_coo(struct state *, int);
static void init_round_lense(struct state *st);
static void reflect_draw(struct state *, int);
static void plain_draw(struct state *, int);

static void fast_draw_8 (struct state *st, XImage *, XImage *, int, int, int *);
static void fast_draw_16(struct state *st, XImage *, XImage *, int, int, int *);
static void fast_draw_32(struct state *st, XImage *, XImage *, int, int, int *);
static void generic_draw(struct state *st, XImage *, XImage *, int, int, int *);


static void distort_finish_loading (struct state *);

static void
distort_reset (struct state *st)
{
    char *s;
    int i;

    st->start_time = 0;

	XGetWindowAttributes (st->dpy, st->window, &st->xgwa);

	st->delay = get_integer_resource(st->dpy, "delay", "Integer");
    st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
	st->radius = get_integer_resource(st->dpy, "radius", "Integer");
	st->speed = get_integer_resource(st->dpy, "speed", "Integer");
	st->number = get_integer_resource(st->dpy, "number", "Integer");

	st->blackhole = get_boolean_resource(st->dpy, "blackhole", "Boolean");
	st->vortex = get_boolean_resource(st->dpy, "vortex", "Boolean");
	st->magnify = get_boolean_resource(st->dpy, "magnify", "Boolean");
	st->reflect = get_boolean_resource(st->dpy, "reflect", "Boolean");
	st->slow = get_boolean_resource(st->dpy, "slow", "Boolean");
	
    if (st->xgwa.width > 2560) st->radius *= 3;  /* Retina displays */

    if (st->delay < 0) st->delay = 0;
    if (st->duration < 1) st->duration = 1;

    st->effect = NULL;
    s = get_string_resource(st->dpy, "effect", "String");
	if (s && !strcasecmp(s,"swamp"))
      st->effect = &swamp_thing;
    else if (s && !strcasecmp(s,"bounce"))
      st->effect = &move_lense;
    else if (s && !strcasecmp(s,"none"))
      ;
    else if (s && *s)
      fprintf(stderr,"%s: bogus effect: %s\n", progname, s);
    if (s) free (s);

	if (st->effect == NULL && st->radius == 0 && st->speed == 0 && st->number == 0
		&& !st->blackhole && !st->vortex && !st->magnify && !st->reflect) {
/* if no cmdline options are given, randomly choose one of:
 * -radius 125 -number 4 -speed 1 -bounce
 * -radius 125 -number 4 -speed 1 -blackhole
 * -radius 125 -number 4 -speed 1 -vortex
 * -radius 125 -number 4 -speed 1 -vortex -magnify
 * -radius 125 -number 4 -speed 1 -vortex -magnify -blackhole
 * -radius 250 -number 1 -speed 2 -bounce
 * -radius 250 -number 1 -speed 2 -blackhole
 * -radius 250 -number 1 -speed 2 -vortex
 * -radius 250 -number 1 -speed 2 -vortex -magnify
 * -radius 250 -number 1 -speed 2 -vortex -magnify -blackhole
 * -radius 80 -number 1 -speed 2 -reflect
 * -radius 125 -number 3 -speed 2 -reflect
 * jwz: not these
 *   -radius 125 -number 4 -speed 2 -swamp
 *   -radius 125 -number 4 -speed 2 -swamp -blackhole
 *   -radius 125 -number 4 -speed 2 -swamp -vortex
 *   -radius 125 -number 4 -speed 2 -swamp -vortex -magnify
 *   -radius 125 -number 4 -speed 2 -swamp -vortex -magnify -blackhole
 */
		
		i = (random() % 12 /* 17 */);

		st->draw = &plain_draw;

		switch (i) {
			case 0:
				st->radius=125;st->number=4;st->speed=1;
				st->effect=&move_lense;break;
			case 1:
				st->radius=125;st->number=4;st->speed=1;st->blackhole=1;
				st->effect=&move_lense;break;
			case 2:
				st->radius=125;st->number=4;st->speed=1;st->vortex=1;
				st->effect=&move_lense;break;
			case 3:
				st->radius=125;st->number=4;st->speed=1;st->vortex=1;st->magnify=1;
				st->effect=&move_lense;break;
			case 4:
				st->radius=125;st->number=4;st->speed=1;st->vortex=1;st->magnify=1;st->blackhole=1;
				st->effect=&move_lense;break;
			case 5:
				st->radius=250;st->number=1;st->speed=2;
				st->effect=&move_lense;break;
			case 6:
				st->radius=250;st->number=1;st->speed=2;st->blackhole=1;
				st->effect=&move_lense;break;
			case 7:
				st->radius=250;st->number=1;st->speed=2;st->vortex=1;
				st->effect=&move_lense;break;
			case 8:
				st->radius=250;st->number=1;st->speed=2;st->vortex=1;st->magnify=1;
				st->effect=&move_lense;break;
			case 9:
				st->radius=250;st->number=1;st->speed=2;st->vortex=1;st->magnify=1;st->blackhole=1;
				st->effect=&move_lense;break;

			case 10:
				st->radius=80;st->number=1;st->speed=2;st->reflect=1;
				st->draw = &reflect_draw;st->effect = &move_lense;break;
			case 11:
				st->radius=125;st->number=4;st->speed=2;st->reflect=1;
				st->draw = &reflect_draw;st->effect = &move_lense;break;

#if 0 /* jwz: not these */
			case 12:
				st->radius=125;st->number=4;st->speed=2;
				effect=&swamp_thing;break;
			case 13:
				st->radius=125;st->number=4;st->speed=2;st->blackhole=1;
				effect=&swamp_thing;break;
			case 14:
				st->radius=125;st->number=4;st->speed=2;st->vortex=1;
				effect=&swamp_thing;break;
			case 15:
				st->radius=125;st->number=4;st->speed=2;st->vortex=1;st->magnify=1;
				effect=&swamp_thing;break;
			case 16:
				st->radius=125;st->number=4;st->speed=2;st->vortex=1;st->magnify=1;st->blackhole=1;
				effect=&swamp_thing;break;
#endif

            default:
                abort(); break;
		}
	}

    /* never allow the radius to be too close to the min window dimension
     */
    if (st->radius > st->xgwa.width  * 0.3) st->radius = st->xgwa.width  * 0.3;
    if (st->radius > st->xgwa.height * 0.3) st->radius = st->xgwa.height * 0.3;


    /* -swamp mode consumes vast amounts of memory, proportional to radius --
       so throttle radius to a small-ish value (60 => ~30MB.)
     */
    if (st->effect == &swamp_thing && st->radius > 60)
      st->radius = 60;

	if (st->delay < 0)
		st->delay = 0;
	if (st->radius <= 0)
		st->radius = 60;
	if (st->speed <= 0) 
		st->speed = 2;
	if (st->number <= 0)
		st->number=1;
	if (st->number >= 10)
		st->number=1;
	if (st->effect == NULL)
		st->effect = &move_lense;
	if (st->reflect) {
		st->draw = &reflect_draw;
		st->effect = &move_lense;
	}
	if (st->draw == NULL)
		st->draw = &plain_draw;
}

static void *
distort_init (Display *dpy, Window window)
{
  struct state *st = (struct state *) calloc (1, sizeof(*st));
	XGCValues gcv;
	long gcflags;

    st->dpy = dpy;
    st->window = window;

    distort_reset (st);

	st->black_pixel = BlackPixelOfScreen( st->xgwa.screen );

	gcv.function = GXcopy;
	gcv.subwindow_mode = IncludeInferiors;
	gcflags = GCFunction;
	if (use_subwindow_mode_p(st->xgwa.screen, st->window)) /* see grabscreen.c */
		gcflags |= GCSubwindowMode;
	st->gc = XCreateGC (st->dpy, st->window, gcflags, &gcv);

    /* On MacOS X11, XGetImage on a Window often gets an inexplicable BadMatch,
       possibly due to the window manager having occluded something?  It seems
       nondeterministic. Loading the image into a pixmap instead fixes it. */
    if (st->pm) XFreePixmap (st->dpy, st->pm);
    st->pm = XCreatePixmap (st->dpy, st->window,
                            st->xgwa.width, st->xgwa.height, st->xgwa.depth);

    st->img_loader = load_image_async_simple (0, st->xgwa.screen, st->window,
                                              st->pm, 0, 0);
    st->start_time = time ((time_t *) 0);
    return st;
}

static void
distort_finish_loading (struct state *st)
{
    int i;

    st->start_time = time ((time_t *) 0);

    if (! st->pm) abort();
    XClearWindow (st->dpy, st->window);
    XCopyArea (st->dpy, st->pm, st->window, st->gc, 
               0, 0, st->xgwa.width, st->xgwa.height, 0, 0);
    if (st->orig_map) XDestroyImage (st->orig_map);
	st->orig_map = XGetImage(st->dpy, st->pm, 0, 0,
                             st->xgwa.width, st->xgwa.height,
                             ~0L, ZPixmap);
    if (st->buffer_map_cache) free (st->buffer_map_cache);
	st->buffer_map_cache = malloc(sizeof(unsigned long)*(2*st->radius+st->speed+2)*(2*st->radius+st->speed+2));

	if (st->buffer_map_cache == NULL) {
		perror("distort");
		exit(EXIT_FAILURE);
	}

    if (st->buffer_map)
      destroy_xshm_image (st->dpy, st->buffer_map, &st->shm_info);
	st->buffer_map = create_xshm_image(st->dpy, st->xgwa.visual, st->orig_map->depth,
	                                   ZPixmap, &st->shm_info,
	                                   2*st->radius + st->speed + 2,
	                                   2*st->radius + st->speed + 2);

	if ((st->buffer_map->byte_order == st->orig_map->byte_order)
			&& (st->buffer_map->depth == st->orig_map->depth)
			&& (st->buffer_map->format == ZPixmap)
			&& (st->orig_map->format == ZPixmap)
			&& !st->slow) {
		switch (st->orig_map->bits_per_pixel) {
			case 32:
				st->draw_routine = &fast_draw_32;
				st->bpp_size = sizeof(CARD32);
				break;
			case 16:
				st->draw_routine = &fast_draw_16;
				st->bpp_size = sizeof(CARD16);
				break;
			case 8:
				st->draw_routine = &fast_draw_8;
				st->bpp_size = sizeof(CARD8);
				break;
			default:
				st->draw_routine = &generic_draw;
				break;
		}
	} else {
		st->draw_routine = &generic_draw;
	}
	init_round_lense(st);

	for (i = 0; i < st->number; i++) {
		new_rnd_coo(st,i);
		if (st->number != 1)
			st->xy_coo[i].r = (i*st->radius)/(st->number-1); /* "randomize" initial */
		else
			 st->xy_coo[i].r = 0;
		st->xy_coo[i].r_change = st->speed + (i%2)*2*(-st->speed);	/* values a bit */
		st->xy_coo[i].xmove = st->speed + (i%2)*2*(-st->speed);
		st->xy_coo[i].ymove = st->speed + (i%2)*2*(-st->speed);
	}
}

/* example: initializes a "see-trough" matrix */
/* static void make_null_lense(struct state *st)
{
	int i, j;
	for (i = 0; i < 2*radius+speed+2; i++) {
		for (j = 0 ; j < 2*radius+speed+2 ; j++) {
			from[i][j][0]=i;
			from[i][j][1]=j;
		}
	} 
}
*/
static void convert(struct state *st) 
{
	int *p;
	int i, j;
    if (st->fast_from) free (st->fast_from);
	st->fast_from = calloc(1, sizeof(int)*((st->buffer_map->bytes_per_line/st->bpp_size)*(2*st->radius+st->speed+2) + 2*st->radius+st->speed+2));
	if (st->fast_from == NULL) {
		perror("distort");
		exit(EXIT_FAILURE);
	}
	p = st->fast_from;
	for (i = 0; i < 2*st->radius+st->speed+2; i++) {
		for (j = 0; j < 2*st->radius+st->speed+2; j++) {
			*(p + i + j*st->buffer_map->bytes_per_line/st->bpp_size)
				= st->from[i][j][0] + st->xgwa.width*st->from[i][j][1];
			if (*(p + i + j*st->buffer_map->bytes_per_line/st->bpp_size) < 0
					|| *(p + i + j*st->buffer_map->bytes_per_line/st->bpp_size) >= st->orig_map->height*st->orig_map->width) {
				*(p + i + j*st->buffer_map->bytes_per_line/st->bpp_size) = 0;
			}
		}
	}
}

/* makes a lense with the Radius=loop and centred in
 * the point (radius, radius)
 */
static void make_round_lense(struct state *st, int radius, int loop)
{
	int i, j;

	for (i = 0; i < 2*radius+st->speed+2; i++) {
		for(j = 0; j < ((0 == st->bpp_size) ? (2*radius+st->speed+2) : (st->buffer_map->bytes_per_line/st->bpp_size)); j++) {
			double r, d;
			r = sqrt ((i-radius)*(i-radius)+(j-radius)*(j-radius));
			if (loop == 0)
			  d=0.0;
			else
			  d=r/loop;

			if (r < loop-1) {

				if (st->vortex) { /* vortex-twist effect */
					double angle;
		/* this one-line formula for getting a nice rotation angle is borrowed
		 * (with permission) from the whirl plugin for gimp,
		 * Copyright (C) 1996 Federico Mena Quintero
		 */
		/* 5 is just a constant used because it looks good :) */
					angle = 5*(1-d)*(1-d);

        /* Avoid atan2: DOMAIN error message */
					if ((radius-j) == 0.0 && (radius-i) == 0.0) {
						st->from[i][j][0] = radius + cos(angle)*r;
						st->from[i][j][1] = radius + sin(angle)*r;
					} else {
						st->from[i][j][0] = radius +
							cos(angle - atan2(radius-j, -(radius-i)))*r;
						st->from[i][j][1] = radius +
							sin(angle - atan2(radius-j, -(radius-i)))*r;
					}
					if (st->magnify) {
						r = sin(d*M_PI_2);
						if (st->blackhole && r != 0) /* blackhole effect */
							r = 1/r;
						st->from[i][j][0] = radius + (st->from[i][j][0]-radius)*r;
						st->from[i][j][1] = radius + (st->from[i][j][1]-radius)*r;
					}
				} else { /* default is to magnify */
					r = sin(d*M_PI_2);
				
	/* raising r to different power here gives different amounts of
	 * distortion, a negative value sucks everything into a black hole
	 */
				/*	r = r*r; */
					if (st->blackhole && r != 0) /* blackhole effect */
						r = 1/r;
									/* bubble effect (and blackhole) */
					st->from[i][j][0] = radius + (i-radius)*r;
					st->from[i][j][1] = radius + (j-radius)*r;
				}
			} else { /* not inside loop */
				st->from[i][j][0] = i;
				st->from[i][j][1] = j;
			}
		}
	}

	/* this is really just a quick hack to keep both the compability mode with all depths and still
	 * allow the custom optimized draw routines with the minimum amount of work */
	if (0 != st->bpp_size) {
		convert(st);
	}
}

#ifndef EXIT_FAILURE
# define EXIT_FAILURE -1
#endif

static void allocate_lense(struct state *st)
{
	int i, j;
	int s = ((0 != st->bpp_size) ? (st->buffer_map->bytes_per_line/st->bpp_size) : (2*st->radius+st->speed+2));
	/* maybe this should be redone so that from[][][] is in one block;
	 * then pointers could be used instead of arrays in some places (and
	 * maybe give a speedup - maybe also consume less memory)
	 */
    if (st->from) {
      for (i = 0; i < st->from_size; i++)
		if (st->from[i]) {
          for (j = 0; j < st->from_size; j++) {
            if (st->from[i][j]) free (st->from[i][j]);
          }
          free (st->from[i]);
        }
      free (st->from);
    }
    st->from_size = s;
	st->from = (int ***)malloc(s*sizeof(int **));
	if (st->from == NULL) {
		perror("distort");
		exit(EXIT_FAILURE);
	}
	for (i = 0; i < s; i++) {
		st->from[i] = (int **)malloc((2*st->radius+st->speed+2) * sizeof(int *));
		if (st->from[i] == NULL) {
			perror("distort");
			exit(EXIT_FAILURE);
		}
		for (j = 0; j < s; j++) {
			st->from[i][j] = (int *)malloc(2 * sizeof(int));
			if (st->from[i][j] == NULL) {
				perror("distort");
				exit(EXIT_FAILURE);
			}
		}
	}
}

/* from_array in an array containing precalculated from matrices,
 * this is a double faced mem vs speed trade, it's faster, but eats
 * _a lot_ of mem for large radius (is there a bug here? I can't see it)
 */
static void init_round_lense(struct state *st)
{
	int k;

	if (st->effect == &swamp_thing) {
        if (st->from_array) free (st->from_array);
		st->from_array = (int ****)malloc((st->radius+1)*sizeof(int ***));
		for (k=0; k <= st->radius; k++) {
			allocate_lense(st);
			make_round_lense(st, st->radius, k);
			st->from_array[k] = st->from;
		}
	} else { /* just allocate one from[][][] */
		allocate_lense(st);
		make_round_lense(st, st->radius,st->radius);
	}
}

/* If fast_draw_8, fast_draw_16 or fast_draw_32 are to be used, the following properties
 * of the src and dest XImages must hold (otherwise the generic, slooow, method provided
 * by X is to be used):
 *	src->byte_order == dest->byte_order
 *	src->format == ZPixmap && dest->format == ZPixmap
 *	src->depth == dest->depth == the depth the function in question asumes
 * x and y is the coordinates in src from where to cut out the image from,
 * distort_matrix is a precalculated array of how to distort the matrix
 */

static void fast_draw_8(struct state *st, XImage *src, XImage *dest, int x, int y, int *distort_matrix)
{
	CARD8 *u = (CARD8 *)dest->data;
	CARD8 *t = (CARD8 *)src->data + x + y*src->bytes_per_line/sizeof(CARD8);

	while (u < (CARD8 *)(dest->data + sizeof(CARD8)*dest->height
				*dest->bytes_per_line/sizeof(CARD8))) {
		*u++ = t[*distort_matrix++];
	}
}

static void fast_draw_16(struct state *st, XImage *src, XImage *dest, int x, int y, int *distort_matrix)
{
	CARD16 *u = (CARD16 *)dest->data;
	CARD16 *t = (CARD16 *)src->data + x + y*src->bytes_per_line/sizeof(CARD16);

	while (u < (CARD16 *)(dest->data + sizeof(CARD16)*dest->height
				*dest->bytes_per_line/sizeof(CARD16))) {
		*u++ = t[*distort_matrix++];
	}
}

static void fast_draw_32(struct state *st, XImage *src, XImage *dest, int x, int y, int *distort_matrix)
{
	CARD32 *u = (CARD32 *)dest->data;
	CARD32 *t = (CARD32 *)src->data + x + y*src->bytes_per_line/sizeof(CARD32);

	while (u < (CARD32 *)(dest->data + sizeof(CARD32)*dest->height
				*dest->bytes_per_line/sizeof(CARD32))) {
		*u++ = t[*distort_matrix++];
	}
}

static void generic_draw(struct state *st, XImage *src, XImage *dest, int x, int y, int *distort_matrix)
{
	int i, j;
	for (i = 0; i < dest->width; i++)
		for (j = 0; j < dest->height; j++)
			if (st->from[i][j][0] + x >= 0 &&
					st->from[i][j][0] + x < src->width &&
					st->from[i][j][1] + y >= 0 &&
					st->from[i][j][1] + y < src->height)
				XPutPixel(dest, i, j,
						XGetPixel(src,
							st->from[i][j][0] + x,
							st->from[i][j][1] + y));
}

/* generate an XImage of from[][][] and draw it on the screen */
static void plain_draw(struct state *st, int k)
{
	if (st->xy_coo[k].x+2*st->radius+st->speed+2 > st->orig_map->width ||
			st->xy_coo[k].y+2*st->radius+st->speed+2 > st->orig_map->height)
		return;

	st->draw_routine(st, st->orig_map, st->buffer_map, st->xy_coo[k].x, st->xy_coo[k].y, st->fast_from);

	put_xshm_image(st->dpy, st->window, st->gc, st->buffer_map, 0, 0, st->xy_coo[k].x, st->xy_coo[k].y,
	               2*st->radius+st->speed+2, 2*st->radius+st->speed+2, &st->shm_info);
}


/* generate an XImage from the reflect algoritm submitted by
 * Randy Zack <randy@acucorp.com>
 * draw really got too big and ugly so I split it up
 * it should be possible to use the from[][] to speed it up
 * (once I figure out the algorithm used :)
 */
static void reflect_draw(struct state *st, int k)
{
	int i, j;
	int	cx, cy;
	int	ly, lysq, lx, ny, dist, rsq = st->radius * st->radius;

	cx = cy = st->radius;
	if (st->xy_coo[k].ymove > 0)
		cy += st->speed;
	if (st->xy_coo[k].xmove > 0)
		cx += st->speed;

	for(i = 0 ; i < 2*st->radius+st->speed+2; i++) {
		ly = i - cy;
		lysq = ly * ly;
		ny = st->xy_coo[k].y + i;
		if (ny >= st->orig_map->height) ny = st->orig_map->height-1;
		for(j = 0 ; j < 2*st->radius+st->speed+2 ; j++) {
			lx = j - cx;
			dist = lx * lx + lysq;
			if (dist > rsq ||
				ly < -st->radius || ly > st->radius ||
				lx < -st->radius || lx > st->radius)
				XPutPixel( st->buffer_map, j, i,
						   XGetPixel( st->orig_map, st->xy_coo[k].x + j, ny ));
			else if (dist == 0)
				XPutPixel( st->buffer_map, j, i, st->black_pixel );
			else {
				int	x = st->xy_coo[k].x + cx + (lx * rsq / dist);
				int	y = st->xy_coo[k].y + cy + (ly * rsq / dist);
				if (x < 0 || x >= st->xgwa.width ||
					y < 0 || y >= st->xgwa.height)
					XPutPixel( st->buffer_map, j, i, st->black_pixel );
				else
					XPutPixel( st->buffer_map, j, i,
							   XGetPixel( st->orig_map, x, y ));
			}
		}
	}

	XPutImage(st->dpy, st->window, st->gc, st->buffer_map, 0, 0, st->xy_coo[k].x, st->xy_coo[k].y,
			2*st->radius+st->speed+2, 2*st->radius+st->speed+2);
}

/* create a new, random coordinate, that won't interfer with any other
 * coordinates, as the drawing routines would be significantly slowed
 * down if they were to handle serveral layers of distortions
 */
static void new_rnd_coo(struct state *st, int k)
{
	int i;
    int loop = 0;

	st->xy_coo[k].x = (random() % (st->xgwa.width-2*st->radius));
	st->xy_coo[k].y = (random() % (st->xgwa.height-2*st->radius));
	
	for (i = 0; i < st->number; i++) {
		if (i != k) {
			if ((abs(st->xy_coo[k].x - st->xy_coo[i].x) <= 2*st->radius+st->speed+2)
			 && (abs(st->xy_coo[k].y - st->xy_coo[i].y) <= 2*st->radius+st->speed+2)) {
				st->xy_coo[k].x = (random() % (st->xgwa.width-2*st->radius));
				st->xy_coo[k].y = (random() % (st->xgwa.height-2*st->radius));
				i=-1; /* ugly */
			} 
		}
        if (loop++ > 1000) return;  /* let's not get stuck */
	}
}

/* move lens and handle bounces with walls and other lenses */
static void move_lense(struct state *st, int k)
{
	int i;

	if (st->xy_coo[k].x + 2*st->radius + st->speed + 2 >= st->xgwa.width)
		st->xy_coo[k].xmove = -abs(st->xy_coo[k].xmove);
	if (st->xy_coo[k].x <= st->speed) 
		st->xy_coo[k].xmove = abs(st->xy_coo[k].xmove);
	if (st->xy_coo[k].y + 2*st->radius + st->speed + 2 >= st->xgwa.height)
		st->xy_coo[k].ymove = -abs(st->xy_coo[k].ymove);
	if (st->xy_coo[k].y <= st->speed)
		st->xy_coo[k].ymove = abs(st->xy_coo[k].ymove);

	st->xy_coo[k].x = st->xy_coo[k].x + st->xy_coo[k].xmove;
	st->xy_coo[k].y = st->xy_coo[k].y + st->xy_coo[k].ymove;

	/* bounce against othe lenses */
	for (i = 0; i < st->number; i++) {
		if ((i != k)
		
/* This commented test is for rectangular lenses (not currently used) and
 * the one used is for circular ones
		&& (abs(xy_coo[k].x - xy_coo[i].x) <= 2*radius)
		&& (abs(xy_coo[k].y - xy_coo[i].y) <= 2*radius)) { */

		&& ((st->xy_coo[k].x - st->xy_coo[i].x)*(st->xy_coo[k].x - st->xy_coo[i].x)
		  + (st->xy_coo[k].y - st->xy_coo[i].y)*(st->xy_coo[k].y - st->xy_coo[i].y)
			<= 2*st->radius*2*st->radius)) {

			int x, y;
			x = st->xy_coo[k].xmove;
			y = st->xy_coo[k].ymove;
			st->xy_coo[k].xmove = st->xy_coo[i].xmove;
			st->xy_coo[k].ymove = st->xy_coo[i].ymove;
			st->xy_coo[i].xmove = x;
			st->xy_coo[i].ymove = y;
		}
	}

}

/* make xy_coo[k] grow/shrink */
static void swamp_thing(struct state *st, int k)
{
	if (st->xy_coo[k].r >= st->radius)
		st->xy_coo[k].r_change = -abs(st->xy_coo[k].r_change);
	
	if (st->xy_coo[k].r <= 0) {
		st->from = st->from_array[0];
		st->draw(st,k); 
		st->xy_coo[k].r_change = abs(st->xy_coo[k].r_change);
		new_rnd_coo(st,k);
		st->xy_coo[k].r=st->xy_coo[k].r_change;
		return;
	}

	st->xy_coo[k].r = st->xy_coo[k].r + st->xy_coo[k].r_change;

	if (st->xy_coo[k].r >= st->radius)
		st->xy_coo[k].r = st->radius;
	if (st->xy_coo[k].r <= 0)
		st->xy_coo[k].r=0;

	st->from = st->from_array[st->xy_coo[k].r];
}


static unsigned long
distort_draw (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  int k;

  if (st->img_loader)   /* still loading */
    {
      st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
      if (! st->img_loader) {  /* just finished */
        distort_finish_loading (st);
      }
      return st->delay;
    }

  if (!st->img_loader &&
      st->start_time + st->duration < time ((time_t *) 0)) {
    if (st->pm) XFreePixmap (st->dpy, st->pm);
    st->pm = XCreatePixmap (st->dpy, st->window,
                            st->xgwa.width, st->xgwa.height, st->xgwa.depth);
    st->img_loader = load_image_async_simple (0, st->xgwa.screen, st->window,
                                              st->pm, 0, 0);
    return st->delay;
  }

  for (k = 0; k < st->number; k++) {
    st->effect(st,k);
    st->draw(st,k);
  }
  return st->delay;
}

static void
distort_reshape (Display *dpy, Window window, void *closure, 
                 unsigned int w, unsigned int h)
{
  struct state *st = (struct state *) closure;
  XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
  /* XClearWindow (dpy, window); */
  /* Why doesn't this work? */
  if (st->orig_map)  /* created in distort_finish_loading, might be early */
    XPutImage (st->dpy, st->window, st->gc, st->orig_map,
               0, 0, st->orig_map->width, st->orig_map->height, 0, 0);
}

static Bool
distort_event (Display *dpy, Window window, void *closure, XEvent *event)
{
  struct state *st = (struct state *) closure;
  if (screenhack_event_helper (dpy, window, event))
    {
      distort_reset(st);
      return True;
    }
  return False;
}

static void
distort_free (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  int i, j;
  XFreeGC (st->dpy, st->gc);
  if (st->pm) XFreePixmap (dpy, st->pm);
  if (st->orig_map) XDestroyImage (st->orig_map);
  if (st->buffer_map)
    destroy_xshm_image (st->dpy, st->buffer_map, &st->shm_info);
  if (st->fast_from) free (st->fast_from);
  if (st->from_array) free (st->from_array);
  if (st->buffer_map_cache) free (st->buffer_map_cache);

  if (st->from) {
    for (i = 0; i < st->from_size; i++)
      if (st->from[i]) {
        for (j = 0; j < st->from_size; j++) {
          if (st->from[i][j]) free (st->from[i][j]);
        }
        free (st->from[i]);
      }
    free (st->from);
  }

  free (st);
}




static const char *distort_defaults [] = {
	"*dontClearRoot:		True",
	"*background:			Black",
    "*fpsSolid:				true",
#ifdef __sgi    /* really, HAVE_READ_DISPLAY_EXTENSION */
	"*visualID:			Best",
#endif

	"*delay:			20000",
    "*duration:			120",
	"*radius:			0",
	"*speed:			0",
	"*number:			0",
	"*slow:				False",
	"*vortex:			False",
	"*magnify:			False",
	"*reflect:			False",
	"*blackhole:		False",
	"*effect:		    none",
#ifdef HAVE_XSHM_EXTENSION
	"*useSHM:			False",		/* xshm turns out not to help. */
#endif /* HAVE_XSHM_EXTENSION */
#ifdef HAVE_MOBILE
  "*ignoreRotation:     True",
  "*rotateImages:       True",
#endif
	0
};

static XrmOptionDescRec distort_options [] = {
  { "-delay",     ".delay",       XrmoptionSepArg, 0 },
  { "-duration",  ".duration",	  XrmoptionSepArg, 0 },
  { "-radius",    ".radius",      XrmoptionSepArg, 0 },
  { "-speed",     ".speed",       XrmoptionSepArg, 0 },
  { "-number",    ".number",      XrmoptionSepArg, 0 },

  { "-effect",    ".effect",      XrmoptionSepArg, 0 },
  { "-swamp",     ".effect",      XrmoptionNoArg, "swamp"  },
  { "-bounce",    ".effect",      XrmoptionNoArg, "bounce" },

  { "-reflect",   ".reflect",     XrmoptionNoArg, "True" },
  { "-vortex",    ".vortex",      XrmoptionNoArg, "True" },
  { "-magnify",   ".magnify",     XrmoptionNoArg, "True" },
  { "-blackhole", ".blackhole",   XrmoptionNoArg, "True" },
  { "-slow",      ".slow",        XrmoptionNoArg, "True" },
#ifdef HAVE_XSHM_EXTENSION
  { "-shm",       ".useSHM",      XrmoptionNoArg, "True" },
  { "-no-shm",    ".useSHM",      XrmoptionNoArg, "False" },
#endif /* HAVE_XSHM_EXTENSION */
  { 0, 0, 0, 0 }
};

XSCREENSAVER_MODULE ("Distort", distort)