Change to the linux kernel coding style
[wmaker-crm.git] / plugins / libwmfun / wave.c
Commit [+]AuthorDateLineData
7aadc1ef id2000-12-03 18:58:41 +00001/*
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
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020039RImage *waves(int argc, char **argv, int width, int height, int relief)
40{
7aadc1ef id2000-12-03 18:58:41 +000041
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020042 int i, j, k, done, sine[256];
7aadc1ef id2000-12-03 18:58:41 +000043 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;
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020053 for (done = 0; !done;) {
7aadc1ef id2000-12-03 18:58:41 +000054 static struct option long_options[] = {
55 {"from", 1, 0, 'f'},
56 {"to", 1, 0, 't'},
57 {0, 0, 0, 0}
58 };
59
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020060 c = getopt_long(argc, argv, "f:t:", long_options, &option_index);
7aadc1ef id2000-12-03 18:58:41 +000061 if (c == -1) {
62 break;
63 }
64
65 switch (c) {
66 case 'f':
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020067 if (!parse_color(optarg, from)) {
68 error("invalid color: %s\n", optarg);
7aadc1ef id2000-12-03 18:58:41 +000069 return 0;
70 }
71 break;
72 case 't':
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020073 if (!parse_color(optarg, to)) {
74 error("invalid color: %s\n", optarg);
7aadc1ef id2000-12-03 18:58:41 +000075 return 0;
76 }
77 break;
78 default:
79 done = 1;
80 break;
81 }
82 }
83
84 argc -= optind;
85 argv += optind;
86
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020087 if (!start_image("waves", argc, 2, 3, width, height, &image)) {
88 return (RImage *) 0;
7aadc1ef id2000-12-03 18:58:41 +000089 }
90
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020091 layers = atoi(argv[0]);
92 range = atoi(argv[1]);
7aadc1ef id2000-12-03 18:58:41 +000093
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
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200105 for (i = 0; i < 256; i++) {
106 sine[i] = (int)(127.0 * sin(2.0 * M_PI * i / 256)) + 128;
7aadc1ef id2000-12-03 18:58:41 +0000107 }
108
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200109 srand(time(0));
7aadc1ef id2000-12-03 18:58:41 +0000110
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200111 for (i = 0; i < layers; i++) {
112 dx[i] = random_int(range) - range / 2;
113 dy[i] = random_int(range) - range / 2;
7aadc1ef id2000-12-03 18:58:41 +0000114 }
115
116 cptr = image->data;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200117 for (i = 0; i < height; i++) {
118 for (j = 0; j < width; j++) {
7aadc1ef id2000-12-03 18:58:41 +0000119 int output[3], value = 0;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200120 for (k = 0; k < layers; k++) {
121 value += sine[(j * dx[k] + i * dy[k]) & 255];
7aadc1ef id2000-12-03 18:58:41 +0000122 }
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200123 interpolate_color(output, from, to, value / layers);
7aadc1ef id2000-12-03 18:58:41 +0000124 *cptr++ = output[0];
125 *cptr++ = output[1];
126 *cptr++ = output[2];
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200127 if (RRGBAFormat == image->format)
7aadc1ef id2000-12-03 18:58:41 +0000128 cptr++;
129 }
130 }
131
132 return image;
133}