initiate plugins branch
[wmaker-crm.git] / plugins / libwmfun / fade.c
blobd0a5b266933d5a0b409f4855cb502164fe2c2b8f
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.
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.
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.
22 * $Id$
24 * $Log$
25 * Revision 1.1 2000/12/03 18:58:41 id
26 * initiate plugins branch
28 * Revision 1.1.1.1 1999/02/21 17:16:47 gloth
29 * initial revision
33 #include "getopt.h"
34 #include <math.h>
35 #include <stdlib.h>
36 #include <time.h>
38 #include "generic.h"
40 RImage *fade (int argc, char **argv, int width, int height, int relief) {
42 int from[3] = { 0x00, 0x00, 0x00 };
43 int to[3] = { 0xff, 0xff, 0xff };
44 int *this, *last;
45 RImage *image;
46 unsigned char *cptr;
47 int i, j;
48 double factor, delta;
50 int c, done, option_index = 0;
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}
60 c = getopt_long (argc, argv, "f:t:",
61 long_options, &option_index);
62 if (c == -1) {
63 break;
66 switch (c) {
67 case 'f':
68 if (!parse_color (optarg, from)) {
69 error ("invalid color: %s\n", optarg);
70 return 0;
72 break;
73 case 't':
74 if (!parse_color (optarg, to)) {
75 error ("invalid color: %s\n", optarg);
76 return 0;
78 break;
79 default:
80 done = 1;
81 break;
85 argc -= optind;
86 argv += optind;
88 if (!start_image ("fade", argc, 0, 1, width, height, &image)) {
89 return (RImage *)0;
92 this = (int *) malloc (width * sizeof (int));
93 last = (int *) malloc (width * sizeof (int));
94 if (!this || !last) {
95 RDestroyImage (image);
96 free (this);
97 free (last);
98 return (RImage *)0;
101 for (i=0; i<width; i++) {
102 this[i] = 255;
105 factor = pow (0.2, 1.0 / height);
106 delta = (factor < 0.5) ? 2.0 * factor : 2.0 * (1.0 - factor);
108 srand (time (0));
110 cptr = image->data;
111 for (j=0; j<height; j++) {
112 memcpy (last, this, width * sizeof (int));
113 for (i=0; i<width; i++) {
114 int output[3];
115 int k = i + random_int (3) - 1;
116 double f = factor + random_double (delta) - delta/2.0;
118 if (k < 0) {
119 k = 0;
121 if (k >= width) {
122 k = width - 1;
125 this[i] = (int) (f * last[k]);
126 interpolate_color (output, from, to, this[i]);
127 *cptr++ = output[0];
128 *cptr++ = output[1];
129 *cptr++ = output[2];
130 if (RRGBAFormat==image->format)
131 cptr++;
135 free (this);
136 free (last);
138 return image;