Updating to version 0.20.2
[wmaker-crm.git] / wrlib / misc.c
blob95f316a6160e4cb71bf0624b497bd15239b3f14f
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 void
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;
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 */
92 void
93 RClearImage(RImage *image, RColor *color)
95 int bytes;
97 bytes = image->width*image->height;
99 if (color->alpha==255) {
100 memset(image->data[0], color->red, bytes);
101 memset(image->data[1], color->green, bytes);
102 memset(image->data[2], color->blue, bytes);
103 if (image->data[3])
104 memset(image->data[3], 0xff, bytes);
105 } else {
106 register int i;
107 unsigned char *dr, *dg, *db;
108 int alpha, nalpha, r, g, b;
110 dr = image->data[0];
111 dg = image->data[1];
112 db = image->data[2];
114 alpha = color->alpha;
115 r = color->red * alpha;
116 g = color->green * alpha;
117 b = color->blue * alpha;
118 nalpha = 255 - alpha;
120 for (i=0; i<bytes; i++) {
121 *dr = (((int)*dr * nalpha) + r)/256;
122 *dg = (((int)*dg * nalpha) + g)/256;
123 *db = (((int)*db * nalpha) + b)/256;
124 dr++; dg++; db++;
130 const char*
131 RMessageForError(int errorCode)
133 switch (errorCode) {
134 case RERR_NONE:
135 return "no error";
136 break;
137 case RERR_OPEN:
138 return "could not open file";
139 break;
140 case RERR_READ:
141 return "error reading from file";
142 break;
143 case RERR_WRITE:
144 return "error writing to file";
145 break;
146 case RERR_NOMEMORY:
147 return "out of memory";
148 break;
149 case RERR_NOCOLOR:
150 return "out of color cells";
151 break;
152 case RERR_BADIMAGEFILE:
153 return "invalid or corrupted image file";
154 break;
155 case RERR_BADFORMAT:
156 return "unsupported image format";
157 break;
158 case RERR_BADINDEX:
159 return "image file does not contain requested image index";
160 break;
161 case RERR_BADVISUALID:
162 return "request for an invalid visual ID";
163 break;
164 case RERR_XERROR:
165 return "internal X error";
166 break;
167 default:
168 case RERR_INTERNAL:
169 return "internal error";
170 break;