Files
wmaker/plugins/libwmfun/fade.c
T

128 lines
2.7 KiB
C
Raw Normal View History

2000-12-03 18:58:41 +00:00
/*
* libwmfun - WindowMaker texture function library
* Copyright (C) 1999 Tobias Gloth
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include "getopt.h"
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "generic.h"
2009-08-20 00:59:40 +02:00
RImage *fade(int argc, char **argv, int width, int height, int relief)
{
2000-12-03 18:58:41 +00:00
int from[3] = { 0x00, 0x00, 0x00 };
int to[3] = { 0xff, 0xff, 0xff };
int *this, *last;
RImage *image;
unsigned char *cptr;
int i, j;
double factor, delta;
int c, done, option_index = 0;
optind = 1;
2009-08-20 00:59:40 +02:00
for (done = 0; !done;) {
2000-12-03 18:58:41 +00:00
static struct option long_options[] = {
{"from", 1, 0, 'f'},
{"to", 1, 0, 't'},
{0, 0, 0, 0}
};
2009-08-20 00:59:40 +02:00
c = getopt_long(argc, argv, "f:t:", long_options, &option_index);
2000-12-03 18:58:41 +00:00
if (c == -1) {
break;
}
switch (c) {
case 'f':
2009-08-20 00:59:40 +02:00
if (!parse_color(optarg, from)) {
error("invalid color: %s\n", optarg);
2000-12-03 18:58:41 +00:00
return 0;
}
break;
case 't':
2009-08-20 00:59:40 +02:00
if (!parse_color(optarg, to)) {
error("invalid color: %s\n", optarg);
2000-12-03 18:58:41 +00:00
return 0;
}
break;
default:
done = 1;
break;
}
}
argc -= optind;
argv += optind;
2009-08-20 00:59:40 +02:00
if (!start_image("fade", argc, 0, 1, width, height, &image)) {
return (RImage *) 0;
2000-12-03 18:58:41 +00:00
}
2009-08-20 00:59:40 +02:00
this = (int *)malloc(width * sizeof(int));
last = (int *)malloc(width * sizeof(int));
2000-12-03 18:58:41 +00:00
if (!this || !last) {
2009-08-20 00:59:40 +02:00
RReleaseImage(image);
free(this);
free(last);
return (RImage *) 0;
2000-12-03 18:58:41 +00:00
}
2009-08-20 00:59:40 +02:00
for (i = 0; i < width; i++) {
2000-12-03 18:58:41 +00:00
this[i] = 255;
}
2009-08-20 00:59:40 +02:00
factor = pow(0.2, 1.0 / height);
2000-12-03 18:58:41 +00:00
delta = (factor < 0.5) ? 2.0 * factor : 2.0 * (1.0 - factor);
2009-08-20 00:59:40 +02:00
srand(time(0));
2000-12-03 18:58:41 +00:00
cptr = image->data;
2009-08-20 00:59:40 +02:00
for (j = 0; j < height; j++) {
memcpy(last, this, width * sizeof(int));
for (i = 0; i < width; i++) {
2000-12-03 18:58:41 +00:00
int output[3];
2009-08-20 00:59:40 +02:00
int k = i + random_int(3) - 1;
double f = factor + random_double(delta) - delta / 2.0;
2000-12-03 18:58:41 +00:00
if (k < 0) {
k = 0;
}
if (k >= width) {
k = width - 1;
}
2009-08-20 00:59:40 +02:00
this[i] = (int)(f * last[k]);
interpolate_color(output, from, to, this[i]);
2000-12-03 18:58:41 +00:00
*cptr++ = output[0];
*cptr++ = output[1];
*cptr++ = output[2];
2009-08-20 00:59:40 +02:00
if (RRGBAFormat == image->format)
2000-12-03 18:58:41 +00:00
cptr++;
}
}
2009-08-20 00:59:40 +02:00
free(this);
free(last);
2000-12-03 18:58:41 +00:00
return image;
}