Change to the linux kernel coding style
[wmaker-crm.git] / plugins / libwmfun / wave.c
1 /*
2  *  libwmfun - WindowMaker texture function library
3  *  Copyright (C) 1999 Tobias Gloth
4  * 
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
18  *  USA.
19  */
20
21 /*
22  * $Id$
23  *
24  * $Log$
25  * Revision 1.1  2000/12/03 18:58:41  id
26  * initiate plugins branch
27  *
28  * Revision 1.1.1.1  1999/02/21 17:16:47  gloth
29  * initial revision
30  *
31  */
32
33 #include "getopt.h"
34 #include <math.h>
35 #include <stdlib.h>
36 #include <time.h>
37 #include "generic.h"
38
39 RImage *waves(int argc, char **argv, int width, int height, int relief)
40 {
41
42         int i, j, k, done, sine[256];
43         int from[3] = { 0x00, 0x00, 0x00 };
44         int to[3] = { 0xff, 0xff, 0xff };
45         int layers, range, dx[1000], dy[1000];
46         RImage *image;
47         unsigned char *cptr;
48
49         int option_index = 0;
50         int c;
51
52         optind = 1;
53         for (done = 0; !done;) {
54                 static struct option long_options[] = {
55                         {"from", 1, 0, 'f'},
56                         {"to", 1, 0, 't'},
57                         {0, 0, 0, 0}
58                 };
59
60                 c = getopt_long(argc, argv, "f:t:", long_options, &option_index);
61                 if (c == -1) {
62                         break;
63                 }
64
65                 switch (c) {
66                 case 'f':
67                         if (!parse_color(optarg, from)) {
68                                 error("invalid color: %s\n", optarg);
69                                 return 0;
70                         }
71                         break;
72                 case 't':
73                         if (!parse_color(optarg, to)) {
74                                 error("invalid color: %s\n", optarg);
75                                 return 0;
76                         }
77                         break;
78                 default:
79                         done = 1;
80                         break;
81                 }
82         }
83
84         argc -= optind;
85         argv += optind;
86
87         if (!start_image("waves", argc, 2, 3, width, height, &image)) {
88                 return (RImage *) 0;
89         }
90
91         layers = atoi(argv[0]);
92         range = atoi(argv[1]);
93
94         if (layers <= 0) {
95                 layers = 1;
96         }
97         if (layers > 256) {
98                 layers = 256;
99         }
100
101         if (range <= 0) {
102                 range = 1;
103         }
104
105         for (i = 0; i < 256; i++) {
106                 sine[i] = (int)(127.0 * sin(2.0 * M_PI * i / 256)) + 128;
107         }
108
109         srand(time(0));
110
111         for (i = 0; i < layers; i++) {
112                 dx[i] = random_int(range) - range / 2;
113                 dy[i] = random_int(range) - range / 2;
114         }
115
116         cptr = image->data;
117         for (i = 0; i < height; i++) {
118                 for (j = 0; j < width; j++) {
119                         int output[3], value = 0;
120                         for (k = 0; k < layers; k++) {
121                                 value += sine[(j * dx[k] + i * dy[k]) & 255];
122                         }
123                         interpolate_color(output, from, to, value / layers);
124                         *cptr++ = output[0];
125                         *cptr++ = output[1];
126                         *cptr++ = output[2];
127                         if (RRGBAFormat == image->format)
128                                 cptr++;
129                 }
130         }
131
132         return image;
133 }