Initial revision
[wmaker-crm.git] / wrlib / misc.c
blobf1bc286a1a3a1f2e657c52580cbe26fbe6eeea5c
1 /*
2 * Raster graphics library
3 *
4 * Copyright (c) 1997 Alfredo K. Kojima
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <config.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <X11/Xlib.h>
28 #include "wraster.h"
31 int
32 RBevelImage(RImage *image, int bevel_type)
34 RColor color;
35 RColor cdelta;
36 int w, h;
38 if (image->width<3 || image->height<3)
39 return False;
41 w = image->width;
42 h = image->height;
43 if (bevel_type>0) { /* raised */
44 /* top */
45 cdelta.alpha = 0;
46 cdelta.red = cdelta.green = cdelta.blue = 80;
47 ROperateLine(image, RAddOperation, 0, 0, w-1, 0, &cdelta);
48 if (bevel_type==RBEV_RAISED3 && w>3)
49 ROperateLine(image, RAddOperation, 1, 1, w-3, 1,&cdelta);
51 /* left */
52 ROperateLine(image, RAddOperation, 0, 1, 0, h-1, &cdelta);
53 if (bevel_type==RBEV_RAISED3 && h>3)
54 ROperateLine(image, RAddOperation, 1, 2, 1, h-3, &cdelta);
56 /* bottom */
57 color.alpha = 255;
58 color.red = color.green = color.blue = 0;
59 cdelta.red = cdelta.green = cdelta.blue = 40;
60 if (bevel_type==RBEV_RAISED2 || bevel_type==RBEV_RAISED3) {
61 ROperateLine(image, RSubtractOperation, 0, h-2, w-3,
62 h-2, &cdelta);
63 RDrawLine(image, 0, h-1, w-1, h-1, &color);
64 } else {
65 ROperateLine(image, RSubtractOperation, 0, h-1, w-1, h-1,
66 &cdelta);
69 /* right */
70 if (bevel_type==RBEV_RAISED2 || bevel_type==RBEV_RAISED3) {
71 ROperateLine(image, RSubtractOperation, w-2, 0, w-2, h-2,
72 &cdelta);
73 RDrawLine(image, w-1, 0, w-1, h-2, &color);
74 } else {
75 ROperateLine(image, RSubtractOperation, w-1, 0, w-1, h-2,
76 &cdelta);
78 } else { /* sunken */
79 cdelta.alpha = 0;
80 cdelta.red = cdelta.green = cdelta.blue = 40;
81 ROperateLine(image, RSubtractOperation, 0, 0, w-1, 0,
82 &cdelta); /* top */
83 ROperateLine(image, RSubtractOperation, 0, 1, 0, h-1,
84 &cdelta); /* left */
85 cdelta.red = cdelta.green = cdelta.blue = 80;
86 ROperateLine(image, RAddOperation, 0, h-1, w-1, h-1, &cdelta); /* bottom */
87 ROperateLine(image, RAddOperation, w-1, 0, w-1, h-2, &cdelta); /* right */
89 return True;
93 int
94 RClearImage(RImage *image, RColor *color)
96 int bytes;
98 bytes = image->width*image->height;
100 if (color->alpha==255) {
101 memset(image->data[0], color->red, bytes);
102 memset(image->data[1], color->green, bytes);
103 memset(image->data[2], color->blue, bytes);
104 if (image->data[3])
105 memset(image->data[3], 0xff, bytes);
106 } else {
107 register int i;
108 unsigned char *dr, *dg, *db;
109 int alpha, nalpha, r, g, b;
111 dr = image->data[0];
112 dg = image->data[1];
113 db = image->data[2];
115 r = color->red;
116 g = color->green;
117 b = color->blue;
118 alpha = color->alpha;
119 nalpha = 255 - alpha;
121 for (i=0; i<bytes; i++) {
122 *dr = (((int)*dr * nalpha) + (r * alpha))/256;
123 *dg = (((int)*dg * nalpha) + (g * alpha))/256;
124 *db = (((int)*db * nalpha) + (b * alpha))/256;
125 dr++; dg++; db++;
129 return True;