Change to the linux kernel coding style
[wmaker-crm.git] / plugins / libwmfun / fade.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 #include "getopt.h"
22 #include <math.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include "generic.h"
27
28 RImage *fade(int argc, char **argv, int width, int height, int relief)
29 {
30
31 int from[3] = { 0x00, 0x00, 0x00 };
32 int to[3] = { 0xff, 0xff, 0xff };
33 int *this, *last;
34 RImage *image;
35 unsigned char *cptr;
36 int i, j;
37 double factor, delta;
38
39 int c, done, option_index = 0;
40
41 optind = 1;
42 for (done = 0; !done;) {
43 static struct option long_options[] = {
44 {"from", 1, 0, 'f'},
45 {"to", 1, 0, 't'},
46 {0, 0, 0, 0}
47 };
48
49 c = getopt_long(argc, argv, "f:t:", long_options, &option_index);
50 if (c == -1) {
51 break;
52 }
53
54 switch (c) {
55 case 'f':
56 if (!parse_color(optarg, from)) {
57 error("invalid color: %s\n", optarg);
58 return 0;
59 }
60 break;
61 case 't':
62 if (!parse_color(optarg, to)) {
63 error("invalid color: %s\n", optarg);
64 return 0;
65 }
66 break;
67 default:
68 done = 1;
69 break;
70 }
71 }
72
73 argc -= optind;
74 argv += optind;
75
76 if (!start_image("fade", argc, 0, 1, width, height, &image)) {
77 return (RImage *) 0;
78 }
79
80 this = (int *)malloc(width * sizeof(int));
81 last = (int *)malloc(width * sizeof(int));
82 if (!this || !last) {
83 RReleaseImage(image);
84 free(this);
85 free(last);
86 return (RImage *) 0;
87 }
88
89 for (i = 0; i < width; i++) {
90 this[i] = 255;
91 }
92
93 factor = pow(0.2, 1.0 / height);
94 delta = (factor < 0.5) ? 2.0 * factor : 2.0 * (1.0 - factor);
95
96 srand(time(0));
97
98 cptr = image->data;
99 for (j = 0; j < height; j++) {
100 memcpy(last, this, width * sizeof(int));
101 for (i = 0; i < width; i++) {
102 int output[3];
103 int k = i + random_int(3) - 1;
104 double f = factor + random_double(delta) - delta / 2.0;
105
106 if (k < 0) {
107 k = 0;
108 }
109 if (k >= width) {
110 k = width - 1;
111 }
112
113 this[i] = (int)(f * last[k]);
114 interpolate_color(output, from, to, this[i]);
115 *cptr++ = output[0];
116 *cptr++ = output[1];
117 *cptr++ = output[2];
118 if (RRGBAFormat == image->format)
119 cptr++;
120 }
121 }
122
123 free(this);
124 free(last);
125
126 return image;
127 }