Change to the linux kernel coding style
[wmaker-crm.git] / plugins / libwmfun / fade.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
7aadc1ef id2000-12-03 18:58:41 +000021#include "getopt.h"
22#include <math.h>
23#include <stdlib.h>
24#include <time.h>
25
26#include "generic.h"
27
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020028RImage *fade(int argc, char **argv, int width, int height, int relief)
29{
7aadc1ef id2000-12-03 18:58:41 +000030
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;
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020042 for (done = 0; !done;) {
7aadc1ef id2000-12-03 18:58:41 +000043 static struct option long_options[] = {
44 {"from", 1, 0, 'f'},
45 {"to", 1, 0, 't'},
46 {0, 0, 0, 0}
47 };
48
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020049 c = getopt_long(argc, argv, "f:t:", long_options, &option_index);
7aadc1ef id2000-12-03 18:58:41 +000050 if (c == -1) {
51 break;
52 }
53
54 switch (c) {
55 case 'f':
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020056 if (!parse_color(optarg, from)) {
57 error("invalid color: %s\n", optarg);
7aadc1ef id2000-12-03 18:58:41 +000058 return 0;
59 }
60 break;
61 case 't':
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020062 if (!parse_color(optarg, to)) {
63 error("invalid color: %s\n", optarg);
7aadc1ef id2000-12-03 18:58:41 +000064 return 0;
65 }
66 break;
67 default:
68 done = 1;
69 break;
70 }
71 }
72
73 argc -= optind;
74 argv += optind;
75
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020076 if (!start_image("fade", argc, 0, 1, width, height, &image)) {
77 return (RImage *) 0;
7aadc1ef id2000-12-03 18:58:41 +000078 }
79
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020080 this = (int *)malloc(width * sizeof(int));
81 last = (int *)malloc(width * sizeof(int));
7aadc1ef id2000-12-03 18:58:41 +000082 if (!this || !last) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020083 RReleaseImage(image);
84 free(this);
85 free(last);
86 return (RImage *) 0;
7aadc1ef id2000-12-03 18:58:41 +000087 }
88
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020089 for (i = 0; i < width; i++) {
7aadc1ef id2000-12-03 18:58:41 +000090 this[i] = 255;
91 }
92
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020093 factor = pow(0.2, 1.0 / height);
7aadc1ef id2000-12-03 18:58:41 +000094 delta = (factor < 0.5) ? 2.0 * factor : 2.0 * (1.0 - factor);
95
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020096 srand(time(0));
7aadc1ef id2000-12-03 18:58:41 +000097
98 cptr = image->data;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020099 for (j = 0; j < height; j++) {
100 memcpy(last, this, width * sizeof(int));
101 for (i = 0; i < width; i++) {
7aadc1ef id2000-12-03 18:58:41 +0000102 int output[3];
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200103 int k = i + random_int(3) - 1;
104 double f = factor + random_double(delta) - delta / 2.0;
7aadc1ef id2000-12-03 18:58:41 +0000105
106 if (k < 0) {
107 k = 0;
108 }
109 if (k >= width) {
110 k = width - 1;
111 }
112
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200113 this[i] = (int)(f * last[k]);
114 interpolate_color(output, from, to, this[i]);
7aadc1ef id2000-12-03 18:58:41 +0000115 *cptr++ = output[0];
116 *cptr++ = output[1];
117 *cptr++ = output[2];
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200118 if (RRGBAFormat == image->format)
7aadc1ef id2000-12-03 18:58:41 +0000119 cptr++;
120 }
121 }
122
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200123 free(this);
124 free(last);
7aadc1ef id2000-12-03 18:58:41 +0000125
126 return image;
127}