For libwraster:
[wmaker-crm.git] / plugins / libwmfun / fade.c
blob36ae3ec1e62a606fb87278c677a98e7e1bb2a96c
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.2 2001/04/21 07:12:30 dan
26 * For libwraster:
27 * ---------------
29 * - Added retain/release mechanism to RImage by adding RRetainImage() and
30 * RReleaseImage(). RDestroyImage() is an alias to RReleaseImage() now, but
31 * will be removed in a future release because it no longer fits with the
32 * semantics. Will be kept for a while to allow a smoother transition.
33 * More about in wrlib/NEWS
36 * For WINGs:
37 * ----------
39 * - Small API change:
40 * 1. Renamed WMSetApplicationIconImage(), WMGetApplicationIconImage() and
41 * WMSetWindowMiniwindowImage() to respectively WMSetApplicationIconPixmap(),
42 * WMGetApplicationIconPixmap() and WMSetWindowMiniwindowPixmap()
43 * They operate on a WMPixmap which is practically an X Pixmap with no alpha
44 * channel information and the new name is more suggestive and also leaves
45 * room for the new functions added for operating on images with alpha info.
46 * 2. Added WMSetApplicationIconImage() and WMGetApplicationIconImage() which
47 * operate on an RImage and store alpha information too.
48 * 3. Added WMGetApplicationIconBlendedPixmap() which will take the image with
49 * alpha set by WMSetApplicationIconImage() and will blend it with a color.
50 * If color is NULL it will blend using the default panel color (#aeaaae)
51 * All these changes will allow WINGs to handle images with alpha blending
52 * correctly in panels and wherever else needed. More about in WINGs/NEWS.
53 * - updated panels to use the newly available RImages if present and fallback
54 * to old WMPixmaps if not, to properly show alpha blended images.
55 * - replaced some still left malloc's with wmalloc's.
58 * For Window Maker:
59 * -----------------
60 * - Fixed wrong mapping position of the "Docked Applications Panel" for some
61 * icons.
62 * - Smoother animation for the smiley =)
63 * - Made images with alpha blending be shown correctly in the panels and the
64 * icon chooser.
65 * - The icon image set to be shown in panels ("Logo.WMPanel") will be
66 * automatically updated if its entry in WMWindowAttributes changes (without
67 * a need to restart as until now).
70 * *** Note!!! ***
72 * If you are developing applications with one of libwraster or libWINGs
73 * then you should look to wrlib/NEWS and WINGs/NEWS to see what changed
74 * and how should you update your code.
76 * Revision 1.1 2000/12/03 18:58:41 id
77 * initiate plugins branch
79 * Revision 1.1.1.1 1999/02/21 17:16:47 gloth
80 * initial revision
84 #include "getopt.h"
85 #include <math.h>
86 #include <stdlib.h>
87 #include <time.h>
89 #include "generic.h"
91 RImage *fade (int argc, char **argv, int width, int height, int relief) {
93 int from[3] = { 0x00, 0x00, 0x00 };
94 int to[3] = { 0xff, 0xff, 0xff };
95 int *this, *last;
96 RImage *image;
97 unsigned char *cptr;
98 int i, j;
99 double factor, delta;
101 int c, done, option_index = 0;
103 optind = 1;
104 for (done=0; !done; ) {
105 static struct option long_options[] = {
106 {"from", 1, 0, 'f'},
107 {"to", 1, 0, 't'},
108 {0, 0, 0, 0}
111 c = getopt_long (argc, argv, "f:t:",
112 long_options, &option_index);
113 if (c == -1) {
114 break;
117 switch (c) {
118 case 'f':
119 if (!parse_color (optarg, from)) {
120 error ("invalid color: %s\n", optarg);
121 return 0;
123 break;
124 case 't':
125 if (!parse_color (optarg, to)) {
126 error ("invalid color: %s\n", optarg);
127 return 0;
129 break;
130 default:
131 done = 1;
132 break;
136 argc -= optind;
137 argv += optind;
139 if (!start_image ("fade", argc, 0, 1, width, height, &image)) {
140 return (RImage *)0;
143 this = (int *) malloc (width * sizeof (int));
144 last = (int *) malloc (width * sizeof (int));
145 if (!this || !last) {
146 RReleaseImage (image);
147 free (this);
148 free (last);
149 return (RImage *)0;
152 for (i=0; i<width; i++) {
153 this[i] = 255;
156 factor = pow (0.2, 1.0 / height);
157 delta = (factor < 0.5) ? 2.0 * factor : 2.0 * (1.0 - factor);
159 srand (time (0));
161 cptr = image->data;
162 for (j=0; j<height; j++) {
163 memcpy (last, this, width * sizeof (int));
164 for (i=0; i<width; i++) {
165 int output[3];
166 int k = i + random_int (3) - 1;
167 double f = factor + random_double (delta) - delta/2.0;
169 if (k < 0) {
170 k = 0;
172 if (k >= width) {
173 k = width - 1;
176 this[i] = (int) (f * last[k]);
177 interpolate_color (output, from, to, this[i]);
178 *cptr++ = output[0];
179 *cptr++ = output[1];
180 *cptr++ = output[2];
181 if (RRGBAFormat==image->format)
182 cptr++;
186 free (this);
187 free (last);
189 return image;